/* $KAME: keydb.h,v 1.14 2000/08/02 17:58:26 sakane Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16* 3. Neither the name of the project nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#ifndef _NETIPSEC_KEYDB_H_34#define _NETIPSEC_KEYDB_H_3536#ifdef _KERNEL37#include <sys/counter.h>38#include <sys/ck.h>39#include <sys/lock.h>40#include <sys/mutex.h>41#include <sys/rmlock.h>42#include <sys/_task.h>4344#include <netipsec/key_var.h>45#include <opencrypto/_cryptodev.h>4647#ifndef _SOCKADDR_UNION_DEFINED48#define _SOCKADDR_UNION_DEFINED49/*50* The union of all possible address formats we handle.51*/52union sockaddr_union {53struct sockaddr sa;54struct sockaddr_in sin;55struct sockaddr_in6 sin6;56};57#endif /* _SOCKADDR_UNION_DEFINED */5859/* Security Association Index */60/* NOTE: Ensure to be same address family */61struct secasindex {62union sockaddr_union src; /* source address for SA */63union sockaddr_union dst; /* destination address for SA */64uint8_t proto; /* IPPROTO_ESP or IPPROTO_AH */65uint8_t mode; /* mode of protocol, see ipsec.h */66uint32_t reqid; /* reqid id who owned this SA */67/* see IPSEC_MANUAL_REQID_MAX. */68};6970/*71* In order to split out the keydb implementation from that of the72* PF_KEY sockets we need to define a few structures that while they73* may seem common are likely to diverge over time.74*/7576/* sadb_identity */77struct secident {78u_int16_t type;79u_int64_t id;80};8182/* sadb_key */83struct seckey {84u_int16_t bits;85char *key_data;86};8788struct seclifetime {89u_int32_t allocations;90u_int64_t bytes;91u_int64_t addtime;92u_int64_t usetime;93};9495struct secnatt {96union sockaddr_union oai; /* original addresses of initiator */97union sockaddr_union oar; /* original address of responder */98uint16_t sport; /* source port */99uint16_t dport; /* destination port */100uint16_t cksum; /* checksum delta */101uint16_t flags;102#define IPSEC_NATT_F_OAI 0x0001103#define IPSEC_NATT_F_OAR 0x0002104};105106/* Security Association Data Base */107TAILQ_HEAD(secasvar_queue, secasvar);108struct secashead {109TAILQ_ENTRY(secashead) chain;110LIST_ENTRY(secashead) addrhash; /* hash by sproto+src+dst addresses */111LIST_ENTRY(secashead) drainq; /* used ONLY by flush callout */112113struct secasindex saidx;114115struct secident *idents; /* source identity */116struct secident *identd; /* destination identity */117/* XXX I don't know how to use them. */118119volatile u_int refcnt; /* reference count */120uint8_t state; /* MATURE or DEAD. */121struct secasvar_queue savtree_alive; /* MATURE and DYING SA */122struct secasvar_queue savtree_larval; /* LARVAL SA */123};124125struct xformsw;126struct enc_xform;127struct auth_hash;128struct comp_algo;129struct ifp_handle_sav;130131/*132* Security Association133*134* For INBOUND packets we do SA lookup using SPI, thus only SPIHASH is used.135* For OUTBOUND packets there may be several SA suitable for packet.136* We use key_preferred_oldsa variable to choose better SA. First of we do137* lookup for suitable SAH using packet's saidx. Then we use SAH's savtree138* to search better candidate. The newer SA (by created time) are placed139* in the beginning of the savtree list. There is no preference between140* DYING and MATURE.141*142* NB: Fields with a tdb_ prefix are part of the "glue" used143* to interface to the OpenBSD crypto support. This was done144* to distinguish this code from the mainline KAME code.145* NB: Fields are sorted on the basis of the frequency of changes, i.e.146* constants and unchangeable fields are going first.147* NB: if you want to change this structure, check that this will not break148* key_updateaddresses().149*/150struct secasvar {151uint32_t spi; /* SPI Value, network byte order */152uint32_t flags; /* holder for SADB_KEY_FLAGS */153uint32_t seq; /* sequence number */154pid_t pid; /* message's pid */155u_int ivlen; /* length of IV */156157struct secashead *sah; /* back pointer to the secashead */158struct seckey *key_auth; /* Key for Authentication */159struct seckey *key_enc; /* Key for Encryption */160struct secreplay *replay; /* replay prevention */161struct secnatt *natt; /* NAT-T config */162struct rmlock *lock; /* update/access lock */163164const struct xformsw *tdb_xform; /* transform */165const struct enc_xform *tdb_encalgxform;/* encoding algorithm */166const struct auth_hash *tdb_authalgxform;/* authentication algorithm */167const struct comp_algo *tdb_compalgxform;/* compression algorithm */168crypto_session_t tdb_cryptoid; /* crypto session */169170uint8_t alg_auth; /* Authentication Algorithm Identifier*/171uint8_t alg_enc; /* Cipher Algorithm Identifier */172uint8_t alg_comp; /* Compression Algorithm Identifier */173uint8_t state; /* Status of this SA (pfkeyv2.h) */174175counter_u64_t lft_c; /* CURRENT lifetime */176#define lft_c_allocations lft_c177#define lft_c_bytes lft_c + 1178struct seclifetime *lft_h; /* HARD lifetime */179struct seclifetime *lft_s; /* SOFT lifetime */180181uint64_t created; /* time when SA was created */182uint64_t firstused; /* time when SA was first used */183184TAILQ_ENTRY(secasvar) chain;185LIST_ENTRY(secasvar) spihash;186LIST_ENTRY(secasvar) drainq; /* used ONLY by flush callout */187188uint64_t cntr; /* counter for GCM and CTR */189volatile u_int refcnt; /* reference count */190CK_LIST_HEAD(, ifp_handle_sav) accel_ifps;191uintptr_t accel_forget_tq;192const char *accel_ifname;193uint32_t accel_flags;194counter_u64_t accel_lft_sw;195uint64_t accel_hw_allocs;196uint64_t accel_hw_octets;197uint64_t accel_firstused;198};199200#define SADB_KEY_ACCEL_INST 0x00000001201#define SADB_KEY_ACCEL_DEINST 0x00000002202203#define SECASVAR_RLOCK_TRACKER struct rm_priotracker _secas_tracker204#define SECASVAR_RLOCK(_sav) rm_rlock((_sav)->lock, &_secas_tracker)205#define SECASVAR_RUNLOCK(_sav) rm_runlock((_sav)->lock, &_secas_tracker)206#define SECASVAR_WLOCK(_sav) rm_wlock((_sav)->lock)207#define SECASVAR_WUNLOCK(_sav) rm_wunlock((_sav)->lock)208#define SECASVAR_LOCK_ASSERT(_sav) rm_assert((_sav)->lock, RA_LOCKED)209#define SECASVAR_LOCK_WASSERT(_sav) rm_assert((_sav)->lock, RA_WLOCKED)210#define SAV_ISGCM(_sav) \211((_sav)->alg_enc == SADB_X_EALG_AESGCM8 || \212(_sav)->alg_enc == SADB_X_EALG_AESGCM12 || \213(_sav)->alg_enc == SADB_X_EALG_AESGCM16)214#define SAV_ISCTR(_sav) ((_sav)->alg_enc == SADB_X_EALG_AESCTR)215#define SAV_ISCHACHA(_sav) \216((_sav)->alg_enc == SADB_X_EALG_CHACHA20POLY1305)217#define SAV_ISCTRORGCM(_sav) (SAV_ISCTR((_sav)) || SAV_ISGCM((_sav)))218219#define IPSEC_SEQH_SHIFT 32220221/* Replay prevention, protected by SECASVAR_LOCK:222* (m) locked by mtx223* (c) read only except during creation / free224*/225struct secreplay {226struct mtx lock;227u_int64_t count; /* (m) */228u_int wsize; /* (c) window size, i.g. 4 bytes */229u_int64_t last; /* (m) used by receiver */230u_int32_t *bitmap; /* (m) used by receiver */231u_int bitmap_size; /* (c) size of the bitmap array */232int overflow; /* (m) overflow flag */233};234235#define SECREPLAY_LOCK(_r) mtx_lock(&(_r)->lock)236#define SECREPLAY_UNLOCK(_r) mtx_unlock(&(_r)->lock)237#define SECREPLAY_ASSERT(_r) mtx_assert(&(_r)->lock, MA_OWNED)238239/* socket table due to send PF_KEY messages. */240struct secreg {241LIST_ENTRY(secreg) chain;242243struct socket *so;244};245246/* acquiring list table. */247struct secacq {248LIST_ENTRY(secacq) chain;249LIST_ENTRY(secacq) addrhash;250LIST_ENTRY(secacq) seqhash;251252struct secasindex saidx;253uint32_t seq; /* sequence number */254time_t created; /* for lifetime */255int count; /* for lifetime */256};257258#endif /* _KERNEL */259260#endif /* _NETIPSEC_KEYDB_H_ */261262263