diff --git a/netlink-packet-netfilter/examples/nflog.rs b/netlink-packet-netfilter/examples/nflog.rs index 44965ad5..42f53182 100644 --- a/netlink-packet-netfilter/examples/nflog.rs +++ b/netlink-packet-netfilter/examples/nflog.rs @@ -13,15 +13,17 @@ use std::{net::Ipv4Addr, time::Duration}; use byteorder::{ByteOrder, NetworkEndian}; use netlink_packet_netfilter::{ constants::*, - message::{NetfilterMessage, NetfilterMessageInner}, nflog::{ - self, - config::{ConfigCmd, ConfigFlags, ConfigMode, Timeout}, - packet::PacketNla, + config_request, + nlas::{ + config::{ConfigCmd, ConfigFlags, ConfigMode, Timeout}, + packet::PacketNla, + }, NfLogMessage, }, - NetlinkMessage, - NetlinkPayload, + nl::{NetlinkMessage, NetlinkPayload}, + NetfilterMessage, + NetfilterMessageInner, }; use netlink_sys::{constants::NETLINK_NETFILTER, Socket}; @@ -45,7 +47,7 @@ fn main() { socket.bind_auto().unwrap(); // Then we issue the PfBind command - let packet = nflog::config::config_request(AF_INET, 0, vec![ConfigCmd::PfBind.into()]); + let packet = config_request(AF_INET, 0, vec![ConfigCmd::PfBind.into()]); let mut buf = vec![0; packet.header.length as usize]; packet.serialize(&mut buf[..]); println!(">>> {:?}", packet); @@ -60,7 +62,7 @@ fn main() { // After that we issue a Bind command, to start receiving packets. We can also set various parameters at the same time let timeout: Timeout = Duration::from_millis(100).into(); - let packet = nflog::config::config_request( + let packet = config_request( AF_INET, 1, vec![ @@ -92,7 +94,7 @@ fn main() { let rx_packet = >::deserialize(bytes).unwrap(); for nla in get_packet_nlas(&rx_packet) { - if let nflog::packet::PacketNla::Payload(payload) = nla { + if let PacketNla::Payload(payload) = nla { let src = Ipv4Addr::from(NetworkEndian::read_u32(&payload[12..])); let dst = Ipv4Addr::from(NetworkEndian::read_u32(&payload[16..])); println!("Packet from {} to {}", src, dst); diff --git a/netlink-packet-netfilter/src/constants.rs b/netlink-packet-netfilter/src/constants.rs index fe4915a6..49c80c43 100644 --- a/netlink-packet-netfilter/src/constants.rs +++ b/netlink-packet-netfilter/src/constants.rs @@ -56,3 +56,34 @@ pub const NFNL_SUBSYS_CTNETLINK_TIMEOUT: u8 = libc::NFNL_SUBSYS_CTNETLINK_TIMEOU pub const NFNL_SUBSYS_CTHELPER: u8 = libc::NFNL_SUBSYS_CTHELPER as u8; pub const NFNL_SUBSYS_NFTABLES: u8 = libc::NFNL_SUBSYS_NFTABLES as u8; pub const NFNL_SUBSYS_NFT_COMPAT: u8 = libc::NFNL_SUBSYS_NFT_COMPAT as u8; + +pub const NFULA_CFG_CMD: u16 = libc::NFULA_CFG_CMD as u16; +pub const NFULA_CFG_MODE: u16 = libc::NFULA_CFG_MODE as u16; +pub const NFULA_CFG_NLBUFSIZ: u16 = libc::NFULA_CFG_NLBUFSIZ as u16; +pub const NFULA_CFG_TIMEOUT: u16 = libc::NFULA_CFG_TIMEOUT as u16; +pub const NFULA_CFG_QTHRESH: u16 = libc::NFULA_CFG_QTHRESH as u16; +pub const NFULA_CFG_FLAGS: u16 = libc::NFULA_CFG_FLAGS as u16; +pub const NLBUFSIZ_MAX: u32 = 131072; + +pub const NFULA_PACKET_HDR: u16 = libc::NFULA_PACKET_HDR as u16; +pub const NFULA_MARK: u16 = libc::NFULA_MARK as u16; +pub const NFULA_TIMESTAMP: u16 = libc::NFULA_TIMESTAMP as u16; +pub const NFULA_IFINDEX_INDEV: u16 = libc::NFULA_IFINDEX_INDEV as u16; +pub const NFULA_IFINDEX_OUTDEV: u16 = libc::NFULA_IFINDEX_OUTDEV as u16; +pub const NFULA_IFINDEX_PHYSINDEV: u16 = libc::NFULA_IFINDEX_PHYSINDEV as u16; +pub const NFULA_IFINDEX_PHYSOUTDEV: u16 = libc::NFULA_IFINDEX_PHYSOUTDEV as u16; +pub const NFULA_HWADDR: u16 = libc::NFULA_HWADDR as u16; +pub const NFULA_PAYLOAD: u16 = libc::NFULA_PAYLOAD as u16; +pub const NFULA_PREFIX: u16 = libc::NFULA_PREFIX as u16; +pub const NFULA_UID: u16 = libc::NFULA_UID as u16; +pub const NFULA_SEQ: u16 = libc::NFULA_SEQ as u16; +pub const NFULA_SEQ_GLOBAL: u16 = libc::NFULA_SEQ_GLOBAL as u16; +pub const NFULA_GID: u16 = libc::NFULA_GID as u16; +pub const NFULA_HWTYPE: u16 = libc::NFULA_HWTYPE as u16; +pub const NFULA_HWHEADER: u16 = libc::NFULA_HWHEADER as u16; +pub const NFULA_HWLEN: u16 = libc::NFULA_HWLEN as u16; +pub const NFULA_CT: u16 = libc::NFULA_CT as u16; +pub const NFULA_CT_INFO: u16 = libc::NFULA_CT_INFO as u16; + +pub const NFULNL_MSG_CONFIG: u8 = libc::NFULNL_MSG_CONFIG as u8; +pub const NFULNL_MSG_PACKET: u8 = libc::NFULNL_MSG_PACKET as u8; diff --git a/netlink-packet-netfilter/src/lib.rs b/netlink-packet-netfilter/src/lib.rs index ee5f7e3a..d0b8d07c 100644 --- a/netlink-packet-netfilter/src/lib.rs +++ b/netlink-packet-netfilter/src/lib.rs @@ -1,16 +1,12 @@ // SPDX-License-Identifier: MIT +pub extern crate netlink_packet_core as nl; pub(crate) extern crate netlink_packet_utils as utils; -pub use self::utils::{traits, DecodeError}; -pub use netlink_packet_core::{ - ErrorMessage, - NetlinkBuffer, - NetlinkHeader, - NetlinkMessage, - NetlinkPayload, -}; + +pub use self::utils::{nla, traits, DecodeError}; pub(crate) mod buffer; pub mod constants; -pub mod message; +mod message; +pub use message::{NetfilterHeader, NetfilterMessage, NetfilterMessageInner}; pub mod nflog; diff --git a/netlink-packet-netfilter/src/message.rs b/netlink-packet-netfilter/src/message.rs index b19a015f..01a87a4c 100644 --- a/netlink-packet-netfilter/src/message.rs +++ b/netlink-packet-netfilter/src/message.rs @@ -93,7 +93,6 @@ impl Emitable for NetfilterMessageInner { } #[derive(Debug, PartialEq, Eq, Clone)] - pub struct NetfilterMessage { pub header: NetfilterHeader, pub inner: NetfilterMessageInner, diff --git a/netlink-packet-netfilter/src/nflog.rs b/netlink-packet-netfilter/src/nflog/message.rs similarity index 72% rename from netlink-packet-netfilter/src/nflog.rs rename to netlink-packet-netfilter/src/nflog/message.rs index fd9bfe53..d0ddbeb7 100644 --- a/netlink-packet-netfilter/src/nflog.rs +++ b/netlink-packet-netfilter/src/nflog/message.rs @@ -1,25 +1,11 @@ -// SPDX-License-Identifier: MIT - -use netlink_packet_core::DecodeError; -use netlink_packet_utils::{nla::DefaultNla, Emitable, Parseable, ParseableParametrized}; -use std::fmt::Debug; - -use crate::{buffer::NetfilterBuffer, constants::NFNL_SUBSYS_ULOG}; - -use config::ConfigNla; - -use self::packet::PacketNla; - -pub const NFULNL_MSG_CONFIG: u8 = libc::NFULNL_MSG_CONFIG as u8; -pub const NFULNL_MSG_PACKET: u8 = libc::NFULNL_MSG_PACKET as u8; - -pub const NFULA_CFG_CMD: u16 = libc::NFULA_CFG_CMD as u16; -pub const NFULA_CFG_MODE: u16 = libc::NFULA_CFG_MODE as u16; -pub const NFULA_CFG_NLBUFSIZ: u16 = libc::NFULA_CFG_NLBUFSIZ as u16; -pub const NFULA_CFG_QTHRESH: u16 = libc::NFULA_CFG_QTHRESH as u16; - -pub mod config; -pub mod packet; +use crate::{ + buffer::NetfilterBuffer, + constants::{NFNL_SUBSYS_ULOG, NFULNL_MSG_CONFIG, NFULNL_MSG_PACKET}, + nflog::nlas::{config::ConfigNla, packet::PacketNla}, + nla::DefaultNla, + traits::{Emitable, Parseable, ParseableParametrized}, + DecodeError, +}; #[derive(Debug, PartialEq, Eq, Clone)] pub enum NfLogMessage { diff --git a/netlink-packet-netfilter/src/nflog/mod.rs b/netlink-packet-netfilter/src/nflog/mod.rs new file mode 100644 index 00000000..445eb318 --- /dev/null +++ b/netlink-packet-netfilter/src/nflog/mod.rs @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: MIT + +mod message; +pub use message::NfLogMessage; +pub mod nlas; + +use crate::{ + constants::NFNETLINK_V0, + nflog::nlas::config::ConfigNla, + nl::{NetlinkHeader, NetlinkMessage, NetlinkPayload, NLM_F_ACK, NLM_F_REQUEST}, + NetfilterHeader, + NetfilterMessage, +}; + +pub fn config_request( + family: u8, + group_num: u16, + nlas: Vec, +) -> NetlinkMessage { + let mut message = NetlinkMessage { + header: NetlinkHeader { + flags: NLM_F_REQUEST | NLM_F_ACK, + ..Default::default() + }, + payload: NetlinkPayload::from(NetfilterMessage::new( + NetfilterHeader::new(family, NFNETLINK_V0, group_num), + NfLogMessage::Config(nlas), + )), + }; + message.finalize(); + message +} diff --git a/netlink-packet-netfilter/src/nflog/config/config_cmd.rs b/netlink-packet-netfilter/src/nflog/nlas/config/config_cmd.rs similarity index 100% rename from netlink-packet-netfilter/src/nflog/config/config_cmd.rs rename to netlink-packet-netfilter/src/nflog/nlas/config/config_cmd.rs diff --git a/netlink-packet-netfilter/src/nflog/config/config_flags.rs b/netlink-packet-netfilter/src/nflog/nlas/config/config_flags.rs similarity index 100% rename from netlink-packet-netfilter/src/nflog/config/config_flags.rs rename to netlink-packet-netfilter/src/nflog/nlas/config/config_flags.rs diff --git a/netlink-packet-netfilter/src/nflog/config/config_mode.rs b/netlink-packet-netfilter/src/nflog/nlas/config/config_mode.rs similarity index 100% rename from netlink-packet-netfilter/src/nflog/config/config_mode.rs rename to netlink-packet-netfilter/src/nflog/nlas/config/config_mode.rs diff --git a/netlink-packet-netfilter/src/nflog/nlas/config/mod.rs b/netlink-packet-netfilter/src/nflog/nlas/config/mod.rs new file mode 100644 index 00000000..3e1d84f6 --- /dev/null +++ b/netlink-packet-netfilter/src/nflog/nlas/config/mod.rs @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +mod config_cmd; +mod config_flags; +mod config_mode; +mod nla; +mod timeout; + +pub use config_cmd::ConfigCmd; +pub use config_flags::ConfigFlags; +pub use config_mode::{ConfigMode, CopyMode}; +pub use nla::ConfigNla; +pub use timeout::Timeout; diff --git a/netlink-packet-netfilter/src/nflog/config.rs b/netlink-packet-netfilter/src/nflog/nlas/config/nla.rs similarity index 66% rename from netlink-packet-netfilter/src/nflog/config.rs rename to netlink-packet-netfilter/src/nflog/nlas/config/nla.rs index 75a08e71..c989b2f5 100644 --- a/netlink-packet-netfilter/src/nflog/config.rs +++ b/netlink-packet-netfilter/src/nflog/nlas/config/nla.rs @@ -1,48 +1,29 @@ -// SPDX-License-Identifier: MIT - use anyhow::Context; use byteorder::{BigEndian, ByteOrder}; use derive_more::{From, IsVariant}; -use netlink_packet_core::{ - DecodeError, - NetlinkHeader, - NetlinkMessage, - NetlinkPayload, - NLM_F_ACK, - NLM_F_REQUEST, -}; -use netlink_packet_utils::{ - nla::{DefaultNla, Nla, NlaBuffer}, - parsers::{parse_u16_be, parse_u32_be, parse_u8}, - Parseable, -}; - -pub use config_cmd::ConfigCmd; -pub use config_flags::ConfigFlags; -pub use config_mode::{ConfigMode, CopyMode}; -pub use timeout::Timeout; use crate::{ - constants::NFNETLINK_V0, - message::{NetfilterHeader, NetfilterMessage}, + constants::{ + NFULA_CFG_CMD, + NFULA_CFG_FLAGS, + NFULA_CFG_MODE, + NFULA_CFG_NLBUFSIZ, + NFULA_CFG_QTHRESH, + NFULA_CFG_TIMEOUT, + }, + nflog::nlas::config::{ + config_mode::ConfigModeBuffer, + ConfigCmd, + ConfigFlags, + ConfigMode, + Timeout, + }, + nl::DecodeError, + nla::{DefaultNla, Nla, NlaBuffer}, + traits::Parseable, + utils::parsers::{parse_u16_be, parse_u32_be, parse_u8}, }; -use super::NfLogMessage; - -mod config_cmd; -mod config_flags; -mod config_mode; -mod timeout; - -pub const NFULA_CFG_CMD: u16 = libc::NFULA_CFG_CMD as u16; -pub const NFULA_CFG_MODE: u16 = libc::NFULA_CFG_MODE as u16; -pub const NFULA_CFG_NLBUFSIZ: u16 = libc::NFULA_CFG_NLBUFSIZ as u16; -pub const NFULA_CFG_TIMEOUT: u16 = libc::NFULA_CFG_TIMEOUT as u16; -pub const NFULA_CFG_QTHRESH: u16 = libc::NFULA_CFG_QTHRESH as u16; -pub const NFULA_CFG_FLAGS: u16 = libc::NFULA_CFG_FLAGS as u16; - -pub const NLBUFSIZ_MAX: u32 = 131072; - #[derive(Clone, Debug, PartialEq, Eq, From, IsVariant)] pub enum ConfigNla { Cmd(ConfigCmd), @@ -103,7 +84,7 @@ impl<'buffer, T: AsRef<[u8]> + ?Sized> Parseable> for Conf ConfigCmd::from(parse_u8(payload).context("invalid NFULA_CFG_CMD value")?).into() } NFULA_CFG_MODE => { - let buf = config_mode::ConfigModeBuffer::new_checked(payload)?; + let buf = ConfigModeBuffer::new_checked(payload)?; ConfigMode::parse(&buf)?.into() } NFULA_CFG_NLBUFSIZ => ConfigNla::NlBufSiz( @@ -125,22 +106,3 @@ impl<'buffer, T: AsRef<[u8]> + ?Sized> Parseable> for Conf Ok(nla) } } - -pub fn config_request( - family: u8, - group_num: u16, - nlas: Vec, -) -> NetlinkMessage { - let mut message = NetlinkMessage { - header: NetlinkHeader { - flags: NLM_F_REQUEST | NLM_F_ACK, - ..Default::default() - }, - payload: NetlinkPayload::from(NetfilterMessage::new( - NetfilterHeader::new(family, NFNETLINK_V0, group_num), - NfLogMessage::Config(nlas), - )), - }; - message.finalize(); - message -} diff --git a/netlink-packet-netfilter/src/nflog/config/timeout.rs b/netlink-packet-netfilter/src/nflog/nlas/config/timeout.rs similarity index 100% rename from netlink-packet-netfilter/src/nflog/config/timeout.rs rename to netlink-packet-netfilter/src/nflog/nlas/config/timeout.rs diff --git a/netlink-packet-netfilter/src/nflog/nlas/mod.rs b/netlink-packet-netfilter/src/nflog/nlas/mod.rs new file mode 100644 index 00000000..94d9be55 --- /dev/null +++ b/netlink-packet-netfilter/src/nflog/nlas/mod.rs @@ -0,0 +1,2 @@ +pub mod config; +pub mod packet; diff --git a/netlink-packet-netfilter/src/nflog/packet/hw_addr.rs b/netlink-packet-netfilter/src/nflog/nlas/packet/hw_addr.rs similarity index 92% rename from netlink-packet-netfilter/src/nflog/packet/hw_addr.rs rename to netlink-packet-netfilter/src/nflog/nlas/packet/hw_addr.rs index b8b90fc4..5176215a 100644 --- a/netlink-packet-netfilter/src/nflog/packet/hw_addr.rs +++ b/netlink-packet-netfilter/src/nflog/nlas/packet/hw_addr.rs @@ -1,9 +1,6 @@ // SPDX-License-Identifier: MIT -use netlink_packet_core::DecodeError; -use netlink_packet_utils::{buffer, nla::Nla, Parseable}; - -use super::NFULA_HWADDR; +use crate::{constants::NFULA_HWADDR, nla::Nla, traits::Parseable, utils::buffer, DecodeError}; const HW_ADDR_LEN: usize = 12; diff --git a/netlink-packet-netfilter/src/nflog/nlas/packet/mod.rs b/netlink-packet-netfilter/src/nflog/nlas/packet/mod.rs new file mode 100644 index 00000000..dd4c4aea --- /dev/null +++ b/netlink-packet-netfilter/src/nflog/nlas/packet/mod.rs @@ -0,0 +1,9 @@ +mod hw_addr; +mod nla; +mod packet_hdr; +mod timestamp; + +pub use hw_addr::{HwAddr, HwAddrBuffer}; +pub use nla::PacketNla; +pub use packet_hdr::{PacketHdr, PacketHdrBuffer}; +pub use timestamp::{TimeStamp, TimeStampBuffer}; diff --git a/netlink-packet-netfilter/src/nflog/packet.rs b/netlink-packet-netfilter/src/nflog/nlas/packet/nla.rs similarity index 82% rename from netlink-packet-netfilter/src/nflog/packet.rs rename to netlink-packet-netfilter/src/nflog/nlas/packet/nla.rs index 5f2058da..737b24ca 100644 --- a/netlink-packet-netfilter/src/nflog/packet.rs +++ b/netlink-packet-netfilter/src/nflog/nlas/packet/nla.rs @@ -5,43 +5,38 @@ use std::ffi::{CStr, CString}; use anyhow::Context; use byteorder::{BigEndian, ByteOrder}; use derive_more::{From, IsVariant}; -use netlink_packet_core::DecodeError; -use netlink_packet_utils::{ - nla::{DefaultNla, Nla, NlaBuffer}, - parsers::{parse_u16_be, parse_u32_be}, - Parseable, -}; -use self::{ - hw_addr::{HwAddr, HwAddrBuffer}, - packet_hdr::{PacketHdr, PacketHdrBuffer}, - timestamp::{TimeStamp, TimeStampBuffer}, +use crate::{ + constants::{ + NFULA_GID, + NFULA_HWADDR, + NFULA_HWHEADER, + NFULA_HWLEN, + NFULA_HWTYPE, + NFULA_IFINDEX_INDEV, + NFULA_IFINDEX_OUTDEV, + NFULA_IFINDEX_PHYSINDEV, + NFULA_IFINDEX_PHYSOUTDEV, + NFULA_MARK, + NFULA_PACKET_HDR, + NFULA_PAYLOAD, + NFULA_PREFIX, + NFULA_SEQ, + NFULA_SEQ_GLOBAL, + NFULA_TIMESTAMP, + NFULA_UID, + }, + nflog::nlas::packet::{ + hw_addr::{HwAddr, HwAddrBuffer}, + packet_hdr::{PacketHdr, PacketHdrBuffer}, + timestamp::{TimeStamp, TimeStampBuffer}, + }, + nla::{DefaultNla, Nla, NlaBuffer}, + traits::Parseable, + utils::parsers::{parse_u16_be, parse_u32_be}, + DecodeError, }; -mod hw_addr; -mod packet_hdr; -mod timestamp; - -pub const NFULA_PACKET_HDR: u16 = libc::NFULA_PACKET_HDR as u16; -pub const NFULA_MARK: u16 = libc::NFULA_MARK as u16; -pub const NFULA_TIMESTAMP: u16 = libc::NFULA_TIMESTAMP as u16; -pub const NFULA_IFINDEX_INDEV: u16 = libc::NFULA_IFINDEX_INDEV as u16; -pub const NFULA_IFINDEX_OUTDEV: u16 = libc::NFULA_IFINDEX_OUTDEV as u16; -pub const NFULA_IFINDEX_PHYSINDEV: u16 = libc::NFULA_IFINDEX_PHYSINDEV as u16; -pub const NFULA_IFINDEX_PHYSOUTDEV: u16 = libc::NFULA_IFINDEX_PHYSOUTDEV as u16; -pub const NFULA_HWADDR: u16 = libc::NFULA_HWADDR as u16; -pub const NFULA_PAYLOAD: u16 = libc::NFULA_PAYLOAD as u16; -pub const NFULA_PREFIX: u16 = libc::NFULA_PREFIX as u16; -pub const NFULA_UID: u16 = libc::NFULA_UID as u16; -pub const NFULA_SEQ: u16 = libc::NFULA_SEQ as u16; -pub const NFULA_SEQ_GLOBAL: u16 = libc::NFULA_SEQ_GLOBAL as u16; -pub const NFULA_GID: u16 = libc::NFULA_GID as u16; -pub const NFULA_HWTYPE: u16 = libc::NFULA_HWTYPE as u16; -pub const NFULA_HWHEADER: u16 = libc::NFULA_HWHEADER as u16; -pub const NFULA_HWLEN: u16 = libc::NFULA_HWLEN as u16; -pub const NFULA_CT: u16 = libc::NFULA_CT as u16; -pub const NFULA_CT_INFO: u16 = libc::NFULA_CT_INFO as u16; - #[derive(Clone, Debug, PartialEq, Eq, From, IsVariant)] pub enum PacketNla { #[from] diff --git a/netlink-packet-netfilter/src/nflog/packet/packet_hdr.rs b/netlink-packet-netfilter/src/nflog/nlas/packet/packet_hdr.rs similarity index 100% rename from netlink-packet-netfilter/src/nflog/packet/packet_hdr.rs rename to netlink-packet-netfilter/src/nflog/nlas/packet/packet_hdr.rs diff --git a/netlink-packet-netfilter/src/nflog/packet/timestamp.rs b/netlink-packet-netfilter/src/nflog/nlas/packet/timestamp.rs similarity index 96% rename from netlink-packet-netfilter/src/nflog/packet/timestamp.rs rename to netlink-packet-netfilter/src/nflog/nlas/packet/timestamp.rs index e195d7e3..2ca3f542 100644 --- a/netlink-packet-netfilter/src/nflog/packet/timestamp.rs +++ b/netlink-packet-netfilter/src/nflog/nlas/packet/timestamp.rs @@ -3,7 +3,7 @@ use netlink_packet_core::DecodeError; use netlink_packet_utils::{buffer, nla::Nla, Parseable}; -use super::NFULA_TIMESTAMP; +use crate::constants::NFULA_TIMESTAMP; const TIMESTAMP_LEN: usize = 16;