Merge branch 'vendor/FLEX'
[dragonfly.git] / share / man / man9 / pfil.9
1 .\"     $NetBSD: pfil.9,v 1.24 2004/01/01 15:24:35 wiz Exp $
2 .\" $DragonFly: src/share/man/man9/pfil.9,v 1.9 2008/09/20 06:08:13 sephe Exp $
3 .\"
4 .\" Copyright (c) 1996 Matthew R. Green
5 .\" All rights reserved.
6 .\"
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
9 .\" are met:
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\"    notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\"    notice, this list of conditions and the following disclaimer in the
14 .\"    documentation and/or other materials provided with the distribution.
15 .\" 3. The name of the author may not be used to endorse or promote products
16 .\"    derived from this software without specific prior written permission.
17 .\"
18 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 .\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 .\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" SUCH DAMAGE.
29 .\"
30 .Dd September 15, 2008
31 .Dt PFIL 9
32 .Os
33 .Sh NAME
34 .Nm pfil ,
35 .Nm pfil_head_register ,
36 .Nm pfil_head_unregister ,
37 .Nm pfil_head_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 .In sys/param.h
44 .In sys/mbuf.h
45 .In net/if.h
46 .In net/pfil.h
47 .Ft typedef int
48 .Fn (*pfil_func_t) "void *arg" "struct mbuf **mp" "struct ifnet *ifp" "int dir"
49 .Ft int
50 .Fn pfil_head_register "struct pfil_head *ph"
51 .Ft int
52 .Fn pfil_head_unregister "struct pfil_head *pfh"
53 .Ft struct pfil_head *
54 .Fn pfil_head_get "int type" "u_long val"
55 .Ft void
56 .Fn pfil_add_hook "pfil_func_t func" "void *arg" "int flags" "struct pfil_head *ph"
57 .Ft void
58 .Fn pfil_remove_hook "pfil_func_t func" "void *arg" "int flags" "struct pfil_head *ph"
59 .Ft int
60 .Fn pfil_run_hooks "struct pfil_head *ph" "struct mbuf **mp" "struct ifnet *ifp" "int dir"
61 .Sh DESCRIPTION
62 The
63 .Nm
64 framework allows for a specified function to be invoked for every
65 incoming or outgoing packet for a particular network I/O stream.
66 These hooks may be used to implement a firewall or perform packet
67 transformations.
68 .Pp
69 Packet filtering points are registered with
70 .Fn pfil_head_register .
71 Filtering points are identified by a key (void *) and a data link type
72 (int) in the
73 .Em pfil_head
74 structure.
75 Packet filters use the key and data link type to look up the filtering
76 point with which they register themselves.
77 The key is unique to the filtering point.
78 The data link type is a
79 .Xr bpf 4
80 DLT constant indicating what kind of header is present on the packet
81 at the filtering point.
82 Filtering points may be unregistered with the
83 .Fn pfil_head_unregister
84 function.
85 .Pp
86 Packet filters register/unregister themselves with a filtering point
87 with the
88 .Fn pfil_add_hook
89 and
90 .Fn pfil_remove_hook
91 functions, respectively.
92 The head is looked up using the
93 .Fn pfil_head_get
94 function, which takes the key and data link type that the packet filter
95 expects.
96 Filters may provide an argument to be passed to the filter when
97 invoked on a packet.
98 .Pp
99 When a filter is invoked, the packet appears just as if it
100 .Dq came off the wire .
101 That is, all protocol fields are in network byte order.
102 The filter is called with its specified argument, the pointer to the
103 pointer to the mbuf containing the packet, the pointer to the network
104 interface that the packet is traversing, and the direction
105 .Dv ( PFIL_IN
106 or
107 .Dv PFIL_OUT ,
108 see also below) that the packet is traveling.
109 The filter may change which mbuf the mbuf ** argument references.
110 The filter returns an
111 .Va errno
112 if the packet processing is to stop, or 0 if the processing is to continue.
113 If the packet processing is to stop, it is the responsibility of the
114 filter to free the packet.
115 .Pp
116 The
117 .Em flags
118 parameter, used in the
119 .Fn pfil_add_hook
120 and
121 .Fn pfil_remove_hook
122 functions, indicates when the filter should be called.
123 The flags are:
124 .Bl -tag -offset indent -width ".Dv PFIL_MPSAFE" -compact
125 .It Dv PFIL_IN
126 call me on incoming packets
127 .It Dv PFIL_OUT
128 call me on outgoing packets
129 .It Dv PFIL_ALL
130 call me on all of the above
131 .It Dv PFIL_MPSAFE
132 call me without BGL
133 .El
134 .Sh SEE ALSO
135 .Xr bpf 4
136 .Sh HISTORY
137 The
138 .Nm
139 interface first appeared in
140 .Nx 1.3 .
141 The
142 .Nm
143 input and output lists were originally implemented as
144 .In sys/queue.h
145 .Dv LIST
146 structures;
147 however this was changed in
148 .Nx 1.4
149 to
150 .Dv TAILQ
151 structures.
152 This change was to allow the input and output filters to be processed in
153 reverse order, to allow the same path to be taken, in or out of the kernel.
154 .Pp
155 The
156 .Nm
157 interface was changed in 1.4T to accept a 3rd parameter to both
158 .Fn pfil_add_hook
159 and
160 .Fn pfil_remove_hook ,
161 introducing the capability of per-protocol filtering.
162 This was done primarily in order to support filtering of IPv6.
163 .Pp
164 In 1.5K, the
165 .Nm
166 framework was changed to work with an arbitrary number of filtering points,
167 as well as be less IP-centric.
168 .Pp
169 The
170 .Nm
171 interface was imported from
172 .Nx
173 into
174 .Dx 1.0
175 and was reworked to suit a threaded kernel model in
176 .Dx 2.1 .
177 .Sh AUTHORS
178 The
179 .Nm
180 interface was designed and implemented by Matthew R. Green, with help
181 from Darren Reed, Jason R. Thorpe and Charles M. Hannum.
182 Darren Reed added support for IPv6 in addition to IPv4.
183 Jason R. Thorpe added support for multiple hooks and other clean up.