Merge branch 'vendor/OPENSSL'
[dragonfly.git] / share / man / man9 / pfil.9
1 .\"     $NetBSD: pfil.9,v 1.24 2004/01/01 15:24:35 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 January 16, 2015
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_add_hook ,
38 .Nm pfil_remove_hook ,
39 .Nm pfil_run_hooks
40 .Nd packet filter interface
41 .Sh SYNOPSIS
42 .In sys/param.h
43 .In sys/mbuf.h
44 .In net/if.h
45 .In net/pfil.h
46 .Ft typedef int
47 .Fn (*pfil_func_t) "void *arg" "struct mbuf **mp" "struct ifnet *ifp" "int dir"
48 .Ft int
49 .Fn pfil_head_register "struct pfil_head *ph"
50 .Ft int
51 .Fn pfil_head_unregister "struct pfil_head *pfh"
52 .Ft struct pfil_head *
53 .Fn pfil_head_get "int type" "u_long val"
54 .Ft int
55 .Fn pfil_add_hook "pfil_func_t func" "void *arg" "int flags" "struct pfil_head *ph"
56 .Ft int
57 .Fn pfil_remove_hook "pfil_func_t func" "void *arg" "int flags" "struct pfil_head *ph"
58 .Ft int
59 .Fn pfil_run_hooks "struct pfil_head *ph" "struct mbuf **mp" "struct ifnet *ifp" "int dir"
60 .Sh DESCRIPTION
61 The
62 .Nm
63 framework allows for a specified function to be invoked for every
64 incoming or outgoing packet for a particular network I/O stream.
65 These hooks may be used to implement a firewall or perform packet
66 transformations.
67 .Pp
68 Packet filtering points are registered with
69 .Fn pfil_head_register .
70 Filtering points are identified by a key (void *) and a data link type
71 (int) in the
72 .Em pfil_head
73 structure.
74 Packet filters use the key and data link type to look up the filtering
75 point with which they register themselves.
76 The key is unique to the filtering point.
77 The data link type is a
78 .Xr bpf 4
79 DLT constant indicating what kind of header is present on the packet
80 at the filtering point.
81 Filtering points may be unregistered with the
82 .Fn pfil_head_unregister
83 function.
84 .Pp
85 Packet filters register/unregister themselves with a filtering point
86 with the
87 .Fn pfil_add_hook
88 and
89 .Fn pfil_remove_hook
90 functions, respectively.
91 The head is looked up using the
92 .Fn pfil_head_get
93 function, which takes the key and data link type that the packet filter
94 expects.
95 Filters may provide an argument to be passed to the filter when
96 invoked on a packet.
97 .Pp
98 When a filter is invoked, the packet appears just as if it
99 .Dq came off the wire .
100 That is, all protocol fields are in network byte order.
101 The filter is called with its specified argument, the pointer to the
102 pointer to the mbuf containing the packet, the pointer to the network
103 interface that the packet is traversing, and the direction
104 .Dv ( PFIL_IN
105 or
106 .Dv PFIL_OUT ,
107 see also below) that the packet is traveling.
108 The filter may change which mbuf the mbuf ** argument references.
109 The filter returns an
110 .Va errno
111 if the packet processing is to stop, or 0 if the processing is to continue.
112 If the packet processing is to stop, it is the responsibility of the
113 filter to free the packet.
114 .Pp
115 The
116 .Em flags
117 parameter, used in the
118 .Fn pfil_add_hook
119 and
120 .Fn pfil_remove_hook
121 functions, indicates when the filter should be called.
122 The flags are:
123 .Bl -tag -offset indent -width ".Dv PFIL_MPSAFE" -compact
124 .It Dv PFIL_IN
125 call me on incoming packets
126 .It Dv PFIL_OUT
127 call me on outgoing packets
128 .It Dv PFIL_ALL
129 call me on all of the above
130 .It Dv PFIL_MPSAFE
131 call me without BGL
132 .El
133 .Sh SEE ALSO
134 .Xr bpf 4
135 .Sh HISTORY
136 The
137 .Nm
138 interface first appeared in
139 .Nx 1.3 .
140 The
141 .Nm
142 input and output lists were originally implemented as
143 .In sys/queue.h
144 .Dv LIST
145 structures;
146 however this was changed in
147 .Nx 1.4
148 to
149 .Dv TAILQ
150 structures.
151 This change was to allow the input and output filters to be processed in
152 reverse order, to allow the same path to be taken, in or out of the kernel.
153 .Pp
154 The
155 .Nm
156 interface was changed in 1.4T to accept a 3rd parameter to both
157 .Fn pfil_add_hook
158 and
159 .Fn pfil_remove_hook ,
160 introducing the capability of per-protocol filtering.
161 This was done primarily in order to support filtering of IPv6.
162 .Pp
163 In 1.5K, the
164 .Nm
165 framework was changed to work with an arbitrary number of filtering points,
166 as well as be less IP-centric.
167 .Pp
168 The
169 .Nm
170 interface was imported from
171 .Nx
172 into
173 .Dx 1.0
174 and was reworked to suit a threaded kernel model in
175 .Dx 2.1 .
176 .Sh AUTHORS
177 The
178 .Nm
179 interface was designed and implemented by Matthew R. Green, with help
180 from Darren Reed, Jason R. Thorpe and Charles M. Hannum.
181 Darren Reed added support for IPv6 in addition to IPv4.
182 Jason R. Thorpe added support for multiple hooks and other clean up.