xref: /titanic_52/usr/src/uts/common/inet/ipsec_impl.h (revision 1a5e258f5471356ca102c7176637cdce45bac147)
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 /*
222b24ab6bSSebastien Roy  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #ifndef _INET_IPSEC_IMPL_H
277c478bd9Sstevel@tonic-gate #define	_INET_IPSEC_IMPL_H
287c478bd9Sstevel@tonic-gate 
29f4b3ec61Sdh155122 #include <inet/ip.h>
30f4b3ec61Sdh155122 #include <inet/ipdrop.h>
31f4b3ec61Sdh155122 
327c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
337c478bd9Sstevel@tonic-gate extern "C" {
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_SRC_ADDRESS	0	/* Source Address */
377c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_SRC_PORT		1	/* Source Port */
387c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_DST_ADDRESS	2	/* Dest Address */
397c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_DST_PORT		3	/* Dest Port */
407c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_SRC_MASK		4	/* Source Address Mask */
417c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_DST_MASK		5	/* Destination Address Mask */
427c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_ULP			6	/* Upper layer Port */
437c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_IPSEC_PROT	7	/* AH or ESP or AH_ESP */
447c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_IPSEC_AALGS	8	/* Auth Algorithms - MD5 etc. */
457c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_IPSEC_EALGS	9	/* Encr Algorithms - DES etc. */
467c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_IPSEC_EAALGS	10	/* Encr Algorithms - MD5 etc. */
477c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_IPSEC_SA		11	/* Shared or unique SA */
487c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_IPSEC_DIR 		12	/* Direction of traffic */
497c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_ICMP_TYPE 		13	/* ICMP type */
507c478bd9Sstevel@tonic-gate #define	IPSEC_CONF_ICMP_CODE 		14	/* ICMP code */
518810c16bSdanmcd #define	IPSEC_CONF_NEGOTIATE		15	/* Negotiation */
528810c16bSdanmcd #define	IPSEC_CONF_TUNNEL		16	/* Tunnel */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* Type of an entry */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	IPSEC_NTYPES			0x02
577c478bd9Sstevel@tonic-gate #define	IPSEC_TYPE_OUTBOUND		0x00
587c478bd9Sstevel@tonic-gate #define	IPSEC_TYPE_INBOUND		0x01
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* Policy */
617c478bd9Sstevel@tonic-gate #define	IPSEC_POLICY_APPLY	0x01
627c478bd9Sstevel@tonic-gate #define	IPSEC_POLICY_DISCARD	0x02
637c478bd9Sstevel@tonic-gate #define	IPSEC_POLICY_BYPASS	0x03
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* Shared or unique SA */
667c478bd9Sstevel@tonic-gate #define	IPSEC_SHARED_SA		0x01
677c478bd9Sstevel@tonic-gate #define	IPSEC_UNIQUE_SA		0x02
687c478bd9Sstevel@tonic-gate 
69437220cdSdanmcd /* IPsec protocols and combinations */
707c478bd9Sstevel@tonic-gate #define	IPSEC_AH_ONLY		0x01
717c478bd9Sstevel@tonic-gate #define	IPSEC_ESP_ONLY		0x02
727c478bd9Sstevel@tonic-gate #define	IPSEC_AH_ESP		0x03
737c478bd9Sstevel@tonic-gate 
74d2f8a3dfSpwernau /*
75d2f8a3dfSpwernau  * Internally defined "any" algorithm.
76d2f8a3dfSpwernau  * Move to PF_KEY v3 when that RFC is released.
77d2f8a3dfSpwernau  */
78d2f8a3dfSpwernau #define	SADB_AALG_ANY 255
79d2f8a3dfSpwernau 
807c478bd9Sstevel@tonic-gate #ifdef _KERNEL
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #include <inet/common.h>
837c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
847c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
857c478bd9Sstevel@tonic-gate #include <net/pfkeyv2.h>
867c478bd9Sstevel@tonic-gate #include <inet/ip.h>
877c478bd9Sstevel@tonic-gate #include <inet/sadb.h>
887c478bd9Sstevel@tonic-gate #include <inet/ipsecah.h>
897c478bd9Sstevel@tonic-gate #include <inet/ipsecesp.h>
907c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h>
917c478bd9Sstevel@tonic-gate #include <sys/crypto/api.h>
926a182920Ssommerfe #include <sys/avl.h>
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Maximum number of authentication algorithms (can be indexed by one byte
967c478bd9Sstevel@tonic-gate  * per PF_KEY and the IKE IPsec DOI.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate #define	MAX_AALGS 256
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * IPsec task queue constants.
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate #define	IPSEC_TASKQ_MIN 10
1047c478bd9Sstevel@tonic-gate #define	IPSEC_TASKQ_MAX 20
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * So we can access IPsec global variables that live in keysock.c.
1087c478bd9Sstevel@tonic-gate  */
109f4b3ec61Sdh155122 extern boolean_t keysock_extended_reg(netstack_t *);
110f4b3ec61Sdh155122 extern uint32_t keysock_next_seq(netstack_t *);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * Locking for ipsec policy rules:
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  * policy heads: system policy is static; per-conn polheads are dynamic,
1167c478bd9Sstevel@tonic-gate  * and refcounted (and inherited); use atomic refcounts and "don't let
1177c478bd9Sstevel@tonic-gate  * go with both hands".
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * policy: refcounted; references from polhead, ipsec_out
1207c478bd9Sstevel@tonic-gate  *
1217c478bd9Sstevel@tonic-gate  * actions: refcounted; referenced from: action hash table, policy, ipsec_out
1227c478bd9Sstevel@tonic-gate  * selectors: refcounted; referenced from: selector hash table, policy.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * the following are inspired by, but not directly based on,
1277c478bd9Sstevel@tonic-gate  * some of the sys/queue.h type-safe pseudo-polymorphic macros
1287c478bd9Sstevel@tonic-gate  * found in BSD.
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  * XXX If we use these more generally, we'll have to make the names
1317c478bd9Sstevel@tonic-gate  * less generic (HASH_* will probably clobber other namespaces).
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #define	HASH_LOCK(table, hash) \
1357c478bd9Sstevel@tonic-gate 	mutex_enter(&(table)[hash].hash_lock)
1367c478bd9Sstevel@tonic-gate #define	HASH_UNLOCK(table, hash) \
1377c478bd9Sstevel@tonic-gate 	mutex_exit(&(table)[hash].hash_lock)
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate #define	HASH_LOCKED(table, hash) \
1407c478bd9Sstevel@tonic-gate 	MUTEX_HELD(&(table)[hash].hash_lock)
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #define	HASH_ITERATE(var, field, table, hash) 		\
1437c478bd9Sstevel@tonic-gate 	var = table[hash].hash_head; var != NULL; var = var->field.hash_next
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate #define	HASH_NEXT(var, field) 		\
1467c478bd9Sstevel@tonic-gate 	(var)->field.hash_next
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate #define	HASH_INSERT(var, field, table, hash)			\
1497c478bd9Sstevel@tonic-gate {								\
1507c478bd9Sstevel@tonic-gate 	ASSERT(HASH_LOCKED(table, hash));			\
1517c478bd9Sstevel@tonic-gate 	(var)->field.hash_next = (table)[hash].hash_head;	\
1527c478bd9Sstevel@tonic-gate 	(var)->field.hash_pp = &(table)[hash].hash_head;	\
1537c478bd9Sstevel@tonic-gate 	(table)[hash].hash_head = var;				\
1547c478bd9Sstevel@tonic-gate 	if ((var)->field.hash_next != NULL)			\
1557c478bd9Sstevel@tonic-gate 		(var)->field.hash_next->field.hash_pp = 	\
1567c478bd9Sstevel@tonic-gate 			&((var)->field.hash_next); 		\
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1596a182920Ssommerfe 
1607c478bd9Sstevel@tonic-gate #define	HASH_UNCHAIN(var, field, table, hash)			\
1617c478bd9Sstevel@tonic-gate {								\
1627c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(table)[hash].hash_lock));		\
1636a182920Ssommerfe 	HASHLIST_UNCHAIN(var, field);				\
1646a182920Ssommerfe }
1656a182920Ssommerfe 
1666a182920Ssommerfe #define	HASHLIST_INSERT(var, field, head)			\
1676a182920Ssommerfe {								\
1686a182920Ssommerfe 	(var)->field.hash_next = head;				\
1696a182920Ssommerfe 	(var)->field.hash_pp = &(head);				\
1706a182920Ssommerfe 	head = var;						\
1716a182920Ssommerfe 	if ((var)->field.hash_next != NULL)			\
1726a182920Ssommerfe 		(var)->field.hash_next->field.hash_pp = 	\
1736a182920Ssommerfe 			&((var)->field.hash_next); 		\
1746a182920Ssommerfe }
1756a182920Ssommerfe 
1766a182920Ssommerfe #define	HASHLIST_UNCHAIN(var, field) 				\
1776a182920Ssommerfe {								\
1787c478bd9Sstevel@tonic-gate 	*var->field.hash_pp = var->field.hash_next;		\
1797c478bd9Sstevel@tonic-gate 	if (var->field.hash_next)				\
1807c478bd9Sstevel@tonic-gate 		var->field.hash_next->field.hash_pp = 		\
1817c478bd9Sstevel@tonic-gate 			var->field.hash_pp;			\
1826a182920Ssommerfe 	HASH_NULL(var, field);					\
1836a182920Ssommerfe }
1846a182920Ssommerfe 
1856a182920Ssommerfe 
1866a182920Ssommerfe #define	HASH_NULL(var, field) 					\
1876a182920Ssommerfe {								\
1887c478bd9Sstevel@tonic-gate 	var->field.hash_next = NULL;				\
1897c478bd9Sstevel@tonic-gate 	var->field.hash_pp = NULL;				\
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate #define	HASH_LINK(fieldname, type)				\
1937c478bd9Sstevel@tonic-gate 	struct {						\
1947c478bd9Sstevel@tonic-gate 		type *hash_next;				\
1957c478bd9Sstevel@tonic-gate 		type **hash_pp;					\
1967c478bd9Sstevel@tonic-gate 	} fieldname
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate #define	HASH_HEAD(tag)						\
2007c478bd9Sstevel@tonic-gate 	struct {						\
2017c478bd9Sstevel@tonic-gate 		struct tag *hash_head;				\
2027c478bd9Sstevel@tonic-gate 		kmutex_t hash_lock;				\
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
205f4b3ec61Sdh155122 
2067c478bd9Sstevel@tonic-gate typedef struct ipsec_policy_s ipsec_policy_t;
2077c478bd9Sstevel@tonic-gate 
2086a182920Ssommerfe typedef HASH_HEAD(ipsec_policy_s) ipsec_policy_hash_t;
2096a182920Ssommerfe 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * When adding new fields to ipsec_prot_t, make sure to update
2127c478bd9Sstevel@tonic-gate  * ipsec_in_to_out_action() as well as other code in spd.c
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate typedef struct ipsec_prot
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	unsigned int
2187c478bd9Sstevel@tonic-gate 		ipp_use_ah : 1,
2197c478bd9Sstevel@tonic-gate 		ipp_use_esp : 1,
2207c478bd9Sstevel@tonic-gate 		ipp_use_se : 1,
2217c478bd9Sstevel@tonic-gate 		ipp_use_unique : 1,
2227c478bd9Sstevel@tonic-gate 		ipp_use_espa : 1,
2237c478bd9Sstevel@tonic-gate 		ipp_pad : 27;
2247c478bd9Sstevel@tonic-gate 	uint8_t		ipp_auth_alg;		 /* DOI number */
2257c478bd9Sstevel@tonic-gate 	uint8_t		ipp_encr_alg;		 /* DOI number */
2267c478bd9Sstevel@tonic-gate 	uint8_t		ipp_esp_auth_alg;	 /* DOI number */
2277c478bd9Sstevel@tonic-gate 	uint16_t 	ipp_ah_minbits;		 /* AH: min keylen */
2287c478bd9Sstevel@tonic-gate 	uint16_t 	ipp_ah_maxbits;		 /* AH: max keylen */
2297c478bd9Sstevel@tonic-gate 	uint16_t	ipp_espe_minbits;	 /* ESP encr: min keylen */
2307c478bd9Sstevel@tonic-gate 	uint16_t	ipp_espe_maxbits;	 /* ESP encr: max keylen */
2317c478bd9Sstevel@tonic-gate 	uint16_t	ipp_espa_minbits;	 /* ESP auth: min keylen */
2327c478bd9Sstevel@tonic-gate 	uint16_t	ipp_espa_maxbits;	 /* ESP auth: max keylen */
2337c478bd9Sstevel@tonic-gate 	uint32_t	ipp_km_proto;		 /* key mgmt protocol */
2347c478bd9Sstevel@tonic-gate 	uint32_t	ipp_km_cookie;		 /* key mgmt cookie */
2357c478bd9Sstevel@tonic-gate 	uint32_t	ipp_replay_depth;	 /* replay window */
2367c478bd9Sstevel@tonic-gate 	/* XXX add lifetimes */
2377c478bd9Sstevel@tonic-gate } ipsec_prot_t;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate #define	IPSEC_MAX_KEYBITS (0xffff)
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  * An individual policy action, possibly a member of a chain.
2437c478bd9Sstevel@tonic-gate  *
2446a182920Ssommerfe  * Action chains may be shared between multiple policy rules.
2457c478bd9Sstevel@tonic-gate  *
2467c478bd9Sstevel@tonic-gate  * With one exception (IPSEC_POLICY_LOG), a chain consists of an
2477c478bd9Sstevel@tonic-gate  * ordered list of alternative ways to handle a packet.
2487c478bd9Sstevel@tonic-gate  *
2497c478bd9Sstevel@tonic-gate  * All actions are also "interned" into a hash table (to allow
2507c478bd9Sstevel@tonic-gate  * multiple rules with the same action chain to share one copy in
2517c478bd9Sstevel@tonic-gate  * memory).
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate typedef struct ipsec_act
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	uint8_t		ipa_type;
2577c478bd9Sstevel@tonic-gate 	uint8_t		ipa_log;
2587c478bd9Sstevel@tonic-gate 	union
2597c478bd9Sstevel@tonic-gate 	{
2607c478bd9Sstevel@tonic-gate 		ipsec_prot_t	ipau_apply;
2617c478bd9Sstevel@tonic-gate 		uint8_t		ipau_reject_type;
2627c478bd9Sstevel@tonic-gate 		uint32_t	ipau_resolve_id; /* magic cookie */
2637c478bd9Sstevel@tonic-gate 		uint8_t		ipau_log_type;
2647c478bd9Sstevel@tonic-gate 	} ipa_u;
2657c478bd9Sstevel@tonic-gate #define	ipa_apply ipa_u.ipau_apply
2667c478bd9Sstevel@tonic-gate #define	ipa_reject_type ipa_u.ipau_reject_type
2677c478bd9Sstevel@tonic-gate #define	ipa_log_type ipa_u.ipau_log_type
2687c478bd9Sstevel@tonic-gate #define	ipa_resolve_type ipa_u.ipau_resolve_type
2697c478bd9Sstevel@tonic-gate } ipsec_act_t;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate #define	IPSEC_ACT_APPLY		0x01 /* match IPSEC_POLICY_APPLY */
2727c478bd9Sstevel@tonic-gate #define	IPSEC_ACT_DISCARD	0x02 /* match IPSEC_POLICY_DISCARD */
2737c478bd9Sstevel@tonic-gate #define	IPSEC_ACT_BYPASS	0x03 /* match IPSEC_POLICY_BYPASS */
2747c478bd9Sstevel@tonic-gate #define	IPSEC_ACT_REJECT	0x04
2757c478bd9Sstevel@tonic-gate #define	IPSEC_ACT_CLEAR		0x05
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate typedef struct ipsec_action_s
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate 	HASH_LINK(ipa_hash, struct ipsec_action_s);
2807c478bd9Sstevel@tonic-gate 	struct ipsec_action_s	*ipa_next;	/* next alternative */
2817c478bd9Sstevel@tonic-gate 	uint32_t		ipa_refs;		/* refcount */
2827c478bd9Sstevel@tonic-gate 	ipsec_act_t		ipa_act;
2837c478bd9Sstevel@tonic-gate 	/*
2847c478bd9Sstevel@tonic-gate 	 * The following bits are equivalent to an OR of bits included in the
2857c478bd9Sstevel@tonic-gate 	 * ipau_apply fields of this and subsequent actions in an
2867c478bd9Sstevel@tonic-gate 	 * action chain; this is an optimization for the sake of
2877c478bd9Sstevel@tonic-gate 	 * ipsec_out_process() in ip.c and a few other places.
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 	unsigned int
2907c478bd9Sstevel@tonic-gate 		ipa_hval: 8,
2917c478bd9Sstevel@tonic-gate 		ipa_allow_clear:1,		/* rule allows cleartext? */
2927c478bd9Sstevel@tonic-gate 		ipa_want_ah:1,			/* an action wants ah */
2937c478bd9Sstevel@tonic-gate 		ipa_want_esp:1,			/* an action wants esp */
2947c478bd9Sstevel@tonic-gate 		ipa_want_se:1,			/* an action wants se */
2957c478bd9Sstevel@tonic-gate 		ipa_want_unique:1,		/* want unique sa's */
2967c478bd9Sstevel@tonic-gate 		ipa_pad:19;
2977c478bd9Sstevel@tonic-gate 	uint32_t		ipa_ovhd;	/* per-packet encap ovhd */
2987c478bd9Sstevel@tonic-gate } ipsec_action_t;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate #define	IPACT_REFHOLD(ipa) {			\
301*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&(ipa)->ipa_refs);	\
3027c478bd9Sstevel@tonic-gate 	ASSERT((ipa)->ipa_refs != 0);	\
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate #define	IPACT_REFRELE(ipa) {					\
3057c478bd9Sstevel@tonic-gate 	ASSERT((ipa)->ipa_refs != 0);				\
3067c478bd9Sstevel@tonic-gate 	membar_exit();						\
307*1a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&(ipa)->ipa_refs) == 0)	\
3087c478bd9Sstevel@tonic-gate 		ipsec_action_free(ipa);				\
3097c478bd9Sstevel@tonic-gate 	(ipa) = 0;						\
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
313f4b3ec61Sdh155122  * For now, use a trivially sized hash table for actions.
314f4b3ec61Sdh155122  * In the future we can add the structure canonicalization necessary
315f4b3ec61Sdh155122  * to get the hash function to behave correctly..
316f4b3ec61Sdh155122  */
317f4b3ec61Sdh155122 #define	IPSEC_ACTION_HASH_SIZE 1
318f4b3ec61Sdh155122 
319f4b3ec61Sdh155122 /*
3208810c16bSdanmcd  * Merged address structure, for cheezy address-family independent
3217c478bd9Sstevel@tonic-gate  * matches in policy code.
3227c478bd9Sstevel@tonic-gate  */
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate typedef union ipsec_addr
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	in6_addr_t	ipsad_v6;
3277c478bd9Sstevel@tonic-gate 	in_addr_t	ipsad_v4;
3287c478bd9Sstevel@tonic-gate } ipsec_addr_t;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  * ipsec selector set, as used by the kernel policy structures.
3327c478bd9Sstevel@tonic-gate  * Note that that we specify "local" and "remote"
3337c478bd9Sstevel@tonic-gate  * rather than "source" and "destination", which allows the selectors
3347c478bd9Sstevel@tonic-gate  * for symmetric policy rules to be shared between inbound and
3357c478bd9Sstevel@tonic-gate  * outbound rules.
3367c478bd9Sstevel@tonic-gate  *
3377c478bd9Sstevel@tonic-gate  * "local" means "destination" on inbound, and "source" on outbound.
3387c478bd9Sstevel@tonic-gate  * "remote" means "source" on inbound, and "destination" on outbound.
3397c478bd9Sstevel@tonic-gate  * XXX if we add a fifth policy enforcement point for forwarded packets,
3407c478bd9Sstevel@tonic-gate  * what do we do?
3417c478bd9Sstevel@tonic-gate  *
3427c478bd9Sstevel@tonic-gate  * The ipsl_valid mask is not done as a bitfield; this is so we
3437c478bd9Sstevel@tonic-gate  * can use "ffs()" to find the "most interesting" valid tag.
3447c478bd9Sstevel@tonic-gate  *
3457c478bd9Sstevel@tonic-gate  * XXX should we have multiple types for space-conservation reasons?
3467c478bd9Sstevel@tonic-gate  * (v4 vs v6?  prefix vs. range)?
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate typedef struct ipsec_selkey
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	uint32_t	ipsl_valid;		/* bitmask of valid entries */
3527c478bd9Sstevel@tonic-gate #define	IPSL_REMOTE_ADDR		0x00000001
3537c478bd9Sstevel@tonic-gate #define	IPSL_LOCAL_ADDR			0x00000002
3547c478bd9Sstevel@tonic-gate #define	IPSL_REMOTE_PORT		0x00000004
3557c478bd9Sstevel@tonic-gate #define	IPSL_LOCAL_PORT			0x00000008
3567c478bd9Sstevel@tonic-gate #define	IPSL_PROTOCOL			0x00000010
3577c478bd9Sstevel@tonic-gate #define	IPSL_ICMP_TYPE			0x00000020
3587c478bd9Sstevel@tonic-gate #define	IPSL_ICMP_CODE			0x00000040
3597c478bd9Sstevel@tonic-gate #define	IPSL_IPV6			0x00000080
3607c478bd9Sstevel@tonic-gate #define	IPSL_IPV4			0x00000100
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate #define	IPSL_WILDCARD			0x0000007f
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	ipsec_addr_t	ipsl_local;
3657c478bd9Sstevel@tonic-gate 	ipsec_addr_t	ipsl_remote;
3667c478bd9Sstevel@tonic-gate 	uint16_t	ipsl_lport;
3677c478bd9Sstevel@tonic-gate 	uint16_t	ipsl_rport;
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * ICMP type and code selectors. Both have an end value to
3707c478bd9Sstevel@tonic-gate 	 * specify ranges, or * and *_end are equal for a single
3717c478bd9Sstevel@tonic-gate 	 * value
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 	uint8_t		ipsl_icmp_type;
3747c478bd9Sstevel@tonic-gate 	uint8_t		ipsl_icmp_type_end;
3757c478bd9Sstevel@tonic-gate 	uint8_t		ipsl_icmp_code;
3767c478bd9Sstevel@tonic-gate 	uint8_t		ipsl_icmp_code_end;
3776a182920Ssommerfe 
3787c478bd9Sstevel@tonic-gate 	uint8_t		ipsl_proto;		/* ip payload type */
3797c478bd9Sstevel@tonic-gate 	uint8_t		ipsl_local_pfxlen;	/* #bits of prefix */
3807c478bd9Sstevel@tonic-gate 	uint8_t		ipsl_remote_pfxlen;	/* #bits of prefix */
3817c478bd9Sstevel@tonic-gate 	uint8_t		ipsl_mbz;
3827c478bd9Sstevel@tonic-gate 
3838810c16bSdanmcd 	/* Insert new elements above this line */
3848810c16bSdanmcd 	uint32_t	ipsl_pol_hval;
3858810c16bSdanmcd 	uint32_t	ipsl_sel_hval;
3866a182920Ssommerfe } ipsec_selkey_t;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate typedef struct ipsec_sel
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	HASH_LINK(ipsl_hash, struct ipsec_sel);
3917c478bd9Sstevel@tonic-gate 	uint32_t	ipsl_refs;		/* # refs to this sel */
3927c478bd9Sstevel@tonic-gate 	ipsec_selkey_t	ipsl_key;		/* actual selector guts */
3937c478bd9Sstevel@tonic-gate } ipsec_sel_t;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate /*
3966a182920Ssommerfe  * One policy rule.  This will be linked into a single hash chain bucket in
3976a182920Ssommerfe  * the parent rule structure.  If the selector is simple enough to
3986a182920Ssommerfe  * allow hashing, it gets filed under ipsec_policy_root_t->ipr_hash.
3996a182920Ssommerfe  * Otherwise it goes onto a linked list in ipsec_policy_root_t->ipr_nonhash[af]
4007c478bd9Sstevel@tonic-gate  *
4016a182920Ssommerfe  * In addition, we file the rule into an avl tree keyed by the rule index.
4026a182920Ssommerfe  * (Duplicate rules are permitted; the comparison function breaks ties).
4037c478bd9Sstevel@tonic-gate  */
4047c478bd9Sstevel@tonic-gate struct ipsec_policy_s
4057c478bd9Sstevel@tonic-gate {
4066a182920Ssommerfe 	HASH_LINK(ipsp_hash, struct ipsec_policy_s);
4076a182920Ssommerfe 	avl_node_t		ipsp_byid;
4087c478bd9Sstevel@tonic-gate 	uint64_t		ipsp_index;	/* unique id */
4097c478bd9Sstevel@tonic-gate 	uint32_t		ipsp_prio; 	/* rule priority */
4107c478bd9Sstevel@tonic-gate 	uint32_t		ipsp_refs;
4117c478bd9Sstevel@tonic-gate 	ipsec_sel_t		*ipsp_sel;	/* selector set (shared) */
4127c478bd9Sstevel@tonic-gate 	ipsec_action_t		*ipsp_act; 	/* action (may be shared) */
413bd670b35SErik Nordmark 	netstack_t		*ipsp_netstack;	/* No netstack_hold */
4147c478bd9Sstevel@tonic-gate };
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate #define	IPPOL_REFHOLD(ipp) {			\
417*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&(ipp)->ipsp_refs);	\
4187c478bd9Sstevel@tonic-gate 	ASSERT((ipp)->ipsp_refs != 0);		\
4197c478bd9Sstevel@tonic-gate }
420bd670b35SErik Nordmark #define	IPPOL_REFRELE(ipp) {					\
4217c478bd9Sstevel@tonic-gate 	ASSERT((ipp)->ipsp_refs != 0);				\
4227c478bd9Sstevel@tonic-gate 	membar_exit();						\
423*1a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&(ipp)->ipsp_refs) == 0)	\
424bd670b35SErik Nordmark 		ipsec_policy_free(ipp);				\
4257c478bd9Sstevel@tonic-gate 	(ipp) = 0;						\
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
428bd670b35SErik Nordmark #define	IPPOL_UNCHAIN(php, ip)					\
4298810c16bSdanmcd 	HASHLIST_UNCHAIN((ip), ipsp_hash);			\
4308810c16bSdanmcd 	avl_remove(&(php)->iph_rulebyid, (ip));			\
431bd670b35SErik Nordmark 	IPPOL_REFRELE(ip);
4328810c16bSdanmcd 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * Policy ruleset.  One per (protocol * direction) for system policy.
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate #define	IPSEC_AF_V4	0
4387c478bd9Sstevel@tonic-gate #define	IPSEC_AF_V6	1
4397c478bd9Sstevel@tonic-gate #define	IPSEC_NAF	2
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate typedef struct ipsec_policy_root_s
4427c478bd9Sstevel@tonic-gate {
4436a182920Ssommerfe 	ipsec_policy_t		*ipr_nonhash[IPSEC_NAF];
4446a182920Ssommerfe 	int			ipr_nchains;
4456a182920Ssommerfe 	ipsec_policy_hash_t 	*ipr_hash;
4467c478bd9Sstevel@tonic-gate } ipsec_policy_root_t;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate /*
4497c478bd9Sstevel@tonic-gate  * Policy head.  One for system policy; there may also be one present
4507c478bd9Sstevel@tonic-gate  * on ill_t's with interface-specific policy, as well as one present
4517c478bd9Sstevel@tonic-gate  * for sockets with per-socket policy allocated.
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate typedef struct ipsec_policy_head_s
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	uint32_t	iph_refs;
4577c478bd9Sstevel@tonic-gate 	krwlock_t	iph_lock;
4587c478bd9Sstevel@tonic-gate 	uint64_t	iph_gen; /* generation number */
4597c478bd9Sstevel@tonic-gate 	ipsec_policy_root_t iph_root[IPSEC_NTYPES];
4606a182920Ssommerfe 	avl_tree_t	iph_rulebyid;
4617c478bd9Sstevel@tonic-gate } ipsec_policy_head_t;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate #define	IPPH_REFHOLD(iph) {			\
464*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&(iph)->iph_refs);	\
4657c478bd9Sstevel@tonic-gate 	ASSERT((iph)->iph_refs != 0);		\
4667c478bd9Sstevel@tonic-gate }
467f4b3ec61Sdh155122 #define	IPPH_REFRELE(iph, ns) {					\
4687c478bd9Sstevel@tonic-gate 	ASSERT((iph)->iph_refs != 0);				\
4697c478bd9Sstevel@tonic-gate 	membar_exit();						\
470*1a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&(iph)->iph_refs) == 0)	\
471f4b3ec61Sdh155122 		ipsec_polhead_free(iph, ns);			\
4727c478bd9Sstevel@tonic-gate 	(iph) = 0;						\
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /*
4768810c16bSdanmcd  * IPsec fragment related structures
4778810c16bSdanmcd  */
4788810c16bSdanmcd 
4798810c16bSdanmcd typedef struct ipsec_fragcache_entry {
4808810c16bSdanmcd 	struct ipsec_fragcache_entry *itpfe_next;	/* hash list chain */
4818810c16bSdanmcd 	mblk_t *itpfe_fraglist;			/* list of fragments */
4828810c16bSdanmcd 	time_t itpfe_exp;			/* time when entry is stale */
4838810c16bSdanmcd 	int itpfe_depth;			/* # of fragments in list */
4848810c16bSdanmcd 	ipsec_addr_t itpfe_frag_src;
4858810c16bSdanmcd 	ipsec_addr_t itpfe_frag_dst;
4868810c16bSdanmcd #define	itpfe_src itpfe_frag_src.ipsad_v4
4878810c16bSdanmcd #define	itpfe_src6 itpfe_frag_src.ipsad_v6
4888810c16bSdanmcd #define	itpfe_dst itpfe_frag_dst.ipsad_v4
4898810c16bSdanmcd #define	itpfe_dst6 itpfe_frag_dst.ipsad_v6
4908810c16bSdanmcd 	uint32_t itpfe_id;			/* IP datagram ID */
4918810c16bSdanmcd 	uint8_t itpfe_proto;			/* IP Protocol */
4928810c16bSdanmcd 	uint8_t itpfe_last;			/* Last packet */
4938810c16bSdanmcd } ipsec_fragcache_entry_t;
4948810c16bSdanmcd 
4958810c16bSdanmcd typedef struct ipsec_fragcache {
4968810c16bSdanmcd 	kmutex_t itpf_lock;
4978810c16bSdanmcd 	struct ipsec_fragcache_entry **itpf_ptr;
4988810c16bSdanmcd 	struct ipsec_fragcache_entry *itpf_freelist;
4998810c16bSdanmcd 	time_t itpf_expire_hint;	/* time when oldest entry is stale */
5008810c16bSdanmcd } ipsec_fragcache_t;
5018810c16bSdanmcd 
5028810c16bSdanmcd /*
5038810c16bSdanmcd  * Tunnel policies.  We keep a minature of the transport-mode/global policy
5048810c16bSdanmcd  * per each tunnel instance.
5058810c16bSdanmcd  *
5068810c16bSdanmcd  * People who need both an itp held down AND one of its polheads need to
5078810c16bSdanmcd  * first lock the itp, THEN the polhead, otherwise deadlock WILL occur.
5088810c16bSdanmcd  */
5098810c16bSdanmcd typedef struct ipsec_tun_pol_s {
5108810c16bSdanmcd 	avl_node_t itp_node;
5118810c16bSdanmcd 	kmutex_t itp_lock;
5128810c16bSdanmcd 	uint64_t itp_next_policy_index;
5138810c16bSdanmcd 	ipsec_policy_head_t *itp_policy;
5148810c16bSdanmcd 	ipsec_policy_head_t *itp_inactive;
5158810c16bSdanmcd 	uint32_t itp_flags;
5168810c16bSdanmcd 	uint32_t itp_refcnt;
5178810c16bSdanmcd 	char itp_name[LIFNAMSIZ];
5188810c16bSdanmcd 	ipsec_fragcache_t itp_fragcache;
5198810c16bSdanmcd } ipsec_tun_pol_t;
5208810c16bSdanmcd /* NOTE - Callers (tun code) synchronize their own instances for these flags. */
5218810c16bSdanmcd #define	ITPF_P_ACTIVE 0x1	/* Are we using IPsec right now? */
5228810c16bSdanmcd #define	ITPF_P_TUNNEL 0x2	/* Negotiate tunnel-mode */
5238810c16bSdanmcd /* Optimization -> Do we have per-port security entries in this polhead? */
5248810c16bSdanmcd #define	ITPF_P_PER_PORT_SECURITY 0x4
5258810c16bSdanmcd #define	ITPF_PFLAGS 0x7
5268810c16bSdanmcd #define	ITPF_SHIFT 3
5278810c16bSdanmcd 
5288810c16bSdanmcd #define	ITPF_I_ACTIVE 0x8	/* Is the inactive using IPsec right now? */
5298810c16bSdanmcd #define	ITPF_I_TUNNEL 0x10	/* Negotiate tunnel-mode (on inactive) */
5308810c16bSdanmcd /* Optimization -> Do we have per-port security entries in this polhead? */
5318810c16bSdanmcd #define	ITPF_I_PER_PORT_SECURITY 0x20
5328810c16bSdanmcd #define	ITPF_IFLAGS 0x38
5338810c16bSdanmcd 
5348810c16bSdanmcd /* NOTE:  f cannot be an expression. */
5358810c16bSdanmcd #define	ITPF_CLONE(f) (f) = (((f) & ITPF_PFLAGS) | \
5368810c16bSdanmcd 	    (((f) & ITPF_PFLAGS) << ITPF_SHIFT));
5378810c16bSdanmcd #define	ITPF_SWAP(f) (f) = ((((f) & ITPF_PFLAGS) << ITPF_SHIFT) | \
5388810c16bSdanmcd 	    (((f) & ITPF_IFLAGS) >> ITPF_SHIFT))
5398810c16bSdanmcd 
5408810c16bSdanmcd #define	ITP_P_ISACTIVE(itp, iph) ((itp)->itp_flags & \
5418810c16bSdanmcd 	(((itp)->itp_policy == (iph)) ? ITPF_P_ACTIVE : ITPF_I_ACTIVE))
5428810c16bSdanmcd 
5438810c16bSdanmcd #define	ITP_P_ISTUNNEL(itp, iph) ((itp)->itp_flags & \
5448810c16bSdanmcd 	(((itp)->itp_policy == (iph)) ? ITPF_P_TUNNEL : ITPF_I_TUNNEL))
5458810c16bSdanmcd 
5468810c16bSdanmcd #define	ITP_P_ISPERPORT(itp, iph) ((itp)->itp_flags & \
5478810c16bSdanmcd 	(((itp)->itp_policy == (iph)) ? ITPF_P_PER_PORT_SECURITY : \
5488810c16bSdanmcd 	ITPF_I_PER_PORT_SECURITY))
5498810c16bSdanmcd 
5508810c16bSdanmcd #define	ITP_REFHOLD(itp) { \
551*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&((itp)->itp_refcnt));	\
5528810c16bSdanmcd 	ASSERT((itp)->itp_refcnt != 0); \
5538810c16bSdanmcd }
5548810c16bSdanmcd 
555f4b3ec61Sdh155122 #define	ITP_REFRELE(itp, ns) { \
5568810c16bSdanmcd 	ASSERT((itp)->itp_refcnt != 0); \
5578810c16bSdanmcd 	membar_exit(); \
558*1a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&((itp)->itp_refcnt)) == 0) \
559f4b3ec61Sdh155122 		itp_free(itp, ns); \
5608810c16bSdanmcd }
5618810c16bSdanmcd 
5628810c16bSdanmcd /*
5637c478bd9Sstevel@tonic-gate  * Certificate identity.
5647c478bd9Sstevel@tonic-gate  */
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate typedef struct ipsid_s
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	struct ipsid_s *ipsid_next;
5697c478bd9Sstevel@tonic-gate 	struct ipsid_s **ipsid_ptpn;
5707c478bd9Sstevel@tonic-gate 	uint32_t	ipsid_refcnt;
5717c478bd9Sstevel@tonic-gate 	int		ipsid_type;	/* id type */
5727c478bd9Sstevel@tonic-gate 	char 		*ipsid_cid;	/* certificate id string */
5737c478bd9Sstevel@tonic-gate } ipsid_t;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate  * ipsid_t reference hold/release macros, just like ipsa versions.
5777c478bd9Sstevel@tonic-gate  */
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate #define	IPSID_REFHOLD(ipsid) {			\
580*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&(ipsid)->ipsid_refcnt);	\
5817c478bd9Sstevel@tonic-gate 	ASSERT((ipsid)->ipsid_refcnt != 0);	\
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate  * Decrement the reference count on the ID.  Someone else will clean up
5867c478bd9Sstevel@tonic-gate  * after us later.
5877c478bd9Sstevel@tonic-gate  */
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate #define	IPSID_REFRELE(ipsid) {					\
5907c478bd9Sstevel@tonic-gate 	membar_exit();						\
591*1a5e258fSJosef 'Jeff' Sipek 	atomic_dec_32(&(ipsid)->ipsid_refcnt);		\
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate /*
5957c478bd9Sstevel@tonic-gate  * Following are the estimates of what the maximum AH and ESP header size
5967c478bd9Sstevel@tonic-gate  * would be. This is used to tell the upper layer the right value of MSS
5977c478bd9Sstevel@tonic-gate  * it should use without consulting AH/ESP. If the size is something
5987c478bd9Sstevel@tonic-gate  * different from this, ULP will learn the right one through
5997c478bd9Sstevel@tonic-gate  * ICMP_FRAGMENTATION_NEEDED messages generated locally.
6007c478bd9Sstevel@tonic-gate  *
6010358d3a6Sdanmcd  * AH : 12 bytes of constant header + 32 bytes of ICV checksum (SHA-512).
6027c478bd9Sstevel@tonic-gate  */
6030358d3a6Sdanmcd #define	IPSEC_MAX_AH_HDR_SIZE   (44)
6047c478bd9Sstevel@tonic-gate 
6050358d3a6Sdanmcd /*
6060358d3a6Sdanmcd  * ESP : Is a bit more complex...
6070358d3a6Sdanmcd  *
6080358d3a6Sdanmcd  * A system of one inequality and one equation MUST be solved for proper ESP
6090358d3a6Sdanmcd  * overhead.  The inequality is:
6100358d3a6Sdanmcd  *
6110358d3a6Sdanmcd  *    MTU - sizeof (IP header + options) >=
6120358d3a6Sdanmcd  *		sizeof (esph_t) + sizeof (IV or ctr) + data-size + 2 + ICV
6130358d3a6Sdanmcd  *
6140358d3a6Sdanmcd  * IV or counter is almost always the cipher's block size.  The equation is:
6150358d3a6Sdanmcd  *
6160358d3a6Sdanmcd  *    data-size % block-size = (block-size - 2)
6170358d3a6Sdanmcd  *
6180358d3a6Sdanmcd  * so we can put as much data into the datagram as possible.  If we are
6190358d3a6Sdanmcd  * pessimistic and include our largest overhead cipher (AES) and hash
6200358d3a6Sdanmcd  * (SHA-512), and assume 1500-byte MTU minus IPv4 overhead of 20 bytes, we get:
6210358d3a6Sdanmcd  *
6220358d3a6Sdanmcd  *    1480 >= 8 + 16 + data-size + 2 + 32
6230358d3a6Sdanmcd  *    1480 >= 58 + data-size
6240358d3a6Sdanmcd  *    1422 >= data-size,      1422 % 16 = 14, so 58 is the overhead!
6250358d3a6Sdanmcd  *
6260358d3a6Sdanmcd  * But, let's re-run the numbers with the same algorithms, but with an IPv6
6270358d3a6Sdanmcd  * header:
6280358d3a6Sdanmcd  *
6290358d3a6Sdanmcd  *    1460 >= 58 + data-size
6300358d3a6Sdanmcd  *    1402 >= data-size,     1402 % 16 = 10, meaning shrink to 1390 to get 14,
6310358d3a6Sdanmcd  *
6320358d3a6Sdanmcd  * which means the overhead is now 70.
6330358d3a6Sdanmcd  *
6340358d3a6Sdanmcd  * Hmmm... IPv4 headers can never be anything other than multiples of 4-bytes,
6350358d3a6Sdanmcd  * and IPv6 ones can never be anything other than multiples of 8-bytes.  We've
6360358d3a6Sdanmcd  * seen overheads of 58 and 70.  58 % 16 == 10, and 70 % 16 == 6.  IPv4 could
6370358d3a6Sdanmcd  * force us to have 62 ( % 16 == 14) or 66 ( % 16 == 2), or IPv6 could force us
6380358d3a6Sdanmcd  * to have 78 ( % 16 = 14).  Let's compute IPv6 + 8-bytes of options:
6390358d3a6Sdanmcd  *
6400358d3a6Sdanmcd  *    1452 >= 58 + data-size
6410358d3a6Sdanmcd  *    1394 >= data-size,     1394 % 16 = 2, meaning shrink to 1390 to get 14,
6420358d3a6Sdanmcd  *
6430358d3a6Sdanmcd  * Aha!  The "ESP overhead" shrinks to 62 (70 - 8).  This is good.  Let's try
6440358d3a6Sdanmcd  * IPv4 + 8 bytes of IPv4 options:
6450358d3a6Sdanmcd  *
6460358d3a6Sdanmcd  *    1472 >= 58 + data-size
6470358d3a6Sdanmcd  *    1414 >= data-size,      1414 % 16 = 6, meaning shrink to 1406,
6480358d3a6Sdanmcd  *
6490358d3a6Sdanmcd  * meaning 66 is the overhead.  Let's try 12 bytes:
6500358d3a6Sdanmcd  *
6510358d3a6Sdanmcd  *    1468 >= 58 + data-size
6520358d3a6Sdanmcd  *    1410 >= data-size,      1410 % 16 = 2, meaning also shrink to 1406,
6530358d3a6Sdanmcd  *
6540358d3a6Sdanmcd  * meaning 62 is the overhead.  How about 16 bytes?
6550358d3a6Sdanmcd  *
6560358d3a6Sdanmcd  *    1464 >= 58 + data-size
6570358d3a6Sdanmcd  *    1406 >= data-size,      1402 % 16 = 14, which is great!
6580358d3a6Sdanmcd  *
6590358d3a6Sdanmcd  * this means 58 is the overhead.  If I wrap and add 20 bytes, it looks just
6600358d3a6Sdanmcd  * like IPv6's 70 bytes.  If I add 24, we go back to 66 bytes.
6610358d3a6Sdanmcd  *
6620358d3a6Sdanmcd  * So picking 70 is a sensible, conservative default.  Optimal calculations
6630358d3a6Sdanmcd  * will depend on knowing pre-ESP header length (called "divpoint" in the ESP
6640358d3a6Sdanmcd  * code), which could be cached in the conn_t for connected endpoints, or
6650358d3a6Sdanmcd  * which must be computed on every datagram otherwise.
6660358d3a6Sdanmcd  */
6670358d3a6Sdanmcd #define	IPSEC_MAX_ESP_HDR_SIZE  (70)
6680358d3a6Sdanmcd 
6690358d3a6Sdanmcd /*
6700358d3a6Sdanmcd  * Alternate, when we know the crypto block size via the SA.  Assume an ICV on
6710358d3a6Sdanmcd  * the SA.  Use:
6720358d3a6Sdanmcd  *
6730358d3a6Sdanmcd  * sizeof (esph_t) + 2 * (sizeof (IV/counter)) - 2 + sizeof (ICV).  The "-2"
6740358d3a6Sdanmcd  * discounts the overhead of the pad + padlen that gets swallowed up by the
6750358d3a6Sdanmcd  * second (theoretically all-pad) cipher-block.  If you use our examples of
6760358d3a6Sdanmcd  * AES and SHA512, you get:
6770358d3a6Sdanmcd  *
6780358d3a6Sdanmcd  *    8 + 32 - 2 + 32 == 70.
6790358d3a6Sdanmcd  *
6800358d3a6Sdanmcd  * Which is our pre-computed maximum above.
6810358d3a6Sdanmcd  */
6820358d3a6Sdanmcd #include <inet/ipsecesp.h>
6830358d3a6Sdanmcd #define	IPSEC_BASE_ESP_HDR_SIZE(sa) \
6840358d3a6Sdanmcd 	(sizeof (esph_t) + ((sa)->ipsa_iv_len << 1) - 2 + (sa)->ipsa_mac_len)
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /*
687f4b3ec61Sdh155122  * Identity hash table.
688f4b3ec61Sdh155122  *
689f4b3ec61Sdh155122  * Identities are refcounted and "interned" into the hash table.
690f4b3ec61Sdh155122  * Only references coming from other objects (SA's, latching state)
691f4b3ec61Sdh155122  * are counted in ipsid_refcnt.
692f4b3ec61Sdh155122  *
693f4b3ec61Sdh155122  * Locking: IPSID_REFHOLD is safe only when (a) the object's hash bucket
694f4b3ec61Sdh155122  * is locked, (b) we know that the refcount must be > 0.
695f4b3ec61Sdh155122  *
696f4b3ec61Sdh155122  * The ipsid_next and ipsid_ptpn fields are only to be referenced or
697f4b3ec61Sdh155122  * modified when the bucket lock is held; in particular, we only
698f4b3ec61Sdh155122  * delete objects while holding the bucket lock, and we only increase
699f4b3ec61Sdh155122  * the refcount from 0 to 1 while the bucket lock is held.
700f4b3ec61Sdh155122  */
701f4b3ec61Sdh155122 
702f4b3ec61Sdh155122 #define	IPSID_HASHSIZE 64
703f4b3ec61Sdh155122 
704f4b3ec61Sdh155122 typedef struct ipsif_s
705f4b3ec61Sdh155122 {
706f4b3ec61Sdh155122 	ipsid_t *ipsif_head;
707f4b3ec61Sdh155122 	kmutex_t ipsif_lock;
708f4b3ec61Sdh155122 } ipsif_t;
709f4b3ec61Sdh155122 
710bd670b35SErik Nordmark /*
711bd670b35SErik Nordmark  * For call to the kernel crypto framework. State needed during
712bd670b35SErik Nordmark  * the execution of a crypto request.
713bd670b35SErik Nordmark  */
714bd670b35SErik Nordmark typedef struct ipsec_crypto_s {
715bd670b35SErik Nordmark 	size_t		ic_skip_len;		/* len to skip for AH auth */
716bd670b35SErik Nordmark 	crypto_data_t	ic_crypto_data;		/* single op crypto data */
717bd670b35SErik Nordmark 	crypto_dual_data_t ic_crypto_dual_data; /* for dual ops */
718bd670b35SErik Nordmark 	crypto_data_t	ic_crypto_mac;		/* to store the MAC */
719bd670b35SErik Nordmark 	ipsa_cm_mech_t	ic_cmm;
720bd670b35SErik Nordmark } ipsec_crypto_t;
721f4b3ec61Sdh155122 
722f4b3ec61Sdh155122 /*
723437220cdSdanmcd  * IPsec stack instances
724f4b3ec61Sdh155122  */
725f4b3ec61Sdh155122 struct ipsec_stack {
726f4b3ec61Sdh155122 	netstack_t		*ipsec_netstack;	/* Common netstack */
727f4b3ec61Sdh155122 
728f4b3ec61Sdh155122 	/* Packet dropper for IP IPsec processing failures */
729f4b3ec61Sdh155122 	ipdropper_t		ipsec_dropper;
730f4b3ec61Sdh155122 
731f4b3ec61Sdh155122 /* From spd.c */
732f4b3ec61Sdh155122 	/*
733f4b3ec61Sdh155122 	 * Policy rule index generator.  We assume this won't wrap in the
734f4b3ec61Sdh155122 	 * lifetime of a system.  If we make 2^20 policy changes per second,
735f4b3ec61Sdh155122 	 * this will last 2^44 seconds, or roughly 500,000 years, so we don't
736f4b3ec61Sdh155122 	 * have to worry about reusing policy index values.
737f4b3ec61Sdh155122 	 */
738f4b3ec61Sdh155122 	uint64_t		ipsec_next_policy_index;
739f4b3ec61Sdh155122 
740f4b3ec61Sdh155122 	HASH_HEAD(ipsec_action_s) ipsec_action_hash[IPSEC_ACTION_HASH_SIZE];
741f4b3ec61Sdh155122 	HASH_HEAD(ipsec_sel)	  *ipsec_sel_hash;
742f4b3ec61Sdh155122 	uint32_t		ipsec_spd_hashsize;
743f4b3ec61Sdh155122 
744f4b3ec61Sdh155122 	ipsif_t			ipsec_ipsid_buckets[IPSID_HASHSIZE];
745f4b3ec61Sdh155122 
746f4b3ec61Sdh155122 	/*
747f4b3ec61Sdh155122 	 * Active & Inactive system policy roots
748f4b3ec61Sdh155122 	 */
749f4b3ec61Sdh155122 	ipsec_policy_head_t	ipsec_system_policy;
750f4b3ec61Sdh155122 	ipsec_policy_head_t	ipsec_inactive_policy;
751f4b3ec61Sdh155122 
752f4b3ec61Sdh155122 	/* Packet dropper for generic SPD drops. */
753f4b3ec61Sdh155122 	ipdropper_t		ipsec_spd_dropper;
754f4b3ec61Sdh155122 
755f4b3ec61Sdh155122 /* ipdrop.c */
756f4b3ec61Sdh155122 	kstat_t			*ipsec_ip_drop_kstat;
757f4b3ec61Sdh155122 	struct ip_dropstats	*ipsec_ip_drop_types;
758f4b3ec61Sdh155122 
759f4b3ec61Sdh155122 /* spd.c */
760f4b3ec61Sdh155122 	/*
761f4b3ec61Sdh155122 	 * Have a counter for every possible policy message in
762f4b3ec61Sdh155122 	 * ipsec_policy_failure_msgs
763f4b3ec61Sdh155122 	 */
764f4b3ec61Sdh155122 	uint32_t		ipsec_policy_failure_count[IPSEC_POLICY_MAX];
765f4b3ec61Sdh155122 	/* Time since last ipsec policy failure that printed a message. */
766f4b3ec61Sdh155122 	hrtime_t		ipsec_policy_failure_last;
767f4b3ec61Sdh155122 
768f4b3ec61Sdh155122 /* ip_spd.c */
769f4b3ec61Sdh155122 	/* stats */
770f4b3ec61Sdh155122 	kstat_t			*ipsec_ksp;
771f4b3ec61Sdh155122 	struct ipsec_kstats_s	*ipsec_kstats;
772f4b3ec61Sdh155122 
773f4b3ec61Sdh155122 /* sadb.c */
774f4b3ec61Sdh155122 	/* Packet dropper for generic SADB drops. */
775f4b3ec61Sdh155122 	ipdropper_t		ipsec_sadb_dropper;
776f4b3ec61Sdh155122 
777f4b3ec61Sdh155122 /* spd.c */
778f4b3ec61Sdh155122 	boolean_t		ipsec_inbound_v4_policy_present;
779f4b3ec61Sdh155122 	boolean_t		ipsec_outbound_v4_policy_present;
780f4b3ec61Sdh155122 	boolean_t		ipsec_inbound_v6_policy_present;
781f4b3ec61Sdh155122 	boolean_t		ipsec_outbound_v6_policy_present;
782f4b3ec61Sdh155122 
783f4b3ec61Sdh155122 /* spd.c */
784f4b3ec61Sdh155122 	/*
785f4b3ec61Sdh155122 	 * Because policy needs to know what algorithms are supported, keep the
786f4b3ec61Sdh155122 	 * lists of algorithms here.
787f4b3ec61Sdh155122 	 */
788f4b3ec61Sdh155122 	kmutex_t 		ipsec_alg_lock;
789f4b3ec61Sdh155122 
790f4b3ec61Sdh155122 	uint8_t			ipsec_nalgs[IPSEC_NALGTYPES];
791f4b3ec61Sdh155122 	ipsec_alginfo_t	*ipsec_alglists[IPSEC_NALGTYPES][IPSEC_MAX_ALGS];
792f4b3ec61Sdh155122 
793f4b3ec61Sdh155122 	uint8_t		ipsec_sortlist[IPSEC_NALGTYPES][IPSEC_MAX_ALGS];
794f4b3ec61Sdh155122 
795f4b3ec61Sdh155122 	int		ipsec_algs_exec_mode[IPSEC_NALGTYPES];
796f4b3ec61Sdh155122 
797f4b3ec61Sdh155122 	uint32_t 	ipsec_tun_spd_hashsize;
798f4b3ec61Sdh155122 	/*
799f4b3ec61Sdh155122 	 * Tunnel policies - AVL tree indexed by tunnel name.
800f4b3ec61Sdh155122 	 */
801f4b3ec61Sdh155122 	krwlock_t 	ipsec_tunnel_policy_lock;
802f4b3ec61Sdh155122 	uint64_t	ipsec_tunnel_policy_gen;
803f4b3ec61Sdh155122 	avl_tree_t	ipsec_tunnel_policies;
804f4b3ec61Sdh155122 
805f4b3ec61Sdh155122 /* ipsec_loader.c */
806f4b3ec61Sdh155122 	kmutex_t	ipsec_loader_lock;
807f4b3ec61Sdh155122 	int		ipsec_loader_state;
808f4b3ec61Sdh155122 	int		ipsec_loader_sig;
809f4b3ec61Sdh155122 	kt_did_t	ipsec_loader_tid;
810f4b3ec61Sdh155122 	kcondvar_t	ipsec_loader_sig_cv;	/* For loader_sig conditions. */
811f4b3ec61Sdh155122 
812f4b3ec61Sdh155122 };
813f4b3ec61Sdh155122 typedef struct ipsec_stack ipsec_stack_t;
814f4b3ec61Sdh155122 
815f4b3ec61Sdh155122 /* Handle the kstat_create in ip_drop_init() failing */
816f4b3ec61Sdh155122 #define	DROPPER(_ipss, _dropper) \
817f4b3ec61Sdh155122 	(((_ipss)->ipsec_ip_drop_types == NULL) ? NULL : \
818f4b3ec61Sdh155122 	&((_ipss)->ipsec_ip_drop_types->_dropper))
819f4b3ec61Sdh155122 
820f4b3ec61Sdh155122 /*
8217c478bd9Sstevel@tonic-gate  * Loader states..
8227c478bd9Sstevel@tonic-gate  */
8237c478bd9Sstevel@tonic-gate #define	IPSEC_LOADER_WAIT	0
8247c478bd9Sstevel@tonic-gate #define	IPSEC_LOADER_FAILED	-1
8257c478bd9Sstevel@tonic-gate #define	IPSEC_LOADER_SUCCEEDED	1
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate /*
8287c478bd9Sstevel@tonic-gate  * ipsec_loader entrypoints.
8297c478bd9Sstevel@tonic-gate  */
830f4b3ec61Sdh155122 extern void ipsec_loader_init(ipsec_stack_t *);
831f4b3ec61Sdh155122 extern void ipsec_loader_start(ipsec_stack_t *);
832f4b3ec61Sdh155122 extern void ipsec_loader_destroy(ipsec_stack_t *);
833f4b3ec61Sdh155122 extern void ipsec_loader_loadnow(ipsec_stack_t *);
834f4b3ec61Sdh155122 extern boolean_t ipsec_loader_wait(queue_t *q, ipsec_stack_t *);
835f4b3ec61Sdh155122 extern boolean_t ipsec_loaded(ipsec_stack_t *);
836f4b3ec61Sdh155122 extern boolean_t ipsec_failed(ipsec_stack_t *);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate  * ipsec policy entrypoints (spd.c)
8407c478bd9Sstevel@tonic-gate  */
8417c478bd9Sstevel@tonic-gate 
842f4b3ec61Sdh155122 extern void ipsec_policy_g_destroy(void);
843f4b3ec61Sdh155122 extern void ipsec_policy_g_init(void);
844f4b3ec61Sdh155122 
845bd670b35SErik Nordmark extern mblk_t	*ipsec_add_crypto_data(mblk_t *, ipsec_crypto_t **);
846bd670b35SErik Nordmark extern mblk_t	*ipsec_remove_crypto_data(mblk_t *, ipsec_crypto_t **);
847bd670b35SErik Nordmark extern mblk_t	*ipsec_free_crypto_data(mblk_t *);
848f4b3ec61Sdh155122 extern int ipsec_alloc_table(ipsec_policy_head_t *, int, int, boolean_t,
849f4b3ec61Sdh155122     netstack_t *);
8508810c16bSdanmcd extern void ipsec_polhead_init(ipsec_policy_head_t *, int);
8518810c16bSdanmcd extern void ipsec_polhead_destroy(ipsec_policy_head_t *);
8528810c16bSdanmcd extern void ipsec_polhead_free_table(ipsec_policy_head_t *);
8537c478bd9Sstevel@tonic-gate extern mblk_t *ipsec_check_global_policy(mblk_t *, conn_t *, ipha_t *,
854bd670b35SErik Nordmark     ip6_t *, ip_recv_attr_t *, netstack_t *ns);
8557c478bd9Sstevel@tonic-gate extern mblk_t *ipsec_check_inbound_policy(mblk_t *, conn_t *, ipha_t *, ip6_t *,
856bd670b35SErik Nordmark     ip_recv_attr_t *);
8577c478bd9Sstevel@tonic-gate 
858bd670b35SErik Nordmark extern boolean_t ipsec_in_to_out(ip_recv_attr_t *, ip_xmit_attr_t *,
859bd670b35SErik Nordmark     mblk_t *, ipha_t *, ip6_t *);
860bd670b35SErik Nordmark extern void ipsec_in_release_refs(ip_recv_attr_t *);
861bd670b35SErik Nordmark extern void ipsec_out_release_refs(ip_xmit_attr_t *);
862f4b3ec61Sdh155122 extern void ipsec_log_policy_failure(int, char *, ipha_t *, ip6_t *, boolean_t,
863f4b3ec61Sdh155122     netstack_t *);
8647c478bd9Sstevel@tonic-gate extern boolean_t ipsec_inbound_accept_clear(mblk_t *, ipha_t *, ip6_t *);
8657c478bd9Sstevel@tonic-gate extern int ipsec_conn_cache_policy(conn_t *, boolean_t);
866bd670b35SErik Nordmark extern void ipsec_cache_outbound_policy(const conn_t *, const in6_addr_t *,
867bd670b35SErik Nordmark     const in6_addr_t *, in_port_t, ip_xmit_attr_t *);
868bd670b35SErik Nordmark extern boolean_t ipsec_outbound_policy_current(ip_xmit_attr_t *);
869bd670b35SErik Nordmark extern ipsec_action_t *ipsec_in_to_out_action(ip_recv_attr_t *);
870bd670b35SErik Nordmark extern void ipsec_latch_inbound(conn_t *connp, ip_recv_attr_t *ira);
8717c478bd9Sstevel@tonic-gate 
872bd670b35SErik Nordmark extern void ipsec_policy_free(ipsec_policy_t *);
8737c478bd9Sstevel@tonic-gate extern void ipsec_action_free(ipsec_action_t *);
874f4b3ec61Sdh155122 extern void ipsec_polhead_free(ipsec_policy_head_t *, netstack_t *);
875f4b3ec61Sdh155122 extern ipsec_policy_head_t *ipsec_polhead_split(ipsec_policy_head_t *,
876f4b3ec61Sdh155122     netstack_t *);
8777c478bd9Sstevel@tonic-gate extern ipsec_policy_head_t *ipsec_polhead_create(void);
878f4b3ec61Sdh155122 extern ipsec_policy_head_t *ipsec_system_policy(netstack_t *);
879f4b3ec61Sdh155122 extern ipsec_policy_head_t *ipsec_inactive_policy(netstack_t *);
880f4b3ec61Sdh155122 extern void ipsec_swap_policy(ipsec_policy_head_t *, ipsec_policy_head_t *,
881f4b3ec61Sdh155122     netstack_t *);
882f4b3ec61Sdh155122 extern void ipsec_swap_global_policy(netstack_t *);
8837c478bd9Sstevel@tonic-gate 
884f4b3ec61Sdh155122 extern int ipsec_clone_system_policy(netstack_t *);
8856a182920Ssommerfe extern ipsec_policy_t *ipsec_policy_create(ipsec_selkey_t *,
886f4b3ec61Sdh155122     const ipsec_act_t *, int, int, uint64_t *, netstack_t *);
8877c478bd9Sstevel@tonic-gate extern boolean_t ipsec_policy_delete(ipsec_policy_head_t *,
888f4b3ec61Sdh155122     ipsec_selkey_t *, int, netstack_t *);
889f4b3ec61Sdh155122 extern int ipsec_policy_delete_index(ipsec_policy_head_t *, uint64_t,
890f4b3ec61Sdh155122     netstack_t *);
8912b24ab6bSSebastien Roy extern boolean_t ipsec_polhead_insert(ipsec_policy_head_t *, ipsec_act_t *,
8922b24ab6bSSebastien Roy     uint_t, int, int, netstack_t *);
893f4b3ec61Sdh155122 extern void ipsec_polhead_flush(ipsec_policy_head_t *, netstack_t *);
894f4b3ec61Sdh155122 extern int ipsec_copy_polhead(ipsec_policy_head_t *, ipsec_policy_head_t *,
895f4b3ec61Sdh155122     netstack_t *);
8962b24ab6bSSebastien Roy extern void ipsec_actvec_from_req(const ipsec_req_t *, ipsec_act_t **, uint_t *,
897f4b3ec61Sdh155122     netstack_t *);
8987c478bd9Sstevel@tonic-gate extern void ipsec_actvec_free(ipsec_act_t *, uint_t);
8998810c16bSdanmcd extern int ipsec_req_from_head(ipsec_policy_head_t *, ipsec_req_t *, int);
900f4b3ec61Sdh155122 extern mblk_t *ipsec_construct_inverse_acquire(sadb_msg_t *, sadb_ext_t **,
901f4b3ec61Sdh155122     netstack_t *);
902bd670b35SErik Nordmark extern ipsec_policy_t *ipsec_find_policy(int, const conn_t *,
903bd670b35SErik Nordmark     ipsec_selector_t *, netstack_t *);
904f4b3ec61Sdh155122 extern ipsid_t *ipsid_lookup(int, char *, netstack_t *);
9057c478bd9Sstevel@tonic-gate extern boolean_t ipsid_equal(ipsid_t *, ipsid_t *);
906f4b3ec61Sdh155122 extern void ipsid_gc(netstack_t *);
9077c478bd9Sstevel@tonic-gate extern void ipsec_latch_ids(ipsec_latch_t *, ipsid_t *, ipsid_t *);
9087c478bd9Sstevel@tonic-gate 
909f4b3ec61Sdh155122 extern void ipsec_config_flush(netstack_t *);
9107c478bd9Sstevel@tonic-gate extern boolean_t ipsec_check_policy(ipsec_policy_head_t *, ipsec_policy_t *,
9117c478bd9Sstevel@tonic-gate     int);
912f4b3ec61Sdh155122 extern void ipsec_enter_policy(ipsec_policy_head_t *, ipsec_policy_t *, int,
913f4b3ec61Sdh155122     netstack_t *);
914f4b3ec61Sdh155122 extern boolean_t ipsec_check_action(ipsec_act_t *, int *, netstack_t *);
9157c478bd9Sstevel@tonic-gate 
916bd670b35SErik Nordmark extern void iplatch_free(ipsec_latch_t *);
9177c478bd9Sstevel@tonic-gate extern ipsec_latch_t *iplatch_create(void);
9187c478bd9Sstevel@tonic-gate extern int ipsec_set_req(cred_t *, conn_t *, ipsec_req_t *);
9197c478bd9Sstevel@tonic-gate 
9206a182920Ssommerfe extern void ipsec_insert_always(avl_tree_t *tree, void *new_node);
9216a182920Ssommerfe 
9228810c16bSdanmcd extern int32_t ipsec_act_ovhd(const ipsec_act_t *act);
923bd670b35SErik Nordmark extern mblk_t *sadb_whack_label(mblk_t *, ipsa_t *, ip_xmit_attr_t *,
924bd670b35SErik Nordmark     kstat_named_t *, ipdropper_t *);
925bd670b35SErik Nordmark extern mblk_t *sadb_whack_label_v4(mblk_t *, ipsa_t *, kstat_named_t *,
926bd670b35SErik Nordmark     ipdropper_t *);
927bd670b35SErik Nordmark extern mblk_t *sadb_whack_label_v6(mblk_t *, ipsa_t *, kstat_named_t *,
928bd670b35SErik Nordmark     ipdropper_t *);
929628b0c67SMark Fenwick extern boolean_t update_iv(uint8_t *, queue_t *, ipsa_t *, ipsecesp_stack_t *);
9308810c16bSdanmcd 
9318810c16bSdanmcd /*
9328810c16bSdanmcd  * Tunnel-support SPD functions and variables.
9338810c16bSdanmcd  */
9342b24ab6bSSebastien Roy struct iptun_s;	/* Defined in inet/iptun/iptun_impl.h. */
935bd670b35SErik Nordmark extern mblk_t *ipsec_tun_inbound(ip_recv_attr_t *, mblk_t *,  ipsec_tun_pol_t *,
936f4b3ec61Sdh155122     ipha_t *, ip6_t *, ipha_t *, ip6_t *, int, netstack_t *);
9372b24ab6bSSebastien Roy extern mblk_t *ipsec_tun_outbound(mblk_t *, struct iptun_s *, ipha_t *,
938bd670b35SErik Nordmark     ip6_t *, ipha_t *, ip6_t *, int, ip_xmit_attr_t *);
939f4b3ec61Sdh155122 extern void itp_free(ipsec_tun_pol_t *, netstack_t *);
940f4b3ec61Sdh155122 extern ipsec_tun_pol_t *create_tunnel_policy(char *, int *, uint64_t *,
941f4b3ec61Sdh155122     netstack_t *);
942f4b3ec61Sdh155122 extern ipsec_tun_pol_t *get_tunnel_policy(char *, netstack_t *);
943f4b3ec61Sdh155122 extern void itp_unlink(ipsec_tun_pol_t *, netstack_t *);
944f4b3ec61Sdh155122 extern void itp_walk(void (*)(ipsec_tun_pol_t *, void *, netstack_t *),
945f4b3ec61Sdh155122     void *, netstack_t *);
9468810c16bSdanmcd 
9472b24ab6bSSebastien Roy extern ipsec_tun_pol_t *itp_get_byaddr(uint32_t *, uint32_t *, int,
9482b24ab6bSSebastien Roy     ip_stack_t *);
9498810c16bSdanmcd 
9507c478bd9Sstevel@tonic-gate /*
951437220cdSdanmcd  * IPsec AH/ESP functions called from IP or the common SADB code in AH.
9527c478bd9Sstevel@tonic-gate  */
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate extern void ipsecah_in_assocfailure(mblk_t *, char, ushort_t, char *,
955bd670b35SErik Nordmark     uint32_t, void *, int, ip_recv_attr_t *ira);
9567c478bd9Sstevel@tonic-gate extern void ipsecesp_in_assocfailure(mblk_t *, char, ushort_t, char *,
957bd670b35SErik Nordmark     uint32_t, void *, int, ip_recv_attr_t *ira);
958437220cdSdanmcd extern void ipsecesp_send_keepalive(ipsa_t *);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate /*
9617c478bd9Sstevel@tonic-gate  * Algorithm management helper functions.
9627c478bd9Sstevel@tonic-gate  */
9637c478bd9Sstevel@tonic-gate extern boolean_t ipsec_valid_key_size(uint16_t, ipsec_alginfo_t *);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate /*
9667c478bd9Sstevel@tonic-gate  * Per-socket policy, for now, takes precedence... this priority value
9677c478bd9Sstevel@tonic-gate  * insures it.
9687c478bd9Sstevel@tonic-gate  */
9697c478bd9Sstevel@tonic-gate #define	IPSEC_PRIO_SOCKET		0x1000000
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate /* DDI initialization functions. */
9727c478bd9Sstevel@tonic-gate extern	boolean_t    ipsecesp_ddi_init(void);
9737c478bd9Sstevel@tonic-gate extern	boolean_t    ipsecah_ddi_init(void);
9747c478bd9Sstevel@tonic-gate extern	boolean_t    keysock_ddi_init(void);
9757c478bd9Sstevel@tonic-gate extern	boolean_t    spdsock_ddi_init(void);
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate extern	void    ipsecesp_ddi_destroy(void);
9787c478bd9Sstevel@tonic-gate extern	void    ipsecah_ddi_destroy(void);
9797c478bd9Sstevel@tonic-gate extern	void	keysock_ddi_destroy(void);
9807c478bd9Sstevel@tonic-gate extern	void    spdsock_ddi_destroy(void);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate /*
9837c478bd9Sstevel@tonic-gate  * AH- and ESP-specific functions that are called directly by other modules.
9847c478bd9Sstevel@tonic-gate  */
985f4b3ec61Sdh155122 extern void ipsecah_fill_defs(struct sadb_x_ecomb *, netstack_t *);
986f4b3ec61Sdh155122 extern void ipsecesp_fill_defs(struct sadb_x_ecomb *, netstack_t *);
987f4b3ec61Sdh155122 extern void ipsecah_algs_changed(netstack_t *);
988f4b3ec61Sdh155122 extern void ipsecesp_algs_changed(netstack_t *);
9897c478bd9Sstevel@tonic-gate extern void ipsecesp_init_funcs(ipsa_t *);
9907c478bd9Sstevel@tonic-gate extern void ipsecah_init_funcs(ipsa_t *);
991bd670b35SErik Nordmark extern mblk_t *ipsecah_icmp_error(mblk_t *, ip_recv_attr_t *);
992bd670b35SErik Nordmark extern mblk_t *ipsecesp_icmp_error(mblk_t *, ip_recv_attr_t *);
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate /*
9957c478bd9Sstevel@tonic-gate  * spdsock functions that are called directly by IP.
9967c478bd9Sstevel@tonic-gate  */
997f4b3ec61Sdh155122 extern void spdsock_update_pending_algs(netstack_t *);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate /*
10007c478bd9Sstevel@tonic-gate  * IP functions that are called from AH and ESP.
10017c478bd9Sstevel@tonic-gate  */
1002bd670b35SErik Nordmark extern boolean_t ipsec_outbound_sa(mblk_t *, ip_xmit_attr_t *, uint_t);
1003bd670b35SErik Nordmark extern mblk_t *ipsec_inbound_esp_sa(mblk_t *, ip_recv_attr_t *, esph_t **);
1004bd670b35SErik Nordmark extern mblk_t *ipsec_inbound_ah_sa(mblk_t *, ip_recv_attr_t *, ah_t **);
10058810c16bSdanmcd extern ipsec_policy_t *ipsec_find_policy_head(ipsec_policy_t *,
1006bd670b35SErik Nordmark     ipsec_policy_head_t *, int, ipsec_selector_t *);
10078810c16bSdanmcd 
1008f4b3ec61Sdh155122 /*
1009f4b3ec61Sdh155122  * IP dropper init/destroy.
1010f4b3ec61Sdh155122  */
1011f4b3ec61Sdh155122 void ip_drop_init(ipsec_stack_t *);
1012f4b3ec61Sdh155122 void ip_drop_destroy(ipsec_stack_t *);
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate /*
10158810c16bSdanmcd  * Common functions
10168810c16bSdanmcd  */
10178810c16bSdanmcd extern boolean_t ip_addr_match(uint8_t *, int, in6_addr_t *);
1018bd670b35SErik Nordmark extern boolean_t ipsec_label_match(ts_label_t *, ts_label_t *);
10198810c16bSdanmcd 
10208810c16bSdanmcd /*
10217c478bd9Sstevel@tonic-gate  * AH and ESP counters types.
10227c478bd9Sstevel@tonic-gate  */
10237c478bd9Sstevel@tonic-gate typedef uint32_t ah_counter;
10247c478bd9Sstevel@tonic-gate typedef uint32_t esp_counter;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate #endif
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate #endif	/* _INET_IPSEC_IMPL_H */
1033