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