1 /* $NetBSD: npf_handler.c,v 1.6 2011/01/18 20:33:45 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.6 2011/01/18 20:33:45 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 **, ifnet_t *, int); 64 65 /* 66 * npf_ifhook: hook handling interface changes. 67 */ 68 static int 69 npf_ifhook(void *arg, struct mbuf **mp, ifnet_t *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, ifnet_t *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 npf_rproc_t *rp; 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 rp = NULL; 98 99 /* Cache everything. Determine whether it is an IPv4 fragment. */ 100 if (npf_cache_all(&npc, nbuf) && npf_iscached(&npc, NPC_IPFRAG)) { 101 struct ip *ip = nbuf_dataptr(*mp); 102 /* 103 * Pass to IPv4 reassembly mechanism. 104 */ 105 if (ip_reass_packet(mp, ip) != 0) { 106 /* Failed; invalid fragment(s) or packet. */ 107 error = EINVAL; 108 se = NULL; 109 goto out; 110 } 111 if (*mp == NULL) { 112 /* More fragments should come; return. */ 113 return 0; 114 } 115 /* Reassembly is complete, we have the final packet. */ 116 nbuf = (nbuf_t *)*mp; 117 } 118 119 /* Inspect the list of sessions. */ 120 se = npf_session_inspect(&npc, nbuf, di); 121 122 /* If "passing" session found - skip the ruleset inspection. */ 123 if (se && npf_session_pass(se, &rp)) { 124 npf_stats_inc(NPF_STAT_PASS_SESSION); 125 goto pass; 126 } 127 128 /* Inspect the ruleset using this packet, acquire the lock. */ 129 rl = npf_ruleset_inspect(&npc, nbuf, ifp, di, NPF_LAYER_3); 130 if (rl == NULL) { 131 if (default_pass) { 132 npf_stats_inc(NPF_STAT_PASS_DEFAULT); 133 goto pass; 134 } 135 npf_stats_inc(NPF_STAT_BLOCK_DEFAULT); 136 error = ENETUNREACH; 137 goto block; 138 } 139 140 /* Get rule procedure for assocation and/or execution. */ 141 KASSERT(rp == NULL); 142 rp = npf_rproc_return(rl); 143 144 /* Apply the rule, release the lock. */ 145 error = npf_rule_apply(&npc, nbuf, rl, &retfl); 146 if (error) { 147 npf_stats_inc(NPF_STAT_BLOCK_RULESET); 148 goto block; 149 } 150 npf_stats_inc(NPF_STAT_PASS_RULESET); 151 152 /* Establish a "pass" session, if required. */ 153 if ((retfl & NPF_RULE_KEEPSTATE) != 0 && !se) { 154 se = npf_session_establish(&npc, nbuf, di); 155 if (se == NULL) { 156 error = ENOMEM; 157 goto out; 158 } 159 npf_session_setpass(se, rp); 160 } 161 pass: 162 KASSERT(error == 0); 163 /* 164 * Perform NAT. 165 */ 166 error = npf_do_nat(&npc, se, nbuf, ifp, di); 167 block: 168 /* 169 * Perform rule procedure, if any. 170 */ 171 if (rp) { 172 npf_rproc_run(&npc, nbuf, rp); 173 } 174 out: 175 /* Release the reference on session, or rule procedure. */ 176 if (se) { 177 npf_session_release(se); 178 } else if (rp) { 179 npf_rproc_release(rp); /* XXXkmem */ 180 } 181 182 /* 183 * If error is set - drop the packet. 184 * Normally, ENETUNREACH is used for "block". 185 */ 186 if (error) { 187 /* 188 * Depending on flags and protocol, return TCP reset (RST) 189 * or ICMP destination unreachable 190 */ 191 if (retfl) { 192 npf_return_block(&npc, nbuf, retfl); 193 } 194 if (error != ENETUNREACH) { 195 NPF_PRINTF(("NPF: error in handler '%d'\n", error)); 196 npf_stats_inc(NPF_STAT_ERROR); 197 } 198 m_freem(*mp); 199 *mp = NULL; 200 } else { 201 /* 202 * XXX: Disable for now, it will be set accordingly later, 203 * for optimisations (to reduce inspection). 204 */ 205 (*mp)->m_flags &= ~M_CANFASTFWD; 206 } 207 return error; 208 } 209 210 /* 211 * npf_register_pfil: register pfil(9) hooks. 212 */ 213 int 214 npf_register_pfil(void) 215 { 216 int error; 217 218 mutex_enter(softnet_lock); 219 KERNEL_LOCK(1, NULL); 220 221 /* Check if pfil hooks are not already registered. */ 222 if (npf_ph_if) { 223 error = EEXIST; 224 goto fail; 225 } 226 227 /* Capture point of any activity in interfaces and IP layer. */ 228 npf_ph_if = pfil_head_get(PFIL_TYPE_IFNET, 0); 229 npf_ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET); 230 if (npf_ph_if == NULL || npf_ph_inet == NULL) { 231 npf_ph_if = NULL; 232 error = ENOENT; 233 goto fail; 234 } 235 236 /* Interface re-config or attach/detach hook. */ 237 error = pfil_add_hook(npf_ifhook, NULL, 238 PFIL_WAITOK | PFIL_IFADDR | PFIL_IFNET, npf_ph_if); 239 KASSERT(error == 0); 240 241 /* Packet IN/OUT handler on all interfaces and IP layer. */ 242 error = pfil_add_hook(npf_packet_handler, NULL, 243 PFIL_WAITOK | PFIL_ALL, npf_ph_inet); 244 KASSERT(error == 0); 245 246 fail: 247 KERNEL_UNLOCK_ONE(NULL); 248 mutex_exit(softnet_lock); 249 250 return error; 251 } 252 253 /* 254 * npf_unregister: unregister pfil(9) hooks. 255 */ 256 void 257 npf_unregister_pfil(void) 258 { 259 260 mutex_enter(softnet_lock); 261 KERNEL_LOCK(1, NULL); 262 263 if (npf_ph_if) { 264 (void)pfil_remove_hook(npf_packet_handler, NULL, 265 PFIL_ALL, npf_ph_inet); 266 (void)pfil_remove_hook(npf_ifhook, NULL, 267 PFIL_IFADDR | PFIL_IFNET, npf_ph_if); 268 269 npf_ph_if = NULL; 270 } 271 272 KERNEL_UNLOCK_ONE(NULL); 273 mutex_exit(softnet_lock); 274 } 275