Home | History | Annotate | Line # | Download | only in npf
npf_connkey.c revision 1.1
      1  1.1  rmind /*-
      2  1.1  rmind  * Copyright (c) 2014-2019 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  * Connection key -- is a structure encoding the 5-tuple (protocol, address
     33  1.1  rmind  * length, source/destination address and source/destination port/ID).
     34  1.1  rmind  *
     35  1.1  rmind  * Key layout
     36  1.1  rmind  *
     37  1.1  rmind  *	The single key is formed out of 32-bit integers.  The layout is:
     38  1.1  rmind  *
     39  1.1  rmind  *	Field: | proto  |  alen  | src-id | dst-id | src-addr | dst-addr |
     40  1.1  rmind  *	       +--------+--------+--------+--------+----------+----------+
     41  1.1  rmind  *	Bits:  |   16   |   16   |   16   |   16   |  32-128  |  32-128  |
     42  1.1  rmind  *
     43  1.1  rmind  *	The source and destination are inverted if the key is for the
     44  1.1  rmind  *	backwards stream (forw == false).  The address length depends on
     45  1.1  rmind  *	the 'alen' field.  The length is in bytes and is either 4 or 16.
     46  1.1  rmind  *
     47  1.1  rmind  *	Warning: the keys must be immutable while they are in conndb.
     48  1.1  rmind  *
     49  1.1  rmind  * Embedding in the connection structure (npf_conn_t)
     50  1.1  rmind  *
     51  1.1  rmind  *	Two keys are stored in the npf_conn_t::c_keys[] array, which is
     52  1.1  rmind  *	variable-length, depending on whether the keys store IPv4 or IPv6
     53  1.1  rmind  *	addresses.  The length of the first key determines the position
     54  1.1  rmind  *	of the second key.
     55  1.1  rmind  */
     56  1.1  rmind 
     57  1.1  rmind #ifdef _KERNEL
     58  1.1  rmind #include <sys/cdefs.h>
     59  1.1  rmind __KERNEL_RCSID(0, "$NetBSD: npf_connkey.c,v 1.1 2019/07/23 00:52:01 rmind Exp $");
     60  1.1  rmind 
     61  1.1  rmind #include <sys/param.h>
     62  1.1  rmind #include <sys/types.h>
     63  1.1  rmind #endif
     64  1.1  rmind 
     65  1.1  rmind #define __NPF_CONN_PRIVATE
     66  1.1  rmind #include "npf_conn.h"
     67  1.1  rmind #include "npf_impl.h"
     68  1.1  rmind 
     69  1.1  rmind static inline unsigned
     70  1.1  rmind connkey_setkey(npf_connkey_t *key, uint16_t proto, const void *ipv,
     71  1.1  rmind     const uint16_t *id, unsigned alen, bool forw)
     72  1.1  rmind {
     73  1.1  rmind 	const npf_addr_t * const *ips = ipv;
     74  1.1  rmind 	uint32_t *k = key->ck_key;
     75  1.1  rmind 	unsigned isrc, idst;
     76  1.1  rmind 
     77  1.1  rmind 	if (__predict_true(forw)) {
     78  1.1  rmind 		isrc = NPF_SRC, idst = NPF_DST;
     79  1.1  rmind 	} else {
     80  1.1  rmind 		isrc = NPF_DST, idst = NPF_SRC;
     81  1.1  rmind 	}
     82  1.1  rmind 
     83  1.1  rmind 	/*
     84  1.1  rmind 	 * See the key layout explanation above.
     85  1.1  rmind 	 */
     86  1.1  rmind 
     87  1.1  rmind 	k[0] = ((uint32_t)proto << 16) | (alen & 0xffff);
     88  1.1  rmind 	k[1] = ((uint32_t)id[isrc] << 16) | id[idst];
     89  1.1  rmind 
     90  1.1  rmind 	if (__predict_true(alen == sizeof(in_addr_t))) {
     91  1.1  rmind 		k[2] = ips[isrc]->word32[0];
     92  1.1  rmind 		k[3] = ips[idst]->word32[0];
     93  1.1  rmind 		return 4 * sizeof(uint32_t);
     94  1.1  rmind 	} else {
     95  1.1  rmind 		const unsigned nwords = alen >> 2;
     96  1.1  rmind 		memcpy(&k[2], ips[isrc], alen);
     97  1.1  rmind 		memcpy(&k[2 + nwords], ips[idst], alen);
     98  1.1  rmind 		return (2 + (nwords * 2)) * sizeof(uint32_t);
     99  1.1  rmind 	}
    100  1.1  rmind }
    101  1.1  rmind 
    102  1.1  rmind static inline void
    103  1.1  rmind connkey_getkey(const npf_connkey_t *key, uint16_t *proto, npf_addr_t *ips,
    104  1.1  rmind     uint16_t *id, uint16_t *alen)
    105  1.1  rmind {
    106  1.1  rmind 	const uint32_t *k = key->ck_key;
    107  1.1  rmind 
    108  1.1  rmind 	/*
    109  1.1  rmind 	 * See the key layout explanation above.
    110  1.1  rmind 	 */
    111  1.1  rmind 
    112  1.1  rmind 	*proto = k[0] >> 16;
    113  1.1  rmind 	*alen = k[0] & 0xffff;
    114  1.1  rmind 	id[NPF_SRC] = k[1] >> 16;
    115  1.1  rmind 	id[NPF_DST] = k[1] & 0xffff;
    116  1.1  rmind 
    117  1.1  rmind 	switch (*alen) {
    118  1.1  rmind 	case sizeof(struct in6_addr):
    119  1.1  rmind 	case sizeof(struct in_addr):
    120  1.1  rmind 		memcpy(&ips[NPF_SRC], &k[2], *alen);
    121  1.1  rmind 		memcpy(&ips[NPF_DST], &k[2 + ((unsigned)*alen >> 2)], *alen);
    122  1.1  rmind 		return;
    123  1.1  rmind 	default:
    124  1.1  rmind 		KASSERT(0);
    125  1.1  rmind 	}
    126  1.1  rmind }
    127  1.1  rmind 
    128  1.1  rmind /*
    129  1.1  rmind  * npf_conn_adjkey: adjust the connection key by resetting the address/port.
    130  1.1  rmind  */
    131  1.1  rmind void
    132  1.1  rmind npf_conn_adjkey(npf_connkey_t *key, const npf_addr_t *naddr,
    133  1.1  rmind     const uint16_t id, const int di)
    134  1.1  rmind {
    135  1.1  rmind 	uint32_t * const k = key->ck_key;
    136  1.1  rmind 	const unsigned alen = k[0] & 0xffff;
    137  1.1  rmind 	uint32_t *addr = &k[2 + ((alen >> 2) * di)];
    138  1.1  rmind 
    139  1.1  rmind 	KASSERT(alen > 0);
    140  1.1  rmind 	memcpy(addr, naddr, alen);
    141  1.1  rmind 
    142  1.1  rmind 	if (id) {
    143  1.1  rmind 		const uint32_t oid = k[1];
    144  1.1  rmind 		const unsigned shift = 16 * !di;
    145  1.1  rmind 		const uint32_t mask = 0xffff0000 >> shift;
    146  1.1  rmind 		k[1] = ((uint32_t)id << shift) | (oid & mask);
    147  1.1  rmind 	}
    148  1.1  rmind }
    149  1.1  rmind 
    150  1.1  rmind /*
    151  1.1  rmind  * npf_conn_conkey: construct a key for the connection lookup.
    152  1.1  rmind  *
    153  1.1  rmind  * => Returns the key length in bytes or zero on failure.
    154  1.1  rmind  */
    155  1.1  rmind unsigned
    156  1.1  rmind npf_conn_conkey(const npf_cache_t *npc, npf_connkey_t *key, const bool forw)
    157  1.1  rmind {
    158  1.1  rmind 	const unsigned proto = npc->npc_proto;
    159  1.1  rmind 	const unsigned alen = npc->npc_alen;
    160  1.1  rmind 	const struct tcphdr *th;
    161  1.1  rmind 	const struct udphdr *uh;
    162  1.1  rmind 	uint16_t id[2] = { 0, 0 };
    163  1.1  rmind 
    164  1.1  rmind 	switch (proto) {
    165  1.1  rmind 	case IPPROTO_TCP:
    166  1.1  rmind 		KASSERT(npf_iscached(npc, NPC_TCP));
    167  1.1  rmind 		th = npc->npc_l4.tcp;
    168  1.1  rmind 		id[NPF_SRC] = th->th_sport;
    169  1.1  rmind 		id[NPF_DST] = th->th_dport;
    170  1.1  rmind 		break;
    171  1.1  rmind 	case IPPROTO_UDP:
    172  1.1  rmind 		KASSERT(npf_iscached(npc, NPC_UDP));
    173  1.1  rmind 		uh = npc->npc_l4.udp;
    174  1.1  rmind 		id[NPF_SRC] = uh->uh_sport;
    175  1.1  rmind 		id[NPF_DST] = uh->uh_dport;
    176  1.1  rmind 		break;
    177  1.1  rmind 	case IPPROTO_ICMP:
    178  1.1  rmind 		if (npf_iscached(npc, NPC_ICMP_ID)) {
    179  1.1  rmind 			const struct icmp *ic = npc->npc_l4.icmp;
    180  1.1  rmind 			id[NPF_SRC] = ic->icmp_id;
    181  1.1  rmind 			id[NPF_DST] = ic->icmp_id;
    182  1.1  rmind 			break;
    183  1.1  rmind 		}
    184  1.1  rmind 		return 0;
    185  1.1  rmind 	case IPPROTO_ICMPV6:
    186  1.1  rmind 		if (npf_iscached(npc, NPC_ICMP_ID)) {
    187  1.1  rmind 			const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
    188  1.1  rmind 			id[NPF_SRC] = ic6->icmp6_id;
    189  1.1  rmind 			id[NPF_DST] = ic6->icmp6_id;
    190  1.1  rmind 			break;
    191  1.1  rmind 		}
    192  1.1  rmind 		return 0;
    193  1.1  rmind 	default:
    194  1.1  rmind 		/* Unsupported protocol. */
    195  1.1  rmind 		return 0;
    196  1.1  rmind 	}
    197  1.1  rmind 	return connkey_setkey(key, proto, npc->npc_ips, id, alen, forw);
    198  1.1  rmind }
    199  1.1  rmind 
    200  1.1  rmind /*
    201  1.1  rmind  * npf_conn_getforwkey: get the address to the "forwards" key.
    202  1.1  rmind  */
    203  1.1  rmind npf_connkey_t *
    204  1.1  rmind npf_conn_getforwkey(npf_conn_t *conn)
    205  1.1  rmind {
    206  1.1  rmind 	return (void *)&conn->c_keys[0];
    207  1.1  rmind }
    208  1.1  rmind 
    209  1.1  rmind /*
    210  1.1  rmind  * npf_conn_getbackkey: get the address to the "backwards" key.
    211  1.1  rmind  *
    212  1.1  rmind  * => It depends on the address length.
    213  1.1  rmind  */
    214  1.1  rmind npf_connkey_t *
    215  1.1  rmind npf_conn_getbackkey(npf_conn_t *conn, unsigned alen)
    216  1.1  rmind {
    217  1.1  rmind 	const unsigned off = 2 + ((alen * 2) >> 2);
    218  1.1  rmind 	KASSERT(off == NPF_CONNKEY_V4WORDS || off == NPF_CONNKEY_V6WORDS);
    219  1.1  rmind 	return (void *)&conn->c_keys[off];
    220  1.1  rmind }
    221  1.1  rmind 
    222  1.1  rmind /*
    223  1.1  rmind  * Connection key exporting/importing.
    224  1.1  rmind  */
    225  1.1  rmind 
    226  1.1  rmind nvlist_t *
    227  1.1  rmind npf_connkey_export(const npf_connkey_t *key)
    228  1.1  rmind {
    229  1.1  rmind 	uint16_t ids[2], alen, proto;
    230  1.1  rmind 	npf_addr_t ips[2];
    231  1.1  rmind 	nvlist_t *kdict;
    232  1.1  rmind 
    233  1.1  rmind 	kdict = nvlist_create(0);
    234  1.1  rmind 	connkey_getkey(key, &proto, ips, ids, &alen);
    235  1.1  rmind 	nvlist_add_number(kdict, "proto", proto);
    236  1.1  rmind 	nvlist_add_number(kdict, "sport", ids[NPF_SRC]);
    237  1.1  rmind 	nvlist_add_number(kdict, "dport", ids[NPF_DST]);
    238  1.1  rmind 	nvlist_add_binary(kdict, "saddr", &ips[NPF_SRC], alen);
    239  1.1  rmind 	nvlist_add_binary(kdict, "daddr", &ips[NPF_DST], alen);
    240  1.1  rmind 	return kdict;
    241  1.1  rmind }
    242  1.1  rmind 
    243  1.1  rmind unsigned
    244  1.1  rmind npf_connkey_import(const nvlist_t *kdict, npf_connkey_t *key)
    245  1.1  rmind {
    246  1.1  rmind 	npf_addr_t const * ips[2];
    247  1.1  rmind 	uint16_t proto, ids[2];
    248  1.1  rmind 	size_t alen1, alen2;
    249  1.1  rmind 
    250  1.1  rmind 	proto = dnvlist_get_number(kdict, "proto", 0);
    251  1.1  rmind 	ids[NPF_SRC] = dnvlist_get_number(kdict, "sport", 0);
    252  1.1  rmind 	ids[NPF_DST] = dnvlist_get_number(kdict, "dport", 0);
    253  1.1  rmind 	ips[NPF_SRC] = dnvlist_get_binary(kdict, "saddr", &alen1, NULL, 0);
    254  1.1  rmind 	ips[NPF_DST] = dnvlist_get_binary(kdict, "daddr", &alen2, NULL, 0);
    255  1.1  rmind 	if (alen1 == 0 || alen1 > sizeof(npf_addr_t) || alen1 != alen2) {
    256  1.1  rmind 		return 0;
    257  1.1  rmind 	}
    258  1.1  rmind 	return connkey_setkey(key, proto, ips, ids, alen1, true);
    259  1.1  rmind }
    260  1.1  rmind 
    261  1.1  rmind #if defined(DDB) || defined(_NPF_TESTING)
    262  1.1  rmind 
    263  1.1  rmind void
    264  1.1  rmind npf_connkey_print(const npf_connkey_t *key)
    265  1.1  rmind {
    266  1.1  rmind 	uint16_t proto, ids[2], alen;
    267  1.1  rmind 	npf_addr_t ips[2];
    268  1.1  rmind 
    269  1.1  rmind 	connkey_getkey(key, &proto, ips, ids, &alen);
    270  1.1  rmind 	printf("\tforw %s:%d", npf_addr_dump(&ips[0], alen), ids[0]);
    271  1.1  rmind 	printf("-> %s:%d\n", npf_addr_dump(&ips[1], alen), ids[1]);
    272  1.1  rmind }
    273  1.1  rmind 
    274  1.1  rmind #endif
    275