1 .\" $NetBSD: pfil.9,v 1.22 2003/07/01 13:04:06 wiz Exp $
3 .\" Copyright (c) 1996 Matthew R. Green
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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.
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
36 .Nm pfil_head_register ,
37 .Nm pfil_head_unregister ,
40 .Nm pfil_remove_hook ,
46 .Nd packet filter interface
53 typedef int (*pfil_func_t)(void *arg, struct mbuf **mp, struct ifnet *, int dir, struct inpcb);
55 .Fn pfil_head_register "struct pfil_head *head"
57 .Fn pfil_head_unregister "struct pfil_head *head"
58 .Ft "struct pfil_head *"
59 .Fn pfil_head_get "int af" "u_long dlt"
61 .Fn pfil_add_hook "pfil_func_t" "void *arg" "int flags" "struct pfil_head *"
63 .Fn pfil_remove_hook "pfil_func_t" "void *arg" "int flags" "struct pfil_head *"
65 .Fn pfil_run_hooks "struct pfil_head *head" "struct mbuf **mp" "struct ifnet *" "int dir" "struct inpcb *"
67 .Fn pfil_rlock "struct pfil_head *" "struct rm_priotracker *"
69 .Fn pfil_runlock "struct pfil_head *" "struct rm_priotracker *"
71 .Fn pfil_wlock "struct pfil_head *"
73 .Fn pfil_wunlock "struct pfil_head *"
78 framework allows for a specified function to be invoked for every
79 incoming or outgoing packet for a particular network I/O stream.
80 These hooks may be used to implement a firewall or perform packet
83 Packet filtering points are registered with
84 .Fn pfil_head_register .
85 Filtering points are identified by a key
92 Packet filters use the key and data link type to look up the filtering
93 point with which they register themselves.
94 The key is unique to the filtering point.
95 The data link type is a
97 DLT constant indicating what kind of header is present on the packet
98 at the filtering point.
99 Each filtering point uses common per-VNET rmlock by default.
100 This can be changed by specifying
101 .Vt PFIL_FLAG_PRIVATE_LOCK
107 Note that specifying private lock can break filters sharing the same
108 ruleset and/or state between different data link types.
109 Filtering points may be unregistered with the
110 .Fn pfil_head_unregister
113 Packet filters register/unregister themselves with a filtering point
118 functions, respectively.
119 The head is looked up using the
121 function, which takes the key and data link type that the packet filter
123 Filters may provide an argument to be passed to the filter when
126 When a filter is invoked, the packet appears just as if it
127 .Dq came off the wire .
128 That is, all protocol fields are in network byte order.
129 The filter is called with its specified argument, the pointer to the
132 containing the packet, the pointer to the network
133 interface that the packet is traversing, and the direction
137 that the packet is traveling.
138 The filter may change which mbuf the
141 The filter returns an error (errno) if the packet processing is to stop, or 0
142 if the processing is to continue.
143 If the packet processing is to stop, it is the responsibility of the
144 filter to free the packet.
146 Every filter hook is called with
149 All heads uses the same lock within the same VNET instance.
150 Packet filter can use this lock instead of own locking model to
160 .Va struct rm_priotracker
161 to be passed as argument.
162 Filter can acquire and release writer lock via
171 Currently, filtering points are implemented for the following link types:
173 .Bl -tag -width "AF_INET6" -offset XXX -compact
186 structure for the given key/dlt.
192 return 0 if successful.
196 is expected to always succeed.
199 .Fn pfil_head_unregister
209 interface first appeared in
213 input and output lists were originally implemented as
217 however this was changed in
222 This change was to allow the input and output filters to be processed in
223 reverse order, to allow the same path to be taken, in or out of the kernel.
227 interface was changed in 1.4T to accept a 3rd parameter to both
230 .Fn pfil_remove_hook ,
231 introducing the capability of per-protocol filtering.
232 This was done primarily in order to support filtering of IPv6.
236 framework was changed to work with an arbitrary number of filtering points,
237 as well as be less IP-centric.
239 Fine-grained locking was added in
242 lock export was added in
247 is being modified, no traffic is diverted
249 This means that traffic may be dropped unconditionally for a short period