1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use aya_ebpf_cty::{c_long, c_void};
use crate::{
EbpfContext, bindings::__sk_buff, helpers::bpf_skb_under_cgroup, programs::sk_buff::SkBuff,
};
pub(crate) mod sealed {
#[expect(unnameable_types, reason = "this is the sealed trait pattern")]
pub trait CgroupArrayMap {
fn as_ptr(&self) -> *mut core::ffi::c_void;
}
}
/// Map types that [`TcContext::skb_under_cgroup`] can target.
///
/// This trait is sealed; aya implements it for both the legacy
/// [`crate::maps::CgroupArray`] and the BTF [`crate::btf_maps::CgroupArray`].
pub trait CgroupArrayMap: sealed::CgroupArrayMap {}
impl<T: sealed::CgroupArrayMap> CgroupArrayMap for T {}
pub struct TcContext {
pub skb: SkBuff,
}
impl TcContext {
pub const fn new(skb: *mut __sk_buff) -> Self {
let skb = SkBuff { skb };
Self { skb }
}
#[inline]
pub fn len(&self) -> u32 {
self.skb.len()
}
#[inline]
pub fn data(&self) -> usize {
self.skb.data()
}
#[inline]
pub fn data_end(&self) -> usize {
self.skb.data_end()
}
#[inline]
pub fn set_mark(&self, mark: u32) {
self.skb.set_mark(mark);
}
#[inline]
pub fn cb(&self) -> &[u32] {
self.skb.cb()
}
/// Returns a mutable slice to the control buffer (cb).
#[inline]
pub fn cb_mut(&mut self) -> &mut [u32] {
self.skb.cb_mut()
}
/// Returns the owner UID of the socket associated to the SKB context.
#[inline]
pub fn get_socket_uid(&self) -> u32 {
self.skb.get_socket_uid()
}
#[inline]
pub fn load<T>(&self, offset: usize) -> Result<T, c_long> {
self.skb.load(offset)
}
/// Reads some bytes from the packet into the specified buffer, returning
/// how many bytes were read.
///
/// Starts reading at `offset` and reads at most `dst.len()` or
/// `self.len() - offset` bytes, depending on which one is smaller.
///
/// # Examples
///
/// Read into a `PerCpuArray`.
///
/// ```no_run
/// use core::mem;
///
/// use aya_ebpf::{bindings::TC_ACT_PIPE, macros::map, maps::PerCpuArray, programs::TcContext};
/// # #[expect(non_camel_case_types)]
/// # struct ethhdr {};
/// # #[expect(non_camel_case_types)]
/// # struct iphdr {};
/// # #[expect(non_camel_case_types)]
/// # struct tcphdr {};
///
/// const ETH_HDR_LEN: usize = mem::size_of::<ethhdr>();
/// const IP_HDR_LEN: usize = mem::size_of::<iphdr>();
/// const TCP_HDR_LEN: usize = mem::size_of::<tcphdr>();
///
/// #[repr(C)]
/// pub struct Buf {
/// pub buf: [u8; 1500],
/// }
///
/// #[map]
/// pub static BUF: PerCpuArray<Buf> = PerCpuArray::with_max_entries(1, 0);
///
/// fn try_classifier(ctx: TcContext) -> Result<i32, i32> {
/// let buf = unsafe {
/// let ptr = BUF.get_ptr_mut(0).ok_or(TC_ACT_PIPE)?;
/// &mut *ptr
/// };
/// let offset = ETH_HDR_LEN + IP_HDR_LEN + TCP_HDR_LEN;
/// ctx.load_bytes(offset, &mut buf.buf).map_err(|_| TC_ACT_PIPE)?;
///
/// // do something with `buf`
///
/// Ok(TC_ACT_PIPE)
/// }
/// ```
#[inline(always)]
pub fn load_bytes(&self, offset: usize, dst: &mut [u8]) -> Result<usize, c_long> {
self.skb.load_bytes(offset, dst)
}
#[inline]
pub fn store<T>(&self, offset: usize, v: &T, flags: u64) -> Result<(), c_long> {
self.skb.store(offset, v, flags)
}
#[inline]
pub fn l3_csum_replace(
&self,
offset: usize,
from: u64,
to: u64,
size: u64,
) -> Result<(), c_long> {
self.skb.l3_csum_replace(offset, from, to, size)
}
#[inline]
pub fn l4_csum_replace(
&self,
offset: usize,
from: u64,
to: u64,
flags: u64,
) -> Result<(), c_long> {
self.skb.l4_csum_replace(offset, from, to, flags)
}
#[inline]
pub fn adjust_room(&self, len_diff: i32, mode: u32, flags: u64) -> Result<(), c_long> {
self.skb.adjust_room(len_diff, mode, flags)
}
#[inline]
pub fn clone_redirect(&self, if_index: u32, flags: u64) -> Result<(), c_long> {
self.skb.clone_redirect(if_index, flags)
}
#[inline]
pub fn change_proto(&self, proto: u16, flags: u64) -> Result<(), c_long> {
self.skb.change_proto(proto, flags)
}
#[inline]
pub fn change_type(&self, ty: u32) -> Result<(), c_long> {
self.skb.change_type(ty)
}
/// Pulls in non-linear data in case the skb is non-linear.
///
/// Make len bytes from skb readable and writable. If a zero value is passed for
/// `len`, then the whole length of the skb is pulled. This helper is only needed
/// for reading and writing with direct packet access.
///
/// # Examples
///
/// ```no_run
/// use aya_ebpf::programs::TcContext;
/// # #[expect(non_camel_case_types)]
/// # struct ethhdr {};
/// # #[expect(non_camel_case_types)]
/// # struct iphdr {};
/// # #[expect(non_camel_case_types)]
/// # struct udphdr {};
///
/// const ETH_HLEN: usize = core::mem::size_of::<ethhdr>();
/// const IP_HLEN: usize = core::mem::size_of::<iphdr>();
/// const UDP_HLEN: usize = core::mem::size_of::<udphdr>();
///
/// fn try_classifier(ctx: TcContext) -> Result<i32, i32> {
/// let len = ETH_HLEN + IP_HLEN + UDP_HLEN;
/// match ctx.pull_data(len as u32) {
/// Ok(()) => Ok(0),
/// Err(ret) => Err(ret as i32),
/// }
/// }
/// ```
#[inline(always)]
pub fn pull_data(&self, len: u32) -> Result<(), c_long> {
self.skb.pull_data(len)
}
/// Returns whether the skb is a descendant of the cgroup at `index`.
///
/// Wraps the `bpf_skb_under_cgroup` helper. This is only callable from
/// classifier programs.
///
/// # Minimum kernel version
///
/// The minimum kernel version required to use this feature is 4.8.
#[inline]
pub fn skb_under_cgroup<M: CgroupArrayMap>(&self, map: &M, index: u32) -> Result<bool, c_long> {
// SAFETY: `self.skb.skb` and `map` are valid pointers managed by aya.
let ret = unsafe { bpf_skb_under_cgroup(self.skb.skb, map.as_ptr().cast(), index) };
match ret {
1 => Ok(true),
0 => Ok(false),
ret => Err(ret),
}
}
}
impl EbpfContext for TcContext {
fn as_ptr(&self) -> *mut c_void {
self.skb.as_ptr()
}
}