1 /* $NetBSD: npf_handler.c,v 1.25 2013/01/20 18:45:56 rmind Exp $ */ 2 3 /*- 4 * Copyright (c) 2009-2012 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.25 2013/01/20 18:45:56 rmind Exp $"); 38 39 #include <sys/types.h> 40 #include <sys/param.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 #include <netinet/ip6.h> 52 #include <netinet6/ip6_var.h> 53 54 #include "npf_impl.h" 55 56 /* 57 * If npf_ph_if != NULL, pfil hooks are registered. If NULL, not registered. 58 * Used to check the state. Locked by: softnet_lock + KERNEL_LOCK (XXX). 59 */ 60 static struct pfil_head * npf_ph_if = NULL; 61 static struct pfil_head * npf_ph_inet = NULL; 62 static struct pfil_head * npf_ph_inet6 = NULL; 63 64 /* 65 * npf_ifhook: hook handling interface changes. 66 */ 67 static int 68 npf_ifhook(void *arg, struct mbuf **mp, ifnet_t *ifp, int di) 69 { 70 71 return 0; 72 } 73 74 static int 75 npf_reassembly(npf_cache_t *npc, nbuf_t *nbuf, struct mbuf **mp) 76 { 77 int error = EINVAL; 78 79 /* Reset the mbuf as it may have changed. */ 80 *mp = nbuf_head_mbuf(nbuf); 81 nbuf_reset(nbuf); 82 83 if (npf_iscached(npc, NPC_IP4)) { 84 struct ip *ip = nbuf_dataptr(nbuf); 85 error = ip_reass_packet(mp, ip); 86 } else if (npf_iscached(npc, NPC_IP6)) { 87 #ifdef INET6 88 /* 89 * Note: ip6_reass_packet() offset is the start of 90 * the fragment header. 91 */ 92 const u_int hlen = npf_cache_hlen(npc); 93 error = ip6_reass_packet(mp, hlen); 94 if (error && *mp == NULL) { 95 memset(nbuf, 0, sizeof(nbuf_t)); 96 } 97 #endif 98 } 99 if (error) { 100 npf_stats_inc(NPF_STAT_REASSFAIL); 101 return error; 102 } 103 if (*mp == NULL) { 104 /* More fragments should come. */ 105 npf_stats_inc(NPF_STAT_FRAGMENTS); 106 return 0; 107 } 108 109 /* 110 * Reassembly is complete, we have the final packet. 111 * Cache again, since layer 4 data is accessible now. 112 */ 113 nbuf_init(nbuf, *mp, nbuf->nb_ifp); 114 npc->npc_info = 0; 115 116 if (npf_cache_all(npc, nbuf) & NPC_IPFRAG) { 117 return EINVAL; 118 } 119 npf_stats_inc(NPF_STAT_REASSEMBLY); 120 return 0; 121 } 122 123 /* 124 * npf_packet_handler: main packet handling routine for layer 3. 125 * 126 * Note: packet flow and inspection logic is in strict order. 127 */ 128 int 129 npf_packet_handler(void *arg, struct mbuf **mp, ifnet_t *ifp, int di) 130 { 131 nbuf_t nbuf; 132 npf_cache_t npc; 133 npf_session_t *se; 134 npf_ruleset_t *rlset; 135 npf_rule_t *rl; 136 npf_rproc_t *rp; 137 int error, retfl; 138 int decision; 139 140 /* 141 * Initialise packet information cache. 142 * Note: it is enough to clear the info bits. 143 */ 144 KASSERT(ifp != NULL); 145 nbuf_init(&nbuf, *mp, ifp); 146 npc.npc_info = 0; 147 decision = NPF_DECISION_BLOCK; 148 error = 0; 149 retfl = 0; 150 rp = NULL; 151 152 /* Cache everything. Determine whether it is an IP fragment. */ 153 if (npf_cache_all(&npc, &nbuf) & NPC_IPFRAG) { 154 /* 155 * Pass to IPv4 or IPv6 reassembly mechanism. 156 */ 157 error = npf_reassembly(&npc, &nbuf, mp); 158 if (error) { 159 se = NULL; 160 goto out; 161 } 162 if (*mp == NULL) { 163 /* More fragments should come; return. */ 164 return 0; 165 } 166 } 167 168 /* Inspect the list of sessions. */ 169 se = npf_session_inspect(&npc, &nbuf, di, &error); 170 171 /* If "passing" session found - skip the ruleset inspection. */ 172 if (se && npf_session_pass(se, &rp)) { 173 npf_stats_inc(NPF_STAT_PASS_SESSION); 174 KASSERT(error == 0); 175 goto pass; 176 } 177 if (error) { 178 if (error == ENETUNREACH) 179 goto block; 180 goto out; 181 } 182 183 /* Acquire the lock, inspect the ruleset using this packet. */ 184 npf_core_enter(); 185 rlset = npf_core_ruleset(); 186 rl = npf_ruleset_inspect(&npc, &nbuf, rlset, di, NPF_LAYER_3); 187 if (rl == NULL) { 188 bool default_pass = npf_default_pass(); 189 npf_core_exit(); 190 191 if (default_pass) { 192 npf_stats_inc(NPF_STAT_PASS_DEFAULT); 193 goto pass; 194 } 195 npf_stats_inc(NPF_STAT_BLOCK_DEFAULT); 196 goto block; 197 } 198 199 /* 200 * Get the rule procedure (acquires a reference) for association 201 * with a session (if any) and execution. 202 */ 203 KASSERT(rp == NULL); 204 rp = npf_rule_getrproc(rl); 205 206 /* Apply the rule, release the lock. */ 207 error = npf_rule_apply(&npc, &nbuf, rl, &retfl); 208 if (error) { 209 npf_stats_inc(NPF_STAT_BLOCK_RULESET); 210 goto block; 211 } 212 npf_stats_inc(NPF_STAT_PASS_RULESET); 213 214 /* 215 * Establish a "pass" session, if required. Just proceed, if session 216 * creation fails (e.g. due to unsupported protocol). 217 * 218 * Note: the reference on the rule procedure is transfered to the 219 * session. It will be released on session destruction. 220 */ 221 if ((retfl & NPF_RULE_STATEFUL) != 0 && !se) { 222 se = npf_session_establish(&npc, &nbuf, di); 223 if (se) { 224 npf_session_setpass(se, rp); 225 } 226 } 227 pass: 228 decision = NPF_DECISION_PASS; 229 KASSERT(error == 0); 230 /* 231 * Perform NAT. 232 */ 233 error = npf_do_nat(&npc, se, &nbuf, di); 234 block: 235 /* 236 * Execute the rule procedure, if any is associated. 237 * It may reverse the decision from pass to block. 238 */ 239 if (rp) { 240 npf_rproc_run(&npc, &nbuf, rp, &decision); 241 } 242 out: 243 /* 244 * Release the reference on a session. Release the reference on a 245 * rule procedure only if there was no association. 246 */ 247 if (se) { 248 npf_session_release(se); 249 } else if (rp) { 250 npf_rproc_release(rp); 251 } 252 253 /* Reset mbuf pointer before returning to the caller. */ 254 if ((*mp = nbuf_head_mbuf(&nbuf)) == NULL) { 255 return error ? error : ENOMEM; 256 } 257 258 /* Pass the packet if decided and there is no error. */ 259 if (decision == NPF_DECISION_PASS && !error) { 260 /* 261 * XXX: Disable for now, it will be set accordingly later, 262 * for optimisations (to reduce inspection). 263 */ 264 (*mp)->m_flags &= ~M_CANFASTFWD; 265 return 0; 266 } 267 268 /* 269 * Block the packet. ENETUNREACH is used to indicate blocking. 270 * Depending on the flags and protocol, return TCP reset (RST) or 271 * ICMP destination unreachable. 272 */ 273 if (retfl && npf_return_block(&npc, &nbuf, retfl)) { 274 *mp = NULL; 275 } 276 277 if (!error) { 278 error = ENETUNREACH; 279 } 280 281 if (*mp) { 282 m_freem(*mp); 283 *mp = NULL; 284 } 285 return error; 286 } 287 288 /* 289 * npf_pfil_register: register pfil(9) hooks. 290 */ 291 int 292 npf_pfil_register(void) 293 { 294 int error; 295 296 mutex_enter(softnet_lock); 297 KERNEL_LOCK(1, NULL); 298 299 /* Check if pfil hooks are not already registered. */ 300 if (npf_ph_if) { 301 error = EEXIST; 302 goto fail; 303 } 304 305 /* Capture point of any activity in interfaces and IP layer. */ 306 npf_ph_if = pfil_head_get(PFIL_TYPE_IFNET, 0); 307 npf_ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET); 308 npf_ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6); 309 if (!npf_ph_if || (!npf_ph_inet && !npf_ph_inet6)) { 310 npf_ph_if = NULL; 311 error = ENOENT; 312 goto fail; 313 } 314 315 /* Interface re-config or attach/detach hook. */ 316 error = pfil_add_hook(npf_ifhook, NULL, 317 PFIL_WAITOK | PFIL_IFADDR | PFIL_IFNET, npf_ph_if); 318 KASSERT(error == 0); 319 320 /* Packet IN/OUT handler on all interfaces and IP layer. */ 321 if (npf_ph_inet) { 322 error = pfil_add_hook(npf_packet_handler, NULL, 323 PFIL_WAITOK | PFIL_ALL, npf_ph_inet); 324 KASSERT(error == 0); 325 } 326 if (npf_ph_inet6) { 327 error = pfil_add_hook(npf_packet_handler, NULL, 328 PFIL_WAITOK | PFIL_ALL, npf_ph_inet6); 329 KASSERT(error == 0); 330 } 331 fail: 332 KERNEL_UNLOCK_ONE(NULL); 333 mutex_exit(softnet_lock); 334 335 return error; 336 } 337 338 /* 339 * npf_pfil_unregister: unregister pfil(9) hooks. 340 */ 341 void 342 npf_pfil_unregister(void) 343 { 344 345 mutex_enter(softnet_lock); 346 KERNEL_LOCK(1, NULL); 347 348 if (npf_ph_if) { 349 (void)pfil_remove_hook(npf_ifhook, NULL, 350 PFIL_IFADDR | PFIL_IFNET, npf_ph_if); 351 } 352 if (npf_ph_inet) { 353 (void)pfil_remove_hook(npf_packet_handler, NULL, 354 PFIL_ALL, npf_ph_inet); 355 } 356 if (npf_ph_inet6) { 357 (void)pfil_remove_hook(npf_packet_handler, NULL, 358 PFIL_ALL, npf_ph_inet6); 359 } 360 361 npf_ph_if = NULL; 362 363 KERNEL_UNLOCK_ONE(NULL); 364 mutex_exit(softnet_lock); 365 } 366 367 bool 368 npf_pfil_registered_p(void) 369 { 370 return npf_ph_if != NULL; 371 } 372