Skip to content

Commit

Permalink
add udphdr in bindings, use it for UDP packets (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitris authored Aug 3, 2022
1 parent df4f4a1 commit ed3b022
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 13 deletions.
1 change: 1 addition & 0 deletions tcbpftest-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct PacketLog {
pub remote_port2: u32,
pub local_port: u32, // TCP or UDP local port (dport for ingress)
pub local_port2: u32,
pub udp_len: u32,
}

#[cfg(feature = "user")]
Expand Down
189 changes: 189 additions & 0 deletions tcbpftest-ebpf/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,195 @@ pub struct ethhdr {
pub type __sum16 = __u16;
#[repr(C)]
#[derive(Copy, Clone)]
pub struct tcphdr {
pub source: __be16,
pub dest: __be16,
pub seq: __be32,
pub ack_seq: __be32,
pub _bitfield_align_1: [u8; 0],
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 2usize]>,
pub window: __be16,
pub check: __sum16,
pub urg_ptr: __be16,
}
impl tcphdr {
#[inline]
pub fn res1(&self) -> __u16 {
unsafe { ::core::mem::transmute(self._bitfield_1.get(0usize, 4u8) as u16) }
}
#[inline]
pub fn set_res1(&mut self, val: __u16) {
unsafe {
let val: u16 = ::core::mem::transmute(val);
self._bitfield_1.set(0usize, 4u8, val as u64)
}
}
#[inline]
pub fn doff(&self) -> __u16 {
unsafe { ::core::mem::transmute(self._bitfield_1.get(4usize, 4u8) as u16) }
}
#[inline]
pub fn set_doff(&mut self, val: __u16) {
unsafe {
let val: u16 = ::core::mem::transmute(val);
self._bitfield_1.set(4usize, 4u8, val as u64)
}
}
#[inline]
pub fn fin(&self) -> __u16 {
unsafe { ::core::mem::transmute(self._bitfield_1.get(8usize, 1u8) as u16) }
}
#[inline]
pub fn set_fin(&mut self, val: __u16) {
unsafe {
let val: u16 = ::core::mem::transmute(val);
self._bitfield_1.set(8usize, 1u8, val as u64)
}
}
#[inline]
pub fn syn(&self) -> __u16 {
unsafe { ::core::mem::transmute(self._bitfield_1.get(9usize, 1u8) as u16) }
}
#[inline]
pub fn set_syn(&mut self, val: __u16) {
unsafe {
let val: u16 = ::core::mem::transmute(val);
self._bitfield_1.set(9usize, 1u8, val as u64)
}
}
#[inline]
pub fn rst(&self) -> __u16 {
unsafe { ::core::mem::transmute(self._bitfield_1.get(10usize, 1u8) as u16) }
}
#[inline]
pub fn set_rst(&mut self, val: __u16) {
unsafe {
let val: u16 = ::core::mem::transmute(val);
self._bitfield_1.set(10usize, 1u8, val as u64)
}
}
#[inline]
pub fn psh(&self) -> __u16 {
unsafe { ::core::mem::transmute(self._bitfield_1.get(11usize, 1u8) as u16) }
}
#[inline]
pub fn set_psh(&mut self, val: __u16) {
unsafe {
let val: u16 = ::core::mem::transmute(val);
self._bitfield_1.set(11usize, 1u8, val as u64)
}
}
#[inline]
pub fn ack(&self) -> __u16 {
unsafe { ::core::mem::transmute(self._bitfield_1.get(12usize, 1u8) as u16) }
}
#[inline]
pub fn set_ack(&mut self, val: __u16) {
unsafe {
let val: u16 = ::core::mem::transmute(val);
self._bitfield_1.set(12usize, 1u8, val as u64)
}
}
#[inline]
pub fn urg(&self) -> __u16 {
unsafe { ::core::mem::transmute(self._bitfield_1.get(13usize, 1u8) as u16) }
}
#[inline]
pub fn set_urg(&mut self, val: __u16) {
unsafe {
let val: u16 = ::core::mem::transmute(val);
self._bitfield_1.set(13usize, 1u8, val as u64)
}
}
#[inline]
pub fn ece(&self) -> __u16 {
unsafe { ::core::mem::transmute(self._bitfield_1.get(14usize, 1u8) as u16) }
}
#[inline]
pub fn set_ece(&mut self, val: __u16) {
unsafe {
let val: u16 = ::core::mem::transmute(val);
self._bitfield_1.set(14usize, 1u8, val as u64)
}
}
#[inline]
pub fn cwr(&self) -> __u16 {
unsafe { ::core::mem::transmute(self._bitfield_1.get(15usize, 1u8) as u16) }
}
#[inline]
pub fn set_cwr(&mut self, val: __u16) {
unsafe {
let val: u16 = ::core::mem::transmute(val);
self._bitfield_1.set(15usize, 1u8, val as u64)
}
}
#[inline]
pub fn new_bitfield_1(
res1: __u16,
doff: __u16,
fin: __u16,
syn: __u16,
rst: __u16,
psh: __u16,
ack: __u16,
urg: __u16,
ece: __u16,
cwr: __u16,
) -> __BindgenBitfieldUnit<[u8; 2usize]> {
let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 2usize]> = Default::default();
__bindgen_bitfield_unit.set(0usize, 4u8, {
let res1: u16 = unsafe { ::core::mem::transmute(res1) };
res1 as u64
});
__bindgen_bitfield_unit.set(4usize, 4u8, {
let doff: u16 = unsafe { ::core::mem::transmute(doff) };
doff as u64
});
__bindgen_bitfield_unit.set(8usize, 1u8, {
let fin: u16 = unsafe { ::core::mem::transmute(fin) };
fin as u64
});
__bindgen_bitfield_unit.set(9usize, 1u8, {
let syn: u16 = unsafe { ::core::mem::transmute(syn) };
syn as u64
});
__bindgen_bitfield_unit.set(10usize, 1u8, {
let rst: u16 = unsafe { ::core::mem::transmute(rst) };
rst as u64
});
__bindgen_bitfield_unit.set(11usize, 1u8, {
let psh: u16 = unsafe { ::core::mem::transmute(psh) };
psh as u64
});
__bindgen_bitfield_unit.set(12usize, 1u8, {
let ack: u16 = unsafe { ::core::mem::transmute(ack) };
ack as u64
});
__bindgen_bitfield_unit.set(13usize, 1u8, {
let urg: u16 = unsafe { ::core::mem::transmute(urg) };
urg as u64
});
__bindgen_bitfield_unit.set(14usize, 1u8, {
let ece: u16 = unsafe { ::core::mem::transmute(ece) };
ece as u64
});
__bindgen_bitfield_unit.set(15usize, 1u8, {
let cwr: u16 = unsafe { ::core::mem::transmute(cwr) };
cwr as u64
});
__bindgen_bitfield_unit
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct udphdr {
pub source: __be16,
pub dest: __be16,
pub len: __be16,
pub check: __sum16,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct iphdr {
pub _bitfield_align_1: [u8; 0],
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize]>,
Expand Down
16 changes: 13 additions & 3 deletions tcbpftest-ebpf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use memoffset::offset_of;
use tcbpftest_common::PacketLog;

mod bindings;
use bindings::{ethhdr, iphdr, tcphdr};
use bindings::{ethhdr, iphdr, tcphdr, udphdr};

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
Expand Down Expand Up @@ -76,18 +76,28 @@ unsafe fn try_tcbpftest(ctx: SkBuffContext) -> Result<i32, i64> {
let rem_port = u16::from_be(rem_port_val);
let loc_port = u16::from_be(loc_port_val);

let mut udp_len_val : u16 = 0;
if ip_proto == IPPROTO_UDP {
unsafe {
udp_len_val = match ptr_at(&ctx, ETH_HDR_LEN + IP_HDR_LEN + offset_of!(udphdr, len)) {
Err(_) => return Err(197),
Ok(val) => *val,
};
}
}
let log_entry = PacketLog {
len: length as u32,
ctx_len: ctx_len,
src_addr: saddr,
dest_addr: daddr,
eth_proto: eth_proto as u32,
eth_proto2: eth_proto2 as u32,
eth_proto: eth_proto as u32,
eth_proto2: eth_proto2 as u32,
ip_proto: ip_proto as u32,
remote_port: rem_port as u32,
remote_port2: rem_port2 as u32,
local_port: loc_port as u32,
local_port2: loc_port2 as u32,
udp_len: u16::from_be(udp_len_val) as u32,
};

unsafe {
Expand Down
3 changes: 2 additions & 1 deletion tcbpftest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ async fn main() -> Result<(), anyhow::Error> {
let ptr = buf.as_ptr() as *const PacketLog;
let data = unsafe { ptr.read_unaligned() };
println!(
"LOG: LEN {}, CTX_LEN {}, SRC_IP {}, DEST_IP {}, ETH_PROTO 0x{:X}, ETH_PROTO2 0x{:X}, IP_PROTO {}, REMOTE_PORT {}, REMOTE_PORT2 {}, LOCAL_PORT {}, LOCAL_PORT2 {}",
"LOG: LEN {}, CTX_LEN {}, UDP_LEN {}, SRC_IP {}, DEST_IP {}, ETH_PROTO 0x{:X}, ETH_PROTO2 0x{:X}, IP_PROTO {}, REMOTE_PORT {}, REMOTE_PORT2 {}, LOCAL_PORT {}, LOCAL_PORT2 {}",
data.len,
data.ctx_len,
data.udp_len,
Ipv4Addr::from(data.src_addr),
Ipv4Addr::from(data.dest_addr),
data.eth_proto,
Expand Down
4 changes: 2 additions & 2 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ edition = "2021"

[dependencies]
aya-gen = { git = "https://github.com/aya-rs/aya", branch = "main" }
structopt = {version = "0.3", default-features = false }
anyhow = "1"
structopt = { version = "0.3.26", default-features = false }
anyhow = "1.0.58"
14 changes: 7 additions & 7 deletions xtask/src/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use aya_gen::generate::InputFile;
use std::{
fs::File,
io::Write,
path::PathBuf,
};
use std::{fs::File, io::Write, path::PathBuf};

pub fn generate() -> Result<(), anyhow::Error> {
let dir = PathBuf::from("tcbpftest-ebpf/src");
let names: Vec<&str> = vec!["ethhdr", "iphdr"];
let bindings = aya_gen::generate(InputFile::Btf(PathBuf::from("/sys/kernel/btf/vmlinux")), &names, &[])?;
let names: Vec<&str> = vec!["ethhdr", "iphdr", "tcphdr", "udphdr"];
let bindings = aya_gen::generate(
InputFile::Btf(PathBuf::from("/sys/kernel/btf/vmlinux")),
&names,
&[],
)?;
// Write the bindings to the $OUT_DIR/bindings.rs file.
let mut out = File::create(dir.join("bindings.rs"))?;
write!(out, "{}", bindings)?;
Expand Down

0 comments on commit ed3b022

Please sign in to comment.