2003-09-25 20:49:28 +00:00
|
|
|
.\" $NetBSD: pfil.9,v 1.22 2003/07/01 13:04:06 wiz Exp $
|
|
|
|
.\"
|
2000-05-10 13:37:51 +00:00
|
|
|
.\" Copyright (c) 1996 Matthew R. Green
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. The name of the author may not be used to endorse or promote products
|
|
|
|
.\" derived from this software without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
2000-05-11 00:44:35 +00:00
|
|
|
.\" $FreeBSD$
|
2004-06-16 08:33:57 +00:00
|
|
|
.\"
|
2012-10-22 14:10:17 +00:00
|
|
|
.Dd October 22, 2012
|
2000-05-10 13:37:51 +00:00
|
|
|
.Dt PFIL 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm pfil ,
|
2003-09-25 20:49:28 +00:00
|
|
|
.Nm pfil_head_register ,
|
|
|
|
.Nm pfil_head_unregister ,
|
|
|
|
.Nm pfil_head_get ,
|
2000-05-10 13:37:51 +00:00
|
|
|
.Nm pfil_hook_get ,
|
|
|
|
.Nm pfil_add_hook ,
|
2003-09-25 20:49:28 +00:00
|
|
|
.Nm pfil_remove_hook ,
|
2012-10-22 14:10:17 +00:00
|
|
|
.Nm pfil_run_hooks ,
|
|
|
|
.Nm pfil_rlock ,
|
|
|
|
.Nm pfil_runlock ,
|
|
|
|
.Nm pfil_wlock ,
|
|
|
|
.Nm pfil_wunlock
|
2000-05-10 13:37:51 +00:00
|
|
|
.Nd packet filter interface
|
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/param.h
|
|
|
|
.In sys/mbuf.h
|
|
|
|
.In net/if.h
|
|
|
|
.In net/pfil.h
|
2001-12-26 23:14:04 +00:00
|
|
|
.Ft int
|
2003-09-25 20:49:28 +00:00
|
|
|
.Fn pfil_head_register "struct pfil_head *head"
|
|
|
|
.Ft int
|
|
|
|
.Fn pfil_head_unregister "struct pfil_head *head"
|
2004-06-16 08:33:57 +00:00
|
|
|
.Ft "struct pfil_head *"
|
2003-09-25 20:49:28 +00:00
|
|
|
.Fn pfil_head_get "int af" "u_long dlt"
|
2004-06-16 08:33:57 +00:00
|
|
|
.Ft "struct packet_filter_hook *"
|
2003-09-25 20:49:28 +00:00
|
|
|
.Fn pfil_hook_get "int dir" "struct pfil_head *head"
|
|
|
|
.Ft void
|
|
|
|
.Fn pfil_add_hook "int (*func)()" "void *arg" "int flags" "struct pfil_head *"
|
|
|
|
.Ft void
|
|
|
|
.Fn pfil_remove_hook "int (*func)()" "void *arg" "int flags" "struct pfil_head *"
|
|
|
|
.Ft int
|
2004-09-29 04:54:33 +00:00
|
|
|
.Fn (*func) "void *arg" "struct mbuf **mp" "struct ifnet *" "int dir" "struct inpcb *"
|
2001-12-26 23:14:04 +00:00
|
|
|
.Ft int
|
2004-09-29 04:54:33 +00:00
|
|
|
.Fn pfil_run_hooks "struct pfil_head *head" "struct mbuf **mp" "struct ifnet *" "int dir" "struct inpcb *"
|
2012-10-22 14:10:17 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn pfil_rlock "struct pfil_head *" "struct rm_priotracker *"
|
|
|
|
.Ft void
|
|
|
|
.Fn pfil_runlock "struct pfil_head *" "struct rm_priotracker *"
|
|
|
|
.Ft void
|
|
|
|
.Fn pfil_wlock "struct pfil_head *"
|
|
|
|
.Ft void
|
|
|
|
.Fn pfil_wunlock "struct pfil_head *"
|
2000-05-10 13:37:51 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Nm
|
2003-09-25 20:49:28 +00:00
|
|
|
framework allows for a specified function to be invoked for every
|
|
|
|
incoming or outgoing packet for a particular network I/O stream.
|
|
|
|
These hooks may be used to implement a firewall or perform packet
|
|
|
|
transformations.
|
|
|
|
.Pp
|
|
|
|
Packet filtering points are registered with
|
|
|
|
.Fn pfil_head_register .
|
2004-06-16 08:33:57 +00:00
|
|
|
Filtering points are identified by a key
|
|
|
|
.Pq Vt "void *"
|
|
|
|
and a data link type
|
|
|
|
.Pq Vt int
|
|
|
|
in the
|
|
|
|
.Vt pfil_head
|
2003-09-25 20:49:28 +00:00
|
|
|
structure.
|
|
|
|
Packet filters use the key and data link type to look up the filtering
|
|
|
|
point with which they register themselves.
|
|
|
|
The key is unique to the filtering point.
|
|
|
|
The data link type is a
|
|
|
|
.Xr bpf 4
|
|
|
|
DLT constant indicating what kind of header is present on the packet
|
|
|
|
at the filtering point.
|
2012-10-22 14:10:17 +00:00
|
|
|
Each filtering point uses common per-VNET rmlock by default.
|
|
|
|
This can be changed by specifying
|
|
|
|
.Vt PFIL_FLAG_PRIVATE_LOCK
|
|
|
|
as
|
|
|
|
.Vt "flags"
|
|
|
|
field in the
|
|
|
|
.Vt pfil_head
|
|
|
|
structure.
|
|
|
|
Note that specifying private lock can break filters sharing the same
|
|
|
|
ruleset and/or state between different data link types.
|
2003-09-25 20:49:28 +00:00
|
|
|
Filtering points may be unregistered with the
|
|
|
|
.Fn pfil_head_unregister
|
|
|
|
function.
|
|
|
|
.Pp
|
|
|
|
Packet filters register/unregister themselves with a filtering point
|
|
|
|
with the
|
2000-05-10 13:37:51 +00:00
|
|
|
.Fn pfil_add_hook
|
2002-01-10 13:09:00 +00:00
|
|
|
and
|
2000-05-10 13:37:51 +00:00
|
|
|
.Fn pfil_remove_hook
|
2003-09-25 20:49:28 +00:00
|
|
|
functions, respectively.
|
|
|
|
The head is looked up using the
|
|
|
|
.Fn pfil_head_get
|
|
|
|
function, which takes the key and data link type that the packet filter
|
|
|
|
expects.
|
|
|
|
Filters may provide an argument to be passed to the filter when
|
|
|
|
invoked on a packet.
|
2000-05-10 13:37:51 +00:00
|
|
|
.Pp
|
2003-09-25 20:49:28 +00:00
|
|
|
When a filter is invoked, the packet appears just as if it
|
|
|
|
.Dq came off the wire .
|
|
|
|
That is, all protocol fields are in network byte order.
|
|
|
|
The filter is called with its specified argument, the pointer to the
|
2004-06-16 08:33:57 +00:00
|
|
|
pointer to the
|
|
|
|
.Vt mbuf
|
|
|
|
containing the packet, the pointer to the network
|
|
|
|
interface that the packet is traversing, and the direction
|
|
|
|
.Dv ( PFIL_IN
|
|
|
|
or
|
|
|
|
.Dv PFIL_OUT )
|
|
|
|
that the packet is traveling.
|
|
|
|
The filter may change which mbuf the
|
|
|
|
.Vt "mbuf\ **"
|
|
|
|
argument references.
|
|
|
|
The filter returns an error (errno) if the packet processing is to stop, or 0
|
2003-09-25 20:49:28 +00:00
|
|
|
if the processing is to continue.
|
|
|
|
If the packet processing is to stop, it is the responsibility of the
|
|
|
|
filter to free the packet.
|
2012-10-22 14:10:17 +00:00
|
|
|
.Pp
|
|
|
|
Every filter hook is called with
|
|
|
|
.Nm
|
|
|
|
read lock held.
|
|
|
|
All heads uses the same lock within the same VNET instance.
|
|
|
|
Packet filter can use this lock instead of own locking model to
|
|
|
|
improve performance.
|
|
|
|
Since
|
|
|
|
.Nm
|
|
|
|
uses
|
|
|
|
.Xr rmlock 9
|
|
|
|
.Fn pfil_rlock
|
|
|
|
and
|
|
|
|
.Fn pfil_runlock
|
|
|
|
require
|
|
|
|
.Va struct rm_priotracker
|
|
|
|
to be passed as argument.
|
|
|
|
Filter can acquire and release writer lock via
|
|
|
|
.Fn pfil_wlock
|
|
|
|
and
|
|
|
|
.Fn pfil_wunlock
|
|
|
|
functions.
|
|
|
|
See
|
|
|
|
.Xr rmlock 9
|
|
|
|
for more details.
|
2012-09-16 13:13:02 +00:00
|
|
|
.Sh FILTERING POINTS
|
|
|
|
Currently, filtering points are implemented for the following link types:
|
|
|
|
.Pp
|
|
|
|
.Bl -tag -width "AF_INET6" -offset XXX -compact
|
|
|
|
.It AF_INET
|
2012-10-06 10:02:11 +00:00
|
|
|
IPv4 packets.
|
2012-09-16 13:13:02 +00:00
|
|
|
.It AF_INET6
|
2012-10-06 10:02:11 +00:00
|
|
|
IPv6 packets.
|
2012-09-16 13:13:02 +00:00
|
|
|
.It AF_LINK
|
2012-09-16 21:17:28 +00:00
|
|
|
Link-layer packets.
|
2012-09-16 13:13:02 +00:00
|
|
|
.El
|
2000-05-10 13:37:51 +00:00
|
|
|
.Sh RETURN VALUES
|
2002-01-10 13:09:00 +00:00
|
|
|
If successful,
|
2004-06-16 08:33:57 +00:00
|
|
|
.Fn pfil_head_get
|
|
|
|
returns the
|
|
|
|
.Vt pfil_head
|
|
|
|
structure for the given key/dlt.
|
|
|
|
The
|
|
|
|
.Fn pfil_add_hook
|
2000-05-10 13:37:51 +00:00
|
|
|
and
|
|
|
|
.Fn pfil_remove_hook
|
2004-06-16 08:33:57 +00:00
|
|
|
functions
|
|
|
|
return 0 if successful.
|
|
|
|
If called with flag
|
|
|
|
.Dv PFIL_WAITOK ,
|
2003-09-25 20:49:28 +00:00
|
|
|
.Fn pfil_remove_hook
|
|
|
|
is expected to always succeed.
|
|
|
|
.Pp
|
2004-06-16 08:33:57 +00:00
|
|
|
The
|
2003-09-25 20:49:28 +00:00
|
|
|
.Fn pfil_head_unregister
|
2004-06-16 08:33:57 +00:00
|
|
|
function
|
2003-09-25 20:49:28 +00:00
|
|
|
might sleep!
|
2005-01-21 08:36:40 +00:00
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr bpf 4 ,
|
2012-11-13 19:03:13 +00:00
|
|
|
.Xr if_bridge 4 ,
|
|
|
|
.Xr rmlock 9
|
2000-05-10 13:37:51 +00:00
|
|
|
.Sh HISTORY
|
|
|
|
The
|
|
|
|
.Nm
|
|
|
|
interface first appeared in
|
|
|
|
.Nx 1.3 .
|
|
|
|
The
|
|
|
|
.Nm
|
|
|
|
input and output lists were originally implemented as
|
2004-06-16 08:33:57 +00:00
|
|
|
.In sys/queue.h
|
2000-05-10 13:37:51 +00:00
|
|
|
.Dv LIST
|
|
|
|
structures;
|
|
|
|
however this was changed in
|
|
|
|
.Nx 1.4
|
|
|
|
to
|
|
|
|
.Dv TAILQ
|
2002-01-10 13:09:00 +00:00
|
|
|
structures.
|
2003-09-25 20:49:28 +00:00
|
|
|
This change was to allow the input and output filters to be processed in
|
|
|
|
reverse order, to allow the same path to be taken, in or out of the kernel.
|
2000-05-10 13:37:51 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Nm
|
|
|
|
interface was changed in 1.4T to accept a 3rd parameter to both
|
|
|
|
.Fn pfil_add_hook
|
|
|
|
and
|
2000-05-11 00:44:35 +00:00
|
|
|
.Fn pfil_remove_hook ,
|
2002-01-10 13:09:00 +00:00
|
|
|
introducing the capability of per-protocol filtering.
|
2003-09-25 20:49:28 +00:00
|
|
|
This was done primarily in order to support filtering of IPv6.
|
|
|
|
.Pp
|
|
|
|
In 1.5K, the
|
2000-05-10 13:37:51 +00:00
|
|
|
.Nm
|
2003-09-25 20:49:28 +00:00
|
|
|
framework was changed to work with an arbitrary number of filtering points,
|
|
|
|
as well as be less IP-centric.
|
|
|
|
.Pp
|
2004-03-01 22:58:07 +00:00
|
|
|
Fine-grained locking was added in
|
|
|
|
.Fx 5.2 .
|
2012-10-22 14:10:17 +00:00
|
|
|
.Nm
|
|
|
|
lock export was added in
|
|
|
|
.Fx 10.0 .
|
2003-09-25 20:49:28 +00:00
|
|
|
.Sh BUGS
|
2004-06-16 08:33:57 +00:00
|
|
|
The
|
2003-09-25 20:49:28 +00:00
|
|
|
.Fn pfil_hook_get
|
2004-06-16 08:33:57 +00:00
|
|
|
function
|
2003-09-25 20:49:28 +00:00
|
|
|
is only safe for internal use.
|
|
|
|
.Pp
|
2004-06-16 08:33:57 +00:00
|
|
|
When a
|
|
|
|
.Vt pfil_head
|
|
|
|
is being modified, no traffic is diverted
|
2003-09-25 20:49:28 +00:00
|
|
|
(to avoid deadlock).
|
2004-10-08 12:07:20 +00:00
|
|
|
This means that traffic may be dropped unconditionally for a short period
|
2003-09-25 20:49:28 +00:00
|
|
|
of time.
|
2004-10-08 12:07:20 +00:00
|
|
|
.Fn pfil_run_hooks
|
|
|
|
will return
|
2005-01-12 21:48:25 +00:00
|
|
|
.Er ENOBUFS
|
2004-10-08 12:07:20 +00:00
|
|
|
to indicate this.
|