BPF Netfilter перехватывает средуLinux

Ответить
Anonymous
 BPF Netfilter перехватывает среду

Сообщение Anonymous »

Я пытаюсь создать простую программу BPF для отбрасывания входящих/исходящих пакетов и приема только пересылаемых пакетов, которые будут загружаться с помощью bpftool.
Я использую Ubuntu 24.04 с ядром Linux версии 6.8 . Это моя программа:

Код: Выделить всё

#include 

#include 
#include 
#include 

SEC("netfilter") /* hint to BPF loader that this is an netfilter BPF program */
int filter(const struct bpf_nf_ctx *ctx) {
const struct nf_hook_state *state = ctx->state;
unsigned int routing_decision = state->hook;

if (routing_decision == NF_INET_LOCAL_IN ||
routing_decision == NF_INET_LOCAL_OUT)
return NF_DROP;

return NF_ACCEPT;
}

char LICENSE[] SEC("license") = "GPL";
Однако, когда я пытаюсь скомпилировать его в объектный файл, я получаю:

Код: Выделить всё

clang --target=bpf -O2 -Wall -I/usr/include/x86_64-linux-gnu -c filter.c -o filter.bpf.o
filter.c:8:27: warning: declaration of 'struct bpf_nf_ctx' will not be visible outside of this function [-Wvisibility]
8 | int filter(const struct bpf_nf_ctx *ctx) {
|                           ^
filter.c:9:42: error: incomplete definition of type 'struct bpf_nf_ctx'
9 |   const struct nf_hook_state *state = ctx->state;
|                                       ~~~^
filter.c:8:27: note: forward declaration of 'struct bpf_nf_ctx'
8 | int filter(const struct bpf_nf_ctx *ctx) {
|                           ^
filter.c:10:29: error: incomplete definition of type 'struct bpf_nf_ctx'
10 |   int routing_decision = ctx->state->hook;
|                          ~~~^
filter.c:8:27: note: forward declaration of 'struct bpf_nf_ctx'
8 | int filter(const struct bpf_nf_ctx *ctx) {
|                           ^
1 warning and 2 errors generated.
После того, как структура bpf_nf_ctx определена в nf_bpf_link.h, я добавил соответствующие включения (

Код: Выделить всё

#include 
, когда полный путь — /usr/src/linux-headers-6.8.0-48/include/net/netfilter/nf_bpf_link.h), и это был результат:

Код: Выделить всё

clang --target=bpf -O2 -Wall -I/usr/include/x86_64-linux-gnu -I/usr/src/linux-headers-6.8.0-48/include -c filter.c -o filter.bpf.o
In file included from filter.c:1:
In file included from /usr/src/linux-headers-6.8.0-48/include/linux/bpf.h:8:
In file included from /usr/src/linux-headers-6.8.0-48/include/uapi/linux/filter.h:9:
/usr/src/linux-headers-6.8.0-48/include/linux/compiler.h:251:10: fatal error: 'asm/rwonce.h' file not found
251 | #include 
|          ^~~~~~~~~~~~~~
1 error generated.
make: *** [Makefile:7: all] Error 1
Как правильно настроить среду для компиляции программы BPF с использованием перехватчиков netfilter?


Подробнее здесь: https://stackoverflow.com/questions/791 ... nvironment
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Linux»