npf_handler.c revision 1.21.2.3 1 1.21.2.3 tls /* $NetBSD: npf_handler.c,v 1.21.2.3 2014/08/20 00:04:35 tls Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.21.2.2 tls * 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.21.2.3 tls *
35 1.21.2.3 tls * 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.21.2.3 tls __KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.21.2.3 2014/08/20 00:04:35 tls Exp $");
40 1.1 rmind
41 1.14 rmind #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.8 zoltan #include <netinet/ip6.h>
54 1.8 zoltan #include <netinet6/ip6_var.h>
55 1.4 rmind
56 1.1 rmind #include "npf_impl.h"
57 1.21.2.3 tls #include "npf_conn.h"
58 1.1 rmind
59 1.21.2.3 tls static bool pfil_registered = false;
60 1.21.2.3 tls static pfil_head_t * npf_ph_if = NULL;
61 1.21.2.3 tls static pfil_head_t * npf_ph_inet = NULL;
62 1.21.2.3 tls static pfil_head_t * npf_ph_inet6 = NULL;
63 1.1 rmind
64 1.21.2.2 tls #ifndef INET6
65 1.21.2.2 tls #define ip6_reass_packet(x, y) ENOTSUP
66 1.21.2.2 tls #endif
67 1.21.2.2 tls
68 1.1 rmind /*
69 1.1 rmind * npf_ifhook: hook handling interface changes.
70 1.1 rmind */
71 1.1 rmind static int
72 1.6 rmind npf_ifhook(void *arg, struct mbuf **mp, ifnet_t *ifp, int di)
73 1.1 rmind {
74 1.21.2.3 tls u_long cmd = (u_long)mp;
75 1.1 rmind
76 1.21.2.3 tls if (di == PFIL_IFNET) {
77 1.21.2.3 tls switch (cmd) {
78 1.21.2.3 tls case PFIL_IFNET_ATTACH:
79 1.21.2.3 tls npf_ifmap_attach(ifp);
80 1.21.2.3 tls break;
81 1.21.2.3 tls case PFIL_IFNET_DETACH:
82 1.21.2.3 tls npf_ifmap_detach(ifp);
83 1.21.2.3 tls break;
84 1.21.2.3 tls }
85 1.21.2.3 tls }
86 1.1 rmind return 0;
87 1.1 rmind }
88 1.1 rmind
89 1.21.2.2 tls static int
90 1.21.2.3 tls npf_reassembly(npf_cache_t *npc, struct mbuf **mp)
91 1.21.2.2 tls {
92 1.21.2.3 tls nbuf_t *nbuf = npc->npc_nbuf;
93 1.21.2.2 tls int error = EINVAL;
94 1.21.2.2 tls
95 1.21.2.2 tls /* Reset the mbuf as it may have changed. */
96 1.21.2.2 tls *mp = nbuf_head_mbuf(nbuf);
97 1.21.2.2 tls nbuf_reset(nbuf);
98 1.21.2.2 tls
99 1.21.2.2 tls if (npf_iscached(npc, NPC_IP4)) {
100 1.21.2.2 tls struct ip *ip = nbuf_dataptr(nbuf);
101 1.21.2.2 tls error = ip_reass_packet(mp, ip);
102 1.21.2.2 tls } else if (npf_iscached(npc, NPC_IP6)) {
103 1.21.2.2 tls /*
104 1.21.2.2 tls * Note: ip6_reass_packet() offset is the start of
105 1.21.2.2 tls * the fragment header.
106 1.21.2.2 tls */
107 1.21.2.2 tls error = ip6_reass_packet(mp, npc->npc_hlen);
108 1.21.2.2 tls if (error && *mp == NULL) {
109 1.21.2.2 tls memset(nbuf, 0, sizeof(nbuf_t));
110 1.21.2.2 tls }
111 1.21.2.2 tls }
112 1.21.2.2 tls if (error) {
113 1.21.2.2 tls npf_stats_inc(NPF_STAT_REASSFAIL);
114 1.21.2.2 tls return error;
115 1.21.2.2 tls }
116 1.21.2.2 tls if (*mp == NULL) {
117 1.21.2.2 tls /* More fragments should come. */
118 1.21.2.2 tls npf_stats_inc(NPF_STAT_FRAGMENTS);
119 1.21.2.2 tls return 0;
120 1.21.2.2 tls }
121 1.21.2.2 tls
122 1.21.2.2 tls /*
123 1.21.2.2 tls * Reassembly is complete, we have the final packet.
124 1.21.2.2 tls * Cache again, since layer 4 data is accessible now.
125 1.21.2.2 tls */
126 1.21.2.2 tls nbuf_init(nbuf, *mp, nbuf->nb_ifp);
127 1.21.2.2 tls npc->npc_info = 0;
128 1.21.2.2 tls
129 1.21.2.3 tls if (npf_cache_all(npc) & NPC_IPFRAG) {
130 1.21.2.2 tls return EINVAL;
131 1.21.2.2 tls }
132 1.21.2.2 tls npf_stats_inc(NPF_STAT_REASSEMBLY);
133 1.21.2.2 tls return 0;
134 1.21.2.2 tls }
135 1.21.2.2 tls
136 1.1 rmind /*
137 1.2 rmind * npf_packet_handler: main packet handling routine for layer 3.
138 1.1 rmind *
139 1.1 rmind * Note: packet flow and inspection logic is in strict order.
140 1.1 rmind */
141 1.1 rmind int
142 1.6 rmind npf_packet_handler(void *arg, struct mbuf **mp, ifnet_t *ifp, int di)
143 1.1 rmind {
144 1.21.2.2 tls nbuf_t nbuf;
145 1.1 rmind npf_cache_t npc;
146 1.21.2.3 tls npf_conn_t *con;
147 1.1 rmind npf_rule_t *rl;
148 1.5 rmind npf_rproc_t *rp;
149 1.14 rmind int error, retfl;
150 1.14 rmind int decision;
151 1.1 rmind
152 1.1 rmind /*
153 1.1 rmind * Initialise packet information cache.
154 1.1 rmind * Note: it is enough to clear the info bits.
155 1.1 rmind */
156 1.21.2.2 tls KASSERT(ifp != NULL);
157 1.21.2.2 tls nbuf_init(&nbuf, *mp, ifp);
158 1.21.2.3 tls npc.npc_nbuf = &nbuf;
159 1.1 rmind npc.npc_info = 0;
160 1.21.2.3 tls
161 1.14 rmind decision = NPF_DECISION_BLOCK;
162 1.2 rmind error = 0;
163 1.2 rmind retfl = 0;
164 1.5 rmind rp = NULL;
165 1.1 rmind
166 1.10 rmind /* Cache everything. Determine whether it is an IP fragment. */
167 1.21.2.3 tls if (__predict_false(npf_cache_all(&npc) & NPC_IPFRAG)) {
168 1.18 rmind /*
169 1.18 rmind * Pass to IPv4 or IPv6 reassembly mechanism.
170 1.18 rmind */
171 1.21.2.3 tls error = npf_reassembly(&npc, mp);
172 1.18 rmind if (error) {
173 1.21.2.3 tls con = NULL;
174 1.4 rmind goto out;
175 1.4 rmind }
176 1.4 rmind if (*mp == NULL) {
177 1.4 rmind /* More fragments should come; return. */
178 1.4 rmind return 0;
179 1.4 rmind }
180 1.4 rmind }
181 1.4 rmind
182 1.21.2.3 tls /* Inspect the list of connections (if found, acquires a reference). */
183 1.21.2.3 tls con = npf_conn_inspect(&npc, di, &error);
184 1.2 rmind
185 1.21.2.3 tls /* If "passing" connection found - skip the ruleset inspection. */
186 1.21.2.3 tls if (con && npf_conn_pass(con, &rp)) {
187 1.21.2.3 tls npf_stats_inc(NPF_STAT_PASS_CONN);
188 1.14 rmind KASSERT(error == 0);
189 1.2 rmind goto pass;
190 1.14 rmind }
191 1.21.2.3 tls if (__predict_false(error)) {
192 1.21.2.2 tls if (error == ENETUNREACH)
193 1.21.2.2 tls goto block;
194 1.21.2.2 tls goto out;
195 1.2 rmind }
196 1.1 rmind
197 1.7 rmind /* Acquire the lock, inspect the ruleset using this packet. */
198 1.21.2.2 tls int slock = npf_config_read_enter();
199 1.21.2.2 tls npf_ruleset_t *rlset = npf_config_ruleset();
200 1.21.2.2 tls
201 1.21.2.3 tls rl = npf_ruleset_inspect(&npc, rlset, di, NPF_LAYER_3);
202 1.21.2.3 tls if (__predict_false(rl == NULL)) {
203 1.21.2.2 tls const bool pass = npf_default_pass();
204 1.21.2.2 tls npf_config_read_exit(slock);
205 1.14 rmind
206 1.21.2.2 tls if (pass) {
207 1.5 rmind npf_stats_inc(NPF_STAT_PASS_DEFAULT);
208 1.2 rmind goto pass;
209 1.2 rmind }
210 1.5 rmind npf_stats_inc(NPF_STAT_BLOCK_DEFAULT);
211 1.6 rmind goto block;
212 1.1 rmind }
213 1.1 rmind
214 1.13 rmind /*
215 1.21.2.2 tls * Get the rule procedure (acquires a reference) for association
216 1.21.2.3 tls * with a connection (if any) and execution.
217 1.13 rmind */
218 1.6 rmind KASSERT(rp == NULL);
219 1.13 rmind rp = npf_rule_getrproc(rl);
220 1.6 rmind
221 1.21.2.2 tls /* Conclude with the rule and release the lock. */
222 1.21.2.2 tls error = npf_rule_conclude(rl, &retfl);
223 1.21.2.2 tls npf_config_read_exit(slock);
224 1.21.2.2 tls
225 1.2 rmind if (error) {
226 1.5 rmind npf_stats_inc(NPF_STAT_BLOCK_RULESET);
227 1.6 rmind goto block;
228 1.1 rmind }
229 1.5 rmind npf_stats_inc(NPF_STAT_PASS_RULESET);
230 1.1 rmind
231 1.14 rmind /*
232 1.21.2.3 tls * Establish a "pass" connection, if required. Just proceed if
233 1.21.2.3 tls * connection creation fails (e.g. due to unsupported protocol).
234 1.14 rmind */
235 1.21.2.3 tls if ((retfl & NPF_RULE_STATEFUL) != 0 && !con) {
236 1.21.2.3 tls con = npf_conn_establish(&npc, di,
237 1.21.2.3 tls (retfl & NPF_RULE_MULTIENDS) == 0);
238 1.21.2.3 tls if (con) {
239 1.21.2.2 tls /*
240 1.21.2.2 tls * Note: the reference on the rule procedure is
241 1.21.2.3 tls * transfered to the connection. It will be
242 1.21.2.3 tls * released on connection destruction.
243 1.21.2.2 tls */
244 1.21.2.3 tls npf_conn_setpass(con, rp);
245 1.2 rmind }
246 1.1 rmind }
247 1.2 rmind pass:
248 1.14 rmind decision = NPF_DECISION_PASS;
249 1.2 rmind KASSERT(error == 0);
250 1.5 rmind /*
251 1.6 rmind * Perform NAT.
252 1.6 rmind */
253 1.21.2.3 tls error = npf_do_nat(&npc, con, di);
254 1.6 rmind block:
255 1.6 rmind /*
256 1.21.2.1 tls * Execute the rule procedure, if any is associated.
257 1.21.2.1 tls * It may reverse the decision from pass to block.
258 1.5 rmind */
259 1.21.2.3 tls if (rp && !npf_rproc_run(&npc, rp, &decision)) {
260 1.21.2.3 tls if (con) {
261 1.21.2.3 tls npf_conn_release(con);
262 1.21.2.3 tls }
263 1.21.2.3 tls npf_rproc_release(rp);
264 1.21.2.3 tls *mp = NULL;
265 1.21.2.3 tls return 0;
266 1.5 rmind }
267 1.1 rmind out:
268 1.13 rmind /*
269 1.21.2.3 tls * Release the reference on a connection. Release the reference
270 1.21.2.3 tls * on a rule procedure only if there was no association.
271 1.13 rmind */
272 1.21.2.3 tls if (con) {
273 1.21.2.3 tls npf_conn_release(con);
274 1.6 rmind } else if (rp) {
275 1.13 rmind npf_rproc_release(rp);
276 1.1 rmind }
277 1.1 rmind
278 1.21.2.2 tls /* Reset mbuf pointer before returning to the caller. */
279 1.21.2.2 tls if ((*mp = nbuf_head_mbuf(&nbuf)) == NULL) {
280 1.21.2.2 tls return error ? error : ENOMEM;
281 1.21.2.2 tls }
282 1.21.2.2 tls
283 1.14 rmind /* Pass the packet if decided and there is no error. */
284 1.14 rmind if (decision == NPF_DECISION_PASS && !error) {
285 1.3 rmind /*
286 1.3 rmind * XXX: Disable for now, it will be set accordingly later,
287 1.3 rmind * for optimisations (to reduce inspection).
288 1.3 rmind */
289 1.3 rmind (*mp)->m_flags &= ~M_CANFASTFWD;
290 1.13 rmind return 0;
291 1.1 rmind }
292 1.13 rmind
293 1.13 rmind /*
294 1.13 rmind * Block the packet. ENETUNREACH is used to indicate blocking.
295 1.13 rmind * Depending on the flags and protocol, return TCP reset (RST) or
296 1.13 rmind * ICMP destination unreachable.
297 1.13 rmind */
298 1.21.2.3 tls if (retfl && npf_return_block(&npc, retfl)) {
299 1.16 rmind *mp = NULL;
300 1.13 rmind }
301 1.16 rmind
302 1.20 rmind if (!error) {
303 1.14 rmind error = ENETUNREACH;
304 1.13 rmind }
305 1.13 rmind
306 1.16 rmind if (*mp) {
307 1.16 rmind m_freem(*mp);
308 1.16 rmind *mp = NULL;
309 1.16 rmind }
310 1.1 rmind return error;
311 1.1 rmind }
312 1.1 rmind
313 1.1 rmind /*
314 1.15 rmind * npf_pfil_register: register pfil(9) hooks.
315 1.1 rmind */
316 1.1 rmind int
317 1.21.2.3 tls npf_pfil_register(bool init)
318 1.1 rmind {
319 1.21.2.3 tls int error = 0;
320 1.1 rmind
321 1.1 rmind mutex_enter(softnet_lock);
322 1.1 rmind KERNEL_LOCK(1, NULL);
323 1.1 rmind
324 1.21.2.3 tls /* Init: interface re-config and attach/detach hook. */
325 1.21.2.3 tls if (!npf_ph_if) {
326 1.21.2.3 tls npf_ph_if = pfil_head_get(PFIL_TYPE_IFNET, 0);
327 1.21.2.3 tls if (!npf_ph_if) {
328 1.21.2.3 tls error = ENOENT;
329 1.21.2.3 tls goto out;
330 1.21.2.3 tls }
331 1.21.2.3 tls error = pfil_add_hook(npf_ifhook, NULL,
332 1.21.2.3 tls PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
333 1.21.2.3 tls KASSERT(error == 0);
334 1.21.2.3 tls }
335 1.21.2.3 tls if (init) {
336 1.21.2.3 tls goto out;
337 1.21.2.3 tls }
338 1.21.2.3 tls
339 1.1 rmind /* Check if pfil hooks are not already registered. */
340 1.21.2.3 tls if (pfil_registered) {
341 1.1 rmind error = EEXIST;
342 1.21.2.3 tls goto out;
343 1.1 rmind }
344 1.1 rmind
345 1.21.2.3 tls /* Capture points of the activity in the IP layer. */
346 1.21.2.3 tls npf_ph_inet = pfil_head_get(PFIL_TYPE_AF, (void *)AF_INET);
347 1.21.2.3 tls npf_ph_inet6 = pfil_head_get(PFIL_TYPE_AF, (void *)AF_INET6);
348 1.21.2.3 tls if (!npf_ph_inet && !npf_ph_inet6) {
349 1.1 rmind error = ENOENT;
350 1.21.2.3 tls goto out;
351 1.1 rmind }
352 1.1 rmind
353 1.21.2.3 tls /* Packet IN/OUT handlers for IP layer. */
354 1.16 rmind if (npf_ph_inet) {
355 1.16 rmind error = pfil_add_hook(npf_packet_handler, NULL,
356 1.21.2.3 tls PFIL_ALL, npf_ph_inet);
357 1.16 rmind KASSERT(error == 0);
358 1.16 rmind }
359 1.16 rmind if (npf_ph_inet6) {
360 1.16 rmind error = pfil_add_hook(npf_packet_handler, NULL,
361 1.21.2.3 tls PFIL_ALL, npf_ph_inet6);
362 1.16 rmind KASSERT(error == 0);
363 1.16 rmind }
364 1.21.2.3 tls pfil_registered = true;
365 1.21.2.3 tls out:
366 1.1 rmind KERNEL_UNLOCK_ONE(NULL);
367 1.1 rmind mutex_exit(softnet_lock);
368 1.1 rmind
369 1.1 rmind return error;
370 1.1 rmind }
371 1.1 rmind
372 1.1 rmind /*
373 1.15 rmind * npf_pfil_unregister: unregister pfil(9) hooks.
374 1.1 rmind */
375 1.1 rmind void
376 1.21.2.3 tls npf_pfil_unregister(bool fini)
377 1.1 rmind {
378 1.1 rmind mutex_enter(softnet_lock);
379 1.1 rmind KERNEL_LOCK(1, NULL);
380 1.1 rmind
381 1.21.2.3 tls if (fini && npf_ph_if) {
382 1.16 rmind (void)pfil_remove_hook(npf_ifhook, NULL,
383 1.16 rmind PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
384 1.16 rmind }
385 1.16 rmind if (npf_ph_inet) {
386 1.16 rmind (void)pfil_remove_hook(npf_packet_handler, NULL,
387 1.16 rmind PFIL_ALL, npf_ph_inet);
388 1.16 rmind }
389 1.16 rmind if (npf_ph_inet6) {
390 1.2 rmind (void)pfil_remove_hook(npf_packet_handler, NULL,
391 1.8 zoltan PFIL_ALL, npf_ph_inet6);
392 1.16 rmind }
393 1.21.2.3 tls pfil_registered = false;
394 1.1 rmind
395 1.1 rmind KERNEL_UNLOCK_ONE(NULL);
396 1.1 rmind mutex_exit(softnet_lock);
397 1.1 rmind }
398 1.15 rmind
399 1.15 rmind bool
400 1.15 rmind npf_pfil_registered_p(void)
401 1.15 rmind {
402 1.21.2.3 tls return pfil_registered;
403 1.15 rmind }
404