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 52751Sdanmcd * Common Development and Distribution License (the "License"). 62751Sdanmcd * 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 /* 22*3448Sdh155122 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/types.h> 290Sstevel@tonic-gate #include <sys/stream.h> 300Sstevel@tonic-gate #include <sys/stropts.h> 310Sstevel@tonic-gate #include <sys/errno.h> 320Sstevel@tonic-gate #include <sys/strlog.h> 330Sstevel@tonic-gate #include <sys/tihdr.h> 340Sstevel@tonic-gate #include <sys/socket.h> 350Sstevel@tonic-gate #include <sys/ddi.h> 360Sstevel@tonic-gate #include <sys/sunddi.h> 370Sstevel@tonic-gate #include <sys/kmem.h> 38*3448Sdh155122 #include <sys/zone.h> 390Sstevel@tonic-gate #include <sys/sysmacros.h> 400Sstevel@tonic-gate #include <sys/cmn_err.h> 410Sstevel@tonic-gate #include <sys/vtrace.h> 420Sstevel@tonic-gate #include <sys/debug.h> 430Sstevel@tonic-gate #include <sys/atomic.h> 440Sstevel@tonic-gate #include <sys/strsun.h> 450Sstevel@tonic-gate #include <sys/random.h> 460Sstevel@tonic-gate #include <netinet/in.h> 470Sstevel@tonic-gate #include <net/if.h> 480Sstevel@tonic-gate #include <netinet/ip6.h> 490Sstevel@tonic-gate #include <net/pfkeyv2.h> 500Sstevel@tonic-gate 510Sstevel@tonic-gate #include <inet/common.h> 520Sstevel@tonic-gate #include <inet/mi.h> 530Sstevel@tonic-gate #include <inet/nd.h> 540Sstevel@tonic-gate #include <inet/ip.h> 550Sstevel@tonic-gate #include <inet/ip6.h> 560Sstevel@tonic-gate #include <inet/sadb.h> 570Sstevel@tonic-gate #include <inet/ipsec_info.h> 580Sstevel@tonic-gate #include <inet/ipsec_impl.h> 590Sstevel@tonic-gate #include <inet/ipsecesp.h> 600Sstevel@tonic-gate #include <inet/ipdrop.h> 610Sstevel@tonic-gate #include <inet/tcp.h> 620Sstevel@tonic-gate #include <sys/kstat.h> 630Sstevel@tonic-gate #include <sys/policy.h> 640Sstevel@tonic-gate #include <sys/strsun.h> 650Sstevel@tonic-gate #include <inet/udp_impl.h> 660Sstevel@tonic-gate #include <sys/taskq.h> 67*3448Sdh155122 #include <sys/note.h> 680Sstevel@tonic-gate 690Sstevel@tonic-gate #include <sys/iphada.h> 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* 720Sstevel@tonic-gate * Table of ND variables supported by ipsecesp. These are loaded into 730Sstevel@tonic-gate * ipsecesp_g_nd in ipsecesp_init_nd. 740Sstevel@tonic-gate * All of these are alterable, within the min/max values given, at run time. 750Sstevel@tonic-gate */ 76*3448Sdh155122 static ipsecespparam_t lcl_param_arr[] = { 770Sstevel@tonic-gate /* min max value name */ 780Sstevel@tonic-gate { 0, 3, 0, "ipsecesp_debug"}, 790Sstevel@tonic-gate { 125, 32000, SADB_AGE_INTERVAL_DEFAULT, "ipsecesp_age_interval"}, 800Sstevel@tonic-gate { 1, 10, 1, "ipsecesp_reap_delay"}, 810Sstevel@tonic-gate { 1, SADB_MAX_REPLAY, 64, "ipsecesp_replay_size"}, 820Sstevel@tonic-gate { 1, 300, 15, "ipsecesp_acquire_timeout"}, 830Sstevel@tonic-gate { 1, 1800, 90, "ipsecesp_larval_timeout"}, 840Sstevel@tonic-gate /* Default lifetime values for ACQUIRE messages. */ 850Sstevel@tonic-gate { 0, 0xffffffffU, 0, "ipsecesp_default_soft_bytes"}, 860Sstevel@tonic-gate { 0, 0xffffffffU, 0, "ipsecesp_default_hard_bytes"}, 870Sstevel@tonic-gate { 0, 0xffffffffU, 24000, "ipsecesp_default_soft_addtime"}, 880Sstevel@tonic-gate { 0, 0xffffffffU, 28800, "ipsecesp_default_hard_addtime"}, 890Sstevel@tonic-gate { 0, 0xffffffffU, 0, "ipsecesp_default_soft_usetime"}, 900Sstevel@tonic-gate { 0, 0xffffffffU, 0, "ipsecesp_default_hard_usetime"}, 910Sstevel@tonic-gate { 0, 1, 0, "ipsecesp_log_unknown_spi"}, 920Sstevel@tonic-gate { 0, 2, 1, "ipsecesp_padding_check"}, 930Sstevel@tonic-gate }; 94*3448Sdh155122 #define ipsecesp_debug ipsecesp_params[0].ipsecesp_param_value 95*3448Sdh155122 #define ipsecesp_age_interval ipsecesp_params[1].ipsecesp_param_value 96*3448Sdh155122 #define ipsecesp_age_int_max ipsecesp_params[1].ipsecesp_param_max 97*3448Sdh155122 #define ipsecesp_reap_delay ipsecesp_params[2].ipsecesp_param_value 98*3448Sdh155122 #define ipsecesp_replay_size ipsecesp_params[3].ipsecesp_param_value 99*3448Sdh155122 #define ipsecesp_acquire_timeout \ 100*3448Sdh155122 ipsecesp_params[4].ipsecesp_param_value 101*3448Sdh155122 #define ipsecesp_larval_timeout \ 102*3448Sdh155122 ipsecesp_params[5].ipsecesp_param_value 103*3448Sdh155122 #define ipsecesp_default_soft_bytes \ 104*3448Sdh155122 ipsecesp_params[6].ipsecesp_param_value 105*3448Sdh155122 #define ipsecesp_default_hard_bytes \ 106*3448Sdh155122 ipsecesp_params[7].ipsecesp_param_value 107*3448Sdh155122 #define ipsecesp_default_soft_addtime \ 108*3448Sdh155122 ipsecesp_params[8].ipsecesp_param_value 109*3448Sdh155122 #define ipsecesp_default_hard_addtime \ 110*3448Sdh155122 ipsecesp_params[9].ipsecesp_param_value 111*3448Sdh155122 #define ipsecesp_default_soft_usetime \ 112*3448Sdh155122 ipsecesp_params[10].ipsecesp_param_value 113*3448Sdh155122 #define ipsecesp_default_hard_usetime \ 114*3448Sdh155122 ipsecesp_params[11].ipsecesp_param_value 115*3448Sdh155122 #define ipsecesp_log_unknown_spi \ 116*3448Sdh155122 ipsecesp_params[12].ipsecesp_param_value 117*3448Sdh155122 #define ipsecesp_padding_check \ 118*3448Sdh155122 ipsecesp_params[13].ipsecesp_param_value 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate #define esp0dbg(a) printf a 1210Sstevel@tonic-gate /* NOTE: != 0 instead of > 0 so lint doesn't complain. */ 122*3448Sdh155122 #define esp1dbg(espstack, a) if (espstack->ipsecesp_debug != 0) printf a 123*3448Sdh155122 #define esp2dbg(espstack, a) if (espstack->ipsecesp_debug > 1) printf a 124*3448Sdh155122 #define esp3dbg(espstack, a) if (espstack->ipsecesp_debug > 2) printf a 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate static int ipsecesp_open(queue_t *, dev_t *, int, int, cred_t *); 1270Sstevel@tonic-gate static int ipsecesp_close(queue_t *); 1280Sstevel@tonic-gate static void ipsecesp_rput(queue_t *, mblk_t *); 1290Sstevel@tonic-gate static void ipsecesp_wput(queue_t *, mblk_t *); 130*3448Sdh155122 static void *ipsecesp_stack_init(netstackid_t stackid, netstack_t *ns); 131*3448Sdh155122 static void ipsecesp_stack_fini(netstackid_t stackid, void *arg); 132*3448Sdh155122 static void esp_send_acquire(ipsacq_t *, mblk_t *, netstack_t *); 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate static ipsec_status_t esp_outbound_accelerated(mblk_t *, uint_t); 1350Sstevel@tonic-gate static ipsec_status_t esp_inbound_accelerated(mblk_t *, mblk_t *, 1360Sstevel@tonic-gate boolean_t, ipsa_t *); 1370Sstevel@tonic-gate 138*3448Sdh155122 static boolean_t esp_register_out(uint32_t, uint32_t, uint_t, 139*3448Sdh155122 ipsecesp_stack_t *); 1400Sstevel@tonic-gate static boolean_t esp_strip_header(mblk_t *, boolean_t, uint32_t, 141*3448Sdh155122 kstat_named_t **, ipsecesp_stack_t *); 1420Sstevel@tonic-gate static ipsec_status_t esp_submit_req_inbound(mblk_t *, ipsa_t *, uint_t); 1430Sstevel@tonic-gate static ipsec_status_t esp_submit_req_outbound(mblk_t *, ipsa_t *, uchar_t *, 1440Sstevel@tonic-gate uint_t); 1450Sstevel@tonic-gate 146*3448Sdh155122 /* Setable in /etc/system */ 147*3448Sdh155122 uint32_t esp_hash_size = IPSEC_DEFAULT_HASH_SIZE; 148*3448Sdh155122 1490Sstevel@tonic-gate static struct module_info info = { 1500Sstevel@tonic-gate 5137, "ipsecesp", 0, INFPSZ, 65536, 1024 1510Sstevel@tonic-gate }; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate static struct qinit rinit = { 1540Sstevel@tonic-gate (pfi_t)ipsecesp_rput, NULL, ipsecesp_open, ipsecesp_close, NULL, &info, 1550Sstevel@tonic-gate NULL 1560Sstevel@tonic-gate }; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate static struct qinit winit = { 1590Sstevel@tonic-gate (pfi_t)ipsecesp_wput, NULL, ipsecesp_open, ipsecesp_close, NULL, &info, 1600Sstevel@tonic-gate NULL 1610Sstevel@tonic-gate }; 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate struct streamtab ipsecespinfo = { 1640Sstevel@tonic-gate &rinit, &winit, NULL, NULL 1650Sstevel@tonic-gate }; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate static taskq_t *esp_taskq; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate /* 1700Sstevel@tonic-gate * OTOH, this one is set at open/close, and I'm D_MTQPAIR for now. 1710Sstevel@tonic-gate * 1720Sstevel@tonic-gate * Question: Do I need this, given that all instance's esps->esps_wq point 1730Sstevel@tonic-gate * to IP? 1740Sstevel@tonic-gate * 1750Sstevel@tonic-gate * Answer: Yes, because I need to know which queue is BOUND to 1760Sstevel@tonic-gate * IPPROTO_ESP 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate /* 1800Sstevel@tonic-gate * Stats. This may eventually become a full-blown SNMP MIB once that spec 1810Sstevel@tonic-gate * stabilizes. 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate 184*3448Sdh155122 typedef struct esp_kstats_s { 1850Sstevel@tonic-gate kstat_named_t esp_stat_num_aalgs; 1860Sstevel@tonic-gate kstat_named_t esp_stat_good_auth; 1870Sstevel@tonic-gate kstat_named_t esp_stat_bad_auth; 1880Sstevel@tonic-gate kstat_named_t esp_stat_bad_padding; 1890Sstevel@tonic-gate kstat_named_t esp_stat_replay_failures; 1900Sstevel@tonic-gate kstat_named_t esp_stat_replay_early_failures; 1910Sstevel@tonic-gate kstat_named_t esp_stat_keysock_in; 1920Sstevel@tonic-gate kstat_named_t esp_stat_out_requests; 1930Sstevel@tonic-gate kstat_named_t esp_stat_acquire_requests; 1940Sstevel@tonic-gate kstat_named_t esp_stat_bytes_expired; 1950Sstevel@tonic-gate kstat_named_t esp_stat_out_discards; 1960Sstevel@tonic-gate kstat_named_t esp_stat_in_accelerated; 1970Sstevel@tonic-gate kstat_named_t esp_stat_out_accelerated; 1980Sstevel@tonic-gate kstat_named_t esp_stat_noaccel; 1990Sstevel@tonic-gate kstat_named_t esp_stat_crypto_sync; 2000Sstevel@tonic-gate kstat_named_t esp_stat_crypto_async; 2010Sstevel@tonic-gate kstat_named_t esp_stat_crypto_failures; 2020Sstevel@tonic-gate kstat_named_t esp_stat_num_ealgs; 2030Sstevel@tonic-gate kstat_named_t esp_stat_bad_decrypt; 2040Sstevel@tonic-gate } esp_kstats_t; 2050Sstevel@tonic-gate 206*3448Sdh155122 /* 207*3448Sdh155122 * espstack->esp_kstats is equal to espstack->esp_ksp->ks_data if 208*3448Sdh155122 * kstat_create_netstack for espstack->esp_ksp succeeds, but when it 209*3448Sdh155122 * fails, it will be NULL. Note this is done for all stack instances, 210*3448Sdh155122 * so it *could* fail. hence a non-NULL checking is done for 211*3448Sdh155122 * ESP_BUMP_STAT and ESP_DEBUMP_STAT 212*3448Sdh155122 */ 213*3448Sdh155122 #define ESP_BUMP_STAT(espstack, x) \ 214*3448Sdh155122 do { \ 215*3448Sdh155122 if (espstack->esp_kstats != NULL) \ 216*3448Sdh155122 (espstack->esp_kstats->esp_stat_ ## x).value.ui64++; \ 217*3448Sdh155122 _NOTE(CONSTCOND) \ 218*3448Sdh155122 } while (0) 219*3448Sdh155122 220*3448Sdh155122 #define ESP_DEBUMP_STAT(espstack, x) \ 221*3448Sdh155122 do { \ 222*3448Sdh155122 if (espstack->esp_kstats != NULL) \ 223*3448Sdh155122 (espstack->esp_kstats->esp_stat_ ## x).value.ui64--; \ 224*3448Sdh155122 _NOTE(CONSTCOND) \ 225*3448Sdh155122 } while (0) 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate static int esp_kstat_update(kstat_t *, int); 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate static boolean_t 230*3448Sdh155122 esp_kstat_init(ipsecesp_stack_t *espstack, netstackid_t stackid) 2310Sstevel@tonic-gate { 232*3448Sdh155122 espstack->esp_ksp = kstat_create_netstack("ipsecesp", 0, "esp_stat", 233*3448Sdh155122 "net", KSTAT_TYPE_NAMED, 234*3448Sdh155122 sizeof (esp_kstats_t) / sizeof (kstat_named_t), 235*3448Sdh155122 KSTAT_FLAG_PERSISTENT, stackid); 236*3448Sdh155122 237*3448Sdh155122 if (espstack->esp_ksp == NULL || espstack->esp_ksp->ks_data == NULL) 2380Sstevel@tonic-gate return (B_FALSE); 2390Sstevel@tonic-gate 240*3448Sdh155122 espstack->esp_kstats = espstack->esp_ksp->ks_data; 241*3448Sdh155122 242*3448Sdh155122 espstack->esp_ksp->ks_update = esp_kstat_update; 243*3448Sdh155122 espstack->esp_ksp->ks_private = (void *)(uintptr_t)stackid; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate #define K64 KSTAT_DATA_UINT64 246*3448Sdh155122 #define KI(x) kstat_named_init(&(espstack->esp_kstats->esp_stat_##x), #x, K64) 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate KI(num_aalgs); 2490Sstevel@tonic-gate KI(num_ealgs); 2500Sstevel@tonic-gate KI(good_auth); 2510Sstevel@tonic-gate KI(bad_auth); 2520Sstevel@tonic-gate KI(bad_padding); 2530Sstevel@tonic-gate KI(replay_failures); 2540Sstevel@tonic-gate KI(replay_early_failures); 2550Sstevel@tonic-gate KI(keysock_in); 2560Sstevel@tonic-gate KI(out_requests); 2570Sstevel@tonic-gate KI(acquire_requests); 2580Sstevel@tonic-gate KI(bytes_expired); 2590Sstevel@tonic-gate KI(out_discards); 2600Sstevel@tonic-gate KI(in_accelerated); 2610Sstevel@tonic-gate KI(out_accelerated); 2620Sstevel@tonic-gate KI(noaccel); 2630Sstevel@tonic-gate KI(crypto_sync); 2640Sstevel@tonic-gate KI(crypto_async); 2650Sstevel@tonic-gate KI(crypto_failures); 2660Sstevel@tonic-gate KI(bad_decrypt); 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate #undef KI 2690Sstevel@tonic-gate #undef K64 2700Sstevel@tonic-gate 271*3448Sdh155122 kstat_install(espstack->esp_ksp); 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate return (B_TRUE); 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate static int 2770Sstevel@tonic-gate esp_kstat_update(kstat_t *kp, int rw) 2780Sstevel@tonic-gate { 2790Sstevel@tonic-gate esp_kstats_t *ekp; 280*3448Sdh155122 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 281*3448Sdh155122 netstack_t *ns; 282*3448Sdh155122 ipsec_stack_t *ipss; 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate if ((kp == NULL) || (kp->ks_data == NULL)) 2850Sstevel@tonic-gate return (EIO); 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate if (rw == KSTAT_WRITE) 2880Sstevel@tonic-gate return (EACCES); 2890Sstevel@tonic-gate 290*3448Sdh155122 ns = netstack_find_by_stackid(stackid); 291*3448Sdh155122 if (ns == NULL) 292*3448Sdh155122 return (-1); 293*3448Sdh155122 ipss = ns->netstack_ipsec; 294*3448Sdh155122 if (ipss == NULL) { 295*3448Sdh155122 netstack_rele(ns); 296*3448Sdh155122 return (-1); 297*3448Sdh155122 } 2980Sstevel@tonic-gate ekp = (esp_kstats_t *)kp->ks_data; 299*3448Sdh155122 300*3448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 301*3448Sdh155122 ekp->esp_stat_num_aalgs.value.ui64 = 302*3448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_AUTH]; 303*3448Sdh155122 ekp->esp_stat_num_ealgs.value.ui64 = 304*3448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 305*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 306*3448Sdh155122 307*3448Sdh155122 netstack_rele(ns); 3080Sstevel@tonic-gate return (0); 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate #ifdef DEBUG 3120Sstevel@tonic-gate /* 3130Sstevel@tonic-gate * Debug routine, useful to see pre-encryption data. 3140Sstevel@tonic-gate */ 3150Sstevel@tonic-gate static char * 3160Sstevel@tonic-gate dump_msg(mblk_t *mp) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate char tmp_str[3], tmp_line[256]; 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate while (mp != NULL) { 3210Sstevel@tonic-gate unsigned char *ptr; 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate printf("mblk address 0x%p, length %ld, db_ref %d " 3240Sstevel@tonic-gate "type %d, base 0x%p, lim 0x%p\n", 3250Sstevel@tonic-gate (void *) mp, (long)(mp->b_wptr - mp->b_rptr), 3260Sstevel@tonic-gate mp->b_datap->db_ref, mp->b_datap->db_type, 3270Sstevel@tonic-gate (void *)mp->b_datap->db_base, (void *)mp->b_datap->db_lim); 3280Sstevel@tonic-gate ptr = mp->b_rptr; 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate tmp_line[0] = '\0'; 3310Sstevel@tonic-gate while (ptr < mp->b_wptr) { 3320Sstevel@tonic-gate uint_t diff; 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate diff = (ptr - mp->b_rptr); 3350Sstevel@tonic-gate if (!(diff & 0x1f)) { 3360Sstevel@tonic-gate if (strlen(tmp_line) > 0) { 3370Sstevel@tonic-gate printf("bytes: %s\n", tmp_line); 3380Sstevel@tonic-gate tmp_line[0] = '\0'; 3390Sstevel@tonic-gate } 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate if (!(diff & 0x3)) 3420Sstevel@tonic-gate (void) strcat(tmp_line, " "); 3430Sstevel@tonic-gate (void) sprintf(tmp_str, "%02x", *ptr); 3440Sstevel@tonic-gate (void) strcat(tmp_line, tmp_str); 3450Sstevel@tonic-gate ptr++; 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate if (strlen(tmp_line) > 0) 3480Sstevel@tonic-gate printf("bytes: %s\n", tmp_line); 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate mp = mp->b_cont; 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate return ("\n"); 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate #else /* DEBUG */ 3570Sstevel@tonic-gate static char * 3580Sstevel@tonic-gate dump_msg(mblk_t *mp) 3590Sstevel@tonic-gate { 3600Sstevel@tonic-gate printf("Find value of mp %p.\n", mp); 3610Sstevel@tonic-gate return ("\n"); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate #endif /* DEBUG */ 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate /* 3660Sstevel@tonic-gate * Don't have to lock age_interval, as only one thread will access it at 3670Sstevel@tonic-gate * a time, because I control the one function that does with timeout(). 3680Sstevel@tonic-gate */ 3690Sstevel@tonic-gate static void 370*3448Sdh155122 esp_ager(void *arg) 3710Sstevel@tonic-gate { 372*3448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)arg; 373*3448Sdh155122 netstack_t *ns = espstack->ipsecesp_netstack; 3740Sstevel@tonic-gate hrtime_t begin = gethrtime(); 3750Sstevel@tonic-gate 376*3448Sdh155122 sadb_ager(&espstack->esp_sadb.s_v4, espstack->esp_pfkey_q, 377*3448Sdh155122 espstack->esp_sadb.s_ip_q, espstack->ipsecesp_reap_delay, ns); 378*3448Sdh155122 sadb_ager(&espstack->esp_sadb.s_v6, espstack->esp_pfkey_q, 379*3448Sdh155122 espstack->esp_sadb.s_ip_q, espstack->ipsecesp_reap_delay, ns); 380*3448Sdh155122 381*3448Sdh155122 espstack->esp_event = sadb_retimeout(begin, espstack->esp_pfkey_q, 382*3448Sdh155122 esp_ager, espstack, 383*3448Sdh155122 &espstack->ipsecesp_age_interval, espstack->ipsecesp_age_int_max, 384*3448Sdh155122 info.mi_idnum); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate /* 3880Sstevel@tonic-gate * Get an ESP NDD parameter. 3890Sstevel@tonic-gate */ 3900Sstevel@tonic-gate /* ARGSUSED */ 3910Sstevel@tonic-gate static int 3920Sstevel@tonic-gate ipsecesp_param_get(q, mp, cp, cr) 3930Sstevel@tonic-gate queue_t *q; 3940Sstevel@tonic-gate mblk_t *mp; 3950Sstevel@tonic-gate caddr_t cp; 3960Sstevel@tonic-gate cred_t *cr; 3970Sstevel@tonic-gate { 3980Sstevel@tonic-gate ipsecespparam_t *ipsecesppa = (ipsecespparam_t *)cp; 3990Sstevel@tonic-gate uint_t value; 400*3448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)q->q_ptr; 401*3448Sdh155122 402*3448Sdh155122 mutex_enter(&espstack->ipsecesp_param_lock); 4030Sstevel@tonic-gate value = ipsecesppa->ipsecesp_param_value; 404*3448Sdh155122 mutex_exit(&espstack->ipsecesp_param_lock); 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate (void) mi_mpprintf(mp, "%u", value); 4070Sstevel@tonic-gate return (0); 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate /* 4110Sstevel@tonic-gate * This routine sets an NDD variable in a ipsecespparam_t structure. 4120Sstevel@tonic-gate */ 4130Sstevel@tonic-gate /* ARGSUSED */ 4140Sstevel@tonic-gate static int 4150Sstevel@tonic-gate ipsecesp_param_set(q, mp, value, cp, cr) 4160Sstevel@tonic-gate queue_t *q; 4170Sstevel@tonic-gate mblk_t *mp; 4180Sstevel@tonic-gate char *value; 4190Sstevel@tonic-gate caddr_t cp; 4200Sstevel@tonic-gate cred_t *cr; 4210Sstevel@tonic-gate { 4220Sstevel@tonic-gate ulong_t new_value; 4230Sstevel@tonic-gate ipsecespparam_t *ipsecesppa = (ipsecespparam_t *)cp; 424*3448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)q->q_ptr; 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate /* 4270Sstevel@tonic-gate * Fail the request if the new value does not lie within the 4280Sstevel@tonic-gate * required bounds. 4290Sstevel@tonic-gate */ 4300Sstevel@tonic-gate if (ddi_strtoul(value, NULL, 10, &new_value) != 0 || 4310Sstevel@tonic-gate new_value < ipsecesppa->ipsecesp_param_min || 4320Sstevel@tonic-gate new_value > ipsecesppa->ipsecesp_param_max) { 4330Sstevel@tonic-gate return (EINVAL); 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate /* Set the new value */ 437*3448Sdh155122 mutex_enter(&espstack->ipsecesp_param_lock); 4380Sstevel@tonic-gate ipsecesppa->ipsecesp_param_value = new_value; 439*3448Sdh155122 mutex_exit(&espstack->ipsecesp_param_lock); 4400Sstevel@tonic-gate return (0); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* 4440Sstevel@tonic-gate * Using lifetime NDD variables, fill in an extended combination's 4450Sstevel@tonic-gate * lifetime information. 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate void 448*3448Sdh155122 ipsecesp_fill_defs(sadb_x_ecomb_t *ecomb, netstack_t *ns) 4490Sstevel@tonic-gate { 450*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 451*3448Sdh155122 452*3448Sdh155122 ecomb->sadb_x_ecomb_soft_bytes = espstack->ipsecesp_default_soft_bytes; 453*3448Sdh155122 ecomb->sadb_x_ecomb_hard_bytes = espstack->ipsecesp_default_hard_bytes; 454*3448Sdh155122 ecomb->sadb_x_ecomb_soft_addtime = 455*3448Sdh155122 espstack->ipsecesp_default_soft_addtime; 456*3448Sdh155122 ecomb->sadb_x_ecomb_hard_addtime = 457*3448Sdh155122 espstack->ipsecesp_default_hard_addtime; 458*3448Sdh155122 ecomb->sadb_x_ecomb_soft_usetime = 459*3448Sdh155122 espstack->ipsecesp_default_soft_usetime; 460*3448Sdh155122 ecomb->sadb_x_ecomb_hard_usetime = 461*3448Sdh155122 espstack->ipsecesp_default_hard_usetime; 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate /* 4650Sstevel@tonic-gate * Initialize things for ESP at module load time. 4660Sstevel@tonic-gate */ 4670Sstevel@tonic-gate boolean_t 4680Sstevel@tonic-gate ipsecesp_ddi_init(void) 4690Sstevel@tonic-gate { 470*3448Sdh155122 esp_taskq = taskq_create("esp_taskq", 1, minclsyspri, 471*3448Sdh155122 IPSEC_TASKQ_MIN, IPSEC_TASKQ_MAX, 0); 472*3448Sdh155122 473*3448Sdh155122 /* 474*3448Sdh155122 * We want to be informed each time a stack is created or 475*3448Sdh155122 * destroyed in the kernel, so we can maintain the 476*3448Sdh155122 * set of ipsecesp_stack_t's. 477*3448Sdh155122 */ 478*3448Sdh155122 netstack_register(NS_IPSECESP, ipsecesp_stack_init, NULL, 479*3448Sdh155122 ipsecesp_stack_fini); 480*3448Sdh155122 481*3448Sdh155122 return (B_TRUE); 482*3448Sdh155122 } 483*3448Sdh155122 484*3448Sdh155122 /* 485*3448Sdh155122 * Walk through the param array specified registering each element with the 486*3448Sdh155122 * named dispatch handler. 487*3448Sdh155122 */ 488*3448Sdh155122 static boolean_t 489*3448Sdh155122 ipsecesp_param_register(IDP *ndp, ipsecespparam_t *espp, int cnt) 490*3448Sdh155122 { 491*3448Sdh155122 for (; cnt-- > 0; espp++) { 4920Sstevel@tonic-gate if (espp->ipsecesp_param_name != NULL && 4930Sstevel@tonic-gate espp->ipsecesp_param_name[0]) { 494*3448Sdh155122 if (!nd_load(ndp, 495*3448Sdh155122 espp->ipsecesp_param_name, 4960Sstevel@tonic-gate ipsecesp_param_get, ipsecesp_param_set, 4970Sstevel@tonic-gate (caddr_t)espp)) { 498*3448Sdh155122 nd_free(ndp); 4990Sstevel@tonic-gate return (B_FALSE); 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate } 5030Sstevel@tonic-gate return (B_TRUE); 5040Sstevel@tonic-gate } 505*3448Sdh155122 /* 506*3448Sdh155122 * Initialize things for ESP for each stack instance 507*3448Sdh155122 */ 508*3448Sdh155122 static void * 509*3448Sdh155122 ipsecesp_stack_init(netstackid_t stackid, netstack_t *ns) 510*3448Sdh155122 { 511*3448Sdh155122 ipsecesp_stack_t *espstack; 512*3448Sdh155122 ipsecespparam_t *espp; 513*3448Sdh155122 514*3448Sdh155122 espstack = (ipsecesp_stack_t *)kmem_zalloc(sizeof (*espstack), 515*3448Sdh155122 KM_SLEEP); 516*3448Sdh155122 espstack->ipsecesp_netstack = ns; 517*3448Sdh155122 518*3448Sdh155122 espp = (ipsecespparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 519*3448Sdh155122 espstack->ipsecesp_params = espp; 520*3448Sdh155122 bcopy(lcl_param_arr, espp, sizeof (lcl_param_arr)); 521*3448Sdh155122 522*3448Sdh155122 (void) ipsecesp_param_register(&espstack->ipsecesp_g_nd, espp, 523*3448Sdh155122 A_CNT(lcl_param_arr)); 524*3448Sdh155122 525*3448Sdh155122 (void) esp_kstat_init(espstack, stackid); 526*3448Sdh155122 527*3448Sdh155122 espstack->esp_sadb.s_acquire_timeout = 528*3448Sdh155122 &espstack->ipsecesp_acquire_timeout; 529*3448Sdh155122 espstack->esp_sadb.s_acqfn = esp_send_acquire; 530*3448Sdh155122 sadbp_init("ESP", &espstack->esp_sadb, SADB_SATYPE_ESP, esp_hash_size, 531*3448Sdh155122 espstack->ipsecesp_netstack); 532*3448Sdh155122 533*3448Sdh155122 mutex_init(&espstack->ipsecesp_param_lock, NULL, MUTEX_DEFAULT, 0); 534*3448Sdh155122 535*3448Sdh155122 ip_drop_register(&espstack->esp_dropper, "IPsec ESP"); 536*3448Sdh155122 return (espstack); 537*3448Sdh155122 } 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate /* 5400Sstevel@tonic-gate * Destroy things for ESP at module unload time. 5410Sstevel@tonic-gate */ 5420Sstevel@tonic-gate void 5430Sstevel@tonic-gate ipsecesp_ddi_destroy(void) 5440Sstevel@tonic-gate { 545*3448Sdh155122 netstack_unregister(NS_IPSECESP); 5460Sstevel@tonic-gate taskq_destroy(esp_taskq); 547*3448Sdh155122 } 548*3448Sdh155122 549*3448Sdh155122 /* 550*3448Sdh155122 * Destroy things for ESP for one stack instance 551*3448Sdh155122 */ 552*3448Sdh155122 static void 553*3448Sdh155122 ipsecesp_stack_fini(netstackid_t stackid, void *arg) 554*3448Sdh155122 { 555*3448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)arg; 556*3448Sdh155122 557*3448Sdh155122 if (espstack->esp_pfkey_q != NULL) { 558*3448Sdh155122 (void) quntimeout(espstack->esp_pfkey_q, espstack->esp_event); 559*3448Sdh155122 } 560*3448Sdh155122 espstack->esp_sadb.s_acqfn = NULL; 561*3448Sdh155122 espstack->esp_sadb.s_acquire_timeout = NULL; 562*3448Sdh155122 sadbp_destroy(&espstack->esp_sadb, espstack->ipsecesp_netstack); 563*3448Sdh155122 ip_drop_unregister(&espstack->esp_dropper); 564*3448Sdh155122 mutex_destroy(&espstack->ipsecesp_param_lock); 565*3448Sdh155122 nd_free(&espstack->ipsecesp_g_nd); 566*3448Sdh155122 567*3448Sdh155122 kmem_free(espstack->ipsecesp_params, sizeof (lcl_param_arr)); 568*3448Sdh155122 espstack->ipsecesp_params = NULL; 569*3448Sdh155122 kstat_delete_netstack(espstack->esp_ksp, stackid); 570*3448Sdh155122 espstack->esp_ksp = NULL; 571*3448Sdh155122 espstack->esp_kstats = NULL; 572*3448Sdh155122 kmem_free(espstack, sizeof (*espstack)); 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate /* 5760Sstevel@tonic-gate * ESP module open routine. 5770Sstevel@tonic-gate */ 5780Sstevel@tonic-gate /* ARGSUSED */ 5790Sstevel@tonic-gate static int 5800Sstevel@tonic-gate ipsecesp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 5810Sstevel@tonic-gate { 582*3448Sdh155122 netstack_t *ns; 583*3448Sdh155122 ipsecesp_stack_t *espstack; 584*3448Sdh155122 585*3448Sdh155122 if (secpolicy_ip_config(credp, B_FALSE) != 0) { 586*3448Sdh155122 esp0dbg(("Non-privileged user trying to open ipsecesp.\n")); 5870Sstevel@tonic-gate return (EPERM); 5880Sstevel@tonic-gate } 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate if (q->q_ptr != NULL) 5910Sstevel@tonic-gate return (0); /* Re-open of an already open instance. */ 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate if (sflag != MODOPEN) 5940Sstevel@tonic-gate return (EINVAL); 5950Sstevel@tonic-gate 596*3448Sdh155122 ns = netstack_find_by_cred(credp); 597*3448Sdh155122 ASSERT(ns != NULL); 598*3448Sdh155122 espstack = ns->netstack_ipsecesp; 599*3448Sdh155122 ASSERT(espstack != NULL); 600*3448Sdh155122 6010Sstevel@tonic-gate /* 6020Sstevel@tonic-gate * ASSUMPTIONS (because I'm MT_OCEXCL): 6030Sstevel@tonic-gate * 6040Sstevel@tonic-gate * * I'm being pushed on top of IP for all my opens (incl. #1). 6050Sstevel@tonic-gate * * Only ipsecesp_open() can write into esp_sadb.s_ip_q. 6060Sstevel@tonic-gate * * Because of this, I can check lazily for esp_sadb.s_ip_q. 6070Sstevel@tonic-gate * 6080Sstevel@tonic-gate * If these assumptions are wrong, I'm in BIG trouble... 6090Sstevel@tonic-gate */ 6100Sstevel@tonic-gate 611*3448Sdh155122 q->q_ptr = espstack; 612*3448Sdh155122 WR(q)->q_ptr = q->q_ptr; 613*3448Sdh155122 614*3448Sdh155122 if (espstack->esp_sadb.s_ip_q == NULL) { 6150Sstevel@tonic-gate struct T_unbind_req *tur; 6160Sstevel@tonic-gate 617*3448Sdh155122 espstack->esp_sadb.s_ip_q = WR(q); 6180Sstevel@tonic-gate /* Allocate an unbind... */ 619*3448Sdh155122 espstack->esp_ip_unbind = allocb(sizeof (struct T_unbind_req), 620*3448Sdh155122 BPRI_HI); 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate /* 6230Sstevel@tonic-gate * Send down T_BIND_REQ to bind IPPROTO_ESP. 6240Sstevel@tonic-gate * Handle the ACK here in ESP. 6250Sstevel@tonic-gate */ 6260Sstevel@tonic-gate qprocson(q); 627*3448Sdh155122 if (espstack->esp_ip_unbind == NULL || 628*3448Sdh155122 !sadb_t_bind_req(espstack->esp_sadb.s_ip_q, IPPROTO_ESP)) { 629*3448Sdh155122 if (espstack->esp_ip_unbind != NULL) { 630*3448Sdh155122 freeb(espstack->esp_ip_unbind); 631*3448Sdh155122 espstack->esp_ip_unbind = NULL; 6320Sstevel@tonic-gate } 6330Sstevel@tonic-gate q->q_ptr = NULL; 634*3448Sdh155122 netstack_rele(espstack->ipsecesp_netstack); 6350Sstevel@tonic-gate return (ENOMEM); 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate 638*3448Sdh155122 espstack->esp_ip_unbind->b_datap->db_type = M_PROTO; 639*3448Sdh155122 tur = (struct T_unbind_req *)espstack->esp_ip_unbind->b_rptr; 6400Sstevel@tonic-gate tur->PRIM_type = T_UNBIND_REQ; 6410Sstevel@tonic-gate } else { 6420Sstevel@tonic-gate qprocson(q); 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate /* 6460Sstevel@tonic-gate * For now, there's not much I can do. I'll be getting a message 6470Sstevel@tonic-gate * passed down to me from keysock (in my wput), and a T_BIND_ACK 6480Sstevel@tonic-gate * up from IP (in my rput). 6490Sstevel@tonic-gate */ 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate return (0); 6520Sstevel@tonic-gate } 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate /* 6550Sstevel@tonic-gate * ESP module close routine. 6560Sstevel@tonic-gate */ 6570Sstevel@tonic-gate static int 6580Sstevel@tonic-gate ipsecesp_close(queue_t *q) 6590Sstevel@tonic-gate { 660*3448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)q->q_ptr; 661*3448Sdh155122 6620Sstevel@tonic-gate /* 6630Sstevel@tonic-gate * If esp_sadb.s_ip_q is attached to this instance, send a 6640Sstevel@tonic-gate * T_UNBIND_REQ to IP for the instance before doing 6650Sstevel@tonic-gate * a qprocsoff(). 6660Sstevel@tonic-gate */ 667*3448Sdh155122 if (WR(q) == espstack->esp_sadb.s_ip_q && 668*3448Sdh155122 espstack->esp_ip_unbind != NULL) { 669*3448Sdh155122 putnext(WR(q), espstack->esp_ip_unbind); 670*3448Sdh155122 espstack->esp_ip_unbind = NULL; 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * Clean up q_ptr, if needed. 6750Sstevel@tonic-gate */ 6760Sstevel@tonic-gate qprocsoff(q); 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate /* Keysock queue check is safe, because of OCEXCL perimeter. */ 6790Sstevel@tonic-gate 680*3448Sdh155122 if (q == espstack->esp_pfkey_q) { 681*3448Sdh155122 esp1dbg(espstack, 682*3448Sdh155122 ("ipsecesp_close: Ummm... keysock is closing ESP.\n")); 683*3448Sdh155122 espstack->esp_pfkey_q = NULL; 6840Sstevel@tonic-gate /* Detach qtimeouts. */ 685*3448Sdh155122 (void) quntimeout(q, espstack->esp_event); 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate 688*3448Sdh155122 if (WR(q) == espstack->esp_sadb.s_ip_q) { 6890Sstevel@tonic-gate /* 6900Sstevel@tonic-gate * If the esp_sadb.s_ip_q is attached to this instance, find 6910Sstevel@tonic-gate * another. The OCEXCL outer perimeter helps us here. 6920Sstevel@tonic-gate */ 693*3448Sdh155122 espstack->esp_sadb.s_ip_q = NULL; 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate /* 6960Sstevel@tonic-gate * Find a replacement queue for esp_sadb.s_ip_q. 6970Sstevel@tonic-gate */ 698*3448Sdh155122 if (espstack->esp_pfkey_q != NULL && 699*3448Sdh155122 espstack->esp_pfkey_q != RD(q)) { 7000Sstevel@tonic-gate /* 7010Sstevel@tonic-gate * See if we can use the pfkey_q. 7020Sstevel@tonic-gate */ 703*3448Sdh155122 espstack->esp_sadb.s_ip_q = WR(espstack->esp_pfkey_q); 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate 706*3448Sdh155122 if (espstack->esp_sadb.s_ip_q == NULL || 707*3448Sdh155122 !sadb_t_bind_req(espstack->esp_sadb.s_ip_q, IPPROTO_ESP)) { 708*3448Sdh155122 esp1dbg(espstack, ("ipsecesp: Can't reassign ip_q.\n")); 709*3448Sdh155122 espstack->esp_sadb.s_ip_q = NULL; 7100Sstevel@tonic-gate } else { 711*3448Sdh155122 espstack->esp_ip_unbind = 712*3448Sdh155122 allocb(sizeof (struct T_unbind_req), BPRI_HI); 713*3448Sdh155122 714*3448Sdh155122 if (espstack->esp_ip_unbind != NULL) { 7150Sstevel@tonic-gate struct T_unbind_req *tur; 7160Sstevel@tonic-gate 717*3448Sdh155122 espstack->esp_ip_unbind->b_datap->db_type = 718*3448Sdh155122 M_PROTO; 7190Sstevel@tonic-gate tur = (struct T_unbind_req *) 720*3448Sdh155122 espstack->esp_ip_unbind->b_rptr; 7210Sstevel@tonic-gate tur->PRIM_type = T_UNBIND_REQ; 7220Sstevel@tonic-gate } 7230Sstevel@tonic-gate /* If it's NULL, I can't do much here. */ 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate 727*3448Sdh155122 netstack_rele(espstack->ipsecesp_netstack); 7280Sstevel@tonic-gate return (0); 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate /* 7320Sstevel@tonic-gate * Add a number of bytes to what the SA has protected so far. Return 7330Sstevel@tonic-gate * B_TRUE if the SA can still protect that many bytes. 7340Sstevel@tonic-gate * 7350Sstevel@tonic-gate * Caller must REFRELE the passed-in assoc. This function must REFRELE 7360Sstevel@tonic-gate * any obtained peer SA. 7370Sstevel@tonic-gate */ 7380Sstevel@tonic-gate static boolean_t 7390Sstevel@tonic-gate esp_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound) 7400Sstevel@tonic-gate { 7410Sstevel@tonic-gate ipsa_t *inassoc, *outassoc; 7420Sstevel@tonic-gate isaf_t *bucket; 7430Sstevel@tonic-gate boolean_t inrc, outrc, isv6; 7440Sstevel@tonic-gate sadb_t *sp; 7450Sstevel@tonic-gate int outhash; 746*3448Sdh155122 netstack_t *ns = assoc->ipsa_netstack; 747*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate /* No peer? No problem! */ 7500Sstevel@tonic-gate if (!assoc->ipsa_haspeer) { 751*3448Sdh155122 return (sadb_age_bytes(espstack->esp_pfkey_q, assoc, bytes, 7520Sstevel@tonic-gate B_TRUE)); 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate /* 7560Sstevel@tonic-gate * Otherwise, we want to grab both the original assoc and its peer. 7570Sstevel@tonic-gate * There might be a race for this, but if it's a real race, two 7580Sstevel@tonic-gate * expire messages may occur. We limit this by only sending the 7590Sstevel@tonic-gate * expire message on one of the peers, we'll pick the inbound 7600Sstevel@tonic-gate * arbitrarily. 7610Sstevel@tonic-gate * 7620Sstevel@tonic-gate * If we need tight synchronization on the peer SA, then we need to 7630Sstevel@tonic-gate * reconsider. 7640Sstevel@tonic-gate */ 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate /* Use address length to select IPv6/IPv4 */ 7670Sstevel@tonic-gate isv6 = (assoc->ipsa_addrfam == AF_INET6); 768*3448Sdh155122 sp = isv6 ? &espstack->esp_sadb.s_v6 : &espstack->esp_sadb.s_v4; 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate if (inbound) { 7710Sstevel@tonic-gate inassoc = assoc; 7720Sstevel@tonic-gate if (isv6) { 773564Ssommerfe outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *) 7740Sstevel@tonic-gate &inassoc->ipsa_dstaddr)); 7750Sstevel@tonic-gate } else { 776564Ssommerfe outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *) 7770Sstevel@tonic-gate &inassoc->ipsa_dstaddr)); 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate bucket = &sp->sdb_of[outhash]; 7800Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock); 7810Sstevel@tonic-gate outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi, 7820Sstevel@tonic-gate inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr, 7830Sstevel@tonic-gate inassoc->ipsa_addrfam); 7840Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock); 7850Sstevel@tonic-gate if (outassoc == NULL) { 7860Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */ 7870Sstevel@tonic-gate esp0dbg(("esp_age_bytes: " 7880Sstevel@tonic-gate "can't find peer for inbound.\n")); 789*3448Sdh155122 return (sadb_age_bytes(espstack->esp_pfkey_q, inassoc, 7900Sstevel@tonic-gate bytes, B_TRUE)); 7910Sstevel@tonic-gate } 7920Sstevel@tonic-gate } else { 7930Sstevel@tonic-gate outassoc = assoc; 794564Ssommerfe bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi); 7950Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock); 7960Sstevel@tonic-gate inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi, 7970Sstevel@tonic-gate outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr, 7980Sstevel@tonic-gate outassoc->ipsa_addrfam); 7990Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock); 8000Sstevel@tonic-gate if (inassoc == NULL) { 8010Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */ 8020Sstevel@tonic-gate esp0dbg(("esp_age_bytes: " 8030Sstevel@tonic-gate "can't find peer for outbound.\n")); 804*3448Sdh155122 return (sadb_age_bytes(espstack->esp_pfkey_q, outassoc, 8050Sstevel@tonic-gate bytes, B_TRUE)); 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate } 8080Sstevel@tonic-gate 809*3448Sdh155122 inrc = sadb_age_bytes(espstack->esp_pfkey_q, inassoc, bytes, B_TRUE); 810*3448Sdh155122 outrc = sadb_age_bytes(espstack->esp_pfkey_q, outassoc, bytes, B_FALSE); 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate /* 8130Sstevel@tonic-gate * REFRELE any peer SA. 8140Sstevel@tonic-gate * 8150Sstevel@tonic-gate * Because of the multi-line macro nature of IPSA_REFRELE, keep 8160Sstevel@tonic-gate * them in { }. 8170Sstevel@tonic-gate */ 8180Sstevel@tonic-gate if (inbound) { 8190Sstevel@tonic-gate IPSA_REFRELE(outassoc); 8200Sstevel@tonic-gate } else { 8210Sstevel@tonic-gate IPSA_REFRELE(inassoc); 8220Sstevel@tonic-gate } 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate return (inrc && outrc); 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate /* 8280Sstevel@tonic-gate * Do incoming NAT-T manipulations for packet. 8290Sstevel@tonic-gate */ 8300Sstevel@tonic-gate static ipsec_status_t 8310Sstevel@tonic-gate esp_fix_natt_checksums(mblk_t *data_mp, ipsa_t *assoc) 8320Sstevel@tonic-gate { 8330Sstevel@tonic-gate ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 8340Sstevel@tonic-gate tcpha_t *tcph; 8350Sstevel@tonic-gate udpha_t *udpha; 8360Sstevel@tonic-gate /* Initialize to our inbound cksum adjustment... */ 8370Sstevel@tonic-gate uint32_t sum = assoc->ipsa_inbound_cksum; 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate switch (ipha->ipha_protocol) { 8400Sstevel@tonic-gate case IPPROTO_TCP: 8410Sstevel@tonic-gate tcph = (tcpha_t *)(data_mp->b_rptr + 8420Sstevel@tonic-gate IPH_HDR_LENGTH(ipha)); 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate #define DOWN_SUM(x) (x) = ((x) & 0xFFFF) + ((x) >> 16) 8450Sstevel@tonic-gate sum += ~ntohs(tcph->tha_sum) & 0xFFFF; 8460Sstevel@tonic-gate DOWN_SUM(sum); 8470Sstevel@tonic-gate DOWN_SUM(sum); 8480Sstevel@tonic-gate tcph->tha_sum = ~htons(sum); 8490Sstevel@tonic-gate break; 8500Sstevel@tonic-gate case IPPROTO_UDP: 8510Sstevel@tonic-gate udpha = (udpha_t *)(data_mp->b_rptr + IPH_HDR_LENGTH(ipha)); 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate if (udpha->uha_checksum != 0) { 8540Sstevel@tonic-gate /* Adujst if the inbound one was not zero. */ 8550Sstevel@tonic-gate sum += ~ntohs(udpha->uha_checksum) & 0xFFFF; 8560Sstevel@tonic-gate DOWN_SUM(sum); 8570Sstevel@tonic-gate DOWN_SUM(sum); 8580Sstevel@tonic-gate udpha->uha_checksum = ~htons(sum); 8590Sstevel@tonic-gate if (udpha->uha_checksum == 0) 8600Sstevel@tonic-gate udpha->uha_checksum = 0xFFFF; 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate #undef DOWN_SUM 8630Sstevel@tonic-gate break; 8640Sstevel@tonic-gate case IPPROTO_IP: 8650Sstevel@tonic-gate /* 8660Sstevel@tonic-gate * This case is only an issue for self-encapsulated 8670Sstevel@tonic-gate * packets. So for now, fall through. 8680Sstevel@tonic-gate */ 8690Sstevel@tonic-gate break; 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate return (IPSEC_STATUS_SUCCESS); 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate /* 8763192Sdanmcd * Strip ESP header, check padding, and fix IP header. 8770Sstevel@tonic-gate * Returns B_TRUE on success, B_FALSE if an error occured. 8780Sstevel@tonic-gate */ 8790Sstevel@tonic-gate static boolean_t 8800Sstevel@tonic-gate esp_strip_header(mblk_t *data_mp, boolean_t isv4, uint32_t ivlen, 881*3448Sdh155122 kstat_named_t **counter, ipsecesp_stack_t *espstack) 8820Sstevel@tonic-gate { 8830Sstevel@tonic-gate ipha_t *ipha; 8840Sstevel@tonic-gate ip6_t *ip6h; 8850Sstevel@tonic-gate uint_t divpoint; 8860Sstevel@tonic-gate mblk_t *scratch; 8870Sstevel@tonic-gate uint8_t nexthdr, padlen; 8880Sstevel@tonic-gate uint8_t lastpad; 889*3448Sdh155122 ipsec_stack_t *ipss = espstack->ipsecesp_netstack->netstack_ipsec; 8903192Sdanmcd uint8_t *lastbyte; 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate /* 8930Sstevel@tonic-gate * Strip ESP data and fix IP header. 8940Sstevel@tonic-gate * 8950Sstevel@tonic-gate * XXX In case the beginning of esp_inbound() changes to not do a 8960Sstevel@tonic-gate * pullup, this part of the code can remain unchanged. 8970Sstevel@tonic-gate */ 8980Sstevel@tonic-gate if (isv4) { 8990Sstevel@tonic-gate ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ipha_t)); 9000Sstevel@tonic-gate ipha = (ipha_t *)data_mp->b_rptr; 9010Sstevel@tonic-gate ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (esph_t) + 9020Sstevel@tonic-gate IPH_HDR_LENGTH(ipha)); 9030Sstevel@tonic-gate divpoint = IPH_HDR_LENGTH(ipha); 9040Sstevel@tonic-gate } else { 9050Sstevel@tonic-gate ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ip6_t)); 9060Sstevel@tonic-gate ip6h = (ip6_t *)data_mp->b_rptr; 9070Sstevel@tonic-gate divpoint = ip_hdr_length_v6(data_mp, ip6h); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate scratch = data_mp; 9110Sstevel@tonic-gate while (scratch->b_cont != NULL) 9120Sstevel@tonic-gate scratch = scratch->b_cont; 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate ASSERT((scratch->b_wptr - scratch->b_rptr) >= 3); 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate /* 9170Sstevel@tonic-gate * "Next header" and padding length are the last two bytes in the 9180Sstevel@tonic-gate * ESP-protected datagram, thus the explicit - 1 and - 2. 9190Sstevel@tonic-gate * lastpad is the last byte of the padding, which can be used for 9200Sstevel@tonic-gate * a quick check to see if the padding is correct. 9210Sstevel@tonic-gate */ 9223192Sdanmcd lastbyte = scratch->b_wptr - 1; 9233192Sdanmcd nexthdr = *lastbyte--; 9243192Sdanmcd padlen = *lastbyte--; 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate if (isv4) { 9270Sstevel@tonic-gate /* Fix part of the IP header. */ 9280Sstevel@tonic-gate ipha->ipha_protocol = nexthdr; 9290Sstevel@tonic-gate /* 9300Sstevel@tonic-gate * Reality check the padlen. The explicit - 2 is for the 9310Sstevel@tonic-gate * padding length and the next-header bytes. 9320Sstevel@tonic-gate */ 9330Sstevel@tonic-gate if (padlen >= ntohs(ipha->ipha_length) - sizeof (ipha_t) - 2 - 9340Sstevel@tonic-gate sizeof (esph_t) - ivlen) { 935*3448Sdh155122 ESP_BUMP_STAT(espstack, bad_decrypt); 936*3448Sdh155122 ipsec_rl_strlog(espstack->ipsecesp_netstack, 937*3448Sdh155122 info.mi_idnum, 0, 0, 938*3448Sdh155122 SL_ERROR | SL_WARN, 9393192Sdanmcd "Corrupt ESP packet (padlen too big).\n"); 940*3448Sdh155122 esp1dbg(espstack, ("padlen (%d) is greater than:\n", 941*3448Sdh155122 padlen)); 942*3448Sdh155122 esp1dbg(espstack, ("pkt len(%d) - ip hdr - esp " 943*3448Sdh155122 "hdr - ivlen(%d) = %d.\n", 944*3448Sdh155122 ntohs(ipha->ipha_length), ivlen, 9450Sstevel@tonic-gate (int)(ntohs(ipha->ipha_length) - sizeof (ipha_t) - 946*3448Sdh155122 2 - sizeof (esph_t) - ivlen))); 947*3448Sdh155122 *counter = DROPPER(ipss, ipds_esp_bad_padlen); 9480Sstevel@tonic-gate return (B_FALSE); 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate /* 9520Sstevel@tonic-gate * Fix the rest of the header. The explicit - 2 is for the 9530Sstevel@tonic-gate * padding length and the next-header bytes. 9540Sstevel@tonic-gate */ 9550Sstevel@tonic-gate ipha->ipha_length = htons(ntohs(ipha->ipha_length) - padlen - 9560Sstevel@tonic-gate 2 - sizeof (esph_t) - ivlen); 9570Sstevel@tonic-gate ipha->ipha_hdr_checksum = 0; 9580Sstevel@tonic-gate ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha); 9590Sstevel@tonic-gate } else { 9600Sstevel@tonic-gate if (ip6h->ip6_nxt == IPPROTO_ESP) { 9610Sstevel@tonic-gate ip6h->ip6_nxt = nexthdr; 9620Sstevel@tonic-gate } else { 9630Sstevel@tonic-gate ip6_pkt_t ipp; 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate bzero(&ipp, sizeof (ipp)); 9660Sstevel@tonic-gate (void) ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL); 9670Sstevel@tonic-gate if (ipp.ipp_dstopts != NULL) { 9680Sstevel@tonic-gate ipp.ipp_dstopts->ip6d_nxt = nexthdr; 9690Sstevel@tonic-gate } else if (ipp.ipp_rthdr != NULL) { 9700Sstevel@tonic-gate ipp.ipp_rthdr->ip6r_nxt = nexthdr; 9710Sstevel@tonic-gate } else if (ipp.ipp_hopopts != NULL) { 9720Sstevel@tonic-gate ipp.ipp_hopopts->ip6h_nxt = nexthdr; 9730Sstevel@tonic-gate } else { 9740Sstevel@tonic-gate /* Panic a DEBUG kernel. */ 9750Sstevel@tonic-gate ASSERT(ipp.ipp_hopopts != NULL); 9760Sstevel@tonic-gate /* Otherwise, pretend it's IP + ESP. */ 9770Sstevel@tonic-gate cmn_err(CE_WARN, "ESP IPv6 headers wrong.\n"); 9780Sstevel@tonic-gate ip6h->ip6_nxt = nexthdr; 9790Sstevel@tonic-gate } 9800Sstevel@tonic-gate } 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate if (padlen >= ntohs(ip6h->ip6_plen) - 2 - sizeof (esph_t) - 9830Sstevel@tonic-gate ivlen) { 984*3448Sdh155122 ESP_BUMP_STAT(espstack, bad_decrypt); 985*3448Sdh155122 ipsec_rl_strlog(espstack->ipsecesp_netstack, 986*3448Sdh155122 info.mi_idnum, 0, 0, 987*3448Sdh155122 SL_ERROR | SL_WARN, 9883192Sdanmcd "Corrupt ESP packet (v6 padlen too big).\n"); 989*3448Sdh155122 esp1dbg(espstack, ("padlen (%d) is greater than:\n", 990*3448Sdh155122 padlen)); 991*3448Sdh155122 esp1dbg(espstack, ("pkt len(%u) - ip hdr - esp " 992*3448Sdh155122 "hdr - ivlen(%d) = %u.\n", 993*3448Sdh155122 (unsigned)(ntohs(ip6h->ip6_plen) 994*3448Sdh155122 + sizeof (ip6_t)), ivlen, 995*3448Sdh155122 (unsigned)(ntohs(ip6h->ip6_plen) - 2 - 996*3448Sdh155122 sizeof (esph_t) - ivlen))); 997*3448Sdh155122 *counter = DROPPER(ipss, ipds_esp_bad_padlen); 9980Sstevel@tonic-gate return (B_FALSE); 9990Sstevel@tonic-gate } 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate 10020Sstevel@tonic-gate /* 10030Sstevel@tonic-gate * Fix the rest of the header. The explicit - 2 is for the 10040Sstevel@tonic-gate * padding length and the next-header bytes. IPv6 is nice, 10050Sstevel@tonic-gate * because there's no hdr checksum! 10060Sstevel@tonic-gate */ 10070Sstevel@tonic-gate ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - padlen - 10080Sstevel@tonic-gate 2 - sizeof (esph_t) - ivlen); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate 1011*3448Sdh155122 if (espstack->ipsecesp_padding_check > 0 && padlen > 0) { 10123192Sdanmcd /* 10133192Sdanmcd * Weak padding check: compare last-byte to length, they 10143192Sdanmcd * should be equal. 10153192Sdanmcd */ 10163192Sdanmcd lastpad = *lastbyte--; 10173192Sdanmcd 10183192Sdanmcd if (padlen != lastpad) { 1019*3448Sdh155122 ipsec_rl_strlog(espstack->ipsecesp_netstack, 1020*3448Sdh155122 info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 10213192Sdanmcd "Corrupt ESP packet (lastpad != padlen).\n"); 1022*3448Sdh155122 esp1dbg(espstack, 1023*3448Sdh155122 ("lastpad (%d) not equal to padlen (%d):\n", 1024*3448Sdh155122 lastpad, padlen)); 1025*3448Sdh155122 ESP_BUMP_STAT(espstack, bad_padding); 1026*3448Sdh155122 *counter = DROPPER(ipss, ipds_esp_bad_padding); 10273192Sdanmcd return (B_FALSE); 10283192Sdanmcd } 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate /* 10313192Sdanmcd * Strong padding check: Check all pad bytes to see that 10323192Sdanmcd * they're ascending. Go backwards using a descending counter 10333192Sdanmcd * to verify. padlen == 1 is checked by previous block, so 10343192Sdanmcd * only bother if we've more than 1 byte of padding. 10353192Sdanmcd * Consequently, start the check one byte before the location 10363192Sdanmcd * of "lastpad". 10370Sstevel@tonic-gate */ 1038*3448Sdh155122 if (espstack->ipsecesp_padding_check > 1) { 10393192Sdanmcd /* 10403192Sdanmcd * This assert may have to become an if and a pullup 10413192Sdanmcd * if we start accepting multi-dblk mblks. For now, 10423192Sdanmcd * though, any packet here will have been pulled up in 10433192Sdanmcd * esp_inbound. 10443192Sdanmcd */ 10453192Sdanmcd ASSERT(MBLKL(scratch) >= lastpad + 3); 10463192Sdanmcd 10473192Sdanmcd /* 10483192Sdanmcd * Use "--lastpad" because we already checked the very 10493192Sdanmcd * last pad byte previously. 10503192Sdanmcd */ 10513192Sdanmcd while (--lastpad != 0) { 10523192Sdanmcd if (lastpad != *lastbyte) { 1053*3448Sdh155122 ipsec_rl_strlog( 1054*3448Sdh155122 espstack->ipsecesp_netstack, 1055*3448Sdh155122 info.mi_idnum, 0, 0, 10563192Sdanmcd SL_ERROR | SL_WARN, "Corrupt ESP " 10573192Sdanmcd "packet (bad padding).\n"); 1058*3448Sdh155122 esp1dbg(espstack, 1059*3448Sdh155122 ("padding not in correct" 1060*3448Sdh155122 " format:\n")); 1061*3448Sdh155122 ESP_BUMP_STAT(espstack, bad_padding); 1062*3448Sdh155122 *counter = DROPPER(ipss, 1063*3448Sdh155122 ipds_esp_bad_padding); 10643192Sdanmcd return (B_FALSE); 10653192Sdanmcd } 10663192Sdanmcd lastbyte--; 10670Sstevel@tonic-gate } 10680Sstevel@tonic-gate } 10690Sstevel@tonic-gate } 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate /* Trim off the padding. */ 10720Sstevel@tonic-gate ASSERT(data_mp->b_cont == NULL); 10730Sstevel@tonic-gate data_mp->b_wptr -= (padlen + 2); 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate /* 10760Sstevel@tonic-gate * Remove the ESP header. 10770Sstevel@tonic-gate * 10780Sstevel@tonic-gate * The above assertions about data_mp's size will make this work. 10790Sstevel@tonic-gate * 10800Sstevel@tonic-gate * XXX Question: If I send up and get back a contiguous mblk, 10810Sstevel@tonic-gate * would it be quicker to bcopy over, or keep doing the dupb stuff? 10820Sstevel@tonic-gate * I go with copying for now. 10830Sstevel@tonic-gate */ 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate if (IS_P2ALIGNED(data_mp->b_rptr, sizeof (uint32_t)) && 10860Sstevel@tonic-gate IS_P2ALIGNED(ivlen, sizeof (uint32_t))) { 10870Sstevel@tonic-gate uint8_t *start = data_mp->b_rptr; 10880Sstevel@tonic-gate uint32_t *src, *dst; 10890Sstevel@tonic-gate 10900Sstevel@tonic-gate src = (uint32_t *)(start + divpoint); 10910Sstevel@tonic-gate dst = (uint32_t *)(start + divpoint + sizeof (esph_t) + ivlen); 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate ASSERT(IS_P2ALIGNED(dst, sizeof (uint32_t)) && 10940Sstevel@tonic-gate IS_P2ALIGNED(src, sizeof (uint32_t))); 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate do { 10970Sstevel@tonic-gate src--; 10980Sstevel@tonic-gate dst--; 10990Sstevel@tonic-gate *dst = *src; 11000Sstevel@tonic-gate } while (src != (uint32_t *)start); 11010Sstevel@tonic-gate 11020Sstevel@tonic-gate data_mp->b_rptr = (uchar_t *)dst; 11030Sstevel@tonic-gate } else { 11040Sstevel@tonic-gate uint8_t *start = data_mp->b_rptr; 11050Sstevel@tonic-gate uint8_t *src, *dst; 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate src = start + divpoint; 11080Sstevel@tonic-gate dst = src + sizeof (esph_t) + ivlen; 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate do { 11110Sstevel@tonic-gate src--; 11120Sstevel@tonic-gate dst--; 11130Sstevel@tonic-gate *dst = *src; 11140Sstevel@tonic-gate } while (src != start); 11150Sstevel@tonic-gate 11160Sstevel@tonic-gate data_mp->b_rptr = dst; 11170Sstevel@tonic-gate } 11180Sstevel@tonic-gate 1119*3448Sdh155122 esp2dbg(espstack, ("data_mp after inbound ESP adjustment:\n")); 1120*3448Sdh155122 esp2dbg(espstack, (dump_msg(data_mp))); 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate return (B_TRUE); 11230Sstevel@tonic-gate } 11240Sstevel@tonic-gate 11250Sstevel@tonic-gate /* 11260Sstevel@tonic-gate * Updating use times can be tricky business if the ipsa_haspeer flag is 11270Sstevel@tonic-gate * set. This function is called once in an SA's lifetime. 11280Sstevel@tonic-gate * 11290Sstevel@tonic-gate * Caller has to REFRELE "assoc" which is passed in. This function has 11300Sstevel@tonic-gate * to REFRELE any peer SA that is obtained. 11310Sstevel@tonic-gate */ 11320Sstevel@tonic-gate static void 11330Sstevel@tonic-gate esp_set_usetime(ipsa_t *assoc, boolean_t inbound) 11340Sstevel@tonic-gate { 11350Sstevel@tonic-gate ipsa_t *inassoc, *outassoc; 11360Sstevel@tonic-gate isaf_t *bucket; 11370Sstevel@tonic-gate sadb_t *sp; 11380Sstevel@tonic-gate int outhash; 11390Sstevel@tonic-gate boolean_t isv6; 1140*3448Sdh155122 netstack_t *ns = assoc->ipsa_netstack; 1141*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate /* No peer? No problem! */ 11440Sstevel@tonic-gate if (!assoc->ipsa_haspeer) { 11450Sstevel@tonic-gate sadb_set_usetime(assoc); 11460Sstevel@tonic-gate return; 11470Sstevel@tonic-gate } 11480Sstevel@tonic-gate 11490Sstevel@tonic-gate /* 11500Sstevel@tonic-gate * Otherwise, we want to grab both the original assoc and its peer. 11510Sstevel@tonic-gate * There might be a race for this, but if it's a real race, the times 11520Sstevel@tonic-gate * will be out-of-synch by at most a second, and since our time 11530Sstevel@tonic-gate * granularity is a second, this won't be a problem. 11540Sstevel@tonic-gate * 11550Sstevel@tonic-gate * If we need tight synchronization on the peer SA, then we need to 11560Sstevel@tonic-gate * reconsider. 11570Sstevel@tonic-gate */ 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate /* Use address length to select IPv6/IPv4 */ 11600Sstevel@tonic-gate isv6 = (assoc->ipsa_addrfam == AF_INET6); 1161*3448Sdh155122 sp = isv6 ? &espstack->esp_sadb.s_v6 : &espstack->esp_sadb.s_v4; 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate if (inbound) { 11640Sstevel@tonic-gate inassoc = assoc; 11650Sstevel@tonic-gate if (isv6) { 1166564Ssommerfe outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *) 11670Sstevel@tonic-gate &inassoc->ipsa_dstaddr)); 11680Sstevel@tonic-gate } else { 1169564Ssommerfe outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *) 11700Sstevel@tonic-gate &inassoc->ipsa_dstaddr)); 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate bucket = &sp->sdb_of[outhash]; 11730Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock); 11740Sstevel@tonic-gate outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi, 11750Sstevel@tonic-gate inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr, 11760Sstevel@tonic-gate inassoc->ipsa_addrfam); 11770Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock); 11780Sstevel@tonic-gate if (outassoc == NULL) { 11790Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */ 11800Sstevel@tonic-gate esp0dbg(("esp_set_usetime: " 11810Sstevel@tonic-gate "can't find peer for inbound.\n")); 11820Sstevel@tonic-gate sadb_set_usetime(inassoc); 11830Sstevel@tonic-gate return; 11840Sstevel@tonic-gate } 11850Sstevel@tonic-gate } else { 11860Sstevel@tonic-gate outassoc = assoc; 1187564Ssommerfe bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi); 11880Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock); 11890Sstevel@tonic-gate inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi, 11900Sstevel@tonic-gate outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr, 11910Sstevel@tonic-gate outassoc->ipsa_addrfam); 11920Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock); 11930Sstevel@tonic-gate if (inassoc == NULL) { 11940Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */ 11950Sstevel@tonic-gate esp0dbg(("esp_set_usetime: " 11960Sstevel@tonic-gate "can't find peer for outbound.\n")); 11970Sstevel@tonic-gate sadb_set_usetime(outassoc); 11980Sstevel@tonic-gate return; 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate } 12010Sstevel@tonic-gate 12020Sstevel@tonic-gate /* Update usetime on both. */ 12030Sstevel@tonic-gate sadb_set_usetime(inassoc); 12040Sstevel@tonic-gate sadb_set_usetime(outassoc); 12050Sstevel@tonic-gate 12060Sstevel@tonic-gate /* 12070Sstevel@tonic-gate * REFRELE any peer SA. 12080Sstevel@tonic-gate * 12090Sstevel@tonic-gate * Because of the multi-line macro nature of IPSA_REFRELE, keep 12100Sstevel@tonic-gate * them in { }. 12110Sstevel@tonic-gate */ 12120Sstevel@tonic-gate if (inbound) { 12130Sstevel@tonic-gate IPSA_REFRELE(outassoc); 12140Sstevel@tonic-gate } else { 12150Sstevel@tonic-gate IPSA_REFRELE(inassoc); 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate /* 12200Sstevel@tonic-gate * Handle ESP inbound data for IPv4 and IPv6. 12210Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the 12220Sstevel@tonic-gate * mblk chain ipsec_in_mp. 12230Sstevel@tonic-gate */ 12240Sstevel@tonic-gate ipsec_status_t 12250Sstevel@tonic-gate esp_inbound(mblk_t *ipsec_in_mp, void *arg) 12260Sstevel@tonic-gate { 12270Sstevel@tonic-gate mblk_t *data_mp = ipsec_in_mp->b_cont; 12280Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr; 12290Sstevel@tonic-gate esph_t *esph = (esph_t *)arg; 12300Sstevel@tonic-gate ipsa_t *ipsa = ii->ipsec_in_esp_sa; 1231*3448Sdh155122 netstack_t *ns = ii->ipsec_in_ns; 1232*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 1233*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate if (ipsa->ipsa_usetime == 0) 12360Sstevel@tonic-gate esp_set_usetime(ipsa, B_TRUE); 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate /* 12390Sstevel@tonic-gate * We may wish to check replay in-range-only here as an optimization. 12400Sstevel@tonic-gate * Include the reality check of ipsa->ipsa_replay > 12410Sstevel@tonic-gate * ipsa->ipsa_replay_wsize for times when it's the first N packets, 12420Sstevel@tonic-gate * where N == ipsa->ipsa_replay_wsize. 12430Sstevel@tonic-gate * 12440Sstevel@tonic-gate * Another check that may come here later is the "collision" check. 12450Sstevel@tonic-gate * If legitimate packets flow quickly enough, this won't be a problem, 12460Sstevel@tonic-gate * but collisions may cause authentication algorithm crunching to 12470Sstevel@tonic-gate * take place when it doesn't need to. 12480Sstevel@tonic-gate */ 12490Sstevel@tonic-gate if (!sadb_replay_peek(ipsa, esph->esph_replay)) { 1250*3448Sdh155122 ESP_BUMP_STAT(espstack, replay_early_failures); 1251*3448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards); 12520Sstevel@tonic-gate /* 12530Sstevel@tonic-gate * TODO: Extract inbound interface from the IPSEC_IN 12540Sstevel@tonic-gate * message's ii->ipsec_in_rill_index. 12550Sstevel@tonic-gate */ 12560Sstevel@tonic-gate ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL, 1257*3448Sdh155122 DROPPER(ipss, ipds_esp_early_replay), 1258*3448Sdh155122 &espstack->esp_dropper); 12590Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 12600Sstevel@tonic-gate } 12610Sstevel@tonic-gate 12620Sstevel@tonic-gate /* 12630Sstevel@tonic-gate * Has this packet already been processed by a hardware 12640Sstevel@tonic-gate * IPsec accelerator? 12650Sstevel@tonic-gate */ 12660Sstevel@tonic-gate if (ii->ipsec_in_accelerated) { 12670Sstevel@tonic-gate ipsec_status_t rv; 1268*3448Sdh155122 esp3dbg(espstack, 1269*3448Sdh155122 ("esp_inbound: pkt processed by ill=%d isv6=%d\n", 12700Sstevel@tonic-gate ii->ipsec_in_ill_index, !ii->ipsec_in_v4)); 12710Sstevel@tonic-gate rv = esp_inbound_accelerated(ipsec_in_mp, 12720Sstevel@tonic-gate data_mp, ii->ipsec_in_v4, ipsa); 12730Sstevel@tonic-gate return (rv); 12740Sstevel@tonic-gate } 1275*3448Sdh155122 ESP_BUMP_STAT(espstack, noaccel); 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate /* 12780Sstevel@tonic-gate * Adjust the IP header's payload length to reflect the removal 12790Sstevel@tonic-gate * of the ICV. 12800Sstevel@tonic-gate */ 12810Sstevel@tonic-gate if (!ii->ipsec_in_v4) { 12820Sstevel@tonic-gate ip6_t *ip6h = (ip6_t *)data_mp->b_rptr; 12830Sstevel@tonic-gate ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 12840Sstevel@tonic-gate ipsa->ipsa_mac_len); 12850Sstevel@tonic-gate } else { 12860Sstevel@tonic-gate ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 12870Sstevel@tonic-gate ipha->ipha_length = htons(ntohs(ipha->ipha_length) - 12880Sstevel@tonic-gate ipsa->ipsa_mac_len); 12890Sstevel@tonic-gate } 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate /* submit the request to the crypto framework */ 12920Sstevel@tonic-gate return (esp_submit_req_inbound(ipsec_in_mp, ipsa, 12930Sstevel@tonic-gate (uint8_t *)esph - data_mp->b_rptr)); 12940Sstevel@tonic-gate } 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate /* 12970Sstevel@tonic-gate * Perform the really difficult work of inserting the proposed situation. 12980Sstevel@tonic-gate * Called while holding the algorithm lock. 12990Sstevel@tonic-gate */ 13000Sstevel@tonic-gate static void 13010Sstevel@tonic-gate esp_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs) 13020Sstevel@tonic-gate { 13030Sstevel@tonic-gate sadb_comb_t *comb = (sadb_comb_t *)(prop + 1); 13040Sstevel@tonic-gate ipsec_out_t *io; 13050Sstevel@tonic-gate ipsec_action_t *ap; 13060Sstevel@tonic-gate ipsec_prot_t *prot; 1307*3448Sdh155122 netstack_t *ns; 1308*3448Sdh155122 ipsecesp_stack_t *espstack; 1309*3448Sdh155122 ipsec_stack_t *ipss; 1310*3448Sdh155122 13110Sstevel@tonic-gate io = (ipsec_out_t *)acqrec->ipsacq_mp->b_rptr; 13120Sstevel@tonic-gate ASSERT(io->ipsec_out_type == IPSEC_OUT); 1313*3448Sdh155122 ns = io->ipsec_out_ns; 1314*3448Sdh155122 espstack = ns->netstack_ipsecesp; 1315*3448Sdh155122 ipss = ns->netstack_ipsec; 1316*3448Sdh155122 ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock)); 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate prop->sadb_prop_exttype = SADB_EXT_PROPOSAL; 13190Sstevel@tonic-gate prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t)); 13200Sstevel@tonic-gate *(uint32_t *)(&prop->sadb_prop_replay) = 0; /* Quick zero-out! */ 13210Sstevel@tonic-gate 1322*3448Sdh155122 prop->sadb_prop_replay = espstack->ipsecesp_replay_size; 13230Sstevel@tonic-gate 13240Sstevel@tonic-gate /* 13250Sstevel@tonic-gate * Based upon algorithm properties, and what-not, prioritize 13260Sstevel@tonic-gate * a proposal. If the IPSEC_OUT message has an algorithm specified, 13270Sstevel@tonic-gate * use it first and foremost. 13280Sstevel@tonic-gate * 13290Sstevel@tonic-gate * For each action in policy list 13300Sstevel@tonic-gate * Add combination. If I've hit limit, return. 13310Sstevel@tonic-gate */ 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate for (ap = acqrec->ipsacq_act; ap != NULL; 13340Sstevel@tonic-gate ap = ap->ipa_next) { 13350Sstevel@tonic-gate ipsec_alginfo_t *ealg = NULL; 13360Sstevel@tonic-gate ipsec_alginfo_t *aalg = NULL; 13370Sstevel@tonic-gate 13380Sstevel@tonic-gate if (ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY) 13390Sstevel@tonic-gate continue; 13400Sstevel@tonic-gate 13410Sstevel@tonic-gate prot = &ap->ipa_act.ipa_apply; 13420Sstevel@tonic-gate 13430Sstevel@tonic-gate if (!(prot->ipp_use_esp)) 13440Sstevel@tonic-gate continue; 13450Sstevel@tonic-gate 13460Sstevel@tonic-gate if (prot->ipp_esp_auth_alg != 0) { 1347*3448Sdh155122 aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH] 13480Sstevel@tonic-gate [prot->ipp_esp_auth_alg]; 13490Sstevel@tonic-gate if (aalg == NULL || !ALG_VALID(aalg)) 13500Sstevel@tonic-gate continue; 13510Sstevel@tonic-gate } 13520Sstevel@tonic-gate 13530Sstevel@tonic-gate ASSERT(prot->ipp_encr_alg > 0); 1354*3448Sdh155122 ealg = ipss->ipsec_alglists[IPSEC_ALG_ENCR] 1355*3448Sdh155122 [prot->ipp_encr_alg]; 13560Sstevel@tonic-gate if (ealg == NULL || !ALG_VALID(ealg)) 13570Sstevel@tonic-gate continue; 13580Sstevel@tonic-gate 13590Sstevel@tonic-gate comb->sadb_comb_flags = 0; 13600Sstevel@tonic-gate comb->sadb_comb_reserved = 0; 13610Sstevel@tonic-gate comb->sadb_comb_encrypt = ealg->alg_id; 13622751Sdanmcd comb->sadb_comb_encrypt_minbits = 13632751Sdanmcd MAX(prot->ipp_espe_minbits, ealg->alg_ef_minbits); 13642751Sdanmcd comb->sadb_comb_encrypt_maxbits = 13652751Sdanmcd MIN(prot->ipp_espe_maxbits, ealg->alg_ef_maxbits); 13660Sstevel@tonic-gate if (aalg == NULL) { 13670Sstevel@tonic-gate comb->sadb_comb_auth = 0; 13680Sstevel@tonic-gate comb->sadb_comb_auth_minbits = 0; 13690Sstevel@tonic-gate comb->sadb_comb_auth_maxbits = 0; 13700Sstevel@tonic-gate } else { 13710Sstevel@tonic-gate comb->sadb_comb_auth = aalg->alg_id; 13722751Sdanmcd comb->sadb_comb_auth_minbits = 13732751Sdanmcd MAX(prot->ipp_espa_minbits, aalg->alg_ef_minbits); 13742751Sdanmcd comb->sadb_comb_auth_maxbits = 13752751Sdanmcd MIN(prot->ipp_espa_maxbits, aalg->alg_ef_maxbits); 13760Sstevel@tonic-gate } 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate /* 13790Sstevel@tonic-gate * The following may be based on algorithm 13800Sstevel@tonic-gate * properties, but in the meantime, we just pick 13810Sstevel@tonic-gate * some good, sensible numbers. Key mgmt. can 13820Sstevel@tonic-gate * (and perhaps should) be the place to finalize 13830Sstevel@tonic-gate * such decisions. 13840Sstevel@tonic-gate */ 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate /* 13870Sstevel@tonic-gate * No limits on allocations, since we really don't 13880Sstevel@tonic-gate * support that concept currently. 13890Sstevel@tonic-gate */ 13900Sstevel@tonic-gate comb->sadb_comb_soft_allocations = 0; 13910Sstevel@tonic-gate comb->sadb_comb_hard_allocations = 0; 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate /* 13940Sstevel@tonic-gate * These may want to come from policy rule.. 13950Sstevel@tonic-gate */ 1396*3448Sdh155122 comb->sadb_comb_soft_bytes = 1397*3448Sdh155122 espstack->ipsecesp_default_soft_bytes; 1398*3448Sdh155122 comb->sadb_comb_hard_bytes = 1399*3448Sdh155122 espstack->ipsecesp_default_hard_bytes; 1400*3448Sdh155122 comb->sadb_comb_soft_addtime = 1401*3448Sdh155122 espstack->ipsecesp_default_soft_addtime; 1402*3448Sdh155122 comb->sadb_comb_hard_addtime = 1403*3448Sdh155122 espstack->ipsecesp_default_hard_addtime; 1404*3448Sdh155122 comb->sadb_comb_soft_usetime = 1405*3448Sdh155122 espstack->ipsecesp_default_soft_usetime; 1406*3448Sdh155122 comb->sadb_comb_hard_usetime = 1407*3448Sdh155122 espstack->ipsecesp_default_hard_usetime; 14080Sstevel@tonic-gate 14090Sstevel@tonic-gate prop->sadb_prop_len += SADB_8TO64(sizeof (*comb)); 14100Sstevel@tonic-gate if (--combs == 0) 14110Sstevel@tonic-gate break; /* out of space.. */ 14120Sstevel@tonic-gate comb++; 14130Sstevel@tonic-gate } 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate 14160Sstevel@tonic-gate /* 14170Sstevel@tonic-gate * Prepare and actually send the SADB_ACQUIRE message to PF_KEY. 14180Sstevel@tonic-gate */ 14190Sstevel@tonic-gate static void 1420*3448Sdh155122 esp_send_acquire(ipsacq_t *acqrec, mblk_t *extended, netstack_t *ns) 14210Sstevel@tonic-gate { 14223055Sdanmcd uint_t combs; 14230Sstevel@tonic-gate sadb_msg_t *samsg; 14240Sstevel@tonic-gate sadb_prop_t *prop; 14253055Sdanmcd mblk_t *pfkeymp, *msgmp; 1426*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 1427*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 1428*3448Sdh155122 1429*3448Sdh155122 ESP_BUMP_STAT(espstack, acquire_requests); 1430*3448Sdh155122 1431*3448Sdh155122 if (espstack->esp_pfkey_q == NULL) 14323055Sdanmcd return; 14330Sstevel@tonic-gate 14340Sstevel@tonic-gate /* Set up ACQUIRE. */ 1435*3448Sdh155122 pfkeymp = sadb_setup_acquire(acqrec, SADB_SATYPE_ESP, 1436*3448Sdh155122 ns->netstack_ipsec); 14373055Sdanmcd if (pfkeymp == NULL) { 14380Sstevel@tonic-gate esp0dbg(("sadb_setup_acquire failed.\n")); 14393055Sdanmcd return; 14400Sstevel@tonic-gate } 1441*3448Sdh155122 ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock)); 1442*3448Sdh155122 combs = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] * 1443*3448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 14443055Sdanmcd msgmp = pfkeymp->b_cont; 14453055Sdanmcd samsg = (sadb_msg_t *)(msgmp->b_rptr); 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate /* Insert proposal here. */ 14480Sstevel@tonic-gate 14490Sstevel@tonic-gate prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len); 14500Sstevel@tonic-gate esp_insert_prop(prop, acqrec, combs); 14510Sstevel@tonic-gate samsg->sadb_msg_len += prop->sadb_prop_len; 14520Sstevel@tonic-gate msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len); 14530Sstevel@tonic-gate 1454*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 14550Sstevel@tonic-gate 14560Sstevel@tonic-gate /* 14570Sstevel@tonic-gate * Must mutex_exit() before sending PF_KEY message up, in 14580Sstevel@tonic-gate * order to avoid recursive mutex_enter() if there are no registered 14590Sstevel@tonic-gate * listeners. 14600Sstevel@tonic-gate * 14610Sstevel@tonic-gate * Once I've sent the message, I'm cool anyway. 14620Sstevel@tonic-gate */ 14630Sstevel@tonic-gate mutex_exit(&acqrec->ipsacq_lock); 14643055Sdanmcd if (extended != NULL) { 1465*3448Sdh155122 putnext(espstack->esp_pfkey_q, extended); 14660Sstevel@tonic-gate } 1467*3448Sdh155122 putnext(espstack->esp_pfkey_q, pfkeymp); 14680Sstevel@tonic-gate } 14690Sstevel@tonic-gate 14700Sstevel@tonic-gate /* 14710Sstevel@tonic-gate * Handle the SADB_GETSPI message. Create a larval SA. 14720Sstevel@tonic-gate */ 14730Sstevel@tonic-gate static void 1474*3448Sdh155122 esp_getspi(mblk_t *mp, keysock_in_t *ksi, ipsecesp_stack_t *espstack) 14750Sstevel@tonic-gate { 14760Sstevel@tonic-gate ipsa_t *newbie, *target; 14770Sstevel@tonic-gate isaf_t *outbound, *inbound; 14780Sstevel@tonic-gate int rc, diagnostic; 14790Sstevel@tonic-gate sadb_sa_t *assoc; 14800Sstevel@tonic-gate keysock_out_t *kso; 14810Sstevel@tonic-gate uint32_t newspi; 14820Sstevel@tonic-gate 14830Sstevel@tonic-gate /* 14840Sstevel@tonic-gate * Randomly generate a proposed SPI value 14850Sstevel@tonic-gate */ 14860Sstevel@tonic-gate (void) random_get_pseudo_bytes((uint8_t *)&newspi, sizeof (uint32_t)); 1487*3448Sdh155122 newbie = sadb_getspi(ksi, newspi, &diagnostic, 1488*3448Sdh155122 espstack->ipsecesp_netstack); 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate if (newbie == NULL) { 1491*3448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, ENOMEM, diagnostic, 14920Sstevel@tonic-gate ksi->ks_in_serial); 14930Sstevel@tonic-gate return; 14940Sstevel@tonic-gate } else if (newbie == (ipsa_t *)-1) { 1495*3448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, EINVAL, diagnostic, 14960Sstevel@tonic-gate ksi->ks_in_serial); 14970Sstevel@tonic-gate return; 14980Sstevel@tonic-gate } 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate /* 15010Sstevel@tonic-gate * XXX - We may randomly collide. We really should recover from this. 15020Sstevel@tonic-gate * Unfortunately, that could require spending way-too-much-time 15030Sstevel@tonic-gate * in here. For now, let the user retry. 15040Sstevel@tonic-gate */ 15050Sstevel@tonic-gate 15060Sstevel@tonic-gate if (newbie->ipsa_addrfam == AF_INET6) { 1507*3448Sdh155122 outbound = OUTBOUND_BUCKET_V6(&espstack->esp_sadb.s_v6, 1508564Ssommerfe *(uint32_t *)(newbie->ipsa_dstaddr)); 1509*3448Sdh155122 inbound = INBOUND_BUCKET(&espstack->esp_sadb.s_v6, 1510*3448Sdh155122 newbie->ipsa_spi); 15110Sstevel@tonic-gate } else { 15120Sstevel@tonic-gate ASSERT(newbie->ipsa_addrfam == AF_INET); 1513*3448Sdh155122 outbound = OUTBOUND_BUCKET_V4(&espstack->esp_sadb.s_v4, 1514564Ssommerfe *(uint32_t *)(newbie->ipsa_dstaddr)); 1515*3448Sdh155122 inbound = INBOUND_BUCKET(&espstack->esp_sadb.s_v4, 1516*3448Sdh155122 newbie->ipsa_spi); 15170Sstevel@tonic-gate } 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate mutex_enter(&outbound->isaf_lock); 15200Sstevel@tonic-gate mutex_enter(&inbound->isaf_lock); 15210Sstevel@tonic-gate 15220Sstevel@tonic-gate /* 15230Sstevel@tonic-gate * Check for collisions (i.e. did sadb_getspi() return with something 15240Sstevel@tonic-gate * that already exists?). 15250Sstevel@tonic-gate * 15260Sstevel@tonic-gate * Try outbound first. Even though SADB_GETSPI is traditionally 15270Sstevel@tonic-gate * for inbound SAs, you never know what a user might do. 15280Sstevel@tonic-gate */ 15290Sstevel@tonic-gate target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi, 15300Sstevel@tonic-gate newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam); 15310Sstevel@tonic-gate if (target == NULL) { 15320Sstevel@tonic-gate target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi, 15330Sstevel@tonic-gate newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, 15340Sstevel@tonic-gate newbie->ipsa_addrfam); 15350Sstevel@tonic-gate } 15360Sstevel@tonic-gate 15370Sstevel@tonic-gate /* 15380Sstevel@tonic-gate * I don't have collisions elsewhere! 15390Sstevel@tonic-gate * (Nor will I because I'm still holding inbound/outbound locks.) 15400Sstevel@tonic-gate */ 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate if (target != NULL) { 15430Sstevel@tonic-gate rc = EEXIST; 15440Sstevel@tonic-gate IPSA_REFRELE(target); 15450Sstevel@tonic-gate } else { 15460Sstevel@tonic-gate /* 15470Sstevel@tonic-gate * sadb_insertassoc() also checks for collisions, so 15480Sstevel@tonic-gate * if there's a colliding entry, rc will be set 15490Sstevel@tonic-gate * to EEXIST. 15500Sstevel@tonic-gate */ 15510Sstevel@tonic-gate rc = sadb_insertassoc(newbie, inbound); 15520Sstevel@tonic-gate (void) drv_getparm(TIME, &newbie->ipsa_hardexpiretime); 1553*3448Sdh155122 newbie->ipsa_hardexpiretime += 1554*3448Sdh155122 espstack->ipsecesp_larval_timeout; 15550Sstevel@tonic-gate } 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate /* 15580Sstevel@tonic-gate * Can exit outbound mutex. Hold inbound until we're done 15590Sstevel@tonic-gate * with newbie. 15600Sstevel@tonic-gate */ 15610Sstevel@tonic-gate mutex_exit(&outbound->isaf_lock); 15620Sstevel@tonic-gate 15630Sstevel@tonic-gate if (rc != 0) { 15640Sstevel@tonic-gate mutex_exit(&inbound->isaf_lock); 15650Sstevel@tonic-gate IPSA_REFRELE(newbie); 1566*3448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, rc, 1567*3448Sdh155122 SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial); 15680Sstevel@tonic-gate return; 15690Sstevel@tonic-gate } 15700Sstevel@tonic-gate 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate /* Can write here because I'm still holding the bucket lock. */ 15730Sstevel@tonic-gate newbie->ipsa_type = SADB_SATYPE_ESP; 15740Sstevel@tonic-gate 15750Sstevel@tonic-gate /* 15760Sstevel@tonic-gate * Construct successful return message. We have one thing going 15770Sstevel@tonic-gate * for us in PF_KEY v2. That's the fact that 15780Sstevel@tonic-gate * sizeof (sadb_spirange_t) == sizeof (sadb_sa_t) 15790Sstevel@tonic-gate */ 15800Sstevel@tonic-gate assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE]; 15810Sstevel@tonic-gate assoc->sadb_sa_exttype = SADB_EXT_SA; 15820Sstevel@tonic-gate assoc->sadb_sa_spi = newbie->ipsa_spi; 15830Sstevel@tonic-gate *((uint64_t *)(&assoc->sadb_sa_replay)) = 0; 15840Sstevel@tonic-gate mutex_exit(&inbound->isaf_lock); 15850Sstevel@tonic-gate 15860Sstevel@tonic-gate /* Convert KEYSOCK_IN to KEYSOCK_OUT. */ 15870Sstevel@tonic-gate kso = (keysock_out_t *)ksi; 15880Sstevel@tonic-gate kso->ks_out_len = sizeof (*kso); 15890Sstevel@tonic-gate kso->ks_out_serial = ksi->ks_in_serial; 15900Sstevel@tonic-gate kso->ks_out_type = KEYSOCK_OUT; 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate /* 15930Sstevel@tonic-gate * Can safely putnext() to esp_pfkey_q, because this is a turnaround 15940Sstevel@tonic-gate * from the esp_pfkey_q. 15950Sstevel@tonic-gate */ 1596*3448Sdh155122 putnext(espstack->esp_pfkey_q, mp); 15970Sstevel@tonic-gate } 15980Sstevel@tonic-gate 15990Sstevel@tonic-gate /* 16000Sstevel@tonic-gate * Insert the ESP header into a packet. Duplicate an mblk, and insert a newly 16010Sstevel@tonic-gate * allocated mblk with the ESP header in between the two. 16020Sstevel@tonic-gate */ 16030Sstevel@tonic-gate static boolean_t 1604*3448Sdh155122 esp_insert_esp(mblk_t *mp, mblk_t *esp_mp, uint_t divpoint, 1605*3448Sdh155122 ipsecesp_stack_t *espstack) 16060Sstevel@tonic-gate { 16070Sstevel@tonic-gate mblk_t *split_mp = mp; 16080Sstevel@tonic-gate uint_t wheretodiv = divpoint; 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate while ((split_mp->b_wptr - split_mp->b_rptr) < wheretodiv) { 16110Sstevel@tonic-gate wheretodiv -= (split_mp->b_wptr - split_mp->b_rptr); 16120Sstevel@tonic-gate split_mp = split_mp->b_cont; 16130Sstevel@tonic-gate ASSERT(split_mp != NULL); 16140Sstevel@tonic-gate } 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate if (split_mp->b_wptr - split_mp->b_rptr != wheretodiv) { 16170Sstevel@tonic-gate mblk_t *scratch; 16180Sstevel@tonic-gate 16190Sstevel@tonic-gate /* "scratch" is the 2nd half, split_mp is the first. */ 16200Sstevel@tonic-gate scratch = dupb(split_mp); 16210Sstevel@tonic-gate if (scratch == NULL) { 1622*3448Sdh155122 esp1dbg(espstack, 1623*3448Sdh155122 ("esp_insert_esp: can't allocate scratch.\n")); 16240Sstevel@tonic-gate return (B_FALSE); 16250Sstevel@tonic-gate } 16260Sstevel@tonic-gate /* NOTE: dupb() doesn't set b_cont appropriately. */ 16270Sstevel@tonic-gate scratch->b_cont = split_mp->b_cont; 16280Sstevel@tonic-gate scratch->b_rptr += wheretodiv; 16290Sstevel@tonic-gate split_mp->b_wptr = split_mp->b_rptr + wheretodiv; 16300Sstevel@tonic-gate split_mp->b_cont = scratch; 16310Sstevel@tonic-gate } 16320Sstevel@tonic-gate /* 16330Sstevel@tonic-gate * At this point, split_mp is exactly "wheretodiv" bytes long, and 16340Sstevel@tonic-gate * holds the end of the pre-ESP part of the datagram. 16350Sstevel@tonic-gate */ 16360Sstevel@tonic-gate esp_mp->b_cont = split_mp->b_cont; 16370Sstevel@tonic-gate split_mp->b_cont = esp_mp; 16380Sstevel@tonic-gate 16390Sstevel@tonic-gate return (B_TRUE); 16400Sstevel@tonic-gate } 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate /* 16430Sstevel@tonic-gate * Finish processing of an inbound ESP packet after processing by the 16440Sstevel@tonic-gate * crypto framework. 16450Sstevel@tonic-gate * - Remove the ESP header. 16460Sstevel@tonic-gate * - Send packet back to IP. 16470Sstevel@tonic-gate * If authentication was performed on the packet, this function is called 16480Sstevel@tonic-gate * only if the authentication succeeded. 16490Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the 16500Sstevel@tonic-gate * mblk chain ipsec_in_mp. 16510Sstevel@tonic-gate */ 16520Sstevel@tonic-gate static ipsec_status_t 16530Sstevel@tonic-gate esp_in_done(mblk_t *ipsec_in_mp) 16540Sstevel@tonic-gate { 16550Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr; 16560Sstevel@tonic-gate mblk_t *data_mp; 16570Sstevel@tonic-gate ipsa_t *assoc; 16580Sstevel@tonic-gate uint_t espstart; 16590Sstevel@tonic-gate uint32_t ivlen = 0; 16600Sstevel@tonic-gate uint_t processed_len; 16610Sstevel@tonic-gate esph_t *esph; 16620Sstevel@tonic-gate kstat_named_t *counter; 16630Sstevel@tonic-gate boolean_t is_natt; 1664*3448Sdh155122 netstack_t *ns = ii->ipsec_in_ns; 1665*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 1666*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 16670Sstevel@tonic-gate 16680Sstevel@tonic-gate assoc = ii->ipsec_in_esp_sa; 16690Sstevel@tonic-gate ASSERT(assoc != NULL); 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0); 16720Sstevel@tonic-gate 16730Sstevel@tonic-gate /* get the pointer to the ESP header */ 16740Sstevel@tonic-gate if (assoc->ipsa_encr_alg == SADB_EALG_NULL) { 16750Sstevel@tonic-gate /* authentication-only ESP */ 16760Sstevel@tonic-gate espstart = ii->ipsec_in_crypto_data.cd_offset; 16770Sstevel@tonic-gate processed_len = ii->ipsec_in_crypto_data.cd_length; 16780Sstevel@tonic-gate } else { 16790Sstevel@tonic-gate /* encryption present */ 16800Sstevel@tonic-gate ivlen = assoc->ipsa_iv_len; 16810Sstevel@tonic-gate if (assoc->ipsa_auth_alg == SADB_AALG_NONE) { 16820Sstevel@tonic-gate /* encryption-only ESP */ 16830Sstevel@tonic-gate espstart = ii->ipsec_in_crypto_data.cd_offset - 16840Sstevel@tonic-gate sizeof (esph_t) - assoc->ipsa_iv_len; 16850Sstevel@tonic-gate processed_len = ii->ipsec_in_crypto_data.cd_length + 16860Sstevel@tonic-gate ivlen; 16870Sstevel@tonic-gate } else { 16880Sstevel@tonic-gate /* encryption with authentication */ 16890Sstevel@tonic-gate espstart = ii->ipsec_in_crypto_dual_data.dd_offset1; 16900Sstevel@tonic-gate processed_len = ii->ipsec_in_crypto_dual_data.dd_len2 + 16910Sstevel@tonic-gate ivlen; 16920Sstevel@tonic-gate } 16930Sstevel@tonic-gate } 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate data_mp = ipsec_in_mp->b_cont; 16960Sstevel@tonic-gate esph = (esph_t *)(data_mp->b_rptr + espstart); 16970Sstevel@tonic-gate 16980Sstevel@tonic-gate if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) { 16990Sstevel@tonic-gate /* authentication passed if we reach this point */ 1700*3448Sdh155122 ESP_BUMP_STAT(espstack, good_auth); 17010Sstevel@tonic-gate data_mp->b_wptr -= assoc->ipsa_mac_len; 17020Sstevel@tonic-gate 17030Sstevel@tonic-gate /* 17040Sstevel@tonic-gate * Check replay window here! 17050Sstevel@tonic-gate * For right now, assume keysock will set the replay window 17060Sstevel@tonic-gate * size to zero for SAs that have an unspecified sender. 17070Sstevel@tonic-gate * This may change... 17080Sstevel@tonic-gate */ 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate if (!sadb_replay_check(assoc, esph->esph_replay)) { 17110Sstevel@tonic-gate /* 17120Sstevel@tonic-gate * Log the event. As of now we print out an event. 17130Sstevel@tonic-gate * Do not print the replay failure number, or else 17140Sstevel@tonic-gate * syslog cannot collate the error messages. Printing 17150Sstevel@tonic-gate * the replay number that failed opens a denial-of- 17160Sstevel@tonic-gate * service attack. 17170Sstevel@tonic-gate */ 17180Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, 17190Sstevel@tonic-gate SL_ERROR | SL_WARN, 17200Sstevel@tonic-gate "Replay failed for ESP spi 0x%x, dst %s.\n", 17210Sstevel@tonic-gate assoc->ipsa_spi, assoc->ipsa_dstaddr, 1722*3448Sdh155122 assoc->ipsa_addrfam, espstack->ipsecesp_netstack); 1723*3448Sdh155122 ESP_BUMP_STAT(espstack, replay_failures); 1724*3448Sdh155122 counter = DROPPER(ipss, ipds_esp_replay); 17250Sstevel@tonic-gate goto drop_and_bail; 17260Sstevel@tonic-gate } 17270Sstevel@tonic-gate } 17280Sstevel@tonic-gate 17290Sstevel@tonic-gate if (!esp_age_bytes(assoc, processed_len, B_TRUE)) { 17300Sstevel@tonic-gate /* The ipsa has hit hard expiration, LOG and AUDIT. */ 17310Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, 17320Sstevel@tonic-gate SL_ERROR | SL_WARN, 17330Sstevel@tonic-gate "ESP association 0x%x, dst %s had bytes expire.\n", 1734*3448Sdh155122 assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam, 1735*3448Sdh155122 espstack->ipsecesp_netstack); 1736*3448Sdh155122 ESP_BUMP_STAT(espstack, bytes_expired); 1737*3448Sdh155122 counter = DROPPER(ipss, ipds_esp_bytes_expire); 17380Sstevel@tonic-gate goto drop_and_bail; 17390Sstevel@tonic-gate } 17400Sstevel@tonic-gate 17410Sstevel@tonic-gate /* 17420Sstevel@tonic-gate * Remove ESP header and padding from packet. I hope the compiler 17430Sstevel@tonic-gate * spews "branch, predict taken" code for this. 17440Sstevel@tonic-gate */ 17450Sstevel@tonic-gate 1746*3448Sdh155122 if (esp_strip_header(data_mp, ii->ipsec_in_v4, ivlen, &counter, 1747*3448Sdh155122 espstack)) { 17480Sstevel@tonic-gate if (is_natt) 17490Sstevel@tonic-gate return (esp_fix_natt_checksums(data_mp, assoc)); 17500Sstevel@tonic-gate return (IPSEC_STATUS_SUCCESS); 17510Sstevel@tonic-gate } 17520Sstevel@tonic-gate 1753*3448Sdh155122 esp1dbg(espstack, ("esp_in_done: esp_strip_header() failed\n")); 17540Sstevel@tonic-gate drop_and_bail: 1755*3448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards); 17560Sstevel@tonic-gate /* 17570Sstevel@tonic-gate * TODO: Extract inbound interface from the IPSEC_IN message's 17580Sstevel@tonic-gate * ii->ipsec_in_rill_index. 17590Sstevel@tonic-gate */ 1760*3448Sdh155122 ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL, counter, 1761*3448Sdh155122 &espstack->esp_dropper); 17620Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 17630Sstevel@tonic-gate } 17640Sstevel@tonic-gate 17650Sstevel@tonic-gate /* 17660Sstevel@tonic-gate * Called upon failing the inbound ICV check. The message passed as 17670Sstevel@tonic-gate * argument is freed. 17680Sstevel@tonic-gate */ 17690Sstevel@tonic-gate static void 17700Sstevel@tonic-gate esp_log_bad_auth(mblk_t *ipsec_in) 17710Sstevel@tonic-gate { 17720Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr; 17730Sstevel@tonic-gate ipsa_t *assoc = ii->ipsec_in_esp_sa; 1774*3448Sdh155122 netstack_t *ns = ii->ipsec_in_ns; 1775*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 1776*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate /* 17790Sstevel@tonic-gate * Log the event. Don't print to the console, block 17800Sstevel@tonic-gate * potential denial-of-service attack. 17810Sstevel@tonic-gate */ 1782*3448Sdh155122 ESP_BUMP_STAT(espstack, bad_auth); 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 17850Sstevel@tonic-gate "ESP Authentication failed for spi 0x%x, dst %s.\n", 1786*3448Sdh155122 assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam, 1787*3448Sdh155122 espstack->ipsecesp_netstack); 1788*3448Sdh155122 1789*3448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards); 17900Sstevel@tonic-gate /* 17910Sstevel@tonic-gate * TODO: Extract inbound interface from the IPSEC_IN 17920Sstevel@tonic-gate * message's ii->ipsec_in_rill_index. 17930Sstevel@tonic-gate */ 1794*3448Sdh155122 ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, 1795*3448Sdh155122 DROPPER(ipss, ipds_esp_bad_auth), 1796*3448Sdh155122 &espstack->esp_dropper); 17970Sstevel@tonic-gate } 17980Sstevel@tonic-gate 17990Sstevel@tonic-gate 18000Sstevel@tonic-gate /* 18010Sstevel@tonic-gate * Invoked for outbound packets after ESP processing. If the packet 18020Sstevel@tonic-gate * also requires AH, performs the AH SA selection and AH processing. 18030Sstevel@tonic-gate * Returns B_TRUE if the AH processing was not needed or if it was 18040Sstevel@tonic-gate * performed successfully. Returns B_FALSE and consumes the passed mblk 18050Sstevel@tonic-gate * if AH processing was required but could not be performed. 18060Sstevel@tonic-gate */ 18070Sstevel@tonic-gate static boolean_t 18080Sstevel@tonic-gate esp_do_outbound_ah(mblk_t *ipsec_mp) 18090Sstevel@tonic-gate { 18100Sstevel@tonic-gate ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 18110Sstevel@tonic-gate ipsec_status_t ipsec_rc; 18120Sstevel@tonic-gate ipsec_action_t *ap; 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate ap = io->ipsec_out_act; 18150Sstevel@tonic-gate if (ap == NULL) { 18160Sstevel@tonic-gate ipsec_policy_t *pp = io->ipsec_out_policy; 18170Sstevel@tonic-gate ap = pp->ipsp_act; 18180Sstevel@tonic-gate } 18190Sstevel@tonic-gate 18200Sstevel@tonic-gate if (!ap->ipa_want_ah) 18210Sstevel@tonic-gate return (B_TRUE); 18220Sstevel@tonic-gate 18230Sstevel@tonic-gate ASSERT(io->ipsec_out_ah_done == B_FALSE); 18240Sstevel@tonic-gate 18250Sstevel@tonic-gate if (io->ipsec_out_ah_sa == NULL) { 18260Sstevel@tonic-gate if (!ipsec_outbound_sa(ipsec_mp, IPPROTO_AH)) { 18270Sstevel@tonic-gate sadb_acquire(ipsec_mp, io, B_TRUE, B_FALSE); 18280Sstevel@tonic-gate return (B_FALSE); 18290Sstevel@tonic-gate } 18300Sstevel@tonic-gate } 18310Sstevel@tonic-gate ASSERT(io->ipsec_out_ah_sa != NULL); 18320Sstevel@tonic-gate 18330Sstevel@tonic-gate io->ipsec_out_ah_done = B_TRUE; 18340Sstevel@tonic-gate ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp); 18350Sstevel@tonic-gate return (ipsec_rc == IPSEC_STATUS_SUCCESS); 18360Sstevel@tonic-gate } 18370Sstevel@tonic-gate 18380Sstevel@tonic-gate 18390Sstevel@tonic-gate /* 18400Sstevel@tonic-gate * Kernel crypto framework callback invoked after completion of async 18410Sstevel@tonic-gate * crypto requests. 18420Sstevel@tonic-gate */ 18430Sstevel@tonic-gate static void 18440Sstevel@tonic-gate esp_kcf_callback(void *arg, int status) 18450Sstevel@tonic-gate { 18460Sstevel@tonic-gate mblk_t *ipsec_mp = (mblk_t *)arg; 18470Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 18480Sstevel@tonic-gate boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN); 1849*3448Sdh155122 netstackid_t stackid; 1850*3448Sdh155122 netstack_t *ns, *ns_arg; 1851*3448Sdh155122 ipsecesp_stack_t *espstack; 1852*3448Sdh155122 ipsec_stack_t *ipss; 1853*3448Sdh155122 ipsec_out_t *io = (ipsec_out_t *)ii; 18540Sstevel@tonic-gate 18550Sstevel@tonic-gate ASSERT(ipsec_mp->b_cont != NULL); 18560Sstevel@tonic-gate 1857*3448Sdh155122 if (is_inbound) { 1858*3448Sdh155122 stackid = ii->ipsec_in_stackid; 1859*3448Sdh155122 ns_arg = ii->ipsec_in_ns; 1860*3448Sdh155122 } else { 1861*3448Sdh155122 stackid = io->ipsec_out_stackid; 1862*3448Sdh155122 ns_arg = io->ipsec_out_ns; 1863*3448Sdh155122 } 1864*3448Sdh155122 1865*3448Sdh155122 /* 1866*3448Sdh155122 * Verify that the netstack is still around; could have vanished 1867*3448Sdh155122 * while kEf was doing its work. 1868*3448Sdh155122 */ 1869*3448Sdh155122 ns = netstack_find_by_stackid(stackid); 1870*3448Sdh155122 if (ns == NULL || ns != ns_arg) { 1871*3448Sdh155122 /* Disappeared on us */ 1872*3448Sdh155122 if (ns != NULL) 1873*3448Sdh155122 netstack_rele(ns); 1874*3448Sdh155122 freemsg(ipsec_mp); 1875*3448Sdh155122 return; 1876*3448Sdh155122 } 1877*3448Sdh155122 1878*3448Sdh155122 espstack = ns->netstack_ipsecesp; 1879*3448Sdh155122 ipss = ns->netstack_ipsec; 1880*3448Sdh155122 18810Sstevel@tonic-gate if (status == CRYPTO_SUCCESS) { 18820Sstevel@tonic-gate if (is_inbound) { 1883*3448Sdh155122 if (esp_in_done(ipsec_mp) != IPSEC_STATUS_SUCCESS) { 1884*3448Sdh155122 netstack_rele(ns); 18850Sstevel@tonic-gate return; 1886*3448Sdh155122 } 18870Sstevel@tonic-gate /* finish IPsec processing */ 18880Sstevel@tonic-gate ip_fanout_proto_again(ipsec_mp, NULL, NULL, NULL); 18890Sstevel@tonic-gate } else { 18900Sstevel@tonic-gate /* 18910Sstevel@tonic-gate * If a ICV was computed, it was stored by the 18920Sstevel@tonic-gate * crypto framework at the end of the packet. 18930Sstevel@tonic-gate */ 18940Sstevel@tonic-gate ipha_t *ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 18950Sstevel@tonic-gate 18960Sstevel@tonic-gate /* do AH processing if needed */ 1897*3448Sdh155122 if (!esp_do_outbound_ah(ipsec_mp)) { 1898*3448Sdh155122 netstack_rele(ns); 18990Sstevel@tonic-gate return; 1900*3448Sdh155122 } 19010Sstevel@tonic-gate /* finish IPsec processing */ 19020Sstevel@tonic-gate if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 19030Sstevel@tonic-gate ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL, 19040Sstevel@tonic-gate NULL); 19050Sstevel@tonic-gate } else { 19060Sstevel@tonic-gate ip6_t *ip6h = (ip6_t *)ipha; 19070Sstevel@tonic-gate ip_wput_ipsec_out_v6(NULL, ipsec_mp, ip6h, 19080Sstevel@tonic-gate NULL, NULL); 19090Sstevel@tonic-gate } 19100Sstevel@tonic-gate } 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate } else if (status == CRYPTO_INVALID_MAC) { 19130Sstevel@tonic-gate esp_log_bad_auth(ipsec_mp); 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate } else { 1916*3448Sdh155122 esp1dbg(espstack, 1917*3448Sdh155122 ("esp_kcf_callback: crypto failed with 0x%x\n", 19180Sstevel@tonic-gate status)); 1919*3448Sdh155122 ESP_BUMP_STAT(espstack, crypto_failures); 19200Sstevel@tonic-gate if (is_inbound) 1921*3448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards); 19220Sstevel@tonic-gate else 1923*3448Sdh155122 ESP_BUMP_STAT(espstack, out_discards); 19240Sstevel@tonic-gate ip_drop_packet(ipsec_mp, is_inbound, NULL, NULL, 1925*3448Sdh155122 DROPPER(ipss, ipds_esp_crypto_failed), 1926*3448Sdh155122 &espstack->esp_dropper); 19270Sstevel@tonic-gate } 1928*3448Sdh155122 netstack_rele(ns); 19290Sstevel@tonic-gate } 19300Sstevel@tonic-gate 19310Sstevel@tonic-gate /* 19320Sstevel@tonic-gate * Invoked on crypto framework failure during inbound and outbound processing. 19330Sstevel@tonic-gate */ 19340Sstevel@tonic-gate static void 1935*3448Sdh155122 esp_crypto_failed(mblk_t *mp, boolean_t is_inbound, int kef_rc, 1936*3448Sdh155122 ipsecesp_stack_t *espstack) 19370Sstevel@tonic-gate { 1938*3448Sdh155122 ipsec_stack_t *ipss = espstack->ipsecesp_netstack->netstack_ipsec; 1939*3448Sdh155122 1940*3448Sdh155122 esp1dbg(espstack, ("crypto failed for %s ESP with 0x%x\n", 19410Sstevel@tonic-gate is_inbound ? "inbound" : "outbound", kef_rc)); 1942*3448Sdh155122 ip_drop_packet(mp, is_inbound, NULL, NULL, 1943*3448Sdh155122 DROPPER(ipss, ipds_esp_crypto_failed), 1944*3448Sdh155122 &espstack->esp_dropper); 1945*3448Sdh155122 ESP_BUMP_STAT(espstack, crypto_failures); 19460Sstevel@tonic-gate if (is_inbound) 1947*3448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards); 19480Sstevel@tonic-gate else 1949*3448Sdh155122 ESP_BUMP_STAT(espstack, out_discards); 19500Sstevel@tonic-gate } 19510Sstevel@tonic-gate 19520Sstevel@tonic-gate #define ESP_INIT_CALLREQ(_cr) { \ 19530Sstevel@tonic-gate (_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_RESTRICTED; \ 19540Sstevel@tonic-gate (_cr)->cr_callback_arg = ipsec_mp; \ 19550Sstevel@tonic-gate (_cr)->cr_callback_func = esp_kcf_callback; \ 19560Sstevel@tonic-gate } 19570Sstevel@tonic-gate 19580Sstevel@tonic-gate #define ESP_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) { \ 19590Sstevel@tonic-gate (mac)->cd_format = CRYPTO_DATA_RAW; \ 19600Sstevel@tonic-gate (mac)->cd_offset = 0; \ 19610Sstevel@tonic-gate (mac)->cd_length = icvlen; \ 19620Sstevel@tonic-gate (mac)->cd_raw.iov_base = (char *)icvbuf; \ 19630Sstevel@tonic-gate (mac)->cd_raw.iov_len = icvlen; \ 19640Sstevel@tonic-gate } 19650Sstevel@tonic-gate 19660Sstevel@tonic-gate #define ESP_INIT_CRYPTO_DATA(data, mp, off, len) { \ 19670Sstevel@tonic-gate if (MBLKL(mp) >= (len) + (off)) { \ 19680Sstevel@tonic-gate (data)->cd_format = CRYPTO_DATA_RAW; \ 19690Sstevel@tonic-gate (data)->cd_raw.iov_base = (char *)(mp)->b_rptr; \ 19700Sstevel@tonic-gate (data)->cd_raw.iov_len = MBLKL(mp); \ 19710Sstevel@tonic-gate (data)->cd_offset = off; \ 19720Sstevel@tonic-gate } else { \ 19730Sstevel@tonic-gate (data)->cd_format = CRYPTO_DATA_MBLK; \ 19740Sstevel@tonic-gate (data)->cd_mp = mp; \ 19750Sstevel@tonic-gate (data)->cd_offset = off; \ 19760Sstevel@tonic-gate } \ 19770Sstevel@tonic-gate (data)->cd_length = len; \ 19780Sstevel@tonic-gate } 19790Sstevel@tonic-gate 19800Sstevel@tonic-gate #define ESP_INIT_CRYPTO_DUAL_DATA(data, mp, off1, len1, off2, len2) { \ 19810Sstevel@tonic-gate (data)->dd_format = CRYPTO_DATA_MBLK; \ 19820Sstevel@tonic-gate (data)->dd_mp = mp; \ 19830Sstevel@tonic-gate (data)->dd_len1 = len1; \ 19840Sstevel@tonic-gate (data)->dd_offset1 = off1; \ 19850Sstevel@tonic-gate (data)->dd_len2 = len2; \ 19860Sstevel@tonic-gate (data)->dd_offset2 = off2; \ 19870Sstevel@tonic-gate } 19880Sstevel@tonic-gate 19890Sstevel@tonic-gate static ipsec_status_t 19900Sstevel@tonic-gate esp_submit_req_inbound(mblk_t *ipsec_mp, ipsa_t *assoc, uint_t esph_offset) 19910Sstevel@tonic-gate { 19920Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 19930Sstevel@tonic-gate boolean_t do_auth; 19940Sstevel@tonic-gate uint_t auth_offset, msg_len, auth_len; 19950Sstevel@tonic-gate crypto_call_req_t call_req; 19960Sstevel@tonic-gate mblk_t *esp_mp; 19970Sstevel@tonic-gate int kef_rc = CRYPTO_FAILED; 19980Sstevel@tonic-gate uint_t icv_len = assoc->ipsa_mac_len; 19990Sstevel@tonic-gate crypto_ctx_template_t auth_ctx_tmpl; 20000Sstevel@tonic-gate boolean_t do_encr; 20010Sstevel@tonic-gate uint_t encr_offset, encr_len; 20020Sstevel@tonic-gate uint_t iv_len = assoc->ipsa_iv_len; 20030Sstevel@tonic-gate crypto_ctx_template_t encr_ctx_tmpl; 2004*3448Sdh155122 netstack_t *ns = ii->ipsec_in_ns; 2005*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 2006*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 20070Sstevel@tonic-gate 20080Sstevel@tonic-gate ASSERT(ii->ipsec_in_type == IPSEC_IN); 20090Sstevel@tonic-gate 2010*3448Sdh155122 /* 2011*3448Sdh155122 * In case kEF queues and calls back, keep netstackid_t for 2012*3448Sdh155122 * verification that the IP instance is still around in 2013*3448Sdh155122 * esp_kcf_callback(). 2014*3448Sdh155122 */ 2015*3448Sdh155122 ii->ipsec_in_stackid = ns->netstack_stackid; 2016*3448Sdh155122 20170Sstevel@tonic-gate do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE; 20180Sstevel@tonic-gate do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL; 20190Sstevel@tonic-gate 20200Sstevel@tonic-gate /* 20210Sstevel@tonic-gate * An inbound packet is of the form: 20220Sstevel@tonic-gate * IPSEC_IN -> [IP,options,ESP,IV,data,ICV,pad] 20230Sstevel@tonic-gate */ 20240Sstevel@tonic-gate esp_mp = ipsec_mp->b_cont; 20250Sstevel@tonic-gate msg_len = MBLKL(esp_mp); 20260Sstevel@tonic-gate 20270Sstevel@tonic-gate ESP_INIT_CALLREQ(&call_req); 20280Sstevel@tonic-gate 20290Sstevel@tonic-gate if (do_auth) { 20300Sstevel@tonic-gate /* force asynchronous processing? */ 2031*3448Sdh155122 if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_AUTH] == 20320Sstevel@tonic-gate IPSEC_ALGS_EXEC_ASYNC) 20330Sstevel@tonic-gate call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE; 20340Sstevel@tonic-gate 20350Sstevel@tonic-gate /* authentication context template */ 20360Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH, 20370Sstevel@tonic-gate auth_ctx_tmpl); 20380Sstevel@tonic-gate 20390Sstevel@tonic-gate /* ICV to be verified */ 20400Sstevel@tonic-gate ESP_INIT_CRYPTO_MAC(&ii->ipsec_in_crypto_mac, 20410Sstevel@tonic-gate icv_len, esp_mp->b_wptr - icv_len); 20420Sstevel@tonic-gate 20430Sstevel@tonic-gate /* authentication starts at the ESP header */ 20440Sstevel@tonic-gate auth_offset = esph_offset; 20450Sstevel@tonic-gate auth_len = msg_len - auth_offset - icv_len; 20460Sstevel@tonic-gate if (!do_encr) { 20470Sstevel@tonic-gate /* authentication only */ 20480Sstevel@tonic-gate /* initialize input data argument */ 20490Sstevel@tonic-gate ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data, 20500Sstevel@tonic-gate esp_mp, auth_offset, auth_len); 20510Sstevel@tonic-gate 20520Sstevel@tonic-gate /* call the crypto framework */ 20530Sstevel@tonic-gate kef_rc = crypto_mac_verify(&assoc->ipsa_amech, 20540Sstevel@tonic-gate &ii->ipsec_in_crypto_data, 20550Sstevel@tonic-gate &assoc->ipsa_kcfauthkey, auth_ctx_tmpl, 20560Sstevel@tonic-gate &ii->ipsec_in_crypto_mac, &call_req); 20570Sstevel@tonic-gate } 20580Sstevel@tonic-gate } 20590Sstevel@tonic-gate 20600Sstevel@tonic-gate if (do_encr) { 20610Sstevel@tonic-gate /* force asynchronous processing? */ 2062*3448Sdh155122 if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_ENCR] == 20630Sstevel@tonic-gate IPSEC_ALGS_EXEC_ASYNC) 20640Sstevel@tonic-gate call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE; 20650Sstevel@tonic-gate 20660Sstevel@tonic-gate /* encryption template */ 20670Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR, 20680Sstevel@tonic-gate encr_ctx_tmpl); 20690Sstevel@tonic-gate 20700Sstevel@tonic-gate /* skip IV, since it is passed separately */ 20710Sstevel@tonic-gate encr_offset = esph_offset + sizeof (esph_t) + iv_len; 20720Sstevel@tonic-gate encr_len = msg_len - encr_offset; 20730Sstevel@tonic-gate 20740Sstevel@tonic-gate if (!do_auth) { 20750Sstevel@tonic-gate /* decryption only */ 20760Sstevel@tonic-gate /* initialize input data argument */ 20770Sstevel@tonic-gate ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data, 20780Sstevel@tonic-gate esp_mp, encr_offset, encr_len); 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate /* specify IV */ 20810Sstevel@tonic-gate ii->ipsec_in_crypto_data.cd_miscdata = 20820Sstevel@tonic-gate (char *)esp_mp->b_rptr + sizeof (esph_t) + 20830Sstevel@tonic-gate esph_offset; 20840Sstevel@tonic-gate 20850Sstevel@tonic-gate /* call the crypto framework */ 20860Sstevel@tonic-gate kef_rc = crypto_decrypt(&assoc->ipsa_emech, 20870Sstevel@tonic-gate &ii->ipsec_in_crypto_data, 20880Sstevel@tonic-gate &assoc->ipsa_kcfencrkey, encr_ctx_tmpl, 20890Sstevel@tonic-gate NULL, &call_req); 20900Sstevel@tonic-gate } 20910Sstevel@tonic-gate } 20920Sstevel@tonic-gate 20930Sstevel@tonic-gate if (do_auth && do_encr) { 20940Sstevel@tonic-gate /* dual operation */ 20950Sstevel@tonic-gate /* initialize input data argument */ 20960Sstevel@tonic-gate ESP_INIT_CRYPTO_DUAL_DATA(&ii->ipsec_in_crypto_dual_data, 20970Sstevel@tonic-gate esp_mp, auth_offset, auth_len, 20980Sstevel@tonic-gate encr_offset, encr_len - icv_len); 20990Sstevel@tonic-gate 21000Sstevel@tonic-gate /* specify IV */ 21010Sstevel@tonic-gate ii->ipsec_in_crypto_dual_data.dd_miscdata = 21020Sstevel@tonic-gate (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset; 21030Sstevel@tonic-gate 21040Sstevel@tonic-gate /* call the framework */ 21050Sstevel@tonic-gate kef_rc = crypto_mac_verify_decrypt(&assoc->ipsa_amech, 21060Sstevel@tonic-gate &assoc->ipsa_emech, &ii->ipsec_in_crypto_dual_data, 21070Sstevel@tonic-gate &assoc->ipsa_kcfauthkey, &assoc->ipsa_kcfencrkey, 21080Sstevel@tonic-gate auth_ctx_tmpl, encr_ctx_tmpl, &ii->ipsec_in_crypto_mac, 21090Sstevel@tonic-gate NULL, &call_req); 21100Sstevel@tonic-gate } 21110Sstevel@tonic-gate 21120Sstevel@tonic-gate switch (kef_rc) { 21130Sstevel@tonic-gate case CRYPTO_SUCCESS: 2114*3448Sdh155122 ESP_BUMP_STAT(espstack, crypto_sync); 21150Sstevel@tonic-gate return (esp_in_done(ipsec_mp)); 21160Sstevel@tonic-gate case CRYPTO_QUEUED: 21170Sstevel@tonic-gate /* esp_kcf_callback() will be invoked on completion */ 2118*3448Sdh155122 ESP_BUMP_STAT(espstack, crypto_async); 21190Sstevel@tonic-gate return (IPSEC_STATUS_PENDING); 21200Sstevel@tonic-gate case CRYPTO_INVALID_MAC: 2121*3448Sdh155122 ESP_BUMP_STAT(espstack, crypto_sync); 21220Sstevel@tonic-gate esp_log_bad_auth(ipsec_mp); 21230Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 21240Sstevel@tonic-gate } 21250Sstevel@tonic-gate 2126*3448Sdh155122 esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc, espstack); 21270Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 21280Sstevel@tonic-gate } 21290Sstevel@tonic-gate 21300Sstevel@tonic-gate static ipsec_status_t 21310Sstevel@tonic-gate esp_submit_req_outbound(mblk_t *ipsec_mp, ipsa_t *assoc, uchar_t *icv_buf, 21320Sstevel@tonic-gate uint_t payload_len) 21330Sstevel@tonic-gate { 21340Sstevel@tonic-gate ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 21350Sstevel@tonic-gate uint_t auth_len; 21360Sstevel@tonic-gate crypto_call_req_t call_req; 21370Sstevel@tonic-gate mblk_t *esp_mp; 21380Sstevel@tonic-gate int kef_rc = CRYPTO_FAILED; 21390Sstevel@tonic-gate uint_t icv_len = assoc->ipsa_mac_len; 21400Sstevel@tonic-gate crypto_ctx_template_t auth_ctx_tmpl; 21410Sstevel@tonic-gate boolean_t do_auth; 21420Sstevel@tonic-gate boolean_t do_encr; 21430Sstevel@tonic-gate uint_t iv_len = assoc->ipsa_iv_len; 21440Sstevel@tonic-gate crypto_ctx_template_t encr_ctx_tmpl; 21450Sstevel@tonic-gate boolean_t is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0); 21460Sstevel@tonic-gate size_t esph_offset = (is_natt ? UDPH_SIZE : 0); 2147*3448Sdh155122 netstack_t *ns = io->ipsec_out_ns; 2148*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 2149*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 2150*3448Sdh155122 2151*3448Sdh155122 esp3dbg(espstack, ("esp_submit_req_outbound:%s", 2152*3448Sdh155122 is_natt ? "natt" : "not natt")); 21530Sstevel@tonic-gate 21540Sstevel@tonic-gate ASSERT(io->ipsec_out_type == IPSEC_OUT); 21550Sstevel@tonic-gate 2156*3448Sdh155122 /* 2157*3448Sdh155122 * In case kEF queues and calls back, keep netstackid_t for 2158*3448Sdh155122 * verification that the IP instance is still around in 2159*3448Sdh155122 * esp_kcf_callback(). 2160*3448Sdh155122 */ 2161*3448Sdh155122 io->ipsec_out_stackid = ns->netstack_stackid; 2162*3448Sdh155122 21630Sstevel@tonic-gate do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL; 21640Sstevel@tonic-gate do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE; 21650Sstevel@tonic-gate 21660Sstevel@tonic-gate /* 21670Sstevel@tonic-gate * Outbound IPsec packets are of the form: 21680Sstevel@tonic-gate * IPSEC_OUT -> [IP,options] -> [ESP,IV] -> [data] -> [pad,ICV] 21690Sstevel@tonic-gate * unless it's NATT, then it's 21700Sstevel@tonic-gate * IPSEC_OUT -> [IP,options] -> [udp][ESP,IV] -> [data] -> [pad,ICV] 21710Sstevel@tonic-gate * Get a pointer to the mblk containing the ESP header. 21720Sstevel@tonic-gate */ 21730Sstevel@tonic-gate ASSERT(ipsec_mp->b_cont != NULL && ipsec_mp->b_cont->b_cont != NULL); 21740Sstevel@tonic-gate esp_mp = ipsec_mp->b_cont->b_cont; 21750Sstevel@tonic-gate 21760Sstevel@tonic-gate ESP_INIT_CALLREQ(&call_req); 21770Sstevel@tonic-gate 21780Sstevel@tonic-gate if (do_auth) { 21790Sstevel@tonic-gate /* force asynchronous processing? */ 2180*3448Sdh155122 if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_AUTH] == 21810Sstevel@tonic-gate IPSEC_ALGS_EXEC_ASYNC) 21820Sstevel@tonic-gate call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE; 21830Sstevel@tonic-gate 21840Sstevel@tonic-gate /* authentication context template */ 21850Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH, 21860Sstevel@tonic-gate auth_ctx_tmpl); 21870Sstevel@tonic-gate 21880Sstevel@tonic-gate /* where to store the computed mac */ 21890Sstevel@tonic-gate ESP_INIT_CRYPTO_MAC(&io->ipsec_out_crypto_mac, 21900Sstevel@tonic-gate icv_len, icv_buf); 21910Sstevel@tonic-gate 21920Sstevel@tonic-gate /* authentication starts at the ESP header */ 2193134Sdanmcd auth_len = payload_len + iv_len + sizeof (esph_t); 21940Sstevel@tonic-gate if (!do_encr) { 21950Sstevel@tonic-gate /* authentication only */ 21960Sstevel@tonic-gate /* initialize input data argument */ 21970Sstevel@tonic-gate ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data, 21980Sstevel@tonic-gate esp_mp, esph_offset, auth_len); 21990Sstevel@tonic-gate 22000Sstevel@tonic-gate /* call the crypto framework */ 22010Sstevel@tonic-gate kef_rc = crypto_mac(&assoc->ipsa_amech, 22020Sstevel@tonic-gate &io->ipsec_out_crypto_data, 22030Sstevel@tonic-gate &assoc->ipsa_kcfauthkey, auth_ctx_tmpl, 22040Sstevel@tonic-gate &io->ipsec_out_crypto_mac, &call_req); 22050Sstevel@tonic-gate } 22060Sstevel@tonic-gate } 22070Sstevel@tonic-gate 22080Sstevel@tonic-gate if (do_encr) { 22090Sstevel@tonic-gate /* force asynchronous processing? */ 2210*3448Sdh155122 if (ipss->ipsec_algs_exec_mode[IPSEC_ALG_ENCR] == 22110Sstevel@tonic-gate IPSEC_ALGS_EXEC_ASYNC) 22120Sstevel@tonic-gate call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE; 22130Sstevel@tonic-gate 22140Sstevel@tonic-gate /* encryption context template */ 22150Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR, 22160Sstevel@tonic-gate encr_ctx_tmpl); 22170Sstevel@tonic-gate 22180Sstevel@tonic-gate if (!do_auth) { 22190Sstevel@tonic-gate /* encryption only, skip mblk that contains ESP hdr */ 22200Sstevel@tonic-gate /* initialize input data argument */ 22210Sstevel@tonic-gate ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data, 22220Sstevel@tonic-gate esp_mp->b_cont, 0, payload_len); 22230Sstevel@tonic-gate 22240Sstevel@tonic-gate /* specify IV */ 22250Sstevel@tonic-gate io->ipsec_out_crypto_data.cd_miscdata = 22260Sstevel@tonic-gate (char *)esp_mp->b_rptr + sizeof (esph_t) + 22270Sstevel@tonic-gate esph_offset; 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate /* call the crypto framework */ 22300Sstevel@tonic-gate kef_rc = crypto_encrypt(&assoc->ipsa_emech, 22310Sstevel@tonic-gate &io->ipsec_out_crypto_data, 22320Sstevel@tonic-gate &assoc->ipsa_kcfencrkey, encr_ctx_tmpl, 22330Sstevel@tonic-gate NULL, &call_req); 22340Sstevel@tonic-gate } 22350Sstevel@tonic-gate } 22360Sstevel@tonic-gate 22370Sstevel@tonic-gate if (do_auth && do_encr) { 22380Sstevel@tonic-gate /* 22390Sstevel@tonic-gate * Encryption and authentication: 22400Sstevel@tonic-gate * Pass the pointer to the mblk chain starting at the ESP 22410Sstevel@tonic-gate * header to the framework. Skip the ESP header mblk 22420Sstevel@tonic-gate * for encryption, which is reflected by an encryption 22430Sstevel@tonic-gate * offset equal to the length of that mblk. Start 22440Sstevel@tonic-gate * the authentication at the ESP header, i.e. use an 22450Sstevel@tonic-gate * authentication offset of zero. 22460Sstevel@tonic-gate */ 22470Sstevel@tonic-gate ESP_INIT_CRYPTO_DUAL_DATA(&io->ipsec_out_crypto_dual_data, 22480Sstevel@tonic-gate esp_mp, MBLKL(esp_mp), payload_len, esph_offset, auth_len); 22490Sstevel@tonic-gate 22500Sstevel@tonic-gate /* specify IV */ 22510Sstevel@tonic-gate io->ipsec_out_crypto_dual_data.dd_miscdata = 22520Sstevel@tonic-gate (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset; 22530Sstevel@tonic-gate 22540Sstevel@tonic-gate /* call the framework */ 22550Sstevel@tonic-gate kef_rc = crypto_encrypt_mac(&assoc->ipsa_emech, 22560Sstevel@tonic-gate &assoc->ipsa_amech, NULL, 22570Sstevel@tonic-gate &assoc->ipsa_kcfencrkey, &assoc->ipsa_kcfauthkey, 22580Sstevel@tonic-gate encr_ctx_tmpl, auth_ctx_tmpl, 22590Sstevel@tonic-gate &io->ipsec_out_crypto_dual_data, 22600Sstevel@tonic-gate &io->ipsec_out_crypto_mac, &call_req); 22610Sstevel@tonic-gate } 22620Sstevel@tonic-gate 22630Sstevel@tonic-gate switch (kef_rc) { 22640Sstevel@tonic-gate case CRYPTO_SUCCESS: 2265*3448Sdh155122 ESP_BUMP_STAT(espstack, crypto_sync); 22660Sstevel@tonic-gate return (IPSEC_STATUS_SUCCESS); 22670Sstevel@tonic-gate case CRYPTO_QUEUED: 22680Sstevel@tonic-gate /* esp_kcf_callback() will be invoked on completion */ 2269*3448Sdh155122 ESP_BUMP_STAT(espstack, crypto_async); 22700Sstevel@tonic-gate return (IPSEC_STATUS_PENDING); 22710Sstevel@tonic-gate } 22720Sstevel@tonic-gate 2273*3448Sdh155122 esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc, espstack); 22740Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 22750Sstevel@tonic-gate } 22760Sstevel@tonic-gate 22770Sstevel@tonic-gate /* 22780Sstevel@tonic-gate * Handle outbound IPsec processing for IPv4 and IPv6 22790Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the 22800Sstevel@tonic-gate * mblk chain ipsec_in_mp. 22810Sstevel@tonic-gate */ 22820Sstevel@tonic-gate static ipsec_status_t 22830Sstevel@tonic-gate esp_outbound(mblk_t *mp) 22840Sstevel@tonic-gate { 22850Sstevel@tonic-gate mblk_t *ipsec_out_mp, *data_mp, *espmp, *tailmp; 22860Sstevel@tonic-gate ipsec_out_t *io; 22870Sstevel@tonic-gate ipha_t *ipha; 22880Sstevel@tonic-gate ip6_t *ip6h; 22890Sstevel@tonic-gate esph_t *esph; 22900Sstevel@tonic-gate uint_t af; 22910Sstevel@tonic-gate uint8_t *nhp; 22920Sstevel@tonic-gate uintptr_t divpoint, datalen, adj, padlen, i, alloclen; 22930Sstevel@tonic-gate uintptr_t esplen = sizeof (esph_t); 22940Sstevel@tonic-gate uint8_t protocol; 22950Sstevel@tonic-gate ipsa_t *assoc; 22963192Sdanmcd uint_t iv_len, mac_len = 0; 22970Sstevel@tonic-gate uchar_t *icv_buf; 22980Sstevel@tonic-gate udpha_t *udpha; 22990Sstevel@tonic-gate boolean_t is_natt = B_FALSE; 2300*3448Sdh155122 netstack_t *ns; 2301*3448Sdh155122 ipsecesp_stack_t *espstack; 2302*3448Sdh155122 ipsec_stack_t *ipss; 23030Sstevel@tonic-gate 23040Sstevel@tonic-gate ipsec_out_mp = mp; 23050Sstevel@tonic-gate data_mp = ipsec_out_mp->b_cont; 23060Sstevel@tonic-gate 2307*3448Sdh155122 io = (ipsec_out_t *)ipsec_out_mp->b_rptr; 2308*3448Sdh155122 ns = io->ipsec_out_ns; 2309*3448Sdh155122 espstack = ns->netstack_ipsecesp; 2310*3448Sdh155122 ipss = ns->netstack_ipsec; 2311*3448Sdh155122 2312*3448Sdh155122 ESP_BUMP_STAT(espstack, out_requests); 2313*3448Sdh155122 23140Sstevel@tonic-gate /* 23150Sstevel@tonic-gate * <sigh> We have to copy the message here, because TCP (for example) 23160Sstevel@tonic-gate * keeps a dupb() of the message lying around for retransmission. 23170Sstevel@tonic-gate * Since ESP changes the whole of the datagram, we have to create our 23180Sstevel@tonic-gate * own copy lest we clobber TCP's data. Since we have to copy anyway, 23190Sstevel@tonic-gate * we might as well make use of msgpullup() and get the mblk into one 23200Sstevel@tonic-gate * contiguous piece! 23210Sstevel@tonic-gate */ 23220Sstevel@tonic-gate ipsec_out_mp->b_cont = msgpullup(data_mp, -1); 23230Sstevel@tonic-gate if (ipsec_out_mp->b_cont == NULL) { 23240Sstevel@tonic-gate esp0dbg(("esp_outbound: msgpullup() failed, " 23250Sstevel@tonic-gate "dropping packet.\n")); 23260Sstevel@tonic-gate ipsec_out_mp->b_cont = data_mp; 23270Sstevel@tonic-gate /* 23280Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 23290Sstevel@tonic-gate * pass it to ip_drop_packet(). 23300Sstevel@tonic-gate */ 23310Sstevel@tonic-gate ip_drop_packet(ipsec_out_mp, B_FALSE, NULL, NULL, 2332*3448Sdh155122 DROPPER(ipss, ipds_esp_nomem), 2333*3448Sdh155122 &espstack->esp_dropper); 23340Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 23350Sstevel@tonic-gate } else { 23360Sstevel@tonic-gate freemsg(data_mp); 23370Sstevel@tonic-gate data_mp = ipsec_out_mp->b_cont; 23380Sstevel@tonic-gate } 23390Sstevel@tonic-gate 23400Sstevel@tonic-gate /* 23410Sstevel@tonic-gate * Reality check.... 23420Sstevel@tonic-gate */ 23430Sstevel@tonic-gate 23440Sstevel@tonic-gate ipha = (ipha_t *)data_mp->b_rptr; /* So we can call esp_acquire(). */ 23450Sstevel@tonic-gate 23460Sstevel@tonic-gate if (io->ipsec_out_v4) { 23470Sstevel@tonic-gate af = AF_INET; 23480Sstevel@tonic-gate divpoint = IPH_HDR_LENGTH(ipha); 23490Sstevel@tonic-gate datalen = ntohs(ipha->ipha_length) - divpoint; 23500Sstevel@tonic-gate nhp = (uint8_t *)&ipha->ipha_protocol; 23510Sstevel@tonic-gate } else { 23520Sstevel@tonic-gate ip6_pkt_t ipp; 23530Sstevel@tonic-gate 23540Sstevel@tonic-gate af = AF_INET6; 23550Sstevel@tonic-gate ip6h = (ip6_t *)ipha; 23560Sstevel@tonic-gate bzero(&ipp, sizeof (ipp)); 23570Sstevel@tonic-gate divpoint = ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL); 23580Sstevel@tonic-gate if (ipp.ipp_dstopts != NULL && 23590Sstevel@tonic-gate ipp.ipp_dstopts->ip6d_nxt != IPPROTO_ROUTING) { 23600Sstevel@tonic-gate /* 23610Sstevel@tonic-gate * Destination options are tricky. If we get in here, 23620Sstevel@tonic-gate * then we have a terminal header following the 23630Sstevel@tonic-gate * destination options. We need to adjust backwards 23640Sstevel@tonic-gate * so we insert ESP BEFORE the destination options 23650Sstevel@tonic-gate * bag. (So that the dstopts get encrypted!) 23660Sstevel@tonic-gate * 23670Sstevel@tonic-gate * Since this is for outbound packets only, we know 23680Sstevel@tonic-gate * that non-terminal destination options only precede 23690Sstevel@tonic-gate * routing headers. 23700Sstevel@tonic-gate */ 23710Sstevel@tonic-gate divpoint -= ipp.ipp_dstoptslen; 23720Sstevel@tonic-gate } 23730Sstevel@tonic-gate datalen = ntohs(ip6h->ip6_plen) + sizeof (ip6_t) - divpoint; 23740Sstevel@tonic-gate 23750Sstevel@tonic-gate if (ipp.ipp_rthdr != NULL) { 23760Sstevel@tonic-gate nhp = &ipp.ipp_rthdr->ip6r_nxt; 23770Sstevel@tonic-gate } else if (ipp.ipp_hopopts != NULL) { 23780Sstevel@tonic-gate nhp = &ipp.ipp_hopopts->ip6h_nxt; 23790Sstevel@tonic-gate } else { 23800Sstevel@tonic-gate ASSERT(divpoint == sizeof (ip6_t)); 23810Sstevel@tonic-gate /* It's probably IP + ESP. */ 23820Sstevel@tonic-gate nhp = &ip6h->ip6_nxt; 23830Sstevel@tonic-gate } 23840Sstevel@tonic-gate } 23850Sstevel@tonic-gate assoc = io->ipsec_out_esp_sa; 23860Sstevel@tonic-gate ASSERT(assoc != NULL); 23870Sstevel@tonic-gate 23880Sstevel@tonic-gate if (assoc->ipsa_usetime == 0) 23890Sstevel@tonic-gate esp_set_usetime(assoc, B_FALSE); 23900Sstevel@tonic-gate 23910Sstevel@tonic-gate if (assoc->ipsa_auth_alg != SADB_AALG_NONE) 23920Sstevel@tonic-gate mac_len = assoc->ipsa_mac_len; 23930Sstevel@tonic-gate 23940Sstevel@tonic-gate if (assoc->ipsa_flags & IPSA_F_NATT) { 23950Sstevel@tonic-gate /* wedge in fake UDP */ 23960Sstevel@tonic-gate is_natt = B_TRUE; 23970Sstevel@tonic-gate esplen += UDPH_SIZE; 23980Sstevel@tonic-gate } 23990Sstevel@tonic-gate 24000Sstevel@tonic-gate /* 24010Sstevel@tonic-gate * Set up ESP header and encryption padding for ENCR PI request. 24020Sstevel@tonic-gate */ 24030Sstevel@tonic-gate 24043192Sdanmcd /* Determine the padding length. Pad to 4-bytes for no-encryption. */ 24050Sstevel@tonic-gate if (assoc->ipsa_encr_alg != SADB_EALG_NULL) { 24063192Sdanmcd iv_len = assoc->ipsa_iv_len; 24073192Sdanmcd 24083192Sdanmcd /* 24093192Sdanmcd * Include the two additional bytes (hence the - 2) for the 24103192Sdanmcd * padding length and the next header. Take this into account 24113192Sdanmcd * when calculating the actual length of the padding. 24123192Sdanmcd */ 24133192Sdanmcd ASSERT(ISP2(iv_len)); 24143192Sdanmcd padlen = ((unsigned)(iv_len - datalen - 2)) & (iv_len - 1); 24150Sstevel@tonic-gate } else { 24163192Sdanmcd iv_len = 0; 24173192Sdanmcd padlen = ((unsigned)(sizeof (uint32_t) - datalen - 2)) & 24183192Sdanmcd (sizeof (uint32_t) - 1); 24190Sstevel@tonic-gate } 24200Sstevel@tonic-gate 24210Sstevel@tonic-gate /* Allocate ESP header and IV. */ 24220Sstevel@tonic-gate esplen += iv_len; 24230Sstevel@tonic-gate 24240Sstevel@tonic-gate /* 24250Sstevel@tonic-gate * Update association byte-count lifetimes. Don't forget to take 24260Sstevel@tonic-gate * into account the padding length and next-header (hence the + 2). 2427134Sdanmcd * 24280Sstevel@tonic-gate * Use the amount of data fed into the "encryption algorithm". This 24290Sstevel@tonic-gate * is the IV, the data length, the padding length, and the final two 24300Sstevel@tonic-gate * bytes (padlen, and next-header). 24310Sstevel@tonic-gate * 24320Sstevel@tonic-gate */ 24330Sstevel@tonic-gate 2434134Sdanmcd if (!esp_age_bytes(assoc, datalen + padlen + iv_len + 2, B_FALSE)) { 24350Sstevel@tonic-gate /* 24360Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 24370Sstevel@tonic-gate * pass it to ip_drop_packet(). 24380Sstevel@tonic-gate */ 24390Sstevel@tonic-gate ip_drop_packet(mp, B_FALSE, NULL, NULL, 2440*3448Sdh155122 DROPPER(ipss, ipds_esp_bytes_expire), 2441*3448Sdh155122 &espstack->esp_dropper); 24420Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 24430Sstevel@tonic-gate } 24440Sstevel@tonic-gate 24450Sstevel@tonic-gate espmp = allocb(esplen, BPRI_HI); 24460Sstevel@tonic-gate if (espmp == NULL) { 2447*3448Sdh155122 ESP_BUMP_STAT(espstack, out_discards); 2448*3448Sdh155122 esp1dbg(espstack, ("esp_outbound: can't allocate espmp.\n")); 24490Sstevel@tonic-gate /* 24500Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 24510Sstevel@tonic-gate * pass it to ip_drop_packet(). 24520Sstevel@tonic-gate */ 2453*3448Sdh155122 ip_drop_packet(mp, B_FALSE, NULL, NULL, 2454*3448Sdh155122 DROPPER(ipss, ipds_esp_nomem), 2455*3448Sdh155122 &espstack->esp_dropper); 24560Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 24570Sstevel@tonic-gate } 24580Sstevel@tonic-gate espmp->b_wptr += esplen; 24590Sstevel@tonic-gate esph = (esph_t *)espmp->b_rptr; 24600Sstevel@tonic-gate 24610Sstevel@tonic-gate if (is_natt) { 2462*3448Sdh155122 esp3dbg(espstack, ("esp_outbound: NATT")); 24630Sstevel@tonic-gate 24640Sstevel@tonic-gate udpha = (udpha_t *)espmp->b_rptr; 24650Sstevel@tonic-gate udpha->uha_src_port = htons(IPPORT_IKE_NATT); 24660Sstevel@tonic-gate if (assoc->ipsa_remote_port != 0) 24670Sstevel@tonic-gate udpha->uha_dst_port = assoc->ipsa_remote_port; 24680Sstevel@tonic-gate else 24690Sstevel@tonic-gate udpha->uha_dst_port = htons(IPPORT_IKE_NATT); 24700Sstevel@tonic-gate /* 24710Sstevel@tonic-gate * Set the checksum to 0, so that the ip_wput_ipsec_out() 24720Sstevel@tonic-gate * can do the right thing. 24730Sstevel@tonic-gate */ 24740Sstevel@tonic-gate udpha->uha_checksum = 0; 24750Sstevel@tonic-gate esph = (esph_t *)(udpha + 1); 24760Sstevel@tonic-gate } 24770Sstevel@tonic-gate 24780Sstevel@tonic-gate esph->esph_spi = assoc->ipsa_spi; 24790Sstevel@tonic-gate 24800Sstevel@tonic-gate esph->esph_replay = htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1)); 24810Sstevel@tonic-gate if (esph->esph_replay == 0 && assoc->ipsa_replay_wsize != 0) { 24820Sstevel@tonic-gate /* 24830Sstevel@tonic-gate * XXX We have replay counter wrapping. 24840Sstevel@tonic-gate * We probably want to nuke this SA (and its peer). 24850Sstevel@tonic-gate */ 24860Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, 24870Sstevel@tonic-gate SL_ERROR | SL_CONSOLE | SL_WARN, 24880Sstevel@tonic-gate "Outbound ESP SA (0x%x, %s) has wrapped sequence.\n", 2489*3448Sdh155122 esph->esph_spi, assoc->ipsa_dstaddr, af, 2490*3448Sdh155122 espstack->ipsecesp_netstack); 2491*3448Sdh155122 2492*3448Sdh155122 ESP_BUMP_STAT(espstack, out_discards); 24930Sstevel@tonic-gate sadb_replay_delete(assoc); 24940Sstevel@tonic-gate /* 24950Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 24960Sstevel@tonic-gate * pass it to ip_drop_packet(). 24970Sstevel@tonic-gate */ 2498*3448Sdh155122 ip_drop_packet(mp, B_FALSE, NULL, NULL, 2499*3448Sdh155122 DROPPER(ipss, ipds_esp_replay), 2500*3448Sdh155122 &espstack->esp_dropper); 25010Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 25020Sstevel@tonic-gate } 25030Sstevel@tonic-gate 25040Sstevel@tonic-gate /* 25050Sstevel@tonic-gate * Set the IV to a random quantity. We do not require the 25060Sstevel@tonic-gate * highest quality random bits, but for best security with CBC 25070Sstevel@tonic-gate * mode ciphers, the value must be unlikely to repeat and also 25080Sstevel@tonic-gate * must not be known in advance to an adversary capable of 25090Sstevel@tonic-gate * influencing the plaintext. 25100Sstevel@tonic-gate */ 25110Sstevel@tonic-gate (void) random_get_pseudo_bytes((uint8_t *)(esph + 1), iv_len); 25120Sstevel@tonic-gate 25130Sstevel@tonic-gate /* Fix the IP header. */ 25140Sstevel@tonic-gate alloclen = padlen + 2 + mac_len; 25150Sstevel@tonic-gate adj = alloclen + (espmp->b_wptr - espmp->b_rptr); 25160Sstevel@tonic-gate 25170Sstevel@tonic-gate protocol = *nhp; 25180Sstevel@tonic-gate 25190Sstevel@tonic-gate if (io->ipsec_out_v4) { 25200Sstevel@tonic-gate ipha->ipha_length = htons(ntohs(ipha->ipha_length) + adj); 25210Sstevel@tonic-gate if (is_natt) { 25220Sstevel@tonic-gate *nhp = IPPROTO_UDP; 25230Sstevel@tonic-gate udpha->uha_length = htons(ntohs(ipha->ipha_length) - 25240Sstevel@tonic-gate IPH_HDR_LENGTH(ipha)); 25250Sstevel@tonic-gate } else { 25260Sstevel@tonic-gate *nhp = IPPROTO_ESP; 25270Sstevel@tonic-gate } 25280Sstevel@tonic-gate ipha->ipha_hdr_checksum = 0; 25290Sstevel@tonic-gate ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha); 25300Sstevel@tonic-gate } else { 25310Sstevel@tonic-gate ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) + adj); 25320Sstevel@tonic-gate *nhp = IPPROTO_ESP; 25330Sstevel@tonic-gate } 25340Sstevel@tonic-gate 25350Sstevel@tonic-gate /* I've got the two ESP mblks, now insert them. */ 25360Sstevel@tonic-gate 2537*3448Sdh155122 esp2dbg(espstack, ("data_mp before outbound ESP adjustment:\n")); 2538*3448Sdh155122 esp2dbg(espstack, (dump_msg(data_mp))); 2539*3448Sdh155122 2540*3448Sdh155122 if (!esp_insert_esp(data_mp, espmp, divpoint, espstack)) { 2541*3448Sdh155122 ESP_BUMP_STAT(espstack, out_discards); 25420Sstevel@tonic-gate /* NOTE: esp_insert_esp() only fails if there's no memory. */ 25430Sstevel@tonic-gate /* 25440Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 25450Sstevel@tonic-gate * pass it to ip_drop_packet(). 25460Sstevel@tonic-gate */ 2547*3448Sdh155122 ip_drop_packet(mp, B_FALSE, NULL, NULL, 2548*3448Sdh155122 DROPPER(ipss, ipds_esp_nomem), 2549*3448Sdh155122 &espstack->esp_dropper); 25500Sstevel@tonic-gate freeb(espmp); 25510Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 25520Sstevel@tonic-gate } 25530Sstevel@tonic-gate 25540Sstevel@tonic-gate /* Append padding (and leave room for ICV). */ 25550Sstevel@tonic-gate for (tailmp = data_mp; tailmp->b_cont != NULL; tailmp = tailmp->b_cont) 25560Sstevel@tonic-gate ; 25570Sstevel@tonic-gate if (tailmp->b_wptr + alloclen > tailmp->b_datap->db_lim) { 25580Sstevel@tonic-gate tailmp->b_cont = allocb(alloclen, BPRI_HI); 25590Sstevel@tonic-gate if (tailmp->b_cont == NULL) { 2560*3448Sdh155122 ESP_BUMP_STAT(espstack, out_discards); 25610Sstevel@tonic-gate esp0dbg(("esp_outbound: Can't allocate tailmp.\n")); 25620Sstevel@tonic-gate /* 25630Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 25640Sstevel@tonic-gate * pass it to ip_drop_packet(). 25650Sstevel@tonic-gate */ 25660Sstevel@tonic-gate ip_drop_packet(mp, B_FALSE, NULL, NULL, 2567*3448Sdh155122 DROPPER(ipss, ipds_esp_nomem), 2568*3448Sdh155122 &espstack->esp_dropper); 25690Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 25700Sstevel@tonic-gate } 25710Sstevel@tonic-gate tailmp = tailmp->b_cont; 25720Sstevel@tonic-gate } 25730Sstevel@tonic-gate 25740Sstevel@tonic-gate /* 25750Sstevel@tonic-gate * If there's padding, N bytes of padding must be of the form 0x1, 25760Sstevel@tonic-gate * 0x2, 0x3... 0xN. 25770Sstevel@tonic-gate */ 25780Sstevel@tonic-gate for (i = 0; i < padlen; ) { 25790Sstevel@tonic-gate i++; 25800Sstevel@tonic-gate *tailmp->b_wptr++ = i; 25810Sstevel@tonic-gate } 25820Sstevel@tonic-gate *tailmp->b_wptr++ = i; 25830Sstevel@tonic-gate *tailmp->b_wptr++ = protocol; 25840Sstevel@tonic-gate 2585*3448Sdh155122 esp2dbg(espstack, ("data_Mp before encryption:\n")); 2586*3448Sdh155122 esp2dbg(espstack, (dump_msg(data_mp))); 25870Sstevel@tonic-gate 25880Sstevel@tonic-gate /* 25890Sstevel@tonic-gate * The packet is eligible for hardware acceleration if the 25900Sstevel@tonic-gate * following conditions are satisfied: 25910Sstevel@tonic-gate * 25920Sstevel@tonic-gate * 1. the packet will not be fragmented 25930Sstevel@tonic-gate * 2. the provider supports the algorithms specified by SA 25940Sstevel@tonic-gate * 3. there is no pending control message being exchanged 25950Sstevel@tonic-gate * 4. snoop is not attached 25960Sstevel@tonic-gate * 5. the destination address is not a multicast address 25970Sstevel@tonic-gate * 25980Sstevel@tonic-gate * All five of these conditions are checked by IP prior to 25990Sstevel@tonic-gate * sending the packet to ESP. 26000Sstevel@tonic-gate * 26010Sstevel@tonic-gate * But We, and We Alone, can, nay MUST check if the packet 26020Sstevel@tonic-gate * is over NATT, and then disqualify it from hardware 26030Sstevel@tonic-gate * acceleration. 26040Sstevel@tonic-gate */ 26050Sstevel@tonic-gate 26060Sstevel@tonic-gate if (io->ipsec_out_is_capab_ill && !(assoc->ipsa_flags & IPSA_F_NATT)) { 26070Sstevel@tonic-gate return (esp_outbound_accelerated(ipsec_out_mp, mac_len)); 26080Sstevel@tonic-gate } 2609*3448Sdh155122 ESP_BUMP_STAT(espstack, noaccel); 26100Sstevel@tonic-gate 26110Sstevel@tonic-gate /* 26120Sstevel@tonic-gate * Okay. I've set up the pre-encryption ESP. Let's do it! 26130Sstevel@tonic-gate */ 26140Sstevel@tonic-gate 26150Sstevel@tonic-gate if (mac_len > 0) { 26160Sstevel@tonic-gate ASSERT(tailmp->b_wptr + mac_len <= tailmp->b_datap->db_lim); 26170Sstevel@tonic-gate icv_buf = tailmp->b_wptr; 26180Sstevel@tonic-gate tailmp->b_wptr += mac_len; 26190Sstevel@tonic-gate } else { 26200Sstevel@tonic-gate icv_buf = NULL; 26210Sstevel@tonic-gate } 26220Sstevel@tonic-gate 26230Sstevel@tonic-gate return (esp_submit_req_outbound(ipsec_out_mp, assoc, icv_buf, 26240Sstevel@tonic-gate datalen + padlen + 2)); 26250Sstevel@tonic-gate } 26260Sstevel@tonic-gate 26270Sstevel@tonic-gate /* 26280Sstevel@tonic-gate * IP calls this to validate the ICMP errors that 26290Sstevel@tonic-gate * we got from the network. 26300Sstevel@tonic-gate */ 26310Sstevel@tonic-gate ipsec_status_t 26320Sstevel@tonic-gate ipsecesp_icmp_error(mblk_t *ipsec_mp) 26330Sstevel@tonic-gate { 2634*3448Sdh155122 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 2635*3448Sdh155122 boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN); 2636*3448Sdh155122 netstack_t *ns; 2637*3448Sdh155122 ipsecesp_stack_t *espstack; 2638*3448Sdh155122 ipsec_stack_t *ipss; 2639*3448Sdh155122 2640*3448Sdh155122 if (is_inbound) { 2641*3448Sdh155122 ns = ii->ipsec_in_ns; 2642*3448Sdh155122 } else { 2643*3448Sdh155122 ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 2644*3448Sdh155122 2645*3448Sdh155122 ns = io->ipsec_out_ns; 2646*3448Sdh155122 } 2647*3448Sdh155122 espstack = ns->netstack_ipsecesp; 2648*3448Sdh155122 ipss = ns->netstack_ipsec; 2649*3448Sdh155122 26500Sstevel@tonic-gate /* 26510Sstevel@tonic-gate * Unless we get an entire packet back, this function is useless. 26520Sstevel@tonic-gate * Why? 26530Sstevel@tonic-gate * 26540Sstevel@tonic-gate * 1.) Partial packets are useless, because the "next header" 26550Sstevel@tonic-gate * is at the end of the decrypted ESP packet. Without the 26560Sstevel@tonic-gate * whole packet, this is useless. 26570Sstevel@tonic-gate * 26580Sstevel@tonic-gate * 2.) If we every use a stateful cipher, such as a stream or a 26590Sstevel@tonic-gate * one-time pad, we can't do anything. 26600Sstevel@tonic-gate * 26610Sstevel@tonic-gate * Since the chances of us getting an entire packet back are very 26620Sstevel@tonic-gate * very small, we discard here. 26630Sstevel@tonic-gate */ 2664*3448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards); 2665*3448Sdh155122 ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, 2666*3448Sdh155122 DROPPER(ipss, ipds_esp_icmp), 2667*3448Sdh155122 &espstack->esp_dropper); 26680Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 26690Sstevel@tonic-gate } 26700Sstevel@tonic-gate 26710Sstevel@tonic-gate /* 26720Sstevel@tonic-gate * ESP module read put routine. 26730Sstevel@tonic-gate */ 26740Sstevel@tonic-gate /* ARGSUSED */ 26750Sstevel@tonic-gate static void 26760Sstevel@tonic-gate ipsecesp_rput(queue_t *q, mblk_t *mp) 26770Sstevel@tonic-gate { 2678*3448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)q->q_ptr; 2679*3448Sdh155122 26803055Sdanmcd ASSERT(mp->b_datap->db_type != M_CTL); /* No more IRE_DB_REQ. */ 2681*3448Sdh155122 26820Sstevel@tonic-gate switch (mp->b_datap->db_type) { 26830Sstevel@tonic-gate case M_PROTO: 26840Sstevel@tonic-gate case M_PCPROTO: 26850Sstevel@tonic-gate /* TPI message of some sort. */ 26860Sstevel@tonic-gate switch (*((t_scalar_t *)mp->b_rptr)) { 26870Sstevel@tonic-gate case T_BIND_ACK: 2688*3448Sdh155122 esp3dbg(espstack, 2689*3448Sdh155122 ("Thank you IP from ESP for T_BIND_ACK\n")); 26900Sstevel@tonic-gate break; 26910Sstevel@tonic-gate case T_ERROR_ACK: 26920Sstevel@tonic-gate cmn_err(CE_WARN, 26930Sstevel@tonic-gate "ipsecesp: ESP received T_ERROR_ACK from IP."); 26940Sstevel@tonic-gate /* 26950Sstevel@tonic-gate * Make esp_sadb.s_ip_q NULL, and in the 26960Sstevel@tonic-gate * future, perhaps try again. 26970Sstevel@tonic-gate */ 2698*3448Sdh155122 espstack->esp_sadb.s_ip_q = NULL; 26990Sstevel@tonic-gate break; 27000Sstevel@tonic-gate case T_OK_ACK: 27010Sstevel@tonic-gate /* Probably from a (rarely sent) T_UNBIND_REQ. */ 27020Sstevel@tonic-gate break; 27030Sstevel@tonic-gate default: 27040Sstevel@tonic-gate esp0dbg(("Unknown M_{,PC}PROTO message.\n")); 27050Sstevel@tonic-gate } 27060Sstevel@tonic-gate freemsg(mp); 27070Sstevel@tonic-gate break; 27080Sstevel@tonic-gate default: 27090Sstevel@tonic-gate /* For now, passthru message. */ 2710*3448Sdh155122 esp2dbg(espstack, ("ESP got unknown mblk type %d.\n", 27110Sstevel@tonic-gate mp->b_datap->db_type)); 27120Sstevel@tonic-gate putnext(q, mp); 27130Sstevel@tonic-gate } 27140Sstevel@tonic-gate } 27150Sstevel@tonic-gate 27160Sstevel@tonic-gate /* 27170Sstevel@tonic-gate * Construct an SADB_REGISTER message with the current algorithms. 27180Sstevel@tonic-gate */ 27190Sstevel@tonic-gate static boolean_t 2720*3448Sdh155122 esp_register_out(uint32_t sequence, uint32_t pid, uint_t serial, 2721*3448Sdh155122 ipsecesp_stack_t *espstack) 27220Sstevel@tonic-gate { 27230Sstevel@tonic-gate mblk_t *pfkey_msg_mp, *keysock_out_mp; 27240Sstevel@tonic-gate sadb_msg_t *samsg; 27250Sstevel@tonic-gate sadb_supported_t *sasupp_auth = NULL; 27260Sstevel@tonic-gate sadb_supported_t *sasupp_encr = NULL; 27270Sstevel@tonic-gate sadb_alg_t *saalg; 27280Sstevel@tonic-gate uint_t allocsize = sizeof (*samsg); 27290Sstevel@tonic-gate uint_t i, numalgs_snap; 27300Sstevel@tonic-gate int current_aalgs; 27310Sstevel@tonic-gate ipsec_alginfo_t **authalgs; 27320Sstevel@tonic-gate uint_t num_aalgs; 27330Sstevel@tonic-gate int current_ealgs; 27340Sstevel@tonic-gate ipsec_alginfo_t **encralgs; 27350Sstevel@tonic-gate uint_t num_ealgs; 2736*3448Sdh155122 ipsec_stack_t *ipss = espstack->ipsecesp_netstack->netstack_ipsec; 27370Sstevel@tonic-gate 27380Sstevel@tonic-gate /* Allocate the KEYSOCK_OUT. */ 27390Sstevel@tonic-gate keysock_out_mp = sadb_keysock_out(serial); 27400Sstevel@tonic-gate if (keysock_out_mp == NULL) { 27410Sstevel@tonic-gate esp0dbg(("esp_register_out: couldn't allocate mblk.\n")); 27420Sstevel@tonic-gate return (B_FALSE); 27430Sstevel@tonic-gate } 27440Sstevel@tonic-gate 27450Sstevel@tonic-gate /* 27460Sstevel@tonic-gate * Allocate the PF_KEY message that follows KEYSOCK_OUT. 27470Sstevel@tonic-gate */ 27480Sstevel@tonic-gate 2749*3448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 27500Sstevel@tonic-gate 27510Sstevel@tonic-gate /* 27520Sstevel@tonic-gate * Fill SADB_REGISTER message's algorithm descriptors. Hold 27530Sstevel@tonic-gate * down the lock while filling it. 27540Sstevel@tonic-gate * 27550Sstevel@tonic-gate * Return only valid algorithms, so the number of algorithms 27560Sstevel@tonic-gate * to send up may be less than the number of algorithm entries 27570Sstevel@tonic-gate * in the table. 27580Sstevel@tonic-gate */ 2759*3448Sdh155122 authalgs = ipss->ipsec_alglists[IPSEC_ALG_AUTH]; 27600Sstevel@tonic-gate for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++) 27610Sstevel@tonic-gate if (authalgs[i] != NULL && ALG_VALID(authalgs[i])) 27620Sstevel@tonic-gate num_aalgs++; 27630Sstevel@tonic-gate 27640Sstevel@tonic-gate if (num_aalgs != 0) { 27650Sstevel@tonic-gate allocsize += (num_aalgs * sizeof (*saalg)); 27660Sstevel@tonic-gate allocsize += sizeof (*sasupp_auth); 27670Sstevel@tonic-gate } 2768*3448Sdh155122 encralgs = ipss->ipsec_alglists[IPSEC_ALG_ENCR]; 27690Sstevel@tonic-gate for (num_ealgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++) 27700Sstevel@tonic-gate if (encralgs[i] != NULL && ALG_VALID(encralgs[i])) 27710Sstevel@tonic-gate num_ealgs++; 27720Sstevel@tonic-gate 27730Sstevel@tonic-gate if (num_ealgs != 0) { 27740Sstevel@tonic-gate allocsize += (num_ealgs * sizeof (*saalg)); 27750Sstevel@tonic-gate allocsize += sizeof (*sasupp_encr); 27760Sstevel@tonic-gate } 27770Sstevel@tonic-gate keysock_out_mp->b_cont = allocb(allocsize, BPRI_HI); 27780Sstevel@tonic-gate if (keysock_out_mp->b_cont == NULL) { 2779*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 27800Sstevel@tonic-gate freemsg(keysock_out_mp); 27810Sstevel@tonic-gate return (B_FALSE); 27820Sstevel@tonic-gate } 27830Sstevel@tonic-gate 27840Sstevel@tonic-gate pfkey_msg_mp = keysock_out_mp->b_cont; 27850Sstevel@tonic-gate pfkey_msg_mp->b_wptr += allocsize; 27860Sstevel@tonic-gate if (num_aalgs != 0) { 27870Sstevel@tonic-gate sasupp_auth = (sadb_supported_t *) 27880Sstevel@tonic-gate (pfkey_msg_mp->b_rptr + sizeof (*samsg)); 27890Sstevel@tonic-gate saalg = (sadb_alg_t *)(sasupp_auth + 1); 27900Sstevel@tonic-gate 27910Sstevel@tonic-gate ASSERT(((ulong_t)saalg & 0x7) == 0); 27920Sstevel@tonic-gate 27930Sstevel@tonic-gate numalgs_snap = 0; 27940Sstevel@tonic-gate for (i = 0; 2795*3448Sdh155122 ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs)); 2796*3448Sdh155122 i++) { 27970Sstevel@tonic-gate if (authalgs[i] == NULL || !ALG_VALID(authalgs[i])) 27980Sstevel@tonic-gate continue; 27990Sstevel@tonic-gate 28000Sstevel@tonic-gate saalg->sadb_alg_id = authalgs[i]->alg_id; 28010Sstevel@tonic-gate saalg->sadb_alg_ivlen = 0; 28020Sstevel@tonic-gate saalg->sadb_alg_minbits = authalgs[i]->alg_ef_minbits; 28030Sstevel@tonic-gate saalg->sadb_alg_maxbits = authalgs[i]->alg_ef_maxbits; 28040Sstevel@tonic-gate saalg->sadb_x_alg_defincr = authalgs[i]->alg_ef_default; 28050Sstevel@tonic-gate saalg->sadb_x_alg_increment = 28060Sstevel@tonic-gate authalgs[i]->alg_increment; 28070Sstevel@tonic-gate numalgs_snap++; 28080Sstevel@tonic-gate saalg++; 28090Sstevel@tonic-gate } 28100Sstevel@tonic-gate ASSERT(numalgs_snap == num_aalgs); 28110Sstevel@tonic-gate #ifdef DEBUG 28120Sstevel@tonic-gate /* 28130Sstevel@tonic-gate * Reality check to make sure I snagged all of the 28140Sstevel@tonic-gate * algorithms. 28150Sstevel@tonic-gate */ 28160Sstevel@tonic-gate for (; i < IPSEC_MAX_ALGS; i++) { 28170Sstevel@tonic-gate if (authalgs[i] != NULL && ALG_VALID(authalgs[i])) { 28180Sstevel@tonic-gate cmn_err(CE_PANIC, "esp_register_out()! " 28190Sstevel@tonic-gate "Missed aalg #%d.\n", i); 28200Sstevel@tonic-gate } 28210Sstevel@tonic-gate } 28220Sstevel@tonic-gate #endif /* DEBUG */ 28230Sstevel@tonic-gate } else { 28240Sstevel@tonic-gate saalg = (sadb_alg_t *)(pfkey_msg_mp->b_rptr + sizeof (*samsg)); 28250Sstevel@tonic-gate } 28260Sstevel@tonic-gate 28270Sstevel@tonic-gate if (num_ealgs != 0) { 28280Sstevel@tonic-gate sasupp_encr = (sadb_supported_t *)saalg; 28290Sstevel@tonic-gate saalg = (sadb_alg_t *)(sasupp_encr + 1); 28300Sstevel@tonic-gate 28310Sstevel@tonic-gate numalgs_snap = 0; 28320Sstevel@tonic-gate for (i = 0; 28330Sstevel@tonic-gate ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_ealgs)); i++) { 28340Sstevel@tonic-gate if (encralgs[i] == NULL || !ALG_VALID(encralgs[i])) 28350Sstevel@tonic-gate continue; 28360Sstevel@tonic-gate saalg->sadb_alg_id = encralgs[i]->alg_id; 28370Sstevel@tonic-gate saalg->sadb_alg_ivlen = encralgs[i]->alg_datalen; 28380Sstevel@tonic-gate saalg->sadb_alg_minbits = encralgs[i]->alg_ef_minbits; 28390Sstevel@tonic-gate saalg->sadb_alg_maxbits = encralgs[i]->alg_ef_maxbits; 28400Sstevel@tonic-gate saalg->sadb_x_alg_defincr = encralgs[i]->alg_ef_default; 28410Sstevel@tonic-gate saalg->sadb_x_alg_increment = 28420Sstevel@tonic-gate encralgs[i]->alg_increment; 28430Sstevel@tonic-gate numalgs_snap++; 28440Sstevel@tonic-gate saalg++; 28450Sstevel@tonic-gate } 28460Sstevel@tonic-gate ASSERT(numalgs_snap == num_ealgs); 28470Sstevel@tonic-gate #ifdef DEBUG 28480Sstevel@tonic-gate /* 28490Sstevel@tonic-gate * Reality check to make sure I snagged all of the 28500Sstevel@tonic-gate * algorithms. 28510Sstevel@tonic-gate */ 28520Sstevel@tonic-gate for (; i < IPSEC_MAX_ALGS; i++) { 28530Sstevel@tonic-gate if (encralgs[i] != NULL && ALG_VALID(encralgs[i])) { 28540Sstevel@tonic-gate cmn_err(CE_PANIC, "esp_register_out()! " 28550Sstevel@tonic-gate "Missed ealg #%d.\n", i); 28560Sstevel@tonic-gate } 28570Sstevel@tonic-gate } 28580Sstevel@tonic-gate #endif /* DEBUG */ 28590Sstevel@tonic-gate } 28600Sstevel@tonic-gate 28610Sstevel@tonic-gate current_aalgs = num_aalgs; 28620Sstevel@tonic-gate current_ealgs = num_ealgs; 28630Sstevel@tonic-gate 2864*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 28650Sstevel@tonic-gate 28660Sstevel@tonic-gate /* Now fill the rest of the SADB_REGISTER message. */ 28670Sstevel@tonic-gate 28680Sstevel@tonic-gate samsg = (sadb_msg_t *)pfkey_msg_mp->b_rptr; 28690Sstevel@tonic-gate samsg->sadb_msg_version = PF_KEY_V2; 28700Sstevel@tonic-gate samsg->sadb_msg_type = SADB_REGISTER; 28710Sstevel@tonic-gate samsg->sadb_msg_errno = 0; 28720Sstevel@tonic-gate samsg->sadb_msg_satype = SADB_SATYPE_ESP; 28730Sstevel@tonic-gate samsg->sadb_msg_len = SADB_8TO64(allocsize); 28740Sstevel@tonic-gate samsg->sadb_msg_reserved = 0; 28750Sstevel@tonic-gate /* 28760Sstevel@tonic-gate * Assume caller has sufficient sequence/pid number info. If it's one 28770Sstevel@tonic-gate * from me over a new alg., I could give two hoots about sequence. 28780Sstevel@tonic-gate */ 28790Sstevel@tonic-gate samsg->sadb_msg_seq = sequence; 28800Sstevel@tonic-gate samsg->sadb_msg_pid = pid; 28810Sstevel@tonic-gate 28820Sstevel@tonic-gate if (sasupp_auth != NULL) { 28830Sstevel@tonic-gate sasupp_auth->sadb_supported_len = 28840Sstevel@tonic-gate SADB_8TO64(sizeof (*sasupp_auth) + 28850Sstevel@tonic-gate sizeof (*saalg) * current_aalgs); 28860Sstevel@tonic-gate sasupp_auth->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH; 28870Sstevel@tonic-gate sasupp_auth->sadb_supported_reserved = 0; 28880Sstevel@tonic-gate } 28890Sstevel@tonic-gate 28900Sstevel@tonic-gate if (sasupp_encr != NULL) { 28910Sstevel@tonic-gate sasupp_encr->sadb_supported_len = 28920Sstevel@tonic-gate SADB_8TO64(sizeof (*sasupp_encr) + 28930Sstevel@tonic-gate sizeof (*saalg) * current_ealgs); 28940Sstevel@tonic-gate sasupp_encr->sadb_supported_exttype = 28950Sstevel@tonic-gate SADB_EXT_SUPPORTED_ENCRYPT; 28960Sstevel@tonic-gate sasupp_encr->sadb_supported_reserved = 0; 28970Sstevel@tonic-gate } 28980Sstevel@tonic-gate 2899*3448Sdh155122 if (espstack->esp_pfkey_q != NULL) 2900*3448Sdh155122 putnext(espstack->esp_pfkey_q, keysock_out_mp); 29010Sstevel@tonic-gate else { 29020Sstevel@tonic-gate freemsg(keysock_out_mp); 29030Sstevel@tonic-gate return (B_FALSE); 29040Sstevel@tonic-gate } 29050Sstevel@tonic-gate 29060Sstevel@tonic-gate return (B_TRUE); 29070Sstevel@tonic-gate } 29080Sstevel@tonic-gate 29090Sstevel@tonic-gate /* 29100Sstevel@tonic-gate * Invoked when the algorithm table changes. Causes SADB_REGISTER 29110Sstevel@tonic-gate * messages continaining the current list of algorithms to be 29120Sstevel@tonic-gate * sent up to the ESP listeners. 29130Sstevel@tonic-gate */ 29140Sstevel@tonic-gate void 2915*3448Sdh155122 ipsecesp_algs_changed(netstack_t *ns) 29160Sstevel@tonic-gate { 2917*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 2918*3448Sdh155122 29190Sstevel@tonic-gate /* 29200Sstevel@tonic-gate * Time to send a PF_KEY SADB_REGISTER message to ESP listeners 29210Sstevel@tonic-gate * everywhere. (The function itself checks for NULL esp_pfkey_q.) 29220Sstevel@tonic-gate */ 2923*3448Sdh155122 (void) esp_register_out(0, 0, 0, espstack); 29240Sstevel@tonic-gate } 29250Sstevel@tonic-gate 29260Sstevel@tonic-gate /* 29270Sstevel@tonic-gate * taskq_dispatch handler. 29280Sstevel@tonic-gate */ 29290Sstevel@tonic-gate static void 29300Sstevel@tonic-gate inbound_task(void *arg) 29310Sstevel@tonic-gate { 29320Sstevel@tonic-gate esph_t *esph; 29330Sstevel@tonic-gate mblk_t *mp = (mblk_t *)arg; 29340Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr; 2935*3448Sdh155122 netstack_t *ns = ii->ipsec_in_ns; 2936*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 29370Sstevel@tonic-gate int ipsec_rc; 29380Sstevel@tonic-gate 2939*3448Sdh155122 esp2dbg(espstack, ("in ESP inbound_task")); 2940*3448Sdh155122 ASSERT(espstack != NULL); 2941*3448Sdh155122 2942*3448Sdh155122 esph = ipsec_inbound_esp_sa(mp, ns); 29430Sstevel@tonic-gate if (esph == NULL) 29440Sstevel@tonic-gate return; 29450Sstevel@tonic-gate ASSERT(ii->ipsec_in_esp_sa != NULL); 29460Sstevel@tonic-gate ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func(mp, esph); 29470Sstevel@tonic-gate if (ipsec_rc != IPSEC_STATUS_SUCCESS) 29480Sstevel@tonic-gate return; 29490Sstevel@tonic-gate ip_fanout_proto_again(mp, NULL, NULL, NULL); 29500Sstevel@tonic-gate } 29510Sstevel@tonic-gate 29520Sstevel@tonic-gate /* 29530Sstevel@tonic-gate * Now that weak-key passed, actually ADD the security association, and 29540Sstevel@tonic-gate * send back a reply ADD message. 29550Sstevel@tonic-gate */ 29560Sstevel@tonic-gate static int 29573055Sdanmcd esp_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi, 2958*3448Sdh155122 int *diagnostic, ipsecesp_stack_t *espstack) 29590Sstevel@tonic-gate { 2960691Ssommerfe isaf_t *primary, *secondary, *inbound, *outbound; 29610Sstevel@tonic-gate sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 29620Sstevel@tonic-gate sadb_address_t *dstext = 29630Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 29640Sstevel@tonic-gate struct sockaddr_in *dst; 29650Sstevel@tonic-gate struct sockaddr_in6 *dst6; 29660Sstevel@tonic-gate boolean_t is_ipv4, clone = B_FALSE, is_inbound = B_FALSE; 29670Sstevel@tonic-gate uint32_t *dstaddr; 29680Sstevel@tonic-gate ipsa_t *larval = NULL; 29690Sstevel@tonic-gate ipsacq_t *acqrec; 29700Sstevel@tonic-gate iacqf_t *acq_bucket; 29710Sstevel@tonic-gate mblk_t *acq_msgs = NULL; 29720Sstevel@tonic-gate int rc; 29730Sstevel@tonic-gate sadb_t *sp; 29740Sstevel@tonic-gate int outhash; 29750Sstevel@tonic-gate mblk_t *lpkt; 2976*3448Sdh155122 ipsec_stack_t *ipss = espstack->ipsecesp_netstack->netstack_ipsec; 29770Sstevel@tonic-gate 29780Sstevel@tonic-gate /* 29790Sstevel@tonic-gate * Locate the appropriate table(s). 29800Sstevel@tonic-gate */ 29810Sstevel@tonic-gate 29820Sstevel@tonic-gate dst = (struct sockaddr_in *)(dstext + 1); 29830Sstevel@tonic-gate dst6 = (struct sockaddr_in6 *)dst; 29840Sstevel@tonic-gate is_ipv4 = (dst->sin_family == AF_INET); 29850Sstevel@tonic-gate if (is_ipv4) { 2986*3448Sdh155122 sp = &espstack->esp_sadb.s_v4; 29870Sstevel@tonic-gate dstaddr = (uint32_t *)(&dst->sin_addr); 2988564Ssommerfe outhash = OUTBOUND_HASH_V4(sp, *(ipaddr_t *)dstaddr); 29890Sstevel@tonic-gate } else { 2990*3448Sdh155122 sp = &espstack->esp_sadb.s_v6; 29910Sstevel@tonic-gate dstaddr = (uint32_t *)(&dst6->sin6_addr); 2992564Ssommerfe outhash = OUTBOUND_HASH_V6(sp, *(in6_addr_t *)dstaddr); 29930Sstevel@tonic-gate } 2994691Ssommerfe 2995564Ssommerfe inbound = INBOUND_BUCKET(sp, assoc->sadb_sa_spi); 2996691Ssommerfe outbound = &sp->sdb_of[outhash]; 2997691Ssommerfe 29980Sstevel@tonic-gate switch (ksi->ks_in_dsttype) { 29990Sstevel@tonic-gate case KS_IN_ADDR_MBCAST: 30000Sstevel@tonic-gate clone = B_TRUE; /* All mcast SAs can be bidirectional */ 30010Sstevel@tonic-gate /* FALLTHRU */ 30020Sstevel@tonic-gate case KS_IN_ADDR_ME: 30030Sstevel@tonic-gate primary = inbound; 3004691Ssommerfe secondary = outbound; 30050Sstevel@tonic-gate /* 30060Sstevel@tonic-gate * If the source address is either one of mine, or unspecified 30070Sstevel@tonic-gate * (which is best summed up by saying "not 'not mine'"), 30080Sstevel@tonic-gate * then the association is potentially bi-directional, 30090Sstevel@tonic-gate * in that it can be used for inbound traffic and outbound 30100Sstevel@tonic-gate * traffic. The best example of such an SA is a multicast 30110Sstevel@tonic-gate * SA (which allows me to receive the outbound traffic). 30120Sstevel@tonic-gate */ 30130Sstevel@tonic-gate if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME) 30140Sstevel@tonic-gate clone = B_TRUE; 30150Sstevel@tonic-gate is_inbound = B_TRUE; 30160Sstevel@tonic-gate break; 30170Sstevel@tonic-gate case KS_IN_ADDR_NOTME: 3018691Ssommerfe primary = outbound; 30190Sstevel@tonic-gate secondary = inbound; 30200Sstevel@tonic-gate /* 30210Sstevel@tonic-gate * If the source address literally not mine (either 30220Sstevel@tonic-gate * unspecified or not mine), then this SA may have an 30230Sstevel@tonic-gate * address that WILL be mine after some configuration. 30240Sstevel@tonic-gate * We pay the price for this by making it a bi-directional 30250Sstevel@tonic-gate * SA. 30260Sstevel@tonic-gate */ 30270Sstevel@tonic-gate if (ksi->ks_in_srctype != KS_IN_ADDR_ME) 30280Sstevel@tonic-gate clone = B_TRUE; 30290Sstevel@tonic-gate break; 30300Sstevel@tonic-gate default: 30313055Sdanmcd *diagnostic = SADB_X_DIAGNOSTIC_BAD_DST; 30320Sstevel@tonic-gate return (EINVAL); 30330Sstevel@tonic-gate } 30340Sstevel@tonic-gate 30350Sstevel@tonic-gate /* 30360Sstevel@tonic-gate * Find a ACQUIRE list entry if possible. If we've added an SA that 30370Sstevel@tonic-gate * suits the needs of an ACQUIRE list entry, we can eliminate the 30380Sstevel@tonic-gate * ACQUIRE list entry and transmit the enqueued packets. Use the 30390Sstevel@tonic-gate * high-bit of the sequence number to queue it. Key off destination 30400Sstevel@tonic-gate * addr, and change acqrec's state. 30410Sstevel@tonic-gate */ 30420Sstevel@tonic-gate 30430Sstevel@tonic-gate if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) { 30440Sstevel@tonic-gate acq_bucket = &sp->sdb_acq[outhash]; 30450Sstevel@tonic-gate mutex_enter(&acq_bucket->iacqf_lock); 30460Sstevel@tonic-gate for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL; 30470Sstevel@tonic-gate acqrec = acqrec->ipsacq_next) { 30480Sstevel@tonic-gate mutex_enter(&acqrec->ipsacq_lock); 30490Sstevel@tonic-gate /* 30500Sstevel@tonic-gate * Q: I only check sequence. Should I check dst? 30510Sstevel@tonic-gate * A: Yes, check dest because those are the packets 30520Sstevel@tonic-gate * that are queued up. 30530Sstevel@tonic-gate */ 30540Sstevel@tonic-gate if (acqrec->ipsacq_seq == samsg->sadb_msg_seq && 30550Sstevel@tonic-gate IPSA_ARE_ADDR_EQUAL(dstaddr, 30560Sstevel@tonic-gate acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam)) 30570Sstevel@tonic-gate break; 30580Sstevel@tonic-gate mutex_exit(&acqrec->ipsacq_lock); 30590Sstevel@tonic-gate } 30600Sstevel@tonic-gate if (acqrec != NULL) { 30610Sstevel@tonic-gate /* 30620Sstevel@tonic-gate * AHA! I found an ACQUIRE record for this SA. 30630Sstevel@tonic-gate * Grab the msg list, and free the acquire record. 30640Sstevel@tonic-gate * I already am holding the lock for this record, 30650Sstevel@tonic-gate * so all I have to do is free it. 30660Sstevel@tonic-gate */ 30670Sstevel@tonic-gate acq_msgs = acqrec->ipsacq_mp; 30680Sstevel@tonic-gate acqrec->ipsacq_mp = NULL; 30690Sstevel@tonic-gate mutex_exit(&acqrec->ipsacq_lock); 3070*3448Sdh155122 sadb_destroy_acquire(acqrec, 3071*3448Sdh155122 espstack->ipsecesp_netstack); 30720Sstevel@tonic-gate } 30730Sstevel@tonic-gate mutex_exit(&acq_bucket->iacqf_lock); 30740Sstevel@tonic-gate } 30750Sstevel@tonic-gate 30760Sstevel@tonic-gate /* 30770Sstevel@tonic-gate * Find PF_KEY message, and see if I'm an update. If so, find entry 30780Sstevel@tonic-gate * in larval list (if there). 30790Sstevel@tonic-gate */ 30800Sstevel@tonic-gate 30810Sstevel@tonic-gate if (samsg->sadb_msg_type == SADB_UPDATE) { 30820Sstevel@tonic-gate mutex_enter(&inbound->isaf_lock); 30830Sstevel@tonic-gate larval = ipsec_getassocbyspi(inbound, assoc->sadb_sa_spi, 30840Sstevel@tonic-gate ALL_ZEROES_PTR, dstaddr, dst->sin_family); 30850Sstevel@tonic-gate mutex_exit(&inbound->isaf_lock); 30860Sstevel@tonic-gate 30870Sstevel@tonic-gate if (larval == NULL) { 30880Sstevel@tonic-gate esp0dbg(("Larval update, but larval disappeared.\n")); 30890Sstevel@tonic-gate return (ESRCH); 30900Sstevel@tonic-gate } /* Else sadb_common_add unlinks it for me! */ 30910Sstevel@tonic-gate } 30920Sstevel@tonic-gate 30930Sstevel@tonic-gate lpkt = NULL; 30940Sstevel@tonic-gate if (larval != NULL) 30950Sstevel@tonic-gate lpkt = sadb_clear_lpkt(larval); 30960Sstevel@tonic-gate 3097*3448Sdh155122 rc = sadb_common_add(espstack->esp_sadb.s_ip_q, espstack->esp_pfkey_q, 3098*3448Sdh155122 mp, samsg, ksi, primary, secondary, larval, clone, is_inbound, 3099*3448Sdh155122 diagnostic, espstack->ipsecesp_netstack); 31000Sstevel@tonic-gate 31010Sstevel@tonic-gate if (rc == 0 && lpkt != NULL) { 31020Sstevel@tonic-gate rc = !taskq_dispatch(esp_taskq, inbound_task, 31030Sstevel@tonic-gate (void *) lpkt, TQ_NOSLEEP); 31040Sstevel@tonic-gate } 31050Sstevel@tonic-gate 31060Sstevel@tonic-gate if (rc != 0) { 31070Sstevel@tonic-gate ip_drop_packet(lpkt, B_TRUE, NULL, NULL, 3108*3448Sdh155122 DROPPER(ipss, ipds_sadb_inlarval_timeout), 3109*3448Sdh155122 &espstack->esp_dropper); 31100Sstevel@tonic-gate } 31110Sstevel@tonic-gate 31120Sstevel@tonic-gate /* 31130Sstevel@tonic-gate * How much more stack will I create with all of these 31140Sstevel@tonic-gate * esp_outbound() calls? 31150Sstevel@tonic-gate */ 31160Sstevel@tonic-gate 31170Sstevel@tonic-gate while (acq_msgs != NULL) { 31180Sstevel@tonic-gate mblk_t *mp = acq_msgs; 31190Sstevel@tonic-gate 31200Sstevel@tonic-gate acq_msgs = acq_msgs->b_next; 31210Sstevel@tonic-gate mp->b_next = NULL; 31220Sstevel@tonic-gate if (rc == 0) { 31230Sstevel@tonic-gate if (ipsec_outbound_sa(mp, IPPROTO_ESP)) { 31240Sstevel@tonic-gate ((ipsec_out_t *)(mp->b_rptr))-> 31250Sstevel@tonic-gate ipsec_out_esp_done = B_TRUE; 31260Sstevel@tonic-gate if (esp_outbound(mp) == IPSEC_STATUS_SUCCESS) { 31273055Sdanmcd ipha_t *ipha; 31280Sstevel@tonic-gate 31290Sstevel@tonic-gate /* do AH processing if needed */ 31300Sstevel@tonic-gate if (!esp_do_outbound_ah(mp)) 31310Sstevel@tonic-gate continue; 31320Sstevel@tonic-gate 31333055Sdanmcd ipha = (ipha_t *)mp->b_cont->b_rptr; 31343055Sdanmcd 31350Sstevel@tonic-gate /* finish IPsec processing */ 31360Sstevel@tonic-gate if (is_ipv4) { 31370Sstevel@tonic-gate ip_wput_ipsec_out(NULL, mp, 31380Sstevel@tonic-gate ipha, NULL, NULL); 31390Sstevel@tonic-gate } else { 31400Sstevel@tonic-gate ip6_t *ip6h = (ip6_t *)ipha; 31410Sstevel@tonic-gate ip_wput_ipsec_out_v6(NULL, 31420Sstevel@tonic-gate mp, ip6h, NULL, NULL); 31430Sstevel@tonic-gate } 31440Sstevel@tonic-gate } 31450Sstevel@tonic-gate continue; 31460Sstevel@tonic-gate } 31470Sstevel@tonic-gate } 3148*3448Sdh155122 ESP_BUMP_STAT(espstack, out_discards); 31490Sstevel@tonic-gate ip_drop_packet(mp, B_FALSE, NULL, NULL, 3150*3448Sdh155122 DROPPER(ipss, ipds_sadb_acquire_timeout), 3151*3448Sdh155122 &espstack->esp_dropper); 31520Sstevel@tonic-gate } 31530Sstevel@tonic-gate 31540Sstevel@tonic-gate return (rc); 31550Sstevel@tonic-gate } 31560Sstevel@tonic-gate 31570Sstevel@tonic-gate /* 31580Sstevel@tonic-gate * Add new ESP security association. This may become a generic AH/ESP 31590Sstevel@tonic-gate * routine eventually. 31600Sstevel@tonic-gate */ 31610Sstevel@tonic-gate static int 3162*3448Sdh155122 esp_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic, netstack_t *ns) 31630Sstevel@tonic-gate { 31640Sstevel@tonic-gate sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 31650Sstevel@tonic-gate sadb_address_t *srcext = 31660Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC]; 31670Sstevel@tonic-gate sadb_address_t *dstext = 31680Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 31693055Sdanmcd sadb_address_t *isrcext = 31703055Sdanmcd (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC]; 31713055Sdanmcd sadb_address_t *idstext = 31723055Sdanmcd (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_DST]; 31730Sstevel@tonic-gate sadb_address_t *nttext_loc = 31740Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_LOC]; 31750Sstevel@tonic-gate sadb_address_t *nttext_rem = 31760Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_REM]; 31770Sstevel@tonic-gate sadb_key_t *akey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH]; 31780Sstevel@tonic-gate sadb_key_t *ekey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT]; 31790Sstevel@tonic-gate struct sockaddr_in *src, *dst; 31800Sstevel@tonic-gate struct sockaddr_in *natt_loc, *natt_rem; 31810Sstevel@tonic-gate struct sockaddr_in6 *natt_loc6, *natt_rem6; 31820Sstevel@tonic-gate sadb_lifetime_t *soft = 31830Sstevel@tonic-gate (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT]; 31840Sstevel@tonic-gate sadb_lifetime_t *hard = 31850Sstevel@tonic-gate (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD]; 3186*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 3187*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 31880Sstevel@tonic-gate 31890Sstevel@tonic-gate /* I need certain extensions present for an ADD message. */ 31900Sstevel@tonic-gate if (srcext == NULL) { 31910Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC; 31920Sstevel@tonic-gate return (EINVAL); 31930Sstevel@tonic-gate } 31940Sstevel@tonic-gate if (dstext == NULL) { 31950Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST; 31960Sstevel@tonic-gate return (EINVAL); 31970Sstevel@tonic-gate } 31983055Sdanmcd if (isrcext == NULL && idstext != NULL) { 31993055Sdanmcd *diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC; 32003055Sdanmcd return (EINVAL); 32013055Sdanmcd } 32023055Sdanmcd if (isrcext != NULL && idstext == NULL) { 32033055Sdanmcd *diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST; 32043055Sdanmcd return (EINVAL); 32053055Sdanmcd } 32060Sstevel@tonic-gate if (assoc == NULL) { 32070Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA; 32080Sstevel@tonic-gate return (EINVAL); 32090Sstevel@tonic-gate } 32100Sstevel@tonic-gate if (ekey == NULL && assoc->sadb_sa_encrypt != SADB_EALG_NULL) { 32110Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_EKEY; 32120Sstevel@tonic-gate return (EINVAL); 32130Sstevel@tonic-gate } 32140Sstevel@tonic-gate 32150Sstevel@tonic-gate src = (struct sockaddr_in *)(srcext + 1); 32160Sstevel@tonic-gate dst = (struct sockaddr_in *)(dstext + 1); 32170Sstevel@tonic-gate natt_loc = (struct sockaddr_in *)(nttext_loc + 1); 32180Sstevel@tonic-gate natt_loc6 = (struct sockaddr_in6 *)(nttext_loc + 1); 32190Sstevel@tonic-gate natt_rem = (struct sockaddr_in *)(nttext_rem + 1); 32200Sstevel@tonic-gate natt_rem6 = (struct sockaddr_in6 *)(nttext_rem + 1); 32210Sstevel@tonic-gate 32220Sstevel@tonic-gate /* Sundry ADD-specific reality checks. */ 32230Sstevel@tonic-gate /* XXX STATS : Logging/stats here? */ 32240Sstevel@tonic-gate if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) { 32250Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE; 32260Sstevel@tonic-gate return (EINVAL); 32270Sstevel@tonic-gate } 32280Sstevel@tonic-gate if (assoc->sadb_sa_encrypt == SADB_EALG_NONE) { 32290Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG; 32300Sstevel@tonic-gate return (EINVAL); 32310Sstevel@tonic-gate } 32320Sstevel@tonic-gate 32330Sstevel@tonic-gate if (assoc->sadb_sa_encrypt == SADB_EALG_NULL && 32340Sstevel@tonic-gate assoc->sadb_sa_auth == SADB_AALG_NONE) { 32350Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG; 32360Sstevel@tonic-gate return (EINVAL); 32370Sstevel@tonic-gate } 32380Sstevel@tonic-gate 32390Sstevel@tonic-gate if (assoc->sadb_sa_flags & ~(SADB_SAFLAGS_NOREPLAY | 32403055Sdanmcd SADB_X_SAFLAGS_NATT_LOC | SADB_X_SAFLAGS_NATT_REM | 32413055Sdanmcd SADB_X_SAFLAGS_TUNNEL)) { 32420Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS; 32430Sstevel@tonic-gate return (EINVAL); 32440Sstevel@tonic-gate } 32450Sstevel@tonic-gate 32460Sstevel@tonic-gate if ((*diagnostic = sadb_hardsoftchk(hard, soft)) != 0) { 32470Sstevel@tonic-gate return (EINVAL); 32480Sstevel@tonic-gate } 32493055Sdanmcd ASSERT(src->sin_family == dst->sin_family); 32500Sstevel@tonic-gate 32510Sstevel@tonic-gate if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC) { 32520Sstevel@tonic-gate if (nttext_loc == NULL) { 32530Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_LOC; 32540Sstevel@tonic-gate return (EINVAL); 32550Sstevel@tonic-gate } 32560Sstevel@tonic-gate 32570Sstevel@tonic-gate if (natt_loc->sin_family == AF_INET6 && 32580Sstevel@tonic-gate !IN6_IS_ADDR_V4MAPPED(&natt_loc6->sin6_addr)) { 32590Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC; 32600Sstevel@tonic-gate return (EINVAL); 32610Sstevel@tonic-gate } 32620Sstevel@tonic-gate } 32630Sstevel@tonic-gate 32640Sstevel@tonic-gate if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM) { 32650Sstevel@tonic-gate if (nttext_rem == NULL) { 32660Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_REM; 32670Sstevel@tonic-gate return (EINVAL); 32680Sstevel@tonic-gate } 32690Sstevel@tonic-gate if (natt_rem->sin_family == AF_INET6 && 32700Sstevel@tonic-gate !IN6_IS_ADDR_V4MAPPED(&natt_rem6->sin6_addr)) { 32710Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM; 32720Sstevel@tonic-gate return (EINVAL); 32730Sstevel@tonic-gate } 32740Sstevel@tonic-gate } 32750Sstevel@tonic-gate 32760Sstevel@tonic-gate 32770Sstevel@tonic-gate /* Stuff I don't support, for now. XXX Diagnostic? */ 32780Sstevel@tonic-gate if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL || 32790Sstevel@tonic-gate ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL) 32800Sstevel@tonic-gate return (EOPNOTSUPP); 32810Sstevel@tonic-gate 32820Sstevel@tonic-gate /* 32830Sstevel@tonic-gate * XXX Policy : I'm not checking identities or sensitivity 32840Sstevel@tonic-gate * labels at this time, but if I did, I'd do them here, before I sent 32850Sstevel@tonic-gate * the weak key check up to the algorithm. 32860Sstevel@tonic-gate */ 32870Sstevel@tonic-gate 3288*3448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 32890Sstevel@tonic-gate 32900Sstevel@tonic-gate /* 32910Sstevel@tonic-gate * First locate the authentication algorithm. 32920Sstevel@tonic-gate */ 32930Sstevel@tonic-gate if (akey != NULL) { 32940Sstevel@tonic-gate ipsec_alginfo_t *aalg; 32950Sstevel@tonic-gate 3296*3448Sdh155122 aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH] 3297*3448Sdh155122 [assoc->sadb_sa_auth]; 32980Sstevel@tonic-gate if (aalg == NULL || !ALG_VALID(aalg)) { 3299*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 3300*3448Sdh155122 esp1dbg(espstack, ("Couldn't find auth alg #%d.\n", 33010Sstevel@tonic-gate assoc->sadb_sa_auth)); 33020Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG; 33030Sstevel@tonic-gate return (EINVAL); 33040Sstevel@tonic-gate } 33052810Smarkfen 33062810Smarkfen /* 33072810Smarkfen * Sanity check key sizes. 33082810Smarkfen * Note: It's not possible to use SADB_AALG_NONE because 33092810Smarkfen * this auth_alg is not defined with ALG_FLAG_VALID. If this 33102810Smarkfen * ever changes, the same check for SADB_AALG_NONE and 33112810Smarkfen * a auth_key != NULL should be made here ( see below). 33122810Smarkfen */ 33130Sstevel@tonic-gate if (!ipsec_valid_key_size(akey->sadb_key_bits, aalg)) { 3314*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 33150Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS; 33160Sstevel@tonic-gate return (EINVAL); 33170Sstevel@tonic-gate } 33182810Smarkfen ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID); 33190Sstevel@tonic-gate 33200Sstevel@tonic-gate /* check key and fix parity if needed */ 33210Sstevel@tonic-gate if (ipsec_check_key(aalg->alg_mech_type, akey, B_TRUE, 33220Sstevel@tonic-gate diagnostic) != 0) { 3323*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 33240Sstevel@tonic-gate return (EINVAL); 33250Sstevel@tonic-gate } 33260Sstevel@tonic-gate } 33270Sstevel@tonic-gate 33280Sstevel@tonic-gate /* 33290Sstevel@tonic-gate * Then locate the encryption algorithm. 33300Sstevel@tonic-gate */ 33310Sstevel@tonic-gate if (ekey != NULL) { 33320Sstevel@tonic-gate ipsec_alginfo_t *ealg; 33330Sstevel@tonic-gate 3334*3448Sdh155122 ealg = ipss->ipsec_alglists[IPSEC_ALG_ENCR] 3335*3448Sdh155122 [assoc->sadb_sa_encrypt]; 33360Sstevel@tonic-gate if (ealg == NULL || !ALG_VALID(ealg)) { 3337*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 3338*3448Sdh155122 esp1dbg(espstack, ("Couldn't find encr alg #%d.\n", 33390Sstevel@tonic-gate assoc->sadb_sa_encrypt)); 33400Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG; 33410Sstevel@tonic-gate return (EINVAL); 33420Sstevel@tonic-gate } 33432810Smarkfen 33442810Smarkfen /* 33452810Smarkfen * Sanity check key sizes. If the encryption algorithm is 33462810Smarkfen * SADB_EALG_NULL but the encryption key is NOT 33472810Smarkfen * NULL then complain. 33482810Smarkfen */ 33492810Smarkfen if ((assoc->sadb_sa_encrypt == SADB_EALG_NULL) || 33502810Smarkfen (!ipsec_valid_key_size(ekey->sadb_key_bits, ealg))) { 3351*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 33520Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_EKEYBITS; 33530Sstevel@tonic-gate return (EINVAL); 33540Sstevel@tonic-gate } 33552810Smarkfen ASSERT(ealg->alg_mech_type != CRYPTO_MECHANISM_INVALID); 33560Sstevel@tonic-gate 33570Sstevel@tonic-gate /* check key */ 33580Sstevel@tonic-gate if (ipsec_check_key(ealg->alg_mech_type, ekey, B_FALSE, 33590Sstevel@tonic-gate diagnostic) != 0) { 3360*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 33610Sstevel@tonic-gate return (EINVAL); 33620Sstevel@tonic-gate } 33630Sstevel@tonic-gate } 3364*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 33650Sstevel@tonic-gate 33663055Sdanmcd return (esp_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi, 3367*3448Sdh155122 diagnostic, espstack)); 33680Sstevel@tonic-gate } 33690Sstevel@tonic-gate 33700Sstevel@tonic-gate /* 33710Sstevel@tonic-gate * Update a security association. Updates come in two varieties. The first 33720Sstevel@tonic-gate * is an update of lifetimes on a non-larval SA. The second is an update of 33730Sstevel@tonic-gate * a larval SA, which ends up looking a lot more like an add. 33740Sstevel@tonic-gate */ 33750Sstevel@tonic-gate static int 3376*3448Sdh155122 esp_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic, 3377*3448Sdh155122 ipsecesp_stack_t *espstack) 33780Sstevel@tonic-gate { 33790Sstevel@tonic-gate sadb_address_t *dstext = 33800Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 33810Sstevel@tonic-gate struct sockaddr_in *sin; 33820Sstevel@tonic-gate 33830Sstevel@tonic-gate if (dstext == NULL) { 33840Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST; 33850Sstevel@tonic-gate return (EINVAL); 33860Sstevel@tonic-gate } 33870Sstevel@tonic-gate 33880Sstevel@tonic-gate sin = (struct sockaddr_in *)(dstext + 1); 33890Sstevel@tonic-gate return (sadb_update_sa(mp, ksi, 3390*3448Sdh155122 (sin->sin_family == AF_INET6) ? &espstack->esp_sadb.s_v6 : 3391*3448Sdh155122 &espstack->esp_sadb.s_v4, 3392*3448Sdh155122 diagnostic, espstack->esp_pfkey_q, esp_add_sa, 3393*3448Sdh155122 espstack->ipsecesp_netstack)); 33940Sstevel@tonic-gate } 33950Sstevel@tonic-gate 33960Sstevel@tonic-gate /* 33970Sstevel@tonic-gate * Delete a security association. This is REALLY likely to be code common to 33980Sstevel@tonic-gate * both AH and ESP. Find the association, then unlink it. 33990Sstevel@tonic-gate */ 34000Sstevel@tonic-gate static int 3401*3448Sdh155122 esp_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic, 3402*3448Sdh155122 ipsecesp_stack_t *espstack) 34030Sstevel@tonic-gate { 34040Sstevel@tonic-gate sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 34050Sstevel@tonic-gate sadb_address_t *dstext = 34060Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 34070Sstevel@tonic-gate sadb_address_t *srcext = 34080Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC]; 34090Sstevel@tonic-gate struct sockaddr_in *sin; 34100Sstevel@tonic-gate 34110Sstevel@tonic-gate if (assoc == NULL) { 34120Sstevel@tonic-gate if (dstext != NULL) { 34130Sstevel@tonic-gate sin = (struct sockaddr_in *)(dstext + 1); 34140Sstevel@tonic-gate } else if (srcext != NULL) { 34150Sstevel@tonic-gate sin = (struct sockaddr_in *)(srcext + 1); 34160Sstevel@tonic-gate } else { 34170Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA; 34180Sstevel@tonic-gate return (EINVAL); 34190Sstevel@tonic-gate } 34203055Sdanmcd return (sadb_purge_sa(mp, ksi, 3421*3448Sdh155122 (sin->sin_family == AF_INET6) ? &espstack->esp_sadb.s_v6 : 3422*3448Sdh155122 &espstack->esp_sadb.s_v4, espstack->esp_pfkey_q, 3423*3448Sdh155122 espstack->esp_sadb.s_ip_q)); 34240Sstevel@tonic-gate } 34250Sstevel@tonic-gate 3426*3448Sdh155122 return (sadb_del_sa(mp, ksi, &espstack->esp_sadb, diagnostic, 3427*3448Sdh155122 espstack->esp_pfkey_q)); 34280Sstevel@tonic-gate } 34290Sstevel@tonic-gate 34300Sstevel@tonic-gate /* 34310Sstevel@tonic-gate * Convert the entire contents of all of ESP's SA tables into PF_KEY SADB_DUMP 34320Sstevel@tonic-gate * messages. 34330Sstevel@tonic-gate */ 34340Sstevel@tonic-gate static void 3435*3448Sdh155122 esp_dump(mblk_t *mp, keysock_in_t *ksi, ipsecesp_stack_t *espstack) 34360Sstevel@tonic-gate { 34370Sstevel@tonic-gate int error; 34380Sstevel@tonic-gate sadb_msg_t *samsg; 34390Sstevel@tonic-gate 34400Sstevel@tonic-gate /* 34410Sstevel@tonic-gate * Dump each fanout, bailing if error is non-zero. 34420Sstevel@tonic-gate */ 34430Sstevel@tonic-gate 3444*3448Sdh155122 error = sadb_dump(espstack->esp_pfkey_q, mp, ksi->ks_in_serial, 3445*3448Sdh155122 &espstack->esp_sadb.s_v4); 34460Sstevel@tonic-gate if (error != 0) 34470Sstevel@tonic-gate goto bail; 34480Sstevel@tonic-gate 3449*3448Sdh155122 error = sadb_dump(espstack->esp_pfkey_q, mp, ksi->ks_in_serial, 3450*3448Sdh155122 &espstack->esp_sadb.s_v6); 34510Sstevel@tonic-gate bail: 34520Sstevel@tonic-gate ASSERT(mp->b_cont != NULL); 34530Sstevel@tonic-gate samsg = (sadb_msg_t *)mp->b_cont->b_rptr; 34540Sstevel@tonic-gate samsg->sadb_msg_errno = (uint8_t)error; 3455*3448Sdh155122 sadb_pfkey_echo(espstack->esp_pfkey_q, mp, 3456*3448Sdh155122 (sadb_msg_t *)mp->b_cont->b_rptr, ksi, NULL); 34570Sstevel@tonic-gate } 34580Sstevel@tonic-gate 34590Sstevel@tonic-gate /* 34603055Sdanmcd * First-cut reality check for an inbound PF_KEY message. 34613055Sdanmcd */ 34623055Sdanmcd static boolean_t 3463*3448Sdh155122 esp_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi, 3464*3448Sdh155122 ipsecesp_stack_t *espstack) 34653055Sdanmcd { 34663055Sdanmcd int diagnostic; 34673055Sdanmcd 34683055Sdanmcd if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) { 34693055Sdanmcd diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT; 34703055Sdanmcd goto badmsg; 34713055Sdanmcd } 34723055Sdanmcd if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL || 34733055Sdanmcd ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) { 34743055Sdanmcd diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT; 34753055Sdanmcd goto badmsg; 34763055Sdanmcd } 34773055Sdanmcd return (B_FALSE); /* False ==> no failures */ 34783055Sdanmcd 34793055Sdanmcd badmsg: 3480*3448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, EINVAL, diagnostic, 34813055Sdanmcd ksi->ks_in_serial); 34823055Sdanmcd return (B_TRUE); /* True ==> failures */ 34833055Sdanmcd } 34843055Sdanmcd 34853055Sdanmcd /* 34860Sstevel@tonic-gate * ESP parsing of PF_KEY messages. Keysock did most of the really silly 34870Sstevel@tonic-gate * error cases. What I receive is a fully-formed, syntactically legal 34880Sstevel@tonic-gate * PF_KEY message. I then need to check semantics... 34890Sstevel@tonic-gate * 34900Sstevel@tonic-gate * This code may become common to AH and ESP. Stay tuned. 34910Sstevel@tonic-gate * 34920Sstevel@tonic-gate * I also make the assumption that db_ref's are cool. If this assumption 34930Sstevel@tonic-gate * is wrong, this means that someone other than keysock or me has been 34940Sstevel@tonic-gate * mucking with PF_KEY messages. 34950Sstevel@tonic-gate */ 34960Sstevel@tonic-gate static void 3497*3448Sdh155122 esp_parse_pfkey(mblk_t *mp, ipsecesp_stack_t *espstack) 34980Sstevel@tonic-gate { 34990Sstevel@tonic-gate mblk_t *msg = mp->b_cont; 35000Sstevel@tonic-gate sadb_msg_t *samsg; 35010Sstevel@tonic-gate keysock_in_t *ksi; 35020Sstevel@tonic-gate int error; 35030Sstevel@tonic-gate int diagnostic = SADB_X_DIAGNOSTIC_NONE; 35040Sstevel@tonic-gate 35050Sstevel@tonic-gate ASSERT(msg != NULL); 3506*3448Sdh155122 35070Sstevel@tonic-gate samsg = (sadb_msg_t *)msg->b_rptr; 35080Sstevel@tonic-gate ksi = (keysock_in_t *)mp->b_rptr; 35090Sstevel@tonic-gate 35100Sstevel@tonic-gate /* 35110Sstevel@tonic-gate * If applicable, convert unspecified AF_INET6 to unspecified 35123055Sdanmcd * AF_INET. And do other address reality checks. 35130Sstevel@tonic-gate */ 3514*3448Sdh155122 if (!sadb_addrfix(ksi, espstack->esp_pfkey_q, mp, 3515*3448Sdh155122 espstack->ipsecesp_netstack) || 3516*3448Sdh155122 esp_pfkey_reality_failures(mp, ksi, espstack)) { 35173055Sdanmcd return; 35183055Sdanmcd } 35190Sstevel@tonic-gate 35200Sstevel@tonic-gate switch (samsg->sadb_msg_type) { 35210Sstevel@tonic-gate case SADB_ADD: 3522*3448Sdh155122 error = esp_add_sa(mp, ksi, &diagnostic, 3523*3448Sdh155122 espstack->ipsecesp_netstack); 35240Sstevel@tonic-gate if (error != 0) { 3525*3448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, error, 3526*3448Sdh155122 diagnostic, ksi->ks_in_serial); 35270Sstevel@tonic-gate } 35280Sstevel@tonic-gate /* else esp_add_sa() took care of things. */ 35290Sstevel@tonic-gate break; 35300Sstevel@tonic-gate case SADB_DELETE: 3531*3448Sdh155122 error = esp_del_sa(mp, ksi, &diagnostic, espstack); 35320Sstevel@tonic-gate if (error != 0) { 3533*3448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, error, 3534*3448Sdh155122 diagnostic, ksi->ks_in_serial); 35350Sstevel@tonic-gate } 35360Sstevel@tonic-gate /* Else esp_del_sa() took care of things. */ 35370Sstevel@tonic-gate break; 35380Sstevel@tonic-gate case SADB_GET: 3539*3448Sdh155122 error = sadb_get_sa(mp, ksi, &espstack->esp_sadb, &diagnostic, 3540*3448Sdh155122 espstack->esp_pfkey_q); 35410Sstevel@tonic-gate if (error != 0) { 3542*3448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, error, 3543*3448Sdh155122 diagnostic, ksi->ks_in_serial); 35440Sstevel@tonic-gate } 35450Sstevel@tonic-gate /* Else sadb_get_sa() took care of things. */ 35460Sstevel@tonic-gate break; 35470Sstevel@tonic-gate case SADB_FLUSH: 3548*3448Sdh155122 sadbp_flush(&espstack->esp_sadb, espstack->ipsecesp_netstack); 3549*3448Sdh155122 sadb_pfkey_echo(espstack->esp_pfkey_q, mp, samsg, ksi, NULL); 35500Sstevel@tonic-gate break; 35510Sstevel@tonic-gate case SADB_REGISTER: 35520Sstevel@tonic-gate /* 35530Sstevel@tonic-gate * Hmmm, let's do it! Check for extensions (there should 35540Sstevel@tonic-gate * be none), extract the fields, call esp_register_out(), 35550Sstevel@tonic-gate * then either free or report an error. 35560Sstevel@tonic-gate * 35570Sstevel@tonic-gate * Keysock takes care of the PF_KEY bookkeeping for this. 35580Sstevel@tonic-gate */ 35590Sstevel@tonic-gate if (esp_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid, 3560*3448Sdh155122 ksi->ks_in_serial, espstack)) { 35610Sstevel@tonic-gate freemsg(mp); 35620Sstevel@tonic-gate } else { 35630Sstevel@tonic-gate /* 35640Sstevel@tonic-gate * Only way this path hits is if there is a memory 35650Sstevel@tonic-gate * failure. It will not return B_FALSE because of 35660Sstevel@tonic-gate * lack of esp_pfkey_q if I am in wput(). 35670Sstevel@tonic-gate */ 3568*3448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, ENOMEM, 3569*3448Sdh155122 diagnostic, ksi->ks_in_serial); 35700Sstevel@tonic-gate } 35710Sstevel@tonic-gate break; 35720Sstevel@tonic-gate case SADB_UPDATE: 35730Sstevel@tonic-gate /* 35740Sstevel@tonic-gate * Find a larval, if not there, find a full one and get 35750Sstevel@tonic-gate * strict. 35760Sstevel@tonic-gate */ 3577*3448Sdh155122 error = esp_update_sa(mp, ksi, &diagnostic, espstack); 35780Sstevel@tonic-gate if (error != 0) { 3579*3448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, error, 3580*3448Sdh155122 diagnostic, ksi->ks_in_serial); 35810Sstevel@tonic-gate } 35820Sstevel@tonic-gate /* else esp_update_sa() took care of things. */ 35830Sstevel@tonic-gate break; 35840Sstevel@tonic-gate case SADB_GETSPI: 35850Sstevel@tonic-gate /* 35860Sstevel@tonic-gate * Reserve a new larval entry. 35870Sstevel@tonic-gate */ 3588*3448Sdh155122 esp_getspi(mp, ksi, espstack); 35890Sstevel@tonic-gate break; 35900Sstevel@tonic-gate case SADB_ACQUIRE: 35910Sstevel@tonic-gate /* 35920Sstevel@tonic-gate * Find larval and/or ACQUIRE record and kill it (them), I'm 35930Sstevel@tonic-gate * most likely an error. Inbound ACQUIRE messages should only 35940Sstevel@tonic-gate * have the base header. 35950Sstevel@tonic-gate */ 3596*3448Sdh155122 sadb_in_acquire(samsg, &espstack->esp_sadb, 3597*3448Sdh155122 espstack->esp_pfkey_q, espstack->ipsecesp_netstack); 35980Sstevel@tonic-gate freemsg(mp); 35990Sstevel@tonic-gate break; 36000Sstevel@tonic-gate case SADB_DUMP: 36010Sstevel@tonic-gate /* 36020Sstevel@tonic-gate * Dump all entries. 36030Sstevel@tonic-gate */ 3604*3448Sdh155122 esp_dump(mp, ksi, espstack); 36050Sstevel@tonic-gate /* esp_dump will take care of the return message, etc. */ 36060Sstevel@tonic-gate break; 36070Sstevel@tonic-gate case SADB_EXPIRE: 36080Sstevel@tonic-gate /* Should never reach me. */ 3609*3448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, EOPNOTSUPP, 3610*3448Sdh155122 diagnostic, ksi->ks_in_serial); 36110Sstevel@tonic-gate break; 36120Sstevel@tonic-gate default: 3613*3448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, EINVAL, 36140Sstevel@tonic-gate SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial); 36150Sstevel@tonic-gate break; 36160Sstevel@tonic-gate } 36170Sstevel@tonic-gate } 36180Sstevel@tonic-gate 36190Sstevel@tonic-gate /* 36200Sstevel@tonic-gate * Handle case where PF_KEY says it can't find a keysock for one of my 36210Sstevel@tonic-gate * ACQUIRE messages. 36220Sstevel@tonic-gate */ 36230Sstevel@tonic-gate static void 3624*3448Sdh155122 esp_keysock_no_socket(mblk_t *mp, ipsecesp_stack_t *espstack) 36250Sstevel@tonic-gate { 36260Sstevel@tonic-gate sadb_msg_t *samsg; 36270Sstevel@tonic-gate keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr; 36280Sstevel@tonic-gate 36290Sstevel@tonic-gate if (mp->b_cont == NULL) { 36300Sstevel@tonic-gate freemsg(mp); 36310Sstevel@tonic-gate return; 36320Sstevel@tonic-gate } 36330Sstevel@tonic-gate samsg = (sadb_msg_t *)mp->b_cont->b_rptr; 36340Sstevel@tonic-gate 36350Sstevel@tonic-gate /* 36360Sstevel@tonic-gate * If keysock can't find any registered, delete the acquire record 36370Sstevel@tonic-gate * immediately, and handle errors. 36380Sstevel@tonic-gate */ 36390Sstevel@tonic-gate if (samsg->sadb_msg_type == SADB_ACQUIRE) { 36400Sstevel@tonic-gate samsg->sadb_msg_errno = kse->ks_err_errno; 36410Sstevel@tonic-gate samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg)); 36420Sstevel@tonic-gate /* 36430Sstevel@tonic-gate * Use the write-side of the esp_pfkey_q, in case there is 36440Sstevel@tonic-gate * no esp_sadb.s_ip_q. 36450Sstevel@tonic-gate */ 3646*3448Sdh155122 sadb_in_acquire(samsg, &espstack->esp_sadb, 3647*3448Sdh155122 WR(espstack->esp_pfkey_q), espstack->ipsecesp_netstack); 36480Sstevel@tonic-gate } 36490Sstevel@tonic-gate 36500Sstevel@tonic-gate freemsg(mp); 36510Sstevel@tonic-gate } 36520Sstevel@tonic-gate 36530Sstevel@tonic-gate /* 36540Sstevel@tonic-gate * ESP module write put routine. 36550Sstevel@tonic-gate */ 36560Sstevel@tonic-gate static void 36570Sstevel@tonic-gate ipsecesp_wput(queue_t *q, mblk_t *mp) 36580Sstevel@tonic-gate { 36590Sstevel@tonic-gate ipsec_info_t *ii; 36600Sstevel@tonic-gate struct iocblk *iocp; 3661*3448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)q->q_ptr; 3662*3448Sdh155122 3663*3448Sdh155122 esp3dbg(espstack, ("In esp_wput().\n")); 36640Sstevel@tonic-gate 36650Sstevel@tonic-gate /* NOTE: Each case must take care of freeing or passing mp. */ 36660Sstevel@tonic-gate switch (mp->b_datap->db_type) { 36670Sstevel@tonic-gate case M_CTL: 36680Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) { 36690Sstevel@tonic-gate /* Not big enough message. */ 36700Sstevel@tonic-gate freemsg(mp); 36710Sstevel@tonic-gate break; 36720Sstevel@tonic-gate } 36730Sstevel@tonic-gate ii = (ipsec_info_t *)mp->b_rptr; 36740Sstevel@tonic-gate 36750Sstevel@tonic-gate switch (ii->ipsec_info_type) { 36760Sstevel@tonic-gate case KEYSOCK_OUT_ERR: 3677*3448Sdh155122 esp1dbg(espstack, ("Got KEYSOCK_OUT_ERR message.\n")); 3678*3448Sdh155122 esp_keysock_no_socket(mp, espstack); 36790Sstevel@tonic-gate break; 36800Sstevel@tonic-gate case KEYSOCK_IN: 3681*3448Sdh155122 ESP_BUMP_STAT(espstack, keysock_in); 3682*3448Sdh155122 esp3dbg(espstack, ("Got KEYSOCK_IN message.\n")); 36833055Sdanmcd 36843055Sdanmcd /* Parse the message. */ 3685*3448Sdh155122 esp_parse_pfkey(mp, espstack); 36860Sstevel@tonic-gate break; 36870Sstevel@tonic-gate case KEYSOCK_HELLO: 3688*3448Sdh155122 sadb_keysock_hello(&espstack->esp_pfkey_q, q, mp, 3689*3448Sdh155122 esp_ager, (void *)espstack, &espstack->esp_event, 3690*3448Sdh155122 SADB_SATYPE_ESP); 36910Sstevel@tonic-gate break; 36920Sstevel@tonic-gate default: 3693*3448Sdh155122 esp2dbg(espstack, ("Got M_CTL from above of 0x%x.\n", 36940Sstevel@tonic-gate ii->ipsec_info_type)); 36950Sstevel@tonic-gate freemsg(mp); 36960Sstevel@tonic-gate break; 36970Sstevel@tonic-gate } 36980Sstevel@tonic-gate break; 36990Sstevel@tonic-gate case M_IOCTL: 37000Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 37010Sstevel@tonic-gate switch (iocp->ioc_cmd) { 37020Sstevel@tonic-gate case ND_SET: 37030Sstevel@tonic-gate case ND_GET: 3704*3448Sdh155122 if (nd_getset(q, espstack->ipsecesp_g_nd, mp)) { 37050Sstevel@tonic-gate qreply(q, mp); 37060Sstevel@tonic-gate return; 37070Sstevel@tonic-gate } else { 37080Sstevel@tonic-gate iocp->ioc_error = ENOENT; 37090Sstevel@tonic-gate } 37100Sstevel@tonic-gate /* FALLTHRU */ 37110Sstevel@tonic-gate default: 37120Sstevel@tonic-gate /* We really don't support any other ioctls, do we? */ 37130Sstevel@tonic-gate 37140Sstevel@tonic-gate /* Return EINVAL */ 37150Sstevel@tonic-gate if (iocp->ioc_error != ENOENT) 37160Sstevel@tonic-gate iocp->ioc_error = EINVAL; 37170Sstevel@tonic-gate iocp->ioc_count = 0; 37180Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 37190Sstevel@tonic-gate qreply(q, mp); 37200Sstevel@tonic-gate return; 37210Sstevel@tonic-gate } 37220Sstevel@tonic-gate default: 3723*3448Sdh155122 esp3dbg(espstack, 3724*3448Sdh155122 ("Got default message, type %d, passing to IP.\n", 37250Sstevel@tonic-gate mp->b_datap->db_type)); 37260Sstevel@tonic-gate putnext(q, mp); 37270Sstevel@tonic-gate } 37280Sstevel@tonic-gate } 37290Sstevel@tonic-gate 37300Sstevel@tonic-gate /* 37310Sstevel@tonic-gate * Process an outbound ESP packet that can be accelerated by a IPsec 37320Sstevel@tonic-gate * hardware acceleration capable Provider. 37330Sstevel@tonic-gate * The caller already inserted and initialized the ESP header. 37340Sstevel@tonic-gate * This function allocates a tagging M_CTL, and adds room at the end 37350Sstevel@tonic-gate * of the packet to hold the ICV if authentication is needed. 37360Sstevel@tonic-gate * 37370Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the 37380Sstevel@tonic-gate * mblk chain ipsec_out. 37390Sstevel@tonic-gate */ 37400Sstevel@tonic-gate static ipsec_status_t 37410Sstevel@tonic-gate esp_outbound_accelerated(mblk_t *ipsec_out, uint_t icv_len) 37420Sstevel@tonic-gate { 37430Sstevel@tonic-gate ipsec_out_t *io; 37440Sstevel@tonic-gate mblk_t *lastmp; 3745*3448Sdh155122 netstack_t *ns; 3746*3448Sdh155122 ipsecesp_stack_t *espstack; 3747*3448Sdh155122 ipsec_stack_t *ipss; 37480Sstevel@tonic-gate 37490Sstevel@tonic-gate io = (ipsec_out_t *)ipsec_out->b_rptr; 3750*3448Sdh155122 ns = io->ipsec_out_ns; 3751*3448Sdh155122 espstack = ns->netstack_ipsecesp; 3752*3448Sdh155122 ipss = ns->netstack_ipsec; 3753*3448Sdh155122 3754*3448Sdh155122 ESP_BUMP_STAT(espstack, out_accelerated); 37550Sstevel@tonic-gate 37560Sstevel@tonic-gate /* mark packet as being accelerated in IPSEC_OUT */ 37570Sstevel@tonic-gate ASSERT(io->ipsec_out_accelerated == B_FALSE); 37580Sstevel@tonic-gate io->ipsec_out_accelerated = B_TRUE; 37590Sstevel@tonic-gate 37600Sstevel@tonic-gate /* 37610Sstevel@tonic-gate * add room at the end of the packet for the ICV if needed 37620Sstevel@tonic-gate */ 37630Sstevel@tonic-gate if (icv_len > 0) { 37640Sstevel@tonic-gate /* go to last mblk */ 37650Sstevel@tonic-gate lastmp = ipsec_out; /* For following while loop. */ 37660Sstevel@tonic-gate do { 37670Sstevel@tonic-gate lastmp = lastmp->b_cont; 37680Sstevel@tonic-gate } while (lastmp->b_cont != NULL); 37690Sstevel@tonic-gate 37700Sstevel@tonic-gate /* if not enough available room, allocate new mblk */ 37710Sstevel@tonic-gate if ((lastmp->b_wptr + icv_len) > lastmp->b_datap->db_lim) { 37720Sstevel@tonic-gate lastmp->b_cont = allocb(icv_len, BPRI_HI); 37730Sstevel@tonic-gate if (lastmp->b_cont == NULL) { 3774*3448Sdh155122 ESP_BUMP_STAT(espstack, out_discards); 37750Sstevel@tonic-gate ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL, 3776*3448Sdh155122 DROPPER(ipss, ipds_esp_nomem), 3777*3448Sdh155122 &espstack->esp_dropper); 37780Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 37790Sstevel@tonic-gate } 37800Sstevel@tonic-gate lastmp = lastmp->b_cont; 37810Sstevel@tonic-gate } 37820Sstevel@tonic-gate lastmp->b_wptr += icv_len; 37830Sstevel@tonic-gate } 37840Sstevel@tonic-gate 37850Sstevel@tonic-gate return (IPSEC_STATUS_SUCCESS); 37860Sstevel@tonic-gate } 37870Sstevel@tonic-gate 37880Sstevel@tonic-gate /* 37890Sstevel@tonic-gate * Process an inbound accelerated ESP packet. 37900Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the 37910Sstevel@tonic-gate * mblk chain ipsec_in. 37920Sstevel@tonic-gate */ 37930Sstevel@tonic-gate static ipsec_status_t 37940Sstevel@tonic-gate esp_inbound_accelerated(mblk_t *ipsec_in, mblk_t *data_mp, boolean_t isv4, 37950Sstevel@tonic-gate ipsa_t *assoc) 37960Sstevel@tonic-gate { 3797*3448Sdh155122 ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr; 37980Sstevel@tonic-gate mblk_t *hada_mp; 37990Sstevel@tonic-gate uint32_t icv_len = 0; 38000Sstevel@tonic-gate da_ipsec_t *hada; 38010Sstevel@tonic-gate ipha_t *ipha; 38020Sstevel@tonic-gate ip6_t *ip6h; 38030Sstevel@tonic-gate kstat_named_t *counter; 3804*3448Sdh155122 netstack_t *ns = ii->ipsec_in_ns; 3805*3448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 3806*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 3807*3448Sdh155122 3808*3448Sdh155122 ESP_BUMP_STAT(espstack, in_accelerated); 3809*3448Sdh155122 38100Sstevel@tonic-gate hada_mp = ii->ipsec_in_da; 38110Sstevel@tonic-gate ASSERT(hada_mp != NULL); 38120Sstevel@tonic-gate hada = (da_ipsec_t *)hada_mp->b_rptr; 38130Sstevel@tonic-gate 38140Sstevel@tonic-gate /* 38150Sstevel@tonic-gate * We only support one level of decapsulation in hardware, so 38160Sstevel@tonic-gate * nuke the pointer. 38170Sstevel@tonic-gate */ 38180Sstevel@tonic-gate ii->ipsec_in_da = NULL; 38190Sstevel@tonic-gate ii->ipsec_in_accelerated = B_FALSE; 38200Sstevel@tonic-gate 38210Sstevel@tonic-gate if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) { 38220Sstevel@tonic-gate /* 38230Sstevel@tonic-gate * ESP with authentication. We expect the Provider to have 38240Sstevel@tonic-gate * computed the ICV and placed it in the hardware acceleration 38250Sstevel@tonic-gate * data attributes. 38260Sstevel@tonic-gate * 38270Sstevel@tonic-gate * Extract ICV length from attributes M_CTL and sanity check 38280Sstevel@tonic-gate * its value. We allow the mblk to be smaller than da_ipsec_t 38290Sstevel@tonic-gate * for a small ICV, as long as the entire ICV fits within the 38300Sstevel@tonic-gate * mblk. 38310Sstevel@tonic-gate * 38320Sstevel@tonic-gate * Also ensures that the ICV length computed by Provider 38330Sstevel@tonic-gate * corresponds to the ICV length of the agorithm specified by 38340Sstevel@tonic-gate * the SA. 38350Sstevel@tonic-gate */ 38360Sstevel@tonic-gate icv_len = hada->da_icv_len; 38370Sstevel@tonic-gate if ((icv_len != assoc->ipsa_mac_len) || 38380Sstevel@tonic-gate (icv_len > DA_ICV_MAX_LEN) || (MBLKL(hada_mp) < 38390Sstevel@tonic-gate (sizeof (da_ipsec_t) - DA_ICV_MAX_LEN + icv_len))) { 38400Sstevel@tonic-gate esp0dbg(("esp_inbound_accelerated: " 38410Sstevel@tonic-gate "ICV len (%u) incorrect or mblk too small (%u)\n", 38420Sstevel@tonic-gate icv_len, (uint32_t)(MBLKL(hada_mp)))); 3843*3448Sdh155122 counter = DROPPER(ipss, ipds_esp_bad_auth); 38440Sstevel@tonic-gate goto esp_in_discard; 38450Sstevel@tonic-gate } 38460Sstevel@tonic-gate } 38470Sstevel@tonic-gate 38480Sstevel@tonic-gate /* get pointers to IP header */ 38490Sstevel@tonic-gate if (isv4) { 38500Sstevel@tonic-gate ipha = (ipha_t *)data_mp->b_rptr; 38510Sstevel@tonic-gate } else { 38520Sstevel@tonic-gate ip6h = (ip6_t *)data_mp->b_rptr; 38530Sstevel@tonic-gate } 38540Sstevel@tonic-gate 38550Sstevel@tonic-gate /* 38560Sstevel@tonic-gate * Compare ICV in ESP packet vs ICV computed by adapter. 38570Sstevel@tonic-gate * We also remove the ICV from the end of the packet since 38580Sstevel@tonic-gate * it will no longer be needed. 38590Sstevel@tonic-gate * 38600Sstevel@tonic-gate * Assume that esp_inbound() already ensured that the pkt 38610Sstevel@tonic-gate * was in one mblk. 38620Sstevel@tonic-gate */ 38630Sstevel@tonic-gate ASSERT(data_mp->b_cont == NULL); 38640Sstevel@tonic-gate data_mp->b_wptr -= icv_len; 38650Sstevel@tonic-gate /* adjust IP header */ 38660Sstevel@tonic-gate if (isv4) 38670Sstevel@tonic-gate ipha->ipha_length = htons(ntohs(ipha->ipha_length) - icv_len); 38680Sstevel@tonic-gate else 38690Sstevel@tonic-gate ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - icv_len); 38700Sstevel@tonic-gate if (icv_len && bcmp(hada->da_icv, data_mp->b_wptr, icv_len)) { 38710Sstevel@tonic-gate int af; 38720Sstevel@tonic-gate void *addr; 38730Sstevel@tonic-gate 38740Sstevel@tonic-gate if (isv4) { 38750Sstevel@tonic-gate addr = &ipha->ipha_dst; 38760Sstevel@tonic-gate af = AF_INET; 38770Sstevel@tonic-gate } else { 38780Sstevel@tonic-gate addr = &ip6h->ip6_dst; 38790Sstevel@tonic-gate af = AF_INET6; 38800Sstevel@tonic-gate } 38810Sstevel@tonic-gate 38820Sstevel@tonic-gate /* 38830Sstevel@tonic-gate * Log the event. Don't print to the console, block 38840Sstevel@tonic-gate * potential denial-of-service attack. 38850Sstevel@tonic-gate */ 3886*3448Sdh155122 ESP_BUMP_STAT(espstack, bad_auth); 38870Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 38880Sstevel@tonic-gate "ESP Authentication failed spi %x, dst_addr %s", 3889*3448Sdh155122 assoc->ipsa_spi, addr, af, espstack->ipsecesp_netstack); 3890*3448Sdh155122 counter = DROPPER(ipss, ipds_esp_bad_auth); 38910Sstevel@tonic-gate goto esp_in_discard; 38920Sstevel@tonic-gate } 38930Sstevel@tonic-gate 3894*3448Sdh155122 esp3dbg(espstack, ("esp_inbound_accelerated: ESP authentication " 3895*3448Sdh155122 "succeeded, checking replay\n")); 38960Sstevel@tonic-gate 38970Sstevel@tonic-gate ipsec_in->b_cont = data_mp; 38980Sstevel@tonic-gate 38990Sstevel@tonic-gate /* 39000Sstevel@tonic-gate * Remove ESP header and padding from packet. 39010Sstevel@tonic-gate */ 39020Sstevel@tonic-gate if (!esp_strip_header(data_mp, ii->ipsec_in_v4, assoc->ipsa_iv_len, 3903*3448Sdh155122 &counter, espstack)) { 3904*3448Sdh155122 esp1dbg(espstack, ("esp_inbound_accelerated: " 39050Sstevel@tonic-gate "esp_strip_header() failed\n")); 39060Sstevel@tonic-gate goto esp_in_discard; 39070Sstevel@tonic-gate } 39080Sstevel@tonic-gate 39090Sstevel@tonic-gate freeb(hada_mp); 39100Sstevel@tonic-gate 39110Sstevel@tonic-gate /* 39120Sstevel@tonic-gate * Account for usage.. 39130Sstevel@tonic-gate */ 39140Sstevel@tonic-gate if (!esp_age_bytes(assoc, msgdsize(data_mp), B_TRUE)) { 39150Sstevel@tonic-gate /* The ipsa has hit hard expiration, LOG and AUDIT. */ 3916*3448Sdh155122 ESP_BUMP_STAT(espstack, bytes_expired); 3917*3448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards); 39180Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 39190Sstevel@tonic-gate "ESP association 0x%x, dst %s had bytes expire.\n", 3920*3448Sdh155122 assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam, 3921*3448Sdh155122 espstack->ipsecesp_netstack); 39220Sstevel@tonic-gate ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, 3923*3448Sdh155122 DROPPER(ipss, ipds_esp_bytes_expire), 3924*3448Sdh155122 &espstack->esp_dropper); 39250Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 39260Sstevel@tonic-gate } 39270Sstevel@tonic-gate 39280Sstevel@tonic-gate /* done processing the packet */ 39290Sstevel@tonic-gate return (IPSEC_STATUS_SUCCESS); 39300Sstevel@tonic-gate 39310Sstevel@tonic-gate esp_in_discard: 3932*3448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards); 39330Sstevel@tonic-gate freeb(hada_mp); 39340Sstevel@tonic-gate 39350Sstevel@tonic-gate ipsec_in->b_cont = data_mp; /* For ip_drop_packet()'s sake... */ 3936*3448Sdh155122 ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter, 3937*3448Sdh155122 &espstack->esp_dropper); 39380Sstevel@tonic-gate 39390Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 39400Sstevel@tonic-gate } 39410Sstevel@tonic-gate 39420Sstevel@tonic-gate /* 39430Sstevel@tonic-gate * Wrapper to allow IP to trigger an ESP association failure message 39440Sstevel@tonic-gate * during inbound SA selection. 39450Sstevel@tonic-gate */ 39460Sstevel@tonic-gate void 39470Sstevel@tonic-gate ipsecesp_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt, 3948*3448Sdh155122 uint32_t spi, void *addr, int af, ipsecesp_stack_t *espstack) 39490Sstevel@tonic-gate { 3950*3448Sdh155122 ipsec_stack_t *ipss = espstack->ipsecesp_netstack->netstack_ipsec; 3951*3448Sdh155122 3952*3448Sdh155122 if (espstack->ipsecesp_log_unknown_spi) { 39530Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi, 3954*3448Sdh155122 addr, af, espstack->ipsecesp_netstack); 39550Sstevel@tonic-gate } 39560Sstevel@tonic-gate 3957*3448Sdh155122 ip_drop_packet(mp, B_TRUE, NULL, NULL, 3958*3448Sdh155122 DROPPER(ipss, ipds_esp_no_sa), 3959*3448Sdh155122 &espstack->esp_dropper); 39600Sstevel@tonic-gate } 39610Sstevel@tonic-gate 39620Sstevel@tonic-gate /* 39630Sstevel@tonic-gate * Initialize the ESP input and output processing functions. 39640Sstevel@tonic-gate */ 39650Sstevel@tonic-gate void 39660Sstevel@tonic-gate ipsecesp_init_funcs(ipsa_t *sa) 39670Sstevel@tonic-gate { 39680Sstevel@tonic-gate if (sa->ipsa_output_func == NULL) 39690Sstevel@tonic-gate sa->ipsa_output_func = esp_outbound; 39700Sstevel@tonic-gate if (sa->ipsa_input_func == NULL) 39710Sstevel@tonic-gate sa->ipsa_input_func = esp_inbound; 39720Sstevel@tonic-gate } 3973