diff --git a/core/src/conntrack/pdu.rs b/core/src/conntrack/pdu.rs index 47737b14..d5e7c61b 100644 --- a/core/src/conntrack/pdu.rs +++ b/core/src/conntrack/pdu.rs @@ -7,12 +7,10 @@ use crate::protocols::packet::ipv4::Ipv4; use crate::protocols::packet::ipv6::Ipv6; use crate::protocols::packet::tcp::{Tcp, TCP_PROTOCOL}; use crate::protocols::packet::udp::{Udp, UDP_PROTOCOL}; -use crate::protocols::packet::Packet; +use crate::protocols::packet::{Packet, PacketParseError}; use std::time::Instant; -use anyhow::{bail, Result}; - use std::net::{IpAddr, SocketAddr}; /// Transport-layer protocol data unit for stream reassembly and application-layer protocol parsing. @@ -119,7 +117,7 @@ pub struct L4Context { } impl L4Context { - pub fn new(mbuf: &Mbuf) -> Result { + pub fn new(mbuf: &Mbuf) -> Result { if let Ok(eth) = mbuf.parse_to::() { if let Ok(ipv4) = eth.parse_to::() { if let Ok(tcp) = ipv4.parse_to::() { @@ -139,7 +137,7 @@ impl L4Context { app_offset: None, }) } else { - bail!("Malformed Packet"); + Err(PacketParseError::InvalidRead) } } else if let Ok(udp) = ipv4.parse_to::() { if let Some(payload_size) = (ipv4.total_length() as usize) @@ -158,10 +156,10 @@ impl L4Context { app_offset: None, }) } else { - bail!("Malformed Packet"); + Err(PacketParseError::InvalidRead) } } else { - bail!("Not TCP or UDP"); + Err(PacketParseError::InvalidProtocol) } } else if let Ok(ipv6) = eth.parse_to::() { if let Ok(tcp) = ipv6.parse_to::() { @@ -181,7 +179,7 @@ impl L4Context { app_offset: None, }) } else { - bail!("Malformed Packet"); + Err(PacketParseError::InvalidRead) } } else if let Ok(udp) = ipv6.parse_to::() { if let Some(payload_size) = @@ -200,16 +198,16 @@ impl L4Context { app_offset: None, }) } else { - bail!("Malformed Packet"); + Err(PacketParseError::InvalidRead) } } else { - bail!("Not TCP or UDP"); + Err(PacketParseError::InvalidProtocol) } } else { - bail!("Not IP"); + Err(PacketParseError::InvalidProtocol) } } else { - bail!("Not Ethernet"); + Err(PacketParseError::InvalidProtocol) } } } diff --git a/core/src/memory/mbuf.rs b/core/src/memory/mbuf.rs index 906cda9d..68798d62 100644 --- a/core/src/memory/mbuf.rs +++ b/core/src/memory/mbuf.rs @@ -122,15 +122,18 @@ impl Mbuf { /// Reads the data at `offset` as `T` and returns it as a raw pointer. Errors if `offset` is /// greater than or equal to the buffer length or the size of `T` exceeds the size of the data /// stored at `offset`. - pub(crate) fn get_data(&self, offset: usize) -> Result<*const T> { + pub(crate) fn get_data( + &self, + offset: usize, + ) -> Result<*const T, PacketParseError> { if offset < self.data_len() { if offset + T::size_of() <= self.data_len() { Ok(self.get_data_address(offset) as *const T) } else { - bail!(MbufError::ReadPastBuffer) + Err(PacketParseError::InvalidRead) } } else { - bail!(MbufError::BadOffset) + Err(PacketParseError::InvalidRead) } } @@ -182,12 +185,12 @@ impl<'a> Packet<'a> for Mbuf { None } - fn parse_from(_outer: &'a impl Packet<'a>) -> Result + fn parse_from(_outer: &'a impl Packet<'a>) -> Result where Self: Sized, { // parse_from should never be called for Mbuf. - bail!(PacketParseError::InvalidProtocol) + Err(PacketParseError::InvalidProtocol) } } diff --git a/core/src/protocols/packet/ethernet.rs b/core/src/protocols/packet/ethernet.rs index d91ea005..98c1947d 100644 --- a/core/src/protocols/packet/ethernet.rs +++ b/core/src/protocols/packet/ethernet.rs @@ -4,7 +4,6 @@ use crate::memory::mbuf::Mbuf; use crate::protocols::packet::{Packet, PacketHeader, PacketParseError}; use crate::utils::types::*; -use anyhow::{bail, Result}; use pnet::datalink::MacAddr; const VLAN_802_1Q: u16 = 0x8100; @@ -99,7 +98,7 @@ impl<'a> Packet<'a> for Ethernet<'a> { } } - fn parse_from(outer: &'a impl Packet<'a>) -> Result + fn parse_from(outer: &'a impl Packet<'a>) -> Result where Self: Sized, { @@ -110,7 +109,7 @@ impl<'a> Packet<'a> for Ethernet<'a> { mbuf: outer.mbuf(), }) } else { - bail!(PacketParseError::InvalidRead) + Err(PacketParseError::InvalidRead) } } } diff --git a/core/src/protocols/packet/ipv4.rs b/core/src/protocols/packet/ipv4.rs index 64377912..393fba82 100644 --- a/core/src/protocols/packet/ipv4.rs +++ b/core/src/protocols/packet/ipv4.rs @@ -6,8 +6,6 @@ use crate::utils::types::*; use std::net::Ipv4Addr; -use anyhow::{bail, Result}; - /// IPv4 EtherType const IPV4_PROTOCOL: usize = 0x0800; /// Flag: "Reserved bit" @@ -171,7 +169,7 @@ impl<'a> Packet<'a> for Ipv4<'a> { Some(self.protocol().into()) } - fn parse_from(outer: &'a impl Packet<'a>) -> Result + fn parse_from(outer: &'a impl Packet<'a>) -> Result where Self: Sized, { @@ -183,10 +181,10 @@ impl<'a> Packet<'a> for Ipv4<'a> { offset, mbuf: outer.mbuf(), }), - _ => bail!(PacketParseError::InvalidProtocol), + _ => Err(PacketParseError::InvalidProtocol), } } else { - bail!(PacketParseError::InvalidRead) + Err(PacketParseError::InvalidRead) } } } diff --git a/core/src/protocols/packet/ipv6.rs b/core/src/protocols/packet/ipv6.rs index 69f2a563..478a0f26 100644 --- a/core/src/protocols/packet/ipv6.rs +++ b/core/src/protocols/packet/ipv6.rs @@ -6,8 +6,6 @@ use crate::utils::types::*; use std::net::Ipv6Addr; -use anyhow::{bail, Result}; - const IPV6_PROTOCOL: usize = 0x86DD; const IPV6_HEADER_LEN: usize = 40; @@ -113,7 +111,7 @@ impl<'a> Packet<'a> for Ipv6<'a> { Some(self.next_header().into()) } - fn parse_from(outer: &'a impl Packet<'a>) -> Result + fn parse_from(outer: &'a impl Packet<'a>) -> Result where Self: Sized, { @@ -125,10 +123,10 @@ impl<'a> Packet<'a> for Ipv6<'a> { offset, mbuf: outer.mbuf(), }), - _ => bail!(PacketParseError::InvalidProtocol), + _ => Err(PacketParseError::InvalidProtocol), } } else { - bail!(PacketParseError::InvalidRead) + Err(PacketParseError::InvalidRead) } } } diff --git a/core/src/protocols/packet/mod.rs b/core/src/protocols/packet/mod.rs index fd48f207..12124478 100644 --- a/core/src/protocols/packet/mod.rs +++ b/core/src/protocols/packet/mod.rs @@ -12,7 +12,6 @@ pub mod tcp; pub mod udp; use crate::memory::mbuf::Mbuf; -use anyhow::Result; use thiserror::Error; /// Represents a single packet. @@ -30,7 +29,9 @@ pub trait Packet<'a> { fn next_header(&self) -> Option; /// Parses the `Packet`'s payload as a new `Packet` of type `T`. - fn parse_to>(&'a self) -> Result + /// + /// Returns the concrete [`PacketParseError`] if parsing fails. + fn parse_to>(&'a self) -> Result where Self: Sized, { @@ -38,7 +39,7 @@ pub trait Packet<'a> { } /// Parses a `Packet` from the outer encapsulating `Packet`'s payload. - fn parse_from(outer: &'a impl Packet<'a>) -> Result + fn parse_from(outer: &'a impl Packet<'a>) -> Result where Self: Sized; } @@ -59,7 +60,7 @@ pub trait PacketHeader { } #[derive(Error, Debug)] -pub(crate) enum PacketParseError { +pub enum PacketParseError { #[error("Invalid protocol")] InvalidProtocol, diff --git a/core/src/protocols/packet/tcp.rs b/core/src/protocols/packet/tcp.rs index a1b2081f..e4e5fd8d 100644 --- a/core/src/protocols/packet/tcp.rs +++ b/core/src/protocols/packet/tcp.rs @@ -4,8 +4,6 @@ use crate::memory::mbuf::Mbuf; use crate::protocols::packet::{Packet, PacketHeader, PacketParseError}; use crate::utils::types::*; -use anyhow::{bail, Result}; - /// TCP assigned protocol number. pub const TCP_PROTOCOL: usize = 6; @@ -179,7 +177,7 @@ impl<'a> Packet<'a> for Tcp<'a> { None } - fn parse_from(outer: &'a impl Packet<'a>) -> Result + fn parse_from(outer: &'a impl Packet<'a>) -> Result where Self: Sized, { @@ -191,10 +189,10 @@ impl<'a> Packet<'a> for Tcp<'a> { offset, mbuf: outer.mbuf(), }), - _ => bail!(PacketParseError::InvalidProtocol), + _ => Err(PacketParseError::InvalidProtocol), } } else { - bail!(PacketParseError::InvalidRead) + Err(PacketParseError::InvalidRead) } } } diff --git a/core/src/protocols/packet/udp.rs b/core/src/protocols/packet/udp.rs index 84f8616f..5c35faac 100644 --- a/core/src/protocols/packet/udp.rs +++ b/core/src/protocols/packet/udp.rs @@ -4,8 +4,6 @@ use crate::memory::mbuf::Mbuf; use crate::protocols::packet::{Packet, PacketHeader, PacketParseError}; use crate::utils::types::*; -use anyhow::{bail, Result}; - /// UDP assigned protocol number. pub const UDP_PROTOCOL: usize = 17; const UDP_HEADER_LEN: usize = 8; @@ -64,7 +62,7 @@ impl<'a> Packet<'a> for Udp<'a> { None } - fn parse_from(outer: &'a impl Packet<'a>) -> Result + fn parse_from(outer: &'a impl Packet<'a>) -> Result where Self: Sized, { @@ -76,10 +74,10 @@ impl<'a> Packet<'a> for Udp<'a> { offset, mbuf: outer.mbuf(), }), - _ => bail!(PacketParseError::InvalidProtocol), + _ => Err(PacketParseError::InvalidProtocol), } } else { - bail!(PacketParseError::InvalidRead) + Err(PacketParseError::InvalidRead) } } }