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