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