Skip to main content

aya_ebpf/programs/
sock_ops.rs

1use core::ffi::c_void;
2
3use aya_ebpf_bindings::helpers::bpf_sock_ops_cb_flags_set;
4
5use crate::{bindings::bpf_sock_ops, EbpfContext};
6
7pub struct SockOpsContext {
8    pub ops: *mut bpf_sock_ops,
9}
10
11impl SockOpsContext {
12    pub fn new(ops: *mut bpf_sock_ops) -> SockOpsContext {
13        SockOpsContext { ops }
14    }
15
16    pub fn op(&self) -> u32 {
17        unsafe { (*self.ops).op }
18    }
19
20    pub fn family(&self) -> u32 {
21        unsafe { (*self.ops).family }
22    }
23
24    pub fn cb_flags(&self) -> u32 {
25        unsafe { (*self.ops).bpf_sock_ops_cb_flags }
26    }
27
28    pub fn set_cb_flags(&self, flags: i32) -> Result<(), i64> {
29        let ret = unsafe { bpf_sock_ops_cb_flags_set(self.ops, flags) };
30        if ret == 0 {
31            Ok(())
32        } else {
33            Err(ret)
34        }
35    }
36
37    pub fn remote_ip4(&self) -> u32 {
38        unsafe { (*self.ops).remote_ip4 }
39    }
40
41    pub fn local_ip4(&self) -> u32 {
42        unsafe { (*self.ops).local_ip4 }
43    }
44
45    pub fn remote_ip6(&self) -> [u32; 4] {
46        unsafe { (*self.ops).remote_ip6 }
47    }
48
49    pub fn local_ip6(&self) -> [u32; 4] {
50        unsafe { (*self.ops).local_ip6 }
51    }
52
53    pub fn local_port(&self) -> u32 {
54        unsafe { (*self.ops).local_port }
55    }
56
57    pub fn remote_port(&self) -> u32 {
58        unsafe { (*self.ops).remote_port }
59    }
60
61    pub fn arg(&self, n: usize) -> u32 {
62        unsafe { (*self.ops).__bindgen_anon_1.args[n] }
63    }
64
65    pub fn set_reply(&mut self, reply: u32) {
66        unsafe { (*self.ops).__bindgen_anon_1.reply = reply }
67    }
68}
69
70impl EbpfContext for SockOpsContext {
71    fn as_ptr(&self) -> *mut c_void {
72        self.ops as *mut _
73    }
74}