/*-
* Copyright (c) 2014-2020 Mindaugas Rasiukevicius <rmind at netbsd org>
* Copyright (c) 2010-2014 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This material is based upon work partially supported by The
* NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Connection key -- is an n-tuple structure encoding the address length,
* layer 3 protocol, source and destination addresses and ports (or other
* protocol IDs) and some configurable elements (see below).
*
* Key layout
*
* The single key is formed out of 32-bit integers. The layout is
* as follows (first row -- fields, second row -- number of bits):
*
* | alen | proto | ckey | src-id | dst-id | src-addr | dst-addr |
* +------+-------+--------+--------+--------+----------+----------+
* | 4 | 8 | 20 | 16 | 16 | 32-128 | 32-128 |
*
* The source and destination are inverted if the key is for the
* backwards stream (NPF_FLOW_BACK). The address length depends on
* the 'alen' field. The length is in words and is either 1 or 4,
* meaning 4 or 16 in bytes.
*
* The 20-bit configurable key area ('ckey') is for the optional
* elements which may be included or excluded by the user. It has
* the following layout:
*
* | direction | interface-id |
* +-----------+--------------+
* | 2 | 18 |
*
* Note: neither direction nor interface ID cannot be zero; we rely
* on this by reserving the zero 'ckey' value to for the case when
* these checks are not applicable.
*
* Embedding in the connection structure (npf_conn_t)
*
* Two keys are stored in the npf_conn_t::c_keys[] array, which is
* variable-length, depending on whether the keys store IPv4 or IPv6
* addresses. The length of the first key determines the position
* of the second key.
*
* WARNING: the keys must be immutable while they are in conndb.
*/
/*
* npf_conn_getforwkey: get the address to the "forwards" key.
*/
npf_connkey_t *
npf_conn_getforwkey(npf_conn_t *conn)
{
return (void *)&conn->c_keys[0];
}
/*
* npf_conn_getbackkey: get the address to the "backwards" key.
*
* => It depends on the address length.
*/
npf_connkey_t *
npf_conn_getbackkey(npf_conn_t *conn, unsigned alen)
{
const unsigned off = 2 + ((alen * 2) >> 2);
KASSERT(off == NPF_CONNKEY_V4WORDS || off == NPF_CONNKEY_V6WORDS);
return (void *)&conn->c_keys[off];
}