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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use ;
use Error;
use size_of_val;
use RawFd;
use null;
/// Represents a single BPF instruction (operation).
///
/// This struct directly maps to the Linux kernel's `sock_filter` structure
/// used for BPF programs. Each operation consists of:
/// - a 16-bit code that defines the operation
/// - 8-bit jump targets for true/false conditions
/// - a 32-bit immediate constant value (k)
///
/// The memory layout must match the kernel's expectation, so we use `repr(C)`.
/// Represents a complete BPF program, consisting of a sequence of operations.
///
/// This struct directly maps to the Linux kernel's `sock_fprog` structure.
/// A program is an array of BPF instructions that are executed sequentially
/// by the kernel to filter packets.
///
/// The memory layout must match the kernel's expectation, so we use `repr(C)`.
/// The struct manages the memory of the operations to ensure safety.
// No longer need custom Drop impl as we're using proper Rust ownership
const SO_ATTACH_FILTER: c_int = 26;
const SO_DETACH_FILTER: c_int = 27;
const SO_LOCK_FILTER: c_int = 44;
/// Macro for creating BPF programs with a more concise syntax.
///
/// This macro allows you to create BPF programs by specifying the operations
/// as a sequence of `code jt jf k` tuples, making it easier to translate BPF
/// assembly code into Rust.
///
/// # Parameters
///
/// * `$count` - The number of operations in the program (for capacity pre-allocation)
/// * `$code $jt $jf $k` - Repeated tuples of operation code, jump-true offset,
/// jump-false offset, and k-value for each operation
///
/// # Examples
///
/// ```
/// use bpf::bpfprog;
///
/// // Create a BPF program that accepts only IPv4 TCP packets
/// let filter = bpfprog!(4,
/// 0x28 0 0 0x0000000c, // ldh [12] ; load ethertype
/// 0x15 0 2 0x00000800, // jeq #0x800, L1, L3 ; if IPv4, goto L1, else L3
/// 0x30 0 0 0x00000017, // ldb [23] ; load protocol
/// 0x15 0 1 0x00000006 // jeq #6, L2, L3 ; if TCP, accept, else drop
/// );
/// ```
/// Attaches a BPF filter program to a socket.
///
/// Once attached, the BPF program will filter all incoming packets on the
/// socket. Only packets that match the filter criteria will be delivered
/// to the application.
///
/// # Parameters
///
/// * `fd` - Raw file descriptor of the socket
/// * `prog` - The BPF program to attach
///
/// # Returns
///
/// * `Ok(())` if the filter was successfully attached
/// * `Err(Error)` with the system error if attachment failed
///
/// # Examples
///
/// ```
/// use bpf::{bpfprog, attach_filter};
/// use std::net::UdpSocket;
/// use std::os::unix::io::AsRawFd;
///
/// let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
/// let filter = bpfprog!(1, 0x06 0 0 0x00000001); // ret #1 (accept 1 byte)
///
/// // Attach the filter to the socket
/// let result = attach_filter(socket.as_raw_fd(), filter);
/// ```
///
/// # Safety
///
/// This function is safe to call, but internally uses unsafe code to interact
/// with the operating system. The `fd` must refer to a valid socket.
/// Detaches any BPF filter program from a socket.
///
/// This removes any previously attached filter, allowing all packets to be
/// delivered to the application again.
///
/// # Parameters
///
/// * `fd` - Raw file descriptor of the socket
///
/// # Returns
///
/// * `Ok(())` if the filter was successfully detached
/// * `Err(Error)` with the system error if detachment failed
///
/// # Examples
///
/// ```
/// use bpf::detach_filter;
/// use std::net::UdpSocket;
/// use std::os::unix::io::AsRawFd;
///
/// let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
///
/// // Detach any filter from the socket
/// let result = detach_filter(socket.as_raw_fd());
/// ```
///
/// # Safety
///
/// This function is safe to call, but internally uses unsafe code to interact
/// with the operating system. The `fd` must refer to a valid socket.
/// Locks the BPF filter on a socket to prevent it from being replaced.
///
/// Once locked, the filter cannot be modified or removed for the lifetime
/// of the socket. This is a security measure to prevent privilege escalation
/// attacks where a program running with lower privileges might try to replace
/// a filter set by a privileged program.
///
/// # Parameters
///
/// * `fd` - Raw file descriptor of the socket
///
/// # Returns
///
/// * `Ok(())` if the filter was successfully locked
/// * `Err(Error)` with the system error if locking failed
///
/// # Examples
///
/// ```
/// use bpf::{bpfprog, attach_filter, lock_filter};
/// use std::net::UdpSocket;
/// use std::os::unix::io::AsRawFd;
///
/// let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
/// let filter = bpfprog!(1, 0x06 0 0 0x00000001); // ret #1 (accept 1 byte)
///
/// // Attach the filter to the socket
/// attach_filter(socket.as_raw_fd(), filter).unwrap();
///
/// // Lock the filter to prevent it from being modified
/// let result = lock_filter(socket.as_raw_fd());
/// ```
///
/// # Safety
///
/// This function is safe to call, but internally uses unsafe code to interact
/// with the operating system. The `fd` must refer to a valid socket.
///
/// # Note
///
/// This operation is irreversible for the lifetime of the socket.