diff options
author | Jo-Philipp Wich <jo@mein.io> | 2025-02-03 14:08:29 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2025-02-04 10:21:39 +0100 |
commit | 001ced17982fffb4132be81d5a59dfe749becc60 (patch) | |
tree | c9187f89f7d6b32e12a91100f747614a760616b4 /lib.c | |
parent | 6dd08716205fb974215521fd03ebcaa96c26b031 (diff) |
socket: properly support SO_ATTACH_FILTER sockopt
The SO_ATTACH_FILTER socket option requires special handling as it is not
passing a self-contained structure to the kernel but a pointer to user
memory holding the actual BPF bytecode.
In order to properly support this, first rework the ucode value to C struct
conversion callback machinery to pass an indirect struct base pointer,
allowing conversion callbacks to realloc the struct memory as needed.
Finally introduce custom uv to C conversion for the BPF data which accepts
either a raw bytecode string, an array of opcode arrays or a flat array of
opcode values which are converted into a C array of struct sock_filter
records appended to the reallocated struct memory.
Attaching a BPF program equivalent to the tcpdump expression
`vlan 20 && ether proto 0x1234` would then look like this:
sock.setopt(SOL_SOCKET, SO_ATTACH_FILTER, {
filter: [
[ 0x28, 0, 0, 0x0000000c ],
[ 0x15, 2, 0, 0x00008100 ],
[ 0x15, 1, 0, 0x000088a8 ],
[ 0x15, 0, 6, 0x00009100 ],
[ 0x28, 0, 0, 0x0000000e ],
[ 0x54, 0, 0, 0x00000fff ],
[ 0x15, 0, 3, 0x00000014 ],
[ 0x28, 0, 0, 0x00000010 ],
[ 0x15, 0, 1, 0x00001234 ],
[ 0x6, 0, 0, 0x00040000 ],
[ 0x6, 0, 0, 0x00000000 ],
]
});
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib.c')
0 files changed, 0 insertions, 0 deletions