Reputation: 3
struct sock_filter code[] = {
{BPF_LD | BPF_MEM, 0, 0, 0x00000000}, // load memory to accumulator
//{BPF_ALU | BPF_ADD, 0, 0, 0x00000001}, // add 1
//{BPF_ALU | BPF_MOD, 0, 0, 0x00000003}, //module 3
//{BPF_ST , 0, 0, 0x00000000}, // save in scratch memory
//{BPF_RET | BPF_A, 0, 0, 0x00000000}, // return value in accumulator
};
struct sock_filter code[] = {
{BPF_RET, 0, 0, 0x00000000}
};
My code is running fine.
What is problem in 1st bpf program??
I tried executing with both bpf program, expecting that first bpf program attach succesfully to my udp socket.
Upvotes: -1
Views: 68
Reputation: 3
Both the BPF program is valid. I made mistake when declaring struct sock_fprog.
Initially it was
struct sock_fprog bpf = {
.len = 1,
.filter = code,
};
So that's why bpf program
struct sock_filter code[] = {
{BPF_RET, 0, 0, 0x00000000}
};
run correctly as it has only one instruction.
Now i changed the struct code to run for any number of instructions:
struct sock_fprog bpf = {
.len = sizeof(code)/sizeof(code[0]),
.filter = code,
};
Upvotes: 0
Reputation: 7978
All possible control flows must end with a BPF_RET operation.
The following should also be valid:
struct sock_filter code[] = {
{BPF_LD | BPF_MEM, 0, 0, 0x00000000}, // load memory to accumulator
//{BPF_ALU | BPF_ADD, 0, 0, 0x00000001}, // add 1
//{BPF_ALU | BPF_MOD, 0, 0, 0x00000003}, //module 3
//{BPF_ST , 0, 0, 0x00000000}, // save in scratch memory
{BPF_RET | BPF_A, 0, 0, 0x00000000}, // return value in accumulator
};
Upvotes: 0