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