Skip to content

Commit bd5976c

Browse files
Merge pull request #163 from LorenzoTettamanti/feature/ebpf-core
Experimental service mapping pt.1
2 parents eb10c76 + 479b83d commit bd5976c

File tree

12 files changed

+658
-135
lines changed

12 files changed

+658
-135
lines changed

cli/Cargo.lock

Lines changed: 0 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/components/conntracker/src/data_structures.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ pub struct VethLog {
6161

6262
}
6363

64+
// TODO: write documentation about this structure
65+
#[repr(C)]
66+
#[derive(Clone,Copy,Debug)]
67+
pub struct TcpPacketRegistry{
68+
pub proto: u8,
69+
pub src_ip: u32,
70+
pub dst_ip: u32,
71+
pub src_port: u16,
72+
pub dst_port: u16,
73+
pub pid: u32,
74+
pub command: [u8;16],
75+
pub cgroup_id: u64,
76+
77+
}
78+
6479
// docs:
6580
//
6681
// BPF maps used in the conntracker programs
@@ -90,4 +105,7 @@ pub static mut VETH_EVENTS: PerfEventArray<VethLog> = PerfEventArray::new(0);
90105

91106
#[map(name = "Blocklist")]
92107
pub static mut BLOCKLIST: HashMap<[u8;4], [u8;4]> = HashMap::<[u8;4], [u8;4]>::with_max_entries(1024, 0);
93-
//here i need to pass an address like this: [135,171,168,192]
108+
//here i need to pass an address like this: [135,171,168,192]
109+
110+
#[map(name = "TcpPacketRegistry",pinning = "by_name")]
111+
pub static mut PACKET_REGISTRY: PerfEventArray<TcpPacketRegistry> = PerfEventArray::new(0);

core/src/components/conntracker/src/offsets.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ impl OFFSETS {
6060
pub const DST_PORT_OFFSET_FROM_IP_HEADER: usize = 2; // destination port offset
6161

6262
// TOTAL BYTES SUM
63-
pub const ETH_STACK_BYTES: usize = OFFSETS::SRC_MAC + OFFSETS::DST_MAC + OFFSETS::ETHERTYPE_BYTES; // ethernet protocol total stacked bytes
63+
pub const ETH_STACK_BYTES: usize =
64+
OFFSETS::SRC_MAC + OFFSETS::DST_MAC + OFFSETS::ETHERTYPE_BYTES; // ethernet protocol total stacked bytes
6465
pub const DST_T0TAL_BYTES_OFFSET: usize = OFFSETS::ETH_STACK_BYTES + OFFSETS::DST_BYTE_OFFSET; // destination total bytes offset
6566
pub const SRC_T0TAL_BYTES_OFFSET: usize = OFFSETS::ETH_STACK_BYTES + OFFSETS::SRC_BYTE_OFFSET; // source total bytes offset
6667
pub const PROTOCOL_T0TAL_BYTES_OFFSET: usize =
6768
OFFSETS::ETH_STACK_BYTES + OFFSETS::IPV4_PROTOCOL_OFFSET; // total bytes offset
69+
70+
pub const SKB_DATA_POINTER: usize = 208; // sk_buff structure data pointer
6871
}
Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,83 @@
1-
21
use aya_ebpf::programs::ProbeContext;
2+
use aya_ebpf::helpers::{
3+
bpf_get_current_comm,
4+
bpf_get_current_pid_tgid,
5+
bpf_get_current_cgroup_id,
6+
};
7+
8+
use crate::bindings::{ sk_buff };
9+
use crate::offsets::OFFSETS;
10+
use crate::data_structures::{ PACKET_REGISTRY, TcpPacketRegistry };
11+
use crate::veth_tracer::{ read_linux_inner_struct, read_linux_inner_value };
12+
13+
// docs:
14+
// TODO: add function documentation
315

