Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macros for ICMP and ICMPv6 payloads #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions nftnl/src/expr/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl HeaderField for Ipv6HeaderField {
pub enum TransportHeaderField {
Tcp(TcpHeaderField),
Udp(UdpHeaderField),
Icmp(IcmpHeaderField),
Icmpv6(Icmpv6HeaderField),
}

Expand All @@ -194,6 +195,7 @@ impl HeaderField for TransportHeaderField {
match *self {
Tcp(ref f) => f.offset(),
Udp(ref f) => f.offset(),
Icmp(ref f) => f.offset(),
Icmpv6(ref f) => f.offset(),
}
}
Expand All @@ -203,6 +205,7 @@ impl HeaderField for TransportHeaderField {
match *self {
Tcp(ref f) => f.len(),
Udp(ref f) => f.len(),
Icmp(ref f) => f.len(),
Icmpv6(ref f) => f.len(),
}
}
Expand Down Expand Up @@ -261,6 +264,34 @@ impl HeaderField for UdpHeaderField {
}
}

#[derive(Copy, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum IcmpHeaderField {
Type,
Code,
Checksum,
}

impl HeaderField for IcmpHeaderField {
fn offset(&self) -> u32 {
use self::IcmpHeaderField::*;
match *self {
Type => 0,
Code => 1,
Checksum => 2,
}
}

fn len(&self) -> u32 {
use self::IcmpHeaderField::*;
match *self {
Type => 1,
Code => 1,
Checksum => 2,
}
}
}

#[derive(Copy, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum Icmpv6HeaderField {
Expand Down Expand Up @@ -334,6 +365,20 @@ macro_rules! nft_expr_payload {
$crate::expr::UdpHeaderField::Len
};

(@icmp_field icmptype) => {
$crate::expr::IcmpHeaderField::Type
};
(@icmp_field code) => {
$crate::expr::IcmpHeaderField::Code
};

(@icmpv6_field icmptype) => {
$crate::expr::Icmpv6HeaderField::Type
};
(@icmpv6_field code) => {
$crate::expr::Icmpv6HeaderField::Code
};

(ethernet daddr) => {
$crate::expr::Payload::LinkLayer($crate::expr::LLHeaderField::Daddr)
};
Expand Down Expand Up @@ -365,4 +410,15 @@ macro_rules! nft_expr_payload {
nft_expr_payload!(@udp_field $field),
))
};

(icmp $field:ident) => {
$crate::expr::Payload::Transport($crate::expr::TransportHeaderField::Icmp(
nft_expr_payload!(@icmp_field $field),
))
};
(icmpv6 $field:ident) => {
$crate::expr::Payload::Transport($crate::expr::TransportHeaderField::Icmpv6(
nft_expr_payload!(@icmpv6_field $field),
))
};
}