1.\" $NetBSD: pfil.9,v 1.28 2004/07/27 14:24:18 wiz Exp $ 2.\" 3.\" Copyright (c) 1996 Matthew R. Green 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. The name of the author may not be used to endorse or promote products 15.\" derived from this software without specific prior written permission. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" SUCH DAMAGE. 28.\" 29.Dd July 27, 2004 30.Dt PFIL 9 31.Os 32.Sh NAME 33.Nm pfil , 34.Nm pfil_head_register , 35.Nm pfil_head_unregister , 36.Nm pfil_head_get , 37.Nm pfil_hook_get , 38.Nm pfil_add_hook , 39.Nm pfil_remove_hook , 40.Nm pfil_run_hooks 41.Nd packet filter interface 42.Sh SYNOPSIS 43.In sys/param.h 44.In sys/mbuf.h 45.In net/if.h 46.In net/pfil.h 47.Ft int 48.Fn pfil_head_register "struct pfil_head *head" 49.Ft int 50.Fn pfil_head_unregister "struct pfil_head *head" 51.Ft struct pfil_head * 52.Fn pfil_head_get "int af" "u_long dlt" 53.Ft struct packet_filter_hook * 54.Fn pfil_hook_get "int dir" "struct pfil_head *head" 55.Ft void 56.Fn pfil_add_hook "int (*func)()" "void *arg" "int flags" "struct pfil_head *" 57.Ft void 58.Fn pfil_remove_hook "int (*func)()" "void *arg" "int flags" "struct pfil_head *" 59.Ft int 60.Fn (*func) "void *arg" "struct mbuf **mp" "struct ifnet *" "int dir" 61.Ft int 62.Fn pfil_run_hooks "struct pfil_head *head" "struct mbuf **mp" "struct ifnet *" "int dir" 63.Sh DESCRIPTION 64The 65.Nm 66framework allows for a specified function to be invoked for every 67incoming or outgoing packet for a particular network I/O stream. 68These hooks may be used to implement a firewall or perform packet 69transformations. 70.Pp 71Packet filtering points are registered with 72.Fn pfil_head_register . 73Filtering points are identified by a key (void *) and a data link type 74(int) in the 75.Em pfil_head 76structure. 77Packet filters use the key and data link type to look up the filtering 78point with which they register themselves. 79The key is unique to the filtering point. 80The data link type is a 81.Xr bpf 4 82DLT constant indicating what kind of header is present on the packet 83at the filtering point. 84Filtering points may be unregistered with the 85.Fn pfil_head_unregister 86function. 87.Pp 88Packet filters register/unregister themselves with a filtering point 89with the 90.Fn pfil_add_hook 91and 92.Fn pfil_remove_hook 93functions, respectively. 94The head is looked up using the 95.Fn pfil_head_get 96function, which takes the key and data link type that the packet filter 97expects. 98Filters may provide an argument to be passed to the filter when 99invoked on a packet. 100.Pp 101When a filter is invoked, the packet appears just as if it 102.Dq came off the wire . 103That is, all protocol fields are in network byte order. 104The filter is called with its specified argument, the pointer to the 105pointer to the mbuf containing the packet, the pointer to the network 106interface that the packet is traversing, and the direction 107.Dv ( PFIL_IN 108or 109.Dv PFIL_OUT , 110see also below) that the packet is traveling. 111The filter may change which mbuf the mbuf ** argument references. 112The filter returns an errno if the packet processing is to stop, or 0 113if the processing is to continue. 114If the packet processing is to stop, it is the responsibility of the 115filter to free the packet. 116.Pp 117The 118.Em flags 119parameter, used in the 120.Fn pfil_add_hook 121and 122.Fn pfil_remove_hook 123functions, indicates when the filter should be called. 124The flags are: 125.Bl -tag -offset indent -width PFIL_WAITOK -compact 126.It PFIL_IN 127call me on incoming packets 128.It PFIL_OUT 129call me on outgoing packets 130.It PFIL_ALL 131call me on all of the above 132.It PFIL_IFADDR 133call me on interface reconfig (mbuf ** is ioctl #) 134.It PFIL_IFNET 135call me on interface attach/detach (mbuf ** is either 136.Dv PFIL_IFNET_ATTACH 137or 138.Dv PFIL_IFNET_DETACH ) 139.It PFIL_WAITOK 140OK to call malloc with M_WAITOK. 141.El 142.Pp 143The 144.Nm 145interface is enabled in the kernel via the 146.Sy PFIL_HOOKS 147option. 148.Sh SEE ALSO 149.Xr bpf 4 150.Sh HISTORY 151The 152.Nm 153interface first appeared in 154.Nx 1.3 . 155The 156.Nm 157input and output lists were originally implemented as 158.Aq Pa sys/queue.h 159.Dv LIST 160structures; 161however this was changed in 162.Nx 1.4 163to 164.Dv TAILQ 165structures. 166This change was to allow the input and output filters to be processed in 167reverse order, to allow the same path to be taken, in or out of the kernel. 168.Pp 169The 170.Nm 171interface was changed in 1.4T to accept a 3rd parameter to both 172.Fn pfil_add_hook 173and 174.Fn pfil_remove_hook , 175introducing the capability of per-protocol filtering. 176This was done primarily in order to support filtering of IPv6. 177.Pp 178In 1.5K, the 179.Nm 180framework was changed to work with an arbitrary number of filtering points, 181as well as be less IP-centric. 182.Sh AUTHORS 183The 184.Nm 185interface was designed and implemented by Matthew R. Green, with help 186from Darren Reed, Jason R. Thorpe and Charles M. Hannum. 187Darren Reed added support for IPv6 in addition to IPv4. 188Jason R. Thorpe added support for multiple hooks and other clean up. 189.Sh BUGS 190The current 191.Nm 192implementation will need changes to suit a threaded kernel model. 193