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