Home | History | Annotate | Line # | Download | only in npf
npf_handler.c revision 1.5
      1 /*	$NetBSD: npf_handler.c,v 1.5 2010/12/18 01:07:25 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.5 2010/12/18 01:07:25 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 	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. */
    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 out;
    138 	}
    139 
    140 	/* Apply the rule. */
    141 	error = npf_rule_apply(&npc, nbuf, rl, &retfl);
    142 	if (error) {
    143 		npf_stats_inc(NPF_STAT_BLOCK_RULESET);
    144 		goto out;
    145 	}
    146 	npf_stats_inc(NPF_STAT_PASS_RULESET);
    147 
    148 	/* Establish a "pass" session, if required. */
    149 	if ((retfl & NPF_RULE_KEEPSTATE) != 0 && !se) {
    150 		se = npf_session_establish(&npc, nbuf, di);
    151 		if (se == NULL) {
    152 			error = ENOMEM;
    153 			goto out;
    154 		}
    155 		/* Associate rule processing data (XXX locking). */
    156 		rp = npf_rproc_return(rl);
    157 		npf_session_setpass(se, rp);
    158 	} else {
    159 		/* XXX: Return rule processing, needs locking. */
    160 	}
    161 pass:
    162 	KASSERT(error == 0);
    163 
    164 	/*
    165 	 * Perform rule processing, if required.
    166 	 */
    167 	if (rp) {
    168 		npf_rproc_run(&npc, nbuf, rp);
    169 	}
    170 	/*
    171 	 * Perform NAT.
    172 	 */
    173 	error = npf_do_nat(&npc, se, nbuf, ifp, di);
    174 out:
    175 	/* Release reference on session. */
    176 	if (se != NULL) {
    177 		npf_session_release(se);
    178 	}
    179 
    180 	/*
    181 	 * If error is set - drop the packet.
    182 	 * Normally, ENETUNREACH is used for "block".
    183 	 */
    184 	if (error) {
    185 		/*
    186 		 * Depending on flags and protocol, return TCP reset (RST)
    187 		 * or ICMP destination unreachable
    188 		 */
    189 		if (retfl) {
    190 			npf_return_block(&npc, nbuf, retfl);
    191 		}
    192 		m_freem(*mp);
    193 		*mp = NULL;
    194 	} else {
    195 		/*
    196 		 * XXX: Disable for now, it will be set accordingly later,
    197 		 * for optimisations (to reduce inspection).
    198 		 */
    199 		(*mp)->m_flags &= ~M_CANFASTFWD;
    200 	}
    201 	return error;
    202 }
    203 
    204 /*
    205  * npf_register_pfil: register pfil(9) hooks.
    206  */
    207 int
    208 npf_register_pfil(void)
    209 {
    210 	int error;
    211 
    212 	mutex_enter(softnet_lock);
    213 	KERNEL_LOCK(1, NULL);
    214 
    215 	/* Check if pfil hooks are not already registered. */
    216 	if (npf_ph_if) {
    217 		error = EEXIST;
    218 		goto fail;
    219 	}
    220 
    221 	/* Capture point of any activity in interfaces and IP layer. */
    222 	npf_ph_if = pfil_head_get(PFIL_TYPE_IFNET, 0);
    223 	npf_ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
    224 	if (npf_ph_if == NULL || npf_ph_inet == NULL) {
    225 		npf_ph_if = NULL;
    226 		error = ENOENT;
    227 		goto fail;
    228 	}
    229 
    230 	/* Interface re-config or attach/detach hook. */
    231 	error = pfil_add_hook(npf_ifhook, NULL,
    232 	    PFIL_WAITOK | PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
    233 	KASSERT(error == 0);
    234 
    235 	/* Packet IN/OUT handler on all interfaces and IP layer. */
    236 	error = pfil_add_hook(npf_packet_handler, NULL,
    237 	    PFIL_WAITOK | PFIL_ALL, npf_ph_inet);
    238 	KASSERT(error == 0);
    239 
    240 fail:
    241 	KERNEL_UNLOCK_ONE(NULL);
    242 	mutex_exit(softnet_lock);
    243 
    244 	return error;
    245 }
    246 
    247 /*
    248  * npf_unregister: unregister pfil(9) hooks.
    249  */
    250 void
    251 npf_unregister_pfil(void)
    252 {
    253 
    254 	mutex_enter(softnet_lock);
    255 	KERNEL_LOCK(1, NULL);
    256 
    257 	if (npf_ph_if) {
    258 		(void)pfil_remove_hook(npf_packet_handler, NULL,
    259 		    PFIL_ALL, npf_ph_inet);
    260 		(void)pfil_remove_hook(npf_ifhook, NULL,
    261 		    PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
    262 
    263 		npf_ph_if = NULL;
    264 	}
    265 
    266 	KERNEL_UNLOCK_ONE(NULL);
    267 	mutex_exit(softnet_lock);
    268 }
    269