10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51659Smarkfen * Common Development and Distribution License (the "License"). 61659Smarkfen * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 2210616SSebastien.Roy@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #ifndef _INET_IPSEC_IMPL_H 270Sstevel@tonic-gate #define _INET_IPSEC_IMPL_H 280Sstevel@tonic-gate 293448Sdh155122 #include <inet/ip.h> 303448Sdh155122 #include <inet/ipdrop.h> 313448Sdh155122 320Sstevel@tonic-gate #ifdef __cplusplus 330Sstevel@tonic-gate extern "C" { 340Sstevel@tonic-gate #endif 350Sstevel@tonic-gate 360Sstevel@tonic-gate #define IPSEC_CONF_SRC_ADDRESS 0 /* Source Address */ 370Sstevel@tonic-gate #define IPSEC_CONF_SRC_PORT 1 /* Source Port */ 380Sstevel@tonic-gate #define IPSEC_CONF_DST_ADDRESS 2 /* Dest Address */ 390Sstevel@tonic-gate #define IPSEC_CONF_DST_PORT 3 /* Dest Port */ 400Sstevel@tonic-gate #define IPSEC_CONF_SRC_MASK 4 /* Source Address Mask */ 410Sstevel@tonic-gate #define IPSEC_CONF_DST_MASK 5 /* Destination Address Mask */ 420Sstevel@tonic-gate #define IPSEC_CONF_ULP 6 /* Upper layer Port */ 430Sstevel@tonic-gate #define IPSEC_CONF_IPSEC_PROT 7 /* AH or ESP or AH_ESP */ 440Sstevel@tonic-gate #define IPSEC_CONF_IPSEC_AALGS 8 /* Auth Algorithms - MD5 etc. */ 450Sstevel@tonic-gate #define IPSEC_CONF_IPSEC_EALGS 9 /* Encr Algorithms - DES etc. */ 460Sstevel@tonic-gate #define IPSEC_CONF_IPSEC_EAALGS 10 /* Encr Algorithms - MD5 etc. */ 470Sstevel@tonic-gate #define IPSEC_CONF_IPSEC_SA 11 /* Shared or unique SA */ 480Sstevel@tonic-gate #define IPSEC_CONF_IPSEC_DIR 12 /* Direction of traffic */ 490Sstevel@tonic-gate #define IPSEC_CONF_ICMP_TYPE 13 /* ICMP type */ 500Sstevel@tonic-gate #define IPSEC_CONF_ICMP_CODE 14 /* ICMP code */ 513055Sdanmcd #define IPSEC_CONF_NEGOTIATE 15 /* Negotiation */ 523055Sdanmcd #define IPSEC_CONF_TUNNEL 16 /* Tunnel */ 530Sstevel@tonic-gate 540Sstevel@tonic-gate /* Type of an entry */ 550Sstevel@tonic-gate 560Sstevel@tonic-gate #define IPSEC_NTYPES 0x02 570Sstevel@tonic-gate #define IPSEC_TYPE_OUTBOUND 0x00 580Sstevel@tonic-gate #define IPSEC_TYPE_INBOUND 0x01 590Sstevel@tonic-gate 600Sstevel@tonic-gate /* Policy */ 610Sstevel@tonic-gate #define IPSEC_POLICY_APPLY 0x01 620Sstevel@tonic-gate #define IPSEC_POLICY_DISCARD 0x02 630Sstevel@tonic-gate #define IPSEC_POLICY_BYPASS 0x03 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* Shared or unique SA */ 660Sstevel@tonic-gate #define IPSEC_SHARED_SA 0x01 670Sstevel@tonic-gate #define IPSEC_UNIQUE_SA 0x02 680Sstevel@tonic-gate 694987Sdanmcd /* IPsec protocols and combinations */ 700Sstevel@tonic-gate #define IPSEC_AH_ONLY 0x01 710Sstevel@tonic-gate #define IPSEC_ESP_ONLY 0x02 720Sstevel@tonic-gate #define IPSEC_AH_ESP 0x03 730Sstevel@tonic-gate 746524Spwernau /* 756524Spwernau * Internally defined "any" algorithm. 766524Spwernau * Move to PF_KEY v3 when that RFC is released. 776524Spwernau */ 786524Spwernau #define SADB_AALG_ANY 255 796524Spwernau 800Sstevel@tonic-gate #ifdef _KERNEL 810Sstevel@tonic-gate 820Sstevel@tonic-gate #include <inet/common.h> 830Sstevel@tonic-gate #include <netinet/ip6.h> 840Sstevel@tonic-gate #include <netinet/icmp6.h> 850Sstevel@tonic-gate #include <net/pfkeyv2.h> 860Sstevel@tonic-gate #include <inet/ip.h> 870Sstevel@tonic-gate #include <inet/sadb.h> 880Sstevel@tonic-gate #include <inet/ipsecah.h> 890Sstevel@tonic-gate #include <inet/ipsecesp.h> 900Sstevel@tonic-gate #include <sys/crypto/common.h> 910Sstevel@tonic-gate #include <sys/crypto/api.h> 9281Ssommerfe #include <sys/avl.h> 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* 950Sstevel@tonic-gate * Maximum number of authentication algorithms (can be indexed by one byte 960Sstevel@tonic-gate * per PF_KEY and the IKE IPsec DOI. 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate #define MAX_AALGS 256 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* 1010Sstevel@tonic-gate * IPsec task queue constants. 1020Sstevel@tonic-gate */ 1030Sstevel@tonic-gate #define IPSEC_TASKQ_MIN 10 1040Sstevel@tonic-gate #define IPSEC_TASKQ_MAX 20 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * So we can access IPsec global variables that live in keysock.c. 1080Sstevel@tonic-gate */ 1093448Sdh155122 extern boolean_t keysock_extended_reg(netstack_t *); 1103448Sdh155122 extern uint32_t keysock_next_seq(netstack_t *); 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* 1130Sstevel@tonic-gate * Locking for ipsec policy rules: 1140Sstevel@tonic-gate * 1150Sstevel@tonic-gate * policy heads: system policy is static; per-conn polheads are dynamic, 1160Sstevel@tonic-gate * and refcounted (and inherited); use atomic refcounts and "don't let 1170Sstevel@tonic-gate * go with both hands". 1180Sstevel@tonic-gate * 1190Sstevel@tonic-gate * policy: refcounted; references from polhead, ipsec_out 1200Sstevel@tonic-gate * 1210Sstevel@tonic-gate * actions: refcounted; referenced from: action hash table, policy, ipsec_out 1220Sstevel@tonic-gate * selectors: refcounted; referenced from: selector hash table, policy. 1230Sstevel@tonic-gate */ 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate /* 1260Sstevel@tonic-gate * the following are inspired by, but not directly based on, 1270Sstevel@tonic-gate * some of the sys/queue.h type-safe pseudo-polymorphic macros 1280Sstevel@tonic-gate * found in BSD. 1290Sstevel@tonic-gate * 1300Sstevel@tonic-gate * XXX If we use these more generally, we'll have to make the names 1310Sstevel@tonic-gate * less generic (HASH_* will probably clobber other namespaces). 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate #define HASH_LOCK(table, hash) \ 1350Sstevel@tonic-gate mutex_enter(&(table)[hash].hash_lock) 1360Sstevel@tonic-gate #define HASH_UNLOCK(table, hash) \ 1370Sstevel@tonic-gate mutex_exit(&(table)[hash].hash_lock) 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate #define HASH_LOCKED(table, hash) \ 1400Sstevel@tonic-gate MUTEX_HELD(&(table)[hash].hash_lock) 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate #define HASH_ITERATE(var, field, table, hash) \ 1430Sstevel@tonic-gate var = table[hash].hash_head; var != NULL; var = var->field.hash_next 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate #define HASH_NEXT(var, field) \ 1460Sstevel@tonic-gate (var)->field.hash_next 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate #define HASH_INSERT(var, field, table, hash) \ 1490Sstevel@tonic-gate { \ 1500Sstevel@tonic-gate ASSERT(HASH_LOCKED(table, hash)); \ 1510Sstevel@tonic-gate (var)->field.hash_next = (table)[hash].hash_head; \ 1520Sstevel@tonic-gate (var)->field.hash_pp = &(table)[hash].hash_head; \ 1530Sstevel@tonic-gate (table)[hash].hash_head = var; \ 1540Sstevel@tonic-gate if ((var)->field.hash_next != NULL) \ 1550Sstevel@tonic-gate (var)->field.hash_next->field.hash_pp = \ 1560Sstevel@tonic-gate &((var)->field.hash_next); \ 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 15981Ssommerfe 16081Ssommerfe #define HASH_UNCHAIN(var, field, table, hash) \ 16181Ssommerfe { \ 16281Ssommerfe ASSERT(MUTEX_HELD(&(table)[hash].hash_lock)); \ 16381Ssommerfe HASHLIST_UNCHAIN(var, field); \ 16481Ssommerfe } 16581Ssommerfe 16681Ssommerfe #define HASHLIST_INSERT(var, field, head) \ 16781Ssommerfe { \ 16881Ssommerfe (var)->field.hash_next = head; \ 16981Ssommerfe (var)->field.hash_pp = &(head); \ 17081Ssommerfe head = var; \ 17181Ssommerfe if ((var)->field.hash_next != NULL) \ 17281Ssommerfe (var)->field.hash_next->field.hash_pp = \ 17381Ssommerfe &((var)->field.hash_next); \ 17481Ssommerfe } 17581Ssommerfe 17681Ssommerfe #define HASHLIST_UNCHAIN(var, field) \ 17781Ssommerfe { \ 17881Ssommerfe *var->field.hash_pp = var->field.hash_next; \ 17981Ssommerfe if (var->field.hash_next) \ 18081Ssommerfe var->field.hash_next->field.hash_pp = \ 18181Ssommerfe var->field.hash_pp; \ 18281Ssommerfe HASH_NULL(var, field); \ 18381Ssommerfe } 18481Ssommerfe 18581Ssommerfe 18681Ssommerfe #define HASH_NULL(var, field) \ 18781Ssommerfe { \ 18881Ssommerfe var->field.hash_next = NULL; \ 18981Ssommerfe var->field.hash_pp = NULL; \ 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate #define HASH_LINK(fieldname, type) \ 1930Sstevel@tonic-gate struct { \ 1940Sstevel@tonic-gate type *hash_next; \ 1950Sstevel@tonic-gate type **hash_pp; \ 1960Sstevel@tonic-gate } fieldname 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate #define HASH_HEAD(tag) \ 2000Sstevel@tonic-gate struct { \ 2010Sstevel@tonic-gate struct tag *hash_head; \ 2020Sstevel@tonic-gate kmutex_t hash_lock; \ 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate 2053448Sdh155122 2060Sstevel@tonic-gate typedef struct ipsec_policy_s ipsec_policy_t; 2070Sstevel@tonic-gate 20881Ssommerfe typedef HASH_HEAD(ipsec_policy_s) ipsec_policy_hash_t; 20981Ssommerfe 2100Sstevel@tonic-gate /* 2110Sstevel@tonic-gate * When adding new fields to ipsec_prot_t, make sure to update 2120Sstevel@tonic-gate * ipsec_in_to_out_action() as well as other code in spd.c 2130Sstevel@tonic-gate */ 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate typedef struct ipsec_prot 2160Sstevel@tonic-gate { 2170Sstevel@tonic-gate unsigned int 2180Sstevel@tonic-gate ipp_use_ah : 1, 2190Sstevel@tonic-gate ipp_use_esp : 1, 2200Sstevel@tonic-gate ipp_use_se : 1, 2210Sstevel@tonic-gate ipp_use_unique : 1, 2220Sstevel@tonic-gate ipp_use_espa : 1, 2230Sstevel@tonic-gate ipp_pad : 27; 2240Sstevel@tonic-gate uint8_t ipp_auth_alg; /* DOI number */ 2250Sstevel@tonic-gate uint8_t ipp_encr_alg; /* DOI number */ 2260Sstevel@tonic-gate uint8_t ipp_esp_auth_alg; /* DOI number */ 2270Sstevel@tonic-gate uint16_t ipp_ah_minbits; /* AH: min keylen */ 2280Sstevel@tonic-gate uint16_t ipp_ah_maxbits; /* AH: max keylen */ 2290Sstevel@tonic-gate uint16_t ipp_espe_minbits; /* ESP encr: min keylen */ 2300Sstevel@tonic-gate uint16_t ipp_espe_maxbits; /* ESP encr: max keylen */ 2310Sstevel@tonic-gate uint16_t ipp_espa_minbits; /* ESP auth: min keylen */ 2320Sstevel@tonic-gate uint16_t ipp_espa_maxbits; /* ESP auth: max keylen */ 2330Sstevel@tonic-gate uint32_t ipp_km_proto; /* key mgmt protocol */ 2340Sstevel@tonic-gate uint32_t ipp_km_cookie; /* key mgmt cookie */ 2350Sstevel@tonic-gate uint32_t ipp_replay_depth; /* replay window */ 2360Sstevel@tonic-gate /* XXX add lifetimes */ 2370Sstevel@tonic-gate } ipsec_prot_t; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate #define IPSEC_MAX_KEYBITS (0xffff) 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate /* 2420Sstevel@tonic-gate * An individual policy action, possibly a member of a chain. 2430Sstevel@tonic-gate * 24481Ssommerfe * Action chains may be shared between multiple policy rules. 2450Sstevel@tonic-gate * 2460Sstevel@tonic-gate * With one exception (IPSEC_POLICY_LOG), a chain consists of an 2470Sstevel@tonic-gate * ordered list of alternative ways to handle a packet. 2480Sstevel@tonic-gate * 2490Sstevel@tonic-gate * All actions are also "interned" into a hash table (to allow 2500Sstevel@tonic-gate * multiple rules with the same action chain to share one copy in 2510Sstevel@tonic-gate * memory). 2520Sstevel@tonic-gate */ 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate typedef struct ipsec_act 2550Sstevel@tonic-gate { 2560Sstevel@tonic-gate uint8_t ipa_type; 2570Sstevel@tonic-gate uint8_t ipa_log; 2580Sstevel@tonic-gate union 2590Sstevel@tonic-gate { 2600Sstevel@tonic-gate ipsec_prot_t ipau_apply; 2610Sstevel@tonic-gate uint8_t ipau_reject_type; 2620Sstevel@tonic-gate uint32_t ipau_resolve_id; /* magic cookie */ 2630Sstevel@tonic-gate uint8_t ipau_log_type; 2640Sstevel@tonic-gate } ipa_u; 2650Sstevel@tonic-gate #define ipa_apply ipa_u.ipau_apply 2660Sstevel@tonic-gate #define ipa_reject_type ipa_u.ipau_reject_type 2670Sstevel@tonic-gate #define ipa_log_type ipa_u.ipau_log_type 2680Sstevel@tonic-gate #define ipa_resolve_type ipa_u.ipau_resolve_type 2690Sstevel@tonic-gate } ipsec_act_t; 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate #define IPSEC_ACT_APPLY 0x01 /* match IPSEC_POLICY_APPLY */ 2720Sstevel@tonic-gate #define IPSEC_ACT_DISCARD 0x02 /* match IPSEC_POLICY_DISCARD */ 2730Sstevel@tonic-gate #define IPSEC_ACT_BYPASS 0x03 /* match IPSEC_POLICY_BYPASS */ 2740Sstevel@tonic-gate #define IPSEC_ACT_REJECT 0x04 2750Sstevel@tonic-gate #define IPSEC_ACT_CLEAR 0x05 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate typedef struct ipsec_action_s 2780Sstevel@tonic-gate { 2790Sstevel@tonic-gate HASH_LINK(ipa_hash, struct ipsec_action_s); 2800Sstevel@tonic-gate struct ipsec_action_s *ipa_next; /* next alternative */ 2810Sstevel@tonic-gate uint32_t ipa_refs; /* refcount */ 2820Sstevel@tonic-gate ipsec_act_t ipa_act; 2830Sstevel@tonic-gate /* 2840Sstevel@tonic-gate * The following bits are equivalent to an OR of bits included in the 2850Sstevel@tonic-gate * ipau_apply fields of this and subsequent actions in an 2860Sstevel@tonic-gate * action chain; this is an optimization for the sake of 2870Sstevel@tonic-gate * ipsec_out_process() in ip.c and a few other places. 2880Sstevel@tonic-gate */ 2890Sstevel@tonic-gate unsigned int 2900Sstevel@tonic-gate ipa_hval: 8, 2910Sstevel@tonic-gate ipa_allow_clear:1, /* rule allows cleartext? */ 2920Sstevel@tonic-gate ipa_want_ah:1, /* an action wants ah */ 2930Sstevel@tonic-gate ipa_want_esp:1, /* an action wants esp */ 2940Sstevel@tonic-gate ipa_want_se:1, /* an action wants se */ 2950Sstevel@tonic-gate ipa_want_unique:1, /* want unique sa's */ 2960Sstevel@tonic-gate ipa_pad:19; 2970Sstevel@tonic-gate uint32_t ipa_ovhd; /* per-packet encap ovhd */ 2980Sstevel@tonic-gate } ipsec_action_t; 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate #define IPACT_REFHOLD(ipa) { \ 3010Sstevel@tonic-gate atomic_add_32(&(ipa)->ipa_refs, 1); \ 3020Sstevel@tonic-gate ASSERT((ipa)->ipa_refs != 0); \ 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate #define IPACT_REFRELE(ipa) { \ 3050Sstevel@tonic-gate ASSERT((ipa)->ipa_refs != 0); \ 3060Sstevel@tonic-gate membar_exit(); \ 3070Sstevel@tonic-gate if (atomic_add_32_nv(&(ipa)->ipa_refs, -1) == 0) \ 3080Sstevel@tonic-gate ipsec_action_free(ipa); \ 3090Sstevel@tonic-gate (ipa) = 0; \ 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* 3133448Sdh155122 * For now, use a trivially sized hash table for actions. 3143448Sdh155122 * In the future we can add the structure canonicalization necessary 3153448Sdh155122 * to get the hash function to behave correctly.. 3163448Sdh155122 */ 3173448Sdh155122 #define IPSEC_ACTION_HASH_SIZE 1 3183448Sdh155122 3193448Sdh155122 /* 3203055Sdanmcd * Merged address structure, for cheezy address-family independent 3210Sstevel@tonic-gate * matches in policy code. 3220Sstevel@tonic-gate */ 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate typedef union ipsec_addr 3250Sstevel@tonic-gate { 3260Sstevel@tonic-gate in6_addr_t ipsad_v6; 3270Sstevel@tonic-gate in_addr_t ipsad_v4; 3280Sstevel@tonic-gate } ipsec_addr_t; 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate /* 3310Sstevel@tonic-gate * ipsec selector set, as used by the kernel policy structures. 3320Sstevel@tonic-gate * Note that that we specify "local" and "remote" 3330Sstevel@tonic-gate * rather than "source" and "destination", which allows the selectors 3340Sstevel@tonic-gate * for symmetric policy rules to be shared between inbound and 3350Sstevel@tonic-gate * outbound rules. 3360Sstevel@tonic-gate * 3370Sstevel@tonic-gate * "local" means "destination" on inbound, and "source" on outbound. 3380Sstevel@tonic-gate * "remote" means "source" on inbound, and "destination" on outbound. 3390Sstevel@tonic-gate * XXX if we add a fifth policy enforcement point for forwarded packets, 3400Sstevel@tonic-gate * what do we do? 3410Sstevel@tonic-gate * 3420Sstevel@tonic-gate * The ipsl_valid mask is not done as a bitfield; this is so we 3430Sstevel@tonic-gate * can use "ffs()" to find the "most interesting" valid tag. 3440Sstevel@tonic-gate * 3450Sstevel@tonic-gate * XXX should we have multiple types for space-conservation reasons? 3460Sstevel@tonic-gate * (v4 vs v6? prefix vs. range)? 3470Sstevel@tonic-gate */ 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate typedef struct ipsec_selkey 3500Sstevel@tonic-gate { 3510Sstevel@tonic-gate uint32_t ipsl_valid; /* bitmask of valid entries */ 3520Sstevel@tonic-gate #define IPSL_REMOTE_ADDR 0x00000001 3530Sstevel@tonic-gate #define IPSL_LOCAL_ADDR 0x00000002 3540Sstevel@tonic-gate #define IPSL_REMOTE_PORT 0x00000004 3550Sstevel@tonic-gate #define IPSL_LOCAL_PORT 0x00000008 3560Sstevel@tonic-gate #define IPSL_PROTOCOL 0x00000010 3570Sstevel@tonic-gate #define IPSL_ICMP_TYPE 0x00000020 3580Sstevel@tonic-gate #define IPSL_ICMP_CODE 0x00000040 3590Sstevel@tonic-gate #define IPSL_IPV6 0x00000080 3600Sstevel@tonic-gate #define IPSL_IPV4 0x00000100 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate #define IPSL_WILDCARD 0x0000007f 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate ipsec_addr_t ipsl_local; 3650Sstevel@tonic-gate ipsec_addr_t ipsl_remote; 3660Sstevel@tonic-gate uint16_t ipsl_lport; 3670Sstevel@tonic-gate uint16_t ipsl_rport; 3680Sstevel@tonic-gate /* 3690Sstevel@tonic-gate * ICMP type and code selectors. Both have an end value to 3700Sstevel@tonic-gate * specify ranges, or * and *_end are equal for a single 3710Sstevel@tonic-gate * value 3720Sstevel@tonic-gate */ 3730Sstevel@tonic-gate uint8_t ipsl_icmp_type; 3740Sstevel@tonic-gate uint8_t ipsl_icmp_type_end; 3750Sstevel@tonic-gate uint8_t ipsl_icmp_code; 3760Sstevel@tonic-gate uint8_t ipsl_icmp_code_end; 37781Ssommerfe 3780Sstevel@tonic-gate uint8_t ipsl_proto; /* ip payload type */ 3790Sstevel@tonic-gate uint8_t ipsl_local_pfxlen; /* #bits of prefix */ 3800Sstevel@tonic-gate uint8_t ipsl_remote_pfxlen; /* #bits of prefix */ 3810Sstevel@tonic-gate uint8_t ipsl_mbz; 38281Ssommerfe 3833055Sdanmcd /* Insert new elements above this line */ 3843055Sdanmcd uint32_t ipsl_pol_hval; 3853055Sdanmcd uint32_t ipsl_sel_hval; 3860Sstevel@tonic-gate } ipsec_selkey_t; 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate typedef struct ipsec_sel 3890Sstevel@tonic-gate { 3900Sstevel@tonic-gate HASH_LINK(ipsl_hash, struct ipsec_sel); 3910Sstevel@tonic-gate uint32_t ipsl_refs; /* # refs to this sel */ 3920Sstevel@tonic-gate ipsec_selkey_t ipsl_key; /* actual selector guts */ 3930Sstevel@tonic-gate } ipsec_sel_t; 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate /* 39681Ssommerfe * One policy rule. This will be linked into a single hash chain bucket in 39781Ssommerfe * the parent rule structure. If the selector is simple enough to 39881Ssommerfe * allow hashing, it gets filed under ipsec_policy_root_t->ipr_hash. 39981Ssommerfe * Otherwise it goes onto a linked list in ipsec_policy_root_t->ipr_nonhash[af] 4000Sstevel@tonic-gate * 40181Ssommerfe * In addition, we file the rule into an avl tree keyed by the rule index. 40281Ssommerfe * (Duplicate rules are permitted; the comparison function breaks ties). 4030Sstevel@tonic-gate */ 4040Sstevel@tonic-gate struct ipsec_policy_s 4050Sstevel@tonic-gate { 40681Ssommerfe HASH_LINK(ipsp_hash, struct ipsec_policy_s); 40781Ssommerfe avl_node_t ipsp_byid; 4080Sstevel@tonic-gate uint64_t ipsp_index; /* unique id */ 4090Sstevel@tonic-gate uint32_t ipsp_prio; /* rule priority */ 4100Sstevel@tonic-gate uint32_t ipsp_refs; 4110Sstevel@tonic-gate ipsec_sel_t *ipsp_sel; /* selector set (shared) */ 4120Sstevel@tonic-gate ipsec_action_t *ipsp_act; /* action (may be shared) */ 413*11042SErik.Nordmark@Sun.COM netstack_t *ipsp_netstack; /* No netstack_hold */ 4140Sstevel@tonic-gate }; 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate #define IPPOL_REFHOLD(ipp) { \ 4170Sstevel@tonic-gate atomic_add_32(&(ipp)->ipsp_refs, 1); \ 4180Sstevel@tonic-gate ASSERT((ipp)->ipsp_refs != 0); \ 4190Sstevel@tonic-gate } 420*11042SErik.Nordmark@Sun.COM #define IPPOL_REFRELE(ipp) { \ 4210Sstevel@tonic-gate ASSERT((ipp)->ipsp_refs != 0); \ 4220Sstevel@tonic-gate membar_exit(); \ 4230Sstevel@tonic-gate if (atomic_add_32_nv(&(ipp)->ipsp_refs, -1) == 0) \ 424*11042SErik.Nordmark@Sun.COM ipsec_policy_free(ipp); \ 4250Sstevel@tonic-gate (ipp) = 0; \ 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate 428*11042SErik.Nordmark@Sun.COM #define IPPOL_UNCHAIN(php, ip) \ 429*11042SErik.Nordmark@Sun.COM HASHLIST_UNCHAIN((ip), ipsp_hash); \ 430*11042SErik.Nordmark@Sun.COM avl_remove(&(php)->iph_rulebyid, (ip)); \ 431*11042SErik.Nordmark@Sun.COM IPPOL_REFRELE(ip); 4323055Sdanmcd 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * Policy ruleset. One per (protocol * direction) for system policy. 4350Sstevel@tonic-gate */ 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate #define IPSEC_AF_V4 0 4380Sstevel@tonic-gate #define IPSEC_AF_V6 1 4390Sstevel@tonic-gate #define IPSEC_NAF 2 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate typedef struct ipsec_policy_root_s 4420Sstevel@tonic-gate { 44381Ssommerfe ipsec_policy_t *ipr_nonhash[IPSEC_NAF]; 44481Ssommerfe int ipr_nchains; 44581Ssommerfe ipsec_policy_hash_t *ipr_hash; 4460Sstevel@tonic-gate } ipsec_policy_root_t; 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate /* 4490Sstevel@tonic-gate * Policy head. One for system policy; there may also be one present 4500Sstevel@tonic-gate * on ill_t's with interface-specific policy, as well as one present 4510Sstevel@tonic-gate * for sockets with per-socket policy allocated. 4520Sstevel@tonic-gate */ 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate typedef struct ipsec_policy_head_s 4550Sstevel@tonic-gate { 4560Sstevel@tonic-gate uint32_t iph_refs; 4570Sstevel@tonic-gate krwlock_t iph_lock; 4580Sstevel@tonic-gate uint64_t iph_gen; /* generation number */ 4590Sstevel@tonic-gate ipsec_policy_root_t iph_root[IPSEC_NTYPES]; 46081Ssommerfe avl_tree_t iph_rulebyid; 4610Sstevel@tonic-gate } ipsec_policy_head_t; 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate #define IPPH_REFHOLD(iph) { \ 4640Sstevel@tonic-gate atomic_add_32(&(iph)->iph_refs, 1); \ 4650Sstevel@tonic-gate ASSERT((iph)->iph_refs != 0); \ 4660Sstevel@tonic-gate } 4673448Sdh155122 #define IPPH_REFRELE(iph, ns) { \ 4680Sstevel@tonic-gate ASSERT((iph)->iph_refs != 0); \ 4690Sstevel@tonic-gate membar_exit(); \ 4700Sstevel@tonic-gate if (atomic_add_32_nv(&(iph)->iph_refs, -1) == 0) \ 4713448Sdh155122 ipsec_polhead_free(iph, ns); \ 4720Sstevel@tonic-gate (iph) = 0; \ 4730Sstevel@tonic-gate } 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate /* 4763055Sdanmcd * IPsec fragment related structures 4773055Sdanmcd */ 4783055Sdanmcd 4793055Sdanmcd typedef struct ipsec_fragcache_entry { 4803055Sdanmcd struct ipsec_fragcache_entry *itpfe_next; /* hash list chain */ 4813055Sdanmcd mblk_t *itpfe_fraglist; /* list of fragments */ 4823055Sdanmcd time_t itpfe_exp; /* time when entry is stale */ 4833055Sdanmcd int itpfe_depth; /* # of fragments in list */ 4843055Sdanmcd ipsec_addr_t itpfe_frag_src; 4853055Sdanmcd ipsec_addr_t itpfe_frag_dst; 4863055Sdanmcd #define itpfe_src itpfe_frag_src.ipsad_v4 4873055Sdanmcd #define itpfe_src6 itpfe_frag_src.ipsad_v6 4883055Sdanmcd #define itpfe_dst itpfe_frag_dst.ipsad_v4 4893055Sdanmcd #define itpfe_dst6 itpfe_frag_dst.ipsad_v6 4903055Sdanmcd uint32_t itpfe_id; /* IP datagram ID */ 4913055Sdanmcd uint8_t itpfe_proto; /* IP Protocol */ 4923055Sdanmcd uint8_t itpfe_last; /* Last packet */ 4933055Sdanmcd } ipsec_fragcache_entry_t; 4943055Sdanmcd 4953055Sdanmcd typedef struct ipsec_fragcache { 4963055Sdanmcd kmutex_t itpf_lock; 4973055Sdanmcd struct ipsec_fragcache_entry **itpf_ptr; 4983055Sdanmcd struct ipsec_fragcache_entry *itpf_freelist; 4993055Sdanmcd time_t itpf_expire_hint; /* time when oldest entry is stale */ 5003055Sdanmcd } ipsec_fragcache_t; 5013055Sdanmcd 5023055Sdanmcd /* 5033055Sdanmcd * Tunnel policies. We keep a minature of the transport-mode/global policy 5043055Sdanmcd * per each tunnel instance. 5053055Sdanmcd * 5063055Sdanmcd * People who need both an itp held down AND one of its polheads need to 5073055Sdanmcd * first lock the itp, THEN the polhead, otherwise deadlock WILL occur. 5083055Sdanmcd */ 5093055Sdanmcd typedef struct ipsec_tun_pol_s { 5103055Sdanmcd avl_node_t itp_node; 5113055Sdanmcd kmutex_t itp_lock; 5123055Sdanmcd uint64_t itp_next_policy_index; 5133055Sdanmcd ipsec_policy_head_t *itp_policy; 5143055Sdanmcd ipsec_policy_head_t *itp_inactive; 5153055Sdanmcd uint32_t itp_flags; 5163055Sdanmcd uint32_t itp_refcnt; 5173055Sdanmcd char itp_name[LIFNAMSIZ]; 5183055Sdanmcd ipsec_fragcache_t itp_fragcache; 5193055Sdanmcd } ipsec_tun_pol_t; 5203055Sdanmcd /* NOTE - Callers (tun code) synchronize their own instances for these flags. */ 5213055Sdanmcd #define ITPF_P_ACTIVE 0x1 /* Are we using IPsec right now? */ 5223055Sdanmcd #define ITPF_P_TUNNEL 0x2 /* Negotiate tunnel-mode */ 5233055Sdanmcd /* Optimization -> Do we have per-port security entries in this polhead? */ 5243055Sdanmcd #define ITPF_P_PER_PORT_SECURITY 0x4 5253055Sdanmcd #define ITPF_PFLAGS 0x7 5263055Sdanmcd #define ITPF_SHIFT 3 5273055Sdanmcd 5283055Sdanmcd #define ITPF_I_ACTIVE 0x8 /* Is the inactive using IPsec right now? */ 5293055Sdanmcd #define ITPF_I_TUNNEL 0x10 /* Negotiate tunnel-mode (on inactive) */ 5303055Sdanmcd /* Optimization -> Do we have per-port security entries in this polhead? */ 5313055Sdanmcd #define ITPF_I_PER_PORT_SECURITY 0x20 5323055Sdanmcd #define ITPF_IFLAGS 0x38 5333055Sdanmcd 5343055Sdanmcd /* NOTE: f cannot be an expression. */ 5353055Sdanmcd #define ITPF_CLONE(f) (f) = (((f) & ITPF_PFLAGS) | \ 5363055Sdanmcd (((f) & ITPF_PFLAGS) << ITPF_SHIFT)); 5373055Sdanmcd #define ITPF_SWAP(f) (f) = ((((f) & ITPF_PFLAGS) << ITPF_SHIFT) | \ 5383055Sdanmcd (((f) & ITPF_IFLAGS) >> ITPF_SHIFT)) 5393055Sdanmcd 5403055Sdanmcd #define ITP_P_ISACTIVE(itp, iph) ((itp)->itp_flags & \ 5413055Sdanmcd (((itp)->itp_policy == (iph)) ? ITPF_P_ACTIVE : ITPF_I_ACTIVE)) 5423055Sdanmcd 5433055Sdanmcd #define ITP_P_ISTUNNEL(itp, iph) ((itp)->itp_flags & \ 5443055Sdanmcd (((itp)->itp_policy == (iph)) ? ITPF_P_TUNNEL : ITPF_I_TUNNEL)) 5453055Sdanmcd 5463055Sdanmcd #define ITP_P_ISPERPORT(itp, iph) ((itp)->itp_flags & \ 5473055Sdanmcd (((itp)->itp_policy == (iph)) ? ITPF_P_PER_PORT_SECURITY : \ 5483055Sdanmcd ITPF_I_PER_PORT_SECURITY)) 5493055Sdanmcd 5503055Sdanmcd #define ITP_REFHOLD(itp) { \ 5513055Sdanmcd atomic_add_32(&((itp)->itp_refcnt), 1); \ 5523055Sdanmcd ASSERT((itp)->itp_refcnt != 0); \ 5533055Sdanmcd } 5543055Sdanmcd 5553448Sdh155122 #define ITP_REFRELE(itp, ns) { \ 5563055Sdanmcd ASSERT((itp)->itp_refcnt != 0); \ 5573055Sdanmcd membar_exit(); \ 5583055Sdanmcd if (atomic_add_32_nv(&((itp)->itp_refcnt), -1) == 0) \ 5593448Sdh155122 itp_free(itp, ns); \ 5603055Sdanmcd } 5613055Sdanmcd 5623055Sdanmcd /* 5630Sstevel@tonic-gate * Certificate identity. 5640Sstevel@tonic-gate */ 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate typedef struct ipsid_s 5670Sstevel@tonic-gate { 5680Sstevel@tonic-gate struct ipsid_s *ipsid_next; 5690Sstevel@tonic-gate struct ipsid_s **ipsid_ptpn; 5700Sstevel@tonic-gate uint32_t ipsid_refcnt; 5710Sstevel@tonic-gate int ipsid_type; /* id type */ 5720Sstevel@tonic-gate char *ipsid_cid; /* certificate id string */ 5730Sstevel@tonic-gate } ipsid_t; 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate /* 5760Sstevel@tonic-gate * ipsid_t reference hold/release macros, just like ipsa versions. 5770Sstevel@tonic-gate */ 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate #define IPSID_REFHOLD(ipsid) { \ 5800Sstevel@tonic-gate atomic_add_32(&(ipsid)->ipsid_refcnt, 1); \ 5810Sstevel@tonic-gate ASSERT((ipsid)->ipsid_refcnt != 0); \ 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate /* 5850Sstevel@tonic-gate * Decrement the reference count on the ID. Someone else will clean up 5860Sstevel@tonic-gate * after us later. 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate #define IPSID_REFRELE(ipsid) { \ 5900Sstevel@tonic-gate membar_exit(); \ 5910Sstevel@tonic-gate atomic_add_32(&(ipsid)->ipsid_refcnt, -1); \ 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate /* 5950Sstevel@tonic-gate * Following are the estimates of what the maximum AH and ESP header size 5960Sstevel@tonic-gate * would be. This is used to tell the upper layer the right value of MSS 5970Sstevel@tonic-gate * it should use without consulting AH/ESP. If the size is something 5980Sstevel@tonic-gate * different from this, ULP will learn the right one through 5990Sstevel@tonic-gate * ICMP_FRAGMENTATION_NEEDED messages generated locally. 6000Sstevel@tonic-gate * 6016126Sdanmcd * AH : 12 bytes of constant header + 32 bytes of ICV checksum (SHA-512). 6026126Sdanmcd */ 6036126Sdanmcd #define IPSEC_MAX_AH_HDR_SIZE (44) 6046126Sdanmcd 6056126Sdanmcd /* 6066126Sdanmcd * ESP : Is a bit more complex... 6076126Sdanmcd * 6086126Sdanmcd * A system of one inequality and one equation MUST be solved for proper ESP 6096126Sdanmcd * overhead. The inequality is: 6106126Sdanmcd * 6116126Sdanmcd * MTU - sizeof (IP header + options) >= 6126126Sdanmcd * sizeof (esph_t) + sizeof (IV or ctr) + data-size + 2 + ICV 6136126Sdanmcd * 6146126Sdanmcd * IV or counter is almost always the cipher's block size. The equation is: 6156126Sdanmcd * 6166126Sdanmcd * data-size % block-size = (block-size - 2) 6176126Sdanmcd * 6186126Sdanmcd * so we can put as much data into the datagram as possible. If we are 6196126Sdanmcd * pessimistic and include our largest overhead cipher (AES) and hash 6206126Sdanmcd * (SHA-512), and assume 1500-byte MTU minus IPv4 overhead of 20 bytes, we get: 6210Sstevel@tonic-gate * 6226126Sdanmcd * 1480 >= 8 + 16 + data-size + 2 + 32 6236126Sdanmcd * 1480 >= 58 + data-size 6246126Sdanmcd * 1422 >= data-size, 1422 % 16 = 14, so 58 is the overhead! 6256126Sdanmcd * 6266126Sdanmcd * But, let's re-run the numbers with the same algorithms, but with an IPv6 6276126Sdanmcd * header: 6286126Sdanmcd * 6296126Sdanmcd * 1460 >= 58 + data-size 6306126Sdanmcd * 1402 >= data-size, 1402 % 16 = 10, meaning shrink to 1390 to get 14, 6316126Sdanmcd * 6326126Sdanmcd * which means the overhead is now 70. 6336126Sdanmcd * 6346126Sdanmcd * Hmmm... IPv4 headers can never be anything other than multiples of 4-bytes, 6356126Sdanmcd * and IPv6 ones can never be anything other than multiples of 8-bytes. We've 6366126Sdanmcd * seen overheads of 58 and 70. 58 % 16 == 10, and 70 % 16 == 6. IPv4 could 6376126Sdanmcd * force us to have 62 ( % 16 == 14) or 66 ( % 16 == 2), or IPv6 could force us 6386126Sdanmcd * to have 78 ( % 16 = 14). Let's compute IPv6 + 8-bytes of options: 6396126Sdanmcd * 6406126Sdanmcd * 1452 >= 58 + data-size 6416126Sdanmcd * 1394 >= data-size, 1394 % 16 = 2, meaning shrink to 1390 to get 14, 6420Sstevel@tonic-gate * 6436126Sdanmcd * Aha! The "ESP overhead" shrinks to 62 (70 - 8). This is good. Let's try 6446126Sdanmcd * IPv4 + 8 bytes of IPv4 options: 6456126Sdanmcd * 6466126Sdanmcd * 1472 >= 58 + data-size 6476126Sdanmcd * 1414 >= data-size, 1414 % 16 = 6, meaning shrink to 1406, 6486126Sdanmcd * 6496126Sdanmcd * meaning 66 is the overhead. Let's try 12 bytes: 6506126Sdanmcd * 6516126Sdanmcd * 1468 >= 58 + data-size 6526126Sdanmcd * 1410 >= data-size, 1410 % 16 = 2, meaning also shrink to 1406, 6536126Sdanmcd * 6546126Sdanmcd * meaning 62 is the overhead. How about 16 bytes? 6556126Sdanmcd * 6566126Sdanmcd * 1464 >= 58 + data-size 6576126Sdanmcd * 1406 >= data-size, 1402 % 16 = 14, which is great! 6586126Sdanmcd * 6596126Sdanmcd * this means 58 is the overhead. If I wrap and add 20 bytes, it looks just 6606126Sdanmcd * like IPv6's 70 bytes. If I add 24, we go back to 66 bytes. 6616126Sdanmcd * 6626126Sdanmcd * So picking 70 is a sensible, conservative default. Optimal calculations 6636126Sdanmcd * will depend on knowing pre-ESP header length (called "divpoint" in the ESP 6646126Sdanmcd * code), which could be cached in the conn_t for connected endpoints, or 6656126Sdanmcd * which must be computed on every datagram otherwise. 6660Sstevel@tonic-gate */ 6676126Sdanmcd #define IPSEC_MAX_ESP_HDR_SIZE (70) 6680Sstevel@tonic-gate 6696126Sdanmcd /* 6706126Sdanmcd * Alternate, when we know the crypto block size via the SA. Assume an ICV on 6716126Sdanmcd * the SA. Use: 6726126Sdanmcd * 6736126Sdanmcd * sizeof (esph_t) + 2 * (sizeof (IV/counter)) - 2 + sizeof (ICV). The "-2" 6746126Sdanmcd * discounts the overhead of the pad + padlen that gets swallowed up by the 6756126Sdanmcd * second (theoretically all-pad) cipher-block. If you use our examples of 6766126Sdanmcd * AES and SHA512, you get: 6776126Sdanmcd * 6786126Sdanmcd * 8 + 32 - 2 + 32 == 70. 6796126Sdanmcd * 6806126Sdanmcd * Which is our pre-computed maximum above. 6816126Sdanmcd */ 6826126Sdanmcd #include <inet/ipsecesp.h> 6836126Sdanmcd #define IPSEC_BASE_ESP_HDR_SIZE(sa) \ 6846126Sdanmcd (sizeof (esph_t) + ((sa)->ipsa_iv_len << 1) - 2 + (sa)->ipsa_mac_len) 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate /* 6873448Sdh155122 * Identity hash table. 6883448Sdh155122 * 6893448Sdh155122 * Identities are refcounted and "interned" into the hash table. 6903448Sdh155122 * Only references coming from other objects (SA's, latching state) 6913448Sdh155122 * are counted in ipsid_refcnt. 6923448Sdh155122 * 6933448Sdh155122 * Locking: IPSID_REFHOLD is safe only when (a) the object's hash bucket 6943448Sdh155122 * is locked, (b) we know that the refcount must be > 0. 6953448Sdh155122 * 6963448Sdh155122 * The ipsid_next and ipsid_ptpn fields are only to be referenced or 6973448Sdh155122 * modified when the bucket lock is held; in particular, we only 6983448Sdh155122 * delete objects while holding the bucket lock, and we only increase 6993448Sdh155122 * the refcount from 0 to 1 while the bucket lock is held. 7003448Sdh155122 */ 7013448Sdh155122 7023448Sdh155122 #define IPSID_HASHSIZE 64 7033448Sdh155122 7043448Sdh155122 typedef struct ipsif_s 7053448Sdh155122 { 7063448Sdh155122 ipsid_t *ipsif_head; 7073448Sdh155122 kmutex_t ipsif_lock; 7083448Sdh155122 } ipsif_t; 7093448Sdh155122 710*11042SErik.Nordmark@Sun.COM /* 711*11042SErik.Nordmark@Sun.COM * For call to the kernel crypto framework. State needed during 712*11042SErik.Nordmark@Sun.COM * the execution of a crypto request. 713*11042SErik.Nordmark@Sun.COM */ 714*11042SErik.Nordmark@Sun.COM typedef struct ipsec_crypto_s { 715*11042SErik.Nordmark@Sun.COM size_t ic_skip_len; /* len to skip for AH auth */ 716*11042SErik.Nordmark@Sun.COM crypto_data_t ic_crypto_data; /* single op crypto data */ 717*11042SErik.Nordmark@Sun.COM crypto_dual_data_t ic_crypto_dual_data; /* for dual ops */ 718*11042SErik.Nordmark@Sun.COM crypto_data_t ic_crypto_mac; /* to store the MAC */ 719*11042SErik.Nordmark@Sun.COM ipsa_cm_mech_t ic_cmm; 720*11042SErik.Nordmark@Sun.COM } ipsec_crypto_t; 7213448Sdh155122 7223448Sdh155122 /* 7234987Sdanmcd * IPsec stack instances 7243448Sdh155122 */ 7253448Sdh155122 struct ipsec_stack { 7263448Sdh155122 netstack_t *ipsec_netstack; /* Common netstack */ 7273448Sdh155122 7283448Sdh155122 /* Packet dropper for IP IPsec processing failures */ 7293448Sdh155122 ipdropper_t ipsec_dropper; 7303448Sdh155122 7313448Sdh155122 /* From spd.c */ 7323448Sdh155122 /* 7333448Sdh155122 * Policy rule index generator. We assume this won't wrap in the 7343448Sdh155122 * lifetime of a system. If we make 2^20 policy changes per second, 7353448Sdh155122 * this will last 2^44 seconds, or roughly 500,000 years, so we don't 7363448Sdh155122 * have to worry about reusing policy index values. 7373448Sdh155122 */ 7383448Sdh155122 uint64_t ipsec_next_policy_index; 7393448Sdh155122 7403448Sdh155122 HASH_HEAD(ipsec_action_s) ipsec_action_hash[IPSEC_ACTION_HASH_SIZE]; 7413448Sdh155122 HASH_HEAD(ipsec_sel) *ipsec_sel_hash; 7423448Sdh155122 uint32_t ipsec_spd_hashsize; 7433448Sdh155122 7443448Sdh155122 ipsif_t ipsec_ipsid_buckets[IPSID_HASHSIZE]; 7453448Sdh155122 7463448Sdh155122 /* 7473448Sdh155122 * Active & Inactive system policy roots 7483448Sdh155122 */ 7493448Sdh155122 ipsec_policy_head_t ipsec_system_policy; 7503448Sdh155122 ipsec_policy_head_t ipsec_inactive_policy; 7513448Sdh155122 7523448Sdh155122 /* Packet dropper for generic SPD drops. */ 7533448Sdh155122 ipdropper_t ipsec_spd_dropper; 7543448Sdh155122 7553448Sdh155122 /* ipdrop.c */ 7563448Sdh155122 kstat_t *ipsec_ip_drop_kstat; 7573448Sdh155122 struct ip_dropstats *ipsec_ip_drop_types; 7583448Sdh155122 7593448Sdh155122 /* spd.c */ 7603448Sdh155122 /* 7613448Sdh155122 * Have a counter for every possible policy message in 7623448Sdh155122 * ipsec_policy_failure_msgs 7633448Sdh155122 */ 7643448Sdh155122 uint32_t ipsec_policy_failure_count[IPSEC_POLICY_MAX]; 7653448Sdh155122 /* Time since last ipsec policy failure that printed a message. */ 7663448Sdh155122 hrtime_t ipsec_policy_failure_last; 7673448Sdh155122 7683448Sdh155122 /* ip_spd.c */ 7693448Sdh155122 /* stats */ 7703448Sdh155122 kstat_t *ipsec_ksp; 7713448Sdh155122 struct ipsec_kstats_s *ipsec_kstats; 7723448Sdh155122 7733448Sdh155122 /* sadb.c */ 7743448Sdh155122 /* Packet dropper for generic SADB drops. */ 7753448Sdh155122 ipdropper_t ipsec_sadb_dropper; 7763448Sdh155122 7773448Sdh155122 /* spd.c */ 7783448Sdh155122 boolean_t ipsec_inbound_v4_policy_present; 7793448Sdh155122 boolean_t ipsec_outbound_v4_policy_present; 7803448Sdh155122 boolean_t ipsec_inbound_v6_policy_present; 7813448Sdh155122 boolean_t ipsec_outbound_v6_policy_present; 7823448Sdh155122 7833448Sdh155122 /* spd.c */ 7843448Sdh155122 /* 7853448Sdh155122 * Because policy needs to know what algorithms are supported, keep the 7863448Sdh155122 * lists of algorithms here. 7873448Sdh155122 */ 7883448Sdh155122 kmutex_t ipsec_alg_lock; 7893448Sdh155122 7903448Sdh155122 uint8_t ipsec_nalgs[IPSEC_NALGTYPES]; 7913448Sdh155122 ipsec_alginfo_t *ipsec_alglists[IPSEC_NALGTYPES][IPSEC_MAX_ALGS]; 7923448Sdh155122 7933448Sdh155122 uint8_t ipsec_sortlist[IPSEC_NALGTYPES][IPSEC_MAX_ALGS]; 7943448Sdh155122 7953448Sdh155122 int ipsec_algs_exec_mode[IPSEC_NALGTYPES]; 7963448Sdh155122 7973448Sdh155122 uint32_t ipsec_tun_spd_hashsize; 7983448Sdh155122 /* 7993448Sdh155122 * Tunnel policies - AVL tree indexed by tunnel name. 8003448Sdh155122 */ 8013448Sdh155122 krwlock_t ipsec_tunnel_policy_lock; 8023448Sdh155122 uint64_t ipsec_tunnel_policy_gen; 8033448Sdh155122 avl_tree_t ipsec_tunnel_policies; 8043448Sdh155122 8053448Sdh155122 /* ipsec_loader.c */ 8063448Sdh155122 kmutex_t ipsec_loader_lock; 8073448Sdh155122 int ipsec_loader_state; 8083448Sdh155122 int ipsec_loader_sig; 8093448Sdh155122 kt_did_t ipsec_loader_tid; 8103448Sdh155122 kcondvar_t ipsec_loader_sig_cv; /* For loader_sig conditions. */ 8113448Sdh155122 8123448Sdh155122 }; 8133448Sdh155122 typedef struct ipsec_stack ipsec_stack_t; 8143448Sdh155122 8153448Sdh155122 /* Handle the kstat_create in ip_drop_init() failing */ 8163448Sdh155122 #define DROPPER(_ipss, _dropper) \ 8173448Sdh155122 (((_ipss)->ipsec_ip_drop_types == NULL) ? NULL : \ 8183448Sdh155122 &((_ipss)->ipsec_ip_drop_types->_dropper)) 8193448Sdh155122 8203448Sdh155122 /* 8210Sstevel@tonic-gate * Loader states.. 8220Sstevel@tonic-gate */ 8230Sstevel@tonic-gate #define IPSEC_LOADER_WAIT 0 8240Sstevel@tonic-gate #define IPSEC_LOADER_FAILED -1 8250Sstevel@tonic-gate #define IPSEC_LOADER_SUCCEEDED 1 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate /* 8280Sstevel@tonic-gate * ipsec_loader entrypoints. 8290Sstevel@tonic-gate */ 8303448Sdh155122 extern void ipsec_loader_init(ipsec_stack_t *); 8313448Sdh155122 extern void ipsec_loader_start(ipsec_stack_t *); 8323448Sdh155122 extern void ipsec_loader_destroy(ipsec_stack_t *); 8333448Sdh155122 extern void ipsec_loader_loadnow(ipsec_stack_t *); 8343448Sdh155122 extern boolean_t ipsec_loader_wait(queue_t *q, ipsec_stack_t *); 8353448Sdh155122 extern boolean_t ipsec_loaded(ipsec_stack_t *); 8363448Sdh155122 extern boolean_t ipsec_failed(ipsec_stack_t *); 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate /* 8390Sstevel@tonic-gate * ipsec policy entrypoints (spd.c) 8400Sstevel@tonic-gate */ 8410Sstevel@tonic-gate 8423448Sdh155122 extern void ipsec_policy_g_destroy(void); 8433448Sdh155122 extern void ipsec_policy_g_init(void); 8443448Sdh155122 845*11042SErik.Nordmark@Sun.COM extern mblk_t *ipsec_add_crypto_data(mblk_t *, ipsec_crypto_t **); 846*11042SErik.Nordmark@Sun.COM extern mblk_t *ipsec_remove_crypto_data(mblk_t *, ipsec_crypto_t **); 847*11042SErik.Nordmark@Sun.COM extern mblk_t *ipsec_free_crypto_data(mblk_t *); 8483448Sdh155122 extern int ipsec_alloc_table(ipsec_policy_head_t *, int, int, boolean_t, 8493448Sdh155122 netstack_t *); 8503055Sdanmcd extern void ipsec_polhead_init(ipsec_policy_head_t *, int); 8513055Sdanmcd extern void ipsec_polhead_destroy(ipsec_policy_head_t *); 8523055Sdanmcd extern void ipsec_polhead_free_table(ipsec_policy_head_t *); 8530Sstevel@tonic-gate extern mblk_t *ipsec_check_global_policy(mblk_t *, conn_t *, ipha_t *, 854*11042SErik.Nordmark@Sun.COM ip6_t *, ip_recv_attr_t *, netstack_t *ns); 8550Sstevel@tonic-gate extern mblk_t *ipsec_check_inbound_policy(mblk_t *, conn_t *, ipha_t *, ip6_t *, 856*11042SErik.Nordmark@Sun.COM ip_recv_attr_t *); 8570Sstevel@tonic-gate 858*11042SErik.Nordmark@Sun.COM extern boolean_t ipsec_in_to_out(ip_recv_attr_t *, ip_xmit_attr_t *, 859*11042SErik.Nordmark@Sun.COM mblk_t *, ipha_t *, ip6_t *); 860*11042SErik.Nordmark@Sun.COM extern void ipsec_in_release_refs(ip_recv_attr_t *); 861*11042SErik.Nordmark@Sun.COM extern void ipsec_out_release_refs(ip_xmit_attr_t *); 8623448Sdh155122 extern void ipsec_log_policy_failure(int, char *, ipha_t *, ip6_t *, boolean_t, 863*11042SErik.Nordmark@Sun.COM netstack_t *); 8640Sstevel@tonic-gate extern boolean_t ipsec_inbound_accept_clear(mblk_t *, ipha_t *, ip6_t *); 8650Sstevel@tonic-gate extern int ipsec_conn_cache_policy(conn_t *, boolean_t); 866*11042SErik.Nordmark@Sun.COM extern void ipsec_cache_outbound_policy(const conn_t *, const in6_addr_t *, 867*11042SErik.Nordmark@Sun.COM const in6_addr_t *, in_port_t, ip_xmit_attr_t *); 868*11042SErik.Nordmark@Sun.COM extern boolean_t ipsec_outbound_policy_current(ip_xmit_attr_t *); 869*11042SErik.Nordmark@Sun.COM extern ipsec_action_t *ipsec_in_to_out_action(ip_recv_attr_t *); 870*11042SErik.Nordmark@Sun.COM extern void ipsec_latch_inbound(conn_t *connp, ip_recv_attr_t *ira); 8710Sstevel@tonic-gate 872*11042SErik.Nordmark@Sun.COM extern void ipsec_policy_free(ipsec_policy_t *); 8730Sstevel@tonic-gate extern void ipsec_action_free(ipsec_action_t *); 8743448Sdh155122 extern void ipsec_polhead_free(ipsec_policy_head_t *, netstack_t *); 8753448Sdh155122 extern ipsec_policy_head_t *ipsec_polhead_split(ipsec_policy_head_t *, 8763448Sdh155122 netstack_t *); 8770Sstevel@tonic-gate extern ipsec_policy_head_t *ipsec_polhead_create(void); 8783448Sdh155122 extern ipsec_policy_head_t *ipsec_system_policy(netstack_t *); 8793448Sdh155122 extern ipsec_policy_head_t *ipsec_inactive_policy(netstack_t *); 8803448Sdh155122 extern void ipsec_swap_policy(ipsec_policy_head_t *, ipsec_policy_head_t *, 8813448Sdh155122 netstack_t *); 8823448Sdh155122 extern void ipsec_swap_global_policy(netstack_t *); 8830Sstevel@tonic-gate 8843448Sdh155122 extern int ipsec_clone_system_policy(netstack_t *); 88581Ssommerfe extern ipsec_policy_t *ipsec_policy_create(ipsec_selkey_t *, 8863448Sdh155122 const ipsec_act_t *, int, int, uint64_t *, netstack_t *); 8870Sstevel@tonic-gate extern boolean_t ipsec_policy_delete(ipsec_policy_head_t *, 8883448Sdh155122 ipsec_selkey_t *, int, netstack_t *); 8893448Sdh155122 extern int ipsec_policy_delete_index(ipsec_policy_head_t *, uint64_t, 8903448Sdh155122 netstack_t *); 89110616SSebastien.Roy@Sun.COM extern boolean_t ipsec_polhead_insert(ipsec_policy_head_t *, ipsec_act_t *, 89210616SSebastien.Roy@Sun.COM uint_t, int, int, netstack_t *); 8933448Sdh155122 extern void ipsec_polhead_flush(ipsec_policy_head_t *, netstack_t *); 8943448Sdh155122 extern int ipsec_copy_polhead(ipsec_policy_head_t *, ipsec_policy_head_t *, 8953448Sdh155122 netstack_t *); 89610616SSebastien.Roy@Sun.COM extern void ipsec_actvec_from_req(const ipsec_req_t *, ipsec_act_t **, uint_t *, 8973448Sdh155122 netstack_t *); 8980Sstevel@tonic-gate extern void ipsec_actvec_free(ipsec_act_t *, uint_t); 8993055Sdanmcd extern int ipsec_req_from_head(ipsec_policy_head_t *, ipsec_req_t *, int); 9003448Sdh155122 extern mblk_t *ipsec_construct_inverse_acquire(sadb_msg_t *, sadb_ext_t **, 9013448Sdh155122 netstack_t *); 902*11042SErik.Nordmark@Sun.COM extern ipsec_policy_t *ipsec_find_policy(int, const conn_t *, 903*11042SErik.Nordmark@Sun.COM ipsec_selector_t *, netstack_t *); 9043448Sdh155122 extern ipsid_t *ipsid_lookup(int, char *, netstack_t *); 9050Sstevel@tonic-gate extern boolean_t ipsid_equal(ipsid_t *, ipsid_t *); 9063448Sdh155122 extern void ipsid_gc(netstack_t *); 9070Sstevel@tonic-gate extern void ipsec_latch_ids(ipsec_latch_t *, ipsid_t *, ipsid_t *); 9080Sstevel@tonic-gate 9093448Sdh155122 extern void ipsec_config_flush(netstack_t *); 9100Sstevel@tonic-gate extern boolean_t ipsec_check_policy(ipsec_policy_head_t *, ipsec_policy_t *, 9110Sstevel@tonic-gate int); 9123448Sdh155122 extern void ipsec_enter_policy(ipsec_policy_head_t *, ipsec_policy_t *, int, 9133448Sdh155122 netstack_t *); 9143448Sdh155122 extern boolean_t ipsec_check_action(ipsec_act_t *, int *, netstack_t *); 9150Sstevel@tonic-gate 916*11042SErik.Nordmark@Sun.COM extern void iplatch_free(ipsec_latch_t *); 9170Sstevel@tonic-gate extern ipsec_latch_t *iplatch_create(void); 9180Sstevel@tonic-gate extern int ipsec_set_req(cred_t *, conn_t *, ipsec_req_t *); 9190Sstevel@tonic-gate 92081Ssommerfe extern void ipsec_insert_always(avl_tree_t *tree, void *new_node); 92181Ssommerfe 9223055Sdanmcd extern int32_t ipsec_act_ovhd(const ipsec_act_t *act); 923*11042SErik.Nordmark@Sun.COM extern mblk_t *sadb_whack_label(mblk_t *, ipsa_t *, ip_xmit_attr_t *, 924*11042SErik.Nordmark@Sun.COM kstat_named_t *, ipdropper_t *); 925*11042SErik.Nordmark@Sun.COM extern mblk_t *sadb_whack_label_v4(mblk_t *, ipsa_t *, kstat_named_t *, 926*11042SErik.Nordmark@Sun.COM ipdropper_t *); 927*11042SErik.Nordmark@Sun.COM extern mblk_t *sadb_whack_label_v6(mblk_t *, ipsa_t *, kstat_named_t *, 928*11042SErik.Nordmark@Sun.COM ipdropper_t *); 92910824SMark.Fenwick@Sun.COM extern boolean_t update_iv(uint8_t *, queue_t *, ipsa_t *, ipsecesp_stack_t *); 9303055Sdanmcd 9313055Sdanmcd /* 9323055Sdanmcd * Tunnel-support SPD functions and variables. 9333055Sdanmcd */ 93410616SSebastien.Roy@Sun.COM struct iptun_s; /* Defined in inet/iptun/iptun_impl.h. */ 935*11042SErik.Nordmark@Sun.COM extern mblk_t *ipsec_tun_inbound(ip_recv_attr_t *, mblk_t *, ipsec_tun_pol_t *, 9363448Sdh155122 ipha_t *, ip6_t *, ipha_t *, ip6_t *, int, netstack_t *); 93710616SSebastien.Roy@Sun.COM extern mblk_t *ipsec_tun_outbound(mblk_t *, struct iptun_s *, ipha_t *, 938*11042SErik.Nordmark@Sun.COM ip6_t *, ipha_t *, ip6_t *, int, ip_xmit_attr_t *); 9393448Sdh155122 extern void itp_free(ipsec_tun_pol_t *, netstack_t *); 9403448Sdh155122 extern ipsec_tun_pol_t *create_tunnel_policy(char *, int *, uint64_t *, 9413448Sdh155122 netstack_t *); 9423448Sdh155122 extern ipsec_tun_pol_t *get_tunnel_policy(char *, netstack_t *); 9433448Sdh155122 extern void itp_unlink(ipsec_tun_pol_t *, netstack_t *); 9443448Sdh155122 extern void itp_walk(void (*)(ipsec_tun_pol_t *, void *, netstack_t *), 9453448Sdh155122 void *, netstack_t *); 9463055Sdanmcd 94710616SSebastien.Roy@Sun.COM extern ipsec_tun_pol_t *itp_get_byaddr(uint32_t *, uint32_t *, int, 94810616SSebastien.Roy@Sun.COM ip_stack_t *); 9493055Sdanmcd 9500Sstevel@tonic-gate /* 9514987Sdanmcd * IPsec AH/ESP functions called from IP or the common SADB code in AH. 9520Sstevel@tonic-gate */ 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate extern void ipsecah_in_assocfailure(mblk_t *, char, ushort_t, char *, 955*11042SErik.Nordmark@Sun.COM uint32_t, void *, int, ip_recv_attr_t *ira); 9560Sstevel@tonic-gate extern void ipsecesp_in_assocfailure(mblk_t *, char, ushort_t, char *, 957*11042SErik.Nordmark@Sun.COM uint32_t, void *, int, ip_recv_attr_t *ira); 9584987Sdanmcd extern void ipsecesp_send_keepalive(ipsa_t *); 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate /* 9610Sstevel@tonic-gate * Algorithm management helper functions. 9620Sstevel@tonic-gate */ 9630Sstevel@tonic-gate extern boolean_t ipsec_valid_key_size(uint16_t, ipsec_alginfo_t *); 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate /* 9660Sstevel@tonic-gate * Per-socket policy, for now, takes precedence... this priority value 9670Sstevel@tonic-gate * insures it. 9680Sstevel@tonic-gate */ 9690Sstevel@tonic-gate #define IPSEC_PRIO_SOCKET 0x1000000 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate /* DDI initialization functions. */ 9720Sstevel@tonic-gate extern boolean_t ipsecesp_ddi_init(void); 9730Sstevel@tonic-gate extern boolean_t ipsecah_ddi_init(void); 9740Sstevel@tonic-gate extern boolean_t keysock_ddi_init(void); 9750Sstevel@tonic-gate extern boolean_t spdsock_ddi_init(void); 9760Sstevel@tonic-gate 9770Sstevel@tonic-gate extern void ipsecesp_ddi_destroy(void); 9780Sstevel@tonic-gate extern void ipsecah_ddi_destroy(void); 9790Sstevel@tonic-gate extern void keysock_ddi_destroy(void); 9800Sstevel@tonic-gate extern void spdsock_ddi_destroy(void); 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate /* 9830Sstevel@tonic-gate * AH- and ESP-specific functions that are called directly by other modules. 9840Sstevel@tonic-gate */ 9853448Sdh155122 extern void ipsecah_fill_defs(struct sadb_x_ecomb *, netstack_t *); 9863448Sdh155122 extern void ipsecesp_fill_defs(struct sadb_x_ecomb *, netstack_t *); 9873448Sdh155122 extern void ipsecah_algs_changed(netstack_t *); 9883448Sdh155122 extern void ipsecesp_algs_changed(netstack_t *); 9890Sstevel@tonic-gate extern void ipsecesp_init_funcs(ipsa_t *); 9900Sstevel@tonic-gate extern void ipsecah_init_funcs(ipsa_t *); 991*11042SErik.Nordmark@Sun.COM extern mblk_t *ipsecah_icmp_error(mblk_t *, ip_recv_attr_t *); 992*11042SErik.Nordmark@Sun.COM extern mblk_t *ipsecesp_icmp_error(mblk_t *, ip_recv_attr_t *); 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate /* 9950Sstevel@tonic-gate * spdsock functions that are called directly by IP. 9960Sstevel@tonic-gate */ 9973448Sdh155122 extern void spdsock_update_pending_algs(netstack_t *); 9980Sstevel@tonic-gate 9990Sstevel@tonic-gate /* 10000Sstevel@tonic-gate * IP functions that are called from AH and ESP. 10010Sstevel@tonic-gate */ 1002*11042SErik.Nordmark@Sun.COM extern boolean_t ipsec_outbound_sa(mblk_t *, ip_xmit_attr_t *, uint_t); 1003*11042SErik.Nordmark@Sun.COM extern mblk_t *ipsec_inbound_esp_sa(mblk_t *, ip_recv_attr_t *, esph_t **); 1004*11042SErik.Nordmark@Sun.COM extern mblk_t *ipsec_inbound_ah_sa(mblk_t *, ip_recv_attr_t *, ah_t **); 10053055Sdanmcd extern ipsec_policy_t *ipsec_find_policy_head(ipsec_policy_t *, 1006*11042SErik.Nordmark@Sun.COM ipsec_policy_head_t *, int, ipsec_selector_t *); 10073055Sdanmcd 10083448Sdh155122 /* 10093448Sdh155122 * IP dropper init/destroy. 10103448Sdh155122 */ 10113448Sdh155122 void ip_drop_init(ipsec_stack_t *); 10123448Sdh155122 void ip_drop_destroy(ipsec_stack_t *); 10130Sstevel@tonic-gate 10140Sstevel@tonic-gate /* 10153055Sdanmcd * Common functions 10163055Sdanmcd */ 10173055Sdanmcd extern boolean_t ip_addr_match(uint8_t *, int, in6_addr_t *); 1018*11042SErik.Nordmark@Sun.COM extern boolean_t ipsec_label_match(ts_label_t *, ts_label_t *); 10193055Sdanmcd 10203055Sdanmcd /* 10210Sstevel@tonic-gate * AH and ESP counters types. 10220Sstevel@tonic-gate */ 10230Sstevel@tonic-gate typedef uint32_t ah_counter; 10240Sstevel@tonic-gate typedef uint32_t esp_counter; 10250Sstevel@tonic-gate 10260Sstevel@tonic-gate #endif /* _KERNEL */ 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate #ifdef __cplusplus 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate #endif 10310Sstevel@tonic-gate 10320Sstevel@tonic-gate #endif /* _INET_IPSEC_IMPL_H */ 1033