xref: /freebsd/share/man/man9/bpf.9 (revision 571dba6ec9f25ecf7582dc2192daf1ceea70065f)
183676de9SDavid Malone.\" Copyright (c) 2004 FreeBSD Inc.
283676de9SDavid Malone.\" All rights reserved.
383676de9SDavid Malone.\"
483676de9SDavid Malone.\" Redistribution and use in source and binary forms, with or without
583676de9SDavid Malone.\" modification, are permitted provided that the following conditions
683676de9SDavid Malone.\" are met:
783676de9SDavid Malone.\" 1. Redistributions of source code must retain the above copyright
883676de9SDavid Malone.\"    notice, this list of conditions and the following disclaimer.
983676de9SDavid Malone.\" 2. Redistributions in binary form must reproduce the above copyright
1083676de9SDavid Malone.\"    notice, this list of conditions and the following disclaimer in the
1183676de9SDavid Malone.\"    documentation and/or other materials provided with the distribution.
1283676de9SDavid Malone.\"
1383676de9SDavid Malone.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1483676de9SDavid Malone.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1583676de9SDavid Malone.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1683676de9SDavid Malone.\" ARE DISCLAIMED.  IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
1783676de9SDavid Malone.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1883676de9SDavid Malone.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1983676de9SDavid Malone.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2083676de9SDavid Malone.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2183676de9SDavid Malone.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2283676de9SDavid Malone.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2383676de9SDavid Malone.\" SUCH DAMAGE.
2483676de9SDavid Malone.\"
2583676de9SDavid Malone.\" $FreeBSD$
2683676de9SDavid Malone.\"
2783676de9SDavid Malone.Dd May 19, 2004
2883676de9SDavid Malone.Dt BPF 9
2983676de9SDavid Malone.Os
3083676de9SDavid Malone.\"
3183676de9SDavid Malone.Sh NAME
3283676de9SDavid Malone.Nm bpf
3383676de9SDavid Malone.Nd "Berkeley Packet Filter"
3483676de9SDavid Malone.\"
3583676de9SDavid Malone.Sh SYNOPSIS
3683676de9SDavid Malone.In net/bpf.h
3783676de9SDavid Malone.\"
3883676de9SDavid Malone.Ft void
3983676de9SDavid Malone.Fn bpfattach "struct ifnet *ifp" "u_int dlt" "u_int hdrlen"
4083676de9SDavid Malone.Ft void
41457432e8SRuslan Ermilov.Fo bpfattach2
42457432e8SRuslan Ermilov.Fa "struct ifnet *ifp" "u_int dlt" "u_int hdrlen" "struct bpf_if **driverp"
43457432e8SRuslan Ermilov.Fc
4483676de9SDavid Malone.Ft void
4583676de9SDavid Malone.Fn bpfdetach "struct ifnet *ifp"
4683676de9SDavid Malone.Ft void
4783676de9SDavid Malone.Fn bpf_tap "struct ifnet *ifp" "u_char *pkt" "u_int *pktlen"
4883676de9SDavid Malone.Ft void
4983676de9SDavid Malone.Fn bpf_mtap "struct ifnet *ifp" "struct mbuf *m"
5083676de9SDavid Malone.Ft void
514291a598SGleb Smirnoff.Fn bpf_mtap2 "struct bpf_if *bp" "void *data" "u_int dlen" "struct mbuf *m"
5283676de9SDavid Malone.Ft u_int
53457432e8SRuslan Ermilov.Fo bpf_filter
54457432e8SRuslan Ermilov.Fa "const struct bpf_insn *pc " "u_char *pkt" "u_int *wirelen" "u_int *buflen"
55457432e8SRuslan Ermilov.Fc
5683676de9SDavid Malone.Ft int
5783676de9SDavid Malone.Fn bpf_validate "const struct bpf_insn *fcode" "int flen"
5883676de9SDavid Malone.\"
5983676de9SDavid Malone.Sh DESCRIPTION
6083676de9SDavid MaloneThe Berkeley Packet Filter provides a raw interface,
6183676de9SDavid Malonethat is protocol independent,
6283676de9SDavid Maloneto data link layers.
6383676de9SDavid MaloneIt allows all packets on the network,
6483676de9SDavid Maloneeven those destined for other hosts,
6583676de9SDavid Maloneto be passed from a network interface to user programs.
6683676de9SDavid MaloneEach program may specify a filter,
67457432e8SRuslan Ermilovin the form of a
68457432e8SRuslan Ermilov.Nm
69457432e8SRuslan Ermilovfilter machine program.
70457432e8SRuslan ErmilovThe
7183676de9SDavid Malone.Xr bpf 4
72571dba6eSHiten Pandyamanual page
7383676de9SDavid Malonedescribes the interface used by user programs.
74571dba6eSHiten PandyaThis manual page describes the functions used by interfaces to pass packets to
7583676de9SDavid Malone.Nm
7683676de9SDavid Maloneand the functions for testing and running
7783676de9SDavid Malone.Nm
7883676de9SDavid Malonefilter machine programs.
7983676de9SDavid Malone.Pp
80457432e8SRuslan ErmilovThe
8183676de9SDavid Malone.Fn bpfattach
82457432e8SRuslan Ermilovfunction
8383676de9SDavid Maloneattaches a network interface to
8483676de9SDavid Malone.Nm .
85457432e8SRuslan ErmilovThe
86457432e8SRuslan Ermilov.Fa ifp
87457432e8SRuslan Ermilovargument
8883676de9SDavid Maloneis a pointer to the structure that defines the interface to be
8983676de9SDavid Maloneattached to an interface.
90457432e8SRuslan ErmilovThe
91457432e8SRuslan Ermilov.Fa dlt
92457432e8SRuslan Ermilovargument
9383676de9SDavid Maloneis the data link-layer type:
94457432e8SRuslan Ermilov.Dv DLT_NULL
95457432e8SRuslan Ermilov(no link-layer encapsulation),
96457432e8SRuslan Ermilov.Dv DLT_EN10MB
97457432e8SRuslan Ermilov(Ethernet),
98457432e8SRuslan Ermilov.Dv DLT_IEEE802_11
99457432e8SRuslan Ermilov(802.11 wireless networks),
10083676de9SDavid Maloneetc.
10183676de9SDavid MaloneThe rest of the link layer types can be found in
102457432e8SRuslan Ermilov.In net/bpf.h .
103457432e8SRuslan ErmilovThe
104457432e8SRuslan Ermilov.Fa hdrlen
105457432e8SRuslan Ermilovargument
10683676de9SDavid Maloneis the fixed size of the link header;
10783676de9SDavid Malonevariable length headers are not yet supported.
10883676de9SDavid MaloneThe
10983676de9SDavid Malone.Nm
11083676de9SDavid Malonesystem will hold a pointer to
111457432e8SRuslan Ermilov.Fa ifp->if_bpf .
112457432e8SRuslan ErmilovThis variable will set to a
113457432e8SRuslan Ermilov.Pf non- Dv NULL
114457432e8SRuslan Ermilovvalue when
11583676de9SDavid Malone.Nm
11683676de9SDavid Malonerequires packets from this interface to be tapped using the functions below.
11783676de9SDavid Malone.Pp
118457432e8SRuslan ErmilovThe
11983676de9SDavid Malone.Fn bpfattach2
120457432e8SRuslan Ermilovfunction
121457432e8SRuslan Ermilovallows multiple
122457432e8SRuslan Ermilov.Nm
123457432e8SRuslan Ermilovinstances to be attached to a single interface,
12483676de9SDavid Maloneby registering an explicit
125457432e8SRuslan Ermilov.Fa if_bpf
12683676de9SDavid Malonerather than using
127457432e8SRuslan Ermilov.Fa ifp->if_bpf .
12883676de9SDavid MaloneIt is then possible to run
12983676de9SDavid Malone.Xr tcpdump 1
13083676de9SDavid Maloneon the interface for any data link-layer types attached.
13183676de9SDavid Malone.Pp
132457432e8SRuslan ErmilovThe
13383676de9SDavid Malone.Fn bpfdetach
134457432e8SRuslan Ermilovfunction detaches a
13583676de9SDavid Malone.Nm
13683676de9SDavid Maloneinstance from an interface,
13783676de9SDavid Malonespecified by
138457432e8SRuslan Ermilov.Fa ifp .
139457432e8SRuslan ErmilovThe
14083676de9SDavid Malone.Fn bpfdetach
141457432e8SRuslan Ermilovfunction
14283676de9SDavid Maloneshould be called once for each
143457432e8SRuslan Ermilov.Nm
14483676de9SDavid Maloneinstance attached.
14583676de9SDavid Malone.Pp
146457432e8SRuslan ErmilovThe
14783676de9SDavid Malone.Fn bpf_tap
148457432e8SRuslan Ermilovfunction
14983676de9SDavid Maloneis used by an interface to pass the packet to
15083676de9SDavid Malone.Nm .
15183676de9SDavid MaloneThe packet data (including link-header),
15283676de9SDavid Malonepointed to by
153457432e8SRuslan Ermilov.Fa pkt ,
15483676de9SDavid Maloneis of length
155457432e8SRuslan Ermilov.Fa pktlen ,
15683676de9SDavid Malonewhich must be a contiguous buffer.
157457432e8SRuslan ErmilovThe
158457432e8SRuslan Ermilov.Fa ifp
159457432e8SRuslan Ermilovargument
16083676de9SDavid Maloneis a pointer to the structure that defines the interface to be tapped.
16183676de9SDavid MaloneThe packet is parsed by each processes filter,
16283676de9SDavid Maloneand if accepted,
16383676de9SDavid Maloneit is buffered for the process to read.
16483676de9SDavid Malone.Pp
165457432e8SRuslan ErmilovThe
16683676de9SDavid Malone.Fn bpf_mtap
167457432e8SRuslan Ermilovfunction is like
16883676de9SDavid Malone.Fn bpf_tap
169457432e8SRuslan Ermilovexcept that it is used to tap packets that are in an
170457432e8SRuslan Ermilov.Vt mbuf
171457432e8SRuslan Ermilovchain,
172457432e8SRuslan Ermilov.Fa m .
173457432e8SRuslan ErmilovThe
174457432e8SRuslan Ermilov.Fa ifp
175457432e8SRuslan Ermilovargument
17683676de9SDavid Maloneis a pointer to the structure that defines the interface to be tapped.
17783676de9SDavid MaloneLike
17883676de9SDavid Malone.Fn bpf_tap ,
17983676de9SDavid Malone.Fn bpf_mtap
18083676de9SDavid Malonerequires a link-header for whatever data link layer type is specified.
18183676de9SDavid MaloneNote that
18283676de9SDavid Malone.Nm
183457432e8SRuslan Ermilovonly reads from the
184457432e8SRuslan Ermilov.Vt mbuf
185457432e8SRuslan Ermilovchain,
18683676de9SDavid Maloneit does not free it or keep a pointer to it.
187457432e8SRuslan ErmilovThis means that an
188457432e8SRuslan Ermilov.Vt mbuf
189457432e8SRuslan Ermilovcontaining the link-header
19083676de9SDavid Malonecan be prepended to the chain if necessary.
19183676de9SDavid MaloneA cleaner interface to achieve this is provided by
19283676de9SDavid Malone.Fn bpf_mtap2 .
19383676de9SDavid Malone.Pp
194457432e8SRuslan ErmilovThe
19583676de9SDavid Malone.Fn bpf_mtap2
196457432e8SRuslan Ermilovfunction
19783676de9SDavid Maloneallows the user to pass a link-header
198457432e8SRuslan Ermilov.Fa data ,
19983676de9SDavid Maloneof length
200457432e8SRuslan Ermilov.Fa dlen ,
201457432e8SRuslan Ermilovindependent of the
202457432e8SRuslan Ermilov.Vt mbuf
203457432e8SRuslan Ermilov.Fa m ,
20483676de9SDavid Malonecontaining the packet.
20583676de9SDavid MaloneThis simplifies the passing of some link-headers.
20683676de9SDavid Malone.Pp
207457432e8SRuslan ErmilovThe
20883676de9SDavid Malone.Fn bpf_filter
209457432e8SRuslan Ermilovfunction
21083676de9SDavid Maloneexecutes the filter program starting at
211457432e8SRuslan Ermilov.Fa pc
21283676de9SDavid Maloneon the packet
213457432e8SRuslan Ermilov.Fa pkt .
214457432e8SRuslan ErmilovThe
215457432e8SRuslan Ermilov.Fa wirelen
216457432e8SRuslan Ermilovargument
21783676de9SDavid Maloneis the length of the original packet and
218457432e8SRuslan Ermilov.Fa buflen
21983676de9SDavid Maloneis the amount of data present.
22083676de9SDavid Malone.Pp
221457432e8SRuslan ErmilovThe
22283676de9SDavid Malone.Fn bpf_validate
223457432e8SRuslan Ermilovfunction
22483676de9SDavid Malonechecks that the filter code
225457432e8SRuslan Ermilov.Fa fcode ,
22683676de9SDavid Maloneof length
227457432e8SRuslan Ermilov.Fa flen ,
22883676de9SDavid Maloneis valid.
22983676de9SDavid Malone.\"
23083676de9SDavid Malone.Sh RETURN VALUES
231457432e8SRuslan ErmilovThe
23283676de9SDavid Malone.Fn bpf_filter
233457432e8SRuslan Ermilovfunction returns \-1
234457432e8SRuslan Ermilov(cast to an unsigned integer)
23583676de9SDavid Maloneif there is no filter.
23683676de9SDavid MaloneOtherwise, it returns the result of the filter program.
23783676de9SDavid Malone.Pp
238457432e8SRuslan ErmilovThe
23983676de9SDavid Malone.Fn bpf_validate
240457432e8SRuslan Ermilovfunction
24183676de9SDavid Malonereturns 0 when the program is not a valid filter program.
24283676de9SDavid Malone.\"
24383676de9SDavid Malone.Sh SEE ALSO
24483676de9SDavid Malone.Xr tcpdump 1 ,
245c0854fb7SRuslan Ermilov.Xr bpf 4
24683676de9SDavid Malone.\"
24783676de9SDavid Malone.Sh HISTORY
24883676de9SDavid MaloneThe Enet packet filter was created in 1980 by Mike Accetta and
24983676de9SDavid MaloneRick Rashid at Carnegie-Mellon University.
25083676de9SDavid MaloneJeffrey Mogul,
25183676de9SDavid Maloneat Stanford,
25283676de9SDavid Maloneported the code to
25383676de9SDavid Malone.Bx
25483676de9SDavid Maloneand continued its development from 1983 on.
25583676de9SDavid MaloneSince then,
25683676de9SDavid Maloneit has evolved into the Ultrix Packet Filter at
25783676de9SDavid Malone.Tn DEC ,
25883676de9SDavid Malonea
25983676de9SDavid Malone.Tn STREAMS
26083676de9SDavid Malone.Tn NIT
26183676de9SDavid Malonemodule under
262457432e8SRuslan Ermilov.Tn SunOS
263457432e8SRuslan Ermilov4.1, and
26483676de9SDavid Malone.Tn BPF .
26583676de9SDavid Malone.\"
26683676de9SDavid Malone.Sh AUTHORS
26783676de9SDavid Malone.An -nosplit
26883676de9SDavid Malone.An Steven McCanne ,
26983676de9SDavid Maloneof Lawrence Berkeley Laboratory, implemented BPF in Summer 1990.
27083676de9SDavid MaloneMuch of the design is due to
27183676de9SDavid Malone.An Van Jacobson .
272457432e8SRuslan ErmilovThis manpage was written by
27383676de9SDavid Malone.An Orla McGann .
274