Home | History | Annotate | Line # | Download | only in npf
npf_handler.c revision 1.39.2.1
      1       1.1     rmind /*-
      2      1.26     rmind  * Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
      3       1.1     rmind  * All rights reserved.
      4       1.1     rmind  *
      5       1.1     rmind  * This material is based upon work partially supported by The
      6       1.1     rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      7       1.1     rmind  *
      8       1.1     rmind  * Redistribution and use in source and binary forms, with or without
      9       1.1     rmind  * modification, are permitted provided that the following conditions
     10       1.1     rmind  * are met:
     11       1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     12       1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     13       1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     15       1.1     rmind  *    documentation and/or other materials provided with the distribution.
     16       1.1     rmind  *
     17       1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18       1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19       1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20       1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21       1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22       1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23       1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24       1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25       1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26       1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27       1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     28       1.1     rmind  */
     29       1.1     rmind 
     30       1.1     rmind /*
     31       1.1     rmind  * NPF packet handler.
     32      1.28     rmind  *
     33      1.28     rmind  * Note: pfil(9) hooks are currently locked by softnet_lock and kernel-lock.
     34       1.1     rmind  */
     35       1.1     rmind 
     36      1.35  christos #ifdef _KERNEL
     37       1.1     rmind #include <sys/cdefs.h>
     38  1.39.2.1  christos __KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.39.2.1 2019/06/10 22:09:46 christos Exp $");
     39       1.1     rmind 
     40      1.14     rmind #include <sys/types.h>
     41       1.1     rmind #include <sys/param.h>
     42       1.1     rmind 
     43       1.1     rmind #include <sys/mbuf.h>
     44       1.1     rmind #include <sys/mutex.h>
     45       1.1     rmind #include <net/if.h>
     46       1.1     rmind #include <net/pfil.h>
     47       1.1     rmind #include <sys/socketvar.h>
     48       1.1     rmind 
     49       1.4     rmind #include <netinet/in_systm.h>
     50       1.4     rmind #include <netinet/in.h>
     51       1.4     rmind #include <netinet/ip_var.h>
     52       1.8    zoltan #include <netinet/ip6.h>
     53       1.8    zoltan #include <netinet6/ip6_var.h>
     54      1.35  christos #endif
     55       1.4     rmind 
     56       1.1     rmind #include "npf_impl.h"
     57      1.31     rmind #include "npf_conn.h"
     58       1.1     rmind 
     59      1.35  christos #if defined(_NPF_STANDALONE)
     60      1.35  christos #define	m_freem(m)		npf->mbufops->free(m)
     61      1.35  christos #define	m_clear_flag(m,f)
     62      1.35  christos #else
     63      1.35  christos #define	m_clear_flag(m,f)	(m)->m_flags &= ~(f)
     64      1.35  christos #endif
     65       1.1     rmind 
     66      1.26     rmind #ifndef INET6
     67      1.26     rmind #define ip6_reass_packet(x, y)	ENOTSUP
     68      1.26     rmind #endif
     69      1.26     rmind 
     70      1.24     rmind static int
     71  1.39.2.1  christos npf_reassembly(npf_t *npf, npf_cache_t *npc, bool *mff)
     72      1.24     rmind {
     73      1.32     rmind 	nbuf_t *nbuf = npc->npc_nbuf;
     74      1.24     rmind 	int error = EINVAL;
     75  1.39.2.1  christos 	struct mbuf *m;
     76      1.24     rmind 
     77  1.39.2.1  christos 	*mff = false;
     78  1.39.2.1  christos 	m = nbuf_head_mbuf(nbuf);
     79      1.24     rmind 
     80      1.24     rmind 	if (npf_iscached(npc, NPC_IP4)) {
     81  1.39.2.1  christos 		error = ip_reass_packet(&m);
     82      1.24     rmind 	} else if (npf_iscached(npc, NPC_IP6)) {
     83  1.39.2.1  christos 		error = ip6_reass_packet(&m, npc->npc_hlen);
     84      1.24     rmind 	}
     85  1.39.2.1  christos 
     86      1.24     rmind 	if (error) {
     87  1.39.2.1  christos 		/* Reass failed. Free the mbuf, clear the nbuf. */
     88      1.35  christos 		npf_stats_inc(npf, NPF_STAT_REASSFAIL);
     89  1.39.2.1  christos 		m_freem(m);
     90  1.39.2.1  christos 		memset(nbuf, 0, sizeof(nbuf_t));
     91      1.24     rmind 		return error;
     92      1.24     rmind 	}
     93  1.39.2.1  christos 	if (m == NULL) {
     94      1.24     rmind 		/* More fragments should come. */
     95      1.35  christos 		npf_stats_inc(npf, NPF_STAT_FRAGMENTS);
     96  1.39.2.1  christos 		*mff = true;
     97      1.24     rmind 		return 0;
     98      1.24     rmind 	}
     99      1.24     rmind 
    100      1.24     rmind 	/*
    101      1.24     rmind 	 * Reassembly is complete, we have the final packet.
    102      1.24     rmind 	 * Cache again, since layer 4 data is accessible now.
    103      1.24     rmind 	 */
    104  1.39.2.1  christos 	nbuf_init(npf, nbuf, m, nbuf->nb_ifp);
    105      1.24     rmind 	npc->npc_info = 0;
    106      1.24     rmind 
    107      1.38      maxv 	if (npf_cache_all(npc) & (NPC_IPFRAG|NPC_FMTERR)) {
    108      1.24     rmind 		return EINVAL;
    109      1.24     rmind 	}
    110      1.35  christos 	npf_stats_inc(npf, NPF_STAT_REASSEMBLY);
    111      1.24     rmind 	return 0;
    112      1.24     rmind }
    113      1.24     rmind 
    114       1.1     rmind /*
    115       1.2     rmind  * npf_packet_handler: main packet handling routine for layer 3.
    116       1.1     rmind  *
    117       1.1     rmind  * Note: packet flow and inspection logic is in strict order.
    118       1.1     rmind  */
    119      1.35  christos __dso_public int
    120      1.35  christos npf_packet_handler(npf_t *npf, struct mbuf **mp, ifnet_t *ifp, int di)
    121       1.1     rmind {
    122      1.24     rmind 	nbuf_t nbuf;
    123       1.1     rmind 	npf_cache_t npc;
    124      1.31     rmind 	npf_conn_t *con;
    125       1.1     rmind 	npf_rule_t *rl;
    126       1.5     rmind 	npf_rproc_t *rp;
    127      1.37  christos 	int error, decision, flags;
    128      1.34     rmind 	uint32_t ntag;
    129      1.36  christos 	npf_match_info_t mi;
    130  1.39.2.1  christos 	bool mff;
    131       1.1     rmind 
    132      1.35  christos 	/* QSBR checkpoint. */
    133      1.35  christos 	pserialize_checkpoint(npf->qsbr);
    134      1.35  christos 	KASSERT(ifp != NULL);
    135      1.35  christos 
    136       1.1     rmind 	/*
    137       1.1     rmind 	 * Initialise packet information cache.
    138       1.1     rmind 	 * Note: it is enough to clear the info bits.
    139       1.1     rmind 	 */
    140      1.35  christos 	npc.npc_ctx = npf;
    141      1.35  christos 	nbuf_init(npf, &nbuf, *mp, ifp);
    142      1.32     rmind 	npc.npc_nbuf = &nbuf;
    143       1.1     rmind 	npc.npc_info = 0;
    144      1.32     rmind 
    145      1.36  christos 	mi.mi_di = di;
    146      1.36  christos 	mi.mi_rid = 0;
    147      1.36  christos 	mi.mi_retfl = 0;
    148      1.36  christos 
    149  1.39.2.1  christos 	*mp = NULL;
    150      1.14     rmind 	decision = NPF_DECISION_BLOCK;
    151       1.2     rmind 	error = 0;
    152       1.5     rmind 	rp = NULL;
    153  1.39.2.1  christos 	con = NULL;
    154       1.1     rmind 
    155      1.38      maxv 	/* Cache everything. */
    156      1.37  christos 	flags = npf_cache_all(&npc);
    157      1.38      maxv 
    158      1.38      maxv 	/* If error on the format, leave quickly. */
    159      1.38      maxv 	if (flags & NPC_FMTERR) {
    160      1.38      maxv 		error = EINVAL;
    161  1.39.2.1  christos 		goto out;
    162      1.38      maxv 	}
    163      1.38      maxv 
    164      1.38      maxv 	/* Determine whether it is an IP fragment. */
    165      1.37  christos 	if (__predict_false(flags & NPC_IPFRAG)) {
    166  1.39.2.1  christos 		/* Pass to IPv4/IPv6 reassembly mechanism. */
    167  1.39.2.1  christos 		error = npf_reassembly(npf, &npc, &mff);
    168      1.18     rmind 		if (error) {
    169       1.4     rmind 			goto out;
    170       1.4     rmind 		}
    171  1.39.2.1  christos 		if (mff) {
    172  1.39.2.1  christos 			/* More fragments should come. */
    173       1.4     rmind 			return 0;
    174       1.4     rmind 		}
    175       1.4     rmind 	}
    176       1.4     rmind 
    177      1.34     rmind 	/* Just pass-through if specially tagged. */
    178      1.34     rmind 	if (nbuf_find_tag(&nbuf, &ntag) == 0 && (ntag & NPF_NTAG_PASS) != 0) {
    179      1.34     rmind 		goto pass;
    180      1.34     rmind 	}
    181      1.34     rmind 
    182      1.31     rmind 	/* Inspect the list of connections (if found, acquires a reference). */
    183      1.32     rmind 	con = npf_conn_inspect(&npc, di, &error);
    184       1.2     rmind 
    185      1.31     rmind 	/* If "passing" connection found - skip the ruleset inspection. */
    186      1.36  christos 	if (con && npf_conn_pass(con, &mi, &rp)) {
    187      1.35  christos 		npf_stats_inc(npf, NPF_STAT_PASS_CONN);
    188      1.14     rmind 		KASSERT(error == 0);
    189       1.2     rmind 		goto pass;
    190      1.14     rmind 	}
    191      1.32     rmind 	if (__predict_false(error)) {
    192      1.24     rmind 		if (error == ENETUNREACH)
    193      1.24     rmind 			goto block;
    194      1.24     rmind 		goto out;
    195       1.2     rmind 	}
    196       1.1     rmind 
    197       1.7     rmind 	/* Acquire the lock, inspect the ruleset using this packet. */
    198      1.26     rmind 	int slock = npf_config_read_enter();
    199      1.35  christos 	npf_ruleset_t *rlset = npf_config_ruleset(npf);
    200      1.26     rmind 
    201      1.32     rmind 	rl = npf_ruleset_inspect(&npc, rlset, di, NPF_LAYER_3);
    202      1.32     rmind 	if (__predict_false(rl == NULL)) {
    203      1.35  christos 		const bool pass = npf_default_pass(npf);
    204      1.26     rmind 		npf_config_read_exit(slock);
    205      1.14     rmind 
    206      1.26     rmind 		if (pass) {
    207      1.35  christos 			npf_stats_inc(npf, NPF_STAT_PASS_DEFAULT);
    208       1.2     rmind 			goto pass;
    209       1.2     rmind 		}
    210      1.35  christos 		npf_stats_inc(npf, NPF_STAT_BLOCK_DEFAULT);
    211       1.6     rmind 		goto block;
    212       1.1     rmind 	}
    213       1.1     rmind 
    214      1.13     rmind 	/*
    215      1.24     rmind 	 * Get the rule procedure (acquires a reference) for association
    216      1.31     rmind 	 * with a connection (if any) and execution.
    217      1.13     rmind 	 */
    218       1.6     rmind 	KASSERT(rp == NULL);
    219      1.13     rmind 	rp = npf_rule_getrproc(rl);
    220       1.6     rmind 
    221      1.26     rmind 	/* Conclude with the rule and release the lock. */
    222      1.36  christos 	error = npf_rule_conclude(rl, &mi);
    223      1.26     rmind 	npf_config_read_exit(slock);
    224      1.26     rmind 
    225       1.2     rmind 	if (error) {
    226      1.35  christos 		npf_stats_inc(npf, NPF_STAT_BLOCK_RULESET);
    227       1.6     rmind 		goto block;
    228       1.1     rmind 	}
    229      1.35  christos 	npf_stats_inc(npf, NPF_STAT_PASS_RULESET);
    230       1.1     rmind 
    231      1.14     rmind 	/*
    232      1.31     rmind 	 * Establish a "pass" connection, if required.  Just proceed if
    233      1.31     rmind 	 * connection creation fails (e.g. due to unsupported protocol).
    234      1.14     rmind 	 */
    235      1.36  christos 	if ((mi.mi_retfl & NPF_RULE_STATEFUL) != 0 && !con) {
    236      1.32     rmind 		con = npf_conn_establish(&npc, di,
    237      1.36  christos 		    (mi.mi_retfl & NPF_RULE_MULTIENDS) == 0);
    238      1.31     rmind 		if (con) {
    239      1.26     rmind 			/*
    240      1.26     rmind 			 * Note: the reference on the rule procedure is
    241      1.31     rmind 			 * transfered to the connection.  It will be
    242      1.31     rmind 			 * released on connection destruction.
    243      1.26     rmind 			 */
    244      1.36  christos 			npf_conn_setpass(con, &mi, rp);
    245       1.2     rmind 		}
    246       1.1     rmind 	}
    247  1.39.2.1  christos 
    248       1.2     rmind pass:
    249      1.14     rmind 	decision = NPF_DECISION_PASS;
    250       1.2     rmind 	KASSERT(error == 0);
    251       1.5     rmind 	/*
    252       1.6     rmind 	 * Perform NAT.
    253       1.6     rmind 	 */
    254      1.32     rmind 	error = npf_do_nat(&npc, con, di);
    255  1.39.2.1  christos 
    256       1.6     rmind block:
    257       1.6     rmind 	/*
    258      1.22     rmind 	 * Execute the rule procedure, if any is associated.
    259      1.22     rmind 	 * It may reverse the decision from pass to block.
    260       1.5     rmind 	 */
    261      1.36  christos 	if (rp && !npf_rproc_run(&npc, rp, &mi, &decision)) {
    262      1.31     rmind 		if (con) {
    263      1.31     rmind 			npf_conn_release(con);
    264      1.30  jakllsch 		}
    265      1.30  jakllsch 		npf_rproc_release(rp);
    266  1.39.2.1  christos 		/* mbuf already freed */
    267      1.30  jakllsch 		return 0;
    268       1.5     rmind 	}
    269  1.39.2.1  christos 
    270       1.1     rmind out:
    271      1.13     rmind 	/*
    272      1.31     rmind 	 * Release the reference on a connection.  Release the reference
    273      1.31     rmind 	 * on a rule procedure only if there was no association.
    274      1.13     rmind 	 */
    275      1.31     rmind 	if (con) {
    276      1.31     rmind 		npf_conn_release(con);
    277       1.6     rmind 	} else if (rp) {
    278      1.13     rmind 		npf_rproc_release(rp);
    279       1.1     rmind 	}
    280       1.1     rmind 
    281  1.39.2.1  christos 	/* Get the new mbuf pointer. */
    282      1.24     rmind 	if ((*mp = nbuf_head_mbuf(&nbuf)) == NULL) {
    283      1.25     rmind 		return error ? error : ENOMEM;
    284      1.24     rmind 	}
    285      1.24     rmind 
    286      1.14     rmind 	/* Pass the packet if decided and there is no error. */
    287      1.14     rmind 	if (decision == NPF_DECISION_PASS && !error) {
    288       1.3     rmind 		/*
    289       1.3     rmind 		 * XXX: Disable for now, it will be set accordingly later,
    290       1.3     rmind 		 * for optimisations (to reduce inspection).
    291       1.3     rmind 		 */
    292      1.35  christos 		m_clear_flag(*mp, M_CANFASTFWD);
    293      1.13     rmind 		return 0;
    294       1.1     rmind 	}
    295      1.13     rmind 
    296      1.13     rmind 	/*
    297      1.13     rmind 	 * Block the packet.  ENETUNREACH is used to indicate blocking.
    298      1.13     rmind 	 * Depending on the flags and protocol, return TCP reset (RST) or
    299      1.13     rmind 	 * ICMP destination unreachable.
    300      1.13     rmind 	 */
    301      1.36  christos 	if (mi.mi_retfl && npf_return_block(&npc, mi.mi_retfl)) {
    302      1.16     rmind 		*mp = NULL;
    303      1.13     rmind 	}
    304      1.16     rmind 
    305      1.20     rmind 	if (!error) {
    306      1.14     rmind 		error = ENETUNREACH;
    307      1.13     rmind 	}
    308      1.13     rmind 
    309      1.16     rmind 	if (*mp) {
    310      1.35  christos 		/* Free the mbuf chain. */
    311      1.16     rmind 		m_freem(*mp);
    312      1.16     rmind 		*mp = NULL;
    313      1.16     rmind 	}
    314       1.1     rmind 	return error;
    315       1.1     rmind }
    316