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