fix: Fix ebpf run on bpflib 0.5 error

This commit is contained in:
DaLaw2 2025-09-21 13:11:13 +00:00
parent b684e58d76
commit e6c6d712aa
5 changed files with 85 additions and 63 deletions

View File

@ -22,18 +22,12 @@ pub struct IPv4Event {
impl IPv4Event {
#[inline(always)]
pub fn source_addr(&self) -> AddrPortV4 {
AddrPortV4 {
ip: self.source_ip,
port: self.source_port,
}
AddrPortV4::new(self.source_ip, self.source_port)
}
#[inline(always)]
pub fn destination_addr(&self) -> AddrPortV4 {
AddrPortV4 {
ip: self.destination_ip,
port: self.destination_port,
}
AddrPortV4::new(self.destination_ip, self.destination_port)
}
}
@ -51,17 +45,11 @@ pub struct IPv6Event {
impl IPv6Event {
#[inline(always)]
pub fn source_addr(&self) -> AddrPortV6 {
AddrPortV6 {
ip: self.source_ip,
port: self.source_port,
}
AddrPortV6::new(self.source_ip, self.source_port)
}
#[inline(always)]
pub fn destination_addr(&self) -> AddrPortV6 {
AddrPortV6 {
ip: self.destination_ip,
port: self.destination_port,
}
AddrPortV6::new(self.destination_ip, self.destination_port)
}
}

View File

@ -5,32 +5,72 @@ pub type IPv4 = u32;
pub type IPv6 = u128;
pub type Port = u16;
#[repr(C, align(8))]
#[derive(Copy, Clone)]
pub struct AddrPortV4 {
pub ip: IPv4,
pub port: Port,
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone)]
pub struct AddrPortV4([u8; 8]);
impl AddrPortV4 {
pub fn new(ip: IPv4, port: Port) -> Self {
AddrPortV4 { ip, port }
#[inline(always)]
pub fn new(ip: u32, port: u16) -> Self {
let mut key = [0u8; 8];
key[0..4].copy_from_slice(&ip.to_ne_bytes());
key[4..6].copy_from_slice(&port.to_ne_bytes());
Self(key)
}
#[inline(always)]
pub fn as_bytes(&self) -> &[u8; 8] {
&self.0
}
#[inline(always)]
pub fn ip(&self) -> IPv4 {
let mut ip_bytes = [0u8; 4];
ip_bytes.copy_from_slice(&self.0[0..4]);
u32::from_ne_bytes(ip_bytes)
}
#[inline(always)]
pub fn port(&self) -> Port {
let mut port_bytes = [0u8; 2];
port_bytes.copy_from_slice(&self.0[4..6]);
u16::from_ne_bytes(port_bytes)
}
}
#[cfg(feature = "user")]
unsafe impl Pod for AddrPortV4 {}
#[repr(C, align(8))]
#[derive(Copy, Clone)]
pub struct AddrPortV6 {
pub ip: IPv6,
pub port: Port,
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone)]
pub struct AddrPortV6([u8; 32]);
impl AddrPortV6 {
pub fn new(ip: IPv6, port: Port) -> Self {
AddrPortV6 { ip, port }
#[inline(always)]
pub fn new(ip: u128, port: u16) -> Self {
let mut key = [0u8; 32];
key[0..16].copy_from_slice(&ip.to_ne_bytes());
key[16..18].copy_from_slice(&port.to_ne_bytes());
Self(key)
}
#[inline(always)]
pub fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
#[inline(always)]
pub fn ip(&self) -> IPv6 {
let mut ip_bytes = [0u8; 16];
ip_bytes.copy_from_slice(&self.0[0..16]);
u128::from_ne_bytes(ip_bytes)
}
#[inline(always)]
pub fn port(&self) -> Port {
let mut port_bytes = [0u8; 2];
port_bytes.copy_from_slice(&self.0[16..18]);
u16::from_ne_bytes(port_bytes)
}
}

View File

@ -137,9 +137,9 @@ fn ipv4_ssh_service_violation(source: &AddrPortV4, destination: &AddrPortV4) ->
unsafe {
if IPV4_SSH_SERVICE.get(destination).is_some() {
if SSH_WHITE_LIST_ENABLE.get(0).is_some() {
IPV4_SSH_WHITE_LIST.get(&source.ip).is_none()
IPV4_SSH_WHITE_LIST.get(&source.ip()).is_none()
} else {
IPV4_SSH_BLACK_LIST.get(&source.ip).is_some()
IPV4_SSH_BLACK_LIST.get(&source.ip()).is_some()
}
} else {
false
@ -152,9 +152,9 @@ fn ipv6_ssh_service_violation(source_ip: &AddrPortV6, destination: &AddrPortV6)
unsafe {
if IPV6_SSH_SERVICE.get(destination).is_some() {
if SSH_WHITE_LIST_ENABLE.get(0).is_some() {
IPV6_SSH_WHITE_LIST.get(&source_ip.ip).is_none()
IPV6_SSH_WHITE_LIST.get(&source_ip.ip()).is_none()
} else {
IPV6_SSH_BLACK_LIST.get(&source_ip.ip).is_some()
IPV6_SSH_BLACK_LIST.get(&source_ip.ip()).is_some()
}
} else {
false

View File

@ -33,23 +33,23 @@ unsafe fn packet_intake(ctx: XdpContext) -> Result<u32, ()> {
let end = ctx.data_end();
let ptr = PARSED_PACKET.get_ptr_mut(0).ok_or(())?;
parsing::parse_packet(start, end, ptr)?;
let copy_size = (end - start).min(STANDARD_MTU);
if copy_size == 0 {
error!(&ctx, "Packet size is 0");
}
if let Some(mut entry) = PACKET_RING.reserve::<Packet>(0) {
let ring_ptr = entry.as_mut_ptr();
core::ptr::copy_nonoverlapping(
ptr as *const u8,
&mut (*ring_ptr).event as *mut Event as *mut u8,
size_of::<Event>(),
);
core::ptr::copy_nonoverlapping(start as *const u8, (*ring_ptr).raw_data.as_mut_ptr(), copy_size);
entry.submit(0);
} else {
error!(&ctx, "Ring buffer");
Err(())?
}
// let copy_size = (end - start).min(STANDARD_MTU);
// if copy_size == 0 {
// error!(&ctx, "Packet size is 0");
// }
// if let Some(mut entry) = PACKET_RING.reserve::<Packet>(0) {
// let ring_ptr = entry.as_mut_ptr();
// core::ptr::copy_nonoverlapping(
// ptr as *const u8,
// &mut (*ring_ptr).event as *mut Event as *mut u8,
// size_of::<Event>(),
// );
// core::ptr::copy_nonoverlapping(start as *const u8, (*ring_ptr).raw_data.as_mut_ptr(), copy_size);
// entry.submit(0);
// } else {
// error!(&ctx, "Ring buffer");
// Err(())?
// }
let _ = PROGRAM_ARRAY.tail_call(&ctx, 0);
Err(())
}

View File

@ -37,14 +37,11 @@ impl NativeConvert for AddrPortV4 {
type Native = SocketAddrV4;
fn into_native(self) -> Self::Native {
SocketAddrV4::new(Ipv4Addr::from(self.ip), self.port)
SocketAddrV4::new(Ipv4Addr::from(self.ip()), self.port())
}
fn from_native(native: Self::Native) -> Self {
Self {
ip: (*native.ip()).to_bits(),
port: native.port(),
}
AddrPortV4::new((*native.ip()).to_bits(), native.port())
}
}
@ -52,13 +49,10 @@ impl NativeConvert for AddrPortV6 {
type Native = SocketAddrV6;
fn into_native(self) -> Self::Native {
SocketAddrV6::new(Ipv6Addr::from(self.ip), self.port, 0, 0)
SocketAddrV6::new(Ipv6Addr::from(self.ip()), self.port(), 0, 0)
}
fn from_native(native: Self::Native) -> Self {
Self {
ip: (*native.ip()).to_bits(),
port: native.port(),
}
AddrPortV6::new((*native.ip()).to_bits(), native.port())
}
}