base commit
This commit is contained in:
6
net-logger-ebpf/.cargo/config.toml
Normal file
6
net-logger-ebpf/.cargo/config.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[build]
|
||||
target-dir = "../target"
|
||||
target = "bpfel-unknown-none"
|
||||
|
||||
[unstable]
|
||||
build-std = ["core"]
|
||||
2
net-logger-ebpf/.helix/config.toml
Normal file
2
net-logger-ebpf/.helix/config.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[editor]
|
||||
workspace-lsp-roots = []
|
||||
4
net-logger-ebpf/.vim/coc-settings.json
Normal file
4
net-logger-ebpf/.vim/coc-settings.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"rust-analyzer.cargo.target": "bpfel-unknown-none",
|
||||
"rust-analyzer.checkOnSave.allTargets": false
|
||||
}
|
||||
4
net-logger-ebpf/.vscode/settings.json
vendored
Normal file
4
net-logger-ebpf/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"rust-analyzer.cargo.target": "bpfel-unknown-none",
|
||||
"rust-analyzer.checkOnSave.allTargets": false
|
||||
}
|
||||
197
net-logger-ebpf/Cargo.lock
generated
Normal file
197
net-logger-ebpf/Cargo.lock
generated
Normal file
@@ -0,0 +1,197 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aya-ebpf"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b7318de0c49a17873182763831cb22f74fb30d04e2eb7e6d7b7e9b7d86d70ed3"
|
||||
dependencies = [
|
||||
"aya-ebpf-bindings",
|
||||
"aya-ebpf-cty",
|
||||
"aya-ebpf-macros",
|
||||
"rustversion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aya-ebpf-bindings"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a8536b7e39b232ecd854e587f473ba15640c09afc3e08408fc28144a7404ae75"
|
||||
dependencies = [
|
||||
"aya-ebpf-cty",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aya-ebpf-cty"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d5c130d898322b9698937465b3b749095dae85dba0da4ee648235947eb95738d"
|
||||
|
||||
[[package]]
|
||||
name = "aya-ebpf-macros"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce7820cc83547582284a140ffbdd46ab527d7ee2d9d0cfedf3f184fad3f8e15c"
|
||||
dependencies = [
|
||||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aya-log-common"
|
||||
version = "0.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6d38a351ee2d5dc24e04cac6184b1b39408642d9a8b585892c99146f8dd4edb"
|
||||
dependencies = [
|
||||
"num_enum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aya-log-ebpf"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a10bbadd0829895a91eb1cd2bb02d7af145704087f03812bed60cb9fe65dbb3"
|
||||
dependencies = [
|
||||
"aya-ebpf",
|
||||
"aya-log-common",
|
||||
"aya-log-ebpf-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aya-log-ebpf-macros"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6d8251a75f56077db51892041aa6b77c70ef2723845d7a210979700b2f01bc4"
|
||||
dependencies = [
|
||||
"aya-log-common",
|
||||
"aya-log-parser",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aya-log-parser"
|
||||
version = "0.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14b102eb5c88c9aa0b49102d3fbcee08ecb0dfa81014f39b373311de7a7032cb"
|
||||
dependencies = [
|
||||
"aya-log-common",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "net-logger-common"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"network-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "net-logger-ebpf"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aya-ebpf",
|
||||
"aya-log-ebpf",
|
||||
"net-logger-common",
|
||||
"network-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "network-types"
|
||||
version = "0.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41b13eba62f530cd2ea031938ac4472b9b649694baa1e587c2a2fadc07844d3c"
|
||||
|
||||
[[package]]
|
||||
name = "num_enum"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e613fc340b2220f734a8595782c551f1250e969d87d3be1ae0579e8d4065179"
|
||||
dependencies = [
|
||||
"num_enum_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_enum_derive"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
||||
dependencies = [
|
||||
"proc-macro-error-attr",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error-attr"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.86"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.72"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||
34
net-logger-ebpf/Cargo.toml
Normal file
34
net-logger-ebpf/Cargo.toml
Normal file
@@ -0,0 +1,34 @@
|
||||
cargo-features = ["profile-rustflags"]
|
||||
[package]
|
||||
name = "net-logger-ebpf"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
aya-ebpf = "0.1.0"
|
||||
aya-log-ebpf = "0.1.0"
|
||||
net-logger-common = { path = "../net-logger-common" }
|
||||
network-types = "0.0.6"
|
||||
|
||||
[[bin]]
|
||||
name = "net-logger"
|
||||
path = "src/main.rs"
|
||||
|
||||
[profile.dev]
|
||||
opt-level = 3
|
||||
debug = false
|
||||
debug-assertions = false
|
||||
overflow-checks = false
|
||||
lto = true
|
||||
panic = "abort"
|
||||
incremental = false
|
||||
codegen-units = 1
|
||||
rpath = false
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
panic = "abort"
|
||||
codegen-units = 1
|
||||
|
||||
[workspace]
|
||||
members = []
|
||||
13
net-logger-ebpf/rust-toolchain.toml
Normal file
13
net-logger-ebpf/rust-toolchain.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[toolchain]
|
||||
channel = "nightly"
|
||||
# The source code of rustc, provided by the rust-src component, is needed for
|
||||
# building eBPF programs.
|
||||
components = [
|
||||
"cargo",
|
||||
"clippy",
|
||||
"rust-docs",
|
||||
"rust-src",
|
||||
"rust-std",
|
||||
"rustc",
|
||||
"rustfmt",
|
||||
]
|
||||
23
net-logger-ebpf/src/main.rs
Normal file
23
net-logger-ebpf/src/main.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext};
|
||||
use aya_log_ebpf::info;
|
||||
pub mod stract_data;
|
||||
#[xdp]
|
||||
pub fn net_logger(ctx: XdpContext) -> u32 {
|
||||
match stract_data::trafic_router(ctx) {
|
||||
Ok(ret) => ret,
|
||||
Err(_) => xdp_action::XDP_ABORTED,
|
||||
}
|
||||
}
|
||||
|
||||
fn try_net_logger(ctx: XdpContext) -> Result<u32, u32> {
|
||||
info!(&ctx, "received a packet");
|
||||
Ok(xdp_action::XDP_PASS)
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
||||
unsafe { core::hint::unreachable_unchecked() }
|
||||
}
|
||||
113
net-logger-ebpf/src/stract_data.rs
Normal file
113
net-logger-ebpf/src/stract_data.rs
Normal file
@@ -0,0 +1,113 @@
|
||||
use aya_ebpf::{
|
||||
bindings::xdp_action,
|
||||
macros::map,
|
||||
programs::XdpContext,
|
||||
maps::PerfEventArray,
|
||||
maps::PerCpuArray,
|
||||
};
|
||||
use aya_log_ebpf::info;
|
||||
use net_logger_common::Event;
|
||||
use core::mem;
|
||||
use network_types::{
|
||||
eth::{EthHdr, EtherType},
|
||||
ip::{IpProto, Ipv4Hdr, Ipv6Hdr, in6_addr},
|
||||
tcp::TcpHdr,
|
||||
udp::UdpHdr,
|
||||
icmp::IcmpHdr,
|
||||
};
|
||||
|
||||
#[map]
|
||||
pub static mut SCRATCH: PerCpuArray<Event> = PerCpuArray::with_max_entries(1, 0); // per-cpu
|
||||
|
||||
#[map]
|
||||
pub static mut EVENTS: PerfEventArray<Event> = PerfEventArray::with_max_entries(0, 0);
|
||||
|
||||
#[inline(always)] //
|
||||
fn ptr_at<T>(ctx: &XdpContext, offset: usize) -> Result<*const T, ()> {
|
||||
let start = ctx.data();
|
||||
let end = ctx.data_end();
|
||||
let len = mem::size_of::<T>();
|
||||
|
||||
if start + offset + len > end {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
Ok((start + offset) as *const T)
|
||||
}
|
||||
|
||||
pub fn trafic_router(ctx: XdpContext) -> Result<u32, ()> {
|
||||
let ethhdr: *const EthHdr = ptr_at(&ctx, 0)?; //
|
||||
match unsafe { (*ethhdr).ether_type } {
|
||||
EtherType::Ipv4 => process_v4(ctx),
|
||||
EtherType::Ipv6 => process_v6(ctx),
|
||||
_ => Ok(xdp_action::XDP_PASS),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_ports(ctx: &XdpContext, protocol: IpProto, ip_hdr_len: usize) -> Result<(u16, u16),()> {
|
||||
let source_port;
|
||||
let dest_port;
|
||||
match protocol {
|
||||
IpProto::Tcp => {
|
||||
let tcphdr: *const TcpHdr =
|
||||
ptr_at(ctx, EthHdr::LEN + ip_hdr_len)?;
|
||||
source_port = u16::from_be(unsafe { (*tcphdr).source });
|
||||
dest_port = u16::from_be(unsafe { (*tcphdr).dest });
|
||||
}
|
||||
IpProto::Udp => {
|
||||
let udphdr: *const UdpHdr =
|
||||
ptr_at(ctx, EthHdr::LEN + ip_hdr_len)?;
|
||||
source_port = u16::from_be(unsafe { (*udphdr).source });
|
||||
dest_port = u16::from_be(unsafe { (*udphdr).dest });
|
||||
}
|
||||
_ => {
|
||||
source_port = 0;
|
||||
dest_port = 0;
|
||||
},
|
||||
};
|
||||
Ok((source_port, dest_port))
|
||||
}
|
||||
|
||||
pub fn process_v6(ctx: XdpContext) -> Result<u32, ()> {
|
||||
let ipv6hdr: *const Ipv6Hdr = ptr_at(&ctx, EthHdr::LEN)?;
|
||||
let source_addr = unsafe { (*ipv6hdr).src_addr() };
|
||||
let dest_addr = unsafe { (*ipv6hdr).dst_addr() };
|
||||
let protocol = unsafe { (*ipv6hdr).next_hdr };
|
||||
let pack_len = unsafe { (*ipv6hdr).payload_len };
|
||||
let ports = get_ports(&ctx, protocol, Ipv6Hdr::LEN)?;
|
||||
|
||||
let mut event = unsafe { *SCRATCH.get_ptr_mut(0).ok_or(())? };
|
||||
event.ipv = EtherType::Ipv6;
|
||||
event.source_port = ports.0;
|
||||
event.source_ipv6 = source_addr;
|
||||
event.dest_port = ports.1;
|
||||
event.dest_ipv6 = dest_addr;
|
||||
event.proto = protocol;
|
||||
event.len = pack_len;
|
||||
unsafe { EVENTS.output(&ctx, &mut event, 0); }
|
||||
|
||||
Ok(xdp_action::XDP_PASS)
|
||||
}
|
||||
|
||||
pub fn process_v4(ctx: XdpContext) -> Result<u32, ()> {
|
||||
|
||||
let ipv4hdr: *const Ipv4Hdr = ptr_at(&ctx, EthHdr::LEN)?;
|
||||
let source_addr = u32::from_be(unsafe { (*ipv4hdr).src_addr });
|
||||
let dest_addr = u32::from_be(unsafe { (*ipv4hdr).dst_addr });
|
||||
let protocol = unsafe { (*ipv4hdr).proto };
|
||||
let pack_len = unsafe { (*ipv4hdr).tot_len };
|
||||
let ports = get_ports(&ctx, protocol, Ipv4Hdr::LEN)?;
|
||||
|
||||
let mut event = unsafe { *SCRATCH.get_ptr_mut(0).ok_or(())? };
|
||||
|
||||
event.ipv = EtherType::Ipv4;
|
||||
event.source_port = ports.0;
|
||||
event.source_ipv4 = source_addr;
|
||||
event.dest_port = ports.1;
|
||||
event.dest_ipv4 = dest_addr;
|
||||
event.proto = protocol;
|
||||
event.len = pack_len;
|
||||
unsafe { EVENTS.output(&ctx, &mut event, 0); }
|
||||
|
||||
Ok(xdp_action::XDP_PASS)
|
||||
}
|
||||
Reference in New Issue
Block a user