16+
// docs:
17+
//
18+
// how skb works? http://oldvger.kernel.org/~davem/skb_data.html
19+
//
20+
// ref: https://elixir.bootlin.com/linux/v6.17.7/source/net/ipv4/tcp_ipv4.c#L2195
21+
//
22+
23+
//in tcp_v4_recv skb->data
424
pub fn try_tcp_analyzer(ctx: ProbeContext) -> Result<u32, i64> {
5-
todo!()
25+
let sk_buff_pointer: *const sk_buff = ctx.arg(0).ok_or(1i64)?;
26+
// first control: i'm, verifying that the pointer is not null
27+
if sk_buff_pointer.is_null() {
28+
return Err(1);
29+
}
30+
31+
let skb_data_pointer = read_linux_inner_struct::<u8>(
32+
sk_buff_pointer as *const u8,
33+
OFFSETS::SKB_DATA_POINTER
34+
)?;
35+
let first_ipv4_byte = read_linux_inner_value::<u8>(skb_data_pointer as *const u8, 0)?;
36+
let ihl = (first_ipv4_byte & 0x0f) as usize; // 0x0F=00001111 &=AND bit a bit operator to extract the last 4 bit
37+
let ip_header_len = ihl * 4; //returns the header lenght in bytes
38+
39+
let proto = read_linux_inner_struct::<u8>(
40+
skb_data_pointer,
41+
OFFSETS::IPV4_PROTOCOL_OFFSET
42+
)? as u8;
43+
44+
if proto != 6 {
45+
return Ok(0);
46+
} else {
47+
// get the source ip,destination ip and connection id
48+
let src_ip = read_linux_inner_value::<u32>(skb_data_pointer, OFFSETS::SRC_BYTE_OFFSET)?;
49+
let dst_ip = read_linux_inner_value::<u32>(skb_data_pointer, OFFSETS::DST_BYTE_OFFSET)?;
50+
let src_port = u16::from_be(
51+
read_linux_inner_value(
52+
skb_data_pointer,
53+
ip_header_len + OFFSETS::SRC_PORT_OFFSET_FROM_IP_HEADER
54+
)?
55+
);
56+
let dst_port = u16::from_be(
57+
read_linux_inner_value(
58+
skb_data_pointer,
59+
ip_header_len + OFFSETS::DST_PORT_OFFSET_FROM_IP_HEADER
60+
)?
61+
);
62+
63+
let command = bpf_get_current_comm()?;
64+
let pid = (bpf_get_current_pid_tgid() >> 32) as u32;
65+
let cgroup_id = unsafe { bpf_get_current_cgroup_id() };
66+
67+
let log = TcpPacketRegistry {
68+
proto,
69+
src_ip,
70+
dst_ip,
71+
src_port,
72+
dst_port,
73+
pid,
74+
command,
75+
cgroup_id,
76+
};
77+
unsafe {
78+
PACKET_REGISTRY.output(&ctx, &log, 0);
79+
}
80+
}
81+
82+
Ok(0)
683
}

core/src/components/conntracker/src/veth_tracer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn try_veth_tracer(ctx: ProbeContext, mode: u8) -> Result<u32, i64> {
7777
//
7878
// Returns a Result type with a const pointer to an inner field or an error code as i64
7979

80-
fn read_linux_inner_struct<T>(ptr: *const u8, offset: usize) -> Result<*const T, i64> {
80+
pub fn read_linux_inner_struct<T>(ptr: *const u8, offset: usize) -> Result<*const T, i64> {
8181
if ptr.is_null() {
8282
return Err(1);
8383
} else {
@@ -105,7 +105,7 @@ fn read_linux_inner_struct<T>(ptr: *const u8, offset: usize) -> Result<*const T,
105105
//
106106
// Returns a Result type with the value or an error code as i64
107107

108-
fn read_linux_inner_value<T: Copy>(ptr: *const u8, offset: usize) -> Result<T, i64> {
108+
pub fn read_linux_inner_value<T: Copy>(ptr: *const u8, offset: usize) -> Result<T, i64> {
109109
if ptr.is_null() {
110110
return Err(1);
111111
}

core/src/components/identity/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ enums = []
1818

1919
[dependencies]
2020
aya = "0.13.1"
21-
aya-log = "0.2.1"
2221
bytes = "1.4"
2322
tokio = { version = "1.48.0", features = ["rt","rt-multi-thread","fs","signal","fs","time","macros"] }
2423
anyhow = "1.0"

0 commit comments

Comments
 (0)