-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a73ff0
commit b81af34
Showing
8 changed files
with
215 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use core::mem; | ||
|
||
use aya_ebpf::{bindings::TC_ACT_PIPE, helpers::bpf_get_current_pid_tgid, programs::TcContext}; | ||
use network_types::{ | ||
eth::EthHdr, | ||
ip::{IpProto, Ipv4Hdr}, | ||
tcp::TcpHdr, | ||
udp::UdpHdr, | ||
}; | ||
use nflux_common::EgressConfig; | ||
|
||
use crate::logger::log_connection; | ||
|
||
#[inline] | ||
fn ptr_at<T>(ctx: &TcContext, 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 handle_icmp_packet( | ||
ctx: &TcContext, | ||
egress_config: &EgressConfig, | ||
destination: u32, | ||
) -> Result<i32, ()> { | ||
let pid_tgid = { bpf_get_current_pid_tgid() }; | ||
let pid = pid_tgid >> 32; | ||
|
||
if egress_config.log_icmp_connections == 1 { | ||
unsafe { | ||
log_connection( | ||
ctx, | ||
egress_config.log_only_new_connections, | ||
destination, | ||
0, | ||
0, | ||
IpProto::Icmp as u8, | ||
pid, | ||
) | ||
}; | ||
} | ||
|
||
Ok(TC_ACT_PIPE) | ||
} | ||
|
||
pub fn handle_tcp_packet( | ||
ctx: &TcContext, | ||
egress_config: &EgressConfig, | ||
destination: u32, | ||
) -> Result<i32, ()> { | ||
let tcphdr: *const TcpHdr = ptr_at(&ctx, EthHdr::LEN + Ipv4Hdr::LEN)?; | ||
|
||
let src_port = u16::from_be((unsafe { *tcphdr }).source); | ||
let dst_port = u16::from_be((unsafe { *tcphdr }).dest); | ||
let protocol = IpProto::Tcp as u8; | ||
let pid_tgid = { bpf_get_current_pid_tgid() }; | ||
let pid = pid_tgid >> 32; | ||
|
||
if egress_config.log_tcp_connections == 1 { | ||
unsafe { | ||
log_connection( | ||
ctx, | ||
egress_config.log_only_new_connections, | ||
destination, | ||
src_port, | ||
dst_port, | ||
protocol, | ||
pid, | ||
) | ||
}; | ||
} | ||
|
||
Ok(TC_ACT_PIPE) | ||
} | ||
|
||
pub fn handle_udp_packet( | ||
ctx: &TcContext, | ||
egress_config: &EgressConfig, | ||
destination: u32, | ||
) -> Result<i32, ()> { | ||
let udphdr: *const UdpHdr = ptr_at(&ctx, EthHdr::LEN + Ipv4Hdr::LEN)?; | ||
let src_port = u16::from_be((unsafe { *udphdr }).source); | ||
let dst_port = u16::from_be((unsafe { *udphdr }).dest); | ||
let protocol = IpProto::Udp as u8; | ||
let pid_tgid = { bpf_get_current_pid_tgid() }; | ||
let pid = pid_tgid >> 32; | ||
|
||
if egress_config.log_udp_connections == 1 { | ||
unsafe { | ||
log_connection( | ||
ctx, | ||
egress_config.log_only_new_connections, | ||
destination, | ||
src_port, | ||
dst_port, | ||
protocol, | ||
pid, | ||
) | ||
}; | ||
} | ||
|
||
Ok(TC_ACT_PIPE) | ||
} |
Oops, something went wrong.