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