Skip to content

Program type BPF_PROG_TYPE_SOCKET_FILTER

v3.19

Socket filter programs can hook into network sockets and are designed to filter or modify packets received by that socket (the program isn't called for egress/outgoing packets).

A noticeable use-case for this program type is tcpdump which uses a raw sockets in combination with a socket filter generated from the filter query to efficiently filter packets and only pay the kernel-userspace barrier cost for packets of interest.

Usage

Socket filter programs are typically put into an ELF section prefixed with socket. The socket filter is called by the kernel with a __sk_buff context. The return value from indicates how many bytes of the message should be kept. Returning a value less then the side of the packet will truncate it and returning 0 will discard the packet.

Context

This program type isn't allowed to read from and write to all fields of the context since doing so might break assumptions in the kernel or because data isn't available at the point where the program is hooked into the kernel.

Context fields
Field Read Write
len
pkt_type
mark
queue_mapping
protocol
vlan_present
vlan_tci
vlan_proto
priority
ingress_ifindex
ifindex
tc_index
cb
hash
tc_classid
data
data_end
napi_id
family
remote_ip4
local_ip4
remote_ip4
remote_ip6
local_ip6
remote_port
local_port
data_meta
flow_keys
tstamp
wire_len
tstamp
gso_segs
sk
gso_size
tstamp_type
hwtstamp

Attachment

This program type can be attached to network sockets using the setsockopt syscall with the SOL_SOCKET socket level and SO_ATTACH_BPF socket option.

Helper functions

Not all helper functions are available in all program types. These are the helper calls available for socket filter programs:

Supported helper functions

KFuncs

Supported kfuncs

Examples

Program example

Attachment example

History

Socket filters pre-date eBPF itself, socket filters were the first ever prototype in the original BPF implementation, now referred to as cBPF (classic BPF). In fact, usage of this program type was the reason for inventing the whole system1.

Change log