1.\" $NetBSD: pfil.9,v 1.20 2002/11/22 12:15:27 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 November 10, 2000 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.Fd #include \*[Lt]sys/param.h\*[Gt] 44.Fd #include \*[Lt]sys/mbuf.h\*[Gt] 45.Fd #include \*[Lt]net/if.h\*[Gt] 46.Fd #include \*[Lt]net/pfil.h\*[Gt] 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 (PFIL_IN 107or PFIL_OUT) that the packet is traveling. 108The filter may change which mbuf the mbuf ** argument references. 109The filter returns an errno if the packet processing is to stop, or 0 110if the processing is to continue. 111If the packet processing is to stop, it is the responsibility of the 112filter to free the packet. 113.Pp 114The 115.Nm 116interface is enabled in the kernel via the 117.Sy PFIL_HOOKS 118option. 119.Sh SEE ALSO 120.Xr bpf 4 121.Sh HISTORY 122The 123.Nm 124interface first appeared in 125.Nx 1.3 . 126The 127.Nm 128input and output lists were originally implemented as 129.Fd \*[Lt]sys/queue.h\*[Gt] 130.Dv LIST 131structures; 132however this was changed in 133.Nx 1.4 134to 135.Dv TAILQ 136structures. 137This change was to allow the input and output filters to be processed in 138reverse order, to allow the same path to be taken, in or out of the kernel. 139.Pp 140The 141.Nm 142interface was changed in 1.4T to accept a 3rd parameter to both 143.Fn pfil_add_hook 144and 145.Fn pfil_remove_hook , 146introducing the capability of per-protocol filtering. 147This was done primarily in order to support filtering of IPv6. 148.Pp 149In 150.Nx 1.5K , 151the 152.Nm 153framework was changed to work with an arbitrary number of filtering points, 154as well as be less IP-centric. 155.Sh BUGS 156The current 157.Nm 158implementation will need changes to suit a threaded kernel model. 159