Changeset View
Changeset View
Standalone View
Standalone View
share/man/man9/pfil.9
.\" $NetBSD: pfil.9,v 1.22 2003/07/01 13:04:06 wiz Exp $ | .\" $NetBSD: pfil.9,v 1.22 2003/07/01 13:04:06 wiz Exp $ | ||||
.\" | .\" | ||||
.\" Copyright (c) 2019 Gleb Smirnoff <glebius@FreeBSD.org> | |||||
.\" Copyright (c) 1996 Matthew R. Green | .\" Copyright (c) 1996 Matthew R. Green | ||||
.\" All rights reserved. | .\" All rights reserved. | ||||
rgrimes: I think this is the place your talking about keeping it. Yes, you need to keep this as it… | |||||
.\" | .\" | ||||
.\" Redistribution and use in source and binary forms, with or without | .\" Redistribution and use in source and binary forms, with or without | ||||
.\" modification, are permitted provided that the following conditions | .\" modification, are permitted provided that the following conditions | ||||
.\" are met: | .\" are met: | ||||
.\" 1. Redistributions of source code must retain the above copyright | .\" 1. Redistributions of source code must retain the above copyright | ||||
.\" notice, this list of conditions and the following disclaimer. | .\" notice, this list of conditions and the following disclaimer. | ||||
.\" 2. Redistributions in binary form must reproduce the above copyright | .\" 2. Redistributions in binary form must reproduce the above copyright | ||||
.\" notice, this list of conditions and the following disclaimer in the | .\" notice, this list of conditions and the following disclaimer in the | ||||
Show All 10 Lines | |||||
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||||
.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | .\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||||
.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||||
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||
.\" SUCH DAMAGE. | .\" SUCH DAMAGE. | ||||
.\" | .\" | ||||
.\" $FreeBSD$ | .\" $FreeBSD$ | ||||
.\" | .\" | ||||
.Dd March 10, 2018 | .Dd January 28, 2019 | ||||
.Dt PFIL 9 | .Dt PFIL 9 | ||||
.Os | .Os | ||||
.Sh NAME | .Sh NAME | ||||
.Nm pfil , | .Nm pfil , | ||||
.Nm pfil_head_register , | .Nm pfil_head_register , | ||||
.Nm pfil_head_unregister , | .Nm pfil_head_unregister , | ||||
.Nm pfil_head_get , | .Nm pfil_link , | ||||
.Nm pfil_add_hook , | .Nm pfil_run_hooks | ||||
.Nm pfil_add_hook_flags , | |||||
.Nm pfil_remove_hook , | |||||
.Nm pfil_remove_hook_flags , | |||||
.Nm pfil_run_hooks , | |||||
.Nm pfil_rlock , | |||||
.Nm pfil_runlock , | |||||
.Nm pfil_wlock , | |||||
.Nm pfil_wunlock | |||||
.Nd packet filter interface | .Nd packet filter interface | ||||
.Sh SYNOPSIS | .Sh SYNOPSIS | ||||
.In sys/param.h | .In sys/param.h | ||||
.In sys/mbuf.h | .In sys/mbuf.h | ||||
.In net/if.h | |||||
.In net/pfil.h | .In net/pfil.h | ||||
.Bd -literal | .Ft pfil_head_t | ||||
typedef int (*pfil_func_t)(void *arg, struct mbuf **mp, struct ifnet *, int dir, struct inpcb); | .Fn pfil_head_register "struct pfil_head_args *args" | ||||
.Bd -literal | |||||
typedef int (*pfil_func_flags_t)(void *arg, struct mbuf **mp, struct ifnet *, int dir, int flags, struct inpcb); | |||||
.Ft int | |||||
.Fn pfil_head_register "struct pfil_head *head" | |||||
.Ft int | |||||
.Fn pfil_head_unregister "struct pfil_head *head" | |||||
.Ft "struct pfil_head *" | |||||
.Fn pfil_head_get "int af" "u_long dlt" | |||||
.Ft int | |||||
.Fn pfil_add_hook "pfil_func_t" "void *arg" "struct pfil_head *" | |||||
.Ft int | |||||
.Fn pfil_add_hook_flags "pfil_func_flags_t" "void *arg" "int flags" "struct pfil_head *" | |||||
.Ft int | |||||
.Fn pfil_remove_hook "pfil_func_t" "void *arg" "struct pfil_head *" | |||||
.Ft int | |||||
.Fn pfil_remove_hook_flags "pfil_func_flags_t" "void *arg" "int flags" "struct pfil_head *" | |||||
.Ft int | |||||
.Fn pfil_run_hooks "struct pfil_head *head" "struct mbuf **mp" "struct ifnet *" "int dir" "int flags" "struct inpcb *" | |||||
.Ft void | .Ft void | ||||
.Fn pfil_rlock "struct pfil_head *" "struct rm_priotracker *" | .Fn pfil_head_unregister "struct pfil_head_t *head" | ||||
.Ft pfil_hook_t | |||||
.Fn pfil_add_hook "struct pfil_hook_args *" | |||||
.Ft void | .Ft void | ||||
.Fn pfil_runlock "struct pfil_head *" "struct rm_priotracker *" | .Fn pfil_remove_hook "pfil_hook_t" | ||||
.Ft void | .Ft int | ||||
.Fn pfil_wlock "struct pfil_head *" | .Fn pfil_link "struct pfil_link_args *args" | ||||
.Ft void | .Ft int | ||||
.Fn pfil_wunlock "struct pfil_head *" | .Fn pfil_run_hooks "phil_head_t *" "pfil_packet_t" "struct ifnet *" "int" "struct inpcb *" | ||||
.Ed | |||||
.Sh DESCRIPTION | .Sh DESCRIPTION | ||||
The | The | ||||
.Nm | .Nm | ||||
framework allows for a specified function to be invoked for every | framework allows for a specified function or a list of functions | ||||
incoming or outgoing packet for a particular network I/O stream. | 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 | These hooks may be used to implement a firewall or perform packet | ||||
transformations. | transformations. | ||||
.Pp | .Pp | ||||
Packet filtering points are registered with | Packet filtering points, for historical reasons named | ||||
.Em heads , | |||||
are registered with | |||||
.Fn pfil_head_register . | .Fn pfil_head_register . | ||||
Filtering points are identified by a key | The function is supplied with special versioned | ||||
.Pq Vt "void *" | .Vt struct pfil_head_args | ||||
and a data link type | structure that specifies type and features of the head as well as | ||||
.Pq Vt int | human readable name. | ||||
in the | If the filtering point to be ever destroyed, the subsystem that | ||||
.Vt pfil_head | created it must unregister it with call to | ||||
structure. | .Fn pfil_head_unregister . | ||||
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. | |||||
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. | |||||
Filtering points may be unregistered with the | |||||
.Fn pfil_head_unregister | |||||
function. | |||||
.Pp | .Pp | ||||
Packet filters register/unregister themselves with a filtering point | Packet filtering systems may register arbitrary number of filters, | ||||
with the | for historical reasons named | ||||
.Em hooks . | |||||
To register a new hook | |||||
.Fn pfil_add_hook | .Fn pfil_add_hook | ||||
and | with special versioned | ||||
.Vt struct pfil_hook_args | |||||
structure is called. | |||||
The structure specifies type and features of the hook, pointer to | |||||
the actual filtering function and user readable name of the filtering | |||||
module and ruleset name. | |||||
Later hooks can be removed with | |||||
.Fn pfil_remove_hook | .Fn pfil_remove_hook | ||||
functions, respectively. | functions. | ||||
.I | |||||
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. | |||||
.Pp | .Pp | ||||
When a filter is invoked, the packet appears just as if it | To connect existing | ||||
.Dq came off the wire . | .Em hook | ||||
That is, all protocol fields are in network byte order. | to an existing | ||||
The filter is called with its specified argument, the pointer to the | .Em head | ||||
pointer to the | function | ||||
.Vt mbuf | .Fn pfil_link | ||||
containing the packet, the pointer to the network | shall be used. | ||||
interface that the packet is traversing, and the direction | The function is supplied with versioned | ||||
.Dv ( PFIL_IN | .Vt struct pfil_link_args | ||||
or | structure that specifies either literal names of hook and head or | ||||
.Dv PFIL_OUT ) | pointers to them. | ||||
that the packet is traveling. | Typically | ||||
The | .Fn pfil_link | ||||
.Vt flags | is called by filtering modules to autoregister their default ruleset | ||||
argument will indicate if an outgoing packet is simply being forwarded with the | and default filtering points. | ||||
value PFIL_FWD. | It also serves on the kernel side of | ||||
The filter may change which mbuf the | .Xr ioctl 2 | ||||
.Vt "mbuf\ **" | when user changes | ||||
argument references. | |||||
The filter returns an error (errno) if the packet processing is to stop, or 0 | |||||
if the processing is to continue. | |||||
If the packet processing is to stop, it is the responsibility of the | |||||
filter to free the packet. | |||||
.Pp | |||||
Every filter hook is called with | |||||
.Nm | .Nm | ||||
read lock held. | configuration with help of | ||||
All heads uses the same lock within the same VNET instance. | .Xr pfilctl 8 | ||||
Packet filter can use this lock instead of own locking model to | utility. | ||||
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. | |||||
.Sh FILTERING POINTS | |||||
Currently, filtering points are implemented for the following link types: | |||||
.Pp | .Pp | ||||
.Bl -tag -width "AF_INET6" -offset XXX -compact | For every packet traveling through a | ||||
.It AF_INET | .Em head | ||||
the latter shall invoke | |||||
.Fn pfil_run_hooks . | |||||
The function can accept either | |||||
.Vt struct mbuf * | |||||
pointer or a | |||||
.Vt void * | |||||
pointer and length. | |||||
In case if a hooked filtering module cannot understand | |||||
.Vt void * | |||||
pointer | |||||
.Nm | |||||
will provide it with a fake one. | |||||
All calls to | |||||
.Fn pfil_run_hooks | |||||
are performed in network | |||||
.Xr epoch 9 . | |||||
.Sh HEADS (filtering points) | |||||
By default kernel creates the following heads: | |||||
.Bl -tag -width "ethernet" | |||||
.It inet | |||||
IPv4 packets. | IPv4 packets. | ||||
.It AF_INET6 | .It inet6 | ||||
IPv6 packets. | IPv6 packets. | ||||
.It AF_LINK | .It ethernet | ||||
Link-layer packets. | Link-layer packets. | ||||
.El | .El | ||||
.Sh RETURN VALUES | |||||
If successful, | |||||
.Fn pfil_head_get | |||||
returns the | |||||
.Vt pfil_head | |||||
structure for the given key/dlt. | |||||
The | |||||
.Fn pfil_add_hook | |||||
and | |||||
.Fn pfil_remove_hook | |||||
functions | |||||
return 0 if successful. | |||||
If called with flag | |||||
.Dv PFIL_WAITOK , | |||||
.Fn pfil_remove_hook | |||||
is expected to always succeed. | |||||
.Pp | .Pp | ||||
The | Default rulesets are automatically linked to these heads to preserve | ||||
.Fn pfil_head_unregister | historical behavavior. | ||||
function | |||||
might sleep! | |||||
.Sh SEE ALSO | .Sh SEE ALSO | ||||
.Xr bpf 4 , | .Xr ipfilter 4 , | ||||
.Xr if_bridge 4 , | .Xr ipfw 4 , | ||||
.Xr rmlock 9 | .Xr pf 4 , | ||||
.Xr pfilctl 8 | |||||
.Sh HISTORY | .Sh HISTORY | ||||
The | The | ||||
.Nm | .Nm | ||||
interface first appeared in | interface first appeared in | ||||
.Nx 1.3 . | .Nx 1.3 . | ||||
The | The | ||||
.Nm | .Nm | ||||
input and output lists were originally implemented as | interface was imported into | ||||
.In sys/queue.h | |||||
.Dv LIST | |||||
structures; | |||||
however this was changed in | |||||
.Nx 1.4 | |||||
to | |||||
.Dv TAILQ | |||||
structures. | |||||
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. | |||||
.Pp | |||||
The | |||||
.Nm | |||||
interface was changed in 1.4T to accept a 3rd parameter to both | |||||
.Fn pfil_add_hook | |||||
and | |||||
.Fn pfil_remove_hook , | |||||
introducing the capability of per-protocol filtering. | |||||
This was done primarily in order to support filtering of IPv6. | |||||
.Pp | |||||
In 1.5K, the | |||||
.Nm | |||||
framework was changed to work with an arbitrary number of filtering points, | |||||
as well as be less IP-centric. | |||||
.Pp | |||||
Fine-grained locking was added in | |||||
.Fx 5.2 . | .Fx 5.2 . | ||||
.Nm | In | ||||
lock export was added in | .Fx 13.0 | ||||
.Fx 10.0 . | the interface was significantly rewritten. | ||||
.Sh BUGS | |||||
When a | |||||
.Vt pfil_head | |||||
is being modified, no traffic is diverted | |||||
(to avoid deadlock). | |||||
This means that traffic may be dropped unconditionally for a short period | |||||
of time. | |||||
.Fn pfil_run_hooks | |||||
will return | |||||
.Er ENOBUFS | |||||
to indicate this. |
I think this is the place your talking about keeping it. Yes, you need to keep this as it looks like Matthew R Green put it there, and it would have to be him to remove it. You could fold it up onto the same line as his copyright, making it clear from here forward that it was him that asserted this, but there is not a need to.
By the way, your adding your copyright a good way, above the other so it doesnt seperate his copyright from his all rights reserved, thank you for this detail!