]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - share/man/man9/pfil.9
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / share / man / man9 / pfil.9
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 August 23, 2013
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_add_hook ,
40 .Nm pfil_remove_hook ,
41 .Nm pfil_run_hooks ,
42 .Nm pfil_rlock ,
43 .Nm pfil_runlock ,
44 .Nm pfil_wlock ,
45 .Nm pfil_wunlock
46 .Nd packet filter interface
47 .Sh SYNOPSIS
48 .In sys/param.h
49 .In sys/mbuf.h
50 .In net/if.h
51 .In net/pfil.h
52 .Bd -literal
53 typedef int (*pfil_func_t)(void *arg, struct mbuf **mp, struct ifnet *, int dir, struct inpcb);
54 .Ft int
55 .Fn pfil_head_register "struct pfil_head *head"
56 .Ft int
57 .Fn pfil_head_unregister "struct pfil_head *head"
58 .Ft "struct pfil_head *"
59 .Fn pfil_head_get "int af" "u_long dlt"
60 .Ft void
61 .Fn pfil_add_hook "pfil_func_t" "void *arg" "int flags" "struct pfil_head *"
62 .Ft void
63 .Fn pfil_remove_hook "pfil_func_t" "void *arg" "int flags" "struct pfil_head *"
64 .Ft int
65 .Fn pfil_run_hooks "struct pfil_head *head" "struct mbuf **mp" "struct ifnet *" "int dir" "struct inpcb *"
66 .Ft void
67 .Fn pfil_rlock "struct pfil_head *" "struct rm_priotracker *"
68 .Ft void
69 .Fn pfil_runlock "struct pfil_head *" "struct rm_priotracker *"
70 .Ft void
71 .Fn pfil_wlock "struct pfil_head *"
72 .Ft void
73 .Fn pfil_wunlock "struct pfil_head *"
74 .Ed
75 .Sh DESCRIPTION
76 The
77 .Nm
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
81 transformations.
82 .Pp
83 Packet filtering points are registered with
84 .Fn pfil_head_register .
85 Filtering points are identified by a key
86 .Pq Vt "void *"
87 and a data link type
88 .Pq Vt int
89 in the
90 .Vt pfil_head
91 structure.
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
96 .Xr bpf 4
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
102 as
103 .Vt "flags"
104 field in the
105 .Vt pfil_head
106 structure.
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
111 function.
112 .Pp
113 Packet filters register/unregister themselves with a filtering point
114 with the
115 .Fn pfil_add_hook
116 and
117 .Fn pfil_remove_hook
118 functions, respectively.
119 The head is looked up using the
120 .Fn pfil_head_get
121 function, which takes the key and data link type that the packet filter
122 expects.
123 Filters may provide an argument to be passed to the filter when
124 invoked on a packet.
125 .Pp
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
130 pointer to the
131 .Vt mbuf
132 containing the packet, the pointer to the network
133 interface that the packet is traversing, and the direction
134 .Dv ( PFIL_IN
135 or
136 .Dv PFIL_OUT )
137 that the packet is traveling.
138 The filter may change which mbuf the
139 .Vt "mbuf\ **"
140 argument references.
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.
145 .Pp
146 Every filter hook is called with
147 .Nm
148 read lock held.
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
151 improve performance.
152 Since
153 .Nm
154 uses
155 .Xr rmlock 9
156 .Fn pfil_rlock
157 and
158 .Fn pfil_runlock
159 require
160 .Va struct rm_priotracker
161 to be passed as argument.
162 Filter can acquire and release writer lock via
163 .Fn pfil_wlock
164 and
165 .Fn pfil_wunlock
166 functions.
167 See
168 .Xr rmlock 9
169 for more details.
170 .Sh FILTERING POINTS
171 Currently, filtering points are implemented for the following link types:
172 .Pp
173 .Bl -tag -width "AF_INET6" -offset XXX -compact
174 .It AF_INET
175 IPv4 packets.
176 .It AF_INET6
177 IPv6 packets.
178 .It AF_LINK
179 Link-layer packets.
180 .El
181 .Sh RETURN VALUES
182 If successful,
183 .Fn pfil_head_get
184 returns the
185 .Vt pfil_head
186 structure for the given key/dlt.
187 The
188 .Fn pfil_add_hook
189 and
190 .Fn pfil_remove_hook
191 functions
192 return 0 if successful.
193 If called with flag
194 .Dv PFIL_WAITOK ,
195 .Fn pfil_remove_hook
196 is expected to always succeed.
197 .Pp
198 The
199 .Fn pfil_head_unregister
200 function
201 might sleep!
202 .Sh SEE ALSO
203 .Xr bpf 4 ,
204 .Xr if_bridge 4 ,
205 .Xr rmlock 9
206 .Sh HISTORY
207 The
208 .Nm
209 interface first appeared in
210 .Nx 1.3 .
211 The
212 .Nm
213 input and output lists were originally implemented as
214 .In sys/queue.h
215 .Dv LIST
216 structures;
217 however this was changed in
218 .Nx 1.4
219 to
220 .Dv TAILQ
221 structures.
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.
224 .Pp
225 The
226 .Nm
227 interface was changed in 1.4T to accept a 3rd parameter to both
228 .Fn pfil_add_hook
229 and
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.
233 .Pp
234 In 1.5K, the
235 .Nm
236 framework was changed to work with an arbitrary number of filtering points,
237 as well as be less IP-centric.
238 .Pp
239 Fine-grained locking was added in
240 .Fx 5.2 .
241 .Nm
242 lock export was added in
243 .Fx 10.0 .
244 .Sh BUGS
245 When a
246 .Vt pfil_head
247 is being modified, no traffic is diverted
248 (to avoid deadlock).
249 This means that traffic may be dropped unconditionally for a short period
250 of time.
251 .Fn pfil_run_hooks
252 will return
253 .Er ENOBUFS
254 to indicate this.