Home | History | Annotate | Line # | Download | only in npf
npf_handler.c revision 1.10
      1 /*	$NetBSD: npf_handler.c,v 1.10 2011/11/06 02:49:03 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.10 2011/11/06 02:49:03 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 #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 registers.  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 static bool			default_pass = true;
     65 
     66 int	npf_packet_handler(void *, struct mbuf **, ifnet_t *, int);
     67 
     68 /*
     69  * npf_ifhook: hook handling interface changes.
     70  */
     71 static int
     72 npf_ifhook(void *arg, struct mbuf **mp, ifnet_t *ifp, int di)
     73 {
     74 
     75 	return 0;
     76 }
     77 
     78 /*
     79  * npf_packet_handler: main packet handling routine for layer 3.
     80  *
     81  * Note: packet flow and inspection logic is in strict order.
     82  */
     83 int
     84 npf_packet_handler(void *arg, struct mbuf **mp, ifnet_t *ifp, int di)
     85 {
     86 	nbuf_t *nbuf = *mp;
     87 	npf_cache_t npc;
     88 	npf_session_t *se;
     89 	npf_ruleset_t *rlset;
     90 	npf_rule_t *rl;
     91 	npf_rproc_t *rp;
     92 	int retfl, error, ret;
     93 
     94 	/*
     95 	 * Initialise packet information cache.
     96 	 * Note: it is enough to clear the info bits.
     97 	 */
     98 	npc.npc_info = 0;
     99 	error = 0;
    100 	retfl = 0;
    101 	rp = NULL;
    102 	ret = 0;
    103 
    104 	/* Cache everything.  Determine whether it is an IP fragment. */
    105 	npf_cache_all(&npc, nbuf);
    106 
    107 	if (npf_iscached(&npc, NPC_IPFRAG)) {
    108 		/* Pass to IPv4 or IPv6 reassembly mechanism. */
    109 		if (npf_iscached(&npc, NPC_IP4)) {
    110 			struct ip *ip = nbuf_dataptr(*mp);
    111 			ret = ip_reass_packet(mp, ip);
    112 		} else {
    113 			KASSERT(npf_iscached(&npc, NPC_IP6));
    114 #ifdef INET6
    115 			/*
    116 			 * Note: frag6_input() offset is the start of the
    117 			 * fragment header.
    118 			 */
    119 			size_t hlen = npf_cache_hlen(&npc, nbuf);
    120 			ret = ip6_reass_packet(mp, hlen);
    121 #else
    122 			ret = -1;
    123 #endif
    124 		}
    125 
    126 		if (ret) {
    127 			error = EINVAL;
    128 			se = NULL;
    129 			goto out;
    130 		}
    131 		if (*mp == NULL) {
    132 			/* More fragments should come; return. */
    133 			return 0;
    134 		}
    135 
    136 		/*
    137 		 * Reassembly is complete, we have the final packet.
    138 		 * Cache again, since layer 3 daya is accessible now.
    139 		 */
    140 		nbuf = (nbuf_t *)*mp;
    141 		npc.npc_info = 0;
    142 		npf_cache_all(&npc, nbuf);
    143 	}
    144 
    145 	/* Inspect the list of sessions. */
    146 	se = npf_session_inspect(&npc, nbuf, di);
    147 
    148 	/* If "passing" session found - skip the ruleset inspection. */
    149 	if (se && npf_session_pass(se, &rp)) {
    150 		npf_stats_inc(NPF_STAT_PASS_SESSION);
    151 		goto pass;
    152 	}
    153 
    154 	/* Acquire the lock, inspect the ruleset using this packet. */
    155 	npf_core_enter();
    156 	rlset = npf_core_ruleset();
    157 	rl = npf_ruleset_inspect(&npc, nbuf, rlset, ifp, di, NPF_LAYER_3);
    158 	if (rl == NULL) {
    159 		if (default_pass) {
    160 			npf_stats_inc(NPF_STAT_PASS_DEFAULT);
    161 			goto pass;
    162 		}
    163 		npf_stats_inc(NPF_STAT_BLOCK_DEFAULT);
    164 		error = ENETUNREACH;
    165 		goto block;
    166 	}
    167 
    168 	/* Get rule procedure for assocation and/or execution. */
    169 	KASSERT(rp == NULL);
    170 	rp = npf_rproc_return(rl);
    171 
    172 	/* Apply the rule, release the lock. */
    173 	error = npf_rule_apply(&npc, nbuf, rl, &retfl);
    174 	if (error) {
    175 		npf_stats_inc(NPF_STAT_BLOCK_RULESET);
    176 		goto block;
    177 	}
    178 	npf_stats_inc(NPF_STAT_PASS_RULESET);
    179 
    180 	/* Establish a "pass" session, if required. */
    181 	if ((retfl & NPF_RULE_KEEPSTATE) != 0 && !se) {
    182 		se = npf_session_establish(&npc, nbuf, di);
    183 		if (se == NULL) {
    184 			error = ENOMEM;
    185 			goto out;
    186 		}
    187 		npf_session_setpass(se, rp);
    188 	}
    189 pass:
    190 	KASSERT(error == 0);
    191 	/*
    192 	 * Perform NAT.
    193 	 */
    194 	error = npf_do_nat(&npc, se, nbuf, ifp, di);
    195 block:
    196 	/*
    197 	 * Perform rule procedure, if any.
    198 	 */
    199 	if (rp) {
    200 		npf_rproc_run(&npc, nbuf, rp, error);
    201 	}
    202 out:
    203 	/* Release the reference on session, or rule procedure. */
    204 	if (se) {
    205 		npf_session_release(se);
    206 	} else if (rp) {
    207 		npf_rproc_release(rp); /* XXXkmem */
    208 	}
    209 
    210 	/*
    211 	 * If error is set - drop the packet.
    212 	 * Normally, ENETUNREACH is used for "block".
    213 	 */
    214 	if (error) {
    215 		/*
    216 		 * Depending on flags and protocol, return TCP reset (RST)
    217 		 * or ICMP destination unreachable
    218 		 */
    219 		if (retfl) {
    220 			npf_return_block(&npc, nbuf, retfl);
    221 		}
    222 		if (error != ENETUNREACH) {
    223 			NPF_PRINTF(("NPF: error in handler '%d'\n", error));
    224 			npf_stats_inc(NPF_STAT_ERROR);
    225 		}
    226 		m_freem(*mp);
    227 		*mp = NULL;
    228 	} else {
    229 		/*
    230 		 * XXX: Disable for now, it will be set accordingly later,
    231 		 * for optimisations (to reduce inspection).
    232 		 */
    233 		(*mp)->m_flags &= ~M_CANFASTFWD;
    234 	}
    235 	return error;
    236 }
    237 
    238 /*
    239  * npf_register_pfil: register pfil(9) hooks.
    240  */
    241 int
    242 npf_register_pfil(void)
    243 {
    244 	int error;
    245 
    246 	mutex_enter(softnet_lock);
    247 	KERNEL_LOCK(1, NULL);
    248 
    249 	/* Check if pfil hooks are not already registered. */
    250 	if (npf_ph_if) {
    251 		error = EEXIST;
    252 		goto fail;
    253 	}
    254 
    255 	/* Capture point of any activity in interfaces and IP layer. */
    256 	npf_ph_if = pfil_head_get(PFIL_TYPE_IFNET, 0);
    257 	npf_ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
    258 	npf_ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
    259 	if (npf_ph_if == NULL || npf_ph_inet == NULL) {
    260 		npf_ph_if = NULL;
    261 		error = ENOENT;
    262 		goto fail;
    263 	}
    264 
    265 	/* Interface re-config or attach/detach hook. */
    266 	error = pfil_add_hook(npf_ifhook, NULL,
    267 	    PFIL_WAITOK | PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
    268 	KASSERT(error == 0);
    269 
    270 	/* Packet IN/OUT handler on all interfaces and IP layer. */
    271 	error = pfil_add_hook(npf_packet_handler, NULL,
    272 	    PFIL_WAITOK | PFIL_ALL, npf_ph_inet);
    273 	KASSERT(error == 0);
    274 
    275 	error = pfil_add_hook(npf_packet_handler, NULL,
    276 	    PFIL_WAITOK | PFIL_ALL, npf_ph_inet6);
    277 	KASSERT(error == 0);
    278 fail:
    279 	KERNEL_UNLOCK_ONE(NULL);
    280 	mutex_exit(softnet_lock);
    281 
    282 	return error;
    283 }
    284 
    285 /*
    286  * npf_unregister: unregister pfil(9) hooks.
    287  */
    288 void
    289 npf_unregister_pfil(void)
    290 {
    291 
    292 	mutex_enter(softnet_lock);
    293 	KERNEL_LOCK(1, NULL);
    294 
    295 	if (npf_ph_if) {
    296 		(void)pfil_remove_hook(npf_packet_handler, NULL,
    297 		    PFIL_ALL, npf_ph_inet6);
    298 		(void)pfil_remove_hook(npf_packet_handler, NULL,
    299 		    PFIL_ALL, npf_ph_inet);
    300 		(void)pfil_remove_hook(npf_ifhook, NULL,
    301 		    PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
    302 
    303 		npf_ph_if = NULL;
    304 	}
    305 
    306 	KERNEL_UNLOCK_ONE(NULL);
    307 	mutex_exit(softnet_lock);
    308 }
    309