1.\" $NetBSD: pfil.9,v 1.22 2003/07/01 13:04:06 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.\" $FreeBSD$ 30.\" 31.Dd September 29, 2004 32.Dt PFIL 9 33.Os 34.Sh NAME 35.Nm pfil , 36.Nm pfil_head_register , 37.Nm pfil_head_unregister , 38.Nm pfil_head_get , 39.Nm pfil_hook_get , 40.Nm pfil_add_hook , 41.Nm pfil_remove_hook , 42.Nm pfil_run_hooks 43.Nd packet filter interface 44.Sh SYNOPSIS 45.In sys/param.h 46.In sys/mbuf.h 47.In net/if.h 48.In net/pfil.h 49.Ft int 50.Fn pfil_head_register "struct pfil_head *head" 51.Ft int 52.Fn pfil_head_unregister "struct pfil_head *head" 53.Ft "struct pfil_head *" 54.Fn pfil_head_get "int af" "u_long dlt" 55.Ft "struct packet_filter_hook *" 56.Fn pfil_hook_get "int dir" "struct pfil_head *head" 57.Ft void 58.Fn pfil_add_hook "int (*func)()" "void *arg" "int flags" "struct pfil_head *" 59.Ft void 60.Fn pfil_remove_hook "int (*func)()" "void *arg" "int flags" "struct pfil_head *" 61.Ft int 62.Fn (*func) "void *arg" "struct mbuf **mp" "struct ifnet *" "int dir" "struct inpcb *" 63.Ft int 64.Fn pfil_run_hooks "struct pfil_head *head" "struct mbuf **mp" "struct ifnet *" "int dir" "struct inpcb *" 65.Sh DESCRIPTION 66The 67.Nm 68framework allows for a specified function to be invoked for every 69incoming or outgoing packet for a particular network I/O stream. 70These hooks may be used to implement a firewall or perform packet 71transformations. 72.Pp 73Packet filtering points are registered with 74.Fn pfil_head_register . 75Filtering points are identified by a key 76.Pq Vt "void *" 77and a data link type 78.Pq Vt int 79in the 80.Vt pfil_head 81structure. 82Packet filters use the key and data link type to look up the filtering 83point with which they register themselves. 84The key is unique to the filtering point. 85The data link type is a 86.Xr bpf 4 87DLT constant indicating what kind of header is present on the packet 88at the filtering point. 89Filtering points may be unregistered with the 90.Fn pfil_head_unregister 91function. 92.Pp 93Packet filters register/unregister themselves with a filtering point 94with the 95.Fn pfil_add_hook 96and 97.Fn pfil_remove_hook 98functions, respectively. 99The head is looked up using the 100.Fn pfil_head_get 101function, which takes the key and data link type that the packet filter 102expects. 103Filters may provide an argument to be passed to the filter when 104invoked on a packet. 105.Pp 106When a filter is invoked, the packet appears just as if it 107.Dq came off the wire . 108That is, all protocol fields are in network byte order. 109The filter is called with its specified argument, the pointer to the 110pointer to the 111.Vt mbuf 112containing the packet, the pointer to the network 113interface that the packet is traversing, and the direction 114.Dv ( PFIL_IN 115or 116.Dv PFIL_OUT ) 117that the packet is traveling. 118The filter may change which mbuf the 119.Vt "mbuf\ **" 120argument references. 121The filter returns an error (errno) if the packet processing is to stop, or 0 122if the processing is to continue. 123If the packet processing is to stop, it is the responsibility of the 124filter to free the packet. 125.Sh RETURN VALUES 126If successful, 127.Fn pfil_head_get 128returns the 129.Vt pfil_head 130structure for the given key/dlt. 131The 132.Fn pfil_add_hook 133and 134.Fn pfil_remove_hook 135functions 136return 0 if successful. 137If called with flag 138.Dv PFIL_WAITOK , 139.Fn pfil_remove_hook 140is expected to always succeed. 141.Pp 142The 143.Fn pfil_head_unregister 144function 145might sleep! 146.Sh SEE ALSO 147.Xr bpf 4 , 148.Xr if_bridge 4 149.Sh HISTORY 150The 151.Nm 152interface first appeared in 153.Nx 1.3 . 154The 155.Nm 156input and output lists were originally implemented as 157.In sys/queue.h 158.Dv LIST 159structures; 160however this was changed in 161.Nx 1.4 162to 163.Dv TAILQ 164structures. 165This change was to allow the input and output filters to be processed in 166reverse order, to allow the same path to be taken, in or out of the kernel. 167.Pp 168The 169.Nm 170interface was changed in 1.4T to accept a 3rd parameter to both 171.Fn pfil_add_hook 172and 173.Fn pfil_remove_hook , 174introducing the capability of per-protocol filtering. 175This was done primarily in order to support filtering of IPv6. 176.Pp 177In 1.5K, the 178.Nm 179framework was changed to work with an arbitrary number of filtering points, 180as well as be less IP-centric. 181.Pp 182Fine-grained locking was added in 183.Fx 5.2 . 184.Sh BUGS 185The 186.Fn pfil_hook_get 187function 188is only safe for internal use. 189.Pp 190.Fx 191implements only hooks for 192.Dv AF_INET 193and 194.Dv AF_INET6 . 195Packets diverted through these hooks have data in 196host byte order contrary to the above statements. 197.Pp 198The 199.Xr if_bridge 4 200diverts 201.Dv AF_INET 202and 203.Dv AF_INET6 204traffic according to its sysctl settings, but contrary to the above 205statements, the data is provided in host byte order. 206.Pp 207When a 208.Vt pfil_head 209is being modified, no traffic is diverted 210(to avoid deadlock). 211This means that traffic may be dropped unconditionally for a short period 212of time. 213.Fn pfil_run_hooks 214will return 215.Er ENOBUFS 216to indicate this. 217