Home | History | Annotate | Line # | Download | only in npf
npf_handler.c revision 1.1
      1  1.1  rmind /*	$NetBSD: npf_handler.c,v 1.1 2010/08/22 18:56:22 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 #ifdef _KERNEL
     37  1.1  rmind #include <sys/cdefs.h>
     38  1.1  rmind __KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.1 2010/08/22 18:56:22 rmind Exp $");
     39  1.1  rmind 
     40  1.1  rmind #include <sys/param.h>
     41  1.1  rmind #include <sys/systm.h>
     42  1.1  rmind #endif
     43  1.1  rmind 
     44  1.1  rmind #include <sys/mbuf.h>
     45  1.1  rmind #include <sys/mutex.h>
     46  1.1  rmind #include <net/if.h>
     47  1.1  rmind #include <net/pfil.h>
     48  1.1  rmind #include <sys/socketvar.h>
     49  1.1  rmind 
     50  1.1  rmind #include "npf_impl.h"
     51  1.1  rmind 
     52  1.1  rmind /*
     53  1.1  rmind  * If npf_ph_if != NULL, pfil hooks are registers.  If NULL, not registered.
     54  1.1  rmind  * Used to check the state.  Locked by: softnet_lock + KERNEL_LOCK (XXX).
     55  1.1  rmind  */
     56  1.1  rmind static struct pfil_head *	npf_ph_if = NULL;
     57  1.1  rmind static struct pfil_head *	npf_ph_inet = NULL;
     58  1.1  rmind 
     59  1.1  rmind int	npf_packet_handler(void *, struct mbuf **, struct ifnet *, int);
     60  1.1  rmind 
     61  1.1  rmind /*
     62  1.1  rmind  * npf_ifhook: hook handling interface changes.
     63  1.1  rmind  */
     64  1.1  rmind static int
     65  1.1  rmind npf_ifhook(void *arg, struct mbuf **mp, struct ifnet *ifp, int di)
     66  1.1  rmind {
     67  1.1  rmind 
     68  1.1  rmind 	return 0;
     69  1.1  rmind }
     70  1.1  rmind 
     71  1.1  rmind /*
     72  1.1  rmind  * npf_packet_handler: main packet handling routine.
     73  1.1  rmind  *
     74  1.1  rmind  * Note: packet flow and inspection logic is in strict order.
     75  1.1  rmind  */
     76  1.1  rmind int
     77  1.1  rmind npf_packet_handler(void *arg, struct mbuf **mp, struct ifnet *ifp, int di)
     78  1.1  rmind {
     79  1.1  rmind 	const int layer = (const int)(long)arg;
     80  1.1  rmind 	nbuf_t *nbuf = *mp;
     81  1.1  rmind 	npf_cache_t npc;
     82  1.1  rmind 	npf_session_t *se;
     83  1.1  rmind 	npf_rule_t *rl;
     84  1.1  rmind 	int error;
     85  1.1  rmind 
     86  1.1  rmind 	/*
     87  1.1  rmind 	 * Initialise packet information cache.
     88  1.1  rmind 	 * Note: it is enough to clear the info bits.
     89  1.1  rmind 	 */
     90  1.1  rmind 	npc.npc_info = 0;
     91  1.1  rmind 
     92  1.1  rmind 	/* Inspect the list of sessions. */
     93  1.1  rmind 	se = npf_session_inspect(&npc, nbuf, ifp, di, layer);
     94  1.1  rmind 
     95  1.1  rmind 	/* Inbound NAT. */
     96  1.1  rmind 	if ((di & PFIL_IN) && (error = npf_natin(&npc, se, nbuf, layer)) != 0) {
     97  1.1  rmind 		goto out;
     98  1.1  rmind 	}
     99  1.1  rmind 
    100  1.1  rmind 	/* If session found - we pass this packet. */
    101  1.1  rmind 	if (se && npf_session_pass(se)) {
    102  1.1  rmind 		error = 0;
    103  1.1  rmind 	} else {
    104  1.1  rmind 		/* Inspect ruleset using this packet. */
    105  1.1  rmind 		rl = npf_ruleset_inspect(&npc, nbuf, ifp, di, layer);
    106  1.1  rmind 		if (rl != NULL) {
    107  1.1  rmind 			bool keepstate;
    108  1.1  rmind 			/* Apply the rule. */
    109  1.1  rmind 			error = npf_rule_apply(&npc, rl, &keepstate);
    110  1.1  rmind 			if (error) {
    111  1.1  rmind 				goto out;
    112  1.1  rmind 			}
    113  1.1  rmind 			/* Establish a session, if required. */
    114  1.1  rmind 			if (keepstate) {
    115  1.1  rmind 				se = npf_session_establish(&npc, NULL, di);
    116  1.1  rmind 			}
    117  1.1  rmind 		}
    118  1.1  rmind 		/* No rules or "default" rule - pass. */
    119  1.1  rmind 	}
    120  1.1  rmind 
    121  1.1  rmind 	/* Outbound NAT. */
    122  1.1  rmind 	if (di & PFIL_OUT) {
    123  1.1  rmind 		error = npf_natout(&npc, se, nbuf, ifp, layer);
    124  1.1  rmind 	}
    125  1.1  rmind out:
    126  1.1  rmind 	/* Release reference on session. */
    127  1.1  rmind 	if (se != NULL) {
    128  1.1  rmind 		npf_session_release(se);
    129  1.1  rmind 	}
    130  1.1  rmind 
    131  1.1  rmind 	/*
    132  1.1  rmind 	 * If error is set - drop the packet.
    133  1.1  rmind 	 * Normally, ENETUNREACH is used to "block".
    134  1.1  rmind 	 */
    135  1.1  rmind 	if (error) {
    136  1.1  rmind 		m_freem(*mp);
    137  1.1  rmind 		*mp = NULL;
    138  1.1  rmind 	}
    139  1.1  rmind 	return error;
    140  1.1  rmind }
    141  1.1  rmind 
    142  1.1  rmind /*
    143  1.1  rmind  * npf_register_pfil: register pfil(9) hooks.
    144  1.1  rmind  */
    145  1.1  rmind int
    146  1.1  rmind npf_register_pfil(void)
    147  1.1  rmind {
    148  1.1  rmind 	int error;
    149  1.1  rmind 
    150  1.1  rmind 	mutex_enter(softnet_lock);
    151  1.1  rmind 	KERNEL_LOCK(1, NULL);
    152  1.1  rmind 
    153  1.1  rmind 	/* Check if pfil hooks are not already registered. */
    154  1.1  rmind 	if (npf_ph_if) {
    155  1.1  rmind 		error = EEXIST;
    156  1.1  rmind 		goto fail;
    157  1.1  rmind 	}
    158  1.1  rmind 
    159  1.1  rmind 	/* Capture point of any activity in interfaces and IP layer. */
    160  1.1  rmind 	npf_ph_if = pfil_head_get(PFIL_TYPE_IFNET, 0);
    161  1.1  rmind 	npf_ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
    162  1.1  rmind 	if (npf_ph_if == NULL || npf_ph_inet == NULL) {
    163  1.1  rmind 		npf_ph_if = NULL;
    164  1.1  rmind 		error = ENOENT;
    165  1.1  rmind 		goto fail;
    166  1.1  rmind 	}
    167  1.1  rmind 
    168  1.1  rmind 	/* Interface re-config or attach/detach hook. */
    169  1.1  rmind 	error = pfil_add_hook(npf_ifhook, NULL,
    170  1.1  rmind 	    PFIL_WAITOK | PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
    171  1.1  rmind 	KASSERT(error == 0);
    172  1.1  rmind 
    173  1.1  rmind 	/* Packet IN/OUT handler on all interfaces and IP layer. */
    174  1.1  rmind 	error = pfil_add_hook(npf_packet_handler, (void *)NPF_LAYER_3,
    175  1.1  rmind 	    PFIL_WAITOK | PFIL_ALL, npf_ph_inet);
    176  1.1  rmind 	KASSERT(error == 0);
    177  1.1  rmind 
    178  1.1  rmind fail:
    179  1.1  rmind 	KERNEL_UNLOCK_ONE(NULL);
    180  1.1  rmind 	mutex_exit(softnet_lock);
    181  1.1  rmind 
    182  1.1  rmind 	return error;
    183  1.1  rmind }
    184  1.1  rmind 
    185  1.1  rmind /*
    186  1.1  rmind  * npf_unregister: unregister pfil(9) hooks.
    187  1.1  rmind  */
    188  1.1  rmind void
    189  1.1  rmind npf_unregister_pfil(void)
    190  1.1  rmind {
    191  1.1  rmind 
    192  1.1  rmind 	mutex_enter(softnet_lock);
    193  1.1  rmind 	KERNEL_LOCK(1, NULL);
    194  1.1  rmind 
    195  1.1  rmind 	if (npf_ph_if) {
    196  1.1  rmind 		(void)pfil_remove_hook(npf_packet_handler, (void *)NPF_LAYER_3,
    197  1.1  rmind 		    PFIL_ALL, npf_ph_inet);
    198  1.1  rmind 		(void)pfil_remove_hook(npf_ifhook, NULL,
    199  1.1  rmind 		    PFIL_IFADDR | PFIL_IFNET, npf_ph_if);
    200  1.1  rmind 
    201  1.1  rmind 		npf_ph_if = NULL;
    202  1.1  rmind 	}
    203  1.1  rmind 
    204  1.1  rmind 	KERNEL_UNLOCK_ONE(NULL);
    205  1.1  rmind 	mutex_exit(softnet_lock);
    206  1.1  rmind }
    207