Skip to content

Commit 4922446

Browse files
d-e-s-odanielocfb
authored andcommitted
Add CHANGELOG entry for #980
Add a CHANGELOG entry for pull request #980, which added support for attaching to netfilter hooks. Also fix a few minor things that slipped through review and remove tests for one more hook which is not available on a local system and causing test failures because of it. Signed-off-by: Daniel Müller <[email protected]>
1 parent dfb28a4 commit 4922446

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

examples/netfilter_blocklist/src/bpf/netfilter_blocklist.bpf.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ int netfilter_local_in(struct bpf_nf_ctx *ctx) {
5151
/* To view log output, use: cat /sys/kernel/debug/tracing/trace_pipe */
5252
__be32 addr_host = bpf_ntohl(key.addr);
5353
bpf_printk("Blocked IP: %d.%d.%d.%d, prefix length: %d, map value: %d\n",
54-
(addr_host >> 24) & 0xFF, (addr_host >> 16) & 0xFF,
55-
(addr_host >> 8) & 0xFF, addr_host & 0xFF,
54+
(addr_host >> 24) & 0xFF, (addr_host >> 16) & 0xFF,
55+
(addr_host >> 8) & 0xFF, addr_host & 0xFF,
5656
key.prefixlen, *match_value);
5757
return NF_DROP;
5858
}

libbpf-rs/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Unreleased
2+
----------
3+
- Added `Program::attach_netfilter_with_opts` for attaching to netfilter
4+
hooks
5+
6+
17
0.24.5
28
------
39
- Renamed `Program::get_id_by_fd` to `id_from_fd`

libbpf-rs/tests/bin/src/netfilter.bpf.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int handle_netfilter(struct bpf_nf_ctx *ctx) {
2121

2222
*value = 1;
2323
bpf_ringbuf_submit(value, 0);
24-
24+
2525
bpf_printk("handle_netfilter: submitted ringbuf value");
2626
return NF_ACCEPT;
2727
}

libbpf-rs/tests/test_netfilter.rs

+30-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#[allow(dead_code)]
22
mod common;
33

4+
use std::net::IpAddr;
5+
use std::net::Ipv4Addr;
6+
use std::net::Ipv6Addr;
7+
use std::net::TcpListener;
48
use std::net::TcpStream;
59

610
use libbpf_rs::NetfilterOpts;
@@ -9,7 +13,6 @@ use libbpf_rs::Object;
913
use libbpf_rs::NFPROTO_IPV4;
1014
use libbpf_rs::NFPROTO_IPV6;
1115

12-
use libbpf_rs::NF_INET_LOCAL_IN;
1316
use libbpf_rs::NF_INET_POST_ROUTING;
1417
use libbpf_rs::NF_INET_PRE_ROUTING;
1518

@@ -28,63 +31,54 @@ fn test_attach_and_detach(obj: &mut Object, protocol_family: i32, hooknum: i32,
2831
hooknum,
2932
..NetfilterOpts::default()
3033
};
31-
let error_message = format!(
32-
"Failed to attach netfilter protocol {}, hook: {}",
33-
protocol_family, hook_desc
34-
);
3534
let link = prog
3635
.attach_netfilter_with_opts(netfilter_opt)
37-
.expect(&error_message);
36+
.unwrap_or_else(|err| {
37+
panic!(
38+
"Failed to attach netfilter protocol {}, hook: {}: {err}",
39+
protocol_family, hook_desc
40+
)
41+
});
3842

3943
let map = get_map_mut(obj, "ringbuf");
4044

41-
let trigger_addr = match protocol_family {
42-
NFPROTO_IPV4 => Some("127.0.0.1:12345"),
43-
NFPROTO_IPV6 => Some("[::1]:12345"),
44-
_ => {
45-
println!("unknow protocol family");
46-
None
45+
let addr = match protocol_family {
46+
NFPROTO_IPV4 => IpAddr::V4(Ipv4Addr::LOCALHOST),
47+
NFPROTO_IPV6 => IpAddr::V6(Ipv6Addr::LOCALHOST),
48+
_ => panic!("unknow protocol family: {protocol_family}"),
49+
};
50+
// We let the kernel decide what port to bind to.
51+
let listener = TcpListener::bind((addr, 0)).unwrap();
52+
let trigger_addr = listener.local_addr().unwrap();
53+
54+
let result = match hooknum {
55+
NF_INET_PRE_ROUTING | NF_INET_POST_ROUTING => {
56+
let action = || {
57+
let _ = TcpStream::connect(trigger_addr);
58+
};
59+
with_ringbuffer(&map, action)
4760
}
61+
_ => panic!("unsupported hook: {hooknum} ({hook_desc})"),
4862
};
49-
50-
if let Some(trigger_addr) = trigger_addr {
51-
let result = match hook_desc {
52-
"PRE_ROUTING" | "LOCAL_IN" | "LOCAL_OUT" | "POST_ROUTING" => {
53-
let action = || {
54-
let _ = TcpStream::connect(trigger_addr);
55-
};
56-
with_ringbuffer(&map, action)
57-
}
58-
"FORWARD" => 1,
59-
_ => {
60-
panic!("unknow hook")
61-
}
62-
};
63-
assert_eq!(result, 1);
64-
}
63+
assert_eq!(result, 1);
6564
assert!(link.detach().is_ok());
6665
}
6766

68-
// Only selected hooks are tested due to CI failures on certain hooks (e.g., FORWARD, LOCAL_OUT).
69-
// Although these hooks might work in actual use, they were removed from automated testing to
70-
// ensure consistent CI results and maintainability. This approach allows the focus to remain
71-
// on primary netfilter paths (e.g., PRE_ROUTING, LOCAL_IN, POST_ROUTING) that have stable CI
72-
// support. These hooks may be re-added for automated testing in the future if CI compatibility
73-
// improves or specific needs arise.
7467
#[tag(root)]
7568
#[test]
7669
fn test_netfilter() {
7770
bump_rlimit_mlock();
7871
let mut obj = get_test_object("netfilter.bpf.o");
7972

73+
// We don't test all hooks here, because support for some may be
74+
// more limited.
75+
8076
// IPv4 hook
8177
test_attach_and_detach(&mut obj, NFPROTO_IPV4, NF_INET_PRE_ROUTING, "PRE_ROUTING");
82-
test_attach_and_detach(&mut obj, NFPROTO_IPV4, NF_INET_LOCAL_IN, "LOCAL_IN");
8378
test_attach_and_detach(&mut obj, NFPROTO_IPV4, NF_INET_POST_ROUTING, "POST_ROUTING");
8479

8580
// IPv6 hook
8681
test_attach_and_detach(&mut obj, NFPROTO_IPV6, NF_INET_PRE_ROUTING, "PRE_ROUTING");
87-
test_attach_and_detach(&mut obj, NFPROTO_IPV6, NF_INET_LOCAL_IN, "LOCAL_IN");
8882
test_attach_and_detach(&mut obj, NFPROTO_IPV6, NF_INET_POST_ROUTING, "POST_ROUTING");
8983
}
9084

0 commit comments

Comments
 (0)