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
// This file is part of linux-support. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/linux-support/master/COPYRIGHT. No part of linux-support, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2020 The developers of linux-support. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/linux-support/master/COPYRIGHT.
/// Supports attaching and detaching programs.
pub trait ExtendedBpfProgramCanBeAttachedFileDescriptor: FileDescriptor
{
/// Program attachment type.
type ProgramAttachmentType: ProgramAttachmentType;
/// Program query flags, if any.
type ProgramQueryFlags: ProgramQueryFlags;
/// Program attachment flags, if any.
type ProgramAttachmentFlags: ProgramAttachmentFlags;
/// Program attachment options, if any.
type ProgramAttachmentOptions: ProgramAttachmentOptions;
#[doc(hidden)]
const InitialProgramCountGuess: usize;
/// Requires the capability `CAP_NET_ADMIN`.
///
/// `query_flags` is only appropriate for queries on `CgroupFileDescriptor`.
/// `attach_flags` is only appropriate for queries on `CgroupFileDescriptor`.
///
/// Returns an error if permission is denied.
///
/// Works for the following `attach_types`:-
///
/// * `CgroupFileDescriptor`.
/// * `BPF_PROG_TYPE_CGROUP_SKB`.
/// * `BPF_CGROUP_INET_INGRESS`.
/// * `BPF_CGROUP_INET_EGRESS`.
/// * `BPF_PROG_TYPE_CGROUP_SOCK`
/// * `BPF_CGROUP_INET_SOCK_CREATE`.
/// * `BPF_CGROUP_INET4_POST_BIND`.
/// * `BPF_CGROUP_INET6_POST_BIND`.
/// * `BPF_PROG_TYPE_CGROUP_SOCK_ADDR`.
/// * `BPF_CGROUP_INET4_BIND`.
/// * `BPF_CGROUP_INET6_BIND`.
/// * `BPF_CGROUP_INET4_CONNECT`.
/// * `BPF_CGROUP_INET6_CONNECT`.
/// * `BPF_CGROUP_UDP4_SENDMSG`.
/// * `BPF_CGROUP_UDP6_SENDMSG`.
/// * `BPF_CGROUP_UDP4_RECVMSG`.
/// * `BPF_CGROUP_UDP6_RECVMSG`.
/// * `BPF_PROG_TYPE_SOCK_OPS`.
/// * `BPF_CGROUP_SOCK_OPS`.
/// * `BPF_PROG_TYPE_CGROUP_DEVICE`.
/// * `BPF_CGROUP_DEVICE`.
/// * `BPF_CGROUP_SYSCTL`.
/// * `BPF_PROG_TYPE_CGROUP_SYSCTL`.
/// * `BPF_PROG_TYPE_CGROUP_SOCKOPT`.
/// * `BPF_CGROUP_GETSOCKOPT`.
/// * `BPF_CGROUP_SETSOCKOPT`.
/// * `NetworkNamespaceFileDescriptor`.
/// * `BPF_FLOW_DISSECTOR`.
/// * `BPF_PROG_TYPE_FLOW_DISSECTOR`.
/// * `LinuxInfraRedRemoteControlRawMode2FileDescriptor`.
/// * `BPF_LIRC_MODE2`.
/// * `BPF_PROG_TYPE_LIRC_MODE2`.
///
/// Unsupported for the `attach_types`:-
/// * `BPF_PROG_TYPE_SK_MSG`.
/// * `BPF_SK_MSG_VERDICT`.
/// * `BPF_PROG_TYPE_SK_SKB`.
/// * `BPF_SK_SKB_STREAM_PARSER`.
/// * `BPF_SK_SKB_STREAM_VERDICT`.
#[inline(always)]
fn query_attached_extended_bpf_programs(&self, program_attachment_type: Self::ProgramAttachmentType, program_query_flags: Self::ProgramQueryFlags) -> Result<(Vec<ExtendedBpfProgramIdentifier>, Self::ProgramAttachmentFlags), ()>
{
let mut programs: Vec<ExtendedBpfProgramIdentifier> = Vec::with_capacity(Self::InitialProgramCountGuess);
let mut attr = bpf_attr::default();
let attach_type = program_attachment_type.to_bpf_attach_type();
attr.query = BpfCommandProgramQuery
{
target_fd: self.as_raw_fd(),
attach_type,
query_flags: program_query_flags.to_query_flags(),
attach_flags: BPF_PROG_ATTACH_flags::empty(),
prog_ids: AlignedU64::from(programs.as_mut_ptr()),
prog_cnt: programs.capacity() as u32,
};
loop
{
let result = attr.syscall(bpf_cmd::BPF_PROG_QUERY);
if likely!(result == 0)
{
unsafe { programs.set_len(attr.query.prog_cnt as usize) };
programs.shrink_to_fit();
let program_attachment_flags = Self::ProgramAttachmentFlags::parse(unsafe { attr.query.attach_flags });
return Ok((programs, program_attachment_flags))
}
else if likely!(result == -1)
{
match errno().0
{
// Only returned as of Linux 5.6 for CgroupFileDescriptor.
ENOSPC =>
{
let required = unsafe { attr.query.prog_cnt as usize };
debug_assert!(required > programs.capacity());
programs.reserve(required - programs.capacity());
continue
}
EPERM => return Err(()),
EINVAL => panic!("Invalid attr or invalid attach type"),
EFAULT => panic!("Fault copying to / from userspace"),
errno @ _ => panic!("Unexpected error `{}`", errno),
}
}
else
{
unreachable_code(format_args!("Unexpected result `{}` from bpf(BPF_PROG_QUERY)", result))
}
}
}
/// Requires the capability `CAP_NET_ADMIN`.
///
/// Supported attach types by program type (found in the Linux kernel functions `bpf_prog_attach()` and `attach_type_to_prog_type()`).
///
/// * `CgroupFileDescriptor`.
/// * `BPF_PROG_TYPE_CGROUP_SKB`.
/// * `BPF_CGROUP_INET_INGRESS`.
/// * `BPF_CGROUP_INET_EGRESS`.
/// * `BPF_PROG_TYPE_CGROUP_SOCK`
/// * `BPF_CGROUP_INET_SOCK_CREATE`.
/// * `BPF_CGROUP_INET4_POST_BIND`.
/// * `BPF_CGROUP_INET6_POST_BIND`.
/// * `BPF_PROG_TYPE_CGROUP_SOCK_ADDR`.
/// * `BPF_CGROUP_INET4_BIND`.
/// * `BPF_CGROUP_INET6_BIND`.
/// * `BPF_CGROUP_INET4_CONNECT`.
/// * `BPF_CGROUP_INET6_CONNECT`.
/// * `BPF_CGROUP_UDP4_SENDMSG`.
/// * `BPF_CGROUP_UDP6_SENDMSG`.
/// * `BPF_CGROUP_UDP4_RECVMSG`.
/// * `BPF_CGROUP_UDP6_RECVMSG`.
/// * `BPF_PROG_TYPE_SOCK_OPS`.
/// * `BPF_CGROUP_SOCK_OPS`.
/// * `BPF_PROG_TYPE_CGROUP_DEVICE`.
/// * `BPF_CGROUP_DEVICE`.
/// * `BPF_CGROUP_SYSCTL`.
/// * `BPF_PROG_TYPE_CGROUP_SYSCTL`.
/// * `BPF_PROG_TYPE_CGROUP_SOCKOPT`.
/// * `BPF_CGROUP_GETSOCKOPT`.
/// * `BPF_CGROUP_SETSOCKOPT`.
/// * `NetworkNamespaceFileDescriptor`.
/// * `BPF_FLOW_DISSECTOR`.
/// * `BPF_PROG_TYPE_FLOW_DISSECTOR`.
/// * `LinuxInfraRedRemoteControlMode2FileDescriptor`.
/// * `BPF_LIRC_MODE2`.
/// * `BPF_PROG_TYPE_LIRC_MODE2`.
///
/// * `sock_map_get_from_fd` (A `MapFileDescriptor` for a map of type `BPF_MAP_TYPE_SOCKMAP` or `BPF_MAP_TYPE_SOCKHASH`).
/// * `BPF_PROG_TYPE_SK_MSG`.
/// * `BPF_SK_MSG_VERDICT`.
/// * `BPF_PROG_TYPE_SK_SKB`.
/// * `BPF_SK_SKB_STREAM_PARSER`.
/// * `BPF_SK_SKB_STREAM_VERDICT`.
///
/// The program type referred to in `attach_program` must be compatible with the `program_attachment_type`.
///
/// Does not support programs loaded onto to devices (`NetworkInterfaceIndex`).
///
/// Fails if `self` has too many programs attached (the norm is 64 for Linux Infra Red Control, PerfEvent and CGroup).
#[inline(always)]
fn attach(&self, program_attachment_type: Self::ProgramAttachmentType, attach_program: &ExtendedBpfProgramFileDescriptor, program_attachment_options: Self::ProgramAttachmentOptions) -> Result<(), ()>
{
let (attach_flags, replace_bpf_fd) = program_attachment_options.to_attach_flags();
let mut attr = bpf_attr::default();
attr.program_attach_or_detach = BpfCommandProgramAttachOrDetach
{
target_fd: self.as_raw_fd(),
attach_bpf_fd: attach_program.as_raw_fd(),
attach_type: program_attachment_type.to_bpf_attach_type(),
attach_flags,
replace_bpf_fd,
};
let result = attr.syscall(bpf_cmd::BPF_PROG_ATTACH);
if likely!(result == 0)
{
Ok(())
}
else if likely!(result == -1)
{
match errno().0
{
EINVAL => panic!("Invalid attr or invalid attachment type to attach"),
EPERM => panic!("Permission denied"),
EEXIST => panic!("BPF_PROG_TYPE_FLOW_DISSECTOR only allows one program to exist at a time"),
E2BIG => Err(()),
ENODEV => panic!("BPF_PROG_TYPE_LIRC_MODE2 only supports raw mode 2"),
EOPNOTSUPP => panic!("BPF_PROG_TYPE_SK_* failure"),
errno @ _ => panic!("Unexpected error `{}`", errno),
}
}
else
{
unreachable_code(format_args!("Unexpected result `{}` from bpf(BPF_PROG_ATTACH)", result))
}
}
/// Requires the capability `CAP_NET_ADMIN`.
///
/// Supported attach types by program type (found in the Linux kernel functions `bpf_prog_attach()` and `attach_type_to_prog_type()`).
///
/// * `CgroupFileDescriptor`.
/// * `BPF_PROG_TYPE_CGROUP_SKB`.
/// * `BPF_CGROUP_INET_INGRESS`.
/// * `BPF_CGROUP_INET_EGRESS`.
/// * `BPF_PROG_TYPE_CGROUP_SOCK`
/// * `BPF_CGROUP_INET_SOCK_CREATE`.
/// * `BPF_CGROUP_INET4_POST_BIND`.
/// * `BPF_CGROUP_INET6_POST_BIND`.
/// * `BPF_PROG_TYPE_CGROUP_SOCK_ADDR`.
/// * `BPF_CGROUP_INET4_BIND`.
/// * `BPF_CGROUP_INET6_BIND`.
/// * `BPF_CGROUP_INET4_CONNECT`.
/// * `BPF_CGROUP_INET6_CONNECT`.
/// * `BPF_CGROUP_UDP4_SENDMSG`.
/// * `BPF_CGROUP_UDP6_SENDMSG`.
/// * `BPF_CGROUP_UDP4_RECVMSG`.
/// * `BPF_CGROUP_UDP6_RECVMSG`.
/// * `BPF_PROG_TYPE_SOCK_OPS`.
/// * `BPF_CGROUP_SOCK_OPS`.
/// * `BPF_PROG_TYPE_CGROUP_DEVICE`.
/// * `BPF_CGROUP_DEVICE`.
/// * `BPF_CGROUP_SYSCTL`.
/// * `BPF_PROG_TYPE_CGROUP_SYSCTL`.
/// * `BPF_PROG_TYPE_CGROUP_SOCKOPT`.
/// * `BPF_CGROUP_GETSOCKOPT`.
/// * `BPF_CGROUP_SETSOCKOPT`.
/// * `NetworkNamespaceFileDescriptor`.
/// * `BPF_FLOW_DISSECTOR`.
/// * `BPF_PROG_TYPE_FLOW_DISSECTOR`.
/// * `LinuxInfraRedRemoteControlMode2FileDescriptor`.
/// * `BPF_LIRC_MODE2`.
/// * `BPF_PROG_TYPE_LIRC_MODE2`.
///
/// * `sock_map_get_from_fd` (A `MapFileDescriptor` for a map of type `type, BPF_MAP_TYPE_SOCKMAP` or `BPF_MAP_TYPE_SOCKHASH`).
/// * `BPF_PROG_TYPE_SK_MSG`.
/// * `BPF_SK_MSG_VERDICT`.
/// * `BPF_PROG_TYPE_SK_SKB`.
/// * `BPF_SK_SKB_STREAM_PARSER`.
/// * `BPF_SK_SKB_STREAM_VERDICT`.
#[inline(always)]
fn detach(&self, program_attachment_type: Self::ProgramAttachmentType, attach_program: Option<&ExtendedBpfProgramFileDescriptor>)
{
let mut attr = bpf_attr::default();
attr.program_attach_or_detach = BpfCommandProgramAttachOrDetach
{
target_fd: self.as_raw_fd(),
attach_bpf_fd: match attach_program
{
None => 0,
Some(file_descriptor) => file_descriptor.as_raw_fd(),
},
attach_type: program_attachment_type.to_bpf_attach_type(),
attach_flags: BPF_PROG_ATTACH_flags::empty(),
replace_bpf_fd: 0
};
let result = attr.syscall(bpf_cmd::BPF_PROG_DETACH);
if likely!(result == 0)
{
}
else if likely!(result == -1)
{
match errno().0
{
EINVAL => panic!("Invalid attr or invalid attachment type to detach"),
EPERM => panic!("Permission denied"),
errno @ _ => panic!("Unexpected error `{}`", errno),
}
}
else
{
unreachable_code(format_args!("Unexpected result `{}` from bpf(BPF_PROG_DETACH)", result))
}
}
}