1105197Ssam/* $FreeBSD: stable/11/sys/netipsec/keydb.h 315514 2017-03-18 22:04:20Z ae $ */ 2105197Ssam/* $KAME: keydb.h,v 1.14 2000/08/02 17:58:26 sakane Exp $ */ 3105197Ssam 4139823Simp/*- 5105197Ssam * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6105197Ssam * All rights reserved. 7105197Ssam * 8105197Ssam * Redistribution and use in source and binary forms, with or without 9105197Ssam * modification, are permitted provided that the following conditions 10105197Ssam * are met: 11105197Ssam * 1. Redistributions of source code must retain the above copyright 12105197Ssam * notice, this list of conditions and the following disclaimer. 13105197Ssam * 2. Redistributions in binary form must reproduce the above copyright 14105197Ssam * notice, this list of conditions and the following disclaimer in the 15105197Ssam * documentation and/or other materials provided with the distribution. 16105197Ssam * 3. Neither the name of the project nor the names of its contributors 17105197Ssam * may be used to endorse or promote products derived from this software 18105197Ssam * without specific prior written permission. 19105197Ssam * 20105197Ssam * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21105197Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22105197Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23105197Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24105197Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25105197Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26105197Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27105197Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28105197Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29105197Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30105197Ssam * SUCH DAMAGE. 31105197Ssam */ 32105197Ssam 33105197Ssam#ifndef _NETIPSEC_KEYDB_H_ 34105197Ssam#define _NETIPSEC_KEYDB_H_ 35105197Ssam 36105197Ssam#ifdef _KERNEL 37315514Sae#include <sys/counter.h> 38315514Sae#include <sys/lock.h> 39315514Sae#include <sys/mutex.h> 40105197Ssam 41105197Ssam#include <netipsec/key_var.h> 42105197Ssam 43135615Smlaier#ifndef _SOCKADDR_UNION_DEFINED 44135615Smlaier#define _SOCKADDR_UNION_DEFINED 45105197Ssam/* 46105197Ssam * The union of all possible address formats we handle. 47105197Ssam */ 48105197Ssamunion sockaddr_union { 49105197Ssam struct sockaddr sa; 50105197Ssam struct sockaddr_in sin; 51105197Ssam struct sockaddr_in6 sin6; 52105197Ssam}; 53135615Smlaier#endif /* _SOCKADDR_UNION_DEFINED */ 54105197Ssam 55105197Ssam/* Security Assocciation Index */ 56105197Ssam/* NOTE: Ensure to be same address family */ 57105197Ssamstruct secasindex { 58204074Spjd union sockaddr_union src; /* source address for SA */ 59105197Ssam union sockaddr_union dst; /* destination address for SA */ 60315514Sae uint8_t proto; /* IPPROTO_ESP or IPPROTO_AH */ 61315514Sae uint8_t mode; /* mode of protocol, see ipsec.h */ 62315514Sae uint32_t reqid; /* reqid id who owned this SA */ 63105197Ssam /* see IPSEC_MANUAL_REQID_MAX. */ 64105197Ssam}; 65105197Ssam 66157123Sgnn/* 67157123Sgnn * In order to split out the keydb implementation from that of the 68157123Sgnn * PF_KEY sockets we need to define a few structures that while they 69157123Sgnn * may seem common are likely to diverge over time. 70157123Sgnn */ 71157123Sgnn 72157123Sgnn/* sadb_identity */ 73157123Sgnnstruct secident { 74157123Sgnn u_int16_t type; 75157123Sgnn u_int64_t id; 76157123Sgnn}; 77157123Sgnn 78157123Sgnn/* sadb_key */ 79157123Sgnnstruct seckey { 80157123Sgnn u_int16_t bits; 81157123Sgnn char *key_data; 82157123Sgnn}; 83157123Sgnn 84157123Sgnnstruct seclifetime { 85157123Sgnn u_int32_t allocations; 86157123Sgnn u_int64_t bytes; 87157123Sgnn u_int64_t addtime; 88157123Sgnn u_int64_t usetime; 89157123Sgnn}; 90157123Sgnn 91315514Saestruct secnatt { 92315514Sae union sockaddr_union oai; /* original addresses of initiator */ 93315514Sae union sockaddr_union oar; /* original address of responder */ 94315514Sae uint16_t sport; /* source port */ 95315514Sae uint16_t dport; /* destination port */ 96315514Sae uint16_t cksum; /* checksum delta */ 97315514Sae uint16_t flags; 98315514Sae#define IPSEC_NATT_F_OAI 0x0001 99315514Sae#define IPSEC_NATT_F_OAR 0x0002 100315514Sae}; 101315514Sae 102105197Ssam/* Security Association Data Base */ 103315514SaeTAILQ_HEAD(secasvar_queue, secasvar); 104105197Ssamstruct secashead { 105315514Sae TAILQ_ENTRY(secashead) chain; 106315514Sae LIST_ENTRY(secashead) addrhash; /* hash by sproto+src+dst addresses */ 107315514Sae LIST_ENTRY(secashead) drainq; /* used ONLY by flush callout */ 108105197Ssam 109105197Ssam struct secasindex saidx; 110105197Ssam 111157123Sgnn struct secident *idents; /* source identity */ 112157123Sgnn struct secident *identd; /* destination identity */ 113105197Ssam /* XXX I don't know how to use them. */ 114105197Ssam 115315514Sae volatile u_int refcnt; /* reference count */ 116315514Sae uint8_t state; /* MATURE or DEAD. */ 117315514Sae struct secasvar_queue savtree_alive; /* MATURE and DYING SA */ 118315514Sae struct secasvar_queue savtree_larval; /* LARVAL SA */ 119105197Ssam}; 120105197Ssam 121105197Ssamstruct xformsw; 122105197Ssamstruct enc_xform; 123105197Ssamstruct auth_hash; 124105197Ssamstruct comp_algo; 125105197Ssam 126315514Sae/* 127315514Sae * Security Association 128315514Sae * 129315514Sae * For INBOUND packets we do SA lookup using SPI, thus only SPIHASH is used. 130315514Sae * For OUTBOUND packets there may be several SA suitable for packet. 131315514Sae * We use key_preferred_oldsa variable to choose better SA. First of we do 132315514Sae * lookup for suitable SAH using packet's saidx. Then we use SAH's savtree 133315514Sae * to search better candidate. The newer SA (by created time) are placed 134315514Sae * in the beginning of the savtree list. There is no preference between 135315514Sae * DYING and MATURE. 136315514Sae * 137315514Sae * NB: Fields with a tdb_ prefix are part of the "glue" used 138315514Sae * to interface to the OpenBSD crypto support. This was done 139315514Sae * to distinguish this code from the mainline KAME code. 140315514Sae * NB: Fields are sorted on the basis of the frequency of changes, i.e. 141315514Sae * constants and unchangeable fields are going first. 142315514Sae * NB: if you want to change this structure, check that this will not break 143315514Sae * key_updateaddresses(). 144315514Sae */ 145105197Ssamstruct secasvar { 146315514Sae uint32_t spi; /* SPI Value, network byte order */ 147315514Sae uint32_t flags; /* holder for SADB_KEY_FLAGS */ 148315514Sae uint32_t seq; /* sequence number */ 149315514Sae pid_t pid; /* message's pid */ 150315514Sae u_int ivlen; /* length of IV */ 151105197Ssam 152315514Sae struct secashead *sah; /* back pointer to the secashead */ 153157123Sgnn struct seckey *key_auth; /* Key for Authentication */ 154157123Sgnn struct seckey *key_enc; /* Key for Encryption */ 155105197Ssam struct secreplay *replay; /* replay prevention */ 156315514Sae struct secnatt *natt; /* NAT-T config */ 157315514Sae struct mtx *lock; /* update/access lock */ 158105197Ssam 159315514Sae const struct xformsw *tdb_xform; /* transform */ 160315514Sae const struct enc_xform *tdb_encalgxform;/* encoding algorithm */ 161315514Sae const struct auth_hash *tdb_authalgxform;/* authentication algorithm */ 162315514Sae const struct comp_algo *tdb_compalgxform;/* compression algorithm */ 163315514Sae uint64_t tdb_cryptoid; /* crypto session id */ 164315514Sae 165315514Sae uint8_t alg_auth; /* Authentication Algorithm Identifier*/ 166315514Sae uint8_t alg_enc; /* Cipher Algorithm Identifier */ 167315514Sae uint8_t alg_comp; /* Compression Algorithm Identifier */ 168315514Sae uint8_t state; /* Status of this SA (pfkeyv2.h) */ 169315514Sae 170315514Sae counter_u64_t lft_c; /* CURRENT lifetime */ 171315514Sae#define lft_c_allocations lft_c 172315514Sae#define lft_c_bytes lft_c + 1 173157123Sgnn struct seclifetime *lft_h; /* HARD lifetime */ 174157123Sgnn struct seclifetime *lft_s; /* SOFT lifetime */ 175105197Ssam 176315514Sae uint64_t created; /* time when SA was created */ 177315514Sae uint64_t firstused; /* time when SA was first used */ 178105197Ssam 179315514Sae TAILQ_ENTRY(secasvar) chain; 180315514Sae LIST_ENTRY(secasvar) spihash; 181315514Sae LIST_ENTRY(secasvar) drainq; /* used ONLY by flush callout */ 182105197Ssam 183315514Sae uint64_t cntr; /* counter for GCM and CTR */ 184315514Sae volatile u_int refcnt; /* reference count */ 185105197Ssam}; 186105197Ssam 187315514Sae#define SECASVAR_LOCK(_sav) mtx_lock((_sav)->lock) 188315514Sae#define SECASVAR_UNLOCK(_sav) mtx_unlock((_sav)->lock) 189315514Sae#define SECASVAR_LOCK_ASSERT(_sav) mtx_assert((_sav)->lock, MA_OWNED) 190286292Sjmg#define SAV_ISGCM(_sav) \ 191286292Sjmg ((_sav)->alg_enc == SADB_X_EALG_AESGCM8 || \ 192286292Sjmg (_sav)->alg_enc == SADB_X_EALG_AESGCM12 || \ 193286292Sjmg (_sav)->alg_enc == SADB_X_EALG_AESGCM16) 194286292Sjmg#define SAV_ISCTR(_sav) ((_sav)->alg_enc == SADB_X_EALG_AESCTR) 195286292Sjmg#define SAV_ISCTRORGCM(_sav) (SAV_ISCTR((_sav)) || SAV_ISGCM((_sav))) 196120585Ssam 197315514Sae/* Replay prevention, protected by SECASVAR_LOCK: 198315514Sae * (m) locked by mtx 199315514Sae * (c) read only except during creation / free 200315514Sae */ 201105197Ssamstruct secreplay { 202315514Sae u_int32_t count; /* (m) */ 203315514Sae u_int wsize; /* (c) window size, i.g. 4 bytes */ 204315514Sae u_int32_t seq; /* (m) used by sender */ 205315514Sae u_int32_t lastseq; /* (m) used by receiver */ 206315514Sae u_int32_t *bitmap; /* (m) used by receiver */ 207315514Sae u_int bitmap_size; /* (c) size of the bitmap array */ 208315514Sae int overflow; /* (m) overflow flag */ 209105197Ssam}; 210105197Ssam 211105197Ssam/* socket table due to send PF_KEY messages. */ 212105197Ssamstruct secreg { 213105197Ssam LIST_ENTRY(secreg) chain; 214105197Ssam 215105197Ssam struct socket *so; 216105197Ssam}; 217105197Ssam 218105197Ssam/* acquiring list table. */ 219105197Ssamstruct secacq { 220105197Ssam LIST_ENTRY(secacq) chain; 221315514Sae LIST_ENTRY(secacq) addrhash; 222315514Sae LIST_ENTRY(secacq) seqhash; 223105197Ssam 224105197Ssam struct secasindex saidx; 225315514Sae uint32_t seq; /* sequence number */ 226120585Ssam time_t created; /* for lifetime */ 227105197Ssam int count; /* for lifetime */ 228105197Ssam}; 229105197Ssam 230105197Ssam/* Sensitivity Level Specification */ 231105197Ssam/* nothing */ 232105197Ssam 233105197Ssam#define SADB_KILL_INTERVAL 600 /* six seconds */ 234105197Ssam 235105197Ssam/* secpolicy */ 236275438Saeextern struct secpolicy *keydb_newsecpolicy(void); 237275438Saeextern void keydb_delsecpolicy(struct secpolicy *); 238105197Ssam/* secashead */ 239275438Saeextern struct secashead *keydb_newsecashead(void); 240275438Saeextern void keydb_delsecashead(struct secashead *); 241105197Ssam/* secasvar */ 242275438Saeextern struct secasvar *keydb_newsecasvar(void); 243275438Saeextern void keydb_refsecasvar(struct secasvar *); 244275438Saeextern void keydb_freesecasvar(struct secasvar *); 245105197Ssam/* secreplay */ 246275438Saeextern struct secreplay *keydb_newsecreplay(size_t); 247275438Saeextern void keydb_delsecreplay(struct secreplay *); 248105197Ssam/* secreg */ 249275438Saeextern struct secreg *keydb_newsecreg(void); 250275438Saeextern void keydb_delsecreg(struct secreg *); 251105197Ssam 252105197Ssam#endif /* _KERNEL */ 253105197Ssam 254105197Ssam#endif /* _NETIPSEC_KEYDB_H_ */ 255