1445813b7SSascha Wildner.\" $NetBSD: pfil.9,v 1.24 2004/01/01 15:24:35 wiz Exp $ 2445813b7SSascha Wildner.\" 3445813b7SSascha Wildner.\" Copyright (c) 1996 Matthew R. Green 4445813b7SSascha Wildner.\" All rights reserved. 5445813b7SSascha Wildner.\" 6445813b7SSascha Wildner.\" Redistribution and use in source and binary forms, with or without 7445813b7SSascha Wildner.\" modification, are permitted provided that the following conditions 8445813b7SSascha Wildner.\" are met: 9445813b7SSascha Wildner.\" 1. Redistributions of source code must retain the above copyright 10445813b7SSascha Wildner.\" notice, this list of conditions and the following disclaimer. 11445813b7SSascha Wildner.\" 2. Redistributions in binary form must reproduce the above copyright 12445813b7SSascha Wildner.\" notice, this list of conditions and the following disclaimer in the 13445813b7SSascha Wildner.\" documentation and/or other materials provided with the distribution. 14445813b7SSascha Wildner.\" 3. The name of the author may not be used to endorse or promote products 15445813b7SSascha Wildner.\" derived from this software without specific prior written permission. 16445813b7SSascha Wildner.\" 17445813b7SSascha Wildner.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18445813b7SSascha Wildner.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19445813b7SSascha Wildner.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20445813b7SSascha Wildner.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21445813b7SSascha Wildner.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22445813b7SSascha Wildner.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23445813b7SSascha Wildner.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24445813b7SSascha Wildner.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25445813b7SSascha Wildner.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26445813b7SSascha Wildner.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27445813b7SSascha Wildner.\" SUCH DAMAGE. 28445813b7SSascha Wildner.\" 29064ade54SSascha Wildner.Dd January 16, 2015 30445813b7SSascha Wildner.Dt PFIL 9 31445813b7SSascha Wildner.Os 32445813b7SSascha Wildner.Sh NAME 33445813b7SSascha Wildner.Nm pfil , 34445813b7SSascha Wildner.Nm pfil_head_register , 35445813b7SSascha Wildner.Nm pfil_head_unregister , 36445813b7SSascha Wildner.Nm pfil_head_get , 37445813b7SSascha Wildner.Nm pfil_add_hook , 38445813b7SSascha Wildner.Nm pfil_remove_hook , 39445813b7SSascha Wildner.Nm pfil_run_hooks 40445813b7SSascha Wildner.Nd packet filter interface 41445813b7SSascha Wildner.Sh SYNOPSIS 42445813b7SSascha Wildner.In sys/param.h 43445813b7SSascha Wildner.In sys/mbuf.h 44445813b7SSascha Wildner.In net/if.h 45445813b7SSascha Wildner.In net/pfil.h 466b53241aSSepherosa Ziehau.Ft typedef int 476b53241aSSepherosa Ziehau.Fn (*pfil_func_t) "void *arg" "struct mbuf **mp" "struct ifnet *ifp" "int dir" 48445813b7SSascha Wildner.Ft int 49445813b7SSascha Wildner.Fn pfil_head_register "struct pfil_head *ph" 50445813b7SSascha Wildner.Ft int 51445813b7SSascha Wildner.Fn pfil_head_unregister "struct pfil_head *pfh" 52445813b7SSascha Wildner.Ft struct pfil_head * 53445813b7SSascha Wildner.Fn pfil_head_get "int type" "u_long val" 54064ade54SSascha Wildner.Ft int 559ed495deSSepherosa Ziehau.Fn pfil_add_hook "pfil_func_t func" "void *arg" "int flags" "struct pfil_head *ph" 56064ade54SSascha Wildner.Ft int 579ed495deSSepherosa Ziehau.Fn pfil_remove_hook "pfil_func_t func" "void *arg" "int flags" "struct pfil_head *ph" 58445813b7SSascha Wildner.Ft int 59445813b7SSascha Wildner.Fn pfil_run_hooks "struct pfil_head *ph" "struct mbuf **mp" "struct ifnet *ifp" "int dir" 60445813b7SSascha Wildner.Sh DESCRIPTION 61445813b7SSascha WildnerThe 62445813b7SSascha Wildner.Nm 63445813b7SSascha Wildnerframework allows for a specified function to be invoked for every 64445813b7SSascha Wildnerincoming or outgoing packet for a particular network I/O stream. 65445813b7SSascha WildnerThese hooks may be used to implement a firewall or perform packet 66445813b7SSascha Wildnertransformations. 67445813b7SSascha Wildner.Pp 68445813b7SSascha WildnerPacket filtering points are registered with 69445813b7SSascha Wildner.Fn pfil_head_register . 70445813b7SSascha WildnerFiltering points are identified by a key (void *) and a data link type 71445813b7SSascha Wildner(int) in the 72445813b7SSascha Wildner.Em pfil_head 73445813b7SSascha Wildnerstructure. 74445813b7SSascha WildnerPacket filters use the key and data link type to look up the filtering 75445813b7SSascha Wildnerpoint with which they register themselves. 76445813b7SSascha WildnerThe key is unique to the filtering point. 77445813b7SSascha WildnerThe data link type is a 78445813b7SSascha Wildner.Xr bpf 4 79445813b7SSascha WildnerDLT constant indicating what kind of header is present on the packet 80445813b7SSascha Wildnerat the filtering point. 81445813b7SSascha WildnerFiltering points may be unregistered with the 82445813b7SSascha Wildner.Fn pfil_head_unregister 83445813b7SSascha Wildnerfunction. 84445813b7SSascha Wildner.Pp 85445813b7SSascha WildnerPacket filters register/unregister themselves with a filtering point 86445813b7SSascha Wildnerwith the 87445813b7SSascha Wildner.Fn pfil_add_hook 88445813b7SSascha Wildnerand 89445813b7SSascha Wildner.Fn pfil_remove_hook 90445813b7SSascha Wildnerfunctions, respectively. 91445813b7SSascha WildnerThe head is looked up using the 92445813b7SSascha Wildner.Fn pfil_head_get 93445813b7SSascha Wildnerfunction, which takes the key and data link type that the packet filter 94445813b7SSascha Wildnerexpects. 95445813b7SSascha WildnerFilters may provide an argument to be passed to the filter when 96445813b7SSascha Wildnerinvoked on a packet. 97445813b7SSascha Wildner.Pp 98445813b7SSascha WildnerWhen a filter is invoked, the packet appears just as if it 99445813b7SSascha Wildner.Dq came off the wire . 100445813b7SSascha WildnerThat is, all protocol fields are in network byte order. 101445813b7SSascha WildnerThe filter is called with its specified argument, the pointer to the 102445813b7SSascha Wildnerpointer to the mbuf containing the packet, the pointer to the network 103445813b7SSascha Wildnerinterface that the packet is traversing, and the direction 104445813b7SSascha Wildner.Dv ( PFIL_IN 105445813b7SSascha Wildneror 106445813b7SSascha Wildner.Dv PFIL_OUT , 107445813b7SSascha Wildnersee also below) that the packet is traveling. 108445813b7SSascha WildnerThe filter may change which mbuf the mbuf ** argument references. 1090f09e575SSascha WildnerThe filter returns an 1100f09e575SSascha Wildner.Va errno 1110f09e575SSascha Wildnerif the packet processing is to stop, or 0 if the processing is to continue. 112445813b7SSascha WildnerIf the packet processing is to stop, it is the responsibility of the 113445813b7SSascha Wildnerfilter to free the packet. 114445813b7SSascha Wildner.Pp 115445813b7SSascha WildnerThe 116445813b7SSascha Wildner.Em flags 117445813b7SSascha Wildnerparameter, used in the 118445813b7SSascha Wildner.Fn pfil_add_hook 119445813b7SSascha Wildnerand 120445813b7SSascha Wildner.Fn pfil_remove_hook 121445813b7SSascha Wildnerfunctions, indicates when the filter should be called. 122445813b7SSascha WildnerThe flags are: 1233d288be0SSascha Wildner.Bl -tag -offset indent -width ".Dv PFIL_OUT" -compact 124f54c32e1SSascha Wildner.It Dv PFIL_IN 125445813b7SSascha Wildnercall me on incoming packets 126f54c32e1SSascha Wildner.It Dv PFIL_OUT 127445813b7SSascha Wildnercall me on outgoing packets 128f54c32e1SSascha Wildner.It Dv PFIL_ALL 129445813b7SSascha Wildnercall me on all of the above 130445813b7SSascha Wildner.El 131445813b7SSascha Wildner.Sh SEE ALSO 132445813b7SSascha Wildner.Xr bpf 4 133445813b7SSascha Wildner.Sh HISTORY 134445813b7SSascha WildnerThe 135445813b7SSascha Wildner.Nm 136445813b7SSascha Wildnerinterface first appeared in 137445813b7SSascha Wildner.Nx 1.3 . 138445813b7SSascha WildnerThe 139445813b7SSascha Wildner.Nm 140445813b7SSascha Wildnerinput and output lists were originally implemented as 14144cb301eSSascha Wildner.In sys/queue.h 142445813b7SSascha Wildner.Dv LIST 143445813b7SSascha Wildnerstructures; 144445813b7SSascha Wildnerhowever this was changed in 145445813b7SSascha Wildner.Nx 1.4 146445813b7SSascha Wildnerto 147445813b7SSascha Wildner.Dv TAILQ 148445813b7SSascha Wildnerstructures. 149445813b7SSascha WildnerThis change was to allow the input and output filters to be processed in 150445813b7SSascha Wildnerreverse order, to allow the same path to be taken, in or out of the kernel. 151445813b7SSascha Wildner.Pp 152445813b7SSascha WildnerThe 153445813b7SSascha Wildner.Nm 154445813b7SSascha Wildnerinterface was changed in 1.4T to accept a 3rd parameter to both 155445813b7SSascha Wildner.Fn pfil_add_hook 156445813b7SSascha Wildnerand 157445813b7SSascha Wildner.Fn pfil_remove_hook , 158445813b7SSascha Wildnerintroducing the capability of per-protocol filtering. 159445813b7SSascha WildnerThis was done primarily in order to support filtering of IPv6. 160445813b7SSascha Wildner.Pp 161445813b7SSascha WildnerIn 1.5K, the 162445813b7SSascha Wildner.Nm 163445813b7SSascha Wildnerframework was changed to work with an arbitrary number of filtering points, 164445813b7SSascha Wildneras well as be less IP-centric. 165445813b7SSascha Wildner.Pp 166445813b7SSascha WildnerThe 167445813b7SSascha Wildner.Nm 168445813b7SSascha Wildnerinterface was imported from 169445813b7SSascha Wildner.Nx 170445813b7SSascha Wildnerinto 1714b2d9c41SSascha Wildner.Dx 1.0 1724b2d9c41SSascha Wildnerand was reworked to suit a threaded kernel model in 1736b53241aSSepherosa Ziehau.Dx 2.1 . 174445813b7SSascha Wildner.Sh AUTHORS 175*63e03116SSascha Wildner.An -nosplit 176445813b7SSascha WildnerThe 177445813b7SSascha Wildner.Nm 178*63e03116SSascha Wildnerinterface was designed and implemented by 179*63e03116SSascha Wildner.An Matthew R. Green , 180*63e03116SSascha Wildnerwith help from 181*63e03116SSascha Wildner.An Darren Reed , 182*63e03116SSascha Wildner.An Jason R. Thorpe 183*63e03116SSascha Wildnerand 184*63e03116SSascha Wildner.An Charles M. Hannum . 185*63e03116SSascha Wildner.An Darren Reed 186*63e03116SSascha Wildneradded support for IPv6 in addition to IPv4. 187*63e03116SSascha Wildner.An Jason R. Thorpe 188*63e03116SSascha Wildneradded support for multiple hooks and other clean up. 189