xref: /freebsd/share/man/man9/bpf.9 (revision 2546665afcaf0d53dc2c7058fee96354b3680f5a)
1.\" Copyright (c) 2004 FreeBSD Inc.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd May 19, 2004
28.Dt BPF 9
29.Os
30.\"
31.Sh NAME
32.Nm bpf
33.Nd "Berkeley Packet Filter"
34.\"
35.Sh SYNOPSIS
36.In net/bpf.h
37.\"
38.Ft void
39.Fn bpfattach "struct ifnet *ifp" "u_int dlt" "u_int hdrlen"
40.Ft void
41.Fn bpfattach2 "struct ifnet *ifp" "u_int dlt" "u_int hdrlen" "struct bpf_if **driverp"
42.Ft void
43.Fn bpfdetach "struct ifnet *ifp"
44.Ft void
45.Fn bpf_tap "struct ifnet *ifp" "u_char *pkt" "u_int *pktlen"
46.Ft void
47.Fn bpf_mtap "struct ifnet *ifp" "struct mbuf *m"
48.Ft void
49.Fn bpf_mtap2 "struct ifnet *bp" "void *data" "u_int dlen" "struct mbuf *m"
50.Ft u_int
51.Fn bpf_filter "const struct bpf_insn *pc " "u_char *pkt" "u_int *wirelen" "u_int *buflen"
52.Ft int
53.Fn bpf_validate "const struct bpf_insn *fcode" "int flen"
54.\"
55.Sh DESCRIPTION
56The Berkeley Packet Filter provides a raw interface,
57that is protocol independent,
58to data link layers.
59It allows all packets on the network,
60even those destined for other hosts,
61to be passed from a network interface to user programs.
62Each program may specify a filter,
63in the form of a bpf filter machine program.
64.Xr bpf 4
65describes the interface used by user programs.
66This man page describes the functions used by interfaces to pass packets to
67.Nm
68and the functions for testing and running
69.Nm
70filter machine programs.
71.Pp
72.Fn bpfattach
73attaches a network interface to
74.Nm .
75.Em ifp
76is a pointer to the structure that defines the interface to be
77attached to an interface.
78.Em dlt
79is the data link-layer type:
80DLT_NULL
81.Po no link-layer encapsulation
82.Pc ,
83DLT_EN10MB
84.Po Ethernet
85.Pc ,
86DLT_IEEE802_11
87.Po 802.11 wireless networks
88.Pc ,
89etc.
90The rest of the link layer types can be found in
91.Pa /usr/src/sys/net/bpf.h .
92.Em hdrlen
93is the fixed size of the link header;
94variable length headers are not yet supported.
95The
96.Nm
97system will hold a pointer to
98.Em ifp->if_bpf .
99This variable will set to a non-NULL value when
100.Nm
101requires packets from this interface to be tapped using the functions below.
102.Pp
103.Fn bpfattach2
104allows multiple bpf instances to be attached to a single interface,
105by registering an explicit
106.Em if_bpf
107rather than using
108.Em ifp->if_bpf .
109It is then possible to run
110.Xr tcpdump 1
111on the interface for any data link-layer types attached.
112.Pp
113.Fn bpfdetach
114detaches a
115.Nm
116instance from an interface,
117specified by
118.Em ifp .
119.Fn bpfdetach
120should be called once for each
121.Nm bpf
122instance attached.
123.Pp
124.Fn bpf_tap
125is used by an interface to pass the packet to
126.Nm .
127The packet data (including link-header),
128pointed to by
129.Em pkt ,
130is of length
131.Em pktlen ,
132which must be a contiguous buffer.
133.Em ifp
134is a pointer to the structure that defines the interface to be tapped.
135The packet is parsed by each processes filter,
136and if accepted,
137it is buffered for the process to read.
138.Pp
139.Fn bpf_mtap
140is
141like
142.Fn bpf_tap
143except that it is used to tap packets that are in an mbuf chain,
144.Em m .
145.Em ifp
146is a pointer to the structure that defines the interface to be tapped.
147Like
148.Fn bpf_tap ,
149.Fn bpf_mtap
150requires a link-header for whatever data link layer type is specified.
151Note that
152.Nm
153only reads from the mbuf chain,
154it does not free it or keep a pointer to it.
155This means that a mbuf containing the link-header
156can be prepended to the chain if necessary.
157A cleaner interface to achieve this is provided by
158.Fn bpf_mtap2 .
159.Pp
160.Fn bpf_mtap2
161allows the user to pass a link-header
162.Em data ,
163of length
164.Em dlen ,
165independent of the mbuf
166.Em m ,
167containing the packet.
168This simplifies the passing of some link-headers.
169.Pp
170.Fn bpf_filter
171executes the filter program starting at
172.Em pc
173on the packet
174.Em pkt .
175.Em wirelen
176is the length of the original packet and
177.Em buflen
178is the amount of data present.
179.Pp
180.Fn bpf_validate
181checks that the filter code
182.Em fcode ,
183of length
184.Em flen ,
185is valid.
186.\"
187.Sh RETURN VALUES
188.Fn bpf_filter
189returns -1
190.Po cast to an unsigned integer
191.Pc
192if there is no filter.
193Otherwise, it returns the result of the filter program.
194.Pp
195.Fn bpf_validate
196returns 0 when the program is not a valid filter program.
197.\"
198.Sh SEE ALSO
199.Xr tcpdump 1 ,
200.Xr bpf 4 .
201.\"
202.Sh HISTORY
203The Enet packet filter was created in 1980 by Mike Accetta and
204Rick Rashid at Carnegie-Mellon University.
205Jeffrey Mogul,
206at Stanford,
207ported the code to
208.Bx
209and continued its development from 1983 on.
210Since then,
211it has evolved into the Ultrix Packet Filter at
212.Tn DEC ,
213a
214.Tn STREAMS
215.Tn NIT
216module under
217.Tn SunOS 4.1 ,
218and
219.Tn BPF .
220.\"
221.Sh AUTHORS
222.An -nosplit
223.An Steven McCanne ,
224of Lawrence Berkeley Laboratory, implemented BPF in Summer 1990.
225Much of the design is due to
226.An Van Jacobson .
227This manpage by was written by
228.An Orla McGann .
229