Skip to content

Commit 4d6cf47

Browse files
[docs]: added documentation to conntracker functions
1 parent 3cb81b3 commit 4d6cf47

6 files changed

Lines changed: 192 additions & 95 deletions

File tree

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ use aya_ebpf::{
33
maps::{LruPerCpuHashMap, PerfEventArray,HashMap},
44
};
55

6+
// docs:
7+
// PacketLog structure used to track an incoming network packet
8+
//
9+
// proto: packet protol (ex. TCP,UDP,ICMP)
10+
// src_ip: source address ip
11+
// src_port: source address port
12+
// dst_ip: destination ip
13+
// dst_port: destination port
14+
// pid: kernel process ID
15+
//
16+
17+
618
#[repr(C)]
719
#[derive(Clone, Copy)]
820
pub struct PacketLog {
@@ -14,7 +26,7 @@ pub struct PacketLog {
1426
pub pid: u32,
1527
}
1628

17-
// This structure is only for active connections
29+
// This structure is only for active connections (TODO: investigate if this is really useful)
1830
#[repr(C)]
1931
#[derive(Clone, Copy)]
2032
pub struct ConnArray {
@@ -25,26 +37,50 @@ pub struct ConnArray {
2537
pub proto: u8,
2638
}
2739

40+
41+
// docs:
42+
// VethLog structure used to track virtual ethernet interfaces creation and deletion
43+
//
44+
// name: veth name
45+
// state: socket state
46+
// dev_addr: veth device addresses
47+
// event_type: creation or deletion
48+
// netns: veth network namespace
49+
// pid: kernel process ID
50+
//
51+
2852
#[repr(C)]
2953
#[derive(Clone, Copy, Debug)]
3054
pub struct VethLog {
3155
pub name: [u8; 16],
32-
pub state: u64, //state var type: long unsigned int
56+
pub state: u64, // state var type: long unsigned int
3357
pub dev_addr: [u32; 8],
34-
pub event_type: u8, //i choose 1 for veth creation or 2 for veth destruction
58+
pub event_type: u8, // i choose 1 for veth creation or 2 for veth destruction
3559
pub netns: u32,
3660
pub pid: u32
3761

3862
}
3963

64+
// docs:
65+
//
66+
// BPF maps used in the conntracker programs
67+
//
68+
// VETH_EVENTS: PerfEventArray used in the veth_tracer functions (veth_tracer.rs module)
69+
//
70+
// BLOCKLIST: an hashmap used to block addresses -----> TODO: key and values are the same for semplicity but we need to
71+
// investigate the possibility to save the service name or the timestamp registered when the command was executed or a simple int index
72+
//
73+
74+
4075
#[map(name = "EventsMap", pinning = "by_name")]
4176
pub static mut EVENTS: PerfEventArray<PacketLog> = PerfEventArray::new(0);
4277

43-
//TODO: ConnectionMap needs a rework after implementing issue #105
78+
// FIXME: this might be useless
4479
#[map(name = "ConnectionMap")]
4580
pub static mut ACTIVE_CONNECTIONS: LruPerCpuHashMap<u16, ConnArray> =
4681
LruPerCpuHashMap::with_max_entries(65536, 0);
4782

83+
// FIXME: this might be useless
4884
#[map(name = "ConnectionTrackerMap")]
4985
pub static mut CONNTRACKER: LruPerCpuHashMap<ConnArray, u8> =
5086
LruPerCpuHashMap::with_max_entries(65536, 0);

core/src/components/conntracker/src/main.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// docs:
2+
//
23
// This file contains the code for the identity service
34
// Functionalities:
45
// 1. Creates a PacketLog structure to track incoming packets
@@ -31,35 +32,38 @@ use crate::tc::try_identity_classifier;
3132
use crate::veth_tracer::try_veth_tracer;
3233
use crate::tcp_analyzer::try_tcp_analyzer;
3334

35+
36+
// docs:
37+
//
38+
// virtual ethernet (veth) interface tracer:
39+
// This function is triggered when a virtual ethernet interface is created
40+
//
41+
3442
#[kprobe]
3543
pub fn veth_creation_trace(ctx: ProbeContext) -> u32 {
3644
match try_veth_tracer(ctx, 1) {
3745
Ok(ret_val) => ret_val,
3846
Err(ret_val) => ret_val.try_into().unwrap_or(1),
3947
}
4048
}
41-
#[kprobe]
42-
pub fn veth_deletion_trace(ctx: ProbeContext) -> u32 {
43-
match try_veth_tracer(ctx, 2) {
44-
Ok(ret_val) => ret_val,
45-
Err(ret_val) => ret_val.try_into().unwrap_or(1),
46-
}
47-
}
4849

49-
// docs;
50-
// this kprobe retrieves pid data and task id of an incoming packet
50+
// docs:
51+
//
52+
// virtual ethernet (veth) interface tracer:
53+
// This function is triggered when a virtual ethernet interface is deleted
54+
//
5155

5256
#[kprobe]
53-
pub fn tcp_message_tracer(ctx: ProbeContext) -> u32 {
54-
match try_tcp_analyzer(ctx) {
57+
pub fn veth_deletion_trace(ctx: ProbeContext) -> u32 {
58+
match try_veth_tracer(ctx, 2) {
5559
Ok(ret_val) => ret_val,
5660
Err(ret_val) => ret_val.try_into().unwrap_or(1),
5761
}
5862
}
5963

6064
// docs: this classifier acts in the very first step when a packet is logged
61-
62-
// Linux hooks stack:
65+
//
66+
// Linux networking stack:
6367
//
6468
// 6.Socket Layer
6569
// |
@@ -74,18 +78,31 @@ pub fn tcp_message_tracer(ctx: ProbeContext) -> u32 {
7478
// 1.Network interface
7579
// |
7680
// Incoming Packet
77-
81+
//
7882
// so we also need to extract the data from a second source in a kprobe context and correlate the data to catch
7983
// most of the value, without losing the ability to block a packet from the very early stages
8084

8185
#[classifier]
8286
pub fn identity_classifier(ctx: TcContext) -> i32 {
8387
match try_identity_classifier(ctx) {
8488
Ok(_) => TC_ACT_OK,
85-
Err(_) => TC_ACT_SHOT, //block packets that returns errors
89+
Err(_) => TC_ACT_SHOT, // block packets that returns errors
8690
}
8791
}
8892

93+
// docs:
94+
//
95+
// this kprobe retrieves pid data and task id of an incoming packet
96+
97+
#[kprobe]
98+
pub fn tcp_message_tracer(ctx: ProbeContext) -> u32 {
99+
match try_tcp_analyzer(ctx) {
100+
Ok(ret_val) => ret_val,
101+
Err(ret_val) => ret_val.try_into().unwrap_or(1),
102+
}
103+
}
104+
105+
89106
//ref:https://elixir.bootlin.com/linux/v6.15.1/source/include/uapi/linux/ethtool.h#L536
90107
//https://elixir.bootlin.com/linux/v6.15.1/source/drivers/net/veth.c#L268
91108
//https://eunomia.dev/tutorials/3-fentry-unlink/

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,23 @@ impl OFFSETS {
4646
pub const IPV4_ETHERTYPE: u16 = 0x0800;
4747

4848
//IPV4 STACK
49-
pub const SRC_BYTE_OFFSET: usize = 12; //source address offset for ipv4 addresses
50-
pub const DST_BYTE_OFFSET: usize = 16; //destination address offset for ipv4 addresses
51-
pub const IPV4_PROTOCOL_OFFSET: usize = 9; //ipv4 protocol offset
49+
pub const SRC_BYTE_OFFSET: usize = 12; // source address offset for ipv4 addresses
50+
pub const DST_BYTE_OFFSET: usize = 16; // destination address offset for ipv4 addresses
51+
pub const IPV4_PROTOCOL_OFFSET: usize = 9; // ipv4 protocol offset
5252

5353
//ETHERNET STACK
5454
pub const SRC_MAC: usize = 6; // source mac address offset
5555
pub const DST_MAC: usize = 6; // destination mac address offset
5656
pub const ETHERTYPE_BYTES: usize = 2; // ethertype bytes doc: https://en.wikipedia.org/wiki/EtherType
5757

5858
//TCP UDP STACK
59-
pub const SRC_PORT_OFFSET_FROM_IP_HEADER: usize = 0; //source port offset
60-
pub const DST_PORT_OFFSET_FROM_IP_HEADER: usize = 2; //destination port offset
59+
pub const SRC_PORT_OFFSET_FROM_IP_HEADER: usize = 0; // source port offset
60+
pub const DST_PORT_OFFSET_FROM_IP_HEADER: usize = 2; // destination port offset
6161

6262
// TOTAL BYTES SUM
6363
pub const ETH_STACK_BYTES: usize = OFFSETS::SRC_MAC + OFFSETS::DST_MAC + OFFSETS::ETHERTYPE_BYTES; // ethernet protocol total stacked bytes
6464
pub const DST_T0TAL_BYTES_OFFSET: usize = OFFSETS::ETH_STACK_BYTES + OFFSETS::DST_BYTE_OFFSET; // destination total bytes offset
65-
pub const SRC_T0TAL_BYTES_OFFSET: usize = OFFSETS::ETH_STACK_BYTES + OFFSETS::SRC_BYTE_OFFSET; //source total bytes offset
65+
pub const SRC_T0TAL_BYTES_OFFSET: usize = OFFSETS::ETH_STACK_BYTES + OFFSETS::SRC_BYTE_OFFSET; // source total bytes offset
6666
pub const PROTOCOL_T0TAL_BYTES_OFFSET: usize =
6767
OFFSETS::ETH_STACK_BYTES + OFFSETS::IPV4_PROTOCOL_OFFSET; // total bytes offset
6868
}

core/src/components/conntracker/src/tc.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// docs:
2-
// TODO: write docs about the traffic control features
3-
41
use core::net::Ipv4Addr;
52

63
use aya_ebpf::{
@@ -13,45 +10,64 @@ use crate::data_structures::{ ConnArray, PacketLog };
1310
use crate::data_structures::{ EVENTS,BLOCKLIST };
1411
use crate::offsets::OFFSETS;
1512

13+
// docs:
14+
//
15+
// This is the main traffic control functions. Takes a TcContext ("ctx") and retrieves many useful info about
16+
// the incoming packet by reading the corresponding bytes in the network stack
17+
//
18+
// The functions returns the following info:
19+
// - source ip (src_ip)
20+
// - destination ip (dst_ip)
21+
// - source port (src_port)
22+
// - destination port (dst_port)
23+
// - protocol (proto)
24+
// - kernel PID (pid)
25+
//
26+
// Features:
27+
// Users can directly block one ip or a list of ips. The ip addresses goes directly into the blocklist hashmap
28+
// and allows users to block the ips before entering into the userspace
29+
//
30+
// Returns a Result with a unit type () and a i64 error code
31+
1632
pub fn try_identity_classifier(ctx: TcContext) -> Result<(), i64> {
1733
let eth_proto = u16::from_be(ctx.load::<u16>(12).map_err(|_| 1)?);
1834

19-
//only ipv4 protcol allowed
35+
// only ipv4 protcol allowed
2036
if eth_proto != OFFSETS::IPV4_ETHERTYPE {
2137
return Ok(());
2238
}
2339

24-
//read if the packets has Options
40+
// read if the packets has Options
2541
let first_ipv4_byte = u8::from_be(ctx.load::<u8>(OFFSETS::ETH_STACK_BYTES).map_err(|_| 1)?);
2642
let ihl = (first_ipv4_byte &
2743
0x0f) as usize; /* 0x0F=00001111 &=AND bit a bit operator to extract the last 4 bit*/
2844
let ip_header_len = ihl * 4; //returns the header lenght in bytes
2945

30-
//get the source ip,destination ip and connection id
46+
// get the source ip,destination ip and connection id
3147
let src_ip = ctx.load::<u32>(OFFSETS::SRC_T0TAL_BYTES_OFFSET).map_err(|_| 1)?; // ETH+SOURCE_ADDRESS
3248
let src_port = u16::from_be(
3349
ctx
3450
.load::<u16>(
3551
OFFSETS::ETH_STACK_BYTES + ip_header_len + OFFSETS::SRC_PORT_OFFSET_FROM_IP_HEADER
3652
)
3753
.map_err(|_| 1)?
38-
); //14+IHL-Lenght+0
54+
); // 14+IHL-Lenght+0
3955
let dst_ip = ctx.load::<u32>(OFFSETS::DST_T0TAL_BYTES_OFFSET).map_err(|_| 1)?; // ETH+ DESTINATION_ADDRESS
4056
let dst_port = u16::from_be(
4157
ctx
4258
.load::<u16>(
4359
OFFSETS::ETH_STACK_BYTES + ip_header_len + OFFSETS::DST_PORT_OFFSET_FROM_IP_HEADER
4460
)
4561
.map_err(|_| 1)?
46-
); //14+IHL-Lenght+0
62+
); // 14+IHL-Lenght+0
4763
let proto = u8::from_be(ctx.load::<u8>(OFFSETS::PROTOCOL_T0TAL_BYTES_OFFSET).map_err(|_| 1)?);
4864

4965
let pid: u32 = bpf_get_current_pid_tgid() as u32;
5066

5167
// check if the address is in the blocklist
52-
let src_ip_be_bytes: [u8; 4] = src_ip.to_be_bytes(); //transforming the src_ip in big endian bytes
68+
let src_ip_be_bytes: [u8; 4] = src_ip.to_be_bytes(); // transforming the src_ip in big endian bytes
5369

54-
// ** blocklist logic
70+
// blocklist logic
5571
if unsafe { BLOCKLIST.get(&src_ip_be_bytes).is_some() } {
5672
info!(
5773
&ctx,

core/src/components/conntracker/src/tcp_analyzer.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// docs:
2-
// TODO: move the kprobe tracer functions here
31

42
use aya_ebpf::programs::ProbeContext;
53

0 commit comments

Comments
 (0)