<s>
The	O
Berkeley	B-Protocol
Packet	I-Protocol
Filter	I-Protocol
(	O
BPF	O
)	O
is	O
a	O
technology	O
used	O
in	O
certain	O
computer	B-General_Concept
operating	I-General_Concept
systems	I-General_Concept
for	O
programs	O
that	O
need	O
to	O
,	O
among	O
other	O
things	O
,	O
analyze	O
network	B-Architecture
traffic	O
.	O
</s>
<s>
It	O
provides	O
a	O
raw	O
interface	O
to	O
data	B-Protocol
link	I-Protocol
layers	I-Protocol
,	O
permitting	O
raw	O
link-layer	O
packets	O
to	O
be	O
sent	O
and	O
received	O
.	O
</s>
<s>
In	O
addition	O
,	O
if	O
the	O
driver	O
for	O
the	O
network	B-Architecture
interface	O
supports	O
promiscuous	B-Protocol
mode	I-Protocol
,	O
it	O
allows	O
the	O
interface	O
to	O
be	O
put	O
into	O
that	O
mode	O
so	O
that	O
all	O
packets	O
on	O
the	O
network	B-Architecture
can	O
be	O
received	O
,	O
even	O
those	O
destined	O
to	O
other	O
hosts	O
.	O
</s>
<s>
BPF	O
supports	O
filtering	O
packets	O
,	O
allowing	O
a	O
userspace	B-Operating_System
process	B-Operating_System
to	O
supply	O
a	O
filter	O
program	O
that	O
specifies	O
which	O
packets	O
it	O
wants	O
to	O
receive	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
tcpdump	B-Protocol
process	B-Operating_System
may	O
want	O
to	O
receive	O
only	O
packets	O
that	O
initiate	O
a	O
TCP	O
connection	O
.	O
</s>
<s>
BPF	O
returns	O
only	O
packets	O
that	O
pass	O
the	O
filter	O
that	O
the	O
process	B-Operating_System
supplies	O
.	O
</s>
<s>
This	O
avoids	O
copying	O
unwanted	O
packets	O
from	O
the	O
operating	B-Operating_System
system	I-Operating_System
kernel	I-Operating_System
to	O
the	O
process	B-Operating_System
,	O
greatly	O
improving	O
performance	O
.	O
</s>
<s>
The	O
filter	O
program	O
is	O
in	O
the	O
form	O
of	O
instructions	O
for	O
a	O
virtual	B-Architecture
machine	I-Architecture
,	O
which	O
are	O
interpreted	O
,	O
or	O
compiled	O
into	O
machine	B-Language
code	I-Language
by	O
a	O
just-in-time	O
(	O
JIT	O
)	O
mechanism	O
and	O
executed	O
,	O
in	O
the	O
kernel	B-Operating_System
.	O
</s>
<s>
Some	O
systems	O
,	O
such	O
as	O
Linux	B-Operating_System
and	O
Tru64	B-Operating_System
UNIX	I-Operating_System
,	O
provide	O
a	O
raw	O
interface	O
to	O
the	O
data	B-Protocol
link	I-Protocol
layer	I-Protocol
other	O
than	O
the	O
BPF	O
raw	O
interface	O
but	O
use	O
the	O
BPF	O
filtering	O
mechanisms	O
for	O
that	O
raw	O
interface	O
.	O
</s>
<s>
The	O
BPF	O
filtering	O
mechanism	O
is	O
available	O
on	O
most	O
Unix-like	B-Operating_System
operating	I-Operating_System
systems	I-Operating_System
.	O
</s>
<s>
The	O
Linux	B-Operating_System
kernel	I-Operating_System
provides	O
an	O
extended	O
version	O
of	O
the	O
BPF	O
filtering	O
mechanism	O
,	O
called	O
eBPF	B-Protocol
,	O
which	O
uses	O
a	O
JIT	O
mechanism	O
,	O
and	O
which	O
is	O
used	O
for	O
packet	O
filtering	O
,	O
as	O
well	O
as	O
for	O
other	O
purposes	O
in	O
the	O
kernel	B-Operating_System
.	O
</s>
<s>
eBPF	B-Protocol
is	O
also	O
available	O
for	O
Microsoft	B-Application
Windows	I-Application
.	O
</s>
<s>
BPF	O
provides	O
pseudo-devices	B-Application
that	O
can	O
be	O
bound	O
to	O
a	O
network	B-Architecture
interface	O
;	O
reads	O
from	O
the	O
device	O
will	O
read	O
buffers	O
full	O
of	O
packets	O
received	O
on	O
the	O
network	B-Architecture
interface	O
,	O
and	O
writes	O
to	O
the	O
device	O
will	O
inject	O
packets	O
on	O
the	O
network	B-Architecture
interface	O
.	O
</s>
<s>
In	O
2007	O
,	O
Robert	O
Watson	O
and	O
Christian	O
Peron	O
added	O
zero-copy	O
buffer	O
extensions	O
to	O
the	O
BPF	O
implementation	O
in	O
the	O
FreeBSD	B-Operating_System
operating	B-General_Concept
system	I-General_Concept
,	O
allowing	O
kernel	B-Operating_System
packet	O
capture	O
in	O
the	O
device	O
driver	O
interrupt	O
handler	O
to	O
write	O
directly	O
to	O
user	O
process	B-Operating_System
memory	O
in	O
order	O
to	O
avoid	O
the	O
requirement	O
for	O
two	O
copies	O
for	O
all	O
packet	O
data	O
received	O
via	O
the	O
BPF	O
device	O
.	O
</s>
<s>
BPF	O
's	O
filtering	O
capabilities	O
are	O
implemented	O
as	O
an	O
interpreter	O
for	O
a	O
machine	B-Language
language	I-Language
for	O
the	O
BPF	O
virtual	B-Architecture
machine	I-Architecture
,	O
a	O
32-bit	O
machine	O
with	O
fixed-length	O
instructions	O
,	O
one	O
accumulator	B-General_Concept
,	O
and	O
one	O
index	B-General_Concept
register	I-General_Concept
.	O
</s>
<s>
Traditional	O
Unix-like	B-Operating_System
BPF	O
implementations	O
can	O
be	O
used	O
in	O
userspace	B-Operating_System
,	O
despite	O
being	O
written	O
for	O
kernel-space	B-Operating_System
.	O
</s>
<s>
This	O
is	O
accomplished	O
using	O
preprocessor	B-General_Concept
conditions	O
.	O
</s>
<s>
Some	O
platforms	O
,	O
including	O
FreeBSD	B-Operating_System
,	O
NetBSD	B-Device
,	O
and	O
WinPcap	B-Language
,	O
use	O
a	O
just-in-time	O
(	O
JIT	O
)	O
compiler	O
to	O
convert	O
BPF	O
instructions	O
into	O
native	B-Language
code	I-Language
in	O
order	O
to	O
improve	O
performance	O
.	O
</s>
<s>
Linux	B-Operating_System
includes	O
a	O
BPF	O
JIT	O
compiler	O
which	O
is	O
disabled	O
by	O
default	O
.	O
</s>
<s>
Kernel-mode	O
interpreters	O
for	O
that	O
same	O
virtual	B-Architecture
machine	I-Architecture
language	O
are	O
used	O
in	O
raw	O
data	B-Protocol
link	I-Protocol
layer	I-Protocol
mechanisms	O
in	O
other	O
operating	B-General_Concept
systems	I-General_Concept
,	O
such	O
as	O
Tru64	B-Operating_System
Unix	I-Operating_System
,	O
and	O
for	O
socket	O
filters	O
in	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
and	O
in	O
the	O
WinPcap	B-Language
and	O
Npcap	O
packet	O
capture	O
mechanism	O
.	O
</s>
<s>
Since	O
version	O
3.18	O
,	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
includes	O
an	O
extended	O
BPF	O
virtual	B-Architecture
machine	I-Architecture
with	O
ten	O
64-bit	O
registers	O
,	O
termed	O
extended	O
BPF	O
(	O
eBPF	B-Protocol
)	O
.	O
</s>
<s>
It	O
can	O
be	O
used	O
for	O
non-networking	O
purposes	O
,	O
such	O
as	O
for	O
attaching	O
eBPF	B-Protocol
programs	O
to	O
various	O
tracepoints	B-Application
.	O
</s>
<s>
Since	O
kernel	B-Operating_System
version	O
3.19	O
,	O
eBPF	B-Protocol
filters	O
can	O
be	O
attached	O
to	O
sockets	B-Protocol
,	O
and	O
,	O
since	O
kernel	B-Operating_System
version	O
4.1	O
,	O
to	O
traffic	B-Application
control	I-Application
classifiers	O
for	O
the	O
ingress	O
and	O
egress	O
networking	O
data	O
path	O
.	O
</s>
<s>
Nowadays	O
,	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
runs	O
eBPF	B-Protocol
only	O
and	O
loaded	O
cBPF	O
bytecode	O
is	O
transparently	O
translated	O
into	O
an	O
eBPF	B-Protocol
representation	O
in	O
the	O
kernel	B-Operating_System
before	O
program	O
execution	O
.	O
</s>
<s>
Until	O
Linux	B-Operating_System
5.3	O
,	O
the	O
verifier	O
prohibited	O
the	O
use	O
of	O
loops	O
,	O
to	O
prevent	O
potentially	O
unbounded	O
execution	O
times	O
;	O
loops	O
with	O
bounded	O
execution	O
time	O
are	O
now	O
permitted	O
in	O
more	O
recent	O
kernels	B-Operating_System
.	O
</s>
<s>
A	O
user-mode	O
interpreter	O
for	O
BPF	O
is	O
provided	O
with	O
the	O
libpcap/WinPcap/Npcap	O
implementation	O
of	O
the	O
pcap	B-Language
API	B-Application
,	O
so	O
that	O
,	O
when	O
capturing	O
packets	O
on	O
systems	O
without	O
kernel-mode	O
support	O
for	O
that	O
filtering	O
mechanism	O
,	O
packets	O
can	O
be	O
filtered	O
in	O
user	B-Operating_System
mode	I-Operating_System
;	O
code	O
using	O
the	O
pcap	B-Language
API	B-Application
will	O
work	O
on	O
both	O
types	O
of	O
systems	O
,	O
although	O
,	O
on	O
systems	O
where	O
the	O
filtering	O
is	O
done	O
in	O
user	B-Operating_System
mode	I-Operating_System
,	O
all	O
packets	O
,	O
including	O
those	O
that	O
will	O
be	O
filtered	O
out	O
,	O
are	O
copied	O
from	O
the	O
kernel	B-Operating_System
to	O
user	B-Operating_System
space	I-Operating_System
.	O
</s>
<s>
That	O
interpreter	O
can	O
also	O
be	O
used	O
when	O
reading	O
a	O
file	O
containing	O
packets	O
captured	O
using	O
pcap	B-Language
.	O
</s>
<s>
Another	O
user-mode	O
interpreter	O
is	O
uBPF	O
,	O
which	O
supports	O
JIT	O
and	O
eBPF	B-Protocol
(	O
without	O
cBPF	O
)	O
.	O
</s>
<s>
Its	O
code	O
has	O
been	O
reused	O
to	O
provide	O
eBPF	B-Protocol
support	O
in	O
non-Linux	O
systems	O
.	O
</s>
<s>
Microsoft	O
's	O
eBPF	B-Protocol
on	O
Windows	B-Application
builds	O
on	O
uBPF	O
and	O
the	O
PREVAIL	O
formal	O
verifier	O
.	O
</s>
<s>
rBPF	O
,	O
a	O
Rust	O
rewrite	O
of	O
uBPF	O
,	O
is	O
used	O
by	O
the	O
Solana	B-Protocol
blockchain	O
platform	O
as	O
the	O
execution	O
engine	O
.	O
</s>
<s>
One	O
such	O
representation	O
is	O
found	O
in	O
libpcap	B-Language
.	O
</s>
<s>
Classic	O
BPF	O
and	O
eBPF	B-Protocol
can	O
also	O
be	O
written	O
either	O
directly	O
as	O
machine	B-Language
code	I-Language
,	O
or	O
using	O
an	O
assembly	B-Language
language	I-Language
for	O
a	O
textual	O
representation	O
.	O
</s>
<s>
Notable	O
assemblers	B-Language
include	O
Linux	B-Operating_System
kernel	I-Operating_System
's	O
tool	O
(	O
cBPF	O
)	O
,	O
(	O
cBPF	O
)	O
,	O
and	O
the	O
assembler	B-Language
(	O
eBPF	B-Protocol
)	O
.	O
</s>
<s>
The	O
assembly	B-Language
languages	I-Language
are	O
not	O
necessarily	O
compatible	O
with	O
each	O
other	O
.	O
</s>
<s>
eBPF	B-Protocol
bytecode	O
has	O
recently	O
become	O
a	O
target	O
of	O
higher-level	O
languages	O
.	O
</s>
<s>
LLVM	B-Application
added	O
eBPF	B-Protocol
support	O
in	O
2014	O
,	O
and	O
GCC	B-Application
followed	O
in	O
2019	O
.	O
</s>
<s>
Both	O
toolkits	O
allow	O
compiling	O
C	B-Language
and	O
other	O
supported	O
languages	O
to	O
eBPF	B-Protocol
.	O
</s>
<s>
A	O
subset	O
of	O
P4	B-Language
can	O
also	O
be	O
compiled	O
into	O
eBPF	B-Protocol
using	O
BCC	O
,	O
an	O
LLVM-based	O
compiler	O
kit	O
.	O
</s>
<s>
In	O
August	O
2003	O
,	O
SCO	O
Group	O
publicly	O
claimed	O
that	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
was	O
infringing	O
Unix	O
code	O
which	O
they	O
owned	O
.	O
</s>
<s>
Programmers	O
quickly	O
discovered	O
that	O
one	O
example	O
they	O
gave	O
was	O
the	O
Berkeley	B-Protocol
Packet	I-Protocol
Filter	I-Protocol
,	O
which	O
in	O
fact	O
SCO	O
never	O
owned	O
.	O
</s>
<s>
The	O
Spectre	B-Error_Name
attack	I-Error_Name
could	O
leverage	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
's	O
eBPF	B-Protocol
interpreter	O
or	O
JIT	O
compiler	O
to	O
extract	O
data	O
from	O
other	O
kernel	B-Operating_System
processes	O
.	O
</s>
<s>
A	O
JIT	O
hardening	O
feature	O
in	O
the	O
kernel	B-Operating_System
mitigates	O
this	O
vulnerability	O
.	O
</s>
<s>
Chinese	O
computer	O
security	O
group	O
Pangu	O
Lab	O
said	O
the	O
NSA	O
used	O
BPF	O
to	O
conceal	O
network	B-Architecture
communications	O
as	O
part	O
of	O
a	O
complex	O
Linux	B-Operating_System
backdoor	O
.	O
</s>
