Home | History | Annotate | Line # | Download | only in npf
npf_handler.c revision 1.42
      1  1.42      maxv /*	$NetBSD: npf_handler.c,v 1.42 2018/07/10 15:25:01 maxv Exp $	*/
      2   1.1     rmind 
      3   1.1     rmind /*-
      4  1.26     rmind  * Copyright (c) 2009-2013 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.28     rmind  *
     35  1.28     rmind  * Note: pfil(9) hooks are currently locked by softnet_lock and kernel-lock.
     36   1.1     rmind  */
     37   1.1     rmind 
     38  1.35  christos #ifdef _KERNEL
     39   1.1     rmind #include <sys/cdefs.h>
     40  1.42      maxv __KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.42 2018/07/10 15:25:01 maxv Exp $");
     41   1.1     rmind 
     42  1.14     rmind #include <sys/types.h>
     43   1.1     rmind #include <sys/param.h>
     44   1.1     rmind 
     45   1.1     rmind #include <sys/mbuf.h>
     46   1.1     rmind #include <sys/mutex.h>
     47   1.1     rmind #include <net/if.h>
     48   1.1     rmind #include <net/pfil.h>
     49   1.1     rmind #include <sys/socketvar.h>
     50   1.1     rmind 
     51   1.4     rmind #include <netinet/in_systm.h>
     52   1.4     rmind #include <netinet/in.h>
     53   1.4     rmind #include <netinet/ip_var.h>
     54   1.8    zoltan #include <netinet/ip6.h>
     55   1.8    zoltan #include <netinet6/ip6_var.h>
     56  1.35  christos #endif
     57   1.4     rmind 
     58   1.1     rmind #include "npf_impl.h"
     59  1.31     rmind #include "npf_conn.h"
     60   1.1     rmind 
     61  1.35  christos #if defined(_NPF_STANDALONE)
     62  1.35  christos #define	m_freem(m)		npf->mbufops->free(m)
     63  1.35  christos #define	m_clear_flag(m,f)
     64  1.35  christos #else
     65  1.35  christos #define	m_clear_flag(m,f)	(m)->m_flags &= ~(f)
     66  1.35  christos #endif
     67   1.1     rmind 
     68  1.26     rmind #ifndef INET6
     69  1.26     rmind #define ip6_reass_packet(x, y)	ENOTSUP
     70  1.26     rmind #endif
     71  1.26     rmind 
     72  1.24     rmind static int
     73  1.42      maxv npf_reassembly(npf_t *npf, npf_cache_t *npc, bool *mff)
     74  1.24     rmind {
     75  1.32     rmind 	nbuf_t *nbuf = npc->npc_nbuf;
     76  1.24     rmind 	int error = EINVAL;
     77  1.42      maxv 	struct mbuf *m;
     78  1.42      maxv 
     79  1.42      maxv 	*mff = false;
     80  1.42      maxv 	m = nbuf_head_mbuf(nbuf);
     81  1.24     rmind 
     82  1.24     rmind 	/* Reset the mbuf as it may have changed. */
     83  1.24     rmind 	nbuf_reset(nbuf);
     84  1.24     rmind 
     85  1.24     rmind 	if (npf_iscached(npc, NPC_IP4)) {
     86  1.24     rmind 		struct ip *ip = nbuf_dataptr(nbuf);
     87  1.42      maxv 		error = ip_reass_packet(&m, ip);
     88  1.42      maxv 		KASSERT(!error || (m != NULL));
     89  1.24     rmind 	} else if (npf_iscached(npc, NPC_IP6)) {
     90  1.42      maxv 		error = ip6_reass_packet(&m, npc->npc_hlen);
     91  1.42      maxv 		if (error && m == NULL) {
     92  1.25     rmind 			memset(nbuf, 0, sizeof(nbuf_t));
     93  1.25     rmind 		}
     94  1.24     rmind 	}
     95  1.24     rmind 	if (error) {
     96  1.35  christos 		npf_stats_inc(npf, NPF_STAT_REASSFAIL);
     97  1.24     rmind 		return error;
     98  1.24     rmind 	}
     99  1.42      maxv 	if (m == NULL) {
    100  1.24     rmind 		/* More fragments should come. */
    101  1.35  christos 		npf_stats_inc(npf, NPF_STAT_FRAGMENTS);
    102  1.42      maxv 		*mff = true;
    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.42      maxv 	nbuf_init(npf, nbuf, m, nbuf->nb_ifp);
    111  1.24     rmind 	npc->npc_info = 0;
    112  1.24     rmind 
    113  1.38      maxv 	if (npf_cache_all(npc) & (NPC_IPFRAG|NPC_FMTERR)) {
    114  1.24     rmind 		return EINVAL;
    115  1.24     rmind 	}
    116  1.35  christos 	npf_stats_inc(npf, 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.35  christos __dso_public int
    126  1.35  christos npf_packet_handler(npf_t *npf, 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.31     rmind 	npf_conn_t *con;
    131   1.1     rmind 	npf_rule_t *rl;
    132   1.5     rmind 	npf_rproc_t *rp;
    133  1.37  christos 	int error, decision, flags;
    134  1.34     rmind 	uint32_t ntag;
    135  1.36  christos 	npf_match_info_t mi;
    136  1.42      maxv 	bool mff;
    137   1.1     rmind 
    138  1.35  christos 	/* QSBR checkpoint. */
    139  1.35  christos 	pserialize_checkpoint(npf->qsbr);
    140  1.35  christos 	KASSERT(ifp != NULL);
    141  1.35  christos 
    142   1.1     rmind 	/*
    143   1.1     rmind 	 * Initialise packet information cache.
    144   1.1     rmind 	 * Note: it is enough to clear the info bits.
    145   1.1     rmind 	 */
    146  1.35  christos 	npc.npc_ctx = npf;
    147  1.35  christos 	nbuf_init(npf, &nbuf, *mp, ifp);
    148  1.32     rmind 	npc.npc_nbuf = &nbuf;
    149   1.1     rmind 	npc.npc_info = 0;
    150  1.32     rmind 
    151  1.36  christos 	mi.mi_di = di;
    152  1.36  christos 	mi.mi_rid = 0;
    153  1.36  christos 	mi.mi_retfl = 0;
    154  1.36  christos 
    155  1.42      maxv 	*mp = NULL;
    156  1.14     rmind 	decision = NPF_DECISION_BLOCK;
    157   1.2     rmind 	error = 0;
    158   1.5     rmind 	rp = NULL;
    159  1.40      maxv 	con = NULL;
    160   1.1     rmind 
    161  1.38      maxv 	/* Cache everything. */
    162  1.37  christos 	flags = npf_cache_all(&npc);
    163  1.38      maxv 
    164  1.38      maxv 	/* If error on the format, leave quickly. */
    165  1.38      maxv 	if (flags & NPC_FMTERR) {
    166  1.38      maxv 		error = EINVAL;
    167  1.42      maxv 		goto out;
    168  1.38      maxv 	}
    169  1.38      maxv 
    170  1.38      maxv 	/* Determine whether it is an IP fragment. */
    171  1.37  christos 	if (__predict_false(flags & NPC_IPFRAG)) {
    172  1.42      maxv 		/* Pass to IPv4/IPv6 reassembly mechanism. */
    173  1.42      maxv 		error = npf_reassembly(npf, &npc, &mff);
    174  1.18     rmind 		if (error) {
    175   1.4     rmind 			goto out;
    176   1.4     rmind 		}
    177  1.42      maxv 		if (mff) {
    178  1.42      maxv 			/* More fragments should come. */
    179   1.4     rmind 			return 0;
    180   1.4     rmind 		}
    181   1.4     rmind 	}
    182   1.4     rmind 
    183  1.34     rmind 	/* Just pass-through if specially tagged. */
    184  1.34     rmind 	if (nbuf_find_tag(&nbuf, &ntag) == 0 && (ntag & NPF_NTAG_PASS) != 0) {
    185  1.34     rmind 		goto pass;
    186  1.34     rmind 	}
    187  1.34     rmind 
    188  1.31     rmind 	/* Inspect the list of connections (if found, acquires a reference). */
    189  1.32     rmind 	con = npf_conn_inspect(&npc, di, &error);
    190   1.2     rmind 
    191  1.31     rmind 	/* If "passing" connection found - skip the ruleset inspection. */
    192  1.36  christos 	if (con && npf_conn_pass(con, &mi, &rp)) {
    193  1.35  christos 		npf_stats_inc(npf, NPF_STAT_PASS_CONN);
    194  1.14     rmind 		KASSERT(error == 0);
    195   1.2     rmind 		goto pass;
    196  1.14     rmind 	}
    197  1.32     rmind 	if (__predict_false(error)) {
    198  1.24     rmind 		if (error == ENETUNREACH)
    199  1.24     rmind 			goto block;
    200  1.24     rmind 		goto out;
    201   1.2     rmind 	}
    202   1.1     rmind 
    203   1.7     rmind 	/* Acquire the lock, inspect the ruleset using this packet. */
    204  1.26     rmind 	int slock = npf_config_read_enter();
    205  1.35  christos 	npf_ruleset_t *rlset = npf_config_ruleset(npf);
    206  1.26     rmind 
    207  1.32     rmind 	rl = npf_ruleset_inspect(&npc, rlset, di, NPF_LAYER_3);
    208  1.32     rmind 	if (__predict_false(rl == NULL)) {
    209  1.35  christos 		const bool pass = npf_default_pass(npf);
    210  1.26     rmind 		npf_config_read_exit(slock);
    211  1.14     rmind 
    212  1.26     rmind 		if (pass) {
    213  1.35  christos 			npf_stats_inc(npf, NPF_STAT_PASS_DEFAULT);
    214   1.2     rmind 			goto pass;
    215   1.2     rmind 		}
    216  1.35  christos 		npf_stats_inc(npf, NPF_STAT_BLOCK_DEFAULT);
    217   1.6     rmind 		goto block;
    218   1.1     rmind 	}
    219   1.1     rmind 
    220  1.13     rmind 	/*
    221  1.24     rmind 	 * Get the rule procedure (acquires a reference) for association
    222  1.31     rmind 	 * with a connection (if any) and execution.
    223  1.13     rmind 	 */
    224   1.6     rmind 	KASSERT(rp == NULL);
    225  1.13     rmind 	rp = npf_rule_getrproc(rl);
    226   1.6     rmind 
    227  1.26     rmind 	/* Conclude with the rule and release the lock. */
    228  1.36  christos 	error = npf_rule_conclude(rl, &mi);
    229  1.26     rmind 	npf_config_read_exit(slock);
    230  1.26     rmind 
    231   1.2     rmind 	if (error) {
    232  1.35  christos 		npf_stats_inc(npf, NPF_STAT_BLOCK_RULESET);
    233   1.6     rmind 		goto block;
    234   1.1     rmind 	}
    235  1.35  christos 	npf_stats_inc(npf, NPF_STAT_PASS_RULESET);
    236   1.1     rmind 
    237  1.14     rmind 	/*
    238  1.31     rmind 	 * Establish a "pass" connection, if required.  Just proceed if
    239  1.31     rmind 	 * connection creation fails (e.g. due to unsupported protocol).
    240  1.14     rmind 	 */
    241  1.36  christos 	if ((mi.mi_retfl & NPF_RULE_STATEFUL) != 0 && !con) {
    242  1.32     rmind 		con = npf_conn_establish(&npc, di,
    243  1.36  christos 		    (mi.mi_retfl & NPF_RULE_MULTIENDS) == 0);
    244  1.31     rmind 		if (con) {
    245  1.26     rmind 			/*
    246  1.26     rmind 			 * Note: the reference on the rule procedure is
    247  1.31     rmind 			 * transfered to the connection.  It will be
    248  1.31     rmind 			 * released on connection destruction.
    249  1.26     rmind 			 */
    250  1.36  christos 			npf_conn_setpass(con, &mi, rp);
    251   1.2     rmind 		}
    252   1.1     rmind 	}
    253  1.42      maxv 
    254   1.2     rmind pass:
    255  1.14     rmind 	decision = NPF_DECISION_PASS;
    256   1.2     rmind 	KASSERT(error == 0);
    257   1.5     rmind 	/*
    258   1.6     rmind 	 * Perform NAT.
    259   1.6     rmind 	 */
    260  1.32     rmind 	error = npf_do_nat(&npc, con, di);
    261  1.42      maxv 
    262   1.6     rmind block:
    263   1.6     rmind 	/*
    264  1.22     rmind 	 * Execute the rule procedure, if any is associated.
    265  1.22     rmind 	 * It may reverse the decision from pass to block.
    266   1.5     rmind 	 */
    267  1.36  christos 	if (rp && !npf_rproc_run(&npc, rp, &mi, &decision)) {
    268  1.31     rmind 		if (con) {
    269  1.31     rmind 			npf_conn_release(con);
    270  1.30  jakllsch 		}
    271  1.30  jakllsch 		npf_rproc_release(rp);
    272  1.42      maxv 		/* mbuf already freed */
    273  1.30  jakllsch 		return 0;
    274   1.5     rmind 	}
    275  1.42      maxv 
    276   1.1     rmind out:
    277  1.13     rmind 	/*
    278  1.31     rmind 	 * Release the reference on a connection.  Release the reference
    279  1.31     rmind 	 * on a rule procedure only if there was no association.
    280  1.13     rmind 	 */
    281  1.31     rmind 	if (con) {
    282  1.31     rmind 		npf_conn_release(con);
    283   1.6     rmind 	} else if (rp) {
    284  1.13     rmind 		npf_rproc_release(rp);
    285   1.1     rmind 	}
    286   1.1     rmind 
    287  1.42      maxv 	/* Get the new mbuf pointer. */
    288  1.24     rmind 	if ((*mp = nbuf_head_mbuf(&nbuf)) == NULL) {
    289  1.25     rmind 		return error ? error : ENOMEM;
    290  1.24     rmind 	}
    291  1.24     rmind 
    292  1.14     rmind 	/* Pass the packet if decided and there is no error. */
    293  1.14     rmind 	if (decision == NPF_DECISION_PASS && !error) {
    294   1.3     rmind 		/*
    295   1.3     rmind 		 * XXX: Disable for now, it will be set accordingly later,
    296   1.3     rmind 		 * for optimisations (to reduce inspection).
    297   1.3     rmind 		 */
    298  1.35  christos 		m_clear_flag(*mp, M_CANFASTFWD);
    299  1.13     rmind 		return 0;
    300   1.1     rmind 	}
    301  1.13     rmind 
    302  1.13     rmind 	/*
    303  1.13     rmind 	 * Block the packet.  ENETUNREACH is used to indicate blocking.
    304  1.13     rmind 	 * Depending on the flags and protocol, return TCP reset (RST) or
    305  1.13     rmind 	 * ICMP destination unreachable.
    306  1.13     rmind 	 */
    307  1.36  christos 	if (mi.mi_retfl && npf_return_block(&npc, mi.mi_retfl)) {
    308  1.16     rmind 		*mp = NULL;
    309  1.13     rmind 	}
    310  1.16     rmind 
    311  1.20     rmind 	if (!error) {
    312  1.14     rmind 		error = ENETUNREACH;
    313  1.13     rmind 	}
    314  1.13     rmind 
    315  1.16     rmind 	if (*mp) {
    316  1.35  christos 		/* Free the mbuf chain. */
    317  1.16     rmind 		m_freem(*mp);
    318  1.16     rmind 		*mp = NULL;
    319  1.16     rmind 	}
    320   1.1     rmind 	return error;
    321   1.1     rmind }
    322