Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions core/src/conntrack/pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -119,7 +117,7 @@ pub struct L4Context {
}

impl L4Context {
pub fn new(mbuf: &Mbuf) -> Result<Self> {
pub fn new(mbuf: &Mbuf) -> Result<Self, PacketParseError> {
if let Ok(eth) = mbuf.parse_to::<Ethernet>() {
if let Ok(ipv4) = eth.parse_to::<Ipv4>() {
if let Ok(tcp) = ipv4.parse_to::<Tcp>() {
Expand All @@ -139,7 +137,7 @@ impl L4Context {
app_offset: None,
})
} else {
bail!("Malformed Packet");
Err(PacketParseError::InvalidRead)
}
} else if let Ok(udp) = ipv4.parse_to::<Udp>() {
if let Some(payload_size) = (ipv4.total_length() as usize)
Expand All @@ -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::<Ipv6>() {
if let Ok(tcp) = ipv6.parse_to::<Tcp>() {
Expand All @@ -181,7 +179,7 @@ impl L4Context {
app_offset: None,
})
} else {
bail!("Malformed Packet");
Err(PacketParseError::InvalidRead)
}
} else if let Ok(udp) = ipv6.parse_to::<Udp>() {
if let Some(payload_size) =
Expand All @@ -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)
}
}
}
13 changes: 8 additions & 5 deletions core/src/memory/mbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: PacketHeader>(&self, offset: usize) -> Result<*const T> {
pub(crate) fn get_data<T: PacketHeader>(
&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)
}
}

Expand Down Expand Up @@ -182,12 +185,12 @@ impl<'a> Packet<'a> for Mbuf {
None
}

fn parse_from(_outer: &'a impl Packet<'a>) -> Result<Self>
fn parse_from(_outer: &'a impl Packet<'a>) -> Result<Self, PacketParseError>
where
Self: Sized,
{
// parse_from should never be called for Mbuf.
bail!(PacketParseError::InvalidProtocol)
Err(PacketParseError::InvalidProtocol)
}
}

Expand Down
5 changes: 2 additions & 3 deletions core/src/protocols/packet/ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,7 +98,7 @@ impl<'a> Packet<'a> for Ethernet<'a> {
}
}

fn parse_from(outer: &'a impl Packet<'a>) -> Result<Self>
fn parse_from(outer: &'a impl Packet<'a>) -> Result<Self, PacketParseError>
where
Self: Sized,
{
Expand All @@ -110,7 +109,7 @@ impl<'a> Packet<'a> for Ethernet<'a> {
mbuf: outer.mbuf(),
})
} else {
bail!(PacketParseError::InvalidRead)
Err(PacketParseError::InvalidRead)
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions core/src/protocols/packet/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -171,7 +169,7 @@ impl<'a> Packet<'a> for Ipv4<'a> {
Some(self.protocol().into())
}

fn parse_from(outer: &'a impl Packet<'a>) -> Result<Self>
fn parse_from(outer: &'a impl Packet<'a>) -> Result<Self, PacketParseError>
where
Self: Sized,
{
Expand All @@ -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)
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions core/src/protocols/packet/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Self>
fn parse_from(outer: &'a impl Packet<'a>) -> Result<Self, PacketParseError>
where
Self: Sized,
{
Expand All @@ -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)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions core/src/protocols/packet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -30,15 +29,17 @@ pub trait Packet<'a> {
fn next_header(&self) -> Option<usize>;

/// Parses the `Packet`'s payload as a new `Packet` of type `T`.
fn parse_to<T: Packet<'a>>(&'a self) -> Result<T>
///
/// Returns the concrete [`PacketParseError`] if parsing fails.
fn parse_to<T: Packet<'a>>(&'a self) -> Result<T, PacketParseError>
where
Self: Sized,
{
T::parse_from(self)
}

/// Parses a `Packet` from the outer encapsulating `Packet`'s payload.
fn parse_from(outer: &'a impl Packet<'a>) -> Result<Self>
fn parse_from(outer: &'a impl Packet<'a>) -> Result<Self, PacketParseError>
where
Self: Sized;
}
Expand All @@ -59,7 +60,7 @@ pub trait PacketHeader {
}

#[derive(Error, Debug)]
pub(crate) enum PacketParseError {
pub enum PacketParseError {
#[error("Invalid protocol")]
InvalidProtocol,

Expand Down
8 changes: 3 additions & 5 deletions core/src/protocols/packet/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -179,7 +177,7 @@ impl<'a> Packet<'a> for Tcp<'a> {
None
}

fn parse_from(outer: &'a impl Packet<'a>) -> Result<Self>
fn parse_from(outer: &'a impl Packet<'a>) -> Result<Self, PacketParseError>
where
Self: Sized,
{
Expand All @@ -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)
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions core/src/protocols/packet/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +62,7 @@ impl<'a> Packet<'a> for Udp<'a> {
None
}

fn parse_from(outer: &'a impl Packet<'a>) -> Result<Self>
fn parse_from(outer: &'a impl Packet<'a>) -> Result<Self, PacketParseError>
where
Self: Sized,
{
Expand All @@ -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)
}
}
}
Expand Down
Loading