npf_handler.c revision 1.22 1 /* $NetBSD: npf_handler.c,v 1.22 2012/09/16 13:47:41 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This material is based upon work partially supported by The
8 * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * NPF packet handler.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.22 2012/09/16 13:47:41 rmind Exp $");
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41
42 #include <sys/mbuf.h>
43 #include <sys/mutex.h>
44 #include <net/if.h>
45 #include <net/pfil.h>
46 #include <sys/socketvar.h>
47
48 #include <netinet/in_systm.h>
49 #include <netinet/in.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/ip6.h>
52 #include <netinet6/ip6_var.h>
53
54 #include "npf_impl.h"
55
56 /*
57 * If npf_ph_if != NULL, pfil hooks are registered. If NULL, not registered.
58 * Used to check the state. Locked by: softnet_lock + KERNEL_LOCK (XXX).
59 */
60 static struct pfil_head * npf_ph_if = NULL;
61 static struct pfil_head * npf_ph_inet = NULL;
62 static struct pfil_head * npf_ph_inet6 = NULL;
63
64 /*
65 * npf_ifhook: hook handling interface changes.
66 */
67 static int
68 npf_ifhook(void *arg, struct mbuf **mp, ifnet_t *ifp, int di)
69 {
70
71 return 0;
72 }
73
74 /*
75 * npf_packet_handler: main packet handling routine for layer 3.
76 *
77 * Note: packet flow and inspection logic is in strict order.
78 */
79 int
80 npf_packet_handler(void *arg, struct mbuf **mp, ifnet_t *ifp, int di)
81 {
82 nbuf_t *nbuf = *mp;
83 npf_cache_t npc;
84 npf_session_t *se;
85 npf_ruleset_t *rlset;
86 npf_rule_t *rl;
87 npf_rproc_t *rp;
88 int error, retfl;
89 int decision;
90
91 /*
92 * Initialise packet information cache.
93 * Note: it is enough to clear the info bits.
94 */
95 npc.npc_info = 0;
96 decision = NPF_DECISION_BLOCK;
97 error = 0;
98 retfl = 0;
99 rp = NULL;
100
101 /* Cache everything. Determine whether it is an IP fragment. */
102 if (npf_cache_all(&npc, nbuf) & NPC_IPFRAG) {
103 /*
104 * Pass to IPv4 or IPv6 reassembly mechanism.
105 */
106 error = EINVAL;
107
108 if (npf_iscached(&npc, NPC_IP4)) {
109 struct ip *ip = nbuf_dataptr(*mp);
110 error = ip_reass_packet(mp, ip);
111 } else if (npf_iscached(&npc, NPC_IP6)) {
112 #ifdef INET6
113 /*
114 * Note: ip6_reass_packet() offset is the start of
115 * the fragment header.
116 */
117 const u_int hlen = npf_cache_hlen(&npc);
118 error = ip6_reass_packet(mp, hlen);
119 #endif
120 }
121 if (error) {
122 npf_stats_inc(NPF_STAT_REASSFAIL);
123 se = NULL;
124 goto out;
125 }
126 if (*mp == NULL) {
127 /* More fragments should come; return. */
128 npf_stats_inc(NPF_STAT_FRAGMENTS);
129 return 0;
130 }
131
132 /*
133 * Reassembly is complete, we have the final packet.
134 * Cache again, since layer 4 data is accessible now.
135 */
136 nbuf = (nbuf_t *)*mp;
137 npc.npc_info = 0;
138
139 int ret __unused = npf_cache_all(&npc, nbuf);
140 KASSERT((ret & NPC_IPFRAG) == 0);
141 npf_stats_inc(NPF_STAT_REASSEMBLY);
142 }
143
144 /* Inspect the list of sessions. */
145 se = npf_session_inspect(&npc, nbuf, ifp, di, &error);
146
147 /* If "passing" session found - skip the ruleset inspection. */
148 if (se && npf_session_pass(se, &rp)) {
149 npf_stats_inc(NPF_STAT_PASS_SESSION);
150 KASSERT(error == 0);
151 goto pass;
152 }
153 if (error) {
154 goto block;
155 }
156
157 /* Acquire the lock, inspect the ruleset using this packet. */
158 npf_core_enter();
159 rlset = npf_core_ruleset();
160 rl = npf_ruleset_inspect(&npc, nbuf, rlset, ifp, di, NPF_LAYER_3);
161 if (rl == NULL) {
162 bool default_pass = npf_default_pass();
163 npf_core_exit();
164
165 if (default_pass) {
166 npf_stats_inc(NPF_STAT_PASS_DEFAULT);
167 goto pass;
168 }
169 npf_stats_inc(NPF_STAT_BLOCK_DEFAULT);
170 goto block;
171 }
172
173 /*
174 * Get the rule procedure (acquires a reference) for assocation
175 * with a session (if any) and execution.
176 */
177 KASSERT(rp == NULL);
178 rp = npf_rule_getrproc(rl);
179
180 /* Apply the rule, release the lock. */
181 error = npf_rule_apply(&npc, nbuf, rl, &retfl);
182 if (error) {
183 npf_stats_inc(NPF_STAT_BLOCK_RULESET);
184 goto block;
185 }
186 npf_stats_inc(NPF_STAT_PASS_RULESET);
187
188 /*
189 * Establish a "pass" session, if required. Just proceed, if session
190 * creation fails (e.g. due to unsupported protocol).
191 *
192 * Note: the reference on the rule procedure is transfered to the
193 * session. It will be released on session destruction.
194 */
195 if ((retfl & NPF_RULE_STATEFUL) != 0 && !se) {
196 se = npf_session_establish(&npc, nbuf, ifp, di);
197 if (se) {
198 npf_session_setpass(se, rp);
199 }
200 }
201 pass:
202 decision = NPF_DECISION_PASS;
203 KASSERT(error == 0);
204 /*
205 * Perform NAT.
206 */
207 error = npf_do_nat(&npc, se, nbuf, ifp, di);
208 block:
209 /*
210 * Execute the rule procedure, if any is associated.
211 * It may reverse the decision from pass to block.
212 */
213 if (rp) {
214 npf_rproc_run(&npc, nbuf, rp, &decision);
215 }
216 out:
217 /*
218 * Release the reference on a session. Release the reference on a
219 * rule procedure only if there was no association.
220 */
221 if (se) {
222 npf_session_release(se);
223 } else if (rp) {
224 npf_rproc_release(rp);
225 }
226
227 /* Pass the packet if decided and there is no error. */
228 if (decision == NPF_DECISION_PASS && !error) {
229 /*
230 * XXX: Disable for now, it will be set accordingly later,
231 * for optimisations (to reduce inspection).
232 */
233 (*mp)->m_flags &= ~M_CANFASTFWD;
234 return 0;
235 }
236
237 /*
238 * Block the packet. ENETUNREACH is used to indicate blocking.
239 * Depending on the flags and protocol, return TCP reset (RST) or
240 * ICMP destination unreachable.
241 */
242 if (retfl && npf_return_block(&npc, nbuf, retfl)) {
243 *mp = NULL;
244 }
245
246 if (!error) {
247 error = ENETUNREACH;
248 }
249
250 if (*mp) {
251 m_freem(*mp);
252 *mp = NULL;
253 }
254 return error;
255 }
256
257 /*
258 * npf_pfil_register: register pfil(9) hooks.
259 */
260 int
261 npf_pfil_register(void)
262 {
263 int error;
264
265 mutex_enter(softnet_lock);
266 KERNEL_LOCK(1, NULL);
267
268 /* Check if pfil hooks are not already registered. */
269 if (npf_ph_if) {
270 error = EEXIST;
271 goto fail;
272 }
273
274 /* Capture point of any activity in interfaces and IP layer. */
275 npf_ph_if = pfil_head_get(PFIL_TYPE_IFNET, 0);
276 npf_ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
277 npf_ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
278 if (!npf_ph_if || (!npf_ph_inet && !npf_ph_inet6)) {
279 npf_ph_if = NULL;
280 error = ENOENT;
281 goto fail;
282 }
283
284 /* Interface re-config or attach/detach hook. */
285 error = pfil_add_hook(npf_ifhook, NULL,
286 PFIL_WAITOK | PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
287 KASSERT(error == 0);
288
289 /* Packet IN/OUT handler on all interfaces and IP layer. */
290 if (npf_ph_inet) {
291 error = pfil_add_hook(npf_packet_handler, NULL,
292 PFIL_WAITOK | PFIL_ALL, npf_ph_inet);
293 KASSERT(error == 0);
294 }
295 if (npf_ph_inet6) {
296 error = pfil_add_hook(npf_packet_handler, NULL,
297 PFIL_WAITOK | PFIL_ALL, npf_ph_inet6);
298 KASSERT(error == 0);
299 }
300 fail:
301 KERNEL_UNLOCK_ONE(NULL);
302 mutex_exit(softnet_lock);
303
304 return error;
305 }
306
307 /*
308 * npf_pfil_unregister: unregister pfil(9) hooks.
309 */
310 void
311 npf_pfil_unregister(void)
312 {
313
314 mutex_enter(softnet_lock);
315 KERNEL_LOCK(1, NULL);
316
317 if (npf_ph_if) {
318 (void)pfil_remove_hook(npf_ifhook, NULL,
319 PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
320 }
321 if (npf_ph_inet) {
322 (void)pfil_remove_hook(npf_packet_handler, NULL,
323 PFIL_ALL, npf_ph_inet);
324 }
325 if (npf_ph_inet6) {
326 (void)pfil_remove_hook(npf_packet_handler, NULL,
327 PFIL_ALL, npf_ph_inet6);
328 }
329
330 npf_ph_if = NULL;
331
332 KERNEL_UNLOCK_ONE(NULL);
333 mutex_exit(softnet_lock);
334 }
335
336 bool
337 npf_pfil_registered_p(void)
338 {
339 return npf_ph_if != NULL;
340 }
341