xref: /titanic_44/usr/src/uts/common/inet/sadb.h (revision 3c04777e760a0ff56ed78e3cd151f923a271e8df)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23fb87b5d2Ssommerfe  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef	_INET_SADB_H
287c478bd9Sstevel@tonic-gate #define	_INET_SADB_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
337c478bd9Sstevel@tonic-gate extern "C" {
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <inet/ipsec_info.h>
377c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h>
387c478bd9Sstevel@tonic-gate #include <sys/crypto/api.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	IPSA_MAX_ADDRLEN 4	/* Max address len. (in 32-bits) for an SA. */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Return codes of IPsec processing functions.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate typedef enum {
467c478bd9Sstevel@tonic-gate 	IPSEC_STATUS_SUCCESS = 1,
477c478bd9Sstevel@tonic-gate 	IPSEC_STATUS_FAILED = 2,
487c478bd9Sstevel@tonic-gate 	IPSEC_STATUS_PENDING = 3
497c478bd9Sstevel@tonic-gate } ipsec_status_t;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * IP security association.  Synchronization assumes 32-bit loads, so
537c478bd9Sstevel@tonic-gate  * the 64-bit quantities can't even be be read w/o locking it down!
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* keying info */
577c478bd9Sstevel@tonic-gate typedef struct ipsa_key_s {
587c478bd9Sstevel@tonic-gate 	void *sak_key;		/* Algorithm key. */
597c478bd9Sstevel@tonic-gate 	uint_t sak_keylen;	/* Algorithm key length (in bytes). */
607c478bd9Sstevel@tonic-gate 	uint_t sak_keybits;	/* Algorithm key length (in bits) */
617c478bd9Sstevel@tonic-gate 	uint_t sak_algid;	/* Algorithm ID number. */
627c478bd9Sstevel@tonic-gate } ipsa_key_t;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* the security association */
657c478bd9Sstevel@tonic-gate typedef struct ipsa_s {
667c478bd9Sstevel@tonic-gate 	struct ipsa_s *ipsa_next;	/* Next in hash bucket */
677c478bd9Sstevel@tonic-gate 	struct ipsa_s **ipsa_ptpn;	/* Pointer to previous next pointer. */
687c478bd9Sstevel@tonic-gate 	kmutex_t *ipsa_linklock;	/* Pointer to hash-chain lock. */
697c478bd9Sstevel@tonic-gate 	void (*ipsa_freefunc)(struct ipsa_s *); /* freeassoc function */
707c478bd9Sstevel@tonic-gate 	/*
717c478bd9Sstevel@tonic-gate 	 * NOTE: I may need more pointers, depending on future SA
727c478bd9Sstevel@tonic-gate 	 * requirements.
737c478bd9Sstevel@tonic-gate 	 */
747c478bd9Sstevel@tonic-gate 	ipsa_key_t ipsa_authkeydata;
757c478bd9Sstevel@tonic-gate #define	ipsa_authkey ipsa_authkeydata.sak_key
767c478bd9Sstevel@tonic-gate #define	ipsa_authkeylen ipsa_authkeydata.sak_keylen
777c478bd9Sstevel@tonic-gate #define	ipsa_authkeybits ipsa_authkeydata.sak_keybits
787c478bd9Sstevel@tonic-gate #define	ipsa_auth_alg ipsa_authkeydata.sak_algid
797c478bd9Sstevel@tonic-gate 	ipsa_key_t ipsa_encrkeydata;
807c478bd9Sstevel@tonic-gate #define	ipsa_encrkey ipsa_encrkeydata.sak_key
817c478bd9Sstevel@tonic-gate #define	ipsa_encrkeylen ipsa_encrkeydata.sak_keylen
827c478bd9Sstevel@tonic-gate #define	ipsa_encrkeybits ipsa_encrkeydata.sak_keybits
837c478bd9Sstevel@tonic-gate #define	ipsa_encr_alg ipsa_encrkeydata.sak_algid
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	struct ipsid_s *ipsa_src_cid;	/* Source certificate identity */
867c478bd9Sstevel@tonic-gate 	struct ipsid_s *ipsa_dst_cid;	/* Destination certificate identity */
877c478bd9Sstevel@tonic-gate 	struct ipsid_s *ipsa_proxy_cid;	/* (src) Proxy agent's cert. id. */
887c478bd9Sstevel@tonic-gate 	uint64_t *ipsa_integ;	/* Integrity bitmap */
897c478bd9Sstevel@tonic-gate 	uint64_t *ipsa_sens;	/* Sensitivity bitmap */
907c478bd9Sstevel@tonic-gate 	mblk_t	*ipsa_lpkt;	/* Packet received while larval (CAS me) */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * PF_KEYv2 supports a replay window size of 255.  Hence there is a
947c478bd9Sstevel@tonic-gate 	 * need a bit vector to support a replay window of 255.  256 is a nice
957c478bd9Sstevel@tonic-gate 	 * round number, so I support that.
967c478bd9Sstevel@tonic-gate 	 *
977c478bd9Sstevel@tonic-gate 	 * Use an array of uint64_t for best performance on 64-bit
987c478bd9Sstevel@tonic-gate 	 * processors.  (And hope that 32-bit compilers can handle things
997c478bd9Sstevel@tonic-gate 	 * okay.)  The " >> 6 " is to get the appropriate number of 64-bit
1007c478bd9Sstevel@tonic-gate 	 * ints.
1017c478bd9Sstevel@tonic-gate 	 */
1027c478bd9Sstevel@tonic-gate #define	SADB_MAX_REPLAY 256	/* Must be 0 mod 64. */
1037c478bd9Sstevel@tonic-gate 	uint64_t ipsa_replay_arr[SADB_MAX_REPLAY >> 6];
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	uint64_t ipsa_unique_id;	/* Non-zero for unique SAs */
1067c478bd9Sstevel@tonic-gate 	uint64_t ipsa_unique_mask;	/* mask value for unique_id */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/*
1097c478bd9Sstevel@tonic-gate 	 * Reference count semantics:
1107c478bd9Sstevel@tonic-gate 	 *
1117c478bd9Sstevel@tonic-gate 	 *	An SA has a reference count of 1 if something's pointing
1127c478bd9Sstevel@tonic-gate 	 *	to it.  This includes being in a hash table.  So if an
1137c478bd9Sstevel@tonic-gate 	 *	SA is in a hash table, it has a reference count of at least 1.
1147c478bd9Sstevel@tonic-gate 	 *
1157c478bd9Sstevel@tonic-gate 	 *	When a ptr. to an IPSA is assigned, you MUST REFHOLD after
1167c478bd9Sstevel@tonic-gate 	 *	said assignment.  When a ptr. to an IPSA is released
1177c478bd9Sstevel@tonic-gate 	 *	you MUST REFRELE.  When the refcount hits 0, REFRELE
1187c478bd9Sstevel@tonic-gate 	 *	will free the IPSA.
1197c478bd9Sstevel@tonic-gate 	 */
1207c478bd9Sstevel@tonic-gate 	kmutex_t ipsa_lock;	/* Locks non-linkage/refcnt fields. */
1217c478bd9Sstevel@tonic-gate 	/* Q:  Since I may be doing refcnts differently, will I need cv? */
1227c478bd9Sstevel@tonic-gate 	uint_t ipsa_refcnt;	/* Reference count. */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/*
1257c478bd9Sstevel@tonic-gate 	 * The following four time fields are the ones monitored by ah_ager()
1267c478bd9Sstevel@tonic-gate 	 * and esp_ager() respectively.  They are all absolute wall-clock
1277c478bd9Sstevel@tonic-gate 	 * times.  The times of creation (i.e. add time) and first use are
1287c478bd9Sstevel@tonic-gate 	 * pretty straightforward.  The soft and hard expire times are
1297c478bd9Sstevel@tonic-gate 	 * derived from the times of first use and creation, plus the minimum
1307c478bd9Sstevel@tonic-gate 	 * expiration times in the fields that follow this.
1317c478bd9Sstevel@tonic-gate 	 *
1327c478bd9Sstevel@tonic-gate 	 * For example, if I had a hard add time of 30 seconds, and a hard
1337c478bd9Sstevel@tonic-gate 	 * use time of 15, the ipsa_hardexpiretime would be time of add, plus
1347c478bd9Sstevel@tonic-gate 	 * 30 seconds.  If I USE the SA such that time of first use plus 15
1357c478bd9Sstevel@tonic-gate 	 * seconds would be earlier than the add time plus 30 seconds, then
1367c478bd9Sstevel@tonic-gate 	 * ipsa_hardexpiretime would become this earlier time.
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 	time_t ipsa_addtime;	/* Time I was added. */
1397c478bd9Sstevel@tonic-gate 	time_t ipsa_usetime;	/* Time of my first use. */
1407c478bd9Sstevel@tonic-gate 	time_t ipsa_softexpiretime;	/* Time of my first soft expire. */
1417c478bd9Sstevel@tonic-gate 	time_t ipsa_hardexpiretime;	/* Time of my first hard expire. */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * The following fields are directly reflected in PF_KEYv2 LIFETIME
1457c478bd9Sstevel@tonic-gate 	 * extensions.  The time_ts are in number-of-seconds, and the bytes
1467c478bd9Sstevel@tonic-gate 	 * are in... bytes.
1477c478bd9Sstevel@tonic-gate 	 */
1487c478bd9Sstevel@tonic-gate 	time_t ipsa_softaddlt;	/* Seconds of soft lifetime after add. */
1497c478bd9Sstevel@tonic-gate 	time_t ipsa_softuselt;	/* Seconds of soft lifetime after first use. */
1507c478bd9Sstevel@tonic-gate 	time_t ipsa_hardaddlt;	/* Seconds of hard lifetime after add. */
1517c478bd9Sstevel@tonic-gate 	time_t ipsa_harduselt;	/* Seconds of hard lifetime after first use. */
1527c478bd9Sstevel@tonic-gate 	uint64_t ipsa_softbyteslt;	/* Bytes of soft lifetime. */
1537c478bd9Sstevel@tonic-gate 	uint64_t ipsa_hardbyteslt;	/* Bytes of hard lifetime. */
1547c478bd9Sstevel@tonic-gate 	uint64_t ipsa_bytes;	/* Bytes encrypted/authed by this SA. */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * "Allocations" are a concept mentioned in PF_KEYv2.  We do not
1587c478bd9Sstevel@tonic-gate 	 * support them, except to record them per the PF_KEYv2 spec.
1597c478bd9Sstevel@tonic-gate 	 */
1607c478bd9Sstevel@tonic-gate 	uint_t ipsa_softalloc;	/* Allocations allowed (soft). */
1617c478bd9Sstevel@tonic-gate 	uint_t ipsa_hardalloc;	/* Allocations allowed (hard). */
1627c478bd9Sstevel@tonic-gate 	uint_t ipsa_alloc;	/* Allocations made. */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	uint_t ipsa_integlen;	/* Length of the integrity bitmap (bytes). */
1657c478bd9Sstevel@tonic-gate 	uint_t ipsa_senslen;	/* Length of the sensitivity bitmap (bytes). */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	uint_t ipsa_type;	/* Type of security association. (AH/etc.) */
1687c478bd9Sstevel@tonic-gate 	uint_t ipsa_dpd;	/* Domain for sensitivity bit vectors. */
1697c478bd9Sstevel@tonic-gate 	uint_t ipsa_senslevel;	/* Sensitivity level. */
1707c478bd9Sstevel@tonic-gate 	uint_t ipsa_integlevel;	/* Integrity level. */
1717c478bd9Sstevel@tonic-gate 	uint_t ipsa_state;	/* State of my association. */
1727c478bd9Sstevel@tonic-gate 	uint_t ipsa_replay_wsize; /* Size of replay window */
1737c478bd9Sstevel@tonic-gate 	uint32_t ipsa_flags;	/* Flags for security association. */
1747c478bd9Sstevel@tonic-gate 	uint32_t ipsa_spi;	/* Security parameters index. */
1757c478bd9Sstevel@tonic-gate 	uint32_t ipsa_replay;	/* Highest seen replay value for this SA. */
1767c478bd9Sstevel@tonic-gate 	uint32_t ipsa_kmp;	/* key management proto */
1777c478bd9Sstevel@tonic-gate 	uint32_t ipsa_kmc;	/* key management cookie */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	boolean_t ipsa_haspeer;	/* Has peer in another table. */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * Address storage.
1837c478bd9Sstevel@tonic-gate 	 * The source address can be INADDR_ANY, IN6ADDR_ANY, etc.
1847c478bd9Sstevel@tonic-gate 	 *
1857c478bd9Sstevel@tonic-gate 	 * Address families (per sys/socket.h) guide us.  We could have just
1867c478bd9Sstevel@tonic-gate 	 * used sockaddr_storage
1877c478bd9Sstevel@tonic-gate 	 */
1887c478bd9Sstevel@tonic-gate 	sa_family_t ipsa_addrfam;
1897c478bd9Sstevel@tonic-gate 	sa_family_t ipsa_proxyfam;	/* Proxy AF can be != src/dst AF. */
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	uint32_t ipsa_srcaddr[IPSA_MAX_ADDRLEN];
1927c478bd9Sstevel@tonic-gate 	uint32_t ipsa_dstaddr[IPSA_MAX_ADDRLEN];
1937c478bd9Sstevel@tonic-gate 	uint32_t ipsa_proxysrc[IPSA_MAX_ADDRLEN];
1947c478bd9Sstevel@tonic-gate 	uint32_t ipsa_proxydst[IPSA_MAX_ADDRLEN];
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/* these can only be v4 */
1977c478bd9Sstevel@tonic-gate 	uint32_t ipsa_natt_addr_loc[IPSA_MAX_ADDRLEN];
1987c478bd9Sstevel@tonic-gate 	uint32_t ipsa_natt_addr_rem[IPSA_MAX_ADDRLEN];
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	uint16_t ipsa_inbound_cksum; /* cksum correction for inbound packets */
2017c478bd9Sstevel@tonic-gate 	uint16_t ipsa_remote_port; /* the other port that isn't 4500 */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	timeout_id_t ipsa_natt_ka_timer;
2047c478bd9Sstevel@tonic-gate 	queue_t *ipsa_natt_q;
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * icmp type and code. *_end are to specify ranges. if only
2077c478bd9Sstevel@tonic-gate 	 * a single value, * and *_end are the same value.
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	uint8_t ipsa_icmp_type;
2107c478bd9Sstevel@tonic-gate 	uint8_t ipsa_icmp_type_end;
2117c478bd9Sstevel@tonic-gate 	uint8_t ipsa_icmp_code;
2127c478bd9Sstevel@tonic-gate 	uint8_t ipsa_icmp_code_end;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * For the kernel crypto framework.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	crypto_key_t ipsa_kcfauthkey;		/* authentication key */
2187c478bd9Sstevel@tonic-gate 	crypto_key_t ipsa_kcfencrkey;		/* encryption key */
2197c478bd9Sstevel@tonic-gate 	crypto_ctx_template_t ipsa_authtmpl;	/* auth context template */
2207c478bd9Sstevel@tonic-gate 	crypto_ctx_template_t ipsa_encrtmpl;	/* encr context template */
2217c478bd9Sstevel@tonic-gate 	crypto_mechanism_t ipsa_amech;		/* auth mech type and ICV len */
2227c478bd9Sstevel@tonic-gate 	crypto_mechanism_t ipsa_emech;		/* encr mech type */
2237c478bd9Sstevel@tonic-gate 	size_t ipsa_mac_len;			/* auth MAC length */
2247c478bd9Sstevel@tonic-gate 	size_t ipsa_iv_len;			/* encr IV length */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * Input and output processing functions called from IP.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	ipsec_status_t (*ipsa_output_func)(mblk_t *);
2307c478bd9Sstevel@tonic-gate 	ipsec_status_t (*ipsa_input_func)(mblk_t *, void *);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/* MLS boxen will probably need more fields in here. */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate } ipsa_t;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * ipsa_t address handling macros.  We want these to be inlined, and deal
2387c478bd9Sstevel@tonic-gate  * with 32-bit words to avoid bcmp/bcopy calls.
2397c478bd9Sstevel@tonic-gate  *
2407c478bd9Sstevel@tonic-gate  * Assume we only have AF_INET and AF_INET6 addresses for now.  Also assume
2417c478bd9Sstevel@tonic-gate  * that we have 32-bit alignment on everything.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate #define	IPSA_IS_ADDR_UNSPEC(addr, fam) ((((uint32_t *)(addr))[0] == 0) && \
2447c478bd9Sstevel@tonic-gate 	(((fam) == AF_INET) || (((uint32_t *)(addr))[3] == 0 && \
2457c478bd9Sstevel@tonic-gate 	((uint32_t *)(addr))[2] == 0 && ((uint32_t *)(addr))[1] == 0)))
2467c478bd9Sstevel@tonic-gate #define	IPSA_ARE_ADDR_EQUAL(addr1, addr2, fam) \
2477c478bd9Sstevel@tonic-gate 	((((uint32_t *)(addr1))[0] == ((uint32_t *)(addr2))[0]) && \
2487c478bd9Sstevel@tonic-gate 	(((fam) == AF_INET) || \
2497c478bd9Sstevel@tonic-gate 	(((uint32_t *)(addr1))[3] == ((uint32_t *)(addr2))[3] && \
2507c478bd9Sstevel@tonic-gate 	((uint32_t *)(addr1))[2] == ((uint32_t *)(addr2))[2] && \
2517c478bd9Sstevel@tonic-gate 	((uint32_t *)(addr1))[1] == ((uint32_t *)(addr2))[1])))
2527c478bd9Sstevel@tonic-gate #define	IPSA_COPY_ADDR(dstaddr, srcaddr, fam) { \
2537c478bd9Sstevel@tonic-gate 	((uint32_t *)(dstaddr))[0] = ((uint32_t *)(srcaddr))[0]; \
2547c478bd9Sstevel@tonic-gate 	if ((fam) == AF_INET6) {\
2557c478bd9Sstevel@tonic-gate 		((uint32_t *)(dstaddr))[1] = ((uint32_t *)(srcaddr))[1]; \
2567c478bd9Sstevel@tonic-gate 		((uint32_t *)(dstaddr))[2] = ((uint32_t *)(srcaddr))[2]; \
2577c478bd9Sstevel@tonic-gate 		((uint32_t *)(dstaddr))[3] = ((uint32_t *)(srcaddr))[3]; } }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * ipsa_t reference hold/release macros.
2617c478bd9Sstevel@tonic-gate  *
2627c478bd9Sstevel@tonic-gate  * If you have a pointer, you REFHOLD.  If you are releasing a pointer, you
2637c478bd9Sstevel@tonic-gate  * REFRELE.  An ipsa_t that is newly inserted into the table should have
2647c478bd9Sstevel@tonic-gate  * a reference count of 1 (for the table's pointer), plus 1 more for every
2657c478bd9Sstevel@tonic-gate  * pointer that is referencing the ipsa_t.
2667c478bd9Sstevel@tonic-gate  */
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate #define	IPSA_REFHOLD(ipsa) {			\
2697c478bd9Sstevel@tonic-gate 	atomic_add_32(&(ipsa)->ipsa_refcnt, 1);	\
2707c478bd9Sstevel@tonic-gate 	ASSERT((ipsa)->ipsa_refcnt != 0);	\
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * Decrement the reference count on the SA.
2757c478bd9Sstevel@tonic-gate  * In architectures e.g sun4u, where atomic_add_32_nv is just
2767c478bd9Sstevel@tonic-gate  * a cas, we need to maintain the right memory barrier semantics
2777c478bd9Sstevel@tonic-gate  * as that of mutex_exit i.e all the loads and stores should complete
2787c478bd9Sstevel@tonic-gate  * before the cas is executed. membar_exit() does that here.
2797c478bd9Sstevel@tonic-gate  */
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate #define	IPSA_REFRELE(ipsa) {					\
2827c478bd9Sstevel@tonic-gate 	ASSERT((ipsa)->ipsa_refcnt != 0);			\
2837c478bd9Sstevel@tonic-gate 	membar_exit();						\
2847c478bd9Sstevel@tonic-gate 	if (atomic_add_32_nv(&(ipsa)->ipsa_refcnt, -1) == 0)	\
2857c478bd9Sstevel@tonic-gate 		((ipsa)->ipsa_freefunc)(ipsa);			\
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * Security association hash macros and definitions.  For now, assume the
2907c478bd9Sstevel@tonic-gate  * IPsec model, and hash outbounds on destination address, and inbounds on
2917c478bd9Sstevel@tonic-gate  * SPI.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate 
294fb87b5d2Ssommerfe #define	IPSEC_DEFAULT_HASH_SIZE 256
295fb87b5d2Ssommerfe 
296fb87b5d2Ssommerfe #define	INBOUND_HASH(sadb, spi) ((spi) % ((sadb)->sdb_hashsize))
297fb87b5d2Ssommerfe #define	OUTBOUND_HASH_V4(sadb, v4addr) ((v4addr) % ((sadb)->sdb_hashsize))
298fb87b5d2Ssommerfe #define	OUTBOUND_HASH_V6(sadb, v6addr) OUTBOUND_HASH_V4((sadb), \
299*3c04777eSsommerfe 	(*(uint32_t *)&(v6addr)) ^ (*(((uint32_t *)&(v6addr)) + 1)) ^ \
300*3c04777eSsommerfe 	(*(((uint32_t *)&(v6addr)) + 2)) ^ (*(((uint32_t *)&(v6addr)) + 3)))
301fb87b5d2Ssommerfe 
302fb87b5d2Ssommerfe /*
303fb87b5d2Ssommerfe  * Syntactic sugar to find the appropriate hash bucket directly.
304fb87b5d2Ssommerfe  */
305fb87b5d2Ssommerfe 
306fb87b5d2Ssommerfe #define	INBOUND_BUCKET(sadb, spi) &(((sadb)->sdb_if)[INBOUND_HASH(sadb, spi)])
307fb87b5d2Ssommerfe #define	OUTBOUND_BUCKET_V4(sadb, v4addr) \
308fb87b5d2Ssommerfe 	&(((sadb)->sdb_of)[OUTBOUND_HASH_V4(sadb, v4addr)])
309fb87b5d2Ssommerfe #define	OUTBOUND_BUCKET_V6(sadb, v6addr) \
310fb87b5d2Ssommerfe 	&(((sadb)->sdb_of)[OUTBOUND_HASH_V6(sadb, v6addr)])
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate #define	IPSA_F_PFS	SADB_SAFLAGS_PFS	/* PFS in use for this SA? */
3137c478bd9Sstevel@tonic-gate #define	IPSA_F_NOREPFLD	SADB_SAFLAGS_NOREPLAY	/* No replay field, for */
3147c478bd9Sstevel@tonic-gate 						/* backward compat. */
3157c478bd9Sstevel@tonic-gate #define	IPSA_F_USED	SADB_X_SAFLAGS_USED	/* SA has been used. */
3167c478bd9Sstevel@tonic-gate #define	IPSA_F_UNIQUE	SADB_X_SAFLAGS_UNIQUE	/* SA is unique */
3177c478bd9Sstevel@tonic-gate #define	IPSA_F_AALG1	SADB_X_SAFLAGS_AALG1	/* Auth alg flag 1 */
3187c478bd9Sstevel@tonic-gate #define	IPSA_F_AALG2	SADB_X_SAFLAGS_AALG2	/* Auth alg flag 2 */
3197c478bd9Sstevel@tonic-gate #define	IPSA_F_EALG1	SADB_X_SAFLAGS_EALG1	/* Encrypt alg flag 1 */
3207c478bd9Sstevel@tonic-gate #define	IPSA_F_EALG2	SADB_X_SAFLAGS_EALG2	/* Encrypt alg flag 2 */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate #define	IPSA_F_HW	0x200000		/* hwaccel capable SA */
3237c478bd9Sstevel@tonic-gate #define	IPSA_F_NATT_LOC	SADB_X_SAFLAGS_NATT_LOC
3247c478bd9Sstevel@tonic-gate #define	IPSA_F_NATT_REM	SADB_X_SAFLAGS_NATT_REM
3257c478bd9Sstevel@tonic-gate #define	IPSA_F_NATT	(SADB_X_SAFLAGS_NATT_LOC | SADB_X_SAFLAGS_NATT_REM)
3267c478bd9Sstevel@tonic-gate #define	IPSA_F_CINVALID	0x40000		/* SA shouldn't be cached */
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /* SA states are important for handling UPDATE PF_KEY messages. */
3297c478bd9Sstevel@tonic-gate #define	IPSA_STATE_LARVAL	SADB_SASTATE_LARVAL
3307c478bd9Sstevel@tonic-gate #define	IPSA_STATE_MATURE	SADB_SASTATE_MATURE
3317c478bd9Sstevel@tonic-gate #define	IPSA_STATE_DYING	SADB_SASTATE_DYING
3327c478bd9Sstevel@tonic-gate #define	IPSA_STATE_DEAD		SADB_SASTATE_DEAD
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate  * NOTE:  If the document authors do things right in defining algorithms, we'll
3367c478bd9Sstevel@tonic-gate  *	  probably have flags for what all is here w.r.t. replay, ESP w/HMAC,
3377c478bd9Sstevel@tonic-gate  *	  etc.
3387c478bd9Sstevel@tonic-gate  */
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate #define	IPSA_T_ACQUIRE	SEC_TYPE_NONE	/* If this typed returned, sa needed */
3417c478bd9Sstevel@tonic-gate #define	IPSA_T_AH	SEC_TYPE_AH	/* IPsec AH association */
3427c478bd9Sstevel@tonic-gate #define	IPSA_T_ESP	SEC_TYPE_ESP	/* IPsec ESP association */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate #define	IPSA_AALG_NONE	SADB_AALG_NONE		/* No auth. algorithm */
3457c478bd9Sstevel@tonic-gate #define	IPSA_AALG_MD5H	SADB_AALG_MD5HMAC	/* MD5-HMAC algorithm */
3467c478bd9Sstevel@tonic-gate #define	IPSA_AALG_SHA1H	SADB_AALG_SHA1HMAC	/* SHA1-HMAC algorithm */
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate #define	IPSA_EALG_NONE		SADB_EALG_NONE	/* No encryption algorithm */
3497c478bd9Sstevel@tonic-gate #define	IPSA_EALG_DES_CBC	SADB_EALG_DESCBC
3507c478bd9Sstevel@tonic-gate #define	IPSA_EALG_3DES		SADB_EALG_3DESCBC
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate  * Protect each ipsa_t bucket (and linkage) with a lock.
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate typedef struct isaf_s {
3577c478bd9Sstevel@tonic-gate 	ipsa_t *isaf_ipsa;
3587c478bd9Sstevel@tonic-gate 	kmutex_t isaf_lock;
3597c478bd9Sstevel@tonic-gate 	uint64_t isaf_gen;
3607c478bd9Sstevel@tonic-gate } isaf_t;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * ACQUIRE record.  If AH/ESP/whatever cannot find an association for outbound
3647c478bd9Sstevel@tonic-gate  * traffic, it sends up an SADB_ACQUIRE message and create an ACQUIRE record.
3657c478bd9Sstevel@tonic-gate  */
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate #define	IPSACQ_MAXPACKETS 4	/* Number of packets that can be queued up */
3687c478bd9Sstevel@tonic-gate 				/* waiting for an ACQUIRE to finish. */
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate typedef struct ipsacq_s {
3717c478bd9Sstevel@tonic-gate 	struct ipsacq_s *ipsacq_next;
3727c478bd9Sstevel@tonic-gate 	struct ipsacq_s **ipsacq_ptpn;
3737c478bd9Sstevel@tonic-gate 	kmutex_t *ipsacq_linklock;
3747c478bd9Sstevel@tonic-gate 	struct ipsec_policy_s  *ipsacq_policy;
3757c478bd9Sstevel@tonic-gate 	struct ipsec_action_s  *ipsacq_act;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	sa_family_t ipsacq_addrfam;	/* Address family. */
3787c478bd9Sstevel@tonic-gate 	int ipsacq_numpackets;		/* How many packets queued up so far. */
3797c478bd9Sstevel@tonic-gate 	uint32_t ipsacq_seq;		/* PF_KEY sequence number. */
3807c478bd9Sstevel@tonic-gate 	uint64_t ipsacq_unique_id;	/* Unique ID for SAs that need it. */
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	kmutex_t ipsacq_lock;	/* Protects non-linkage fields. */
3837c478bd9Sstevel@tonic-gate 	time_t ipsacq_expire;	/* Wall-clock time when this record expires. */
3847c478bd9Sstevel@tonic-gate 	mblk_t *ipsacq_mp;	/* List of datagrams waiting for an SA. */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/* These two point inside the last mblk inserted. */
3877c478bd9Sstevel@tonic-gate 	uint32_t *ipsacq_srcaddr;
3887c478bd9Sstevel@tonic-gate 	uint32_t *ipsacq_dstaddr;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	/* uint32_t ipsacq_proxysrc[IPSA_MAX_ADDRLEN]; */	/* For later */
3917c478bd9Sstevel@tonic-gate 	/* uint32_t ipsacq_proxydst[IPSA_MAX_ADDRLEN]; */	/* For later */
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/* These may change per-acquire. */
3947c478bd9Sstevel@tonic-gate 	uint16_t ipsacq_srcport;
3957c478bd9Sstevel@tonic-gate 	uint16_t ipsacq_dstport;
3967c478bd9Sstevel@tonic-gate 	uint8_t ipsacq_proto;
3977c478bd9Sstevel@tonic-gate 	/* icmp type and code of triggering packet (if applicable) */
3987c478bd9Sstevel@tonic-gate 	uint8_t	ipsacq_icmp_type;
3997c478bd9Sstevel@tonic-gate 	uint8_t ipsacq_icmp_code;
4007c478bd9Sstevel@tonic-gate } ipsacq_t;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * Kernel-generated sequence numbers will be no less than 0x80000000 to
4047c478bd9Sstevel@tonic-gate  * forestall any cretinous problems with manual keying accidentally updating
4057c478bd9Sstevel@tonic-gate  * an ACQUIRE entry.
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate #define	IACQF_LOWEST_SEQ 0x80000000
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate #define	SADB_AGE_INTERVAL_DEFAULT 1000
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate  * ACQUIRE fanout.  Protect each linkage with a lock.
4137c478bd9Sstevel@tonic-gate  */
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate typedef struct iacqf_s {
4167c478bd9Sstevel@tonic-gate 	ipsacq_t *iacqf_ipsacq;
4177c478bd9Sstevel@tonic-gate 	kmutex_t iacqf_lock;
4187c478bd9Sstevel@tonic-gate } iacqf_t;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate  * A (network protocol, ipsec protocol) specific SADB.
4227c478bd9Sstevel@tonic-gate  * (i.e., one each for {ah, esp} and {v4, v6}.
4237c478bd9Sstevel@tonic-gate  *
4247c478bd9Sstevel@tonic-gate  * Keep outbound assocs about the same as ire_cache entries for now.
4257c478bd9Sstevel@tonic-gate  * One danger point, multiple SAs for a single dest will clog a bucket.
4267c478bd9Sstevel@tonic-gate  * For the future, consider two-level hashing (2nd hash on IPC?), then probe.
4277c478bd9Sstevel@tonic-gate  */
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate typedef struct sadb_s
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	isaf_t	*sdb_of;
4327c478bd9Sstevel@tonic-gate 	isaf_t	*sdb_if;
4337c478bd9Sstevel@tonic-gate 	iacqf_t	*sdb_acq;
434fb87b5d2Ssommerfe 	int	sdb_hashsize;
4357c478bd9Sstevel@tonic-gate } sadb_t;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate  * A pair of SADB's (one for v4, one for v6), and related state (including
4397c478bd9Sstevel@tonic-gate  * acquire callbacks).
4407c478bd9Sstevel@tonic-gate  */
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate typedef struct sadbp_s
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	uint32_t	s_satype;
4457c478bd9Sstevel@tonic-gate 	queue_t		*s_ip_q;
4467c478bd9Sstevel@tonic-gate 	uint32_t	*s_acquire_timeout;
4477c478bd9Sstevel@tonic-gate 	void 		(*s_acqfn)(ipsacq_t *, mblk_t *);
4487c478bd9Sstevel@tonic-gate 	sadb_t		s_v4;
4497c478bd9Sstevel@tonic-gate 	sadb_t		s_v6;
4507c478bd9Sstevel@tonic-gate } sadbp_t;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * Global IPsec security association databases (and all that go with them).
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate extern sadbp_t ah_sadb, esp_sadb;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate /* Pointer to an all-zeroes IPv6 address. */
4587c478bd9Sstevel@tonic-gate #define	ALL_ZEROES_PTR	((uint32_t *)&ipv6_all_zeros)
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate /*
4617c478bd9Sstevel@tonic-gate  * Form unique id from ipsec_out_t
4627c478bd9Sstevel@tonic-gate  */
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate #define	SA_FORM_UNIQUE_ID(io)				\
4657c478bd9Sstevel@tonic-gate 	SA_UNIQUE_ID((io)->ipsec_out_src_port, (io)->ipsec_out_dst_port, \
4667c478bd9Sstevel@tonic-gate 		(io)->ipsec_out_proto)
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * This macro is used to generate unique ids (along with the addresses) for
4707c478bd9Sstevel@tonic-gate  * outbound datagrams that require unique SAs.
4717c478bd9Sstevel@tonic-gate  *
4727c478bd9Sstevel@tonic-gate  * N.B. casts and unsigned shift amounts discourage unwarranted
4737c478bd9Sstevel@tonic-gate  * sign extension of dstport and proto.
4747c478bd9Sstevel@tonic-gate  */
4757c478bd9Sstevel@tonic-gate #define	SA_UNIQUE_ID(srcport, dstport, proto) 		\
4767c478bd9Sstevel@tonic-gate 	((srcport) | ((uint64_t)(dstport) << 16U) | ((uint64_t)(proto) << 32U))
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate  * SA_UNIQUE_MASK generates a mask value to use when comparing the unique value
4807c478bd9Sstevel@tonic-gate  * from a packet to an SA.
4817c478bd9Sstevel@tonic-gate  */
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate #define	SA_UNIQUE_MASK(srcport, dstport, proto) 		\
4847c478bd9Sstevel@tonic-gate 	SA_UNIQUE_ID((srcport != 0)? 0xffff : 0,		\
4857c478bd9Sstevel@tonic-gate 		    (dstport != 0)? 0xffff : 0,			\
4867c478bd9Sstevel@tonic-gate 		    (proto != 0)? 0xff : 0)
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate  * Decompose unique id back into its original fields.
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate #define	SA_PROTO(ipsa) ((ipsa)->ipsa_unique_id>>32)&0xff
4927c478bd9Sstevel@tonic-gate #define	SA_SRCPORT(ipsa) ((ipsa)->ipsa_unique_id & 0xffff)
4937c478bd9Sstevel@tonic-gate #define	SA_DSTPORT(ipsa) (((ipsa)->ipsa_unique_id >> 16) & 0xffff)
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * All functions that return an ipsa_t will return it with IPSA_REFHOLD()
4977c478bd9Sstevel@tonic-gate  * already called.
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /* SA retrieval (inbound and outbound) */
5017c478bd9Sstevel@tonic-gate ipsa_t *ipsec_getassocbyspi(isaf_t *, uint32_t, uint32_t *, uint32_t *,
5027c478bd9Sstevel@tonic-gate     sa_family_t);
5037c478bd9Sstevel@tonic-gate ipsa_t *ipsec_getassocbyconn(isaf_t *, ipsec_out_t *, uint32_t *, uint32_t *,
5047c478bd9Sstevel@tonic-gate     sa_family_t, uint8_t);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate /* SA insertion. */
5077c478bd9Sstevel@tonic-gate int sadb_insertassoc(ipsa_t *, isaf_t *);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /* SA table construction and destruction. */
510fb87b5d2Ssommerfe void sadbp_init(const char *name, sadbp_t *, int, int);
5117c478bd9Sstevel@tonic-gate void sadbp_flush(sadbp_t *);
5127c478bd9Sstevel@tonic-gate void sadbp_destroy(sadbp_t *);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /* SA insertion and deletion. */
5157c478bd9Sstevel@tonic-gate int sadb_insertassoc(ipsa_t *, isaf_t *);
5167c478bd9Sstevel@tonic-gate void sadb_unlinkassoc(ipsa_t *);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate /* Support routines to interface a keysock consumer to PF_KEY. */
5197c478bd9Sstevel@tonic-gate mblk_t *sadb_keysock_out(minor_t);
5207c478bd9Sstevel@tonic-gate int sadb_hardsoftchk(sadb_lifetime_t *, sadb_lifetime_t *);
5217c478bd9Sstevel@tonic-gate void sadb_pfkey_echo(queue_t *, mblk_t *, sadb_msg_t *, struct keysock_in_s *,
5227c478bd9Sstevel@tonic-gate     ipsa_t *);
5237c478bd9Sstevel@tonic-gate void sadb_pfkey_error(queue_t *, mblk_t *, int, int, uint_t);
5247c478bd9Sstevel@tonic-gate void sadb_keysock_hello(queue_t **, queue_t *, mblk_t *, void (*)(void *),
5257c478bd9Sstevel@tonic-gate     timeout_id_t *, int);
5267c478bd9Sstevel@tonic-gate int sadb_addrcheck(queue_t *, queue_t *, mblk_t *, sadb_ext_t *, uint_t);
5277c478bd9Sstevel@tonic-gate void sadb_srcaddrfix(keysock_in_t *);
5287c478bd9Sstevel@tonic-gate int sadb_addrset(ire_t *);
5297c478bd9Sstevel@tonic-gate int sadb_delget_sa(mblk_t *, keysock_in_t *, sadbp_t *, int *, queue_t *,
5307c478bd9Sstevel@tonic-gate     boolean_t);
5317c478bd9Sstevel@tonic-gate #define	sadb_get_sa(m, k, s, i, q)	sadb_delget_sa(m, k, s, i, q, B_FALSE)
5327c478bd9Sstevel@tonic-gate #define	sadb_del_sa(m, k, s, i, q)	sadb_delget_sa(m, k, s, i, q, B_TRUE)
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate int sadb_purge_sa(mblk_t *, keysock_in_t *, sadb_t *, int *,
5357c478bd9Sstevel@tonic-gate     queue_t *, queue_t *);
5367c478bd9Sstevel@tonic-gate int sadb_common_add(queue_t *, queue_t *, mblk_t *, sadb_msg_t *,
5377c478bd9Sstevel@tonic-gate     keysock_in_t *, isaf_t *, isaf_t *, ipsa_t *, boolean_t, boolean_t);
5387c478bd9Sstevel@tonic-gate void sadb_set_usetime(ipsa_t *);
5397c478bd9Sstevel@tonic-gate boolean_t sadb_age_bytes(queue_t *, ipsa_t *, uint64_t, boolean_t);
5407c478bd9Sstevel@tonic-gate int sadb_update_sa(mblk_t *, keysock_in_t *, sadb_t *,
5417c478bd9Sstevel@tonic-gate     int *, queue_t *, int (*)(mblk_t *, keysock_in_t *, int *));
5427c478bd9Sstevel@tonic-gate void sadb_acquire(mblk_t *, ipsec_out_t *, boolean_t, boolean_t);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate void sadb_destroy_acquire(ipsacq_t *);
5457c478bd9Sstevel@tonic-gate uint8_t *sadb_setup_acquire(uint8_t *, uint8_t *, ipsacq_t *);
5467c478bd9Sstevel@tonic-gate ipsa_t *sadb_getspi(keysock_in_t *, uint32_t, int *);
5477c478bd9Sstevel@tonic-gate void sadb_in_acquire(sadb_msg_t *, sadbp_t *, queue_t *);
5487c478bd9Sstevel@tonic-gate boolean_t sadb_replay_check(ipsa_t *, uint32_t);
5497c478bd9Sstevel@tonic-gate boolean_t sadb_replay_peek(ipsa_t *, uint32_t);
5507c478bd9Sstevel@tonic-gate mblk_t *sadb_sa2msg(ipsa_t *, sadb_msg_t *);
5517c478bd9Sstevel@tonic-gate int sadb_dump(queue_t *, mblk_t *, minor_t, sadb_t *);
5527c478bd9Sstevel@tonic-gate void sadb_replay_delete(ipsa_t *);
5537c478bd9Sstevel@tonic-gate void sadb_ager(sadb_t *, queue_t *, queue_t *, int);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate timeout_id_t sadb_retimeout(hrtime_t, queue_t *, void (*)(void *),
5567c478bd9Sstevel@tonic-gate     uint_t *, uint_t, short);
5577c478bd9Sstevel@tonic-gate void sadb_sa_refrele(void *target);
5587c478bd9Sstevel@tonic-gate void sadb_set_lpkt(ipsa_t *, mblk_t *);
5597c478bd9Sstevel@tonic-gate mblk_t *sadb_clear_lpkt(ipsa_t *);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate  * Hw accel-related calls (downloading sadb to driver)
5637c478bd9Sstevel@tonic-gate  */
5647c478bd9Sstevel@tonic-gate void sadb_ill_download(ill_t *, uint_t);
5657c478bd9Sstevel@tonic-gate mblk_t *sadb_fmt_sa_req(uint_t, uint_t, ipsa_t *, boolean_t);
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * Sub-set of the IPsec hardware acceleration capabilities functions
5687c478bd9Sstevel@tonic-gate  * implemented by ip_if.c
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate extern	boolean_t ipsec_capab_match(ill_t *, uint_t, boolean_t, ipsa_t *);
5717c478bd9Sstevel@tonic-gate extern	void	ill_ipsec_capab_send_all(uint_t, mblk_t *, ipsa_t *);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate  * One IPsec -> IP linking routine, and two IPsec rate-limiting routines.
5767c478bd9Sstevel@tonic-gate  */
5777c478bd9Sstevel@tonic-gate extern boolean_t sadb_t_bind_req(queue_t *, int);
5787c478bd9Sstevel@tonic-gate /*PRINTFLIKE5*/
5797c478bd9Sstevel@tonic-gate extern void ipsec_rl_strlog(short, short, char, ushort_t, char *, ...)
5807c478bd9Sstevel@tonic-gate     __KPRINTFLIKE(5);
5817c478bd9Sstevel@tonic-gate extern void ipsec_assocfailure(short, short, char, ushort_t, char *, uint32_t,
5827c478bd9Sstevel@tonic-gate     void *, int);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate  * Algorithm types.
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate #define	IPSEC_NALGTYPES 	2
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate typedef enum ipsec_algtype {
5917c478bd9Sstevel@tonic-gate 	IPSEC_ALG_AUTH = 0,
5927c478bd9Sstevel@tonic-gate 	IPSEC_ALG_ENCR = 1
5937c478bd9Sstevel@tonic-gate } ipsec_algtype_t;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate /*
5967c478bd9Sstevel@tonic-gate  * Definitions as per IPsec/ISAKMP DOI.
5977c478bd9Sstevel@tonic-gate  */
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate #define	IPSEC_MAX_ALGS		256
6007c478bd9Sstevel@tonic-gate #define	PROTO_IPSEC_AH		2
6017c478bd9Sstevel@tonic-gate #define	PROTO_IPSEC_ESP		3
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate /*
6047c478bd9Sstevel@tonic-gate  * Common algorithm info.
6057c478bd9Sstevel@tonic-gate  */
6067c478bd9Sstevel@tonic-gate typedef struct ipsec_alginfo
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	uint8_t		alg_id;
6097c478bd9Sstevel@tonic-gate 	uint8_t		alg_flags;
6107c478bd9Sstevel@tonic-gate 	uint16_t	*alg_key_sizes;
6117c478bd9Sstevel@tonic-gate 	uint16_t	*alg_block_sizes;
6127c478bd9Sstevel@tonic-gate 	uint16_t	alg_nkey_sizes;
6137c478bd9Sstevel@tonic-gate 	uint16_t	alg_nblock_sizes;
6147c478bd9Sstevel@tonic-gate 	uint16_t	alg_minbits;
6157c478bd9Sstevel@tonic-gate 	uint16_t	alg_maxbits;
6167c478bd9Sstevel@tonic-gate 	uint16_t	alg_datalen;
6177c478bd9Sstevel@tonic-gate 	/*
6187c478bd9Sstevel@tonic-gate 	 * increment: number of bits from keysize to keysize
6197c478bd9Sstevel@tonic-gate 	 * default: # of increments from min to default key len
6207c478bd9Sstevel@tonic-gate 	 */
6217c478bd9Sstevel@tonic-gate 	uint16_t	alg_increment;
6227c478bd9Sstevel@tonic-gate 	uint16_t	alg_default;
6237c478bd9Sstevel@tonic-gate 	uint16_t	alg_default_bits;
6247c478bd9Sstevel@tonic-gate 	/*
6257c478bd9Sstevel@tonic-gate 	 * Min, max, and default key sizes effectively supported
6267c478bd9Sstevel@tonic-gate 	 * by the encryption framework.
6277c478bd9Sstevel@tonic-gate 	 */
6287c478bd9Sstevel@tonic-gate 	uint16_t	alg_ef_minbits;
6297c478bd9Sstevel@tonic-gate 	uint16_t	alg_ef_maxbits;
6307c478bd9Sstevel@tonic-gate 	uint16_t	alg_ef_default;
6317c478bd9Sstevel@tonic-gate 	uint16_t	alg_ef_default_bits;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	crypto_mech_type_t alg_mech_type;	/* KCF mechanism type */
6347c478bd9Sstevel@tonic-gate 	crypto_mech_name_t alg_mech_name;	/* KCF mechanism name */
6357c478bd9Sstevel@tonic-gate } ipsec_alginfo_t;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate #define	alg_datalen alg_block_sizes[0]
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate #define	ALG_FLAG_VALID	0x01
6407c478bd9Sstevel@tonic-gate #define	ALG_VALID(_alg)	((_alg)->alg_flags & ALG_FLAG_VALID)
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate  * Software crypto execution mode.
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate typedef enum {
6467c478bd9Sstevel@tonic-gate 	IPSEC_ALGS_EXEC_SYNC = 0,
6477c478bd9Sstevel@tonic-gate 	IPSEC_ALGS_EXEC_ASYNC = 1
6487c478bd9Sstevel@tonic-gate } ipsec_algs_exec_mode_t;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate extern uint8_t ipsec_nalgs[IPSEC_NALGTYPES];
6517c478bd9Sstevel@tonic-gate extern ipsec_alginfo_t *ipsec_alglists[IPSEC_NALGTYPES][IPSEC_MAX_ALGS];
6527c478bd9Sstevel@tonic-gate extern uint8_t ipsec_sortlist[IPSEC_NALGTYPES][IPSEC_MAX_ALGS];
6537c478bd9Sstevel@tonic-gate extern ipsec_algs_exec_mode_t ipsec_algs_exec_mode[IPSEC_NALGTYPES];
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate extern kmutex_t alg_lock;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate extern void ipsec_alg_reg(ipsec_algtype_t, ipsec_alginfo_t *);
6587c478bd9Sstevel@tonic-gate extern void ipsec_alg_unreg(ipsec_algtype_t, uint8_t);
6597c478bd9Sstevel@tonic-gate extern void ipsec_alg_fix_min_max(ipsec_alginfo_t *, ipsec_algtype_t);
6607c478bd9Sstevel@tonic-gate extern void ipsec_alg_free(ipsec_alginfo_t *);
6617c478bd9Sstevel@tonic-gate extern void ipsec_register_prov_update(void);
6627c478bd9Sstevel@tonic-gate extern void sadb_alg_update(ipsec_algtype_t, uint8_t, boolean_t);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate  * Context templates management.
6667c478bd9Sstevel@tonic-gate  */
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate #define	IPSEC_CTX_TMPL_ALLOC ((crypto_ctx_template_t)-1)
6697c478bd9Sstevel@tonic-gate #define	IPSEC_CTX_TMPL(_sa, _which, _type, _tmpl) {			\
6707c478bd9Sstevel@tonic-gate 	if ((_tmpl = (_sa)->_which) == IPSEC_CTX_TMPL_ALLOC) {		\
6717c478bd9Sstevel@tonic-gate 		mutex_enter(&assoc->ipsa_lock);				\
6727c478bd9Sstevel@tonic-gate 		if ((_sa)->_which == IPSEC_CTX_TMPL_ALLOC) {		\
6737c478bd9Sstevel@tonic-gate 			mutex_enter(&alg_lock);				\
6747c478bd9Sstevel@tonic-gate 			(void) ipsec_create_ctx_tmpl(_sa, _type);	\
6757c478bd9Sstevel@tonic-gate 			mutex_exit(&alg_lock);				\
6767c478bd9Sstevel@tonic-gate 		}							\
6777c478bd9Sstevel@tonic-gate 		mutex_exit(&assoc->ipsa_lock);				\
6787c478bd9Sstevel@tonic-gate 		if ((_tmpl = (_sa)->_which) == IPSEC_CTX_TMPL_ALLOC)	\
6797c478bd9Sstevel@tonic-gate 			_tmpl = NULL;					\
6807c478bd9Sstevel@tonic-gate 	}								\
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate extern int ipsec_create_ctx_tmpl(ipsa_t *, ipsec_algtype_t);
6847c478bd9Sstevel@tonic-gate extern void ipsec_destroy_ctx_tmpl(ipsa_t *, ipsec_algtype_t);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /* key checking */
6877c478bd9Sstevel@tonic-gate extern int ipsec_check_key(crypto_mech_type_t, sadb_key_t *, boolean_t, int *);
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate /* natt cleanup */
6907c478bd9Sstevel@tonic-gate extern void sadb_clear_timeouts(queue_t *);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate typedef struct {
6937c478bd9Sstevel@tonic-gate 	kstat_named_t esp_stat_in_requests;
6947c478bd9Sstevel@tonic-gate 	kstat_named_t esp_stat_in_discards;
6957c478bd9Sstevel@tonic-gate 	kstat_named_t esp_stat_lookup_failure;
6967c478bd9Sstevel@tonic-gate 	kstat_named_t ah_stat_in_requests;
6977c478bd9Sstevel@tonic-gate 	kstat_named_t ah_stat_in_discards;
6987c478bd9Sstevel@tonic-gate 	kstat_named_t ah_stat_lookup_failure;
6997c478bd9Sstevel@tonic-gate } ipsec_kstats_t;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate extern ipsec_kstats_t *ipsec_kstats;
7027c478bd9Sstevel@tonic-gate extern void ipsec_kstat_init(void);
7037c478bd9Sstevel@tonic-gate extern void ipsec_kstat_destroy(void);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate #define	IP_ESP_BUMP_STAT(x) (ipsec_kstats->esp_stat_ ## x).value.ui64++
7067c478bd9Sstevel@tonic-gate #define	IP_AH_BUMP_STAT(x) (ipsec_kstats->ah_stat_ ## x).value.ui64++
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate #endif
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate #endif /* _INET_SADB_H */
713