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