Skip to content

Commit e5490a0

Browse files
fix: address comments
1 parent f54bff7 commit e5490a0

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

bpf-prog/block-iptables/bpf/src/block_iptables.bpf.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ volatile const u64 host_netns_inode = 4026531840; // Initialized by userspace
3131

3232
struct {
3333
__uint(type, BPF_MAP_TYPE_ARRAY);
34-
__uint(max_entries, 1);
34+
__uint(max_entries, 2);
3535
__type(key, u32);
3636
__type(value, u64);
3737
__uint(pinning, LIBBPF_PIN_BY_NAME);
@@ -69,12 +69,10 @@ bool is_allowed_parent ()
6969
}
7070

7171
if(match) {
72-
bpf_printk("Allowed netlink from parent: %s\n", parent_comm);
7372
return 1;
7473
}
7574
}
7675

77-
7876
return 0; // Block
7977
}
8078

@@ -104,10 +102,10 @@ bool is_host_ns() {
104102
return 1;
105103
}
106104

107-
// Increment the event counter in the BPF map.
108-
// This counter will be read from usersace to track the number of blocked events.
109-
void increment_event_counter() {
110-
u32 key = 0;
105+
// Increment the event counters in the BPF map. Key is 0 for blocked rules and 1 for allowed rules.
106+
// This counter will be read from userspace to track the number of blocked/allowed events.
107+
void increment_event_counter(bool isAllow) {
108+
u32 key = isAllow ? 1 : 0;
111109
u64 *value;
112110

113111
value = bpf_map_lookup_elem(&iptables_block_event_counter, &key);
@@ -132,9 +130,14 @@ int BPF_PROG(iptables_legacy_block, struct socket *sock, int level, int optname)
132130
//iptables-legacy uses IPT_SO_SET_REPLACE to install rules
133131
if (optname == IPT_SO_SET_REPLACE) {
134132
// block if not in host network namespace, and if the parent process is not allowed
135-
if (is_host_ns() && !is_allowed_parent()) {
136-
increment_event_counter();
137-
return -EPERM;
133+
if (is_host_ns()) {
134+
if (!is_allowed_parent()) {
135+
increment_event_counter(false);
136+
return -EPERM;
137+
} else {
138+
increment_event_counter(true);
139+
return 0; // Allow the operation
140+
}
138141
}
139142
}
140143
}
@@ -205,9 +208,11 @@ int BPF_PROG(iptables_nftables_block, struct sock *sk, struct sk_buff *skb) {
205208
// and whether we are in the host network namespace.
206209
// If not allowed, increment the event counter and return -EPERM.
207210
if(is_allowed_parent()) {
208-
return 0;
211+
increment_event_counter(true);
212+
// Allow the operation
213+
return 0;
209214
} else {
210-
increment_event_counter();
215+
increment_event_counter(false);
211216
return -EPERM;
212217
}
213218
}

0 commit comments

Comments
 (0)