sadb.h revision 11042:2d6e217af1b4
1203954Srdivacky/* 2203954Srdivacky * CDDL HEADER START 3203954Srdivacky * 4203954Srdivacky * The contents of this file are subject to the terms of the 5203954Srdivacky * Common Development and Distribution License (the "License"). 6203954Srdivacky * You may not use this file except in compliance with the License. 7203954Srdivacky * 8203954Srdivacky * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9203954Srdivacky * or http://www.opensolaris.org/os/licensing. 10203954Srdivacky * See the License for the specific language governing permissions 11203954Srdivacky * and limitations under the License. 12221345Sdim * 13203954Srdivacky * When distributing Covered Code, include this CDDL HEADER in each 14221345Sdim * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15204642Srdivacky * If applicable, add the following below this CDDL HEADER, with the 16203954Srdivacky * fields enclosed by brackets "[]" replaced with your own identifying 17204642Srdivacky * information: Portions Copyright [yyyy] [name of copyright owner] 18203954Srdivacky * 19203954Srdivacky * CDDL HEADER END 20204642Srdivacky */ 21204642Srdivacky/* 22204642Srdivacky * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23204642Srdivacky * Use is subject to license terms. 24204642Srdivacky */ 25204642Srdivacky 26204642Srdivacky#ifndef _INET_SADB_H 27204642Srdivacky#define _INET_SADB_H 28204642Srdivacky 29204642Srdivacky#ifdef __cplusplus 30218893Sdimextern "C" { 31204642Srdivacky#endif 32204642Srdivacky 33204642Srdivacky#include <inet/ipsec_info.h> 34204642Srdivacky#include <sys/crypto/common.h> 35218893Sdim#include <sys/crypto/api.h> 36204642Srdivacky#include <sys/note.h> 37204642Srdivacky 38204642Srdivacky#define IPSA_MAX_ADDRLEN 4 /* Max address len. (in 32-bits) for an SA. */ 39204642Srdivacky 40204642Srdivacky#define MAXSALTSIZE 8 41204642Srdivacky 42204642Srdivacky/* 43204642Srdivacky * For combined mode ciphers, store the crypto_mechanism_t in the 44204642Srdivacky * per-packet ipsec_in_t/ipsec_out_t structures. This is because the PARAMS 45204642Srdivacky * and nonce values change for each packet. For non-combined mode 46204642Srdivacky * ciphers, these values are constant for the life of the SA. 47204642Srdivacky */ 48204642Srdivackytypedef struct ipsa_cm_mech_s { 49203954Srdivacky crypto_mechanism_t combined_mech; 50203954Srdivacky union { 51203954Srdivacky CK_AES_CCM_PARAMS paramu_ccm; 52203954Srdivacky CK_AES_GCM_PARAMS paramu_gcm; 53218893Sdim } paramu; 54203954Srdivacky uint8_t nonce[MAXSALTSIZE + sizeof (uint64_t)]; 55203954Srdivacky#define param_ulMACSize paramu.paramu_ccm.ulMACSize 56203954Srdivacky#define param_ulNonceSize paramu.paramu_ccm.ipsa_ulNonceSize 57203954Srdivacky#define param_ulAuthDataSize paramu.paramu_ccm.ipsa_ulAuthDataSize 58218893Sdim#define param_ulDataSize paramu.paramu_ccm.ipsa_ulDataSize 59203954Srdivacky#define param_nonce paramu.paramu_ccm.nonce 60203954Srdivacky#define param_authData paramu.paramu_ccm.authData 61203954Srdivacky#define param_pIv paramu.paramu_gcm.ipsa_pIv 62203954Srdivacky#define param_ulIvLen paramu.paramu_gcm.ulIvLen 63218893Sdim#define param_ulIvBits paramu.paramu_gcm.ulIvBits 64204642Srdivacky#define param_pAAD paramu.paramu_gcm.pAAD 65204642Srdivacky#define param_ulAADLen paramu.paramu_gcm.ulAADLen 66204642Srdivacky#define param_ulTagBits paramu.paramu_gcm.ulTagBits 67203954Srdivacky} ipsa_cm_mech_t; 68218893Sdim 69204642Srdivacky/* 70204642Srdivacky * The Initialization Vector (also known as IV or Nonce) used to 71204642Srdivacky * initialize the Block Cipher, is made up of a Counter and a Salt. 72204642Srdivacky * The Counter is fixed at 64 bits and is incremented for each packet. 73218893Sdim * The Salt value can be any whole byte value upto 64 bits. This is 74218893Sdim * algorithm mode specific and can be configured with ipsecalgs(1m). 75218893Sdim * 76218893Sdim * We only support whole byte salt lengths, this is because the salt is 77204792Srdivacky * stored in an array of uint8_t's. This is enforced by ipsecalgs(1m) 78204792Srdivacky * which configures the salt length as a number of bytes. Checks are 79204792Srdivacky * made to ensure the salt length defined in ipsecalgs(1m) fits in 80204792Srdivacky * the ipsec_nonce_t. 81204792Srdivacky * 82204792Srdivacky * The Salt value remains constant for the life of the SA, the Salt is 83204792Srdivacky * know to both peers, but NOT transmitted on the network. The Counter 84218893Sdim * portion of the nonce is transmitted over the network with each packet 85204642Srdivacky * and is confusingly described as the Initialization Vector by RFCs 86204642Srdivacky * 4309/4106. 87204642Srdivacky * 88204642Srdivacky * The maximum Initialization Vector length is 128 bits, if the actual 89218893Sdim * size is less, its padded internally by the algorithm. 90204642Srdivacky * 91204642Srdivacky * The nonce structure is defined like this in the SA (ipsa_t)to ensure 92218893Sdim * the Initilization Vector (counter) is 64 bit aligned, because it will 93204642Srdivacky * be incremented as an uint64_t. The nonce as used by the algorithms is 94204642Srdivacky * a straight uint8_t array. 95204642Srdivacky * 96221345Sdim * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 97221345Sdim * | | | | |x|x|x|x| | 98221345Sdim * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 99221345Sdim * salt_offset <------> 100203954Srdivacky * ipsa_saltlen <-------> 101203954Srdivacky * ipsa_nonce_buf------^ 102218893Sdim * ipsa_salt-------------~~~~~~^ 103203954Srdivacky * ipsa_nonce------------~~~~~~^ 104203954Srdivacky * ipsa_iv-----------------------------^ 105203954Srdivacky */ 106218893Sdimtypedef struct ipsec_nonce_s { 107204642Srdivacky uint8_t salt[MAXSALTSIZE]; 108204642Srdivacky uint64_t iv; 109218893Sdim} ipsec_nonce_t; 110204642Srdivacky 111203954Srdivacky/* 112204642Srdivacky * IP security association. Synchronization assumes 32-bit loads, so 113203954Srdivacky * the 64-bit quantities can't even be be read w/o locking it down! 114218893Sdim */ 115204642Srdivacky 116203954Srdivacky/* keying info */ 117203954Srdivackytypedef struct ipsa_key_s { 118203954Srdivacky uint8_t *sak_key; /* Algorithm key. */ 119203954Srdivacky uint_t sak_keylen; /* Algorithm key length (in bytes). */ 120218893Sdim uint_t sak_keybits; /* Algorithm key length (in bits) */ 121204642Srdivacky uint_t sak_algid; /* Algorithm ID number. */ 122204642Srdivacky} ipsa_key_t; 123204642Srdivacky 124204642Srdivackytypedef struct ipsa_s { 125204642Srdivacky struct ipsa_s *ipsa_next; /* Next in hash bucket */ 126204642Srdivacky struct ipsa_s **ipsa_ptpn; /* Pointer to previous next pointer. */ 127204642Srdivacky kmutex_t *ipsa_linklock; /* Pointer to hash-chain lock. */ 128204642Srdivacky void (*ipsa_freefunc)(struct ipsa_s *); /* freeassoc function */ 129204642Srdivacky void (*ipsa_noncefunc)(struct ipsa_s *, uchar_t *, 130204642Srdivacky uint_t, uchar_t *, ipsa_cm_mech_t *, crypto_data_t *); 131204642Srdivacky /* 132218893Sdim * NOTE: I may need more pointers, depending on future SA 133204642Srdivacky * requirements. 134204642Srdivacky */ 135204642Srdivacky ipsa_key_t ipsa_authkeydata; 136204642Srdivacky#define ipsa_authkey ipsa_authkeydata.sak_key 137204642Srdivacky#define ipsa_authkeylen ipsa_authkeydata.sak_keylen 138204642Srdivacky#define ipsa_authkeybits ipsa_authkeydata.sak_keybits 139204642Srdivacky#define ipsa_auth_alg ipsa_authkeydata.sak_algid 140204642Srdivacky ipsa_key_t ipsa_encrkeydata; 141204642Srdivacky#define ipsa_encrkey ipsa_encrkeydata.sak_key 142204642Srdivacky#define ipsa_encrkeylen ipsa_encrkeydata.sak_keylen 143204642Srdivacky#define ipsa_encrkeybits ipsa_encrkeydata.sak_keybits 144218893Sdim#define ipsa_encr_alg ipsa_encrkeydata.sak_algid 145203954Srdivacky 146203954Srdivacky struct ipsid_s *ipsa_src_cid; /* Source certificate identity */ 147203954Srdivacky struct ipsid_s *ipsa_dst_cid; /* Destination certificate identity */ 148203954Srdivacky mblk_t *ipsa_lpkt; /* Packet received while larval (CAS me) */ 149203954Srdivacky mblk_t *ipsa_bpkt_head; /* Packets received while idle */ 150204642Srdivacky mblk_t *ipsa_bpkt_tail; 151203954Srdivacky#define SADB_MAX_IDLEPKTS 100 152203954Srdivacky uint8_t ipsa_mblkcnt; /* Number of packets received while idle */ 153203954Srdivacky 154203954Srdivacky /* 155203954Srdivacky * PF_KEYv2 supports a replay window size of 255. Hence there is a 156203954Srdivacky * need a bit vector to support a replay window of 255. 256 is a nice 157203954Srdivacky * round number, so I support that. 158203954Srdivacky * 159203954Srdivacky * Use an array of uint64_t for best performance on 64-bit 160203954Srdivacky * processors. (And hope that 32-bit compilers can handle things 161203954Srdivacky * okay.) The " >> 6 " is to get the appropriate number of 64-bit 162203954Srdivacky * ints. 163203954Srdivacky */ 164203954Srdivacky#define SADB_MAX_REPLAY 256 /* Must be 0 mod 64. */ 165218893Sdim uint64_t ipsa_replay_arr[SADB_MAX_REPLAY >> 6]; 166203954Srdivacky 167203954Srdivacky uint64_t ipsa_unique_id; /* Non-zero for unique SAs */ 168221345Sdim uint64_t ipsa_unique_mask; /* mask value for unique_id */ 169221345Sdim 170221345Sdim /* 171221345Sdim * Reference count semantics: 172221345Sdim * 173221345Sdim * An SA has a reference count of 1 if something's pointing 174203954Srdivacky * to it. This includes being in a hash table. So if an 175203954Srdivacky * SA is in a hash table, it has a reference count of at least 1. 176203954Srdivacky * 177203954Srdivacky * When a ptr. to an IPSA is assigned, you MUST REFHOLD after 178203954Srdivacky * said assignment. When a ptr. to an IPSA is released 179203954Srdivacky * you MUST REFRELE. When the refcount hits 0, REFRELE 180203954Srdivacky * will free the IPSA. 181203954Srdivacky */ 182203954Srdivacky kmutex_t ipsa_lock; /* Locks non-linkage/refcnt fields. */ 183203954Srdivacky /* Q: Since I may be doing refcnts differently, will I need cv? */ 184218893Sdim uint_t ipsa_refcnt; /* Reference count. */ 185203954Srdivacky 186203954Srdivacky /* 187203954Srdivacky * The following four time fields are the ones monitored by ah_ager() 188203954Srdivacky * and esp_ager() respectively. They are all absolute wall-clock 189203954Srdivacky * times. The times of creation (i.e. add time) and first use are 190203954Srdivacky * pretty straightforward. The soft and hard expire times are 191203954Srdivacky * derived from the times of first use and creation, plus the minimum 192203954Srdivacky * expiration times in the fields that follow this. 193203954Srdivacky * 194203954Srdivacky * For example, if I had a hard add time of 30 seconds, and a hard 195203954Srdivacky * use time of 15, the ipsa_hardexpiretime would be time of add, plus 196203954Srdivacky * 30 seconds. If I USE the SA such that time of first use plus 15 197218893Sdim * seconds would be earlier than the add time plus 30 seconds, then 198204642Srdivacky * ipsa_hardexpiretime would become this earlier time. 199203954Srdivacky */ 200204642Srdivacky time_t ipsa_addtime; /* Time I was added. */ 201203954Srdivacky time_t ipsa_usetime; /* Time of my first use. */ 202204642Srdivacky time_t ipsa_lastuse; /* Time of my last use. */ 203203954Srdivacky time_t ipsa_idletime; /* Seconds of idle time */ 204203954Srdivacky time_t ipsa_last_nat_t_ka; /* Time of my last NAT-T keepalive. */ 205203954Srdivacky time_t ipsa_softexpiretime; /* Time of my first soft expire. */ 206203954Srdivacky time_t ipsa_hardexpiretime; /* Time of my first hard expire. */ 207204642Srdivacky time_t ipsa_idleexpiretime; /* Time of my next idle expire time */ 208204642Srdivacky 209204642Srdivacky struct ipsec_nonce_s *ipsa_nonce_buf; 210203954Srdivacky uint8_t *ipsa_nonce; 211203954Srdivacky uint_t ipsa_nonce_len; 212203954Srdivacky uint8_t *ipsa_salt; 213203954Srdivacky uint_t ipsa_saltbits; 214218893Sdim uint_t ipsa_saltlen; 215203954Srdivacky uint64_t *ipsa_iv; 216204642Srdivacky 217204642Srdivacky uint64_t ipsa_iv_hardexpire; 218204642Srdivacky uint64_t ipsa_iv_softexpire; 219204642Srdivacky /* 220204642Srdivacky * The following fields are directly reflected in PF_KEYv2 LIFETIME 221204642Srdivacky * extensions. The time_ts are in number-of-seconds, and the bytes 222204642Srdivacky * are in... bytes. 223204642Srdivacky */ 224204642Srdivacky time_t ipsa_softaddlt; /* Seconds of soft lifetime after add. */ 225204642Srdivacky time_t ipsa_softuselt; /* Seconds of soft lifetime after first use. */ 226204642Srdivacky time_t ipsa_hardaddlt; /* Seconds of hard lifetime after add. */ 227218893Sdim time_t ipsa_harduselt; /* Seconds of hard lifetime after first use. */ 228203954Srdivacky time_t ipsa_idleaddlt; /* Seconds of idle time after add */ 229203954Srdivacky time_t ipsa_idleuselt; /* Seconds of idle time after first use */ 230203954Srdivacky uint64_t ipsa_softbyteslt; /* Bytes of soft lifetime. */ 231203954Srdivacky uint64_t ipsa_hardbyteslt; /* Bytes of hard lifetime. */ 232203954Srdivacky uint64_t ipsa_bytes; /* Bytes encrypted/authed by this SA. */ 233218893Sdim 234203954Srdivacky /* 235203954Srdivacky * "Allocations" are a concept mentioned in PF_KEYv2. We do not 236218893Sdim * support them, except to record them per the PF_KEYv2 spec. 237203954Srdivacky */ 238208599Srdivacky uint_t ipsa_softalloc; /* Allocations allowed (soft). */ 239203954Srdivacky uint_t ipsa_hardalloc; /* Allocations allowed (hard). */ 240203954Srdivacky uint_t ipsa_alloc; /* Allocations made. */ 241203954Srdivacky 242204642Srdivacky uint_t ipsa_type; /* Type of security association. (AH/etc.) */ 243204642Srdivacky uint_t ipsa_state; /* State of my association. */ 244218893Sdim uint_t ipsa_replay_wsize; /* Size of replay window */ 245204642Srdivacky uint32_t ipsa_flags; /* Flags for security association. */ 246204642Srdivacky uint32_t ipsa_spi; /* Security parameters index. */ 247204642Srdivacky uint32_t ipsa_replay; /* Highest seen replay value for this SA. */ 248204642Srdivacky uint32_t ipsa_kmp; /* key management proto */ 249204642Srdivacky uint32_t ipsa_kmc; /* key management cookie */ 250204642Srdivacky 251218893Sdim boolean_t ipsa_haspeer; /* Has peer in another table. */ 252203954Srdivacky 253204642Srdivacky /* 254218893Sdim * Address storage. 255203954Srdivacky * The source address can be INADDR_ANY, IN6ADDR_ANY, etc. 256204642Srdivacky * 257218893Sdim * Address families (per sys/socket.h) guide us. We could have just 258203954Srdivacky * used sockaddr_storage 259204642Srdivacky */ 260204642Srdivacky sa_family_t ipsa_addrfam; 261204642Srdivacky sa_family_t ipsa_innerfam; /* Inner AF can be != src/dst AF. */ 262204642Srdivacky 263204642Srdivacky uint32_t ipsa_srcaddr[IPSA_MAX_ADDRLEN]; 264204642Srdivacky uint32_t ipsa_dstaddr[IPSA_MAX_ADDRLEN]; 265204642Srdivacky uint32_t ipsa_innersrc[IPSA_MAX_ADDRLEN]; 266204792Srdivacky uint32_t ipsa_innerdst[IPSA_MAX_ADDRLEN]; 267204792Srdivacky 268204792Srdivacky uint8_t ipsa_innersrcpfx; 269204642Srdivacky uint8_t ipsa_innerdstpfx; 270203954Srdivacky 271218893Sdim uint16_t ipsa_inbound_cksum; /* cksum correction for inbound packets */ 272203954Srdivacky uint16_t ipsa_local_nat_port; /* Local NAT-T port. (0 --> 4500) */ 273203954Srdivacky uint16_t ipsa_remote_nat_port; /* The other port that isn't 4500 */ 274203954Srdivacky 275203954Srdivacky /* these can only be v4 */ 276203954Srdivacky uint32_t ipsa_natt_addr_loc; 277203954Srdivacky uint32_t ipsa_natt_addr_rem; 278203954Srdivacky 279203954Srdivacky /* 280218893Sdim * icmp type and code. *_end are to specify ranges. if only 281203954Srdivacky * a single value, * and *_end are the same value. 282203954Srdivacky */ 283203954Srdivacky uint8_t ipsa_icmp_type; 284203954Srdivacky uint8_t ipsa_icmp_type_end; 285203954Srdivacky uint8_t ipsa_icmp_code; 286203954Srdivacky uint8_t ipsa_icmp_code_end; 287203954Srdivacky 288203954Srdivacky /* 289203954Srdivacky * For the kernel crypto framework. 290203954Srdivacky */ 291218893Sdim crypto_key_t ipsa_kcfauthkey; /* authentication key */ 292203954Srdivacky crypto_key_t ipsa_kcfencrkey; /* encryption key */ 293204642Srdivacky crypto_ctx_template_t ipsa_authtmpl; /* auth context template */ 294204642Srdivacky crypto_ctx_template_t ipsa_encrtmpl; /* encr context template */ 295203954Srdivacky crypto_mechanism_t ipsa_amech; /* auth mech type and ICV len */ 296203954Srdivacky crypto_mechanism_t ipsa_emech; /* encr mech type */ 297204642Srdivacky size_t ipsa_mac_len; /* auth MAC/ICV length */ 298204642Srdivacky size_t ipsa_iv_len; /* encr IV length */ 299204642Srdivacky size_t ipsa_datalen; /* block length in bytes. */ 300204642Srdivacky 301204642Srdivacky /* 302204642Srdivacky * Input and output processing functions called from IP. 303204642Srdivacky * The mblk_t is the data; the IPsec information is in the attributes 304203954Srdivacky * Returns NULL if the mblk is consumed which it is if there was 305204642Srdivacky * a failure or if pending. If failure then 306203954Srdivacky * the ipIfInDiscards/OutDiscards counters are increased. 307204642Srdivacky */ 308203954Srdivacky mblk_t *(*ipsa_output_func)(mblk_t *, ip_xmit_attr_t *); 309203954Srdivacky mblk_t *(*ipsa_input_func)(mblk_t *, void *, ip_recv_attr_t *); 310204642Srdivacky 311203954Srdivacky /* 312204642Srdivacky * Soft reference to paired SA 313203954Srdivacky */ 314203954Srdivacky uint32_t ipsa_otherspi; 315203954Srdivacky netstack_t *ipsa_netstack; /* Does not have a netstack_hold */ 316203954Srdivacky 317218893Sdim ts_label_t *ipsa_tsl; /* MLS: label attributes */ 318203954Srdivacky ts_label_t *ipsa_otsl; /* MLS: outer label */ 319204642Srdivacky uint8_t ipsa_mac_exempt; /* MLS: mac exempt flag */ 320218893Sdim uchar_t ipsa_opt_storage[IP_MAX_OPT_LENGTH]; 321204642Srdivacky} ipsa_t; 322204642Srdivacky 323204642Srdivacky/* 324204642Srdivacky * ipsa_t address handling macros. We want these to be inlined, and deal 325218893Sdim * with 32-bit words to avoid bcmp/bcopy calls. 326203954Srdivacky * 327203954Srdivacky * Assume we only have AF_INET and AF_INET6 addresses for now. Also assume 328203954Srdivacky * that we have 32-bit alignment on everything. 329204642Srdivacky */ 330204642Srdivacky#define IPSA_IS_ADDR_UNSPEC(addr, fam) ((((uint32_t *)(addr))[0] == 0) && \ 331204642Srdivacky (((fam) == AF_INET) || (((uint32_t *)(addr))[3] == 0 && \ 332204642Srdivacky ((uint32_t *)(addr))[2] == 0 && ((uint32_t *)(addr))[1] == 0))) 333204642Srdivacky#define IPSA_ARE_ADDR_EQUAL(addr1, addr2, fam) \ 334204642Srdivacky ((((uint32_t *)(addr1))[0] == ((uint32_t *)(addr2))[0]) && \ 335204642Srdivacky (((fam) == AF_INET) || \ 336218893Sdim (((uint32_t *)(addr1))[3] == ((uint32_t *)(addr2))[3] && \ 337204642Srdivacky ((uint32_t *)(addr1))[2] == ((uint32_t *)(addr2))[2] && \ 338204642Srdivacky ((uint32_t *)(addr1))[1] == ((uint32_t *)(addr2))[1]))) 339203954Srdivacky#define IPSA_COPY_ADDR(dstaddr, srcaddr, fam) { \ 340203954Srdivacky ((uint32_t *)(dstaddr))[0] = ((uint32_t *)(srcaddr))[0]; \ 341204642Srdivacky if ((fam) == AF_INET6) {\ 342204642Srdivacky ((uint32_t *)(dstaddr))[1] = ((uint32_t *)(srcaddr))[1]; \ 343204642Srdivacky ((uint32_t *)(dstaddr))[2] = ((uint32_t *)(srcaddr))[2]; \ 344204642Srdivacky ((uint32_t *)(dstaddr))[3] = ((uint32_t *)(srcaddr))[3]; } } 345204642Srdivacky 346204642Srdivacky/* 347204642Srdivacky * ipsa_t reference hold/release macros. 348204642Srdivacky * 349204642Srdivacky * If you have a pointer, you REFHOLD. If you are releasing a pointer, you 350204642Srdivacky * REFRELE. An ipsa_t that is newly inserted into the table should have 351204642Srdivacky * a reference count of 1 (for the table's pointer), plus 1 more for every 352204642Srdivacky * pointer that is referencing the ipsa_t. 353204642Srdivacky */ 354204642Srdivacky 355204642Srdivacky#define IPSA_REFHOLD(ipsa) { \ 356204642Srdivacky atomic_add_32(&(ipsa)->ipsa_refcnt, 1); \ 357204642Srdivacky ASSERT((ipsa)->ipsa_refcnt != 0); \ 358204642Srdivacky} 359204642Srdivacky 360204642Srdivacky/* 361204642Srdivacky * Decrement the reference count on the SA. 362204642Srdivacky * In architectures e.g sun4u, where atomic_add_32_nv is just 363204642Srdivacky * a cas, we need to maintain the right memory barrier semantics 364203954Srdivacky * as that of mutex_exit i.e all the loads and stores should complete 365203954Srdivacky * before the cas is executed. membar_exit() does that here. 366203954Srdivacky */ 367218893Sdim 368203954Srdivacky#define IPSA_REFRELE(ipsa) { \ 369203954Srdivacky ASSERT((ipsa)->ipsa_refcnt != 0); \ 370203954Srdivacky membar_exit(); \ 371218893Sdim if (atomic_add_32_nv(&(ipsa)->ipsa_refcnt, -1) == 0) \ 372203954Srdivacky ((ipsa)->ipsa_freefunc)(ipsa); \ 373203954Srdivacky} 374203954Srdivacky 375203954Srdivacky/* 376203954Srdivacky * Security association hash macros and definitions. For now, assume the 377203954Srdivacky * IPsec model, and hash outbounds on destination address, and inbounds on 378203954Srdivacky * SPI. 379203954Srdivacky */ 380218893Sdim 381218893Sdim#define IPSEC_DEFAULT_HASH_SIZE 256 382203954Srdivacky 383218893Sdim#define INBOUND_HASH(sadb, spi) ((spi) % ((sadb)->sdb_hashsize)) 384203954Srdivacky#define OUTBOUND_HASH_V4(sadb, v4addr) ((v4addr) % ((sadb)->sdb_hashsize)) 385204642Srdivacky#define OUTBOUND_HASH_V6(sadb, v6addr) OUTBOUND_HASH_V4((sadb), \ 386203954Srdivacky (*(uint32_t *)&(v6addr)) ^ (*(((uint32_t *)&(v6addr)) + 1)) ^ \ 387203954Srdivacky (*(((uint32_t *)&(v6addr)) + 2)) ^ (*(((uint32_t *)&(v6addr)) + 3))) 388204642Srdivacky 389218893Sdim/* 390218893Sdim * Syntactic sugar to find the appropriate hash bucket directly. 391204642Srdivacky */ 392218893Sdim 393218893Sdim#define INBOUND_BUCKET(sadb, spi) &(((sadb)->sdb_if)[INBOUND_HASH(sadb, spi)]) 394204642Srdivacky#define OUTBOUND_BUCKET_V4(sadb, v4addr) \ 395204642Srdivacky &(((sadb)->sdb_of)[OUTBOUND_HASH_V4(sadb, v4addr)]) 396218893Sdim#define OUTBOUND_BUCKET_V6(sadb, v6addr) \ 397204642Srdivacky &(((sadb)->sdb_of)[OUTBOUND_HASH_V6(sadb, v6addr)]) 398218893Sdim 399218893Sdim#define IPSA_F_PFS SADB_SAFLAGS_PFS /* PFS in use for this SA? */ 400204642Srdivacky#define IPSA_F_NOREPFLD SADB_SAFLAGS_NOREPLAY /* No replay field, for */ 401218893Sdim /* backward compat. */ 402218893Sdim#define IPSA_F_USED SADB_X_SAFLAGS_USED /* SA has been used. */ 403218893Sdim#define IPSA_F_UNIQUE SADB_X_SAFLAGS_UNIQUE /* SA is unique */ 404218893Sdim#define IPSA_F_AALG1 SADB_X_SAFLAGS_AALG1 /* Auth alg flag 1 */ 405218893Sdim#define IPSA_F_AALG2 SADB_X_SAFLAGS_AALG2 /* Auth alg flag 2 */ 406218893Sdim#define IPSA_F_EALG1 SADB_X_SAFLAGS_EALG1 /* Encrypt alg flag 1 */ 407218893Sdim#define IPSA_F_EALG2 SADB_X_SAFLAGS_EALG2 /* Encrypt alg flag 2 */ 408203954Srdivacky 409203954Srdivacky#define IPSA_F_ASYNC 0x200000 /* Call KCF asynchronously? */ 410203954Srdivacky#define IPSA_F_NATT_LOC SADB_X_SAFLAGS_NATT_LOC 411204642Srdivacky#define IPSA_F_NATT_REM SADB_X_SAFLAGS_NATT_REM 412203954Srdivacky#define IPSA_F_BEHIND_NAT SADB_X_SAFLAGS_NATTED 413204642Srdivacky#define IPSA_F_NATT (SADB_X_SAFLAGS_NATT_LOC | SADB_X_SAFLAGS_NATT_REM | \ 414203954Srdivacky SADB_X_SAFLAGS_NATTED) 415203954Srdivacky#define IPSA_F_CINVALID 0x40000 /* SA shouldn't be cached */ 416203954Srdivacky#define IPSA_F_PAIRED SADB_X_SAFLAGS_PAIRED /* SA is one of a pair */ 417203954Srdivacky#define IPSA_F_OUTBOUND SADB_X_SAFLAGS_OUTBOUND /* SA direction bit */ 418203954Srdivacky#define IPSA_F_INBOUND SADB_X_SAFLAGS_INBOUND /* SA direction bit */ 419203954Srdivacky#define IPSA_F_TUNNEL SADB_X_SAFLAGS_TUNNEL 420203954Srdivacky/* 421203954Srdivacky * These flags are only defined here to prevent a flag value collision. 422203954Srdivacky */ 423206083Srdivacky#define IPSA_F_COMBINED SADB_X_SAFLAGS_EALG1 /* Defined in pfkeyv2.h */ 424218893Sdim#define IPSA_F_COUNTERMODE SADB_X_SAFLAGS_EALG2 /* Defined in pfkeyv2.h */ 425206083Srdivacky 426206083Srdivacky/* 427206083Srdivacky * Sets of flags that are allowed to by set or modified by PF_KEY apps. 428203954Srdivacky */ 429206083Srdivacky#define AH_UPDATE_SETTABLE_FLAGS \ 430203954Srdivacky (SADB_X_SAFLAGS_PAIRED | SADB_SAFLAGS_NOREPLAY | \ 431218893Sdim SADB_X_SAFLAGS_OUTBOUND | SADB_X_SAFLAGS_INBOUND | \ 432203954Srdivacky SADB_X_SAFLAGS_KM1 | SADB_X_SAFLAGS_KM2 | \ 433203954Srdivacky SADB_X_SAFLAGS_KM3 | SADB_X_SAFLAGS_KM4) 434203954Srdivacky 435203954Srdivacky/* AH can't set NAT flags (or even use NAT). Add NAT flags to the ESP set. */ 436203954Srdivacky#define ESP_UPDATE_SETTABLE_FLAGS (AH_UPDATE_SETTABLE_FLAGS | IPSA_F_NATT) 437204642Srdivacky 438204642Srdivacky#define AH_ADD_SETTABLE_FLAGS \ 439203954Srdivacky (AH_UPDATE_SETTABLE_FLAGS | SADB_X_SAFLAGS_AALG1 | \ 440203954Srdivacky SADB_X_SAFLAGS_AALG2 | SADB_X_SAFLAGS_TUNNEL | \ 441203954Srdivacky SADB_SAFLAGS_NOREPLAY) 442203954Srdivacky 443203954Srdivacky/* AH can't set NAT flags (or even use NAT). Add NAT flags to the ESP set. */ 444203954Srdivacky#define ESP_ADD_SETTABLE_FLAGS (AH_ADD_SETTABLE_FLAGS | IPSA_F_NATT | \ 445204642Srdivacky SADB_X_SAFLAGS_EALG1 | SADB_X_SAFLAGS_EALG2) 446203954Srdivacky 447203954Srdivacky 448203954Srdivacky 449218893Sdim/* SA states are important for handling UPDATE PF_KEY messages. */ 450203954Srdivacky#define IPSA_STATE_LARVAL SADB_SASTATE_LARVAL 451203954Srdivacky#define IPSA_STATE_MATURE SADB_SASTATE_MATURE 452203954Srdivacky#define IPSA_STATE_DYING SADB_SASTATE_DYING 453203954Srdivacky#define IPSA_STATE_DEAD SADB_SASTATE_DEAD 454218893Sdim#define IPSA_STATE_IDLE SADB_X_SASTATE_IDLE 455204961Srdivacky#define IPSA_STATE_ACTIVE_ELSEWHERE SADB_X_SASTATE_ACTIVE_ELSEWHERE 456204961Srdivacky 457204961Srdivacky/* 458218893Sdim * NOTE: If the document authors do things right in defining algorithms, we'll 459206083Srdivacky * probably have flags for what all is here w.r.t. replay, ESP w/HMAC, 460206083Srdivacky * etc. 461206083Srdivacky */ 462203954Srdivacky 463203954Srdivacky#define IPSA_T_ACQUIRE SEC_TYPE_NONE /* If this typed returned, sa needed */ 464204642Srdivacky#define IPSA_T_AH SEC_TYPE_AH /* IPsec AH association */ 465204642Srdivacky#define IPSA_T_ESP SEC_TYPE_ESP /* IPsec ESP association */ 466204642Srdivacky 467204642Srdivacky#define IPSA_AALG_NONE SADB_AALG_NONE /* No auth. algorithm */ 468204642Srdivacky#define IPSA_AALG_MD5H SADB_AALG_MD5HMAC /* MD5-HMAC algorithm */ 469204642Srdivacky#define IPSA_AALG_SHA1H SADB_AALG_SHA1HMAC /* SHA1-HMAC algorithm */ 470204642Srdivacky 471204642Srdivacky#define IPSA_EALG_NONE SADB_EALG_NONE /* No encryption algorithm */ 472204642Srdivacky#define IPSA_EALG_DES_CBC SADB_EALG_DESCBC 473204642Srdivacky#define IPSA_EALG_3DES SADB_EALG_3DESCBC 474204642Srdivacky 475204642Srdivacky/* 476204642Srdivacky * Protect each ipsa_t bucket (and linkage) with a lock. 477218893Sdim */ 478204642Srdivacky 479204642Srdivackytypedef struct isaf_s { 480204642Srdivacky ipsa_t *isaf_ipsa; 481204642Srdivacky kmutex_t isaf_lock; 482218893Sdim uint64_t isaf_gen; 483204792Srdivacky} isaf_t; 484204792Srdivacky 485218893Sdim/* 486203954Srdivacky * ACQUIRE record. If AH/ESP/whatever cannot find an association for outbound 487203954Srdivacky * traffic, it sends up an SADB_ACQUIRE message and create an ACQUIRE record. 488203954Srdivacky */ 489204642Srdivacky 490218893Sdim#define IPSACQ_MAXPACKETS 4 /* Number of packets that can be queued up */ 491204792Srdivacky /* waiting for an ACQUIRE to finish. */ 492204792Srdivacky 493204792Srdivackytypedef struct ipsacq_s { 494204792Srdivacky struct ipsacq_s *ipsacq_next; 495204792Srdivacky struct ipsacq_s **ipsacq_ptpn; 496204792Srdivacky kmutex_t *ipsacq_linklock; 497218893Sdim struct ipsec_policy_s *ipsacq_policy; 498204792Srdivacky struct ipsec_action_s *ipsacq_act; 499204792Srdivacky 500204642Srdivacky sa_family_t ipsacq_addrfam; /* Address family. */ 501204792Srdivacky sa_family_t ipsacq_inneraddrfam; /* Inner-packet address family. */ 502204792Srdivacky int ipsacq_numpackets; /* How many packets queued up so far. */ 503204792Srdivacky uint32_t ipsacq_seq; /* PF_KEY sequence number. */ 504204792Srdivacky uint64_t ipsacq_unique_id; /* Unique ID for SAs that need it. */ 505204792Srdivacky 506218893Sdim kmutex_t ipsacq_lock; /* Protects non-linkage fields. */ 507204792Srdivacky time_t ipsacq_expire; /* Wall-clock time when this record expires. */ 508204792Srdivacky mblk_t *ipsacq_mp; /* List of datagrams waiting for an SA. */ 509218893Sdim 510204792Srdivacky /* These two point inside the last mblk inserted. */ 511204792Srdivacky uint32_t *ipsacq_srcaddr; 512204792Srdivacky uint32_t *ipsacq_dstaddr; 513204792Srdivacky 514218893Sdim /* Cache these instead of point so we can mask off accordingly */ 515204792Srdivacky uint32_t ipsacq_innersrc[IPSA_MAX_ADDRLEN]; 516204792Srdivacky uint32_t ipsacq_innerdst[IPSA_MAX_ADDRLEN]; 517204792Srdivacky 518204792Srdivacky /* These may change per-acquire. */ 519204792Srdivacky uint16_t ipsacq_srcport; 520204792Srdivacky uint16_t ipsacq_dstport; 521204792Srdivacky uint8_t ipsacq_proto; 522218893Sdim uint8_t ipsacq_inner_proto; 523204792Srdivacky uint8_t ipsacq_innersrcpfx; 524204792Srdivacky uint8_t ipsacq_innerdstpfx; 525204792Srdivacky 526204792Srdivacky /* icmp type and code of triggering packet (if applicable) */ 527204792Srdivacky uint8_t ipsacq_icmp_type; 528218893Sdim uint8_t ipsacq_icmp_code; 529218893Sdim 530204792Srdivacky /* label associated with triggering packet */ 531204792Srdivacky ts_label_t *ipsacq_tsl; 532218893Sdim} ipsacq_t; 533204642Srdivacky 534203954Srdivacky/* 535203954Srdivacky * Kernel-generated sequence numbers will be no less than 0x80000000 to 536203954Srdivacky * forestall any cretinous problems with manual keying accidentally updating 537204642Srdivacky * an ACQUIRE entry. 538204642Srdivacky */ 539204642Srdivacky#define IACQF_LOWEST_SEQ 0x80000000 540204642Srdivacky 541204642Srdivacky#define SADB_AGE_INTERVAL_DEFAULT 8000 542204642Srdivacky 543204642Srdivacky/* 544218893Sdim * ACQUIRE fanout. Protect each linkage with a lock. 545204642Srdivacky */ 546204642Srdivacky 547204642Srdivackytypedef struct iacqf_s { 548204792Srdivacky ipsacq_t *iacqf_ipsacq; 549204792Srdivacky kmutex_t iacqf_lock; 550204792Srdivacky} iacqf_t; 551204792Srdivacky 552204792Srdivacky/* 553204792Srdivacky * A (network protocol, ipsec protocol) specific SADB. 554204792Srdivacky * (i.e., one each for {ah, esp} and {v4, v6}. 555218893Sdim * 556204642Srdivacky * Keep outbound assocs in a simple hash table for now. 557204642Srdivacky * One danger point, multiple SAs for a single dest will clog a bucket. 558204642Srdivacky * For the future, consider two-level hashing (2nd hash on IPC?), then probe. 559204792Srdivacky */ 560204642Srdivacky 561204642Srdivackytypedef struct sadb_s 562204642Srdivacky{ 563204792Srdivacky isaf_t *sdb_of; 564204792Srdivacky isaf_t *sdb_if; 565204642Srdivacky iacqf_t *sdb_acq; 566204642Srdivacky int sdb_hashsize; 567204642Srdivacky} sadb_t; 568204642Srdivacky 569204642Srdivacky/* 570204642Srdivacky * A pair of SADB's (one for v4, one for v6), and related state (including 571204642Srdivacky * acquire callbacks). 572204642Srdivacky */ 573204642Srdivacky 574204642Srdivackytypedef struct sadbp_s 575204642Srdivacky{ 576218893Sdim uint32_t s_satype; 577204642Srdivacky uint32_t *s_acquire_timeout; 578204642Srdivacky void (*s_acqfn)(ipsacq_t *, mblk_t *, netstack_t *); 579204642Srdivacky sadb_t s_v4; 580204642Srdivacky sadb_t s_v6; 581204642Srdivacky uint32_t s_addflags; 582204642Srdivacky uint32_t s_updateflags; 583218893Sdim} sadbp_t; 584204642Srdivacky 585205407Srdivacky/* 586204642Srdivacky * A pair of SA's for a single connection, the structure contains a 587204642Srdivacky * pointer to a SA and the SA its paired with (opposite direction) as well 588204642Srdivacky * as the SA's respective hash buckets. 589218893Sdim */ 590204642Srdivackytypedef struct ipsap_s 591204642Srdivacky{ 592204642Srdivacky boolean_t in_inbound_table; 593221345Sdim isaf_t *ipsap_bucket; 594221345Sdim ipsa_t *ipsap_sa_ptr; 595204642Srdivacky isaf_t *ipsap_pbucket; 596204642Srdivacky ipsa_t *ipsap_psa_ptr; 597204642Srdivacky} ipsap_t; 598218893Sdim 599204642Srdivackytypedef struct templist_s 600205407Srdivacky{ 601204642Srdivacky ipsa_t *ipsa; 602204642Srdivacky struct templist_s *next; 603204642Srdivacky} templist_t; 604218893Sdim 605204642Srdivacky/* Pointer to an all-zeroes IPv6 address. */ 606204642Srdivacky#define ALL_ZEROES_PTR ((uint32_t *)&ipv6_all_zeros) 607204642Srdivacky 608204642Srdivacky/* 609204642Srdivacky * Form unique id from ip_xmit_attr_t. 610204642Srdivacky */ 611204642Srdivacky#define SA_FORM_UNIQUE_ID(ixa) \ 612204642Srdivacky SA_UNIQUE_ID((ixa)->ixa_ipsec_src_port, (ixa)->ixa_ipsec_dst_port, \ 613208599Srdivacky (((ixa)->ixa_flags & IXAF_IPSEC_TUNNEL) ? \ 614208599Srdivacky ((ixa)->ixa_ipsec_inaf == AF_INET6 ? \ 615208599Srdivacky IPPROTO_IPV6 : IPPROTO_ENCAP) : \ 616208599Srdivacky (ixa)->ixa_ipsec_proto), \ 617208599Srdivacky (((ixa)->ixa_flags & IXAF_IPSEC_TUNNEL) ? \ 618208599Srdivacky (ixa)->ixa_ipsec_proto : 0)) 619208599Srdivacky 620208599Srdivacky/* 621204642Srdivacky * This macro is used to generate unique ids (along with the addresses, both 622218893Sdim * inner and outer) for outbound datagrams that require unique SAs. 623204642Srdivacky * 624204642Srdivacky * N.B. casts and unsigned shift amounts discourage unwarranted 625204642Srdivacky * sign extension of dstport, proto, and iproto. 626204642Srdivacky * 627204642Srdivacky * Unique ID is 64-bits allocated as follows (pardon my big-endian bias): 628218893Sdim * 629204642Srdivacky * 6 4 43 33 11 630204642Srdivacky * 3 7 09 21 65 0 631204642Srdivacky * +---------------*-------+-------+--------------+---------------+ 632218893Sdim * | MUST-BE-ZERO |<iprot>|<proto>| <src port> | <dest port> | 633204642Srdivacky * +---------------*-------+-------+--------------+---------------+ 634204642Srdivacky * 635204642Srdivacky * If there are inner addresses (tunnel mode) the ports come from the 636204642Srdivacky * inner addresses. If there are no inner addresses, the ports come from 637204642Srdivacky * the outer addresses (transport mode). Tunnel mode MUST have <proto> 638204642Srdivacky * set to either IPPROTO_ENCAP or IPPPROTO_IPV6. 639204642Srdivacky */ 640204642Srdivacky#define SA_UNIQUE_ID(srcport, dstport, proto, iproto) \ 641218893Sdim ((srcport) | ((uint64_t)(dstport) << 16U) | \ 642204642Srdivacky ((uint64_t)(proto) << 32U) | ((uint64_t)(iproto) << 40U)) 643204642Srdivacky 644204642Srdivacky/* 645218893Sdim * SA_UNIQUE_MASK generates a mask value to use when comparing the unique value 646204642Srdivacky * from a packet to an SA. 647204642Srdivacky */ 648204642Srdivacky 649223017Sdim#define SA_UNIQUE_MASK(srcport, dstport, proto, iproto) \ 650223017Sdim SA_UNIQUE_ID((srcport != 0) ? 0xffff : 0, \ 651223017Sdim (dstport != 0) ? 0xffff : 0, \ 652223017Sdim (proto != 0) ? 0xff : 0, \ 653223017Sdim (iproto != 0) ? 0xff : 0) 654223017Sdim 655223017Sdim/* 656223017Sdim * Decompose unique id back into its original fields. 657223017Sdim */ 658223017Sdim#define SA_IPROTO(ipsa) ((ipsa)->ipsa_unique_id>>40)&0xff 659223017Sdim#define SA_PROTO(ipsa) ((ipsa)->ipsa_unique_id>>32)&0xff 660223017Sdim#define SA_SRCPORT(ipsa) ((ipsa)->ipsa_unique_id & 0xffff) 661223017Sdim#define SA_DSTPORT(ipsa) (((ipsa)->ipsa_unique_id >> 16) & 0xffff) 662223017Sdim 663223017Sdimtypedef struct ipsa_query_s ipsa_query_t; 664223017Sdim 665223017Sdimtypedef boolean_t (*ipsa_match_fn_t)(ipsa_query_t *, ipsa_t *); 666223017Sdim 667223017Sdim#define IPSA_NMATCH 10 668223017Sdim 669223017Sdim/* 670223017Sdim * SADB query structure. 671223017Sdim * 672223017Sdim * Provide a generalized mechanism for matching entries in the SADB; 673223017Sdim * one of these structures is initialized using sadb_form_query(), 674223017Sdim * and then can be used as a parameter to sadb_match_query() which returns 675223017Sdim * B_TRUE if the SA matches the query. 676223017Sdim * 677223017Sdim * Under the covers, sadb_form_query populates the matchers[] array with 678204642Srdivacky * functions which are called one at a time until one fails to match. 679204642Srdivacky */ 680204642Srdivackystruct ipsa_query_s { 681204642Srdivacky uint32_t req, match; 682204642Srdivacky sadb_address_t *srcext, *dstext; 683205407Srdivacky sadb_ident_t *srcid, *dstid; 684204642Srdivacky sadb_x_kmc_t *kmcext; 685218893Sdim sadb_sa_t *assoc; 686204642Srdivacky uint32_t spi; 687204642Srdivacky struct sockaddr_in *src; 688204642Srdivacky struct sockaddr_in6 *src6; 689204642Srdivacky struct sockaddr_in *dst; 690204642Srdivacky struct sockaddr_in6 *dst6; 691204642Srdivacky sa_family_t af; 692204642Srdivacky uint32_t *srcaddr, *dstaddr; 693204642Srdivacky uint32_t ifindex; 694218893Sdim uint32_t kmc, kmp; 695204642Srdivacky char *didstr, *sidstr; 696204642Srdivacky uint16_t didtype, sidtype; 697218893Sdim sadbp_t *spp; 698204642Srdivacky sadb_t *sp; 699204642Srdivacky isaf_t *inbound, *outbound; 700218893Sdim uint32_t outhash; 701218893Sdim uint32_t inhash; 702204642Srdivacky ipsa_match_fn_t matchers[IPSA_NMATCH]; 703204642Srdivacky}; 704218893Sdim 705218893Sdim#define IPSA_Q_SA 0x00000001 706218893Sdim#define IPSA_Q_DST 0x00000002 707204642Srdivacky#define IPSA_Q_SRC 0x00000004 708204642Srdivacky#define IPSA_Q_DSTID 0x00000008 709218893Sdim#define IPSA_Q_SRCID 0x00000010 710204642Srdivacky#define IPSA_Q_KMC 0x00000020 711204642Srdivacky#define IPSA_Q_INBOUND 0x00000040 /* fill in inbound isaf_t */ 712204642Srdivacky#define IPSA_Q_OUTBOUND 0x00000080 /* fill in outbound isaf_t */ 713204642Srdivacky 714218893Sdimint sadb_form_query(keysock_in_t *, uint32_t, uint32_t, ipsa_query_t *, int *); 715204642Srdivackyboolean_t sadb_match_query(ipsa_query_t *q, ipsa_t *sa); 716204642Srdivacky 717204642Srdivacky 718204642Srdivacky/* 719204642Srdivacky * All functions that return an ipsa_t will return it with IPSA_REFHOLD() 720204642Srdivacky * already called. 721204642Srdivacky */ 722204642Srdivacky 723218893Sdim/* SA retrieval (inbound and outbound) */ 724204642Srdivackyipsa_t *ipsec_getassocbyspi(isaf_t *, uint32_t, uint32_t *, uint32_t *, 725218893Sdim sa_family_t); 726204642Srdivackyipsa_t *ipsec_getassocbyconn(isaf_t *, ip_xmit_attr_t *, uint32_t *, uint32_t *, 727218893Sdim sa_family_t, uint8_t, ts_label_t *); 728204642Srdivacky 729204642Srdivacky/* SA insertion. */ 730204642Srdivackyint sadb_insertassoc(ipsa_t *, isaf_t *); 731204642Srdivacky 732204642Srdivacky/* SA table construction and destruction. */ 733212904Sdimvoid sadbp_init(const char *name, sadbp_t *, int, int, netstack_t *); 734218893Sdimvoid sadbp_flush(sadbp_t *, netstack_t *); 735204642Srdivackyvoid sadbp_destroy(sadbp_t *, netstack_t *); 736204642Srdivacky 737204642Srdivacky/* SA insertion and deletion. */ 738204642Srdivackyint sadb_insertassoc(ipsa_t *, isaf_t *); 739218893Sdimvoid sadb_unlinkassoc(ipsa_t *); 740206083Srdivacky 741218893Sdim/* Support routines to interface a keysock consumer to PF_KEY. */ 742204642Srdivackymblk_t *sadb_keysock_out(minor_t); 743204642Srdivackyint sadb_hardsoftchk(sadb_lifetime_t *, sadb_lifetime_t *, sadb_lifetime_t *); 744206083Srdivackyint sadb_labelchk(struct keysock_in_s *); 745206083Srdivackyvoid sadb_pfkey_echo(queue_t *, mblk_t *, sadb_msg_t *, struct keysock_in_s *, 746206083Srdivacky ipsa_t *); 747218893Sdimvoid sadb_pfkey_error(queue_t *, mblk_t *, int, int, uint_t); 748206083Srdivackyvoid sadb_keysock_hello(queue_t **, queue_t *, mblk_t *, void (*)(void *), 749206083Srdivacky void *, timeout_id_t *, int); 750206083Srdivackyint sadb_addrcheck(queue_t *, mblk_t *, sadb_ext_t *, uint_t, netstack_t *); 751206083Srdivackyboolean_t sadb_addrfix(keysock_in_t *, queue_t *, mblk_t *, netstack_t *); 752218893Sdimint sadb_addrset(ire_t *); 753204642Srdivackyint sadb_delget_sa(mblk_t *, keysock_in_t *, sadbp_t *, int *, queue_t *, 754204642Srdivacky uint8_t); 755218893Sdim 756218893Sdimint sadb_purge_sa(mblk_t *, keysock_in_t *, sadb_t *, int *, queue_t *); 757218893Sdimint sadb_common_add(queue_t *, mblk_t *, sadb_msg_t *, 758204642Srdivacky keysock_in_t *, isaf_t *, isaf_t *, ipsa_t *, boolean_t, boolean_t, int *, 759204642Srdivacky netstack_t *, sadbp_t *); 760204642Srdivackyvoid sadb_set_usetime(ipsa_t *); 761204642Srdivackyboolean_t sadb_age_bytes(queue_t *, ipsa_t *, uint64_t, boolean_t); 762204642Srdivackyint sadb_update_sa(mblk_t *, keysock_in_t *, mblk_t **, sadbp_t *, 763204642Srdivacky int *, queue_t *, int (*)(mblk_t *, keysock_in_t *, int *, netstack_t *), 764205407Srdivacky netstack_t *, uint8_t); 765218893Sdimvoid sadb_acquire(mblk_t *, ip_xmit_attr_t *, boolean_t, boolean_t); 766218893Sdimvoid gcm_params_init(ipsa_t *, uchar_t *, uint_t, uchar_t *, ipsa_cm_mech_t *, 767218893Sdim crypto_data_t *); 768204642Srdivackyvoid ccm_params_init(ipsa_t *, uchar_t *, uint_t, uchar_t *, ipsa_cm_mech_t *, 769218893Sdim crypto_data_t *); 770218893Sdimvoid cbc_params_init(ipsa_t *, uchar_t *, uint_t, uchar_t *, ipsa_cm_mech_t *, 771218893Sdim crypto_data_t *); 772204642Srdivacky 773204642Srdivackyvoid sadb_destroy_acquire(ipsacq_t *, netstack_t *); 774206083Srdivackystruct ipsec_stack; 775206083Srdivackymblk_t *sadb_setup_acquire(ipsacq_t *, uint8_t, struct ipsec_stack *); 776218893Sdimipsa_t *sadb_getspi(keysock_in_t *, uint32_t, int *, netstack_t *, uint_t); 777204642Srdivackyvoid sadb_in_acquire(sadb_msg_t *, sadbp_t *, queue_t *, netstack_t *); 778204642Srdivackyboolean_t sadb_replay_check(ipsa_t *, uint32_t); 779204642Srdivackyboolean_t sadb_replay_peek(ipsa_t *, uint32_t); 780204642Srdivackyint sadb_dump(queue_t *, mblk_t *, keysock_in_t *, sadb_t *); 781204642Srdivackyvoid sadb_replay_delete(ipsa_t *); 782206083Srdivackyvoid sadb_ager(sadb_t *, queue_t *, int, netstack_t *); 783205407Srdivacky 784205407Srdivackytimeout_id_t sadb_retimeout(hrtime_t, queue_t *, void (*)(void *), void *, 785205407Srdivacky uint_t *, uint_t, short); 786206083Srdivackyvoid sadb_sa_refrele(void *target); 787205407Srdivackyboolean_t sadb_set_lpkt(ipsa_t *, mblk_t *, ip_recv_attr_t *); 788218893Sdimmblk_t *sadb_clear_lpkt(ipsa_t *); 789205407Srdivackyvoid sadb_buf_pkt(ipsa_t *, mblk_t *, ip_recv_attr_t *); 790205407Srdivackyvoid sadb_clear_buf_pkt(void *ipkt); 791205407Srdivacky 792205407Srdivacky/* Note that buf_pkt is the product of ip_recv_attr_to_mblk() */ 793205407Srdivacky#define HANDLE_BUF_PKT(taskq, stack, dropper, buf_pkt) \ 794204642Srdivacky{ \ 795204642Srdivacky if (buf_pkt != NULL) { \ 796205407Srdivacky if (taskq_dispatch(taskq, sadb_clear_buf_pkt, \ 797205407Srdivacky (void *) buf_pkt, TQ_NOSLEEP) == 0) { \ 798205407Srdivacky /* Dispatch was unsuccessful drop the packets. */ \ 799204642Srdivacky mblk_t *tmp; \ 800205407Srdivacky while (buf_pkt != NULL) { \ 801205407Srdivacky tmp = buf_pkt->b_next; \ 802204642Srdivacky buf_pkt->b_next = NULL; \ 803218893Sdim buf_pkt = ip_recv_attr_free_mblk(buf_pkt); \ 804223017Sdim ip_drop_packet(buf_pkt, B_TRUE, NULL, \ 805223017Sdim DROPPER(stack, \ 806223017Sdim ipds_sadb_inidle_timeout), \ 807223017Sdim &dropper); \ 808204642Srdivacky buf_pkt = tmp; \ 809223017Sdim } \ 810223017Sdim } \ 811223017Sdim } \ 812223017Sdim} \ 813204642Srdivacky 814223017Sdim/* 815223017Sdim * Two IPsec rate-limiting routines. 816223017Sdim */ 817223017Sdim/*PRINTFLIKE6*/ 818223017Sdimextern void ipsec_rl_strlog(netstack_t *, short, short, char, 819223017Sdim ushort_t, char *, ...) 820223017Sdim __KPRINTFLIKE(6); 821223017Sdimextern void ipsec_assocfailure(short, short, char, ushort_t, char *, uint32_t, 822223017Sdim void *, int, netstack_t *); 823223017Sdim 824223017Sdim/* 825218893Sdim * Algorithm types. 826206083Srdivacky */ 827218893Sdim 828204642Srdivacky#define IPSEC_NALGTYPES 2 829204642Srdivacky 830204642Srdivackytypedef enum ipsec_algtype { 831218893Sdim IPSEC_ALG_AUTH = 0, 832204642Srdivacky IPSEC_ALG_ENCR = 1, 833204642Srdivacky IPSEC_ALG_ALL = 2 834218893Sdim} ipsec_algtype_t; 835218893Sdim 836204642Srdivacky/* 837218893Sdim * Definitions as per IPsec/ISAKMP DOI. 838204642Srdivacky */ 839204642Srdivacky 840204642Srdivacky#define IPSEC_MAX_ALGS 256 841204642Srdivacky#define PROTO_IPSEC_AH 2 842204642Srdivacky#define PROTO_IPSEC_ESP 3 843204642Srdivacky 844204642Srdivacky/* 845204642Srdivacky * Common algorithm info. 846204642Srdivacky */ 847204642Srdivackytypedef struct ipsec_alginfo 848204642Srdivacky{ 849218893Sdim uint8_t alg_id; 850204642Srdivacky uint8_t alg_flags; 851204642Srdivacky uint16_t *alg_key_sizes; 852204642Srdivacky uint16_t *alg_block_sizes; 853204642Srdivacky uint16_t *alg_params; 854204642Srdivacky uint16_t alg_nkey_sizes; 855204642Srdivacky uint16_t alg_ivlen; 856204642Srdivacky uint16_t alg_icvlen; 857204642Srdivacky uint8_t alg_saltlen; 858204642Srdivacky uint16_t alg_nblock_sizes; 859204642Srdivacky uint16_t alg_nparams; 860204642Srdivacky uint16_t alg_minbits; 861204642Srdivacky uint16_t alg_maxbits; 862204642Srdivacky uint16_t alg_datalen; 863204642Srdivacky /* 864204642Srdivacky * increment: number of bits from keysize to keysize 865204642Srdivacky * default: # of increments from min to default key len 866204642Srdivacky */ 867204642Srdivacky uint16_t alg_increment; 868204642Srdivacky uint16_t alg_default; 869204642Srdivacky uint16_t alg_default_bits; 870204642Srdivacky /* 871204642Srdivacky * Min, max, and default key sizes effectively supported 872204642Srdivacky * by the encryption framework. 873204642Srdivacky */ 874204642Srdivacky uint16_t alg_ef_minbits; 875204642Srdivacky uint16_t alg_ef_maxbits; 876204642Srdivacky uint16_t alg_ef_default; 877204642Srdivacky uint16_t alg_ef_default_bits; 878204642Srdivacky 879204642Srdivacky crypto_mech_type_t alg_mech_type; /* KCF mechanism type */ 880204642Srdivacky crypto_mech_name_t alg_mech_name; /* KCF mechanism name */ 881204642Srdivacky} ipsec_alginfo_t; 882204642Srdivacky 883204642Srdivacky#define alg_datalen alg_block_sizes[0] 884204642Srdivacky#define ALG_VALID(_alg) ((_alg)->alg_flags & ALG_FLAG_VALID) 885204642Srdivacky 886204642Srdivacky/* 887204642Srdivacky * Software crypto execution mode. 888218893Sdim */ 889204642Srdivackytypedef enum { 890204642Srdivacky IPSEC_ALGS_EXEC_SYNC = 0, 891204642Srdivacky IPSEC_ALGS_EXEC_ASYNC = 1 892204642Srdivacky} ipsec_algs_exec_mode_t; 893204642Srdivacky 894204642Srdivackyextern void ipsec_alg_reg(ipsec_algtype_t, ipsec_alginfo_t *, netstack_t *); 895206083Srdivackyextern void ipsec_alg_unreg(ipsec_algtype_t, uint8_t, netstack_t *); 896218893Sdimextern void ipsec_alg_fix_min_max(ipsec_alginfo_t *, ipsec_algtype_t, 897206083Srdivacky netstack_t *ns); 898204642Srdivackyextern void alg_flag_check(ipsec_alginfo_t *); 899206083Srdivackyextern void ipsec_alg_free(ipsec_alginfo_t *); 900218893Sdimextern void ipsec_register_prov_update(void); 901206083Srdivackyextern void sadb_alg_update(ipsec_algtype_t, uint8_t, boolean_t, netstack_t *); 902206083Srdivacky 903206083Srdivackyextern int sadb_sens_len_from_label(ts_label_t *); 904206083Srdivackyextern void sadb_sens_from_label(sadb_sens_t *, int, ts_label_t *, int); 905206083Srdivacky 906206083Srdivacky/* 907206083Srdivacky * Context templates management. 908206083Srdivacky */ 909206083Srdivacky 910206083Srdivacky#define IPSEC_CTX_TMPL_ALLOC ((crypto_ctx_template_t)-1) 911206083Srdivacky#define IPSEC_CTX_TMPL(_sa, _which, _type, _tmpl) { \ 912206083Srdivacky if ((_tmpl = (_sa)->_which) == IPSEC_CTX_TMPL_ALLOC) { \ 913206083Srdivacky mutex_enter(&assoc->ipsa_lock); \ 914218893Sdim if ((_sa)->_which == IPSEC_CTX_TMPL_ALLOC) { \ 915206083Srdivacky ipsec_stack_t *ipss; \ 916206083Srdivacky \ 917206083Srdivacky ipss = assoc->ipsa_netstack->netstack_ipsec; \ 918206083Srdivacky mutex_enter(&ipss->ipsec_alg_lock); \ 919206083Srdivacky (void) ipsec_create_ctx_tmpl(_sa, _type); \ 920218893Sdim mutex_exit(&ipss->ipsec_alg_lock); \ 921218893Sdim } \ 922204642Srdivacky mutex_exit(&assoc->ipsa_lock); \ 923204642Srdivacky if ((_tmpl = (_sa)->_which) == IPSEC_CTX_TMPL_ALLOC) \ 924204642Srdivacky _tmpl = NULL; \ 925218893Sdim } \ 926204642Srdivacky} 927218893Sdim 928218893Sdimextern int ipsec_create_ctx_tmpl(ipsa_t *, ipsec_algtype_t); 929218893Sdimextern void ipsec_destroy_ctx_tmpl(ipsa_t *, ipsec_algtype_t); 930218893Sdim 931204642Srdivacky/* key checking */ 932204642Srdivackyextern int ipsec_check_key(crypto_mech_type_t, sadb_key_t *, boolean_t, int *); 933204642Srdivacky 934204642Srdivackytypedef struct ipsec_kstats_s { 935204642Srdivacky kstat_named_t esp_stat_in_requests; 936204642Srdivacky kstat_named_t esp_stat_in_discards; 937204642Srdivacky kstat_named_t esp_stat_lookup_failure; 938204642Srdivacky kstat_named_t ah_stat_in_requests; 939204642Srdivacky kstat_named_t ah_stat_in_discards; 940203954Srdivacky kstat_named_t ah_stat_lookup_failure; 941203954Srdivacky kstat_named_t sadb_acquire_maxpackets; 942203954Srdivacky kstat_named_t sadb_acquire_qhiwater; 943204642Srdivacky} ipsec_kstats_t; 944204642Srdivacky 945218893Sdim/* 946204642Srdivacky * (ipss)->ipsec_kstats is equal to (ipss)->ipsec_ksp->ks_data if 947204642Srdivacky * kstat_create_netstack for (ipss)->ipsec_ksp succeeds, but when it 948204642Srdivacky * fails, it will be NULL. Note this is done for all stack instances, 949204642Srdivacky * so it *could* fail. hence a non-NULL checking is done for 950218893Sdim * IP_ESP_BUMP_STAT, IP_AH_BUMP_STAT and IP_ACQUIRE_STAT 951203954Srdivacky */ 952204642Srdivacky#define IP_ESP_BUMP_STAT(ipss, x) \ 953203954Srdivackydo { \ 954203954Srdivacky if ((ipss)->ipsec_kstats != NULL) \ 955204642Srdivacky ((ipss)->ipsec_kstats->esp_stat_ ## x).value.ui64++; \ 956203954Srdivacky_NOTE(CONSTCOND) \ 957} while (0) 958 959#define IP_AH_BUMP_STAT(ipss, x) \ 960do { \ 961 if ((ipss)->ipsec_kstats != NULL) \ 962 ((ipss)->ipsec_kstats->ah_stat_ ## x).value.ui64++; \ 963_NOTE(CONSTCOND) \ 964} while (0) 965 966#define IP_ACQUIRE_STAT(ipss, val, new) \ 967do { \ 968 if ((ipss)->ipsec_kstats != NULL && \ 969 ((uint64_t)(new)) > \ 970 ((ipss)->ipsec_kstats->sadb_acquire_ ## val).value.ui64) \ 971 ((ipss)->ipsec_kstats->sadb_acquire_ ## val).value.ui64 = \ 972 ((uint64_t)(new)); \ 973_NOTE(CONSTCOND) \ 974} while (0) 975 976 977#ifdef __cplusplus 978} 979#endif 980 981#endif /* _INET_SADB_H */ 982