Home | History | Annotate | Line # | Download | only in npf
npf_handler.c revision 1.11.2.4
      1  1.11.2.2     mrg /*	$NetBSD: npf_handler.c,v 1.11.2.4 2012/06/02 11:09:38 mrg Exp $	*/
      2       1.1   rmind 
      3       1.1   rmind /*-
      4  1.11.2.2     mrg  * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
      5       1.1   rmind  * All rights reserved.
      6       1.1   rmind  *
      7       1.1   rmind  * This material is based upon work partially supported by The
      8       1.1   rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9       1.1   rmind  *
     10       1.1   rmind  * Redistribution and use in source and binary forms, with or without
     11       1.1   rmind  * modification, are permitted provided that the following conditions
     12       1.1   rmind  * are met:
     13       1.1   rmind  * 1. Redistributions of source code must retain the above copyright
     14       1.1   rmind  *    notice, this list of conditions and the following disclaimer.
     15       1.1   rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   rmind  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   rmind  *    documentation and/or other materials provided with the distribution.
     18       1.1   rmind  *
     19       1.1   rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1   rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1   rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1   rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1   rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1   rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1   rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1   rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1   rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1   rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1   rmind  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1   rmind  */
     31       1.1   rmind 
     32       1.1   rmind /*
     33       1.1   rmind  * NPF packet handler.
     34       1.1   rmind  */
     35       1.1   rmind 
     36       1.1   rmind #include <sys/cdefs.h>
     37  1.11.2.2     mrg __KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.11.2.4 2012/06/02 11:09:38 mrg Exp $");
     38       1.1   rmind 
     39  1.11.2.2     mrg #include <sys/types.h>
     40       1.1   rmind #include <sys/param.h>
     41       1.1   rmind 
     42       1.1   rmind #include <sys/mbuf.h>
     43       1.1   rmind #include <sys/mutex.h>
     44       1.1   rmind #include <net/if.h>
     45       1.1   rmind #include <net/pfil.h>
     46       1.1   rmind #include <sys/socketvar.h>
     47       1.1   rmind 
     48       1.4   rmind #include <netinet/in_systm.h>
     49       1.4   rmind #include <netinet/in.h>
     50       1.4   rmind #include <netinet/ip_var.h>
     51       1.8  zoltan #include <netinet/ip6.h>
     52       1.8  zoltan #include <netinet6/ip6_var.h>
     53       1.4   rmind 
     54       1.1   rmind #include "npf_impl.h"
     55       1.1   rmind 
     56       1.1   rmind /*
     57       1.1   rmind  * If npf_ph_if != NULL, pfil hooks are registers.  If NULL, not registered.
     58       1.1   rmind  * Used to check the state.  Locked by: softnet_lock + KERNEL_LOCK (XXX).
     59       1.1   rmind  */
     60       1.1   rmind static struct pfil_head *	npf_ph_if = NULL;
     61       1.1   rmind static struct pfil_head *	npf_ph_inet = NULL;
     62       1.8  zoltan static struct pfil_head *	npf_ph_inet6 = NULL;
     63       1.1   rmind 
     64       1.1   rmind /*
     65       1.1   rmind  * npf_ifhook: hook handling interface changes.
     66       1.1   rmind  */
     67       1.1   rmind static int
     68       1.6   rmind npf_ifhook(void *arg, struct mbuf **mp, ifnet_t *ifp, int di)
     69       1.1   rmind {
     70       1.1   rmind 
     71       1.1   rmind 	return 0;
     72       1.1   rmind }
     73       1.1   rmind 
     74       1.1   rmind /*
     75       1.2   rmind  * npf_packet_handler: main packet handling routine for layer 3.
     76       1.1   rmind  *
     77       1.1   rmind  * Note: packet flow and inspection logic is in strict order.
     78       1.1   rmind  */
     79       1.1   rmind int
     80       1.6   rmind npf_packet_handler(void *arg, struct mbuf **mp, ifnet_t *ifp, int di)
     81       1.1   rmind {
     82       1.1   rmind 	nbuf_t *nbuf = *mp;
     83       1.1   rmind 	npf_cache_t npc;
     84       1.1   rmind 	npf_session_t *se;
     85       1.7   rmind 	npf_ruleset_t *rlset;
     86       1.1   rmind 	npf_rule_t *rl;
     87       1.5   rmind 	npf_rproc_t *rp;
     88  1.11.2.2     mrg 	int error, retfl;
     89  1.11.2.2     mrg 	int decision;
     90       1.1   rmind 
     91       1.1   rmind 	/*
     92       1.1   rmind 	 * Initialise packet information cache.
     93       1.1   rmind 	 * Note: it is enough to clear the info bits.
     94       1.1   rmind 	 */
     95       1.1   rmind 	npc.npc_info = 0;
     96  1.11.2.2     mrg 	decision = NPF_DECISION_BLOCK;
     97       1.2   rmind 	error = 0;
     98       1.2   rmind 	retfl = 0;
     99       1.5   rmind 	rp = NULL;
    100       1.1   rmind 
    101      1.10   rmind 	/* Cache everything.  Determine whether it is an IP fragment. */
    102      1.11   rmind 	if (npf_cache_all(&npc, nbuf) & NPC_IPFRAG) {
    103  1.11.2.2     mrg 		int ret = -1;
    104  1.11.2.2     mrg 
    105      1.10   rmind 		/* Pass to IPv4 or IPv6 reassembly mechanism. */
    106       1.8  zoltan 		if (npf_iscached(&npc, NPC_IP4)) {
    107       1.8  zoltan 			struct ip *ip = nbuf_dataptr(*mp);
    108       1.8  zoltan 			ret = ip_reass_packet(mp, ip);
    109  1.11.2.2     mrg 		} else if (npf_iscached(&npc, NPC_IP6)) {
    110       1.9  zoltan #ifdef INET6
    111      1.10   rmind 			/*
    112      1.10   rmind 			 * Note: frag6_input() offset is the start of the
    113      1.10   rmind 			 * fragment header.
    114      1.10   rmind 			 */
    115      1.11   rmind 			const u_int hlen = npf_cache_hlen(&npc);
    116       1.8  zoltan 			ret = ip6_reass_packet(mp, hlen);
    117       1.9  zoltan #endif
    118       1.8  zoltan 		}
    119      1.10   rmind 		if (ret) {
    120       1.4   rmind 			error = EINVAL;
    121       1.4   rmind 			se = NULL;
    122       1.4   rmind 			goto out;
    123       1.4   rmind 		}
    124       1.4   rmind 		if (*mp == NULL) {
    125       1.4   rmind 			/* More fragments should come; return. */
    126       1.4   rmind 			return 0;
    127       1.4   rmind 		}
    128       1.8  zoltan 
    129       1.8  zoltan 		/*
    130      1.10   rmind 		 * Reassembly is complete, we have the final packet.
    131      1.11   rmind 		 * Cache again, since layer 4 data is accessible now.
    132       1.8  zoltan 		 */
    133      1.10   rmind 		nbuf = (nbuf_t *)*mp;
    134       1.8  zoltan 		npc.npc_info = 0;
    135  1.11.2.2     mrg 
    136  1.11.2.2     mrg 		ret = npf_cache_all(&npc, nbuf);
    137  1.11.2.2     mrg 		KASSERT((ret & NPC_IPFRAG) == 0);
    138       1.4   rmind 	}
    139       1.4   rmind 
    140       1.1   rmind 	/* Inspect the list of sessions. */
    141      1.11   rmind 	se = npf_session_inspect(&npc, nbuf, di, &error);
    142       1.2   rmind 
    143       1.2   rmind 	/* If "passing" session found - skip the ruleset inspection. */
    144       1.5   rmind 	if (se && npf_session_pass(se, &rp)) {
    145       1.5   rmind 		npf_stats_inc(NPF_STAT_PASS_SESSION);
    146  1.11.2.2     mrg 		KASSERT(error == 0);
    147       1.2   rmind 		goto pass;
    148  1.11.2.2     mrg 	}
    149  1.11.2.2     mrg 	if (error) {
    150      1.11   rmind 		goto block;
    151       1.2   rmind 	}
    152       1.1   rmind 
    153       1.7   rmind 	/* Acquire the lock, inspect the ruleset using this packet. */
    154       1.7   rmind 	npf_core_enter();
    155       1.7   rmind 	rlset = npf_core_ruleset();
    156       1.7   rmind 	rl = npf_ruleset_inspect(&npc, nbuf, rlset, ifp, di, NPF_LAYER_3);
    157       1.2   rmind 	if (rl == NULL) {
    158  1.11.2.2     mrg 		bool default_pass = npf_default_pass();
    159  1.11.2.1     mrg 		npf_core_exit();
    160  1.11.2.2     mrg 
    161       1.2   rmind 		if (default_pass) {
    162       1.5   rmind 			npf_stats_inc(NPF_STAT_PASS_DEFAULT);
    163       1.2   rmind 			goto pass;
    164       1.2   rmind 		}
    165       1.5   rmind 		npf_stats_inc(NPF_STAT_BLOCK_DEFAULT);
    166       1.6   rmind 		goto block;
    167       1.1   rmind 	}
    168       1.1   rmind 
    169  1.11.2.1     mrg 	/*
    170  1.11.2.1     mrg 	 * Get the rule procedure (acquires a reference) for assocation
    171  1.11.2.1     mrg 	 * with a session (if any) and execution.
    172  1.11.2.1     mrg 	 */
    173       1.6   rmind 	KASSERT(rp == NULL);
    174  1.11.2.1     mrg 	rp = npf_rule_getrproc(rl);
    175       1.6   rmind 
    176       1.6   rmind 	/* Apply the rule, release the lock. */
    177       1.5   rmind 	error = npf_rule_apply(&npc, nbuf, rl, &retfl);
    178       1.2   rmind 	if (error) {
    179       1.5   rmind 		npf_stats_inc(NPF_STAT_BLOCK_RULESET);
    180       1.6   rmind 		goto block;
    181       1.1   rmind 	}
    182       1.5   rmind 	npf_stats_inc(NPF_STAT_PASS_RULESET);
    183       1.1   rmind 
    184  1.11.2.2     mrg 	/*
    185  1.11.2.2     mrg 	 * Establish a "pass" session, if required.  Just proceed, if session
    186  1.11.2.2     mrg 	 * creation fails (e.g. due to unsupported protocol).
    187  1.11.2.2     mrg 	 *
    188  1.11.2.2     mrg 	 * Note: the reference on the rule procedure is transfered to the
    189  1.11.2.2     mrg 	 * session.  It will be released on session destruction.
    190  1.11.2.2     mrg 	 */
    191       1.5   rmind 	if ((retfl & NPF_RULE_KEEPSTATE) != 0 && !se) {
    192       1.5   rmind 		se = npf_session_establish(&npc, nbuf, di);
    193  1.11.2.2     mrg 		if (se) {
    194  1.11.2.2     mrg 			npf_session_setpass(se, rp);
    195       1.2   rmind 		}
    196       1.1   rmind 	}
    197       1.2   rmind pass:
    198  1.11.2.2     mrg 	decision = NPF_DECISION_PASS;
    199       1.2   rmind 	KASSERT(error == 0);
    200       1.5   rmind 	/*
    201       1.6   rmind 	 * Perform NAT.
    202       1.6   rmind 	 */
    203       1.6   rmind 	error = npf_do_nat(&npc, se, nbuf, ifp, di);
    204       1.6   rmind block:
    205       1.6   rmind 	/*
    206  1.11.2.1     mrg 	 * Execute rule procedure, if any.
    207       1.5   rmind 	 */
    208       1.5   rmind 	if (rp) {
    209       1.7   rmind 		npf_rproc_run(&npc, nbuf, rp, error);
    210       1.5   rmind 	}
    211       1.1   rmind out:
    212  1.11.2.1     mrg 	/*
    213  1.11.2.1     mrg 	 * Release the reference on a session.  Release the reference on a
    214  1.11.2.1     mrg 	 * rule procedure only if there was no association.
    215  1.11.2.1     mrg 	 */
    216       1.6   rmind 	if (se) {
    217       1.1   rmind 		npf_session_release(se);
    218       1.6   rmind 	} else if (rp) {
    219  1.11.2.1     mrg 		npf_rproc_release(rp);
    220       1.1   rmind 	}
    221       1.1   rmind 
    222  1.11.2.2     mrg 	/* Pass the packet if decided and there is no error. */
    223  1.11.2.2     mrg 	if (decision == NPF_DECISION_PASS && !error) {
    224       1.3   rmind 		/*
    225       1.3   rmind 		 * XXX: Disable for now, it will be set accordingly later,
    226       1.3   rmind 		 * for optimisations (to reduce inspection).
    227       1.3   rmind 		 */
    228       1.3   rmind 		(*mp)->m_flags &= ~M_CANFASTFWD;
    229  1.11.2.1     mrg 		return 0;
    230       1.1   rmind 	}
    231  1.11.2.1     mrg 
    232  1.11.2.1     mrg 	/*
    233  1.11.2.1     mrg 	 * Block the packet.  ENETUNREACH is used to indicate blocking.
    234  1.11.2.1     mrg 	 * Depending on the flags and protocol, return TCP reset (RST) or
    235  1.11.2.1     mrg 	 * ICMP destination unreachable.
    236  1.11.2.1     mrg 	 */
    237  1.11.2.4     mrg 	if (retfl && npf_return_block(&npc, nbuf, retfl)) {
    238  1.11.2.4     mrg 		*mp = NULL;
    239  1.11.2.1     mrg 	}
    240  1.11.2.4     mrg 
    241  1.11.2.2     mrg 	if (error) {
    242  1.11.2.1     mrg 		npf_stats_inc(NPF_STAT_ERROR);
    243  1.11.2.2     mrg 	} else {
    244  1.11.2.2     mrg 		error = ENETUNREACH;
    245  1.11.2.1     mrg 	}
    246  1.11.2.1     mrg 
    247  1.11.2.4     mrg 	if (*mp) {
    248  1.11.2.4     mrg 		m_freem(*mp);
    249  1.11.2.4     mrg 		*mp = NULL;
    250  1.11.2.4     mrg 	}
    251       1.1   rmind 	return error;
    252       1.1   rmind }
    253       1.1   rmind 
    254       1.1   rmind /*
    255  1.11.2.3     mrg  * npf_pfil_register: register pfil(9) hooks.
    256       1.1   rmind  */
    257       1.1   rmind int
    258  1.11.2.3     mrg npf_pfil_register(void)
    259       1.1   rmind {
    260       1.1   rmind 	int error;
    261       1.1   rmind 
    262       1.1   rmind 	mutex_enter(softnet_lock);
    263       1.1   rmind 	KERNEL_LOCK(1, NULL);
    264       1.1   rmind 
    265       1.1   rmind 	/* Check if pfil hooks are not already registered. */
    266       1.1   rmind 	if (npf_ph_if) {
    267       1.1   rmind 		error = EEXIST;
    268       1.1   rmind 		goto fail;
    269       1.1   rmind 	}
    270       1.1   rmind 
    271       1.1   rmind 	/* Capture point of any activity in interfaces and IP layer. */
    272       1.1   rmind 	npf_ph_if = pfil_head_get(PFIL_TYPE_IFNET, 0);
    273       1.1   rmind 	npf_ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
    274       1.8  zoltan 	npf_ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
    275  1.11.2.4     mrg 	if (!npf_ph_if || (!npf_ph_inet && !npf_ph_inet6)) {
    276       1.1   rmind 		npf_ph_if = NULL;
    277       1.1   rmind 		error = ENOENT;
    278       1.1   rmind 		goto fail;
    279       1.1   rmind 	}
    280       1.1   rmind 
    281       1.1   rmind 	/* Interface re-config or attach/detach hook. */
    282       1.1   rmind 	error = pfil_add_hook(npf_ifhook, NULL,
    283       1.1   rmind 	    PFIL_WAITOK | PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
    284       1.1   rmind 	KASSERT(error == 0);
    285       1.1   rmind 
    286       1.1   rmind 	/* Packet IN/OUT handler on all interfaces and IP layer. */
    287  1.11.2.4     mrg 	if (npf_ph_inet) {
    288  1.11.2.4     mrg 		error = pfil_add_hook(npf_packet_handler, NULL,
    289  1.11.2.4     mrg 		    PFIL_WAITOK | PFIL_ALL, npf_ph_inet);
    290  1.11.2.4     mrg 		KASSERT(error == 0);
    291  1.11.2.4     mrg 	}
    292  1.11.2.4     mrg 	if (npf_ph_inet6) {
    293  1.11.2.4     mrg 		error = pfil_add_hook(npf_packet_handler, NULL,
    294  1.11.2.4     mrg 		    PFIL_WAITOK | PFIL_ALL, npf_ph_inet6);
    295  1.11.2.4     mrg 		KASSERT(error == 0);
    296  1.11.2.4     mrg 	}
    297       1.1   rmind fail:
    298       1.1   rmind 	KERNEL_UNLOCK_ONE(NULL);
    299       1.1   rmind 	mutex_exit(softnet_lock);
    300       1.1   rmind 
    301       1.1   rmind 	return error;
    302       1.1   rmind }
    303       1.1   rmind 
    304       1.1   rmind /*
    305  1.11.2.3     mrg  * npf_pfil_unregister: unregister pfil(9) hooks.
    306       1.1   rmind  */
    307       1.1   rmind void
    308  1.11.2.3     mrg npf_pfil_unregister(void)
    309       1.1   rmind {
    310       1.1   rmind 
    311       1.1   rmind 	mutex_enter(softnet_lock);
    312       1.1   rmind 	KERNEL_LOCK(1, NULL);
    313       1.1   rmind 
    314       1.1   rmind 	if (npf_ph_if) {
    315       1.1   rmind 		(void)pfil_remove_hook(npf_ifhook, NULL,
    316       1.1   rmind 		    PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
    317       1.1   rmind 	}
    318  1.11.2.4     mrg 	if (npf_ph_inet) {
    319  1.11.2.4     mrg 		(void)pfil_remove_hook(npf_packet_handler, NULL,
    320  1.11.2.4     mrg 		    PFIL_ALL, npf_ph_inet);
    321  1.11.2.4     mrg 	}
    322  1.11.2.4     mrg 	if (npf_ph_inet6) {
    323  1.11.2.4     mrg 		(void)pfil_remove_hook(npf_packet_handler, NULL,
    324  1.11.2.4     mrg 		    PFIL_ALL, npf_ph_inet6);
    325  1.11.2.4     mrg 	}
    326  1.11.2.4     mrg 
    327  1.11.2.4     mrg 	npf_ph_if = NULL;
    328       1.1   rmind 
    329       1.1   rmind 	KERNEL_UNLOCK_ONE(NULL);
    330       1.1   rmind 	mutex_exit(softnet_lock);
    331       1.1   rmind }
    332  1.11.2.3     mrg 
    333  1.11.2.3     mrg bool
    334  1.11.2.3     mrg npf_pfil_registered_p(void)
    335  1.11.2.3     mrg {
    336  1.11.2.3     mrg 	return npf_ph_if != NULL;
    337  1.11.2.3     mrg }
    338