xref: /freebsd/share/man/man9/pfil.9 (revision 54202ab3d1305a8ea292b7aa90d988ee6f5e5776)
189a68827SSam Leffler.\"	$NetBSD: pfil.9,v 1.22 2003/07/01 13:04:06 wiz Exp $
289a68827SSam Leffler.\"
3e9c7bebfSDarren Reed.\" Copyright (c) 1996 Matthew R. Green
4e9c7bebfSDarren Reed.\" All rights reserved.
5e9c7bebfSDarren Reed.\"
6e9c7bebfSDarren Reed.\" Redistribution and use in source and binary forms, with or without
7e9c7bebfSDarren Reed.\" modification, are permitted provided that the following conditions
8e9c7bebfSDarren Reed.\" are met:
9e9c7bebfSDarren Reed.\" 1. Redistributions of source code must retain the above copyright
10e9c7bebfSDarren Reed.\"    notice, this list of conditions and the following disclaimer.
11e9c7bebfSDarren Reed.\" 2. Redistributions in binary form must reproduce the above copyright
12e9c7bebfSDarren Reed.\"    notice, this list of conditions and the following disclaimer in the
13e9c7bebfSDarren Reed.\"    documentation and/or other materials provided with the distribution.
14e9c7bebfSDarren Reed.\" 3. The name of the author may not be used to endorse or promote products
15e9c7bebfSDarren Reed.\"    derived from this software without specific prior written permission.
16e9c7bebfSDarren Reed.\"
17e9c7bebfSDarren Reed.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18e9c7bebfSDarren Reed.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19e9c7bebfSDarren Reed.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20e9c7bebfSDarren Reed.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21e9c7bebfSDarren Reed.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22e9c7bebfSDarren Reed.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23e9c7bebfSDarren Reed.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24e9c7bebfSDarren Reed.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25e9c7bebfSDarren Reed.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26e9c7bebfSDarren Reed.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27e9c7bebfSDarren Reed.\" SUCH DAMAGE.
28e9c7bebfSDarren Reed.\"
2908aa4ea3SDarren Reed.\" $FreeBSD$
30bf7f20c2SRuslan Ermilov.\"
31*54202ab3SAlexander V. Chernikov.Dd September 16, 2012
32e9c7bebfSDarren Reed.Dt PFIL 9
33e9c7bebfSDarren Reed.Os
34e9c7bebfSDarren Reed.Sh NAME
35e9c7bebfSDarren Reed.Nm pfil ,
3689a68827SSam Leffler.Nm pfil_head_register ,
3789a68827SSam Leffler.Nm pfil_head_unregister ,
3889a68827SSam Leffler.Nm pfil_head_get ,
39e9c7bebfSDarren Reed.Nm pfil_hook_get ,
40e9c7bebfSDarren Reed.Nm pfil_add_hook ,
4189a68827SSam Leffler.Nm pfil_remove_hook ,
4289a68827SSam Leffler.Nm pfil_run_hooks
43e9c7bebfSDarren Reed.Nd packet filter interface
44e9c7bebfSDarren Reed.Sh SYNOPSIS
4532eef9aeSRuslan Ermilov.In sys/param.h
4632eef9aeSRuslan Ermilov.In sys/mbuf.h
4732eef9aeSRuslan Ermilov.In net/if.h
4832eef9aeSRuslan Ermilov.In net/pfil.h
49f16b3c0dSChad David.Ft int
5089a68827SSam Leffler.Fn pfil_head_register "struct pfil_head *head"
51f16b3c0dSChad David.Ft int
5289a68827SSam Leffler.Fn pfil_head_unregister "struct pfil_head *head"
53bf7f20c2SRuslan Ermilov.Ft "struct pfil_head *"
5489a68827SSam Leffler.Fn pfil_head_get "int af" "u_long dlt"
55bf7f20c2SRuslan Ermilov.Ft "struct packet_filter_hook *"
5689a68827SSam Leffler.Fn pfil_hook_get "int dir" "struct pfil_head *head"
5789a68827SSam Leffler.Ft void
5889a68827SSam Leffler.Fn pfil_add_hook "int (*func)()" "void *arg" "int flags" "struct pfil_head *"
5989a68827SSam Leffler.Ft void
6089a68827SSam Leffler.Fn pfil_remove_hook "int (*func)()" "void *arg" "int flags" "struct pfil_head *"
6189a68827SSam Leffler.Ft int
62d6a8d588SMax Laier.Fn (*func) "void *arg" "struct mbuf **mp" "struct ifnet *" "int dir" "struct inpcb *"
6389a68827SSam Leffler.Ft int
64d6a8d588SMax Laier.Fn pfil_run_hooks "struct pfil_head *head" "struct mbuf **mp" "struct ifnet *" "int dir" "struct inpcb *"
65e9c7bebfSDarren Reed.Sh DESCRIPTION
66e9c7bebfSDarren ReedThe
67e9c7bebfSDarren Reed.Nm
6889a68827SSam Lefflerframework allows for a specified function to be invoked for every
6989a68827SSam Lefflerincoming or outgoing packet for a particular network I/O stream.
7089a68827SSam LefflerThese hooks may be used to implement a firewall or perform packet
7189a68827SSam Lefflertransformations.
72e9c7bebfSDarren Reed.Pp
7389a68827SSam LefflerPacket filtering points are registered with
7489a68827SSam Leffler.Fn pfil_head_register .
75bf7f20c2SRuslan ErmilovFiltering points are identified by a key
76bf7f20c2SRuslan Ermilov.Pq Vt "void *"
77bf7f20c2SRuslan Ermilovand a data link type
78bf7f20c2SRuslan Ermilov.Pq Vt int
79bf7f20c2SRuslan Ermilovin the
80bf7f20c2SRuslan Ermilov.Vt pfil_head
81e4c7f9b6SRuslan Ermilovstructure.
8289a68827SSam LefflerPacket filters use the key and data link type to look up the filtering
8389a68827SSam Lefflerpoint with which they register themselves.
8489a68827SSam LefflerThe key is unique to the filtering point.
8589a68827SSam LefflerThe data link type is a
8689a68827SSam Leffler.Xr bpf 4
8789a68827SSam LefflerDLT constant indicating what kind of header is present on the packet
8889a68827SSam Lefflerat the filtering point.
8989a68827SSam LefflerFiltering points may be unregistered with the
9089a68827SSam Leffler.Fn pfil_head_unregister
9189a68827SSam Lefflerfunction.
9289a68827SSam Leffler.Pp
9389a68827SSam LefflerPacket filters register/unregister themselves with a filtering point
9489a68827SSam Lefflerwith the
9589a68827SSam Leffler.Fn pfil_add_hook
96e9c7bebfSDarren Reedand
9789a68827SSam Leffler.Fn pfil_remove_hook
9889a68827SSam Lefflerfunctions, respectively.
9989a68827SSam LefflerThe head is looked up using the
10089a68827SSam Leffler.Fn pfil_head_get
10189a68827SSam Lefflerfunction, which takes the key and data link type that the packet filter
10289a68827SSam Lefflerexpects.
10389a68827SSam LefflerFilters may provide an argument to be passed to the filter when
10489a68827SSam Lefflerinvoked on a packet.
10589a68827SSam Leffler.Pp
10689a68827SSam LefflerWhen a filter is invoked, the packet appears just as if it
10789a68827SSam Leffler.Dq came off the wire .
10889a68827SSam LefflerThat is, all protocol fields are in network byte order.
10989a68827SSam LefflerThe filter is called with its specified argument, the pointer to the
110bf7f20c2SRuslan Ermilovpointer to the
111bf7f20c2SRuslan Ermilov.Vt mbuf
112bf7f20c2SRuslan Ermilovcontaining the packet, the pointer to the network
113bf7f20c2SRuslan Ermilovinterface that the packet is traversing, and the direction
114bf7f20c2SRuslan Ermilov.Dv ( PFIL_IN
115bf7f20c2SRuslan Ermilovor
116bf7f20c2SRuslan Ermilov.Dv PFIL_OUT )
117bf7f20c2SRuslan Ermilovthat the packet is traveling.
118bf7f20c2SRuslan ErmilovThe filter may change which mbuf the
119bf7f20c2SRuslan Ermilov.Vt "mbuf\ **"
120bf7f20c2SRuslan Ermilovargument references.
121bf7f20c2SRuslan ErmilovThe filter returns an error (errno) if the packet processing is to stop, or 0
12289a68827SSam Lefflerif the processing is to continue.
12389a68827SSam LefflerIf the packet processing is to stop, it is the responsibility of the
12489a68827SSam Lefflerfilter to free the packet.
125*54202ab3SAlexander V. Chernikov.Sh FILTERING POINTS
126*54202ab3SAlexander V. ChernikovCurrently, filtering points are implemented for the following link types:
127*54202ab3SAlexander V. Chernikov.Pp
128*54202ab3SAlexander V. Chernikov.Bl -tag -width "AF_INET6" -offset XXX -compact
129*54202ab3SAlexander V. Chernikov.It AF_INET
130*54202ab3SAlexander V. Chernikov.It AF_INET6
131*54202ab3SAlexander V. ChernikovIPv4 and IPv6 packets. Note that packet header is already
132*54202ab3SAlexander V. Chernikov.Cm converted to host format.
133*54202ab3SAlexander V. ChernikovHost format has to be preserved in case of header modifications.
134*54202ab3SAlexander V. Chernikov.It AF_LINK
135*54202ab3SAlexander V. ChernikovLink-layer packets.
136*54202ab3SAlexander V. Chernikov.El
137e9c7bebfSDarren Reed.Sh RETURN VALUES
138e4c7f9b6SRuslan ErmilovIf successful,
13989a68827SSam Leffler.Fn pfil_head_get
140bf7f20c2SRuslan Ermilovreturns the
141bf7f20c2SRuslan Ermilov.Vt pfil_head
142bf7f20c2SRuslan Ermilovstructure for the given key/dlt.
143bf7f20c2SRuslan ErmilovThe
144e9c7bebfSDarren Reed.Fn pfil_add_hook
145e9c7bebfSDarren Reedand
146e9c7bebfSDarren Reed.Fn pfil_remove_hook
147bf7f20c2SRuslan Ermilovfunctions
148bf7f20c2SRuslan Ermilovreturn 0 if successful.
149bf7f20c2SRuslan ErmilovIf called with flag
150bf7f20c2SRuslan Ermilov.Dv PFIL_WAITOK ,
15189a68827SSam Leffler.Fn pfil_remove_hook
15289a68827SSam Leffleris expected to always succeed.
15389a68827SSam Leffler.Pp
154bf7f20c2SRuslan ErmilovThe
15589a68827SSam Leffler.Fn pfil_head_unregister
156bf7f20c2SRuslan Ermilovfunction
15789a68827SSam Lefflermight sleep!
1589cbda590SRuslan Ermilov.Sh SEE ALSO
1599cbda590SRuslan Ermilov.Xr bpf 4 ,
160b6de9e91SMax Laier.Xr if_bridge 4
161e9c7bebfSDarren Reed.Sh HISTORY
162e9c7bebfSDarren ReedThe
163e9c7bebfSDarren Reed.Nm
164e9c7bebfSDarren Reedinterface first appeared in
165e9c7bebfSDarren Reed.Nx 1.3 .
166e9c7bebfSDarren ReedThe
167e9c7bebfSDarren Reed.Nm
168e9c7bebfSDarren Reedinput and output lists were originally implemented as
169bf7f20c2SRuslan Ermilov.In sys/queue.h
170e9c7bebfSDarren Reed.Dv LIST
171e9c7bebfSDarren Reedstructures;
172e9c7bebfSDarren Reedhowever this was changed in
173e9c7bebfSDarren Reed.Nx 1.4
174e9c7bebfSDarren Reedto
175e9c7bebfSDarren Reed.Dv TAILQ
176e4c7f9b6SRuslan Ermilovstructures.
17789a68827SSam LefflerThis change was to allow the input and output filters to be processed in
17889a68827SSam Lefflerreverse order, to allow the same path to be taken, in or out of the kernel.
179e9c7bebfSDarren Reed.Pp
180e9c7bebfSDarren ReedThe
181e9c7bebfSDarren Reed.Nm
182e9c7bebfSDarren Reedinterface was changed in 1.4T to accept a 3rd parameter to both
183e9c7bebfSDarren Reed.Fn pfil_add_hook
184e9c7bebfSDarren Reedand
18508aa4ea3SDarren Reed.Fn pfil_remove_hook ,
186e4c7f9b6SRuslan Ermilovintroducing the capability of per-protocol filtering.
18789a68827SSam LefflerThis was done primarily in order to support filtering of IPv6.
18889a68827SSam Leffler.Pp
18989a68827SSam LefflerIn 1.5K, the
190e9c7bebfSDarren Reed.Nm
19189a68827SSam Lefflerframework was changed to work with an arbitrary number of filtering points,
19289a68827SSam Leffleras well as be less IP-centric.
19389a68827SSam Leffler.Pp
1940e87afb6SSimon L. B. NielsenFine-grained locking was added in
1950e87afb6SSimon L. B. Nielsen.Fx 5.2 .
19689a68827SSam Leffler.Sh BUGS
197bf7f20c2SRuslan ErmilovThe
19889a68827SSam Leffler.Fn pfil_hook_get
199bf7f20c2SRuslan Ermilovfunction
20089a68827SSam Leffleris only safe for internal use.
20189a68827SSam Leffler.Pp
202bf7f20c2SRuslan ErmilovWhen a
203bf7f20c2SRuslan Ermilov.Vt pfil_head
204bf7f20c2SRuslan Ermilovis being modified, no traffic is diverted
20589a68827SSam Leffler(to avoid deadlock).
20685bba445SMax LaierThis means that traffic may be dropped unconditionally for a short period
20789a68827SSam Lefflerof time.
20885bba445SMax Laier.Fn pfil_run_hooks
20985bba445SMax Laierwill return
210c0854fb7SRuslan Ermilov.Er ENOBUFS
21185bba445SMax Laierto indicate this.
212