Home | History | Annotate | Line # | Download | only in npf
npf_conn.c revision 1.32
      1   1.1     rmind /*-
      2  1.32     rmind  * Copyright (c) 2014-2020 Mindaugas Rasiukevicius <rmind at noxt eu>
      3   1.1     rmind  * Copyright (c) 2010-2014 The NetBSD Foundation, Inc.
      4   1.1     rmind  * All rights reserved.
      5   1.1     rmind  *
      6   1.1     rmind  * This material is based upon work partially supported by The
      7   1.1     rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      8   1.1     rmind  *
      9   1.1     rmind  * Redistribution and use in source and binary forms, with or without
     10   1.1     rmind  * modification, are permitted provided that the following conditions
     11   1.1     rmind  * are met:
     12   1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     13   1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     14   1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     16   1.1     rmind  *    documentation and/or other materials provided with the distribution.
     17   1.1     rmind  *
     18   1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19   1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20   1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21   1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22   1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23   1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24   1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25   1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26   1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27   1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28   1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     29   1.1     rmind  */
     30   1.1     rmind 
     31   1.1     rmind /*
     32   1.1     rmind  * NPF connection tracking for stateful filtering and translation.
     33   1.1     rmind  *
     34   1.1     rmind  * Overview
     35   1.1     rmind  *
     36  1.26     rmind  *	Packets can be incoming or outgoing with respect to an interface.
     37   1.1     rmind  *	Connection direction is identified by the direction of its first
     38  1.26     rmind  *	packet.  The meaning of incoming/outgoing packet in the context of
     39  1.26     rmind  *	connection direction can be confusing.  Therefore, we will use the
     40  1.26     rmind  *	terms "forwards stream" and "backwards stream", where packets in
     41  1.26     rmind  *	the forwards stream mean the packets travelling in the direction
     42  1.26     rmind  *	as the connection direction.
     43  1.26     rmind  *
     44  1.26     rmind  *	All connections have two keys and thus two entries:
     45   1.1     rmind  *
     46  1.27     rmind  *	- npf_conn_getforwkey(con)        -- for the forwards stream;
     47  1.27     rmind  *	- npf_conn_getbackkey(con, alen)  -- for the backwards stream.
     48  1.27     rmind  *
     49  1.27     rmind  *	Note: the keys are stored in npf_conn_t::c_keys[], which is used
     50  1.27     rmind  *	to allocate variable-length npf_conn_t structures based on whether
     51  1.32     rmind  *	the IPv4 or IPv6 addresses are used.
     52   1.1     rmind  *
     53  1.32     rmind  *	The key is an n-tuple used to identify the connection flow: see the
     54  1.32     rmind  *	npf_connkey.c source file for the description of the key layouts.
     55  1.32     rmind  *	The key may be formed using translated values in a case of NAT.
     56   1.1     rmind  *
     57  1.32     rmind  *	Connections can serve two purposes: for the implicit passing and/or
     58   1.1     rmind  *	to accommodate the dynamic NAT.  Connections for the former purpose
     59   1.1     rmind  *	are created by the rules with "stateful" attribute and are used for
     60   1.1     rmind  *	stateful filtering.  Such connections indicate that the packet of
     61   1.1     rmind  *	the backwards stream should be passed without inspection of the
     62   1.1     rmind  *	ruleset.  The other purpose is to associate a dynamic NAT mechanism
     63   1.1     rmind  *	with a connection.  Such connections are created by the NAT policies
     64   1.1     rmind  *	and they have a relationship with NAT translation structure via
     65   1.1     rmind  *	npf_conn_t::c_nat.  A single connection can serve both purposes,
     66   1.1     rmind  *	which is a common case.
     67   1.1     rmind  *
     68   1.1     rmind  * Connection life-cycle
     69   1.1     rmind  *
     70   1.1     rmind  *	Connections are established when a packet matches said rule or
     71   1.1     rmind  *	NAT policy.  Both keys of the established connection are inserted
     72   1.1     rmind  *	into the connection database.  A garbage collection thread
     73   1.1     rmind  *	periodically scans all connections and depending on connection
     74   1.1     rmind  *	properties (e.g. last activity time, protocol) removes connection
     75   1.1     rmind  *	entries and expires the actual connections.
     76   1.1     rmind  *
     77   1.1     rmind  *	Each connection has a reference count.  The reference is acquired
     78   1.1     rmind  *	on lookup and should be released by the caller.  It guarantees that
     79   1.1     rmind  *	the connection will not be destroyed, although it may be expired.
     80   1.1     rmind  *
     81  1.32     rmind  * Synchronization
     82   1.1     rmind  *
     83  1.32     rmind  *	Connection database is accessed in a lock-free manner by the main
     84   1.1     rmind  *	routines: npf_conn_inspect() and npf_conn_establish().  Since they
     85   1.1     rmind  *	are always called from a software interrupt, the database is
     86  1.32     rmind  *	protected using EBR.  The main place which can destroy a connection
     87  1.32     rmind  *	is npf_conn_worker().  The database itself can be replaced and
     88  1.32     rmind  *	destroyed in npf_conn_reload().
     89   1.1     rmind  *
     90   1.1     rmind  * ALG support
     91   1.1     rmind  *
     92   1.1     rmind  *	Application-level gateways (ALGs) can override generic connection
     93   1.1     rmind  *	inspection (npf_alg_conn() call in npf_conn_inspect() function) by
     94   1.1     rmind  *	performing their own lookup using different key.  Recursive call
     95   1.1     rmind  *	to npf_conn_inspect() is not allowed.  The ALGs ought to use the
     96   1.1     rmind  *	npf_conn_lookup() function for this purpose.
     97   1.1     rmind  *
     98   1.1     rmind  * Lock order
     99   1.1     rmind  *
    100   1.6     rmind  *	npf_config_lock ->
    101   1.6     rmind  *		conn_lock ->
    102   1.6     rmind  *			npf_conn_t::c_lock
    103   1.1     rmind  */
    104   1.1     rmind 
    105  1.22  christos #ifdef _KERNEL
    106   1.1     rmind #include <sys/cdefs.h>
    107  1.32     rmind __KERNEL_RCSID(0, "$NetBSD: npf_conn.c,v 1.32 2020/05/30 14:16:56 rmind Exp $");
    108   1.1     rmind 
    109   1.1     rmind #include <sys/param.h>
    110   1.1     rmind #include <sys/types.h>
    111   1.1     rmind 
    112   1.1     rmind #include <netinet/in.h>
    113   1.1     rmind #include <netinet/tcp.h>
    114   1.1     rmind 
    115   1.1     rmind #include <sys/atomic.h>
    116   1.1     rmind #include <sys/kmem.h>
    117   1.1     rmind #include <sys/mutex.h>
    118   1.1     rmind #include <net/pfil.h>
    119   1.1     rmind #include <sys/pool.h>
    120   1.1     rmind #include <sys/queue.h>
    121   1.1     rmind #include <sys/systm.h>
    122  1.22  christos #endif
    123   1.1     rmind 
    124   1.1     rmind #define __NPF_CONN_PRIVATE
    125   1.1     rmind #include "npf_conn.h"
    126   1.1     rmind #include "npf_impl.h"
    127   1.1     rmind 
    128  1.27     rmind /* A helper to select the IPv4 or IPv6 connection cache. */
    129  1.27     rmind #define	NPF_CONNCACHE(alen)	(((alen) >> 4) & 0x1)
    130  1.27     rmind 
    131   1.1     rmind /*
    132   1.1     rmind  * Connection flags: PFIL_IN and PFIL_OUT values are reserved for direction.
    133   1.1     rmind  */
    134   1.1     rmind CTASSERT(PFIL_ALL == (0x001 | 0x002));
    135   1.1     rmind #define	CONN_ACTIVE	0x004	/* visible on inspection */
    136   1.1     rmind #define	CONN_PASS	0x008	/* perform implicit passing */
    137   1.1     rmind #define	CONN_EXPIRE	0x010	/* explicitly expire */
    138   1.1     rmind #define	CONN_REMOVED	0x020	/* "forw/back" entries removed */
    139   1.1     rmind 
    140   1.6     rmind enum { CONN_TRACKING_OFF, CONN_TRACKING_ON };
    141   1.1     rmind 
    142  1.32     rmind static int	npf_conn_export(npf_t *, npf_conn_t *, nvlist_t *);
    143   1.1     rmind 
    144   1.1     rmind /*
    145  1.32     rmind  * npf_conn_sys{init,fini}: initialize/destroy connection tracking.
    146   1.1     rmind  */
    147   1.1     rmind 
    148   1.1     rmind void
    149  1.29  christos npf_conn_init(npf_t *npf)
    150   1.1     rmind {
    151  1.32     rmind 	npf_conn_params_t *params = npf_param_allocgroup(npf,
    152  1.32     rmind 	    NPF_PARAMS_CONN, sizeof(npf_conn_params_t));
    153  1.32     rmind 	npf_param_t param_map[] = {
    154  1.32     rmind 		{
    155  1.32     rmind 			"state.key.interface",
    156  1.32     rmind 			&params->connkey_interface,
    157  1.32     rmind 			.default_val = 1, // true
    158  1.32     rmind 			.min = 0, .max = 1
    159  1.32     rmind 		},
    160  1.32     rmind 		{
    161  1.32     rmind 			"state.key.direction",
    162  1.32     rmind 			&params->connkey_direction,
    163  1.32     rmind 			.default_val = 1, // true
    164  1.32     rmind 			.min = 0, .max = 1
    165  1.32     rmind 		},
    166  1.32     rmind 	};
    167  1.32     rmind 	npf_param_register(npf, param_map, __arraycount(param_map));
    168  1.32     rmind 
    169  1.27     rmind 	npf->conn_cache[0] = pool_cache_init(
    170  1.27     rmind 	    offsetof(npf_conn_t, c_keys[NPF_CONNKEY_V4WORDS * 2]),
    171  1.27     rmind 	    0, 0, 0, "npfcn4pl", NULL, IPL_NET, NULL, NULL, NULL);
    172  1.27     rmind 	npf->conn_cache[1] = pool_cache_init(
    173  1.27     rmind 	    offsetof(npf_conn_t, c_keys[NPF_CONNKEY_V6WORDS * 2]),
    174  1.27     rmind 	    0, 0, 0, "npfcn6pl", NULL, IPL_NET, NULL, NULL, NULL);
    175  1.27     rmind 
    176  1.22  christos 	mutex_init(&npf->conn_lock, MUTEX_DEFAULT, IPL_NONE);
    177  1.32     rmind 	atomic_store_relaxed(&npf->conn_tracking, CONN_TRACKING_OFF);
    178  1.22  christos 	npf->conn_db = npf_conndb_create();
    179  1.27     rmind 	npf_conndb_sysinit(npf);
    180  1.32     rmind 
    181  1.32     rmind 	npf_worker_addfunc(npf, npf_conn_worker);
    182   1.1     rmind }
    183   1.1     rmind 
    184   1.1     rmind void
    185  1.22  christos npf_conn_fini(npf_t *npf)
    186   1.1     rmind {
    187  1.32     rmind 	const size_t len = sizeof(npf_conn_params_t);
    188  1.27     rmind 
    189   1.6     rmind 	/* Note: the caller should have flushed the connections. */
    190  1.32     rmind 	KASSERT(atomic_load_relaxed(&npf->conn_tracking) == CONN_TRACKING_OFF);
    191   1.1     rmind 
    192  1.22  christos 	npf_conndb_destroy(npf->conn_db);
    193  1.27     rmind 	pool_cache_destroy(npf->conn_cache[0]);
    194  1.27     rmind 	pool_cache_destroy(npf->conn_cache[1]);
    195  1.22  christos 	mutex_destroy(&npf->conn_lock);
    196  1.32     rmind 
    197  1.32     rmind 	npf_param_freegroup(npf, NPF_PARAMS_CONN, len);
    198  1.32     rmind 	npf_conndb_sysfini(npf);
    199   1.1     rmind }
    200   1.1     rmind 
    201   1.1     rmind /*
    202   1.6     rmind  * npf_conn_load: perform the load by flushing the current connection
    203   1.6     rmind  * database and replacing it with the new one or just destroying.
    204   1.1     rmind  *
    205   1.6     rmind  * => The caller must disable the connection tracking and ensure that
    206   1.6     rmind  *    there are no connection database lookups or references in-flight.
    207   1.1     rmind  */
    208   1.6     rmind void
    209  1.22  christos npf_conn_load(npf_t *npf, npf_conndb_t *ndb, bool track)
    210   1.1     rmind {
    211   1.6     rmind 	npf_conndb_t *odb = NULL;
    212   1.1     rmind 
    213  1.22  christos 	KASSERT(npf_config_locked_p(npf));
    214   1.1     rmind 
    215   1.1     rmind 	/*
    216   1.6     rmind 	 * The connection database is in the quiescent state.
    217   1.6     rmind 	 * Prevent G/C thread from running and install a new database.
    218   1.1     rmind 	 */
    219  1.22  christos 	mutex_enter(&npf->conn_lock);
    220   1.6     rmind 	if (ndb) {
    221  1.32     rmind 		KASSERT(atomic_load_relaxed(&npf->conn_tracking)
    222  1.32     rmind 		    == CONN_TRACKING_OFF);
    223  1.32     rmind 		odb = atomic_load_relaxed(&npf->conn_db);
    224   1.6     rmind 		membar_sync();
    225  1.32     rmind 		atomic_store_relaxed(&npf->conn_db, ndb);
    226   1.6     rmind 	}
    227   1.6     rmind 	if (track) {
    228   1.6     rmind 		/* After this point lookups start flying in. */
    229  1.32     rmind 		membar_producer();
    230  1.32     rmind 		atomic_store_relaxed(&npf->conn_tracking, CONN_TRACKING_ON);
    231   1.1     rmind 	}
    232  1.22  christos 	mutex_exit(&npf->conn_lock);
    233   1.1     rmind 
    234   1.1     rmind 	if (odb) {
    235   1.6     rmind 		/*
    236   1.6     rmind 		 * Flush all, no sync since the caller did it for us.
    237   1.6     rmind 		 * Also, release the pool cache memory.
    238   1.6     rmind 		 */
    239  1.26     rmind 		npf_conndb_gc(npf, odb, true, false);
    240   1.1     rmind 		npf_conndb_destroy(odb);
    241  1.27     rmind 		pool_cache_invalidate(npf->conn_cache[0]);
    242  1.27     rmind 		pool_cache_invalidate(npf->conn_cache[1]);
    243   1.1     rmind 	}
    244   1.1     rmind }
    245   1.1     rmind 
    246   1.1     rmind /*
    247   1.1     rmind  * npf_conn_tracking: enable/disable connection tracking.
    248   1.1     rmind  */
    249   1.1     rmind void
    250  1.22  christos npf_conn_tracking(npf_t *npf, bool track)
    251   1.1     rmind {
    252  1.22  christos 	KASSERT(npf_config_locked_p(npf));
    253  1.32     rmind 	atomic_store_relaxed(&npf->conn_tracking,
    254  1.32     rmind 	    track ? CONN_TRACKING_ON : CONN_TRACKING_OFF);
    255   1.1     rmind }
    256   1.1     rmind 
    257   1.6     rmind static inline bool
    258   1.1     rmind npf_conn_trackable_p(const npf_cache_t *npc)
    259   1.1     rmind {
    260  1.22  christos 	const npf_t *npf = npc->npc_ctx;
    261  1.22  christos 
    262   1.1     rmind 	/*
    263   1.1     rmind 	 * Check if connection tracking is on.  Also, if layer 3 and 4 are
    264   1.1     rmind 	 * not cached - protocol is not supported or packet is invalid.
    265   1.1     rmind 	 */
    266  1.32     rmind 	if (atomic_load_relaxed(&npf->conn_tracking) != CONN_TRACKING_ON) {
    267   1.1     rmind 		return false;
    268   1.1     rmind 	}
    269   1.1     rmind 	if (!npf_iscached(npc, NPC_IP46) || !npf_iscached(npc, NPC_LAYER4)) {
    270   1.1     rmind 		return false;
    271   1.1     rmind 	}
    272   1.1     rmind 	return true;
    273   1.1     rmind }
    274   1.1     rmind 
    275  1.22  christos static inline void
    276  1.22  christos conn_update_atime(npf_conn_t *con)
    277  1.22  christos {
    278  1.22  christos 	struct timespec tsnow;
    279  1.22  christos 
    280  1.22  christos 	getnanouptime(&tsnow);
    281  1.32     rmind 	atomic_store_relaxed(&con->c_atime, tsnow.tv_sec);
    282  1.22  christos }
    283  1.22  christos 
    284   1.1     rmind /*
    285  1.27     rmind  * npf_conn_check: check that:
    286  1.27     rmind  *
    287  1.27     rmind  *	- the connection is active;
    288  1.27     rmind  *
    289  1.27     rmind  *	- the packet is travelling in the right direction with the respect
    290  1.27     rmind  *	  to the connection direction (if interface-id is not zero);
    291  1.27     rmind  *
    292  1.27     rmind  *	- the packet is travelling on the same interface as the
    293  1.27     rmind  *	  connection interface (if interface-id is not zero).
    294  1.18  christos  */
    295  1.18  christos static bool
    296  1.27     rmind npf_conn_check(const npf_conn_t *con, const nbuf_t *nbuf,
    297  1.32     rmind     const unsigned di, const npf_flow_t flow)
    298  1.18  christos {
    299  1.32     rmind 	const uint32_t flags = atomic_load_relaxed(&con->c_flags);
    300  1.32     rmind 	const unsigned ifid = atomic_load_relaxed(&con->c_ifid);
    301  1.32     rmind 	bool active;
    302  1.18  christos 
    303  1.27     rmind 	active = (flags & (CONN_ACTIVE | CONN_EXPIRE)) == CONN_ACTIVE;
    304  1.27     rmind 	if (__predict_false(!active)) {
    305  1.18  christos 		return false;
    306  1.18  christos 	}
    307  1.27     rmind 	if (ifid && nbuf) {
    308  1.32     rmind 		const bool match = (flags & PFIL_ALL) == di;
    309  1.32     rmind 		npf_flow_t pflow = match ? NPF_FLOW_FORW : NPF_FLOW_BACK;
    310  1.32     rmind 
    311  1.32     rmind 		if (__predict_false(flow != pflow)) {
    312  1.27     rmind 			return false;
    313  1.27     rmind 		}
    314  1.27     rmind 		if (__predict_false(ifid != nbuf->nb_ifid)) {
    315  1.27     rmind 			return false;
    316  1.27     rmind 		}
    317  1.18  christos 	}
    318  1.18  christos 	return true;
    319  1.18  christos }
    320  1.18  christos 
    321  1.18  christos /*
    322   1.1     rmind  * npf_conn_lookup: lookup if there is an established connection.
    323   1.1     rmind  *
    324   1.1     rmind  * => If found, we will hold a reference for the caller.
    325   1.1     rmind  */
    326   1.1     rmind npf_conn_t *
    327  1.32     rmind npf_conn_lookup(const npf_cache_t *npc, const unsigned di, npf_flow_t *flow)
    328   1.1     rmind {
    329  1.22  christos 	npf_t *npf = npc->npc_ctx;
    330   1.4     rmind 	const nbuf_t *nbuf = npc->npc_nbuf;
    331   1.1     rmind 	npf_conn_t *con;
    332   1.1     rmind 	npf_connkey_t key;
    333   1.1     rmind 
    334   1.1     rmind 	/* Construct a key and lookup for a connection in the store. */
    335  1.32     rmind 	if (!npf_conn_conkey(npc, &key, di, NPF_FLOW_FORW)) {
    336   1.1     rmind 		return NULL;
    337   1.1     rmind 	}
    338  1.32     rmind 	con = npf_conndb_lookup(npf, &key, flow);
    339   1.1     rmind 	if (con == NULL) {
    340   1.1     rmind 		return NULL;
    341   1.1     rmind 	}
    342  1.32     rmind 	KASSERT(npc->npc_proto == atomic_load_relaxed(&con->c_proto));
    343   1.1     rmind 
    344  1.27     rmind 	/* Extra checks for the connection and packet. */
    345  1.32     rmind 	if (!npf_conn_check(con, nbuf, di, *flow)) {
    346   1.1     rmind 		atomic_dec_uint(&con->c_refcnt);
    347   1.1     rmind 		return NULL;
    348   1.1     rmind 	}
    349   1.1     rmind 
    350   1.1     rmind 	/* Update the last activity time. */
    351  1.22  christos 	conn_update_atime(con);
    352   1.1     rmind 	return con;
    353   1.1     rmind }
    354   1.1     rmind 
    355   1.1     rmind /*
    356   1.1     rmind  * npf_conn_inspect: lookup a connection and inspecting the protocol data.
    357   1.1     rmind  *
    358   1.1     rmind  * => If found, we will hold a reference for the caller.
    359   1.1     rmind  */
    360   1.1     rmind npf_conn_t *
    361  1.32     rmind npf_conn_inspect(npf_cache_t *npc, const unsigned di, int *error)
    362   1.1     rmind {
    363   1.4     rmind 	nbuf_t *nbuf = npc->npc_nbuf;
    364  1.32     rmind 	npf_flow_t flow;
    365   1.1     rmind 	npf_conn_t *con;
    366  1.32     rmind 	bool ok;
    367   1.1     rmind 
    368   1.1     rmind 	KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    369   1.1     rmind 	if (!npf_conn_trackable_p(npc)) {
    370   1.1     rmind 		return NULL;
    371   1.1     rmind 	}
    372   1.1     rmind 
    373   1.1     rmind 	/* Query ALG which may lookup connection for us. */
    374   1.4     rmind 	if ((con = npf_alg_conn(npc, di)) != NULL) {
    375   1.1     rmind 		/* Note: reference is held. */
    376   1.1     rmind 		return con;
    377   1.1     rmind 	}
    378   1.1     rmind 	if (nbuf_head_mbuf(nbuf) == NULL) {
    379   1.1     rmind 		*error = ENOMEM;
    380   1.1     rmind 		return NULL;
    381   1.1     rmind 	}
    382   1.1     rmind 	KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    383   1.1     rmind 
    384  1.32     rmind 	/* The main lookup of the connection (acquires a reference). */
    385  1.32     rmind 	if ((con = npf_conn_lookup(npc, di, &flow)) == NULL) {
    386   1.1     rmind 		return NULL;
    387   1.1     rmind 	}
    388   1.1     rmind 
    389   1.1     rmind 	/* Inspect the protocol data and handle state changes. */
    390   1.1     rmind 	mutex_enter(&con->c_lock);
    391  1.32     rmind 	ok = npf_state_inspect(npc, &con->c_state, flow);
    392   1.1     rmind 	mutex_exit(&con->c_lock);
    393   1.1     rmind 
    394  1.17     rmind 	/* If invalid state: let the rules deal with it. */
    395   1.1     rmind 	if (__predict_false(!ok)) {
    396   1.1     rmind 		npf_conn_release(con);
    397  1.22  christos 		npf_stats_inc(npc->npc_ctx, NPF_STAT_INVALID_STATE);
    398  1.17     rmind 		return NULL;
    399  1.17     rmind 	}
    400  1.32     rmind #if 0
    401  1.17     rmind 	/*
    402  1.32     rmind 	 * TODO -- determine when this might be wanted/used.
    403  1.32     rmind 	 *
    404  1.32     rmind 	 * Note: skipping the connection lookup and ruleset inspection
    405  1.32     rmind 	 * on other interfaces will also bypass dynamic NAT.
    406  1.17     rmind 	 */
    407  1.32     rmind 	if (atomic_load_relaxed(&con->c_flags) & CONN_GPASS) {
    408  1.32     rmind 		/*
    409  1.32     rmind 		 * Note: if tagging fails, then give this packet a chance
    410  1.32     rmind 		 * to go through a regular ruleset.
    411  1.32     rmind 		 */
    412  1.32     rmind 		(void)nbuf_add_tag(nbuf, NPF_NTAG_PASS);
    413   1.1     rmind 	}
    414  1.32     rmind #endif
    415   1.1     rmind 	return con;
    416   1.1     rmind }
    417   1.1     rmind 
    418   1.1     rmind /*
    419   1.1     rmind  * npf_conn_establish: create a new connection, insert into the global list.
    420   1.1     rmind  *
    421   1.1     rmind  * => Connection is created with the reference held for the caller.
    422   1.1     rmind  * => Connection will be activated on the first reference release.
    423   1.1     rmind  */
    424   1.1     rmind npf_conn_t *
    425  1.32     rmind npf_conn_establish(npf_cache_t *npc, const unsigned di, bool global)
    426   1.1     rmind {
    427  1.22  christos 	npf_t *npf = npc->npc_ctx;
    428  1.27     rmind 	const unsigned alen = npc->npc_alen;
    429  1.27     rmind 	const unsigned idx = NPF_CONNCACHE(alen);
    430   1.4     rmind 	const nbuf_t *nbuf = npc->npc_nbuf;
    431  1.27     rmind 	npf_connkey_t *fw, *bk;
    432  1.32     rmind 	npf_conndb_t *conn_db;
    433   1.1     rmind 	npf_conn_t *con;
    434  1.15     rmind 	int error = 0;
    435   1.1     rmind 
    436   1.1     rmind 	KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    437   1.1     rmind 
    438   1.1     rmind 	if (!npf_conn_trackable_p(npc)) {
    439   1.1     rmind 		return NULL;
    440   1.1     rmind 	}
    441   1.1     rmind 
    442  1.32     rmind 	/* Allocate and initialize the new connection. */
    443  1.27     rmind 	con = pool_cache_get(npf->conn_cache[idx], PR_NOWAIT);
    444   1.1     rmind 	if (__predict_false(!con)) {
    445  1.22  christos 		npf_worker_signal(npf);
    446   1.1     rmind 		return NULL;
    447   1.1     rmind 	}
    448   1.1     rmind 	NPF_PRINTF(("NPF: create conn %p\n", con));
    449  1.22  christos 	npf_stats_inc(npf, NPF_STAT_CONN_CREATE);
    450   1.1     rmind 
    451   1.1     rmind 	mutex_init(&con->c_lock, MUTEX_DEFAULT, IPL_SOFTNET);
    452  1.32     rmind 	atomic_store_relaxed(&con->c_flags, di & PFIL_ALL);
    453  1.32     rmind 	atomic_store_relaxed(&con->c_refcnt, 0);
    454   1.1     rmind 	con->c_rproc = NULL;
    455   1.1     rmind 	con->c_nat = NULL;
    456   1.1     rmind 
    457  1.27     rmind 	con->c_proto = npc->npc_proto;
    458  1.27     rmind 	CTASSERT(sizeof(con->c_proto) >= sizeof(npc->npc_proto));
    459  1.29  christos 	con->c_alen = alen;
    460  1.27     rmind 
    461  1.15     rmind 	/* Initialize the protocol state. */
    462   1.4     rmind 	if (!npf_state_init(npc, &con->c_state)) {
    463  1.29  christos 		npf_conn_destroy(npf, con);
    464  1.15     rmind 		return NULL;
    465   1.1     rmind 	}
    466  1.27     rmind 	KASSERT(npf_iscached(npc, NPC_IP46));
    467   1.1     rmind 
    468  1.27     rmind 	fw = npf_conn_getforwkey(con);
    469  1.27     rmind 	bk = npf_conn_getbackkey(con, alen);
    470   1.1     rmind 
    471   1.1     rmind 	/*
    472   1.1     rmind 	 * Construct "forwards" and "backwards" keys.  Also, set the
    473   1.1     rmind 	 * interface ID for this connection (unless it is global).
    474   1.1     rmind 	 */
    475  1.32     rmind 	if (!npf_conn_conkey(npc, fw, di, NPF_FLOW_FORW) ||
    476  1.32     rmind 	    !npf_conn_conkey(npc, bk, di ^ PFIL_ALL, NPF_FLOW_BACK)) {
    477  1.29  christos 		npf_conn_destroy(npf, con);
    478  1.15     rmind 		return NULL;
    479   1.1     rmind 	}
    480  1.27     rmind 	con->c_ifid = global ? nbuf->nb_ifid : 0;
    481   1.1     rmind 
    482  1.15     rmind 	/*
    483  1.15     rmind 	 * Set last activity time for a new connection and acquire
    484  1.15     rmind 	 * a reference for the caller before we make it visible.
    485  1.15     rmind 	 */
    486  1.22  christos 	conn_update_atime(con);
    487  1.32     rmind 	atomic_store_relaxed(&con->c_refcnt, 1);
    488   1.1     rmind 
    489   1.1     rmind 	/*
    490   1.1     rmind 	 * Insert both keys (entries representing directions) of the
    491  1.15     rmind 	 * connection.  At this point it becomes visible, but we activate
    492  1.15     rmind 	 * the connection later.
    493   1.1     rmind 	 */
    494  1.15     rmind 	mutex_enter(&con->c_lock);
    495  1.32     rmind 	conn_db = atomic_load_relaxed(&npf->conn_db);
    496  1.32     rmind 	if (!npf_conndb_insert(conn_db, fw, con, NPF_FLOW_FORW)) {
    497  1.15     rmind 		error = EISCONN;
    498   1.1     rmind 		goto err;
    499   1.1     rmind 	}
    500  1.32     rmind 	if (!npf_conndb_insert(conn_db, bk, con, NPF_FLOW_BACK)) {
    501  1.15     rmind 		npf_conn_t *ret __diagused;
    502  1.32     rmind 		ret = npf_conndb_remove(conn_db, fw);
    503  1.15     rmind 		KASSERT(ret == con);
    504  1.15     rmind 		error = EISCONN;
    505  1.15     rmind 		goto err;
    506  1.15     rmind 	}
    507  1.15     rmind err:
    508  1.15     rmind 	/*
    509  1.15     rmind 	 * If we have hit the duplicate: mark the connection as expired
    510  1.15     rmind 	 * and let the G/C thread to take care of it.  We cannot do it
    511  1.15     rmind 	 * here since there might be references acquired already.
    512  1.15     rmind 	 */
    513  1.15     rmind 	if (error) {
    514  1.16     rmind 		atomic_or_uint(&con->c_flags, CONN_REMOVED | CONN_EXPIRE);
    515  1.16     rmind 		atomic_dec_uint(&con->c_refcnt);
    516  1.22  christos 		npf_stats_inc(npf, NPF_STAT_RACE_CONN);
    517  1.15     rmind 	} else {
    518  1.15     rmind 		NPF_PRINTF(("NPF: establish conn %p\n", con));
    519   1.1     rmind 	}
    520   1.1     rmind 
    521   1.1     rmind 	/* Finally, insert into the connection list. */
    522  1.32     rmind 	npf_conndb_enqueue(conn_db, con);
    523  1.15     rmind 	mutex_exit(&con->c_lock);
    524  1.15     rmind 
    525  1.15     rmind 	return error ? NULL : con;
    526   1.1     rmind }
    527   1.1     rmind 
    528  1.26     rmind void
    529  1.22  christos npf_conn_destroy(npf_t *npf, npf_conn_t *con)
    530   1.1     rmind {
    531  1.29  christos 	const unsigned idx __unused = NPF_CONNCACHE(con->c_alen);
    532  1.27     rmind 
    533  1.32     rmind 	KASSERT(atomic_load_relaxed(&con->c_refcnt) == 0);
    534  1.15     rmind 
    535   1.1     rmind 	if (con->c_nat) {
    536   1.1     rmind 		/* Release any NAT structures. */
    537  1.32     rmind 		npf_nat_destroy(con, con->c_nat);
    538   1.1     rmind 	}
    539   1.1     rmind 	if (con->c_rproc) {
    540   1.1     rmind 		/* Release the rule procedure. */
    541   1.1     rmind 		npf_rproc_release(con->c_rproc);
    542   1.1     rmind 	}
    543   1.1     rmind 
    544   1.1     rmind 	/* Destroy the state. */
    545   1.1     rmind 	npf_state_destroy(&con->c_state);
    546   1.1     rmind 	mutex_destroy(&con->c_lock);
    547   1.1     rmind 
    548   1.1     rmind 	/* Free the structure, increase the counter. */
    549  1.27     rmind 	pool_cache_put(npf->conn_cache[idx], con);
    550  1.22  christos 	npf_stats_inc(npf, NPF_STAT_CONN_DESTROY);
    551   1.1     rmind 	NPF_PRINTF(("NPF: conn %p destroyed\n", con));
    552   1.1     rmind }
    553   1.1     rmind 
    554   1.1     rmind /*
    555   1.1     rmind  * npf_conn_setnat: associate NAT entry with the connection, update and
    556   1.1     rmind  * re-insert connection entry using the translation values.
    557  1.16     rmind  *
    558  1.16     rmind  * => The caller must be holding a reference.
    559   1.1     rmind  */
    560   1.1     rmind int
    561   1.1     rmind npf_conn_setnat(const npf_cache_t *npc, npf_conn_t *con,
    562  1.27     rmind     npf_nat_t *nt, unsigned ntype)
    563   1.1     rmind {
    564  1.32     rmind 	static const unsigned nat_type_which[] = {
    565  1.32     rmind 		/* See the description in npf_nat_which(). */
    566   1.1     rmind 		[NPF_NATOUT] = NPF_DST,
    567   1.1     rmind 		[NPF_NATIN] = NPF_SRC,
    568   1.1     rmind 	};
    569  1.22  christos 	npf_t *npf = npc->npc_ctx;
    570   1.2     rmind 	npf_conn_t *ret __diagused;
    571  1.32     rmind 	npf_conndb_t *conn_db;
    572  1.32     rmind 	npf_connkey_t *bk;
    573   1.1     rmind 	npf_addr_t *taddr;
    574   1.1     rmind 	in_port_t tport;
    575  1.32     rmind 	uint32_t flags;
    576   1.1     rmind 
    577  1.32     rmind 	KASSERT(atomic_load_relaxed(&con->c_refcnt) > 0);
    578   1.1     rmind 
    579   1.1     rmind 	npf_nat_gettrans(nt, &taddr, &tport);
    580   1.1     rmind 	KASSERT(ntype == NPF_NATOUT || ntype == NPF_NATIN);
    581   1.1     rmind 
    582   1.1     rmind 	/* Acquire the lock and check for the races. */
    583   1.1     rmind 	mutex_enter(&con->c_lock);
    584  1.32     rmind 	flags = atomic_load_relaxed(&con->c_flags);
    585  1.32     rmind 	if (__predict_false(flags & CONN_EXPIRE)) {
    586   1.1     rmind 		/* The connection got expired. */
    587   1.1     rmind 		mutex_exit(&con->c_lock);
    588   1.1     rmind 		return EINVAL;
    589   1.1     rmind 	}
    590  1.32     rmind 	KASSERT((flags & CONN_REMOVED) == 0);
    591  1.15     rmind 
    592   1.1     rmind 	if (__predict_false(con->c_nat != NULL)) {
    593   1.1     rmind 		/* Race with a duplicate packet. */
    594   1.1     rmind 		mutex_exit(&con->c_lock);
    595  1.22  christos 		npf_stats_inc(npc->npc_ctx, NPF_STAT_RACE_NAT);
    596   1.1     rmind 		return EISCONN;
    597   1.1     rmind 	}
    598   1.1     rmind 
    599  1.27     rmind 	/* Remove the "backwards" key. */
    600  1.32     rmind 	conn_db = atomic_load_relaxed(&npf->conn_db);
    601  1.32     rmind 	bk = npf_conn_getbackkey(con, con->c_alen);
    602  1.32     rmind 	ret = npf_conndb_remove(conn_db, bk);
    603   1.1     rmind 	KASSERT(ret == con);
    604   1.1     rmind 
    605   1.1     rmind 	/* Set the source/destination IDs to the translation values. */
    606  1.32     rmind 	npf_conn_adjkey(bk, taddr, tport, nat_type_which[ntype]);
    607   1.1     rmind 
    608  1.27     rmind 	/* Finally, re-insert the "backwards" key. */
    609  1.32     rmind 	if (!npf_conndb_insert(conn_db, bk, con, NPF_FLOW_BACK)) {
    610   1.1     rmind 		/*
    611   1.1     rmind 		 * Race: we have hit the duplicate, remove the "forwards"
    612  1.27     rmind 		 * key and expire our connection; it is no longer valid.
    613   1.1     rmind 		 */
    614  1.32     rmind 		npf_connkey_t *fw = npf_conn_getforwkey(con);
    615  1.32     rmind 		ret = npf_conndb_remove(conn_db, fw);
    616  1.15     rmind 		KASSERT(ret == con);
    617  1.15     rmind 
    618   1.1     rmind 		atomic_or_uint(&con->c_flags, CONN_REMOVED | CONN_EXPIRE);
    619   1.1     rmind 		mutex_exit(&con->c_lock);
    620   1.1     rmind 
    621  1.22  christos 		npf_stats_inc(npc->npc_ctx, NPF_STAT_RACE_NAT);
    622   1.1     rmind 		return EISCONN;
    623   1.1     rmind 	}
    624   1.1     rmind 
    625   1.1     rmind 	/* Associate the NAT entry and release the lock. */
    626   1.1     rmind 	con->c_nat = nt;
    627   1.1     rmind 	mutex_exit(&con->c_lock);
    628   1.1     rmind 	return 0;
    629   1.1     rmind }
    630   1.1     rmind 
    631   1.1     rmind /*
    632   1.1     rmind  * npf_conn_expire: explicitly mark connection as expired.
    633  1.32     rmind  *
    634  1.32     rmind  * => Must be called with: a) reference held  b) the relevant lock held.
    635  1.32     rmind  *    The relevant lock should prevent from connection destruction, e.g.
    636  1.32     rmind  *    npf_t::conn_lock or npf_natpolicy_t::n_lock.
    637   1.1     rmind  */
    638   1.1     rmind void
    639   1.1     rmind npf_conn_expire(npf_conn_t *con)
    640   1.1     rmind {
    641   1.1     rmind 	atomic_or_uint(&con->c_flags, CONN_EXPIRE);
    642   1.1     rmind }
    643   1.1     rmind 
    644   1.1     rmind /*
    645   1.1     rmind  * npf_conn_pass: return true if connection is "pass" one, otherwise false.
    646   1.1     rmind  */
    647   1.1     rmind bool
    648  1.23  christos npf_conn_pass(const npf_conn_t *con, npf_match_info_t *mi, npf_rproc_t **rp)
    649   1.1     rmind {
    650  1.32     rmind 	KASSERT(atomic_load_relaxed(&con->c_refcnt) > 0);
    651  1.32     rmind 	if (__predict_true(atomic_load_relaxed(&con->c_flags) & CONN_PASS)) {
    652  1.32     rmind 		mi->mi_retfl = atomic_load_relaxed(&con->c_retfl);
    653  1.24     rmind 		mi->mi_rid = con->c_rid;
    654   1.1     rmind 		*rp = con->c_rproc;
    655   1.1     rmind 		return true;
    656   1.1     rmind 	}
    657   1.1     rmind 	return false;
    658   1.1     rmind }
    659   1.1     rmind 
    660   1.1     rmind /*
    661   1.1     rmind  * npf_conn_setpass: mark connection as a "pass" one and associate the
    662   1.1     rmind  * rule procedure with it.
    663   1.1     rmind  */
    664   1.1     rmind void
    665  1.23  christos npf_conn_setpass(npf_conn_t *con, const npf_match_info_t *mi, npf_rproc_t *rp)
    666   1.1     rmind {
    667  1.32     rmind 	KASSERT((atomic_load_relaxed(&con->c_flags) & CONN_ACTIVE) == 0);
    668  1.32     rmind 	KASSERT(atomic_load_relaxed(&con->c_refcnt) > 0);
    669   1.1     rmind 	KASSERT(con->c_rproc == NULL);
    670   1.1     rmind 
    671   1.1     rmind 	/*
    672   1.1     rmind 	 * No need for atomic since the connection is not yet active.
    673   1.1     rmind 	 * If rproc is set, the caller transfers its reference to us,
    674   1.1     rmind 	 * which will be released on npf_conn_destroy().
    675   1.1     rmind 	 */
    676  1.14     rmind 	atomic_or_uint(&con->c_flags, CONN_PASS);
    677   1.1     rmind 	con->c_rproc = rp;
    678  1.24     rmind 	if (rp) {
    679  1.24     rmind 		con->c_rid = mi->mi_rid;
    680  1.24     rmind 		con->c_retfl = mi->mi_retfl;
    681  1.24     rmind 	}
    682   1.1     rmind }
    683   1.1     rmind 
    684   1.1     rmind /*
    685   1.1     rmind  * npf_conn_release: release a reference, which might allow G/C thread
    686   1.1     rmind  * to destroy this connection.
    687   1.1     rmind  */
    688   1.1     rmind void
    689   1.1     rmind npf_conn_release(npf_conn_t *con)
    690   1.1     rmind {
    691  1.32     rmind 	const unsigned flags = atomic_load_relaxed(&con->c_flags);
    692  1.32     rmind 
    693  1.32     rmind 	if ((flags & (CONN_ACTIVE | CONN_EXPIRE)) == 0) {
    694   1.1     rmind 		/* Activate: after this, connection is globally visible. */
    695  1.14     rmind 		atomic_or_uint(&con->c_flags, CONN_ACTIVE);
    696   1.1     rmind 	}
    697  1.32     rmind 	KASSERT(atomic_load_relaxed(&con->c_refcnt) > 0);
    698   1.1     rmind 	atomic_dec_uint(&con->c_refcnt);
    699   1.1     rmind }
    700   1.1     rmind 
    701   1.1     rmind /*
    702  1.32     rmind  * npf_conn_getnat: return the associated NAT entry, if any.
    703   1.1     rmind  */
    704   1.1     rmind npf_nat_t *
    705  1.32     rmind npf_conn_getnat(const npf_conn_t *con)
    706   1.1     rmind {
    707   1.1     rmind 	return con->c_nat;
    708   1.1     rmind }
    709   1.1     rmind 
    710   1.1     rmind /*
    711   1.1     rmind  * npf_conn_expired: criterion to check if connection is expired.
    712   1.1     rmind  */
    713  1.26     rmind bool
    714  1.27     rmind npf_conn_expired(npf_t *npf, const npf_conn_t *con, uint64_t tsnow)
    715   1.1     rmind {
    716  1.32     rmind 	const unsigned flags = atomic_load_relaxed(&con->c_flags);
    717  1.27     rmind 	const int etime = npf_state_etime(npf, &con->c_state, con->c_proto);
    718  1.22  christos 	int elapsed;
    719   1.1     rmind 
    720  1.32     rmind 	if (__predict_false(flags & CONN_EXPIRE)) {
    721   1.1     rmind 		/* Explicitly marked to be expired. */
    722   1.1     rmind 		return true;
    723   1.1     rmind 	}
    724  1.22  christos 
    725  1.22  christos 	/*
    726  1.22  christos 	 * Note: another thread may update 'atime' and it might
    727  1.22  christos 	 * become greater than 'now'.
    728  1.22  christos 	 */
    729  1.32     rmind 	elapsed = (int64_t)tsnow - atomic_load_relaxed(&con->c_atime);
    730  1.22  christos 	return elapsed > etime;
    731   1.1     rmind }
    732   1.1     rmind 
    733   1.1     rmind /*
    734  1.26     rmind  * npf_conn_remove: unlink the connection and mark as expired.
    735   1.1     rmind  */
    736   1.7     rmind void
    737  1.26     rmind npf_conn_remove(npf_conndb_t *cd, npf_conn_t *con)
    738   1.1     rmind {
    739  1.26     rmind 	/* Remove both entries of the connection. */
    740  1.26     rmind 	mutex_enter(&con->c_lock);
    741  1.32     rmind 	if ((atomic_load_relaxed(&con->c_flags) & CONN_REMOVED) == 0) {
    742  1.27     rmind 		npf_connkey_t *fw, *bk;
    743  1.26     rmind 		npf_conn_t *ret __diagused;
    744   1.1     rmind 
    745  1.27     rmind 		fw = npf_conn_getforwkey(con);
    746  1.27     rmind 		ret = npf_conndb_remove(cd, fw);
    747  1.26     rmind 		KASSERT(ret == con);
    748  1.27     rmind 
    749  1.27     rmind 		bk = npf_conn_getbackkey(con, NPF_CONNKEY_ALEN(fw));
    750  1.27     rmind 		ret = npf_conndb_remove(cd, bk);
    751  1.26     rmind 		KASSERT(ret == con);
    752   1.1     rmind 	}
    753   1.6     rmind 
    754  1.26     rmind 	/* Flag the removal and expiration. */
    755  1.26     rmind 	atomic_or_uint(&con->c_flags, CONN_REMOVED | CONN_EXPIRE);
    756  1.26     rmind 	mutex_exit(&con->c_lock);
    757   1.1     rmind }
    758   1.1     rmind 
    759   1.6     rmind /*
    760  1.32     rmind  * npf_conn_worker: G/C to run from a worker thread or via npfk_gc().
    761   1.6     rmind  */
    762  1.22  christos void
    763  1.22  christos npf_conn_worker(npf_t *npf)
    764   1.1     rmind {
    765  1.32     rmind 	npf_conndb_t *conn_db = atomic_load_relaxed(&npf->conn_db);
    766  1.32     rmind 	npf_conndb_gc(npf, conn_db, false, true);
    767   1.1     rmind }
    768   1.1     rmind 
    769   1.1     rmind /*
    770  1.10     rmind  * npf_conndb_export: construct a list of connections prepared for saving.
    771   1.1     rmind  * Note: this is expected to be an expensive operation.
    772   1.1     rmind  */
    773   1.1     rmind int
    774  1.32     rmind npf_conndb_export(npf_t *npf, nvlist_t *nvl)
    775   1.1     rmind {
    776  1.26     rmind 	npf_conn_t *head, *con;
    777  1.32     rmind 	npf_conndb_t *conn_db;
    778   1.1     rmind 
    779   1.1     rmind 	/*
    780   1.1     rmind 	 * Note: acquire conn_lock to prevent from the database
    781   1.1     rmind 	 * destruction and G/C thread.
    782   1.1     rmind 	 */
    783  1.22  christos 	mutex_enter(&npf->conn_lock);
    784  1.32     rmind 	if (atomic_load_relaxed(&npf->conn_tracking) != CONN_TRACKING_ON) {
    785  1.22  christos 		mutex_exit(&npf->conn_lock);
    786   1.1     rmind 		return 0;
    787   1.1     rmind 	}
    788  1.32     rmind 	conn_db = atomic_load_relaxed(&npf->conn_db);
    789  1.32     rmind 	head = npf_conndb_getlist(conn_db);
    790  1.26     rmind 	con = head;
    791   1.1     rmind 	while (con) {
    792  1.32     rmind 		nvlist_t *con_nvl;
    793   1.1     rmind 
    794  1.32     rmind 		con_nvl = nvlist_create(0);
    795  1.32     rmind 		if (npf_conn_export(npf, con, con_nvl) == 0) {
    796  1.32     rmind 			nvlist_append_nvlist_array(nvl, "conn-list", con_nvl);
    797   1.1     rmind 		}
    798  1.32     rmind 		nvlist_destroy(con_nvl);
    799  1.32     rmind 
    800  1.32     rmind 		if ((con = npf_conndb_getnext(conn_db, con)) == head) {
    801  1.26     rmind 			break;
    802  1.26     rmind 		}
    803   1.1     rmind 	}
    804  1.22  christos 	mutex_exit(&npf->conn_lock);
    805   1.5     joerg 	return 0;
    806   1.1     rmind }
    807   1.1     rmind 
    808   1.1     rmind /*
    809  1.32     rmind  * npf_conn_export: serialize a single connection.
    810  1.10     rmind  */
    811  1.32     rmind static int
    812  1.32     rmind npf_conn_export(npf_t *npf, npf_conn_t *con, nvlist_t *nvl)
    813  1.10     rmind {
    814  1.32     rmind 	nvlist_t *knvl;
    815  1.27     rmind 	npf_connkey_t *fw, *bk;
    816  1.32     rmind 	unsigned flags, alen;
    817  1.10     rmind 
    818  1.32     rmind 	flags = atomic_load_relaxed(&con->c_flags);
    819  1.32     rmind 	if ((flags & (CONN_ACTIVE|CONN_EXPIRE)) != CONN_ACTIVE) {
    820  1.32     rmind 		return ESRCH;
    821  1.10     rmind 	}
    822  1.32     rmind 	nvlist_add_number(nvl, "flags", flags);
    823  1.32     rmind 	nvlist_add_number(nvl, "proto", con->c_proto);
    824  1.10     rmind 	if (con->c_ifid) {
    825  1.30     rmind 		char ifname[IFNAMSIZ];
    826  1.30     rmind 		npf_ifmap_copyname(npf, con->c_ifid, ifname, sizeof(ifname));
    827  1.32     rmind 		nvlist_add_string(nvl, "ifname", ifname);
    828  1.10     rmind 	}
    829  1.32     rmind 	nvlist_add_binary(nvl, "state", &con->c_state, sizeof(npf_state_t));
    830  1.10     rmind 
    831  1.27     rmind 	fw = npf_conn_getforwkey(con);
    832  1.27     rmind 	alen = NPF_CONNKEY_ALEN(fw);
    833  1.29  christos 	KASSERT(alen == con->c_alen);
    834  1.27     rmind 	bk = npf_conn_getbackkey(con, alen);
    835  1.27     rmind 
    836  1.32     rmind 	knvl = npf_connkey_export(npf, fw);
    837  1.32     rmind 	nvlist_move_nvlist(nvl, "forw-key", knvl);
    838  1.10     rmind 
    839  1.32     rmind 	knvl = npf_connkey_export(npf, bk);
    840  1.32     rmind 	nvlist_move_nvlist(nvl, "back-key", knvl);
    841  1.10     rmind 
    842  1.27     rmind 	/* Let the address length be based on on first key. */
    843  1.32     rmind 	nvlist_add_number(nvl, "alen", alen);
    844  1.27     rmind 
    845  1.10     rmind 	if (con->c_nat) {
    846  1.32     rmind 		npf_nat_export(npf, con->c_nat, nvl);
    847  1.10     rmind 	}
    848  1.32     rmind 	return 0;
    849  1.10     rmind }
    850  1.10     rmind 
    851  1.10     rmind /*
    852   1.6     rmind  * npf_conn_import: fully reconstruct a single connection from a
    853  1.25     rmind  * nvlist and insert into the given database.
    854   1.1     rmind  */
    855   1.1     rmind int
    856  1.25     rmind npf_conn_import(npf_t *npf, npf_conndb_t *cd, const nvlist_t *cdict,
    857   1.6     rmind     npf_ruleset_t *natlist)
    858   1.1     rmind {
    859   1.1     rmind 	npf_conn_t *con;
    860   1.1     rmind 	npf_connkey_t *fw, *bk;
    861  1.25     rmind 	const nvlist_t *nat, *conkey;
    862  1.32     rmind 	unsigned flags, alen, idx;
    863  1.10     rmind 	const char *ifname;
    864  1.25     rmind 	const void *state;
    865  1.25     rmind 	size_t len;
    866   1.1     rmind 
    867  1.27     rmind 	/*
    868  1.27     rmind 	 * To determine the length of the connection, which depends
    869  1.27     rmind 	 * on the address length in the connection keys.
    870  1.27     rmind 	 */
    871  1.27     rmind 	alen = dnvlist_get_number(cdict, "alen", 0);
    872  1.27     rmind 	idx = NPF_CONNCACHE(alen);
    873  1.27     rmind 
    874  1.32     rmind 	/* Allocate a connection and initialize it (clear first). */
    875  1.27     rmind 	con = pool_cache_get(npf->conn_cache[idx], PR_WAITOK);
    876   1.1     rmind 	memset(con, 0, sizeof(npf_conn_t));
    877   1.1     rmind 	mutex_init(&con->c_lock, MUTEX_DEFAULT, IPL_SOFTNET);
    878  1.22  christos 	npf_stats_inc(npf, NPF_STAT_CONN_CREATE);
    879   1.1     rmind 
    880  1.25     rmind 	con->c_proto = dnvlist_get_number(cdict, "proto", 0);
    881  1.32     rmind 	flags = dnvlist_get_number(cdict, "flags", 0);
    882  1.32     rmind 	flags &= PFIL_ALL | CONN_ACTIVE | CONN_PASS;
    883  1.32     rmind 	atomic_store_relaxed(&con->c_flags, flags);
    884  1.22  christos 	conn_update_atime(con);
    885   1.1     rmind 
    886  1.25     rmind 	ifname = dnvlist_get_string(cdict, "ifname", NULL);
    887  1.25     rmind 	if (ifname && (con->c_ifid = npf_ifmap_register(npf, ifname)) == 0) {
    888  1.10     rmind 		goto err;
    889  1.10     rmind 	}
    890  1.10     rmind 
    891  1.25     rmind 	state = dnvlist_get_binary(cdict, "state", &len, NULL, 0);
    892  1.25     rmind 	if (!state || len != sizeof(npf_state_t)) {
    893   1.1     rmind 		goto err;
    894   1.1     rmind 	}
    895  1.25     rmind 	memcpy(&con->c_state, state, sizeof(npf_state_t));
    896   1.1     rmind 
    897  1.11     rmind 	/* Reconstruct NAT association, if any. */
    898  1.25     rmind 	if ((nat = dnvlist_get_nvlist(cdict, "nat", NULL)) != NULL &&
    899  1.25     rmind 	    (con->c_nat = npf_nat_import(npf, nat, natlist, con)) == NULL) {
    900  1.11     rmind 		goto err;
    901  1.11     rmind 	}
    902   1.1     rmind 
    903   1.1     rmind 	/*
    904   1.1     rmind 	 * Fetch and copy the keys for each direction.
    905   1.1     rmind 	 */
    906  1.27     rmind 	fw = npf_conn_getforwkey(con);
    907  1.25     rmind 	conkey = dnvlist_get_nvlist(cdict, "forw-key", NULL);
    908  1.32     rmind 	if (conkey == NULL || !npf_connkey_import(npf, conkey, fw)) {
    909   1.1     rmind 		goto err;
    910   1.1     rmind 	}
    911  1.27     rmind 	bk = npf_conn_getbackkey(con, NPF_CONNKEY_ALEN(fw));
    912  1.25     rmind 	conkey = dnvlist_get_nvlist(cdict, "back-key", NULL);
    913  1.32     rmind 	if (conkey == NULL || !npf_connkey_import(npf, conkey, bk)) {
    914   1.1     rmind 		goto err;
    915   1.1     rmind 	}
    916  1.27     rmind 
    917  1.27     rmind 	/* Guard against the contradicting address lengths. */
    918  1.27     rmind 	if (NPF_CONNKEY_ALEN(fw) != alen || NPF_CONNKEY_ALEN(bk) != alen) {
    919  1.27     rmind 		goto err;
    920  1.27     rmind 	}
    921   1.1     rmind 
    922   1.1     rmind 	/* Insert the entries and the connection itself. */
    923  1.32     rmind 	if (!npf_conndb_insert(cd, fw, con, NPF_FLOW_FORW)) {
    924   1.1     rmind 		goto err;
    925   1.1     rmind 	}
    926  1.32     rmind 	if (!npf_conndb_insert(cd, bk, con, NPF_FLOW_BACK)) {
    927   1.1     rmind 		npf_conndb_remove(cd, fw);
    928   1.1     rmind 		goto err;
    929   1.1     rmind 	}
    930  1.12     rmind 
    931  1.12     rmind 	NPF_PRINTF(("NPF: imported conn %p\n", con));
    932   1.1     rmind 	npf_conndb_enqueue(cd, con);
    933   1.1     rmind 	return 0;
    934   1.1     rmind err:
    935  1.29  christos 	npf_conn_destroy(npf, con);
    936   1.1     rmind 	return EINVAL;
    937   1.1     rmind }
    938   1.1     rmind 
    939  1.32     rmind /*
    940  1.32     rmind  * npf_conn_find: lookup a connection in the list of connections
    941  1.32     rmind  */
    942  1.20  christos int
    943  1.32     rmind npf_conn_find(npf_t *npf, const nvlist_t *req, nvlist_t *resp)
    944  1.20  christos {
    945  1.32     rmind 	const nvlist_t *key_nv;
    946  1.32     rmind 	npf_conn_t *con;
    947  1.20  christos 	npf_connkey_t key;
    948  1.32     rmind 	npf_flow_t flow;
    949  1.32     rmind 	int error;
    950  1.20  christos 
    951  1.32     rmind 	key_nv = dnvlist_get_nvlist(req, "key", NULL);
    952  1.32     rmind 	if (!key_nv || !npf_connkey_import(npf, key_nv, &key)) {
    953  1.20  christos 		return EINVAL;
    954  1.25     rmind 	}
    955  1.32     rmind 	con = npf_conndb_lookup(npf, &key, &flow);
    956  1.20  christos 	if (con == NULL) {
    957  1.20  christos 		return ESRCH;
    958  1.20  christos 	}
    959  1.32     rmind 	if (!npf_conn_check(con, NULL, 0, NPF_FLOW_FORW)) {
    960  1.20  christos 		atomic_dec_uint(&con->c_refcnt);
    961  1.20  christos 		return ESRCH;
    962  1.20  christos 	}
    963  1.32     rmind 	error = npf_conn_export(npf, con, resp);
    964  1.32     rmind 	nvlist_add_number(resp, "flow", flow);
    965  1.20  christos 	atomic_dec_uint(&con->c_refcnt);
    966  1.32     rmind 	return error;
    967  1.20  christos }
    968  1.20  christos 
    969   1.1     rmind #if defined(DDB) || defined(_NPF_TESTING)
    970   1.1     rmind 
    971   1.1     rmind void
    972  1.27     rmind npf_conn_print(npf_conn_t *con)
    973   1.1     rmind {
    974  1.27     rmind 	const npf_connkey_t *fw = npf_conn_getforwkey(con);
    975  1.27     rmind 	const npf_connkey_t *bk = npf_conn_getbackkey(con, NPF_CONNKEY_ALEN(fw));
    976  1.32     rmind 	const unsigned flags = atomic_load_relaxed(&con->c_flags);
    977  1.27     rmind 	const unsigned proto = con->c_proto;
    978  1.22  christos 	struct timespec tspnow;
    979   1.1     rmind 
    980  1.22  christos 	getnanouptime(&tspnow);
    981  1.22  christos 	printf("%p:\n\tproto %d flags 0x%x tsdiff %ld etime %d\n", con,
    982  1.32     rmind 	    proto, flags, (long)(tspnow.tv_sec - con->c_atime),
    983  1.27     rmind 	    npf_state_etime(npf_getkernctx(), &con->c_state, proto));
    984  1.27     rmind 	npf_connkey_print(fw);
    985  1.27     rmind 	npf_connkey_print(bk);
    986   1.1     rmind 	npf_state_dump(&con->c_state);
    987   1.1     rmind 	if (con->c_nat) {
    988   1.1     rmind 		npf_nat_dump(con->c_nat);
    989   1.1     rmind 	}
    990   1.1     rmind }
    991   1.1     rmind 
    992   1.1     rmind #endif
    993