From e93465ce84b19a85fbfd285206779f296e0e5286 Mon Sep 17 00:00:00 2001 From: Martin Andre Date: Wed, 31 Aug 2022 17:14:49 +0200 Subject: [PATCH 1/3] dhcp: initial support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (see https://github.com/libpnet/libpnet/pull/386) Signed-off-by: Martin André --- pnet_packet/src/dhcp.rs | 130 ++++++++++++++++++++++++++++++++++++++++ pnet_packet/src/lib.rs | 1 + 2 files changed, 131 insertions(+) create mode 100644 pnet_packet/src/dhcp.rs diff --git a/pnet_packet/src/dhcp.rs b/pnet_packet/src/dhcp.rs new file mode 100644 index 00000000..89c561df --- /dev/null +++ b/pnet_packet/src/dhcp.rs @@ -0,0 +1,130 @@ +use crate::PrimitiveValues; + +use alloc::vec::Vec; + +use pnet_base::core_net::Ipv4Addr; +use pnet_base::MacAddr; +use pnet_macros::packet; +use pnet_macros_support::types::*; + +/// Represents an Dhcp operation. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct DhcpOperation(pub u8); + +impl DhcpOperation { + /// Create a new `ArpOperation`. + pub fn new(value: u8) -> Self { + DhcpOperation(value) + } +} + +impl PrimitiveValues for DhcpOperation { + type T = (u8,); + fn to_primitive_values(&self) -> (u8,) { + (self.0,) + } +} + +/// The Dhcp protocol operations. +#[allow(non_snake_case)] +#[allow(non_upper_case_globals)] +pub mod DhcpOperations { + use super::DhcpOperation; + + /// DHCP request + pub const Request: DhcpOperation = DhcpOperation(1); + + /// Dhcp reply + pub const Reply: DhcpOperation = DhcpOperation(2); +} + +/// Represents the Dhcp hardware types. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct DhcpHardwareType(pub u8); + +impl DhcpHardwareType { + /// Create a new `DhcpHardwareType`. + pub fn new(value: u8) -> Self { + DhcpHardwareType(value) + } +} + +impl PrimitiveValues for DhcpHardwareType { + type T = (u8,); + fn to_primitive_values(&self) -> (u8,) { + (self.0,) + } +} + +/// The Dhcp protocol hardware types. +#[allow(non_snake_case)] +#[allow(non_upper_case_globals)] +pub mod DhcpHardwareTypes { + use super::DhcpHardwareType; + + /// Ethernet + pub const Ethernet: DhcpHardwareType = DhcpHardwareType(1); +} + +/// Represents the Dhcp magic cookie. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct DhcpMagicCookie(pub u32); + +impl DhcpMagicCookie { + /// Create a new `DhcpMagicCookie`. + pub fn new(value: u32) -> Self { + DhcpMagicCookie(value) + } +} + +impl PrimitiveValues for DhcpMagicCookie { + type T = (u32,); + fn to_primitive_values(&self) -> (u32,) { + (self.0,) + } +} + +/// The Dhcp protocol hardware types. +#[allow(non_snake_case)] +#[allow(non_upper_case_globals)] +pub mod DhcpMagicCookies { + use super::DhcpMagicCookie; + + /// Cookie default + pub const cookie: DhcpMagicCookie = DhcpMagicCookie(0x63825363); +} + +/// Represents an DHCP Packet. +#[packet] +#[allow(non_snake_case)] +pub struct Dhcp { + #[construct_with(u8)] + pub op_code: DhcpOperation, + #[construct_with(u8)] + pub hw_type: DhcpHardwareType, + pub hw_address_length: u8, + pub hop_count: u8, + pub transaction_id: u32be, + pub number_of_seconds: u16be, + pub flags: u16be, + #[construct_with(u8, u8, u8, u8)] + pub client_ip_addr: Ipv4Addr, + #[construct_with(u8, u8, u8, u8)] + pub your_ip_addr: Ipv4Addr, + #[construct_with(u8, u8, u8, u8)] + pub server_ip_addr: Ipv4Addr, + #[construct_with(u8, u8, u8, u8)] + pub reply_agent_ip_addr: Ipv4Addr, + #[construct_with(u8, u8, u8, u8, u8, u8)] + pub client_hw_addr: MacAddr, + #[length = "10"] + pub client_hw_addr_pad: Vec, + #[length = "64"] + pub server_host_name: Vec, + #[length = "128"] + pub boot_file_name: Vec, + #[construct_with(u32)] + pub magic_cookie: DhcpMagicCookie, + #[payload] + pub payload: Vec, +} \ No newline at end of file diff --git a/pnet_packet/src/lib.rs b/pnet_packet/src/lib.rs index 85720ef9..a96dc69e 100644 --- a/pnet_packet/src/lib.rs +++ b/pnet_packet/src/lib.rs @@ -34,5 +34,6 @@ pub mod tcp; pub mod udp; pub mod usbpcap; pub mod vlan; +pub mod dhcp; pub mod util; From db655b49e0951434323ab8a01e3f638e9b5fea31 Mon Sep 17 00:00:00 2001 From: Martin Andre Date: Wed, 31 Aug 2022 21:24:55 +0200 Subject: [PATCH 2/3] dhcp: change struct layout to match RFC 2131 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin André --- pnet_packet/src/dhcp.rs | 60 +++++++++++------------------------------ 1 file changed, 15 insertions(+), 45 deletions(-) diff --git a/pnet_packet/src/dhcp.rs b/pnet_packet/src/dhcp.rs index 89c561df..86f77811 100644 --- a/pnet_packet/src/dhcp.rs +++ b/pnet_packet/src/dhcp.rs @@ -66,65 +66,35 @@ pub mod DhcpHardwareTypes { pub const Ethernet: DhcpHardwareType = DhcpHardwareType(1); } -/// Represents the Dhcp magic cookie. -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DhcpMagicCookie(pub u32); - -impl DhcpMagicCookie { - /// Create a new `DhcpMagicCookie`. - pub fn new(value: u32) -> Self { - DhcpMagicCookie(value) - } -} - -impl PrimitiveValues for DhcpMagicCookie { - type T = (u32,); - fn to_primitive_values(&self) -> (u32,) { - (self.0,) - } -} - -/// The Dhcp protocol hardware types. -#[allow(non_snake_case)] -#[allow(non_upper_case_globals)] -pub mod DhcpMagicCookies { - use super::DhcpMagicCookie; - - /// Cookie default - pub const cookie: DhcpMagicCookie = DhcpMagicCookie(0x63825363); -} - /// Represents an DHCP Packet. #[packet] #[allow(non_snake_case)] pub struct Dhcp { #[construct_with(u8)] - pub op_code: DhcpOperation, + pub op: DhcpOperation, #[construct_with(u8)] - pub hw_type: DhcpHardwareType, - pub hw_address_length: u8, - pub hop_count: u8, - pub transaction_id: u32be, - pub number_of_seconds: u16be, + pub htype: DhcpHardwareType, + pub hlen: u8, + pub hops: u8, + pub xid: u32be, + pub secs: u16be, pub flags: u16be, #[construct_with(u8, u8, u8, u8)] - pub client_ip_addr: Ipv4Addr, + pub ciaddr: Ipv4Addr, #[construct_with(u8, u8, u8, u8)] - pub your_ip_addr: Ipv4Addr, + pub yiaddr: Ipv4Addr, #[construct_with(u8, u8, u8, u8)] - pub server_ip_addr: Ipv4Addr, + pub siaddr: Ipv4Addr, #[construct_with(u8, u8, u8, u8)] - pub reply_agent_ip_addr: Ipv4Addr, + pub giaddr: Ipv4Addr, #[construct_with(u8, u8, u8, u8, u8, u8)] - pub client_hw_addr: MacAddr, + pub chaddr: MacAddr, #[length = "10"] - pub client_hw_addr_pad: Vec, + pub chaddr_pad: Vec, #[length = "64"] - pub server_host_name: Vec, + pub sname: Vec, #[length = "128"] - pub boot_file_name: Vec, - #[construct_with(u32)] - pub magic_cookie: DhcpMagicCookie, + pub file: Vec, #[payload] - pub payload: Vec, + pub options: Vec, } \ No newline at end of file From 71bdd351921ec32e13e32595a3f29fda964372c8 Mon Sep 17 00:00:00 2001 From: Martin Andre Date: Thu, 1 Sep 2022 09:40:52 +0200 Subject: [PATCH 3/3] sort mod alphabetically MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin André --- pnet_packet/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pnet_packet/src/lib.rs b/pnet_packet/src/lib.rs index a96dc69e..d87db705 100644 --- a/pnet_packet/src/lib.rs +++ b/pnet_packet/src/lib.rs @@ -23,6 +23,7 @@ extern crate pnet_macros; pub use pnet_macros_support::packet::*; pub mod arp; +pub mod dhcp; pub mod ethernet; pub mod gre; pub mod icmp; @@ -34,6 +35,5 @@ pub mod tcp; pub mod udp; pub mod usbpcap; pub mod vlan; -pub mod dhcp; pub mod util;