npf_handler.c revision 1.5 1 1.5 rmind /* $NetBSD: npf_handler.c,v 1.5 2010/12/18 01:07:25 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2009-2010 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.1 rmind */
35 1.1 rmind
36 1.1 rmind #include <sys/cdefs.h>
37 1.5 rmind __KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.5 2010/12/18 01:07:25 rmind Exp $");
38 1.1 rmind
39 1.1 rmind #include <sys/param.h>
40 1.1 rmind #include <sys/systm.h>
41 1.1 rmind
42 1.1 rmind #include <sys/mbuf.h>
43 1.1 rmind #include <sys/mutex.h>
44 1.1 rmind #include <net/if.h>
45 1.1 rmind #include <net/pfil.h>
46 1.1 rmind #include <sys/socketvar.h>
47 1.1 rmind
48 1.4 rmind #include <netinet/in_systm.h>
49 1.4 rmind #include <netinet/in.h>
50 1.4 rmind #include <netinet/ip_var.h>
51 1.4 rmind
52 1.1 rmind #include "npf_impl.h"
53 1.1 rmind
54 1.1 rmind /*
55 1.1 rmind * If npf_ph_if != NULL, pfil hooks are registers. If NULL, not registered.
56 1.1 rmind * Used to check the state. Locked by: softnet_lock + KERNEL_LOCK (XXX).
57 1.1 rmind */
58 1.1 rmind static struct pfil_head * npf_ph_if = NULL;
59 1.1 rmind static struct pfil_head * npf_ph_inet = NULL;
60 1.1 rmind
61 1.2 rmind static bool default_pass = true;
62 1.2 rmind
63 1.1 rmind int npf_packet_handler(void *, struct mbuf **, struct ifnet *, int);
64 1.1 rmind
65 1.1 rmind /*
66 1.1 rmind * npf_ifhook: hook handling interface changes.
67 1.1 rmind */
68 1.1 rmind static int
69 1.1 rmind npf_ifhook(void *arg, struct mbuf **mp, struct ifnet *ifp, int di)
70 1.1 rmind {
71 1.1 rmind
72 1.1 rmind return 0;
73 1.1 rmind }
74 1.1 rmind
75 1.1 rmind /*
76 1.2 rmind * npf_packet_handler: main packet handling routine for layer 3.
77 1.1 rmind *
78 1.1 rmind * Note: packet flow and inspection logic is in strict order.
79 1.1 rmind */
80 1.1 rmind int
81 1.1 rmind npf_packet_handler(void *arg, struct mbuf **mp, struct ifnet *ifp, int di)
82 1.1 rmind {
83 1.1 rmind nbuf_t *nbuf = *mp;
84 1.1 rmind npf_cache_t npc;
85 1.1 rmind npf_session_t *se;
86 1.1 rmind npf_rule_t *rl;
87 1.5 rmind npf_rproc_t *rp;
88 1.2 rmind int retfl, error;
89 1.1 rmind
90 1.1 rmind /*
91 1.1 rmind * Initialise packet information cache.
92 1.1 rmind * Note: it is enough to clear the info bits.
93 1.1 rmind */
94 1.1 rmind npc.npc_info = 0;
95 1.2 rmind error = 0;
96 1.2 rmind retfl = 0;
97 1.5 rmind rp = NULL;
98 1.1 rmind
99 1.4 rmind /* Cache everything. Determine whether it is an IPv4 fragment. */
100 1.4 rmind if (npf_cache_all(&npc, nbuf) && npf_iscached(&npc, NPC_IPFRAG)) {
101 1.4 rmind struct ip *ip = nbuf_dataptr(*mp);
102 1.4 rmind /*
103 1.4 rmind * Pass to IPv4 reassembly mechanism.
104 1.4 rmind */
105 1.4 rmind if (ip_reass_packet(mp, ip) != 0) {
106 1.4 rmind /* Failed; invalid fragment(s) or packet. */
107 1.4 rmind error = EINVAL;
108 1.4 rmind se = NULL;
109 1.4 rmind goto out;
110 1.4 rmind }
111 1.4 rmind if (*mp == NULL) {
112 1.4 rmind /* More fragments should come; return. */
113 1.4 rmind return 0;
114 1.4 rmind }
115 1.4 rmind /* Reassembly is complete, we have the final packet. */
116 1.4 rmind nbuf = (nbuf_t *)*mp;
117 1.4 rmind }
118 1.4 rmind
119 1.1 rmind /* Inspect the list of sessions. */
120 1.4 rmind se = npf_session_inspect(&npc, nbuf, di);
121 1.2 rmind
122 1.2 rmind /* If "passing" session found - skip the ruleset inspection. */
123 1.5 rmind if (se && npf_session_pass(se, &rp)) {
124 1.5 rmind npf_stats_inc(NPF_STAT_PASS_SESSION);
125 1.2 rmind goto pass;
126 1.2 rmind }
127 1.1 rmind
128 1.2 rmind /* Inspect the ruleset using this packet. */
129 1.2 rmind rl = npf_ruleset_inspect(&npc, nbuf, ifp, di, NPF_LAYER_3);
130 1.2 rmind if (rl == NULL) {
131 1.2 rmind if (default_pass) {
132 1.5 rmind npf_stats_inc(NPF_STAT_PASS_DEFAULT);
133 1.2 rmind goto pass;
134 1.2 rmind }
135 1.5 rmind npf_stats_inc(NPF_STAT_BLOCK_DEFAULT);
136 1.2 rmind error = ENETUNREACH;
137 1.1 rmind goto out;
138 1.1 rmind }
139 1.1 rmind
140 1.2 rmind /* Apply the rule. */
141 1.5 rmind error = npf_rule_apply(&npc, nbuf, rl, &retfl);
142 1.2 rmind if (error) {
143 1.5 rmind npf_stats_inc(NPF_STAT_BLOCK_RULESET);
144 1.2 rmind goto out;
145 1.1 rmind }
146 1.5 rmind npf_stats_inc(NPF_STAT_PASS_RULESET);
147 1.1 rmind
148 1.2 rmind /* Establish a "pass" session, if required. */
149 1.5 rmind if ((retfl & NPF_RULE_KEEPSTATE) != 0 && !se) {
150 1.5 rmind se = npf_session_establish(&npc, nbuf, di);
151 1.2 rmind if (se == NULL) {
152 1.2 rmind error = ENOMEM;
153 1.2 rmind goto out;
154 1.2 rmind }
155 1.5 rmind /* Associate rule processing data (XXX locking). */
156 1.5 rmind rp = npf_rproc_return(rl);
157 1.5 rmind npf_session_setpass(se, rp);
158 1.5 rmind } else {
159 1.5 rmind /* XXX: Return rule processing, needs locking. */
160 1.1 rmind }
161 1.2 rmind pass:
162 1.2 rmind KASSERT(error == 0);
163 1.5 rmind
164 1.5 rmind /*
165 1.5 rmind * Perform rule processing, if required.
166 1.5 rmind */
167 1.5 rmind if (rp) {
168 1.5 rmind npf_rproc_run(&npc, nbuf, rp);
169 1.5 rmind }
170 1.2 rmind /*
171 1.2 rmind * Perform NAT.
172 1.2 rmind */
173 1.2 rmind error = npf_do_nat(&npc, se, nbuf, ifp, di);
174 1.1 rmind out:
175 1.1 rmind /* Release reference on session. */
176 1.1 rmind if (se != NULL) {
177 1.1 rmind npf_session_release(se);
178 1.1 rmind }
179 1.1 rmind
180 1.1 rmind /*
181 1.1 rmind * If error is set - drop the packet.
182 1.2 rmind * Normally, ENETUNREACH is used for "block".
183 1.1 rmind */
184 1.1 rmind if (error) {
185 1.2 rmind /*
186 1.2 rmind * Depending on flags and protocol, return TCP reset (RST)
187 1.2 rmind * or ICMP destination unreachable
188 1.2 rmind */
189 1.2 rmind if (retfl) {
190 1.2 rmind npf_return_block(&npc, nbuf, retfl);
191 1.2 rmind }
192 1.1 rmind m_freem(*mp);
193 1.1 rmind *mp = NULL;
194 1.3 rmind } else {
195 1.3 rmind /*
196 1.3 rmind * XXX: Disable for now, it will be set accordingly later,
197 1.3 rmind * for optimisations (to reduce inspection).
198 1.3 rmind */
199 1.3 rmind (*mp)->m_flags &= ~M_CANFASTFWD;
200 1.1 rmind }
201 1.1 rmind return error;
202 1.1 rmind }
203 1.1 rmind
204 1.1 rmind /*
205 1.1 rmind * npf_register_pfil: register pfil(9) hooks.
206 1.1 rmind */
207 1.1 rmind int
208 1.1 rmind npf_register_pfil(void)
209 1.1 rmind {
210 1.1 rmind int error;
211 1.1 rmind
212 1.1 rmind mutex_enter(softnet_lock);
213 1.1 rmind KERNEL_LOCK(1, NULL);
214 1.1 rmind
215 1.1 rmind /* Check if pfil hooks are not already registered. */
216 1.1 rmind if (npf_ph_if) {
217 1.1 rmind error = EEXIST;
218 1.1 rmind goto fail;
219 1.1 rmind }
220 1.1 rmind
221 1.1 rmind /* Capture point of any activity in interfaces and IP layer. */
222 1.1 rmind npf_ph_if = pfil_head_get(PFIL_TYPE_IFNET, 0);
223 1.1 rmind npf_ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
224 1.1 rmind if (npf_ph_if == NULL || npf_ph_inet == NULL) {
225 1.1 rmind npf_ph_if = NULL;
226 1.1 rmind error = ENOENT;
227 1.1 rmind goto fail;
228 1.1 rmind }
229 1.1 rmind
230 1.1 rmind /* Interface re-config or attach/detach hook. */
231 1.1 rmind error = pfil_add_hook(npf_ifhook, NULL,
232 1.1 rmind PFIL_WAITOK | PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
233 1.1 rmind KASSERT(error == 0);
234 1.1 rmind
235 1.1 rmind /* Packet IN/OUT handler on all interfaces and IP layer. */
236 1.2 rmind error = pfil_add_hook(npf_packet_handler, NULL,
237 1.1 rmind PFIL_WAITOK | PFIL_ALL, npf_ph_inet);
238 1.1 rmind KASSERT(error == 0);
239 1.1 rmind
240 1.1 rmind fail:
241 1.1 rmind KERNEL_UNLOCK_ONE(NULL);
242 1.1 rmind mutex_exit(softnet_lock);
243 1.1 rmind
244 1.1 rmind return error;
245 1.1 rmind }
246 1.1 rmind
247 1.1 rmind /*
248 1.1 rmind * npf_unregister: unregister pfil(9) hooks.
249 1.1 rmind */
250 1.1 rmind void
251 1.1 rmind npf_unregister_pfil(void)
252 1.1 rmind {
253 1.1 rmind
254 1.1 rmind mutex_enter(softnet_lock);
255 1.1 rmind KERNEL_LOCK(1, NULL);
256 1.1 rmind
257 1.1 rmind if (npf_ph_if) {
258 1.2 rmind (void)pfil_remove_hook(npf_packet_handler, NULL,
259 1.1 rmind PFIL_ALL, npf_ph_inet);
260 1.1 rmind (void)pfil_remove_hook(npf_ifhook, NULL,
261 1.1 rmind PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
262 1.1 rmind
263 1.1 rmind npf_ph_if = NULL;
264 1.1 rmind }
265 1.1 rmind
266 1.1 rmind KERNEL_UNLOCK_ONE(NULL);
267 1.1 rmind mutex_exit(softnet_lock);
268 1.1 rmind }
269