npf_handler.c revision 1.42 1 /* $NetBSD: npf_handler.c,v 1.42 2018/07/10 15:25:01 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 2009-2013 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 * Note: pfil(9) hooks are currently locked by softnet_lock and kernel-lock.
36 */
37
38 #ifdef _KERNEL
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.42 2018/07/10 15:25:01 maxv Exp $");
41
42 #include <sys/types.h>
43 #include <sys/param.h>
44
45 #include <sys/mbuf.h>
46 #include <sys/mutex.h>
47 #include <net/if.h>
48 #include <net/pfil.h>
49 #include <sys/socketvar.h>
50
51 #include <netinet/in_systm.h>
52 #include <netinet/in.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/ip6.h>
55 #include <netinet6/ip6_var.h>
56 #endif
57
58 #include "npf_impl.h"
59 #include "npf_conn.h"
60
61 #if defined(_NPF_STANDALONE)
62 #define m_freem(m) npf->mbufops->free(m)
63 #define m_clear_flag(m,f)
64 #else
65 #define m_clear_flag(m,f) (m)->m_flags &= ~(f)
66 #endif
67
68 #ifndef INET6
69 #define ip6_reass_packet(x, y) ENOTSUP
70 #endif
71
72 static int
73 npf_reassembly(npf_t *npf, npf_cache_t *npc, bool *mff)
74 {
75 nbuf_t *nbuf = npc->npc_nbuf;
76 int error = EINVAL;
77 struct mbuf *m;
78
79 *mff = false;
80 m = nbuf_head_mbuf(nbuf);
81
82 /* Reset the mbuf as it may have changed. */
83 nbuf_reset(nbuf);
84
85 if (npf_iscached(npc, NPC_IP4)) {
86 struct ip *ip = nbuf_dataptr(nbuf);
87 error = ip_reass_packet(&m, ip);
88 KASSERT(!error || (m != NULL));
89 } else if (npf_iscached(npc, NPC_IP6)) {
90 error = ip6_reass_packet(&m, npc->npc_hlen);
91 if (error && m == NULL) {
92 memset(nbuf, 0, sizeof(nbuf_t));
93 }
94 }
95 if (error) {
96 npf_stats_inc(npf, NPF_STAT_REASSFAIL);
97 return error;
98 }
99 if (m == NULL) {
100 /* More fragments should come. */
101 npf_stats_inc(npf, NPF_STAT_FRAGMENTS);
102 *mff = true;
103 return 0;
104 }
105
106 /*
107 * Reassembly is complete, we have the final packet.
108 * Cache again, since layer 4 data is accessible now.
109 */
110 nbuf_init(npf, nbuf, m, nbuf->nb_ifp);
111 npc->npc_info = 0;
112
113 if (npf_cache_all(npc) & (NPC_IPFRAG|NPC_FMTERR)) {
114 return EINVAL;
115 }
116 npf_stats_inc(npf, NPF_STAT_REASSEMBLY);
117 return 0;
118 }
119
120 /*
121 * npf_packet_handler: main packet handling routine for layer 3.
122 *
123 * Note: packet flow and inspection logic is in strict order.
124 */
125 __dso_public int
126 npf_packet_handler(npf_t *npf, struct mbuf **mp, ifnet_t *ifp, int di)
127 {
128 nbuf_t nbuf;
129 npf_cache_t npc;
130 npf_conn_t *con;
131 npf_rule_t *rl;
132 npf_rproc_t *rp;
133 int error, decision, flags;
134 uint32_t ntag;
135 npf_match_info_t mi;
136 bool mff;
137
138 /* QSBR checkpoint. */
139 pserialize_checkpoint(npf->qsbr);
140 KASSERT(ifp != NULL);
141
142 /*
143 * Initialise packet information cache.
144 * Note: it is enough to clear the info bits.
145 */
146 npc.npc_ctx = npf;
147 nbuf_init(npf, &nbuf, *mp, ifp);
148 npc.npc_nbuf = &nbuf;
149 npc.npc_info = 0;
150
151 mi.mi_di = di;
152 mi.mi_rid = 0;
153 mi.mi_retfl = 0;
154
155 *mp = NULL;
156 decision = NPF_DECISION_BLOCK;
157 error = 0;
158 rp = NULL;
159 con = NULL;
160
161 /* Cache everything. */
162 flags = npf_cache_all(&npc);
163
164 /* If error on the format, leave quickly. */
165 if (flags & NPC_FMTERR) {
166 error = EINVAL;
167 goto out;
168 }
169
170 /* Determine whether it is an IP fragment. */
171 if (__predict_false(flags & NPC_IPFRAG)) {
172 /* Pass to IPv4/IPv6 reassembly mechanism. */
173 error = npf_reassembly(npf, &npc, &mff);
174 if (error) {
175 goto out;
176 }
177 if (mff) {
178 /* More fragments should come. */
179 return 0;
180 }
181 }
182
183 /* Just pass-through if specially tagged. */
184 if (nbuf_find_tag(&nbuf, &ntag) == 0 && (ntag & NPF_NTAG_PASS) != 0) {
185 goto pass;
186 }
187
188 /* Inspect the list of connections (if found, acquires a reference). */
189 con = npf_conn_inspect(&npc, di, &error);
190
191 /* If "passing" connection found - skip the ruleset inspection. */
192 if (con && npf_conn_pass(con, &mi, &rp)) {
193 npf_stats_inc(npf, NPF_STAT_PASS_CONN);
194 KASSERT(error == 0);
195 goto pass;
196 }
197 if (__predict_false(error)) {
198 if (error == ENETUNREACH)
199 goto block;
200 goto out;
201 }
202
203 /* Acquire the lock, inspect the ruleset using this packet. */
204 int slock = npf_config_read_enter();
205 npf_ruleset_t *rlset = npf_config_ruleset(npf);
206
207 rl = npf_ruleset_inspect(&npc, rlset, di, NPF_LAYER_3);
208 if (__predict_false(rl == NULL)) {
209 const bool pass = npf_default_pass(npf);
210 npf_config_read_exit(slock);
211
212 if (pass) {
213 npf_stats_inc(npf, NPF_STAT_PASS_DEFAULT);
214 goto pass;
215 }
216 npf_stats_inc(npf, NPF_STAT_BLOCK_DEFAULT);
217 goto block;
218 }
219
220 /*
221 * Get the rule procedure (acquires a reference) for association
222 * with a connection (if any) and execution.
223 */
224 KASSERT(rp == NULL);
225 rp = npf_rule_getrproc(rl);
226
227 /* Conclude with the rule and release the lock. */
228 error = npf_rule_conclude(rl, &mi);
229 npf_config_read_exit(slock);
230
231 if (error) {
232 npf_stats_inc(npf, NPF_STAT_BLOCK_RULESET);
233 goto block;
234 }
235 npf_stats_inc(npf, NPF_STAT_PASS_RULESET);
236
237 /*
238 * Establish a "pass" connection, if required. Just proceed if
239 * connection creation fails (e.g. due to unsupported protocol).
240 */
241 if ((mi.mi_retfl & NPF_RULE_STATEFUL) != 0 && !con) {
242 con = npf_conn_establish(&npc, di,
243 (mi.mi_retfl & NPF_RULE_MULTIENDS) == 0);
244 if (con) {
245 /*
246 * Note: the reference on the rule procedure is
247 * transfered to the connection. It will be
248 * released on connection destruction.
249 */
250 npf_conn_setpass(con, &mi, rp);
251 }
252 }
253
254 pass:
255 decision = NPF_DECISION_PASS;
256 KASSERT(error == 0);
257 /*
258 * Perform NAT.
259 */
260 error = npf_do_nat(&npc, con, di);
261
262 block:
263 /*
264 * Execute the rule procedure, if any is associated.
265 * It may reverse the decision from pass to block.
266 */
267 if (rp && !npf_rproc_run(&npc, rp, &mi, &decision)) {
268 if (con) {
269 npf_conn_release(con);
270 }
271 npf_rproc_release(rp);
272 /* mbuf already freed */
273 return 0;
274 }
275
276 out:
277 /*
278 * Release the reference on a connection. Release the reference
279 * on a rule procedure only if there was no association.
280 */
281 if (con) {
282 npf_conn_release(con);
283 } else if (rp) {
284 npf_rproc_release(rp);
285 }
286
287 /* Get the new mbuf pointer. */
288 if ((*mp = nbuf_head_mbuf(&nbuf)) == NULL) {
289 return error ? error : ENOMEM;
290 }
291
292 /* Pass the packet if decided and there is no error. */
293 if (decision == NPF_DECISION_PASS && !error) {
294 /*
295 * XXX: Disable for now, it will be set accordingly later,
296 * for optimisations (to reduce inspection).
297 */
298 m_clear_flag(*mp, M_CANFASTFWD);
299 return 0;
300 }
301
302 /*
303 * Block the packet. ENETUNREACH is used to indicate blocking.
304 * Depending on the flags and protocol, return TCP reset (RST) or
305 * ICMP destination unreachable.
306 */
307 if (mi.mi_retfl && npf_return_block(&npc, mi.mi_retfl)) {
308 *mp = NULL;
309 }
310
311 if (!error) {
312 error = ENETUNREACH;
313 }
314
315 if (*mp) {
316 /* Free the mbuf chain. */
317 m_freem(*mp);
318 *mp = NULL;
319 }
320 return error;
321 }
322