use net_types::ethernet::Mac;
use net_types::ip::{Ip, IpVersion, Ipv4, Ipv6};
use packet::{
BufferView, BufferViewMut, FragmentedBytesMut, PacketBuilder, PacketConstraints,
ParsablePacket, ParseMetadata, SerializeTarget,
};
use zerocopy::byteorder::network_endian::{U16, U32};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, SplitByteSlice, Unaligned};
use crate::error::{ParseError, ParseResult};
const ETHERNET_MIN_ILLEGAL_ETHERTYPE: u16 = 1501;
const ETHERNET_MAX_ILLEGAL_ETHERTYPE: u16 = 1535;
create_protocol_enum!(
#[allow(missing_docs)]
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
pub enum EtherType: u16 {
Ipv4, 0x0800, "IPv4";
Arp, 0x0806, "ARP";
Ipv6, 0x86DD, "IPv6";
_, "EtherType {}";
}
);
impl EtherType {
pub fn from_ip_version(ip_version: IpVersion) -> Self {
match ip_version {
IpVersion::V4 => EtherType::Ipv4,
IpVersion::V6 => EtherType::Ipv6,
}
}
}
pub trait EthernetIpExt: Ip {
const ETHER_TYPE: EtherType;
}
impl EthernetIpExt for Ipv4 {
const ETHER_TYPE: EtherType = EtherType::Ipv4;
}
impl EthernetIpExt for Ipv6 {
const ETHER_TYPE: EtherType = EtherType::Ipv6;
}
#[derive(KnownLayout, FromBytes, IntoBytes, Immutable, Unaligned)]
#[repr(C)]
struct HeaderPrefix {
dst_mac: Mac,
src_mac: Mac,
}
const TPID_8021Q: u16 = 0x8100;
const TPID_8021AD: u16 = 0x88a8;
pub struct EthernetFrame<B> {
hdr_prefix: Ref<B, HeaderPrefix>,
tag: Option<Ref<B, U32>>,
ethertype: Ref<B, U16>,
body: B,
}
#[derive(PartialEq)]
pub enum EthernetFrameLengthCheck {
Check,
NoCheck,
}
impl<B: SplitByteSlice> ParsablePacket<B, EthernetFrameLengthCheck> for EthernetFrame<B> {
type Error = ParseError;
fn parse_metadata(&self) -> ParseMetadata {
let header_len = Ref::bytes(&self.hdr_prefix).len()
+ self.tag.as_ref().map(|tag| Ref::bytes(tag).len()).unwrap_or(0)
+ Ref::bytes(&self.ethertype).len();
ParseMetadata::from_packet(header_len, self.body.len(), 0)
}
fn parse<BV: BufferView<B>>(
mut buffer: BV,
length_check: EthernetFrameLengthCheck,
) -> ParseResult<Self> {
let hdr_prefix = buffer
.take_obj_front::<HeaderPrefix>()
.ok_or_else(debug_err_fn!(ParseError::Format, "too few bytes for header"))?;
if length_check == EthernetFrameLengthCheck::Check && buffer.len() < 48 {
return debug_err!(Err(ParseError::Format), "too few bytes for frame");
}
let ethertype_or_tpid = buffer
.peek_obj_front::<U16>()
.ok_or_else(debug_err_fn!(ParseError::Format, "too few bytes for header"))?
.get();
let (tag, ethertype, body) = match ethertype_or_tpid {
self::TPID_8021Q | self::TPID_8021AD => (
Some(
buffer.take_obj_front().ok_or_else(debug_err_fn!(
ParseError::Format,
"too few bytes for header"
))?,
),
buffer
.take_obj_front()
.ok_or_else(debug_err_fn!(ParseError::Format, "too few bytes for header"))?,
buffer.into_rest(),
),
_ => (
None,
buffer
.take_obj_front()
.ok_or_else(debug_err_fn!(ParseError::Format, "too few bytes for header"))?,
buffer.into_rest(),
),
};
let frame = EthernetFrame { hdr_prefix, tag, ethertype, body };
let et = frame.ethertype.get();
if (ETHERNET_MIN_ILLEGAL_ETHERTYPE..=ETHERNET_MAX_ILLEGAL_ETHERTYPE).contains(&et)
|| (et < ETHERNET_MIN_ILLEGAL_ETHERTYPE && et as usize != frame.body.len())
{
return debug_err!(Err(ParseError::Format), "invalid ethertype number: {:x}", et);
}
Ok(frame)
}
}
impl<B: SplitByteSlice> EthernetFrame<B> {
pub fn body(&self) -> &[u8] {
&self.body
}
pub fn into_body(self) -> B
where
B: Copy,
{
self.body
}
pub fn src_mac(&self) -> Mac {
self.hdr_prefix.src_mac
}
pub fn dst_mac(&self) -> Mac {
self.hdr_prefix.dst_mac
}
pub fn ethertype(&self) -> Option<EtherType> {
let et = self.ethertype.get();
if et < ETHERNET_MIN_ILLEGAL_ETHERTYPE {
return None;
}
debug_assert!(et > ETHERNET_MAX_ILLEGAL_ETHERTYPE);
Some(EtherType::from(et))
}
fn header_len(&self) -> usize {
Ref::bytes(&self.hdr_prefix).len()
+ self.tag.as_ref().map(|t| Ref::bytes(t).len()).unwrap_or(0)
+ Ref::bytes(&self.ethertype).len()
}
#[allow(dead_code)]
fn total_frame_len(&self) -> usize {
self.header_len() + self.body.len()
}
pub fn builder(&self) -> EthernetFrameBuilder {
EthernetFrameBuilder {
src_mac: self.src_mac(),
dst_mac: self.dst_mac(),
ethertype: self.ethertype.get(),
min_body_len: ETHERNET_MIN_BODY_LEN_NO_TAG,
}
}
}
#[derive(Debug)]
pub struct EthernetFrameBuilder {
src_mac: Mac,
dst_mac: Mac,
ethertype: u16,
min_body_len: usize,
}
impl EthernetFrameBuilder {
pub fn new(
src_mac: Mac,
dst_mac: Mac,
ethertype: EtherType,
min_body_len: usize,
) -> EthernetFrameBuilder {
EthernetFrameBuilder { src_mac, dst_mac, ethertype: ethertype.into(), min_body_len }
}
}
impl PacketBuilder for EthernetFrameBuilder {
fn constraints(&self) -> PacketConstraints {
PacketConstraints::new(ETHERNET_HDR_LEN_NO_TAG, 0, self.min_body_len, core::usize::MAX)
}
fn serialize(&self, target: &mut SerializeTarget<'_>, body: FragmentedBytesMut<'_, '_>) {
let total_len = target.header.len() + body.len();
let mut header = &mut target.header;
header
.write_obj_front(&HeaderPrefix { src_mac: self.src_mac, dst_mac: self.dst_mac })
.expect("too few bytes for Ethernet header");
header
.write_obj_front(&U16::new(self.ethertype))
.expect("too few bytes for Ethernet header");
let min_frame_size = self.min_body_len + ETHERNET_HDR_LEN_NO_TAG;
assert!(
total_len >= min_frame_size,
"total frame size of {} bytes is below minimum frame size of {}",
total_len,
min_frame_size,
);
}
}
pub const ETHERNET_HDR_LEN_NO_TAG: usize = 14;
pub const ETHERNET_MIN_BODY_LEN_NO_TAG: usize = 46;
pub mod testutil {
pub use super::{ETHERNET_HDR_LEN_NO_TAG, ETHERNET_MIN_BODY_LEN_NO_TAG};
pub const ETHERNET_DST_MAC_BYTE_OFFSET: usize = 0;
pub const ETHERNET_SRC_MAC_BYTE_OFFSET: usize = 6;
}
#[cfg(test)]
mod tests {
use byteorder::{ByteOrder, NetworkEndian};
use packet::{
AsFragmentedByteSlice, Buf, GrowBufferMut, InnerPacketBuilder, ParseBuffer, Serializer,
};
use super::*;
const DEFAULT_DST_MAC: Mac = Mac::new([0, 1, 2, 3, 4, 5]);
const DEFAULT_SRC_MAC: Mac = Mac::new([6, 7, 8, 9, 10, 11]);
const ETHERNET_ETHERTYPE_BYTE_OFFSET: usize = 12;
const ETHERNET_MIN_FRAME_LEN: usize = 60;
fn new_parse_buf() -> ([u8; ETHERNET_MIN_FRAME_LEN], [u8; ETHERNET_MIN_BODY_LEN_NO_TAG]) {
let mut buf = [0; ETHERNET_MIN_FRAME_LEN];
for (i, elem) in buf.iter_mut().enumerate() {
*elem = i as u8;
}
NetworkEndian::write_u16(&mut buf[ETHERNET_ETHERTYPE_BYTE_OFFSET..], EtherType::Arp.into());
let mut body = [0; ETHERNET_MIN_BODY_LEN_NO_TAG];
(&mut body).copy_from_slice(&buf[ETHERNET_HDR_LEN_NO_TAG..]);
(buf, body)
}
fn new_serialize_buf() -> [u8; ETHERNET_MIN_BODY_LEN_NO_TAG] {
let mut buf = [0; ETHERNET_MIN_BODY_LEN_NO_TAG];
for (i, elem) in buf.iter_mut().enumerate() {
*elem = i as u8;
}
buf
}
#[test]
fn test_parse() {
crate::testutil::set_logger_for_test();
let (mut backing_buf, body) = new_parse_buf();
let mut buf = &mut backing_buf[..];
let frame = buf.parse_with::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::Check).unwrap();
assert_eq!(frame.hdr_prefix.dst_mac, DEFAULT_DST_MAC);
assert_eq!(frame.hdr_prefix.src_mac, DEFAULT_SRC_MAC);
assert!(frame.tag.is_none());
assert_eq!(frame.ethertype(), Some(EtherType::Arp));
assert_eq!(frame.body(), &body[..]);
let mut buf = &mut backing_buf[..ETHERNET_HDR_LEN_NO_TAG];
let frame =
buf.parse_with::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::NoCheck).unwrap();
assert_eq!(frame.hdr_prefix.dst_mac, DEFAULT_DST_MAC);
assert_eq!(frame.hdr_prefix.src_mac, DEFAULT_SRC_MAC);
assert!(frame.tag.is_none());
assert_eq!(frame.ethertype(), Some(EtherType::Arp));
assert_eq!(frame.body(), &[]);
for tpid in [TPID_8021Q, TPID_8021AD].iter() {
let (mut buf, body) = new_parse_buf();
let mut buf = &mut buf[..];
const TPID_OFFSET: usize = 12;
NetworkEndian::write_u16(&mut buf[TPID_OFFSET..], *tpid);
NetworkEndian::write_u16(&mut buf[TPID_OFFSET + 4..], EtherType::Arp.into());
let frame =
buf.parse_with::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::Check).unwrap();
assert_eq!(frame.hdr_prefix.dst_mac, DEFAULT_DST_MAC);
assert_eq!(frame.hdr_prefix.src_mac, DEFAULT_SRC_MAC);
assert_eq!(frame.ethertype(), Some(EtherType::Arp));
let tag: &U32 = frame.tag.as_ref().unwrap();
let want_tag =
u32::from(*tpid) << 16 | ((TPID_OFFSET as u32 + 2) << 8) | (TPID_OFFSET as u32 + 3);
assert_eq!(tag.get(), want_tag);
assert_eq!(frame.body(), &body[4..]);
}
}
#[test]
fn test_ethertype() {
let mut buf = [0u8; 1014];
NetworkEndian::write_u16(&mut buf[ETHERNET_ETHERTYPE_BYTE_OFFSET..], 1001);
assert!((&mut buf[..])
.parse_with::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::Check)
.is_err());
NetworkEndian::write_u16(&mut buf[ETHERNET_ETHERTYPE_BYTE_OFFSET..], 1000);
assert_eq!(
(&mut buf[..])
.parse_with::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::Check)
.unwrap()
.ethertype(),
None
);
let mut buf = [0u8; 1014];
NetworkEndian::write_u16(
&mut buf[ETHERNET_ETHERTYPE_BYTE_OFFSET..],
ETHERNET_MAX_ILLEGAL_ETHERTYPE + 1,
);
assert_eq!(
(&mut buf[..])
.parse_with::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::Check)
.unwrap()
.ethertype(),
Some(EtherType::Other(ETHERNET_MAX_ILLEGAL_ETHERTYPE + 1))
);
}
#[test]
fn test_serialize() {
let buf = (&new_serialize_buf()[..])
.into_serializer()
.encapsulate(EthernetFrameBuilder::new(
DEFAULT_DST_MAC,
DEFAULT_SRC_MAC,
EtherType::Arp,
ETHERNET_MIN_BODY_LEN_NO_TAG,
))
.serialize_vec_outer()
.unwrap();
assert_eq!(
&buf.as_ref()[..ETHERNET_HDR_LEN_NO_TAG],
[6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 0x08, 0x06]
);
}
#[test]
fn test_serialize_zeroes() {
let mut buf_0 = [0; ETHERNET_MIN_FRAME_LEN];
let _: Buf<&mut [u8]> = Buf::new(&mut buf_0[..], ETHERNET_HDR_LEN_NO_TAG..)
.encapsulate(EthernetFrameBuilder::new(
DEFAULT_SRC_MAC,
DEFAULT_DST_MAC,
EtherType::Arp,
ETHERNET_MIN_BODY_LEN_NO_TAG,
))
.serialize_vec_outer()
.unwrap()
.unwrap_a();
let mut buf_1 = [0; ETHERNET_MIN_FRAME_LEN];
(&mut buf_1[..ETHERNET_HDR_LEN_NO_TAG]).copy_from_slice(&[0xFF; ETHERNET_HDR_LEN_NO_TAG]);
let _: Buf<&mut [u8]> = Buf::new(&mut buf_1[..], ETHERNET_HDR_LEN_NO_TAG..)
.encapsulate(EthernetFrameBuilder::new(
DEFAULT_SRC_MAC,
DEFAULT_DST_MAC,
EtherType::Arp,
ETHERNET_MIN_BODY_LEN_NO_TAG,
))
.serialize_vec_outer()
.unwrap()
.unwrap_a();
assert_eq!(&buf_0[..], &buf_1[..]);
}
#[test]
fn test_parse_error() {
let mut buf = [0u8; ETHERNET_MIN_FRAME_LEN - 1];
assert!((&mut buf[..])
.parse_with::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::Check)
.is_err());
let mut buf = [0u8; ETHERNET_HDR_LEN_NO_TAG - 1];
assert!((&mut buf[..])
.parse_with::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::NoCheck)
.is_err());
let mut buf = [0u8; ETHERNET_MIN_FRAME_LEN];
NetworkEndian::write_u16(
&mut buf[ETHERNET_ETHERTYPE_BYTE_OFFSET..],
ETHERNET_MIN_ILLEGAL_ETHERTYPE - 1,
);
assert!((&mut buf[..])
.parse_with::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::Check)
.is_err());
let mut buf = [0u8; ETHERNET_MIN_FRAME_LEN];
NetworkEndian::write_u16(
&mut buf[ETHERNET_ETHERTYPE_BYTE_OFFSET..],
ETHERNET_MIN_ILLEGAL_ETHERTYPE,
);
assert!((&mut buf[..])
.parse_with::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::Check)
.is_err());
let mut buf = [0u8; ETHERNET_MIN_FRAME_LEN];
NetworkEndian::write_u16(
&mut buf[ETHERNET_ETHERTYPE_BYTE_OFFSET..],
ETHERNET_MAX_ILLEGAL_ETHERTYPE,
);
assert!((&mut buf[..])
.parse_with::<_, EthernetFrame<_>>(EthernetFrameLengthCheck::Check)
.is_err());
}
#[test]
#[should_panic(expected = "bytes is below minimum frame size of")]
fn test_serialize_panic() {
let mut buf = [0u8; ETHERNET_MIN_FRAME_LEN - 1];
let mut b = [&mut buf[..]];
let buf = b.as_fragmented_byte_slice();
let (header, body, footer) = buf.try_split_contiguous(ETHERNET_HDR_LEN_NO_TAG..).unwrap();
EthernetFrameBuilder::new(
Mac::new([0, 1, 2, 3, 4, 5]),
Mac::new([6, 7, 8, 9, 10, 11]),
EtherType::Arp,
ETHERNET_MIN_BODY_LEN_NO_TAG,
)
.serialize(&mut SerializeTarget { header, footer }, body);
}
#[test]
fn test_custom_min_body_len() {
const MIN_BODY_LEN: usize = 4;
const UNWRITTEN_BYTE: u8 = 0xAA;
let builder = EthernetFrameBuilder::new(
Mac::new([0, 1, 2, 3, 4, 5]),
Mac::new([6, 7, 8, 9, 10, 11]),
EtherType::Arp,
MIN_BODY_LEN,
);
let mut buffer = [UNWRITTEN_BYTE; ETHERNET_MIN_FRAME_LEN];
GrowBufferMut::serialize(
&mut Buf::new(&mut buffer[..], ETHERNET_HDR_LEN_NO_TAG..ETHERNET_HDR_LEN_NO_TAG),
builder,
);
let (header, tail) = buffer.split_at(ETHERNET_HDR_LEN_NO_TAG);
let (padding, unwritten) = tail.split_at(MIN_BODY_LEN);
assert_eq!(
header,
&[
6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 08, 06, ]
);
assert_eq!(padding, &[0; MIN_BODY_LEN]);
assert_eq!(
unwritten,
&[UNWRITTEN_BYTE; ETHERNET_MIN_FRAME_LEN - MIN_BODY_LEN - ETHERNET_HDR_LEN_NO_TAG]
);
}
}