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