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