xref: /illumos-gate/usr/src/uts/common/inet/sadb.h (revision bbf21555)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5bffb04cfSmarkfen  * Common Development and Distribution License (the "License").
6bffb04cfSmarkfen  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22930af642SDan McDonald  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
2469e71331SBayard Bell  * Copyright (c) 2012 Nexenta Systems, Inc. All rights reserved.
25f4a6f97eSDan McDonald  * Copyright 2017 Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #ifndef	_INET_SADB_H
297c478bd9Sstevel@tonic-gate #define	_INET_SADB_H
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
327c478bd9Sstevel@tonic-gate extern "C" {
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <inet/ipsec_info.h>
367c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h>
377c478bd9Sstevel@tonic-gate #include <sys/crypto/api.h>
38f4b3ec61Sdh #include <sys/note.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	IPSA_MAX_ADDRLEN 4	/* Max address len. (in 32-bits) for an SA. */
417c478bd9Sstevel@tonic-gate 
42bd670b35SErik Nordmark #define	MAXSALTSIZE 8
43bd670b35SErik Nordmark 
44bd670b35SErik Nordmark /*
45bd670b35SErik Nordmark  * For combined mode ciphers, store the crypto_mechanism_t in the
46bd670b35SErik Nordmark  * per-packet ipsec_in_t/ipsec_out_t structures. This is because the PARAMS
47bd670b35SErik Nordmark  * and nonce values change for each packet. For non-combined mode
48bd670b35SErik Nordmark  * ciphers, these values are constant for the life of the SA.
49bd670b35SErik Nordmark  */
50bd670b35SErik Nordmark typedef struct ipsa_cm_mech_s {
51bd670b35SErik Nordmark 	crypto_mechanism_t combined_mech;
52bd670b35SErik Nordmark 	union {
53bd670b35SErik Nordmark 		CK_AES_CCM_PARAMS paramu_ccm;
54bd670b35SErik Nordmark 		CK_AES_GCM_PARAMS paramu_gcm;
55bd670b35SErik Nordmark 	} paramu;
56bd670b35SErik Nordmark 	uint8_t nonce[MAXSALTSIZE + sizeof (uint64_t)];
57bd670b35SErik Nordmark #define	param_ulMACSize paramu.paramu_ccm.ulMACSize
58bd670b35SErik Nordmark #define	param_ulNonceSize paramu.paramu_ccm.ipsa_ulNonceSize
59bd670b35SErik Nordmark #define	param_ulAuthDataSize paramu.paramu_ccm.ipsa_ulAuthDataSize
60bd670b35SErik Nordmark #define	param_ulDataSize paramu.paramu_ccm.ipsa_ulDataSize
61bd670b35SErik Nordmark #define	param_nonce paramu.paramu_ccm.nonce
62bd670b35SErik Nordmark #define	param_authData paramu.paramu_ccm.authData
63bd670b35SErik Nordmark #define	param_pIv paramu.paramu_gcm.ipsa_pIv
64bd670b35SErik Nordmark #define	param_ulIvLen paramu.paramu_gcm.ulIvLen
65bd670b35SErik Nordmark #define	param_ulIvBits paramu.paramu_gcm.ulIvBits
66bd670b35SErik Nordmark #define	param_pAAD paramu.paramu_gcm.pAAD
67bd670b35SErik Nordmark #define	param_ulAADLen paramu.paramu_gcm.ulAADLen
68bd670b35SErik Nordmark #define	param_ulTagBits paramu.paramu_gcm.ulTagBits
69bd670b35SErik Nordmark } ipsa_cm_mech_t;
707c478bd9Sstevel@tonic-gate 
71628b0c67SMark Fenwick /*
72628b0c67SMark Fenwick  * The Initialization Vector (also known as IV or Nonce) used to
73628b0c67SMark Fenwick  * initialize the Block Cipher, is made up of a Counter and a Salt.
74628b0c67SMark Fenwick  * The Counter is fixed at 64 bits and is incremented for each packet.
75628b0c67SMark Fenwick  * The Salt value can be any whole byte value upto 64 bits. This is
76*bbf21555SRichard Lowe  * algorithm mode specific and can be configured with ipsecalgs(8).
77628b0c67SMark Fenwick  *
78628b0c67SMark Fenwick  * We only support whole byte salt lengths, this is because the salt is
79*bbf21555SRichard Lowe  * stored in an array of uint8_t's. This is enforced by ipsecalgs(8)
80628b0c67SMark Fenwick  * which configures the salt length as a number of bytes. Checks are
81*bbf21555SRichard Lowe  * made to ensure the salt length defined in ipsecalgs(8) fits in
82628b0c67SMark Fenwick  * the ipsec_nonce_t.
83628b0c67SMark Fenwick  *
84628b0c67SMark Fenwick  * The Salt value remains constant for the life of the SA, the Salt is
85628b0c67SMark Fenwick  * know to both peers, but NOT transmitted on the network. The Counter
86628b0c67SMark Fenwick  * portion of the nonce is transmitted over the network with each packet
87628b0c67SMark Fenwick  * and is confusingly described as the Initialization Vector by RFCs
88628b0c67SMark Fenwick  * 4309/4106.
89628b0c67SMark Fenwick  *
90628b0c67SMark Fenwick  * The maximum Initialization Vector length is 128 bits, if the actual
91628b0c67SMark Fenwick  * size is less, its padded internally by the algorithm.
92628b0c67SMark Fenwick  *
93628b0c67SMark Fenwick  * The nonce structure is defined like this in the SA (ipsa_t)to ensure
94628b0c67SMark Fenwick  * the Initilization Vector (counter) is 64 bit aligned, because it will
95628b0c67SMark Fenwick  * be incremented as an uint64_t. The nonce as used by the algorithms is
96628b0c67SMark Fenwick  * a straight uint8_t array.
97628b0c67SMark Fenwick  *
98628b0c67SMark Fenwick  *                     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99628b0c67SMark Fenwick  *                     | | | | |x|x|x|x|               |
100628b0c67SMark Fenwick  *                     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101628b0c67SMark Fenwick  * salt_offset         <------>
102628b0c67SMark Fenwick  * ipsa_saltlen                <------->
103628b0c67SMark Fenwick  * ipsa_nonce_buf------^
104628b0c67SMark Fenwick  * ipsa_salt-------------~~~~~~^
105628b0c67SMark Fenwick  * ipsa_nonce------------~~~~~~^
106628b0c67SMark Fenwick  * ipsa_iv-----------------------------^
107628b0c67SMark Fenwick  */
108628b0c67SMark Fenwick typedef struct ipsec_nonce_s {
109628b0c67SMark Fenwick 	uint8_t		salt[MAXSALTSIZE];
110628b0c67SMark Fenwick 	uint64_t	iv;
111628b0c67SMark Fenwick } ipsec_nonce_t;
112628b0c67SMark Fenwick 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * IP security association.  Synchronization assumes 32-bit loads, so
1157c478bd9Sstevel@tonic-gate  * the 64-bit quantities can't even be be read w/o locking it down!
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /* keying info */
1197c478bd9Sstevel@tonic-gate typedef struct ipsa_key_s {
120628b0c67SMark Fenwick 	uint8_t *sak_key;		/* Algorithm key. */
1217c478bd9Sstevel@tonic-gate 	uint_t sak_keylen;	/* Algorithm key length (in bytes). */
1227c478bd9Sstevel@tonic-gate 	uint_t sak_keybits;	/* Algorithm key length (in bits) */
1237c478bd9Sstevel@tonic-gate 	uint_t sak_algid;	/* Algorithm ID number. */
1247c478bd9Sstevel@tonic-gate } ipsa_key_t;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate typedef struct ipsa_s {
1277c478bd9Sstevel@tonic-gate 	struct ipsa_s *ipsa_next;	/* Next in hash bucket */
1287c478bd9Sstevel@tonic-gate 	struct ipsa_s **ipsa_ptpn;	/* Pointer to previous next pointer. */
1297c478bd9Sstevel@tonic-gate 	kmutex_t *ipsa_linklock;	/* Pointer to hash-chain lock. */
1307c478bd9Sstevel@tonic-gate 	void (*ipsa_freefunc)(struct ipsa_s *); /* freeassoc function */
131628b0c67SMark Fenwick 	void (*ipsa_noncefunc)(struct ipsa_s *, uchar_t *,
132628b0c67SMark Fenwick 	    uint_t, uchar_t *, ipsa_cm_mech_t *, crypto_data_t *);
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * NOTE: I may need more pointers, depending on future SA
1357c478bd9Sstevel@tonic-gate 	 * requirements.
1367c478bd9Sstevel@tonic-gate 	 */
1377c478bd9Sstevel@tonic-gate 	ipsa_key_t ipsa_authkeydata;
1387c478bd9Sstevel@tonic-gate #define	ipsa_authkey ipsa_authkeydata.sak_key
1397c478bd9Sstevel@tonic-gate #define	ipsa_authkeylen ipsa_authkeydata.sak_keylen
1407c478bd9Sstevel@tonic-gate #define	ipsa_authkeybits ipsa_authkeydata.sak_keybits
1417c478bd9Sstevel@tonic-gate #define	ipsa_auth_alg ipsa_authkeydata.sak_algid
1427c478bd9Sstevel@tonic-gate 	ipsa_key_t ipsa_encrkeydata;
1437c478bd9Sstevel@tonic-gate #define	ipsa_encrkey ipsa_encrkeydata.sak_key
1447c478bd9Sstevel@tonic-gate #define	ipsa_encrkeylen ipsa_encrkeydata.sak_keylen
1457c478bd9Sstevel@tonic-gate #define	ipsa_encrkeybits ipsa_encrkeydata.sak_keybits
1467c478bd9Sstevel@tonic-gate #define	ipsa_encr_alg ipsa_encrkeydata.sak_algid
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	struct ipsid_s *ipsa_src_cid;	/* Source certificate identity */
1497c478bd9Sstevel@tonic-gate 	struct ipsid_s *ipsa_dst_cid;	/* Destination certificate identity */
1507c478bd9Sstevel@tonic-gate 	mblk_t	*ipsa_lpkt;	/* Packet received while larval (CAS me) */
1519c2c14abSThejaswini Singarajipura 	mblk_t	*ipsa_bpkt_head;	/* Packets received while idle */
1529c2c14abSThejaswini Singarajipura 	mblk_t	*ipsa_bpkt_tail;
1539c2c14abSThejaswini Singarajipura #define	SADB_MAX_IDLEPKTS	100
1549c2c14abSThejaswini Singarajipura 	uint8_t	ipsa_mblkcnt;	/* Number of packets received while idle */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * PF_KEYv2 supports a replay window size of 255.  Hence there is a
1587c478bd9Sstevel@tonic-gate 	 * need a bit vector to support a replay window of 255.  256 is a nice
1597c478bd9Sstevel@tonic-gate 	 * round number, so I support that.
1607c478bd9Sstevel@tonic-gate 	 *
1617c478bd9Sstevel@tonic-gate 	 * Use an array of uint64_t for best performance on 64-bit
1627c478bd9Sstevel@tonic-gate 	 * processors.  (And hope that 32-bit compilers can handle things
1637c478bd9Sstevel@tonic-gate 	 * okay.)  The " >> 6 " is to get the appropriate number of 64-bit
1647c478bd9Sstevel@tonic-gate 	 * ints.
1657c478bd9Sstevel@tonic-gate 	 */
1667c478bd9Sstevel@tonic-gate #define	SADB_MAX_REPLAY 256	/* Must be 0 mod 64. */
1677c478bd9Sstevel@tonic-gate 	uint64_t ipsa_replay_arr[SADB_MAX_REPLAY >> 6];
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	uint64_t ipsa_unique_id;	/* Non-zero for unique SAs */
1707c478bd9Sstevel@tonic-gate 	uint64_t ipsa_unique_mask;	/* mask value for unique_id */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * Reference count semantics:
1747c478bd9Sstevel@tonic-gate 	 *
1757c478bd9Sstevel@tonic-gate 	 *	An SA has a reference count of 1 if something's pointing
1767c478bd9Sstevel@tonic-gate 	 *	to it.  This includes being in a hash table.  So if an
1777c478bd9Sstevel@tonic-gate 	 *	SA is in a hash table, it has a reference count of at least 1.
1787c478bd9Sstevel@tonic-gate 	 *
1797c478bd9Sstevel@tonic-gate 	 *	When a ptr. to an IPSA is assigned, you MUST REFHOLD after
1807c478bd9Sstevel@tonic-gate 	 *	said assignment.  When a ptr. to an IPSA is released
1817c478bd9Sstevel@tonic-gate 	 *	you MUST REFRELE.  When the refcount hits 0, REFRELE
1827c478bd9Sstevel@tonic-gate 	 *	will free the IPSA.
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	kmutex_t ipsa_lock;	/* Locks non-linkage/refcnt fields. */
1857c478bd9Sstevel@tonic-gate 	/* Q:  Since I may be doing refcnts differently, will I need cv? */
1867c478bd9Sstevel@tonic-gate 	uint_t ipsa_refcnt;	/* Reference count. */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/*
1897c478bd9Sstevel@tonic-gate 	 * The following four time fields are the ones monitored by ah_ager()
1907c478bd9Sstevel@tonic-gate 	 * and esp_ager() respectively.  They are all absolute wall-clock
1917c478bd9Sstevel@tonic-gate 	 * times.  The times of creation (i.e. add time) and first use are
1927c478bd9Sstevel@tonic-gate 	 * pretty straightforward.  The soft and hard expire times are
1937c478bd9Sstevel@tonic-gate 	 * derived from the times of first use and creation, plus the minimum
1947c478bd9Sstevel@tonic-gate 	 * expiration times in the fields that follow this.
1957c478bd9Sstevel@tonic-gate 	 *
1967c478bd9Sstevel@tonic-gate 	 * For example, if I had a hard add time of 30 seconds, and a hard
1977c478bd9Sstevel@tonic-gate 	 * use time of 15, the ipsa_hardexpiretime would be time of add, plus
1987c478bd9Sstevel@tonic-gate 	 * 30 seconds.  If I USE the SA such that time of first use plus 15
1997c478bd9Sstevel@tonic-gate 	 * seconds would be earlier than the add time plus 30 seconds, then
2007c478bd9Sstevel@tonic-gate 	 * ipsa_hardexpiretime would become this earlier time.
2017c478bd9Sstevel@tonic-gate 	 */
2027c478bd9Sstevel@tonic-gate 	time_t ipsa_addtime;	/* Time I was added. */
2037c478bd9Sstevel@tonic-gate 	time_t ipsa_usetime;	/* Time of my first use. */
204437220cdSdanmcd 	time_t ipsa_lastuse;	/* Time of my last use. */
205628b0c67SMark Fenwick 	time_t ipsa_idletime;	/* Seconds of idle time */
206437220cdSdanmcd 	time_t ipsa_last_nat_t_ka;	/* Time of my last NAT-T keepalive. */
2077c478bd9Sstevel@tonic-gate 	time_t ipsa_softexpiretime;	/* Time of my first soft expire. */
2087c478bd9Sstevel@tonic-gate 	time_t ipsa_hardexpiretime;	/* Time of my first hard expire. */
209628b0c67SMark Fenwick 	time_t ipsa_idleexpiretime;	/* Time of my next idle expire time */
210628b0c67SMark Fenwick 
211628b0c67SMark Fenwick 	struct ipsec_nonce_s *ipsa_nonce_buf;
212628b0c67SMark Fenwick 	uint8_t	*ipsa_nonce;
213628b0c67SMark Fenwick 	uint_t ipsa_nonce_len;
214628b0c67SMark Fenwick 	uint8_t	*ipsa_salt;
215628b0c67SMark Fenwick 	uint_t ipsa_saltbits;
216628b0c67SMark Fenwick 	uint_t ipsa_saltlen;
217628b0c67SMark Fenwick 	uint64_t *ipsa_iv;
218628b0c67SMark Fenwick 
219628b0c67SMark Fenwick 	uint64_t ipsa_iv_hardexpire;
220628b0c67SMark Fenwick 	uint64_t ipsa_iv_softexpire;
2217c478bd9Sstevel@tonic-gate 	/*
2227c478bd9Sstevel@tonic-gate 	 * The following fields are directly reflected in PF_KEYv2 LIFETIME
2237c478bd9Sstevel@tonic-gate 	 * extensions.  The time_ts are in number-of-seconds, and the bytes
2247c478bd9Sstevel@tonic-gate 	 * are in... bytes.
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	time_t ipsa_softaddlt;	/* Seconds of soft lifetime after add. */
2277c478bd9Sstevel@tonic-gate 	time_t ipsa_softuselt;	/* Seconds of soft lifetime after first use. */
2287c478bd9Sstevel@tonic-gate 	time_t ipsa_hardaddlt;	/* Seconds of hard lifetime after add. */
2297c478bd9Sstevel@tonic-gate 	time_t ipsa_harduselt;	/* Seconds of hard lifetime after first use. */
2309c2c14abSThejaswini Singarajipura 	time_t ipsa_idleaddlt;	/* Seconds of idle time after add */
2319c2c14abSThejaswini Singarajipura 	time_t ipsa_idleuselt;	/* Seconds of idle time after first use */
2327c478bd9Sstevel@tonic-gate 	uint64_t ipsa_softbyteslt;	/* Bytes of soft lifetime. */
2337c478bd9Sstevel@tonic-gate 	uint64_t ipsa_hardbyteslt;	/* Bytes of hard lifetime. */
2347c478bd9Sstevel@tonic-gate 	uint64_t ipsa_bytes;	/* Bytes encrypted/authed by this SA. */
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2377c478bd9Sstevel@tonic-gate 	 * "Allocations" are a concept mentioned in PF_KEYv2.  We do not
2387c478bd9Sstevel@tonic-gate 	 * support them, except to record them per the PF_KEYv2 spec.
2397c478bd9Sstevel@tonic-gate 	 */
2407c478bd9Sstevel@tonic-gate 	uint_t ipsa_softalloc;	/* Allocations allowed (soft). */
2417c478bd9Sstevel@tonic-gate 	uint_t ipsa_hardalloc;	/* Allocations allowed (hard). */
24238d95a78Smarkfen 	uint_t ipsa_alloc;	/* Allocations made. */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	uint_t ipsa_type;	/* Type of security association. (AH/etc.) */
2457c478bd9Sstevel@tonic-gate 	uint_t ipsa_state;	/* State of my association. */
2467c478bd9Sstevel@tonic-gate 	uint_t ipsa_replay_wsize; /* Size of replay window */
2477c478bd9Sstevel@tonic-gate 	uint32_t ipsa_flags;	/* Flags for security association. */
2487c478bd9Sstevel@tonic-gate 	uint32_t ipsa_spi;	/* Security parameters index. */
2497c478bd9Sstevel@tonic-gate 	uint32_t ipsa_replay;	/* Highest seen replay value for this SA. */
2507c478bd9Sstevel@tonic-gate 	uint32_t ipsa_kmp;	/* key management proto */
251f4a6f97eSDan McDonald 	uint64_t ipsa_kmc;	/* key management cookie (now 64-bit) */
2527c478bd9Sstevel@tonic-gate 
2538810c16bSdanmcd 	boolean_t ipsa_haspeer;		/* Has peer in another table. */
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * Address storage.
2577c478bd9Sstevel@tonic-gate 	 * The source address can be INADDR_ANY, IN6ADDR_ANY, etc.
2587c478bd9Sstevel@tonic-gate 	 *
2597c478bd9Sstevel@tonic-gate 	 * Address families (per sys/socket.h) guide us.  We could have just
2607c478bd9Sstevel@tonic-gate 	 * used sockaddr_storage
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	sa_family_t ipsa_addrfam;
2638810c16bSdanmcd 	sa_family_t ipsa_innerfam;	/* Inner AF can be != src/dst AF. */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	uint32_t ipsa_srcaddr[IPSA_MAX_ADDRLEN];
2667c478bd9Sstevel@tonic-gate 	uint32_t ipsa_dstaddr[IPSA_MAX_ADDRLEN];
2678810c16bSdanmcd 	uint32_t ipsa_innersrc[IPSA_MAX_ADDRLEN];
2688810c16bSdanmcd 	uint32_t ipsa_innerdst[IPSA_MAX_ADDRLEN];
2698810c16bSdanmcd 
2708810c16bSdanmcd 	uint8_t ipsa_innersrcpfx;
2718810c16bSdanmcd 	uint8_t ipsa_innerdstpfx;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	uint16_t ipsa_inbound_cksum; /* cksum correction for inbound packets */
274437220cdSdanmcd 	uint16_t ipsa_local_nat_port;	/* Local NAT-T port.  (0 --> 4500) */
275437220cdSdanmcd 	uint16_t ipsa_remote_nat_port; /* The other port that isn't 4500 */
276437220cdSdanmcd 
277437220cdSdanmcd 	/* these can only be v4 */
278437220cdSdanmcd 	uint32_t ipsa_natt_addr_loc;
279437220cdSdanmcd 	uint32_t ipsa_natt_addr_rem;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * icmp type and code. *_end are to specify ranges. if only
2837c478bd9Sstevel@tonic-gate 	 * a single value, * and *_end are the same value.
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	uint8_t ipsa_icmp_type;
2867c478bd9Sstevel@tonic-gate 	uint8_t ipsa_icmp_type_end;
2877c478bd9Sstevel@tonic-gate 	uint8_t ipsa_icmp_code;
2887c478bd9Sstevel@tonic-gate 	uint8_t ipsa_icmp_code_end;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * For the kernel crypto framework.
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	crypto_key_t ipsa_kcfauthkey;		/* authentication key */
2947c478bd9Sstevel@tonic-gate 	crypto_key_t ipsa_kcfencrkey;		/* encryption key */
2957c478bd9Sstevel@tonic-gate 	crypto_ctx_template_t ipsa_authtmpl;	/* auth context template */
2967c478bd9Sstevel@tonic-gate 	crypto_ctx_template_t ipsa_encrtmpl;	/* encr context template */
2977c478bd9Sstevel@tonic-gate 	crypto_mechanism_t ipsa_amech;		/* auth mech type and ICV len */
2987c478bd9Sstevel@tonic-gate 	crypto_mechanism_t ipsa_emech;		/* encr mech type */
299628b0c67SMark Fenwick 	size_t ipsa_mac_len;			/* auth MAC/ICV length */
3007c478bd9Sstevel@tonic-gate 	size_t ipsa_iv_len;			/* encr IV length */
301628b0c67SMark Fenwick 	size_t ipsa_datalen;			/* block length in bytes. */
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * Input and output processing functions called from IP.
305bd670b35SErik Nordmark 	 * The mblk_t is the data; the IPsec information is in the attributes
306bd670b35SErik Nordmark 	 * Returns NULL if the mblk is consumed which it is if there was
307bd670b35SErik Nordmark 	 * a failure or if pending. If failure then
308bd670b35SErik Nordmark 	 * the ipIfInDiscards/OutDiscards counters are increased.
3097c478bd9Sstevel@tonic-gate 	 */
310bd670b35SErik Nordmark 	mblk_t *(*ipsa_output_func)(mblk_t *, ip_xmit_attr_t *);
311bd670b35SErik Nordmark 	mblk_t *(*ipsa_input_func)(mblk_t *, void *, ip_recv_attr_t *);
3127c478bd9Sstevel@tonic-gate 
31338d95a78Smarkfen 	/*
31438d95a78Smarkfen 	 * Soft reference to paired SA
31538d95a78Smarkfen 	 */
31638d95a78Smarkfen 	uint32_t	ipsa_otherspi;
317f4b3ec61Sdh 	netstack_t	*ipsa_netstack;	/* Does not have a netstack_hold */
3185d3b8cb7SBill Sommerfeld 
319bd670b35SErik Nordmark 	ts_label_t *ipsa_tsl;			/* MLS: label attributes */
320bd670b35SErik Nordmark 	ts_label_t *ipsa_otsl;			/* MLS: outer label */
3215d3b8cb7SBill Sommerfeld 	uint8_t	ipsa_mac_exempt;		/* MLS: mac exempt flag */
3225d3b8cb7SBill Sommerfeld 	uchar_t	ipsa_opt_storage[IP_MAX_OPT_LENGTH];
3237c478bd9Sstevel@tonic-gate } ipsa_t;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * ipsa_t address handling macros.  We want these to be inlined, and deal
3277c478bd9Sstevel@tonic-gate  * with 32-bit words to avoid bcmp/bcopy calls.
3287c478bd9Sstevel@tonic-gate  *
3297c478bd9Sstevel@tonic-gate  * Assume we only have AF_INET and AF_INET6 addresses for now.  Also assume
3307c478bd9Sstevel@tonic-gate  * that we have 32-bit alignment on everything.
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate #define	IPSA_IS_ADDR_UNSPEC(addr, fam) ((((uint32_t *)(addr))[0] == 0) && \
3337c478bd9Sstevel@tonic-gate 	(((fam) == AF_INET) || (((uint32_t *)(addr))[3] == 0 && \
3347c478bd9Sstevel@tonic-gate 	((uint32_t *)(addr))[2] == 0 && ((uint32_t *)(addr))[1] == 0)))
3357c478bd9Sstevel@tonic-gate #define	IPSA_ARE_ADDR_EQUAL(addr1, addr2, fam) \
3367c478bd9Sstevel@tonic-gate 	((((uint32_t *)(addr1))[0] == ((uint32_t *)(addr2))[0]) && \
3377c478bd9Sstevel@tonic-gate 	(((fam) == AF_INET) || \
3387c478bd9Sstevel@tonic-gate 	(((uint32_t *)(addr1))[3] == ((uint32_t *)(addr2))[3] && \
3397c478bd9Sstevel@tonic-gate 	((uint32_t *)(addr1))[2] == ((uint32_t *)(addr2))[2] && \
3407c478bd9Sstevel@tonic-gate 	((uint32_t *)(addr1))[1] == ((uint32_t *)(addr2))[1])))
3417c478bd9Sstevel@tonic-gate #define	IPSA_COPY_ADDR(dstaddr, srcaddr, fam) { \
3427c478bd9Sstevel@tonic-gate 	((uint32_t *)(dstaddr))[0] = ((uint32_t *)(srcaddr))[0]; \
3437c478bd9Sstevel@tonic-gate 	if ((fam) == AF_INET6) {\
3447c478bd9Sstevel@tonic-gate 		((uint32_t *)(dstaddr))[1] = ((uint32_t *)(srcaddr))[1]; \
3457c478bd9Sstevel@tonic-gate 		((uint32_t *)(dstaddr))[2] = ((uint32_t *)(srcaddr))[2]; \
3467c478bd9Sstevel@tonic-gate 		((uint32_t *)(dstaddr))[3] = ((uint32_t *)(srcaddr))[3]; } }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate  * ipsa_t reference hold/release macros.
3507c478bd9Sstevel@tonic-gate  *
3517c478bd9Sstevel@tonic-gate  * If you have a pointer, you REFHOLD.  If you are releasing a pointer, you
3527c478bd9Sstevel@tonic-gate  * REFRELE.  An ipsa_t that is newly inserted into the table should have
3537c478bd9Sstevel@tonic-gate  * a reference count of 1 (for the table's pointer), plus 1 more for every
3547c478bd9Sstevel@tonic-gate  * pointer that is referencing the ipsa_t.
3557c478bd9Sstevel@tonic-gate  */
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate #define	IPSA_REFHOLD(ipsa) {			\
3581a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&(ipsa)->ipsa_refcnt);	\
3597c478bd9Sstevel@tonic-gate 	ASSERT((ipsa)->ipsa_refcnt != 0);	\
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * Decrement the reference count on the SA.
3647c478bd9Sstevel@tonic-gate  * In architectures e.g sun4u, where atomic_add_32_nv is just
3657c478bd9Sstevel@tonic-gate  * a cas, we need to maintain the right memory barrier semantics
3667c478bd9Sstevel@tonic-gate  * as that of mutex_exit i.e all the loads and stores should complete
3677c478bd9Sstevel@tonic-gate  * before the cas is executed. membar_exit() does that here.
3687c478bd9Sstevel@tonic-gate  */
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate #define	IPSA_REFRELE(ipsa) {					\
3717c478bd9Sstevel@tonic-gate 	ASSERT((ipsa)->ipsa_refcnt != 0);			\
3727c478bd9Sstevel@tonic-gate 	membar_exit();						\
3731a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&(ipsa)->ipsa_refcnt) == 0)	\
3747c478bd9Sstevel@tonic-gate 		((ipsa)->ipsa_freefunc)(ipsa);			\
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * Security association hash macros and definitions.  For now, assume the
3797c478bd9Sstevel@tonic-gate  * IPsec model, and hash outbounds on destination address, and inbounds on
3807c478bd9Sstevel@tonic-gate  * SPI.
3817c478bd9Sstevel@tonic-gate  */
382fb87b5d2Ssommerfe 
383fb87b5d2Ssommerfe #define	IPSEC_DEFAULT_HASH_SIZE 256
384fb87b5d2Ssommerfe 
385fb87b5d2Ssommerfe #define	INBOUND_HASH(sadb, spi) ((spi) % ((sadb)->sdb_hashsize))
386fb87b5d2Ssommerfe #define	OUTBOUND_HASH_V4(sadb, v4addr) ((v4addr) % ((sadb)->sdb_hashsize))
387fb87b5d2Ssommerfe #define	OUTBOUND_HASH_V6(sadb, v6addr) OUTBOUND_HASH_V4((sadb), \
3883c04777eSsommerfe 	(*(uint32_t *)&(v6addr)) ^ (*(((uint32_t *)&(v6addr)) + 1)) ^ \
3893c04777eSsommerfe 	(*(((uint32_t *)&(v6addr)) + 2)) ^ (*(((uint32_t *)&(v6addr)) + 3)))
390fb87b5d2Ssommerfe 
391fb87b5d2Ssommerfe /*
392fb87b5d2Ssommerfe  * Syntactic sugar to find the appropriate hash bucket directly.
393fb87b5d2Ssommerfe  */
394fb87b5d2Ssommerfe 
395fb87b5d2Ssommerfe #define	INBOUND_BUCKET(sadb, spi) &(((sadb)->sdb_if)[INBOUND_HASH(sadb, spi)])
396fb87b5d2Ssommerfe #define	OUTBOUND_BUCKET_V4(sadb, v4addr) \
397fb87b5d2Ssommerfe 	&(((sadb)->sdb_of)[OUTBOUND_HASH_V4(sadb, v4addr)])
398fb87b5d2Ssommerfe #define	OUTBOUND_BUCKET_V6(sadb, v6addr) \
399fb87b5d2Ssommerfe 	&(((sadb)->sdb_of)[OUTBOUND_HASH_V6(sadb, v6addr)])
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate #define	IPSA_F_PFS	SADB_SAFLAGS_PFS	/* PFS in use for this SA? */
4027c478bd9Sstevel@tonic-gate #define	IPSA_F_NOREPFLD	SADB_SAFLAGS_NOREPLAY	/* No replay field, for */
4037c478bd9Sstevel@tonic-gate 						/* backward compat. */
4047c478bd9Sstevel@tonic-gate #define	IPSA_F_USED	SADB_X_SAFLAGS_USED	/* SA has been used. */
4057c478bd9Sstevel@tonic-gate #define	IPSA_F_UNIQUE	SADB_X_SAFLAGS_UNIQUE	/* SA is unique */
4067c478bd9Sstevel@tonic-gate #define	IPSA_F_AALG1	SADB_X_SAFLAGS_AALG1	/* Auth alg flag 1 */
4077c478bd9Sstevel@tonic-gate #define	IPSA_F_AALG2	SADB_X_SAFLAGS_AALG2	/* Auth alg flag 2 */
4087c478bd9Sstevel@tonic-gate #define	IPSA_F_EALG1	SADB_X_SAFLAGS_EALG1	/* Encrypt alg flag 1 */
4097c478bd9Sstevel@tonic-gate #define	IPSA_F_EALG2	SADB_X_SAFLAGS_EALG2	/* Encrypt alg flag 2 */
4107c478bd9Sstevel@tonic-gate 
411bd670b35SErik Nordmark #define	IPSA_F_ASYNC	0x200000		/* Call KCF asynchronously? */
4127c478bd9Sstevel@tonic-gate #define	IPSA_F_NATT_LOC	SADB_X_SAFLAGS_NATT_LOC
4137c478bd9Sstevel@tonic-gate #define	IPSA_F_NATT_REM	SADB_X_SAFLAGS_NATT_REM
4144a179720Sdanmcd #define	IPSA_F_BEHIND_NAT SADB_X_SAFLAGS_NATTED
41572bd9b6bSdanmcd #define	IPSA_F_NATT	(SADB_X_SAFLAGS_NATT_LOC | SADB_X_SAFLAGS_NATT_REM | \
41672bd9b6bSdanmcd 	SADB_X_SAFLAGS_NATTED)
4177c478bd9Sstevel@tonic-gate #define	IPSA_F_CINVALID	0x40000		/* SA shouldn't be cached */
41838d95a78Smarkfen #define	IPSA_F_PAIRED	SADB_X_SAFLAGS_PAIRED	/* SA is one of a pair */
41938d95a78Smarkfen #define	IPSA_F_OUTBOUND	SADB_X_SAFLAGS_OUTBOUND	/* SA direction bit */
42038d95a78Smarkfen #define	IPSA_F_INBOUND	SADB_X_SAFLAGS_INBOUND	/* SA direction bit */
4218810c16bSdanmcd #define	IPSA_F_TUNNEL	SADB_X_SAFLAGS_TUNNEL
422628b0c67SMark Fenwick /*
423628b0c67SMark Fenwick  * These flags are only defined here to prevent a flag value collision.
424628b0c67SMark Fenwick  */
425628b0c67SMark Fenwick #define	IPSA_F_COMBINED	SADB_X_SAFLAGS_EALG1	/* Defined in pfkeyv2.h */
426628b0c67SMark Fenwick #define	IPSA_F_COUNTERMODE SADB_X_SAFLAGS_EALG2	/* Defined in pfkeyv2.h */
4277c478bd9Sstevel@tonic-gate 
42872bd9b6bSdanmcd /*
42972bd9b6bSdanmcd  * Sets of flags that are allowed to by set or modified by PF_KEY apps.
43072bd9b6bSdanmcd  */
43172bd9b6bSdanmcd #define	AH_UPDATE_SETTABLE_FLAGS \
43272bd9b6bSdanmcd 	(SADB_X_SAFLAGS_PAIRED | SADB_SAFLAGS_NOREPLAY | \
43372bd9b6bSdanmcd 	SADB_X_SAFLAGS_OUTBOUND | SADB_X_SAFLAGS_INBOUND | \
43472bd9b6bSdanmcd 	SADB_X_SAFLAGS_KM1 | SADB_X_SAFLAGS_KM2 | \
43572bd9b6bSdanmcd 	SADB_X_SAFLAGS_KM3 | SADB_X_SAFLAGS_KM4)
43672bd9b6bSdanmcd 
43772bd9b6bSdanmcd /* AH can't set NAT flags (or even use NAT).  Add NAT flags to the ESP set. */
43872bd9b6bSdanmcd #define	ESP_UPDATE_SETTABLE_FLAGS (AH_UPDATE_SETTABLE_FLAGS | IPSA_F_NATT)
43972bd9b6bSdanmcd 
44072bd9b6bSdanmcd #define	AH_ADD_SETTABLE_FLAGS \
44172bd9b6bSdanmcd 	(AH_UPDATE_SETTABLE_FLAGS | SADB_X_SAFLAGS_AALG1 | \
44272bd9b6bSdanmcd 	SADB_X_SAFLAGS_AALG2 | SADB_X_SAFLAGS_TUNNEL | \
44372bd9b6bSdanmcd 	SADB_SAFLAGS_NOREPLAY)
44472bd9b6bSdanmcd 
44572bd9b6bSdanmcd /* AH can't set NAT flags (or even use NAT).  Add NAT flags to the ESP set. */
44672bd9b6bSdanmcd #define	ESP_ADD_SETTABLE_FLAGS (AH_ADD_SETTABLE_FLAGS | IPSA_F_NATT | \
44772bd9b6bSdanmcd 	SADB_X_SAFLAGS_EALG1 | SADB_X_SAFLAGS_EALG2)
44872bd9b6bSdanmcd 
44972bd9b6bSdanmcd 
45072bd9b6bSdanmcd 
4517c478bd9Sstevel@tonic-gate /* SA states are important for handling UPDATE PF_KEY messages. */
4529c2c14abSThejaswini Singarajipura #define	IPSA_STATE_LARVAL		SADB_SASTATE_LARVAL
4539c2c14abSThejaswini Singarajipura #define	IPSA_STATE_MATURE		SADB_SASTATE_MATURE
4549c2c14abSThejaswini Singarajipura #define	IPSA_STATE_DYING		SADB_SASTATE_DYING
4559c2c14abSThejaswini Singarajipura #define	IPSA_STATE_DEAD			SADB_SASTATE_DEAD
4569c2c14abSThejaswini Singarajipura #define	IPSA_STATE_IDLE			SADB_X_SASTATE_IDLE
4579c2c14abSThejaswini Singarajipura #define	IPSA_STATE_ACTIVE_ELSEWHERE	SADB_X_SASTATE_ACTIVE_ELSEWHERE
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate /*
4607c478bd9Sstevel@tonic-gate  * NOTE:  If the document authors do things right in defining algorithms, we'll
4617c478bd9Sstevel@tonic-gate  *	  probably have flags for what all is here w.r.t. replay, ESP w/HMAC,
4627c478bd9Sstevel@tonic-gate  *	  etc.
4637c478bd9Sstevel@tonic-gate  */
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate #define	IPSA_T_ACQUIRE	SEC_TYPE_NONE	/* If this typed returned, sa needed */
4667c478bd9Sstevel@tonic-gate #define	IPSA_T_AH	SEC_TYPE_AH	/* IPsec AH association */
4677c478bd9Sstevel@tonic-gate #define	IPSA_T_ESP	SEC_TYPE_ESP	/* IPsec ESP association */
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate #define	IPSA_AALG_NONE	SADB_AALG_NONE		/* No auth. algorithm */
4707c478bd9Sstevel@tonic-gate #define	IPSA_AALG_MD5H	SADB_AALG_MD5HMAC	/* MD5-HMAC algorithm */
4717c478bd9Sstevel@tonic-gate #define	IPSA_AALG_SHA1H	SADB_AALG_SHA1HMAC	/* SHA1-HMAC algorithm */
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate #define	IPSA_EALG_NONE		SADB_EALG_NONE	/* No encryption algorithm */
4747c478bd9Sstevel@tonic-gate #define	IPSA_EALG_DES_CBC	SADB_EALG_DESCBC
4757c478bd9Sstevel@tonic-gate #define	IPSA_EALG_3DES		SADB_EALG_3DESCBC
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate  * Protect each ipsa_t bucket (and linkage) with a lock.
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate typedef struct isaf_s {
4827c478bd9Sstevel@tonic-gate 	ipsa_t *isaf_ipsa;
4837c478bd9Sstevel@tonic-gate 	kmutex_t isaf_lock;
4847c478bd9Sstevel@tonic-gate 	uint64_t isaf_gen;
4857c478bd9Sstevel@tonic-gate } isaf_t;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * ACQUIRE record.  If AH/ESP/whatever cannot find an association for outbound
4897c478bd9Sstevel@tonic-gate  * traffic, it sends up an SADB_ACQUIRE message and create an ACQUIRE record.
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate #define	IPSACQ_MAXPACKETS 4	/* Number of packets that can be queued up */
4937c478bd9Sstevel@tonic-gate 				/* waiting for an ACQUIRE to finish. */
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate typedef struct ipsacq_s {
4967c478bd9Sstevel@tonic-gate 	struct ipsacq_s *ipsacq_next;
4977c478bd9Sstevel@tonic-gate 	struct ipsacq_s **ipsacq_ptpn;
4987c478bd9Sstevel@tonic-gate 	kmutex_t *ipsacq_linklock;
4997c478bd9Sstevel@tonic-gate 	struct ipsec_policy_s  *ipsacq_policy;
5007c478bd9Sstevel@tonic-gate 	struct ipsec_action_s  *ipsacq_act;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	sa_family_t ipsacq_addrfam;	/* Address family. */
5038810c16bSdanmcd 	sa_family_t ipsacq_inneraddrfam; /* Inner-packet address family. */
5047c478bd9Sstevel@tonic-gate 	int ipsacq_numpackets;		/* How many packets queued up so far. */
5057c478bd9Sstevel@tonic-gate 	uint32_t ipsacq_seq;		/* PF_KEY sequence number. */
5067c478bd9Sstevel@tonic-gate 	uint64_t ipsacq_unique_id;	/* Unique ID for SAs that need it. */
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	kmutex_t ipsacq_lock;	/* Protects non-linkage fields. */
5097c478bd9Sstevel@tonic-gate 	time_t ipsacq_expire;	/* Wall-clock time when this record expires. */
5107c478bd9Sstevel@tonic-gate 	mblk_t *ipsacq_mp;	/* List of datagrams waiting for an SA. */
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/* These two point inside the last mblk inserted. */
5137c478bd9Sstevel@tonic-gate 	uint32_t *ipsacq_srcaddr;
5147c478bd9Sstevel@tonic-gate 	uint32_t *ipsacq_dstaddr;
5157c478bd9Sstevel@tonic-gate 
5168810c16bSdanmcd 	/* Cache these instead of point so we can mask off accordingly */
5178810c16bSdanmcd 	uint32_t ipsacq_innersrc[IPSA_MAX_ADDRLEN];
5188810c16bSdanmcd 	uint32_t ipsacq_innerdst[IPSA_MAX_ADDRLEN];
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/* These may change per-acquire. */
5217c478bd9Sstevel@tonic-gate 	uint16_t ipsacq_srcport;
5227c478bd9Sstevel@tonic-gate 	uint16_t ipsacq_dstport;
5237c478bd9Sstevel@tonic-gate 	uint8_t ipsacq_proto;
5248810c16bSdanmcd 	uint8_t ipsacq_inner_proto;
5258810c16bSdanmcd 	uint8_t ipsacq_innersrcpfx;
5268810c16bSdanmcd 	uint8_t ipsacq_innerdstpfx;
5278810c16bSdanmcd 
5287c478bd9Sstevel@tonic-gate 	/* icmp type and code of triggering packet (if applicable) */
5297c478bd9Sstevel@tonic-gate 	uint8_t	ipsacq_icmp_type;
5307c478bd9Sstevel@tonic-gate 	uint8_t ipsacq_icmp_code;
5315d3b8cb7SBill Sommerfeld 
532bd670b35SErik Nordmark 	/* label associated with triggering packet */
533bd670b35SErik Nordmark 	ts_label_t	*ipsacq_tsl;
5347c478bd9Sstevel@tonic-gate } ipsacq_t;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate /*
5377c478bd9Sstevel@tonic-gate  * Kernel-generated sequence numbers will be no less than 0x80000000 to
5387c478bd9Sstevel@tonic-gate  * forestall any cretinous problems with manual keying accidentally updating
5397c478bd9Sstevel@tonic-gate  * an ACQUIRE entry.
5407c478bd9Sstevel@tonic-gate  */
5417c478bd9Sstevel@tonic-gate #define	IACQF_LOWEST_SEQ 0x80000000
5427c478bd9Sstevel@tonic-gate 
5430e9b5742SDan McDonald #define	SADB_AGE_INTERVAL_DEFAULT 8000
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate  * ACQUIRE fanout.  Protect each linkage with a lock.
5477c478bd9Sstevel@tonic-gate  */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate typedef struct iacqf_s {
5507c478bd9Sstevel@tonic-gate 	ipsacq_t *iacqf_ipsacq;
5517c478bd9Sstevel@tonic-gate 	kmutex_t iacqf_lock;
5527c478bd9Sstevel@tonic-gate } iacqf_t;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate  * A (network protocol, ipsec protocol) specific SADB.
5567c478bd9Sstevel@tonic-gate  * (i.e., one each for {ah, esp} and {v4, v6}.
5577c478bd9Sstevel@tonic-gate  *
558bd670b35SErik Nordmark  * Keep outbound assocs in a simple hash table for now.
5597c478bd9Sstevel@tonic-gate  * One danger point, multiple SAs for a single dest will clog a bucket.
5607c478bd9Sstevel@tonic-gate  * For the future, consider two-level hashing (2nd hash on IPC?), then probe.
5617c478bd9Sstevel@tonic-gate  */
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate typedef struct sadb_s
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	isaf_t	*sdb_of;
5667c478bd9Sstevel@tonic-gate 	isaf_t	*sdb_if;
5677c478bd9Sstevel@tonic-gate 	iacqf_t	*sdb_acq;
568fb87b5d2Ssommerfe 	int	sdb_hashsize;
5697c478bd9Sstevel@tonic-gate } sadb_t;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /*
572b7daf799SDan McDonald  * A pair of SADB's (one for v4, one for v6), and related state.
5737c478bd9Sstevel@tonic-gate  */
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate typedef struct sadbp_s
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	uint32_t	s_satype;
5787c478bd9Sstevel@tonic-gate 	uint32_t	*s_acquire_timeout;
5797c478bd9Sstevel@tonic-gate 	sadb_t		s_v4;
5807c478bd9Sstevel@tonic-gate 	sadb_t		s_v6;
58172bd9b6bSdanmcd 	uint32_t	s_addflags;
58272bd9b6bSdanmcd 	uint32_t	s_updateflags;
5837c478bd9Sstevel@tonic-gate } sadbp_t;
5847c478bd9Sstevel@tonic-gate 
58538d95a78Smarkfen /*
58638d95a78Smarkfen  * A pair of SA's for a single connection, the structure contains a
58738d95a78Smarkfen  * pointer to a SA and the SA its paired with (opposite direction) as well
58838d95a78Smarkfen  * as the SA's respective hash buckets.
58938d95a78Smarkfen  */
59038d95a78Smarkfen typedef struct ipsap_s
59138d95a78Smarkfen {
592a1ba8781SMark Fenwick 	boolean_t	in_inbound_table;
59338d95a78Smarkfen 	isaf_t		*ipsap_bucket;
59438d95a78Smarkfen 	ipsa_t		*ipsap_sa_ptr;
59538d95a78Smarkfen 	isaf_t		*ipsap_pbucket;
59638d95a78Smarkfen 	ipsa_t		*ipsap_psa_ptr;
59738d95a78Smarkfen } ipsap_t;
59838d95a78Smarkfen 
59938d95a78Smarkfen typedef struct templist_s
60038d95a78Smarkfen {
60138d95a78Smarkfen 	ipsa_t		*ipsa;
60238d95a78Smarkfen 	struct templist_s	*next;
60338d95a78Smarkfen } templist_t;
60438d95a78Smarkfen 
6057c478bd9Sstevel@tonic-gate /* Pointer to an all-zeroes IPv6 address. */
6067c478bd9Sstevel@tonic-gate #define	ALL_ZEROES_PTR	((uint32_t *)&ipv6_all_zeros)
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate /*
609bd670b35SErik Nordmark  * Form unique id from ip_xmit_attr_t.
6107c478bd9Sstevel@tonic-gate  */
611bd670b35SErik Nordmark #define	SA_FORM_UNIQUE_ID(ixa)					\
612bd670b35SErik Nordmark 	SA_UNIQUE_ID((ixa)->ixa_ipsec_src_port, (ixa)->ixa_ipsec_dst_port, \
613bd670b35SErik Nordmark 	    (((ixa)->ixa_flags & IXAF_IPSEC_TUNNEL) ?			\
614bd670b35SErik Nordmark 	    ((ixa)->ixa_ipsec_inaf == AF_INET6 ? \
615bd670b35SErik Nordmark 	    IPPROTO_IPV6 : IPPROTO_ENCAP) :				\
616bd670b35SErik Nordmark 	    (ixa)->ixa_ipsec_proto),					\
617bd670b35SErik Nordmark 	    (((ixa)->ixa_flags & IXAF_IPSEC_TUNNEL) ? \
618bd670b35SErik Nordmark 	    (ixa)->ixa_ipsec_proto : 0))
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate /*
6218810c16bSdanmcd  * This macro is used to generate unique ids (along with the addresses, both
6228810c16bSdanmcd  * inner and outer) for outbound datagrams that require unique SAs.
6237c478bd9Sstevel@tonic-gate  *
6247c478bd9Sstevel@tonic-gate  * N.B. casts and unsigned shift amounts discourage unwarranted
6258810c16bSdanmcd  * sign extension of dstport, proto, and iproto.
6268810c16bSdanmcd  *
6278810c16bSdanmcd  * Unique ID is 64-bits allocated as follows (pardon my big-endian bias):
6288810c16bSdanmcd  *
6298810c16bSdanmcd  *   6               4      43      33              11
6308810c16bSdanmcd  *   3               7      09      21              65              0
6318810c16bSdanmcd  *   +---------------*-------+-------+--------------+---------------+
6328810c16bSdanmcd  *   |  MUST-BE-ZERO |<iprot>|<proto>| <src port>   |  <dest port>  |
6338810c16bSdanmcd  *   +---------------*-------+-------+--------------+---------------+
6348810c16bSdanmcd  *
6358810c16bSdanmcd  * If there are inner addresses (tunnel mode) the ports come from the
6368810c16bSdanmcd  * inner addresses.  If there are no inner addresses, the ports come from
6378810c16bSdanmcd  * the outer addresses (transport mode).  Tunnel mode MUST have <proto>
6388810c16bSdanmcd  * set to either IPPROTO_ENCAP or IPPPROTO_IPV6.
6397c478bd9Sstevel@tonic-gate  */
6408810c16bSdanmcd #define	SA_UNIQUE_ID(srcport, dstport, proto, iproto) 	\
6418810c16bSdanmcd 	((srcport) | ((uint64_t)(dstport) << 16U) | \
6428810c16bSdanmcd 	((uint64_t)(proto) << 32U) | ((uint64_t)(iproto) << 40U))
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate /*
6457c478bd9Sstevel@tonic-gate  * SA_UNIQUE_MASK generates a mask value to use when comparing the unique value
6467c478bd9Sstevel@tonic-gate  * from a packet to an SA.
6477c478bd9Sstevel@tonic-gate  */
6487c478bd9Sstevel@tonic-gate 
6498810c16bSdanmcd #define	SA_UNIQUE_MASK(srcport, dstport, proto, iproto) 	\
6508810c16bSdanmcd 	SA_UNIQUE_ID((srcport != 0) ? 0xffff : 0,		\
6518810c16bSdanmcd 		    (dstport != 0) ? 0xffff : 0,		\
6528810c16bSdanmcd 		    (proto != 0) ? 0xff : 0,			\
6538810c16bSdanmcd 		    (iproto != 0) ? 0xff : 0)
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate /*
6567c478bd9Sstevel@tonic-gate  * Decompose unique id back into its original fields.
6577c478bd9Sstevel@tonic-gate  */
6588810c16bSdanmcd #define	SA_IPROTO(ipsa) ((ipsa)->ipsa_unique_id>>40)&0xff
6597c478bd9Sstevel@tonic-gate #define	SA_PROTO(ipsa) ((ipsa)->ipsa_unique_id>>32)&0xff
6607c478bd9Sstevel@tonic-gate #define	SA_SRCPORT(ipsa) ((ipsa)->ipsa_unique_id & 0xffff)
6617c478bd9Sstevel@tonic-gate #define	SA_DSTPORT(ipsa) (((ipsa)->ipsa_unique_id >> 16) & 0xffff)
6627c478bd9Sstevel@tonic-gate 
6635d3b8cb7SBill Sommerfeld typedef struct ipsa_query_s ipsa_query_t;
6645d3b8cb7SBill Sommerfeld 
6655d3b8cb7SBill Sommerfeld typedef boolean_t (*ipsa_match_fn_t)(ipsa_query_t *, ipsa_t *);
6665d3b8cb7SBill Sommerfeld 
6675d3b8cb7SBill Sommerfeld #define	IPSA_NMATCH	10
6685d3b8cb7SBill Sommerfeld 
6695d3b8cb7SBill Sommerfeld /*
6705d3b8cb7SBill Sommerfeld  * SADB query structure.
6715d3b8cb7SBill Sommerfeld  *
6725d3b8cb7SBill Sommerfeld  * Provide a generalized mechanism for matching entries in the SADB;
6735d3b8cb7SBill Sommerfeld  * one of these structures is initialized using sadb_form_query(),
6745d3b8cb7SBill Sommerfeld  * and then can be used as a parameter to sadb_match_query() which returns
6755d3b8cb7SBill Sommerfeld  * B_TRUE if the SA matches the query.
6765d3b8cb7SBill Sommerfeld  *
6775d3b8cb7SBill Sommerfeld  * Under the covers, sadb_form_query populates the matchers[] array with
6785d3b8cb7SBill Sommerfeld  * functions which are called one at a time until one fails to match.
6795d3b8cb7SBill Sommerfeld  */
6805d3b8cb7SBill Sommerfeld struct ipsa_query_s {
6815d3b8cb7SBill Sommerfeld 	uint32_t req, match;
6825d3b8cb7SBill Sommerfeld 	sadb_address_t *srcext, *dstext;
6835d3b8cb7SBill Sommerfeld 	sadb_ident_t *srcid, *dstid;
6845d3b8cb7SBill Sommerfeld 	sadb_x_kmc_t *kmcext;
6855d3b8cb7SBill Sommerfeld 	sadb_sa_t *assoc;
6865d3b8cb7SBill Sommerfeld 	uint32_t spi;
6875d3b8cb7SBill Sommerfeld 	struct sockaddr_in *src;
6885d3b8cb7SBill Sommerfeld 	struct sockaddr_in6 *src6;
6895d3b8cb7SBill Sommerfeld 	struct sockaddr_in *dst;
6905d3b8cb7SBill Sommerfeld 	struct sockaddr_in6 *dst6;
6915d3b8cb7SBill Sommerfeld 	sa_family_t af;
6925d3b8cb7SBill Sommerfeld 	uint32_t *srcaddr, *dstaddr;
6935d3b8cb7SBill Sommerfeld 	uint32_t ifindex;
694f4a6f97eSDan McDonald 	uint32_t kmp;
695f4a6f97eSDan McDonald 	uint64_t kmc;
6965d3b8cb7SBill Sommerfeld 	char *didstr, *sidstr;
6975d3b8cb7SBill Sommerfeld 	uint16_t didtype, sidtype;
6985d3b8cb7SBill Sommerfeld 	sadbp_t *spp;
6995d3b8cb7SBill Sommerfeld 	sadb_t *sp;
7005d3b8cb7SBill Sommerfeld 	isaf_t	*inbound, *outbound;
7015d3b8cb7SBill Sommerfeld 	uint32_t outhash;
7025d3b8cb7SBill Sommerfeld 	uint32_t inhash;
7035d3b8cb7SBill Sommerfeld 	ipsa_match_fn_t matchers[IPSA_NMATCH];
7045d3b8cb7SBill Sommerfeld };
7055d3b8cb7SBill Sommerfeld 
7065d3b8cb7SBill Sommerfeld #define	IPSA_Q_SA		0x00000001
7075d3b8cb7SBill Sommerfeld #define	IPSA_Q_DST		0x00000002
7085d3b8cb7SBill Sommerfeld #define	IPSA_Q_SRC		0x00000004
7095d3b8cb7SBill Sommerfeld #define	IPSA_Q_DSTID		0x00000008
7105d3b8cb7SBill Sommerfeld #define	IPSA_Q_SRCID		0x00000010
7115d3b8cb7SBill Sommerfeld #define	IPSA_Q_KMC		0x00000020
7125d3b8cb7SBill Sommerfeld #define	IPSA_Q_INBOUND		0x00000040 /* fill in inbound isaf_t */
7135d3b8cb7SBill Sommerfeld #define	IPSA_Q_OUTBOUND		0x00000080 /* fill in outbound isaf_t */
7145d3b8cb7SBill Sommerfeld 
7155d3b8cb7SBill Sommerfeld int sadb_form_query(keysock_in_t *, uint32_t, uint32_t, ipsa_query_t *, int *);
7165d3b8cb7SBill Sommerfeld boolean_t sadb_match_query(ipsa_query_t *q, ipsa_t *sa);
7175d3b8cb7SBill Sommerfeld 
7185d3b8cb7SBill Sommerfeld 
7197c478bd9Sstevel@tonic-gate /*
7207c478bd9Sstevel@tonic-gate  * All functions that return an ipsa_t will return it with IPSA_REFHOLD()
7217c478bd9Sstevel@tonic-gate  * already called.
7227c478bd9Sstevel@tonic-gate  */
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate /* SA retrieval (inbound and outbound) */
7257c478bd9Sstevel@tonic-gate ipsa_t *ipsec_getassocbyspi(isaf_t *, uint32_t, uint32_t *, uint32_t *,
7267c478bd9Sstevel@tonic-gate     sa_family_t);
727bd670b35SErik Nordmark ipsa_t *ipsec_getassocbyconn(isaf_t *, ip_xmit_attr_t *, uint32_t *, uint32_t *,
728bd670b35SErik Nordmark     sa_family_t, uint8_t, ts_label_t *);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate /* SA insertion. */
7317c478bd9Sstevel@tonic-gate int sadb_insertassoc(ipsa_t *, isaf_t *);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate /* SA table construction and destruction. */
734f4b3ec61Sdh void sadbp_init(const char *name, sadbp_t *, int, int, netstack_t *);
735f4b3ec61Sdh void sadbp_flush(sadbp_t *, netstack_t *);
736f4b3ec61Sdh void sadbp_destroy(sadbp_t *, netstack_t *);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate /* SA insertion and deletion. */
7397c478bd9Sstevel@tonic-gate int sadb_insertassoc(ipsa_t *, isaf_t *);
7407c478bd9Sstevel@tonic-gate void sadb_unlinkassoc(ipsa_t *);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate /* Support routines to interface a keysock consumer to PF_KEY. */
7437c478bd9Sstevel@tonic-gate mblk_t *sadb_keysock_out(minor_t);
7449c2c14abSThejaswini Singarajipura int sadb_hardsoftchk(sadb_lifetime_t *, sadb_lifetime_t *, sadb_lifetime_t *);
7455d3b8cb7SBill Sommerfeld int sadb_labelchk(struct keysock_in_s *);
7467c478bd9Sstevel@tonic-gate void sadb_pfkey_echo(queue_t *, mblk_t *, sadb_msg_t *, struct keysock_in_s *,
7477c478bd9Sstevel@tonic-gate     ipsa_t *);
7487c478bd9Sstevel@tonic-gate void sadb_pfkey_error(queue_t *, mblk_t *, int, int, uint_t);
7497c478bd9Sstevel@tonic-gate void sadb_keysock_hello(queue_t **, queue_t *, mblk_t *, void (*)(void *),
750f4b3ec61Sdh     void *, timeout_id_t *, int);
751f4b3ec61Sdh int sadb_addrcheck(queue_t *, mblk_t *, sadb_ext_t *, uint_t, netstack_t *);
752f4b3ec61Sdh boolean_t sadb_addrfix(keysock_in_t *, queue_t *, mblk_t *, netstack_t *);
7537c478bd9Sstevel@tonic-gate int sadb_addrset(ire_t *);
7547c478bd9Sstevel@tonic-gate int sadb_delget_sa(mblk_t *, keysock_in_t *, sadbp_t *, int *, queue_t *,
75538d95a78Smarkfen     uint8_t);
756bd670b35SErik Nordmark 
757bd670b35SErik Nordmark int sadb_purge_sa(mblk_t *, keysock_in_t *, sadb_t *, int *, queue_t *);
758bd670b35SErik Nordmark int sadb_common_add(queue_t *, mblk_t *, sadb_msg_t *,
759f4b3ec61Sdh     keysock_in_t *, isaf_t *, isaf_t *, ipsa_t *, boolean_t, boolean_t, int *,
76038d95a78Smarkfen     netstack_t *, sadbp_t *);
7617c478bd9Sstevel@tonic-gate void sadb_set_usetime(ipsa_t *);
7627c478bd9Sstevel@tonic-gate boolean_t sadb_age_bytes(queue_t *, ipsa_t *, uint64_t, boolean_t);
7639c2c14abSThejaswini Singarajipura int sadb_update_sa(mblk_t *, keysock_in_t *, mblk_t **, sadbp_t *,
764f4b3ec61Sdh     int *, queue_t *, int (*)(mblk_t *, keysock_in_t *, int *, netstack_t *),
76538d95a78Smarkfen     netstack_t *, uint8_t);
766bd670b35SErik Nordmark void sadb_acquire(mblk_t *, ip_xmit_attr_t *, boolean_t, boolean_t);
767628b0c67SMark Fenwick void gcm_params_init(ipsa_t *, uchar_t *, uint_t, uchar_t *, ipsa_cm_mech_t *,
768628b0c67SMark Fenwick     crypto_data_t *);
769628b0c67SMark Fenwick void ccm_params_init(ipsa_t *, uchar_t *, uint_t, uchar_t *, ipsa_cm_mech_t *,
770628b0c67SMark Fenwick     crypto_data_t *);
771628b0c67SMark Fenwick void cbc_params_init(ipsa_t *, uchar_t *, uint_t, uchar_t *, ipsa_cm_mech_t *,
772628b0c67SMark Fenwick     crypto_data_t *);
7737c478bd9Sstevel@tonic-gate 
774f4b3ec61Sdh void sadb_destroy_acquire(ipsacq_t *, netstack_t *);
775f4b3ec61Sdh struct ipsec_stack;
7769c2c14abSThejaswini Singarajipura ipsa_t *sadb_getspi(keysock_in_t *, uint32_t, int *, netstack_t *, uint_t);
777f4b3ec61Sdh void sadb_in_acquire(sadb_msg_t *, sadbp_t *, queue_t *, netstack_t *);
7787c478bd9Sstevel@tonic-gate boolean_t sadb_replay_check(ipsa_t *, uint32_t);
7797c478bd9Sstevel@tonic-gate boolean_t sadb_replay_peek(ipsa_t *, uint32_t);
7809c2c14abSThejaswini Singarajipura int sadb_dump(queue_t *, mblk_t *, keysock_in_t *, sadb_t *);
7817c478bd9Sstevel@tonic-gate void sadb_replay_delete(ipsa_t *);
782bd670b35SErik Nordmark void sadb_ager(sadb_t *, queue_t *, int, netstack_t *);
7837c478bd9Sstevel@tonic-gate 
784f4b3ec61Sdh timeout_id_t sadb_retimeout(hrtime_t, queue_t *, void (*)(void *), void *,
7857c478bd9Sstevel@tonic-gate     uint_t *, uint_t, short);
7867c478bd9Sstevel@tonic-gate void sadb_sa_refrele(void *target);
787930af642SDan McDonald mblk_t *sadb_set_lpkt(ipsa_t *, mblk_t *, ip_recv_attr_t *);
7887c478bd9Sstevel@tonic-gate mblk_t *sadb_clear_lpkt(ipsa_t *);
789bd670b35SErik Nordmark void sadb_buf_pkt(ipsa_t *, mblk_t *, ip_recv_attr_t *);
7909c2c14abSThejaswini Singarajipura void sadb_clear_buf_pkt(void *ipkt);
7919c2c14abSThejaswini Singarajipura 
792bd670b35SErik Nordmark /* Note that buf_pkt is the product of ip_recv_attr_to_mblk() */
7939c2c14abSThejaswini Singarajipura #define	HANDLE_BUF_PKT(taskq, stack, dropper, buf_pkt)			\
7949c2c14abSThejaswini Singarajipura {									\
7959c2c14abSThejaswini Singarajipura 	if (buf_pkt != NULL) {						\
7969c2c14abSThejaswini Singarajipura 		if (taskq_dispatch(taskq, sadb_clear_buf_pkt,		\
797fc8ae2ecSToomas Soome 		    (void *) buf_pkt, TQ_NOSLEEP) == TASKQID_INVALID) {	\
7989c2c14abSThejaswini Singarajipura 		    /* Dispatch was unsuccessful drop the packets. */	\
7999c2c14abSThejaswini Singarajipura 			mblk_t		*tmp;				\
8009c2c14abSThejaswini Singarajipura 			while (buf_pkt != NULL) {			\
8019c2c14abSThejaswini Singarajipura 				tmp = buf_pkt->b_next;			\
8029c2c14abSThejaswini Singarajipura 				buf_pkt->b_next = NULL;			\
803bd670b35SErik Nordmark 				buf_pkt = ip_recv_attr_free_mblk(buf_pkt); \
8049c2c14abSThejaswini Singarajipura 				ip_drop_packet(buf_pkt, B_TRUE, NULL,	\
805bd670b35SErik Nordmark 				    DROPPER(stack,			\
8069c2c14abSThejaswini Singarajipura 				    ipds_sadb_inidle_timeout),		\
8079c2c14abSThejaswini Singarajipura 				    &dropper);				\
8089c2c14abSThejaswini Singarajipura 				buf_pkt = tmp;				\
8099c2c14abSThejaswini Singarajipura 			}						\
8109c2c14abSThejaswini Singarajipura 		}							\
8119c2c14abSThejaswini Singarajipura 	}								\
8129c2c14abSThejaswini Singarajipura }									\
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate /*
815bd670b35SErik Nordmark  * Two IPsec rate-limiting routines.
8167c478bd9Sstevel@tonic-gate  */
817f4b3ec61Sdh /*PRINTFLIKE6*/
818f4b3ec61Sdh extern void ipsec_rl_strlog(netstack_t *, short, short, char,
819f4b3ec61Sdh     ushort_t, char *, ...)
820f4b3ec61Sdh     __KPRINTFLIKE(6);
8217c478bd9Sstevel@tonic-gate extern void ipsec_assocfailure(short, short, char, ushort_t, char *, uint32_t,
822f4b3ec61Sdh     void *, int, netstack_t *);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate /*
8257c478bd9Sstevel@tonic-gate  * Algorithm types.
8267c478bd9Sstevel@tonic-gate  */
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate #define	IPSEC_NALGTYPES 	2
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate typedef enum ipsec_algtype {
8317c478bd9Sstevel@tonic-gate 	IPSEC_ALG_AUTH = 0,
832bd670b35SErik Nordmark 	IPSEC_ALG_ENCR = 1,
833bd670b35SErik Nordmark 	IPSEC_ALG_ALL = 2
8347c478bd9Sstevel@tonic-gate } ipsec_algtype_t;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate /*
8377c478bd9Sstevel@tonic-gate  * Definitions as per IPsec/ISAKMP DOI.
8387c478bd9Sstevel@tonic-gate  */
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate #define	IPSEC_MAX_ALGS		256
8417c478bd9Sstevel@tonic-gate #define	PROTO_IPSEC_AH		2
8427c478bd9Sstevel@tonic-gate #define	PROTO_IPSEC_ESP		3
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate /*
8457c478bd9Sstevel@tonic-gate  * Common algorithm info.
8467c478bd9Sstevel@tonic-gate  */
8477c478bd9Sstevel@tonic-gate typedef struct ipsec_alginfo
8487c478bd9Sstevel@tonic-gate {
8497c478bd9Sstevel@tonic-gate 	uint8_t		alg_id;
8507c478bd9Sstevel@tonic-gate 	uint8_t		alg_flags;
8517c478bd9Sstevel@tonic-gate 	uint16_t	*alg_key_sizes;
8527c478bd9Sstevel@tonic-gate 	uint16_t	*alg_block_sizes;
853628b0c67SMark Fenwick 	uint16_t	*alg_params;
8547c478bd9Sstevel@tonic-gate 	uint16_t	alg_nkey_sizes;
855628b0c67SMark Fenwick 	uint16_t	alg_ivlen;
856628b0c67SMark Fenwick 	uint16_t	alg_icvlen;
857628b0c67SMark Fenwick 	uint8_t		alg_saltlen;
8587c478bd9Sstevel@tonic-gate 	uint16_t	alg_nblock_sizes;
859628b0c67SMark Fenwick 	uint16_t	alg_nparams;
8607c478bd9Sstevel@tonic-gate 	uint16_t	alg_minbits;
8617c478bd9Sstevel@tonic-gate 	uint16_t	alg_maxbits;
8627c478bd9Sstevel@tonic-gate 	uint16_t	alg_datalen;
8637c478bd9Sstevel@tonic-gate 	/*
8647c478bd9Sstevel@tonic-gate 	 * increment: number of bits from keysize to keysize
8657c478bd9Sstevel@tonic-gate 	 * default: # of increments from min to default key len
8667c478bd9Sstevel@tonic-gate 	 */
8677c478bd9Sstevel@tonic-gate 	uint16_t	alg_increment;
8687c478bd9Sstevel@tonic-gate 	uint16_t	alg_default;
8697c478bd9Sstevel@tonic-gate 	uint16_t	alg_default_bits;
8707c478bd9Sstevel@tonic-gate 	/*
8717c478bd9Sstevel@tonic-gate 	 * Min, max, and default key sizes effectively supported
8727c478bd9Sstevel@tonic-gate 	 * by the encryption framework.
8737c478bd9Sstevel@tonic-gate 	 */
8747c478bd9Sstevel@tonic-gate 	uint16_t	alg_ef_minbits;
8757c478bd9Sstevel@tonic-gate 	uint16_t	alg_ef_maxbits;
8767c478bd9Sstevel@tonic-gate 	uint16_t	alg_ef_default;
8777c478bd9Sstevel@tonic-gate 	uint16_t	alg_ef_default_bits;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	crypto_mech_type_t alg_mech_type;	/* KCF mechanism type */
8807c478bd9Sstevel@tonic-gate 	crypto_mech_name_t alg_mech_name;	/* KCF mechanism name */
8817c478bd9Sstevel@tonic-gate } ipsec_alginfo_t;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate #define	alg_datalen alg_block_sizes[0]
8847c478bd9Sstevel@tonic-gate #define	ALG_VALID(_alg)	((_alg)->alg_flags & ALG_FLAG_VALID)
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate /*
8877c478bd9Sstevel@tonic-gate  * Software crypto execution mode.
8887c478bd9Sstevel@tonic-gate  */
8897c478bd9Sstevel@tonic-gate typedef enum {
8907c478bd9Sstevel@tonic-gate 	IPSEC_ALGS_EXEC_SYNC = 0,
8917c478bd9Sstevel@tonic-gate 	IPSEC_ALGS_EXEC_ASYNC = 1
8927c478bd9Sstevel@tonic-gate } ipsec_algs_exec_mode_t;
8937c478bd9Sstevel@tonic-gate 
894f4b3ec61Sdh extern void ipsec_alg_reg(ipsec_algtype_t, ipsec_alginfo_t *, netstack_t *);
895f4b3ec61Sdh extern void ipsec_alg_unreg(ipsec_algtype_t, uint8_t, netstack_t *);
896f4b3ec61Sdh extern void ipsec_alg_fix_min_max(ipsec_alginfo_t *, ipsec_algtype_t,
897f4b3ec61Sdh     netstack_t *ns);
898628b0c67SMark Fenwick extern void alg_flag_check(ipsec_alginfo_t *);
8997c478bd9Sstevel@tonic-gate extern void ipsec_alg_free(ipsec_alginfo_t *);
9007c478bd9Sstevel@tonic-gate extern void ipsec_register_prov_update(void);
901bd670b35SErik Nordmark extern void sadb_alg_update(ipsec_algtype_t, uint8_t, boolean_t, netstack_t *);
9027c478bd9Sstevel@tonic-gate 
903bd670b35SErik Nordmark extern int sadb_sens_len_from_label(ts_label_t *);
904bd670b35SErik Nordmark extern void sadb_sens_from_label(sadb_sens_t *, int, ts_label_t *, int);
9055d3b8cb7SBill Sommerfeld 
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate  * Context templates management.
9087c478bd9Sstevel@tonic-gate  */
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate #define	IPSEC_CTX_TMPL_ALLOC ((crypto_ctx_template_t)-1)
9117c478bd9Sstevel@tonic-gate #define	IPSEC_CTX_TMPL(_sa, _which, _type, _tmpl) {			\
9127c478bd9Sstevel@tonic-gate 	if ((_tmpl = (_sa)->_which) == IPSEC_CTX_TMPL_ALLOC) {		\
9137c478bd9Sstevel@tonic-gate 		mutex_enter(&assoc->ipsa_lock);				\
9147c478bd9Sstevel@tonic-gate 		if ((_sa)->_which == IPSEC_CTX_TMPL_ALLOC) {		\
915f4b3ec61Sdh 			ipsec_stack_t *ipss;				\
916f4b3ec61Sdh 									\
917f4b3ec61Sdh 			ipss = assoc->ipsa_netstack->netstack_ipsec;	\
91869e71331SBayard Bell 			rw_enter(&ipss->ipsec_alg_lock, RW_READER);	\
9197c478bd9Sstevel@tonic-gate 			(void) ipsec_create_ctx_tmpl(_sa, _type);	\
92069e71331SBayard Bell 			rw_exit(&ipss->ipsec_alg_lock);			\
9217c478bd9Sstevel@tonic-gate 		}							\
9227c478bd9Sstevel@tonic-gate 		mutex_exit(&assoc->ipsa_lock);				\
9237c478bd9Sstevel@tonic-gate 		if ((_tmpl = (_sa)->_which) == IPSEC_CTX_TMPL_ALLOC)	\
9247c478bd9Sstevel@tonic-gate 			_tmpl = NULL;					\
9257c478bd9Sstevel@tonic-gate 	}								\
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate extern int ipsec_create_ctx_tmpl(ipsa_t *, ipsec_algtype_t);
9297c478bd9Sstevel@tonic-gate extern void ipsec_destroy_ctx_tmpl(ipsa_t *, ipsec_algtype_t);
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate /* key checking */
9327c478bd9Sstevel@tonic-gate extern int ipsec_check_key(crypto_mech_type_t, sadb_key_t *, boolean_t, int *);
9337c478bd9Sstevel@tonic-gate 
934f4b3ec61Sdh typedef struct ipsec_kstats_s {
9357c478bd9Sstevel@tonic-gate 	kstat_named_t esp_stat_in_requests;
9367c478bd9Sstevel@tonic-gate 	kstat_named_t esp_stat_in_discards;
9377c478bd9Sstevel@tonic-gate 	kstat_named_t esp_stat_lookup_failure;
9387c478bd9Sstevel@tonic-gate 	kstat_named_t ah_stat_in_requests;
9397c478bd9Sstevel@tonic-gate 	kstat_named_t ah_stat_in_discards;
9407c478bd9Sstevel@tonic-gate 	kstat_named_t ah_stat_lookup_failure;
941bffb04cfSmarkfen 	kstat_named_t sadb_acquire_maxpackets;
942bffb04cfSmarkfen 	kstat_named_t sadb_acquire_qhiwater;
9437c478bd9Sstevel@tonic-gate } ipsec_kstats_t;
9447c478bd9Sstevel@tonic-gate 
945f4b3ec61Sdh /*
946f4b3ec61Sdh  * (ipss)->ipsec_kstats is equal to (ipss)->ipsec_ksp->ks_data if
947f4b3ec61Sdh  * kstat_create_netstack for (ipss)->ipsec_ksp succeeds, but when it
948f4b3ec61Sdh  * fails, it will be NULL. Note this is done for all stack instances,
949f4b3ec61Sdh  * so it *could* fail. hence a non-NULL checking is done for
950f4b3ec61Sdh  * IP_ESP_BUMP_STAT, IP_AH_BUMP_STAT and IP_ACQUIRE_STAT
951f4b3ec61Sdh  */
952f4b3ec61Sdh #define	IP_ESP_BUMP_STAT(ipss, x)					\
953f4b3ec61Sdh do {									\
954f4b3ec61Sdh 	if ((ipss)->ipsec_kstats != NULL)				\
955f4b3ec61Sdh 		((ipss)->ipsec_kstats->esp_stat_ ## x).value.ui64++;	\
956f4b3ec61Sdh _NOTE(CONSTCOND)							\
957f4b3ec61Sdh } while (0)
958f4b3ec61Sdh 
959f4b3ec61Sdh #define	IP_AH_BUMP_STAT(ipss, x)					\
960f4b3ec61Sdh do {									\
961f4b3ec61Sdh 	if ((ipss)->ipsec_kstats != NULL)				\
962f4b3ec61Sdh 		((ipss)->ipsec_kstats->ah_stat_ ## x).value.ui64++;	\
963f4b3ec61Sdh _NOTE(CONSTCOND)							\
964f4b3ec61Sdh } while (0)
965f4b3ec61Sdh 
966f4b3ec61Sdh #define	IP_ACQUIRE_STAT(ipss, val, new)					\
967f4b3ec61Sdh do {									\
968f4b3ec61Sdh 	if ((ipss)->ipsec_kstats != NULL &&				\
969f4b3ec61Sdh 	    ((uint64_t)(new)) >						\
970f4b3ec61Sdh 	    ((ipss)->ipsec_kstats->sadb_acquire_ ## val).value.ui64)	\
971f4b3ec61Sdh 		((ipss)->ipsec_kstats->sadb_acquire_ ## val).value.ui64 = \
972f4b3ec61Sdh 			((uint64_t)(new));				\
973f4b3ec61Sdh _NOTE(CONSTCOND)							\
974f4b3ec61Sdh } while (0)
9757c478bd9Sstevel@tonic-gate 
9769c2c14abSThejaswini Singarajipura 
9777c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate #endif
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate #endif /* _INET_SADB_H */
982