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