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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23134Sdanmcd * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/stream.h> 310Sstevel@tonic-gate #include <sys/stropts.h> 320Sstevel@tonic-gate #include <sys/errno.h> 330Sstevel@tonic-gate #include <sys/strlog.h> 340Sstevel@tonic-gate #include <sys/tihdr.h> 350Sstevel@tonic-gate #include <sys/socket.h> 360Sstevel@tonic-gate #include <sys/ddi.h> 370Sstevel@tonic-gate #include <sys/sunddi.h> 380Sstevel@tonic-gate #include <sys/kmem.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> 670Sstevel@tonic-gate 680Sstevel@tonic-gate #include <sys/iphada.h> 690Sstevel@tonic-gate 700Sstevel@tonic-gate /* Packet dropper for ESP drops. */ 710Sstevel@tonic-gate static ipdropper_t esp_dropper; 720Sstevel@tonic-gate 730Sstevel@tonic-gate static kmutex_t ipsecesp_param_lock; /* Protects ipsecesp_param_arr[] below. */ 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * Table of ND variables supported by ipsecesp. These are loaded into 760Sstevel@tonic-gate * ipsecesp_g_nd in ipsecesp_init_nd. 770Sstevel@tonic-gate * All of these are alterable, within the min/max values given, at run time. 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate static ipsecespparam_t ipsecesp_param_arr[] = { 800Sstevel@tonic-gate /* min max value name */ 810Sstevel@tonic-gate { 0, 3, 0, "ipsecesp_debug"}, 820Sstevel@tonic-gate { 125, 32000, SADB_AGE_INTERVAL_DEFAULT, "ipsecesp_age_interval"}, 830Sstevel@tonic-gate { 1, 10, 1, "ipsecesp_reap_delay"}, 840Sstevel@tonic-gate { 1, SADB_MAX_REPLAY, 64, "ipsecesp_replay_size"}, 850Sstevel@tonic-gate { 1, 300, 15, "ipsecesp_acquire_timeout"}, 860Sstevel@tonic-gate { 1, 1800, 90, "ipsecesp_larval_timeout"}, 870Sstevel@tonic-gate /* Default lifetime values for ACQUIRE messages. */ 880Sstevel@tonic-gate { 0, 0xffffffffU, 0, "ipsecesp_default_soft_bytes"}, 890Sstevel@tonic-gate { 0, 0xffffffffU, 0, "ipsecesp_default_hard_bytes"}, 900Sstevel@tonic-gate { 0, 0xffffffffU, 24000, "ipsecesp_default_soft_addtime"}, 910Sstevel@tonic-gate { 0, 0xffffffffU, 28800, "ipsecesp_default_hard_addtime"}, 920Sstevel@tonic-gate { 0, 0xffffffffU, 0, "ipsecesp_default_soft_usetime"}, 930Sstevel@tonic-gate { 0, 0xffffffffU, 0, "ipsecesp_default_hard_usetime"}, 940Sstevel@tonic-gate { 0, 1, 0, "ipsecesp_log_unknown_spi"}, 950Sstevel@tonic-gate { 0, 2, 1, "ipsecesp_padding_check"}, 960Sstevel@tonic-gate }; 970Sstevel@tonic-gate #define ipsecesp_debug ipsecesp_param_arr[0].ipsecesp_param_value 980Sstevel@tonic-gate #define ipsecesp_age_interval ipsecesp_param_arr[1].ipsecesp_param_value 990Sstevel@tonic-gate #define ipsecesp_age_int_max ipsecesp_param_arr[1].ipsecesp_param_max 1000Sstevel@tonic-gate #define ipsecesp_reap_delay ipsecesp_param_arr[2].ipsecesp_param_value 1010Sstevel@tonic-gate #define ipsecesp_replay_size ipsecesp_param_arr[3].ipsecesp_param_value 1020Sstevel@tonic-gate #define ipsecesp_acquire_timeout ipsecesp_param_arr[4].ipsecesp_param_value 1030Sstevel@tonic-gate #define ipsecesp_larval_timeout ipsecesp_param_arr[5].ipsecesp_param_value 1040Sstevel@tonic-gate #define ipsecesp_default_soft_bytes \ 1050Sstevel@tonic-gate ipsecesp_param_arr[6].ipsecesp_param_value 1060Sstevel@tonic-gate #define ipsecesp_default_hard_bytes \ 1070Sstevel@tonic-gate ipsecesp_param_arr[7].ipsecesp_param_value 1080Sstevel@tonic-gate #define ipsecesp_default_soft_addtime \ 1090Sstevel@tonic-gate ipsecesp_param_arr[8].ipsecesp_param_value 1100Sstevel@tonic-gate #define ipsecesp_default_hard_addtime \ 1110Sstevel@tonic-gate ipsecesp_param_arr[9].ipsecesp_param_value 1120Sstevel@tonic-gate #define ipsecesp_default_soft_usetime \ 1130Sstevel@tonic-gate ipsecesp_param_arr[10].ipsecesp_param_value 1140Sstevel@tonic-gate #define ipsecesp_default_hard_usetime \ 1150Sstevel@tonic-gate ipsecesp_param_arr[11].ipsecesp_param_value 1160Sstevel@tonic-gate #define ipsecesp_log_unknown_spi \ 1170Sstevel@tonic-gate ipsecesp_param_arr[12].ipsecesp_param_value 1180Sstevel@tonic-gate #define ipsecesp_padding_check \ 1190Sstevel@tonic-gate ipsecesp_param_arr[13].ipsecesp_param_value 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate #define esp0dbg(a) printf a 1220Sstevel@tonic-gate /* NOTE: != 0 instead of > 0 so lint doesn't complain. */ 1230Sstevel@tonic-gate #define esp1dbg(a) if (ipsecesp_debug != 0) printf a 1240Sstevel@tonic-gate #define esp2dbg(a) if (ipsecesp_debug > 1) printf a 1250Sstevel@tonic-gate #define esp3dbg(a) if (ipsecesp_debug > 2) printf a 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate static IDP ipsecesp_g_nd; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate static int ipsecesp_open(queue_t *, dev_t *, int, int, cred_t *); 1300Sstevel@tonic-gate static int ipsecesp_close(queue_t *); 1310Sstevel@tonic-gate static void ipsecesp_rput(queue_t *, mblk_t *); 1320Sstevel@tonic-gate static void ipsecesp_wput(queue_t *, mblk_t *); 1330Sstevel@tonic-gate static void esp_send_acquire(ipsacq_t *, mblk_t *); 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate static ipsec_status_t esp_outbound_accelerated(mblk_t *, uint_t); 1360Sstevel@tonic-gate static ipsec_status_t esp_inbound_accelerated(mblk_t *, mblk_t *, 1370Sstevel@tonic-gate boolean_t, ipsa_t *); 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate static boolean_t esp_register_out(uint32_t, uint32_t, uint_t); 1400Sstevel@tonic-gate static boolean_t esp_strip_header(mblk_t *, boolean_t, uint32_t, 1410Sstevel@tonic-gate kstat_named_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 1460Sstevel@tonic-gate static struct module_info info = { 1470Sstevel@tonic-gate 5137, "ipsecesp", 0, INFPSZ, 65536, 1024 1480Sstevel@tonic-gate }; 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate static struct qinit rinit = { 1510Sstevel@tonic-gate (pfi_t)ipsecesp_rput, NULL, ipsecesp_open, ipsecesp_close, NULL, &info, 1520Sstevel@tonic-gate NULL 1530Sstevel@tonic-gate }; 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate static struct qinit winit = { 1560Sstevel@tonic-gate (pfi_t)ipsecesp_wput, NULL, ipsecesp_open, ipsecesp_close, NULL, &info, 1570Sstevel@tonic-gate NULL 1580Sstevel@tonic-gate }; 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate struct streamtab ipsecespinfo = { 1610Sstevel@tonic-gate &rinit, &winit, NULL, NULL 1620Sstevel@tonic-gate }; 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate /* 1650Sstevel@tonic-gate * Keysock instance of ESP. "There can be only one." :) 1660Sstevel@tonic-gate * Use casptr() on this because I don't set it until KEYSOCK_HELLO comes down. 1670Sstevel@tonic-gate * Paired up with the esp_pfkey_q is the esp_event, which will age SAs. 1680Sstevel@tonic-gate */ 1690Sstevel@tonic-gate static queue_t *esp_pfkey_q; 1700Sstevel@tonic-gate static timeout_id_t esp_event; 1710Sstevel@tonic-gate static taskq_t *esp_taskq; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * OTOH, this one is set at open/close, and I'm D_MTQPAIR for now. 1750Sstevel@tonic-gate * 1760Sstevel@tonic-gate * Question: Do I need this, given that all instance's esps->esps_wq point 1770Sstevel@tonic-gate * to IP? 1780Sstevel@tonic-gate * 1790Sstevel@tonic-gate * Answer: Yes, because I need to know which queue is BOUND to 1800Sstevel@tonic-gate * IPPROTO_ESP 1810Sstevel@tonic-gate */ 1820Sstevel@tonic-gate static mblk_t *esp_ip_unbind; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* 1850Sstevel@tonic-gate * Stats. This may eventually become a full-blown SNMP MIB once that spec 1860Sstevel@tonic-gate * stabilizes. 1870Sstevel@tonic-gate */ 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate typedef struct { 1900Sstevel@tonic-gate kstat_named_t esp_stat_num_aalgs; 1910Sstevel@tonic-gate kstat_named_t esp_stat_good_auth; 1920Sstevel@tonic-gate kstat_named_t esp_stat_bad_auth; 1930Sstevel@tonic-gate kstat_named_t esp_stat_bad_padding; 1940Sstevel@tonic-gate kstat_named_t esp_stat_replay_failures; 1950Sstevel@tonic-gate kstat_named_t esp_stat_replay_early_failures; 1960Sstevel@tonic-gate kstat_named_t esp_stat_keysock_in; 1970Sstevel@tonic-gate kstat_named_t esp_stat_out_requests; 1980Sstevel@tonic-gate kstat_named_t esp_stat_acquire_requests; 1990Sstevel@tonic-gate kstat_named_t esp_stat_bytes_expired; 2000Sstevel@tonic-gate kstat_named_t esp_stat_out_discards; 2010Sstevel@tonic-gate kstat_named_t esp_stat_in_accelerated; 2020Sstevel@tonic-gate kstat_named_t esp_stat_out_accelerated; 2030Sstevel@tonic-gate kstat_named_t esp_stat_noaccel; 2040Sstevel@tonic-gate kstat_named_t esp_stat_crypto_sync; 2050Sstevel@tonic-gate kstat_named_t esp_stat_crypto_async; 2060Sstevel@tonic-gate kstat_named_t esp_stat_crypto_failures; 2070Sstevel@tonic-gate kstat_named_t esp_stat_num_ealgs; 2080Sstevel@tonic-gate kstat_named_t esp_stat_bad_decrypt; 2090Sstevel@tonic-gate } esp_kstats_t; 2100Sstevel@tonic-gate 211564Ssommerfe uint32_t esp_hash_size = IPSEC_DEFAULT_HASH_SIZE; 2120Sstevel@tonic-gate #define ESP_BUMP_STAT(x) (esp_kstats->esp_stat_ ## x).value.ui64++ 2130Sstevel@tonic-gate #define ESP_DEBUMP_STAT(x) (esp_kstats->esp_stat_ ## x).value.ui64-- 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate static kstat_t *esp_ksp; 2160Sstevel@tonic-gate static esp_kstats_t *esp_kstats; 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate static int esp_kstat_update(kstat_t *, int); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate static boolean_t 2210Sstevel@tonic-gate esp_kstat_init(void) 2220Sstevel@tonic-gate { 2230Sstevel@tonic-gate esp_ksp = kstat_create("ipsecesp", 0, "esp_stat", "net", 2240Sstevel@tonic-gate KSTAT_TYPE_NAMED, sizeof (*esp_kstats) / sizeof (kstat_named_t), 2250Sstevel@tonic-gate KSTAT_FLAG_PERSISTENT); 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate if (esp_ksp == NULL) 2280Sstevel@tonic-gate return (B_FALSE); 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate esp_kstats = esp_ksp->ks_data; 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate esp_ksp->ks_update = esp_kstat_update; 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate #define K64 KSTAT_DATA_UINT64 2350Sstevel@tonic-gate #define KI(x) kstat_named_init(&(esp_kstats->esp_stat_##x), #x, K64) 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate KI(num_aalgs); 2380Sstevel@tonic-gate KI(num_ealgs); 2390Sstevel@tonic-gate KI(good_auth); 2400Sstevel@tonic-gate KI(bad_auth); 2410Sstevel@tonic-gate KI(bad_padding); 2420Sstevel@tonic-gate KI(replay_failures); 2430Sstevel@tonic-gate KI(replay_early_failures); 2440Sstevel@tonic-gate KI(keysock_in); 2450Sstevel@tonic-gate KI(out_requests); 2460Sstevel@tonic-gate KI(acquire_requests); 2470Sstevel@tonic-gate KI(bytes_expired); 2480Sstevel@tonic-gate KI(out_discards); 2490Sstevel@tonic-gate KI(in_accelerated); 2500Sstevel@tonic-gate KI(out_accelerated); 2510Sstevel@tonic-gate KI(noaccel); 2520Sstevel@tonic-gate KI(crypto_sync); 2530Sstevel@tonic-gate KI(crypto_async); 2540Sstevel@tonic-gate KI(crypto_failures); 2550Sstevel@tonic-gate KI(bad_decrypt); 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate #undef KI 2580Sstevel@tonic-gate #undef K64 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate kstat_install(esp_ksp); 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate return (B_TRUE); 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate static int 2660Sstevel@tonic-gate esp_kstat_update(kstat_t *kp, int rw) 2670Sstevel@tonic-gate { 2680Sstevel@tonic-gate esp_kstats_t *ekp; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if ((kp == NULL) || (kp->ks_data == NULL)) 2710Sstevel@tonic-gate return (EIO); 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate if (rw == KSTAT_WRITE) 2740Sstevel@tonic-gate return (EACCES); 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate ASSERT(kp == esp_ksp); 2770Sstevel@tonic-gate ekp = (esp_kstats_t *)kp->ks_data; 2780Sstevel@tonic-gate ASSERT(ekp == esp_kstats); 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate mutex_enter(&alg_lock); 2810Sstevel@tonic-gate ekp->esp_stat_num_aalgs.value.ui64 = ipsec_nalgs[IPSEC_ALG_AUTH]; 2820Sstevel@tonic-gate ekp->esp_stat_num_ealgs.value.ui64 = ipsec_nalgs[IPSEC_ALG_ENCR]; 2830Sstevel@tonic-gate mutex_exit(&alg_lock); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate return (0); 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate #ifdef DEBUG 2890Sstevel@tonic-gate /* 2900Sstevel@tonic-gate * Debug routine, useful to see pre-encryption data. 2910Sstevel@tonic-gate */ 2920Sstevel@tonic-gate static char * 2930Sstevel@tonic-gate dump_msg(mblk_t *mp) 2940Sstevel@tonic-gate { 2950Sstevel@tonic-gate char tmp_str[3], tmp_line[256]; 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate while (mp != NULL) { 2980Sstevel@tonic-gate unsigned char *ptr; 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate printf("mblk address 0x%p, length %ld, db_ref %d " 3010Sstevel@tonic-gate "type %d, base 0x%p, lim 0x%p\n", 3020Sstevel@tonic-gate (void *) mp, (long)(mp->b_wptr - mp->b_rptr), 3030Sstevel@tonic-gate mp->b_datap->db_ref, mp->b_datap->db_type, 3040Sstevel@tonic-gate (void *)mp->b_datap->db_base, (void *)mp->b_datap->db_lim); 3050Sstevel@tonic-gate ptr = mp->b_rptr; 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate tmp_line[0] = '\0'; 3080Sstevel@tonic-gate while (ptr < mp->b_wptr) { 3090Sstevel@tonic-gate uint_t diff; 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate diff = (ptr - mp->b_rptr); 3120Sstevel@tonic-gate if (!(diff & 0x1f)) { 3130Sstevel@tonic-gate if (strlen(tmp_line) > 0) { 3140Sstevel@tonic-gate printf("bytes: %s\n", tmp_line); 3150Sstevel@tonic-gate tmp_line[0] = '\0'; 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate if (!(diff & 0x3)) 3190Sstevel@tonic-gate (void) strcat(tmp_line, " "); 3200Sstevel@tonic-gate (void) sprintf(tmp_str, "%02x", *ptr); 3210Sstevel@tonic-gate (void) strcat(tmp_line, tmp_str); 3220Sstevel@tonic-gate ptr++; 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate if (strlen(tmp_line) > 0) 3250Sstevel@tonic-gate printf("bytes: %s\n", tmp_line); 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate mp = mp->b_cont; 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate return ("\n"); 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate #else /* DEBUG */ 3340Sstevel@tonic-gate static char * 3350Sstevel@tonic-gate dump_msg(mblk_t *mp) 3360Sstevel@tonic-gate { 3370Sstevel@tonic-gate printf("Find value of mp %p.\n", mp); 3380Sstevel@tonic-gate return ("\n"); 3390Sstevel@tonic-gate } 3400Sstevel@tonic-gate #endif /* DEBUG */ 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate /* 3430Sstevel@tonic-gate * Don't have to lock age_interval, as only one thread will access it at 3440Sstevel@tonic-gate * a time, because I control the one function that does with timeout(). 3450Sstevel@tonic-gate */ 3460Sstevel@tonic-gate /* ARGSUSED */ 3470Sstevel@tonic-gate static void 3480Sstevel@tonic-gate esp_ager(void *ignoreme) 3490Sstevel@tonic-gate { 3500Sstevel@tonic-gate hrtime_t begin = gethrtime(); 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate sadb_ager(&esp_sadb.s_v4, esp_pfkey_q, esp_sadb.s_ip_q, 3530Sstevel@tonic-gate ipsecesp_reap_delay); 3540Sstevel@tonic-gate sadb_ager(&esp_sadb.s_v6, esp_pfkey_q, esp_sadb.s_ip_q, 3550Sstevel@tonic-gate ipsecesp_reap_delay); 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate esp_event = sadb_retimeout(begin, esp_pfkey_q, esp_ager, 3580Sstevel@tonic-gate &(ipsecesp_age_interval), ipsecesp_age_int_max, info.mi_idnum); 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate /* 3620Sstevel@tonic-gate * Get an ESP NDD parameter. 3630Sstevel@tonic-gate */ 3640Sstevel@tonic-gate /* ARGSUSED */ 3650Sstevel@tonic-gate static int 3660Sstevel@tonic-gate ipsecesp_param_get(q, mp, cp, cr) 3670Sstevel@tonic-gate queue_t *q; 3680Sstevel@tonic-gate mblk_t *mp; 3690Sstevel@tonic-gate caddr_t cp; 3700Sstevel@tonic-gate cred_t *cr; 3710Sstevel@tonic-gate { 3720Sstevel@tonic-gate ipsecespparam_t *ipsecesppa = (ipsecespparam_t *)cp; 3730Sstevel@tonic-gate uint_t value; 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate mutex_enter(&ipsecesp_param_lock); 3760Sstevel@tonic-gate value = ipsecesppa->ipsecesp_param_value; 3770Sstevel@tonic-gate mutex_exit(&ipsecesp_param_lock); 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate (void) mi_mpprintf(mp, "%u", value); 3800Sstevel@tonic-gate return (0); 3810Sstevel@tonic-gate } 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate /* 3840Sstevel@tonic-gate * This routine sets an NDD variable in a ipsecespparam_t structure. 3850Sstevel@tonic-gate */ 3860Sstevel@tonic-gate /* ARGSUSED */ 3870Sstevel@tonic-gate static int 3880Sstevel@tonic-gate ipsecesp_param_set(q, mp, value, cp, cr) 3890Sstevel@tonic-gate queue_t *q; 3900Sstevel@tonic-gate mblk_t *mp; 3910Sstevel@tonic-gate char *value; 3920Sstevel@tonic-gate caddr_t cp; 3930Sstevel@tonic-gate cred_t *cr; 3940Sstevel@tonic-gate { 3950Sstevel@tonic-gate ulong_t new_value; 3960Sstevel@tonic-gate ipsecespparam_t *ipsecesppa = (ipsecespparam_t *)cp; 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate /* 3990Sstevel@tonic-gate * Fail the request if the new value does not lie within the 4000Sstevel@tonic-gate * required bounds. 4010Sstevel@tonic-gate */ 4020Sstevel@tonic-gate if (ddi_strtoul(value, NULL, 10, &new_value) != 0 || 4030Sstevel@tonic-gate new_value < ipsecesppa->ipsecesp_param_min || 4040Sstevel@tonic-gate new_value > ipsecesppa->ipsecesp_param_max) { 4050Sstevel@tonic-gate return (EINVAL); 4060Sstevel@tonic-gate } 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate /* Set the new value */ 4090Sstevel@tonic-gate mutex_enter(&ipsecesp_param_lock); 4100Sstevel@tonic-gate ipsecesppa->ipsecesp_param_value = new_value; 4110Sstevel@tonic-gate mutex_exit(&ipsecesp_param_lock); 4120Sstevel@tonic-gate return (0); 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate /* 4160Sstevel@tonic-gate * Using lifetime NDD variables, fill in an extended combination's 4170Sstevel@tonic-gate * lifetime information. 4180Sstevel@tonic-gate */ 4190Sstevel@tonic-gate void 4200Sstevel@tonic-gate ipsecesp_fill_defs(sadb_x_ecomb_t *ecomb) 4210Sstevel@tonic-gate { 4220Sstevel@tonic-gate ecomb->sadb_x_ecomb_soft_bytes = ipsecesp_default_soft_bytes; 4230Sstevel@tonic-gate ecomb->sadb_x_ecomb_hard_bytes = ipsecesp_default_hard_bytes; 4240Sstevel@tonic-gate ecomb->sadb_x_ecomb_soft_addtime = ipsecesp_default_soft_addtime; 4250Sstevel@tonic-gate ecomb->sadb_x_ecomb_hard_addtime = ipsecesp_default_hard_addtime; 4260Sstevel@tonic-gate ecomb->sadb_x_ecomb_soft_usetime = ipsecesp_default_soft_usetime; 4270Sstevel@tonic-gate ecomb->sadb_x_ecomb_hard_usetime = ipsecesp_default_hard_usetime; 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate /* 4310Sstevel@tonic-gate * Initialize things for ESP at module load time. 4320Sstevel@tonic-gate */ 4330Sstevel@tonic-gate boolean_t 4340Sstevel@tonic-gate ipsecesp_ddi_init(void) 4350Sstevel@tonic-gate { 4360Sstevel@tonic-gate int count; 4370Sstevel@tonic-gate ipsecespparam_t *espp = ipsecesp_param_arr; 4380Sstevel@tonic-gate 4390Sstevel@tonic-gate for (count = A_CNT(ipsecesp_param_arr); count-- > 0; espp++) { 4400Sstevel@tonic-gate if (espp->ipsecesp_param_name != NULL && 4410Sstevel@tonic-gate espp->ipsecesp_param_name[0]) { 4420Sstevel@tonic-gate if (!nd_load(&ipsecesp_g_nd, espp->ipsecesp_param_name, 4430Sstevel@tonic-gate ipsecesp_param_get, ipsecesp_param_set, 4440Sstevel@tonic-gate (caddr_t)espp)) { 4450Sstevel@tonic-gate nd_free(&ipsecesp_g_nd); 4460Sstevel@tonic-gate return (B_FALSE); 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate } 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate if (!esp_kstat_init()) { 4520Sstevel@tonic-gate nd_free(&ipsecesp_g_nd); 4530Sstevel@tonic-gate return (B_FALSE); 4540Sstevel@tonic-gate } 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate esp_sadb.s_acquire_timeout = &ipsecesp_acquire_timeout; 4570Sstevel@tonic-gate esp_sadb.s_acqfn = esp_send_acquire; 458564Ssommerfe sadbp_init("ESP", &esp_sadb, SADB_SATYPE_ESP, esp_hash_size); 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate esp_taskq = taskq_create("esp_taskq", 1, minclsyspri, 4610Sstevel@tonic-gate IPSEC_TASKQ_MIN, IPSEC_TASKQ_MAX, 0); 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate mutex_init(&ipsecesp_param_lock, NULL, MUTEX_DEFAULT, 0); 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate ip_drop_register(&esp_dropper, "IPsec ESP"); 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate return (B_TRUE); 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate /* 4710Sstevel@tonic-gate * Destroy things for ESP at module unload time. 4720Sstevel@tonic-gate */ 4730Sstevel@tonic-gate void 4740Sstevel@tonic-gate ipsecesp_ddi_destroy(void) 4750Sstevel@tonic-gate { 4760Sstevel@tonic-gate esp1dbg(("In ipsecesp_ddi_destroy.\n")); 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate sadbp_destroy(&esp_sadb); 4790Sstevel@tonic-gate ip_drop_unregister(&esp_dropper); 4800Sstevel@tonic-gate taskq_destroy(esp_taskq); 4810Sstevel@tonic-gate mutex_destroy(&ipsecesp_param_lock); 4820Sstevel@tonic-gate nd_free(&ipsecesp_g_nd); 4830Sstevel@tonic-gate kstat_delete(esp_ksp); 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* 4870Sstevel@tonic-gate * ESP module open routine. 4880Sstevel@tonic-gate */ 4890Sstevel@tonic-gate /* ARGSUSED */ 4900Sstevel@tonic-gate static int 4910Sstevel@tonic-gate ipsecesp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 4920Sstevel@tonic-gate { 4930Sstevel@tonic-gate if (secpolicy_net_config(credp, B_FALSE) != 0) { 4940Sstevel@tonic-gate esp1dbg(("Non-privileged user trying to open ipsecesp.\n")); 4950Sstevel@tonic-gate return (EPERM); 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate if (q->q_ptr != NULL) 4990Sstevel@tonic-gate return (0); /* Re-open of an already open instance. */ 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate if (sflag != MODOPEN) 5020Sstevel@tonic-gate return (EINVAL); 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate /* 5050Sstevel@tonic-gate * ASSUMPTIONS (because I'm MT_OCEXCL): 5060Sstevel@tonic-gate * 5070Sstevel@tonic-gate * * I'm being pushed on top of IP for all my opens (incl. #1). 5080Sstevel@tonic-gate * * Only ipsecesp_open() can write into esp_sadb.s_ip_q. 5090Sstevel@tonic-gate * * Because of this, I can check lazily for esp_sadb.s_ip_q. 5100Sstevel@tonic-gate * 5110Sstevel@tonic-gate * If these assumptions are wrong, I'm in BIG trouble... 5120Sstevel@tonic-gate */ 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate q->q_ptr = q; /* just so I know I'm open */ 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate if (esp_sadb.s_ip_q == NULL) { 5170Sstevel@tonic-gate struct T_unbind_req *tur; 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate esp_sadb.s_ip_q = WR(q); 5200Sstevel@tonic-gate /* Allocate an unbind... */ 5210Sstevel@tonic-gate esp_ip_unbind = allocb(sizeof (struct T_unbind_req), BPRI_HI); 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate /* 5240Sstevel@tonic-gate * Send down T_BIND_REQ to bind IPPROTO_ESP. 5250Sstevel@tonic-gate * Handle the ACK here in ESP. 5260Sstevel@tonic-gate */ 5270Sstevel@tonic-gate qprocson(q); 5280Sstevel@tonic-gate if (esp_ip_unbind == NULL || 5290Sstevel@tonic-gate !sadb_t_bind_req(esp_sadb.s_ip_q, IPPROTO_ESP)) { 5300Sstevel@tonic-gate if (esp_ip_unbind != NULL) { 5310Sstevel@tonic-gate freeb(esp_ip_unbind); 5320Sstevel@tonic-gate esp_ip_unbind = NULL; 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate q->q_ptr = NULL; 5350Sstevel@tonic-gate return (ENOMEM); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate esp_ip_unbind->b_datap->db_type = M_PROTO; 5390Sstevel@tonic-gate tur = (struct T_unbind_req *)esp_ip_unbind->b_rptr; 5400Sstevel@tonic-gate tur->PRIM_type = T_UNBIND_REQ; 5410Sstevel@tonic-gate } else { 5420Sstevel@tonic-gate qprocson(q); 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate /* 5460Sstevel@tonic-gate * For now, there's not much I can do. I'll be getting a message 5470Sstevel@tonic-gate * passed down to me from keysock (in my wput), and a T_BIND_ACK 5480Sstevel@tonic-gate * up from IP (in my rput). 5490Sstevel@tonic-gate */ 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate return (0); 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate /* 5550Sstevel@tonic-gate * ESP module close routine. 5560Sstevel@tonic-gate */ 5570Sstevel@tonic-gate static int 5580Sstevel@tonic-gate ipsecesp_close(queue_t *q) 5590Sstevel@tonic-gate { 5600Sstevel@tonic-gate /* 5610Sstevel@tonic-gate * If esp_sadb.s_ip_q is attached to this instance, send a 5620Sstevel@tonic-gate * T_UNBIND_REQ to IP for the instance before doing 5630Sstevel@tonic-gate * a qprocsoff(). 5640Sstevel@tonic-gate */ 5650Sstevel@tonic-gate if (WR(q) == esp_sadb.s_ip_q && esp_ip_unbind != NULL) { 5660Sstevel@tonic-gate putnext(WR(q), esp_ip_unbind); 5670Sstevel@tonic-gate esp_ip_unbind = NULL; 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate /* 5710Sstevel@tonic-gate * Clean up q_ptr, if needed. 5720Sstevel@tonic-gate */ 5730Sstevel@tonic-gate qprocsoff(q); 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate /* Keysock queue check is safe, because of OCEXCL perimeter. */ 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate if (q == esp_pfkey_q) { 5780Sstevel@tonic-gate esp0dbg(("ipsecesp_close: Ummm... keysock is closing ESP.\n")); 5790Sstevel@tonic-gate esp_pfkey_q = NULL; 5800Sstevel@tonic-gate /* Detach qtimeouts. */ 5810Sstevel@tonic-gate (void) quntimeout(q, esp_event); 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate if (WR(q) == esp_sadb.s_ip_q) { 5850Sstevel@tonic-gate /* 5860Sstevel@tonic-gate * If the esp_sadb.s_ip_q is attached to this instance, find 5870Sstevel@tonic-gate * another. The OCEXCL outer perimeter helps us here. 5880Sstevel@tonic-gate */ 5890Sstevel@tonic-gate esp_sadb.s_ip_q = NULL; 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate /* 5920Sstevel@tonic-gate * Find a replacement queue for esp_sadb.s_ip_q. 5930Sstevel@tonic-gate */ 5940Sstevel@tonic-gate if (esp_pfkey_q != NULL && esp_pfkey_q != RD(q)) { 5950Sstevel@tonic-gate /* 5960Sstevel@tonic-gate * See if we can use the pfkey_q. 5970Sstevel@tonic-gate */ 5980Sstevel@tonic-gate esp_sadb.s_ip_q = WR(esp_pfkey_q); 5990Sstevel@tonic-gate } 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate if (esp_sadb.s_ip_q == NULL || 6020Sstevel@tonic-gate !sadb_t_bind_req(esp_sadb.s_ip_q, IPPROTO_ESP)) { 6030Sstevel@tonic-gate esp1dbg(("ipsecesp: Can't reassign ip_q.\n")); 6040Sstevel@tonic-gate esp_sadb.s_ip_q = NULL; 6050Sstevel@tonic-gate } else { 6060Sstevel@tonic-gate esp_ip_unbind = allocb(sizeof (struct T_unbind_req), 6070Sstevel@tonic-gate BPRI_HI); 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate if (esp_ip_unbind != NULL) { 6100Sstevel@tonic-gate struct T_unbind_req *tur; 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate esp_ip_unbind->b_datap->db_type = M_PROTO; 6130Sstevel@tonic-gate tur = (struct T_unbind_req *) 6140Sstevel@tonic-gate esp_ip_unbind->b_rptr; 6150Sstevel@tonic-gate tur->PRIM_type = T_UNBIND_REQ; 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate /* If it's NULL, I can't do much here. */ 6180Sstevel@tonic-gate } 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate return (0); 6220Sstevel@tonic-gate } 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate /* 6250Sstevel@tonic-gate * Add a number of bytes to what the SA has protected so far. Return 6260Sstevel@tonic-gate * B_TRUE if the SA can still protect that many bytes. 6270Sstevel@tonic-gate * 6280Sstevel@tonic-gate * Caller must REFRELE the passed-in assoc. This function must REFRELE 6290Sstevel@tonic-gate * any obtained peer SA. 6300Sstevel@tonic-gate */ 6310Sstevel@tonic-gate static boolean_t 6320Sstevel@tonic-gate esp_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound) 6330Sstevel@tonic-gate { 6340Sstevel@tonic-gate ipsa_t *inassoc, *outassoc; 6350Sstevel@tonic-gate isaf_t *bucket; 6360Sstevel@tonic-gate boolean_t inrc, outrc, isv6; 6370Sstevel@tonic-gate sadb_t *sp; 6380Sstevel@tonic-gate int outhash; 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* No peer? No problem! */ 6410Sstevel@tonic-gate if (!assoc->ipsa_haspeer) { 6420Sstevel@tonic-gate return (sadb_age_bytes(esp_pfkey_q, assoc, bytes, 6430Sstevel@tonic-gate B_TRUE)); 6440Sstevel@tonic-gate } 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate /* 6470Sstevel@tonic-gate * Otherwise, we want to grab both the original assoc and its peer. 6480Sstevel@tonic-gate * There might be a race for this, but if it's a real race, two 6490Sstevel@tonic-gate * expire messages may occur. We limit this by only sending the 6500Sstevel@tonic-gate * expire message on one of the peers, we'll pick the inbound 6510Sstevel@tonic-gate * arbitrarily. 6520Sstevel@tonic-gate * 6530Sstevel@tonic-gate * If we need tight synchronization on the peer SA, then we need to 6540Sstevel@tonic-gate * reconsider. 6550Sstevel@tonic-gate */ 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate /* Use address length to select IPv6/IPv4 */ 6580Sstevel@tonic-gate isv6 = (assoc->ipsa_addrfam == AF_INET6); 6590Sstevel@tonic-gate sp = isv6 ? &esp_sadb.s_v6 : &esp_sadb.s_v4; 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate if (inbound) { 6620Sstevel@tonic-gate inassoc = assoc; 6630Sstevel@tonic-gate if (isv6) { 664564Ssommerfe outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *) 6650Sstevel@tonic-gate &inassoc->ipsa_dstaddr)); 6660Sstevel@tonic-gate } else { 667564Ssommerfe outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *) 6680Sstevel@tonic-gate &inassoc->ipsa_dstaddr)); 6690Sstevel@tonic-gate } 6700Sstevel@tonic-gate bucket = &sp->sdb_of[outhash]; 6710Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock); 6720Sstevel@tonic-gate outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi, 6730Sstevel@tonic-gate inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr, 6740Sstevel@tonic-gate inassoc->ipsa_addrfam); 6750Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock); 6760Sstevel@tonic-gate if (outassoc == NULL) { 6770Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */ 6780Sstevel@tonic-gate esp0dbg(("esp_age_bytes: " 6790Sstevel@tonic-gate "can't find peer for inbound.\n")); 6800Sstevel@tonic-gate return (sadb_age_bytes(esp_pfkey_q, inassoc, 6810Sstevel@tonic-gate bytes, B_TRUE)); 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate } else { 6840Sstevel@tonic-gate outassoc = assoc; 685564Ssommerfe bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi); 6860Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock); 6870Sstevel@tonic-gate inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi, 6880Sstevel@tonic-gate outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr, 6890Sstevel@tonic-gate outassoc->ipsa_addrfam); 6900Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock); 6910Sstevel@tonic-gate if (inassoc == NULL) { 6920Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */ 6930Sstevel@tonic-gate esp0dbg(("esp_age_bytes: " 6940Sstevel@tonic-gate "can't find peer for outbound.\n")); 6950Sstevel@tonic-gate return (sadb_age_bytes(esp_pfkey_q, outassoc, 6960Sstevel@tonic-gate bytes, B_TRUE)); 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate } 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate inrc = sadb_age_bytes(esp_pfkey_q, inassoc, bytes, B_TRUE); 7010Sstevel@tonic-gate outrc = sadb_age_bytes(esp_pfkey_q, outassoc, bytes, B_FALSE); 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate /* 7040Sstevel@tonic-gate * REFRELE any peer SA. 7050Sstevel@tonic-gate * 7060Sstevel@tonic-gate * Because of the multi-line macro nature of IPSA_REFRELE, keep 7070Sstevel@tonic-gate * them in { }. 7080Sstevel@tonic-gate */ 7090Sstevel@tonic-gate if (inbound) { 7100Sstevel@tonic-gate IPSA_REFRELE(outassoc); 7110Sstevel@tonic-gate } else { 7120Sstevel@tonic-gate IPSA_REFRELE(inassoc); 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate return (inrc && outrc); 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate /* 7190Sstevel@tonic-gate * Do incoming NAT-T manipulations for packet. 7200Sstevel@tonic-gate */ 7210Sstevel@tonic-gate static ipsec_status_t 7220Sstevel@tonic-gate esp_fix_natt_checksums(mblk_t *data_mp, ipsa_t *assoc) 7230Sstevel@tonic-gate { 7240Sstevel@tonic-gate ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 7250Sstevel@tonic-gate tcpha_t *tcph; 7260Sstevel@tonic-gate udpha_t *udpha; 7270Sstevel@tonic-gate /* Initialize to our inbound cksum adjustment... */ 7280Sstevel@tonic-gate uint32_t sum = assoc->ipsa_inbound_cksum; 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate switch (ipha->ipha_protocol) { 7310Sstevel@tonic-gate case IPPROTO_TCP: 7320Sstevel@tonic-gate tcph = (tcpha_t *)(data_mp->b_rptr + 7330Sstevel@tonic-gate IPH_HDR_LENGTH(ipha)); 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate #define DOWN_SUM(x) (x) = ((x) & 0xFFFF) + ((x) >> 16) 7360Sstevel@tonic-gate sum += ~ntohs(tcph->tha_sum) & 0xFFFF; 7370Sstevel@tonic-gate DOWN_SUM(sum); 7380Sstevel@tonic-gate DOWN_SUM(sum); 7390Sstevel@tonic-gate tcph->tha_sum = ~htons(sum); 7400Sstevel@tonic-gate break; 7410Sstevel@tonic-gate case IPPROTO_UDP: 7420Sstevel@tonic-gate udpha = (udpha_t *)(data_mp->b_rptr + IPH_HDR_LENGTH(ipha)); 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate if (udpha->uha_checksum != 0) { 7450Sstevel@tonic-gate /* Adujst if the inbound one was not zero. */ 7460Sstevel@tonic-gate sum += ~ntohs(udpha->uha_checksum) & 0xFFFF; 7470Sstevel@tonic-gate DOWN_SUM(sum); 7480Sstevel@tonic-gate DOWN_SUM(sum); 7490Sstevel@tonic-gate udpha->uha_checksum = ~htons(sum); 7500Sstevel@tonic-gate if (udpha->uha_checksum == 0) 7510Sstevel@tonic-gate udpha->uha_checksum = 0xFFFF; 7520Sstevel@tonic-gate } 7530Sstevel@tonic-gate #undef DOWN_SUM 7540Sstevel@tonic-gate break; 7550Sstevel@tonic-gate case IPPROTO_IP: 7560Sstevel@tonic-gate /* 7570Sstevel@tonic-gate * This case is only an issue for self-encapsulated 7580Sstevel@tonic-gate * packets. So for now, fall through. 7590Sstevel@tonic-gate */ 7600Sstevel@tonic-gate break; 7610Sstevel@tonic-gate } 7620Sstevel@tonic-gate return (IPSEC_STATUS_SUCCESS); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate /* 7670Sstevel@tonic-gate * Strip ESP header and fix IP header 7680Sstevel@tonic-gate * Returns B_TRUE on success, B_FALSE if an error occured. 7690Sstevel@tonic-gate */ 7700Sstevel@tonic-gate static boolean_t 7710Sstevel@tonic-gate esp_strip_header(mblk_t *data_mp, boolean_t isv4, uint32_t ivlen, 7720Sstevel@tonic-gate kstat_named_t **counter) 7730Sstevel@tonic-gate { 7740Sstevel@tonic-gate ipha_t *ipha; 7750Sstevel@tonic-gate ip6_t *ip6h; 7760Sstevel@tonic-gate uint_t divpoint; 7770Sstevel@tonic-gate mblk_t *scratch; 7780Sstevel@tonic-gate uint8_t nexthdr, padlen; 7790Sstevel@tonic-gate uint8_t lastpad; 7800Sstevel@tonic-gate 7810Sstevel@tonic-gate /* 7820Sstevel@tonic-gate * Strip ESP data and fix IP header. 7830Sstevel@tonic-gate * 7840Sstevel@tonic-gate * XXX In case the beginning of esp_inbound() changes to not do a 7850Sstevel@tonic-gate * pullup, this part of the code can remain unchanged. 7860Sstevel@tonic-gate */ 7870Sstevel@tonic-gate if (isv4) { 7880Sstevel@tonic-gate ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ipha_t)); 7890Sstevel@tonic-gate ipha = (ipha_t *)data_mp->b_rptr; 7900Sstevel@tonic-gate ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (esph_t) + 7910Sstevel@tonic-gate IPH_HDR_LENGTH(ipha)); 7920Sstevel@tonic-gate divpoint = IPH_HDR_LENGTH(ipha); 7930Sstevel@tonic-gate } else { 7940Sstevel@tonic-gate ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ip6_t)); 7950Sstevel@tonic-gate ip6h = (ip6_t *)data_mp->b_rptr; 7960Sstevel@tonic-gate divpoint = ip_hdr_length_v6(data_mp, ip6h); 7970Sstevel@tonic-gate } 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate scratch = data_mp; 8000Sstevel@tonic-gate while (scratch->b_cont != NULL) 8010Sstevel@tonic-gate scratch = scratch->b_cont; 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate ASSERT((scratch->b_wptr - scratch->b_rptr) >= 3); 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate /* 8060Sstevel@tonic-gate * "Next header" and padding length are the last two bytes in the 8070Sstevel@tonic-gate * ESP-protected datagram, thus the explicit - 1 and - 2. 8080Sstevel@tonic-gate * lastpad is the last byte of the padding, which can be used for 8090Sstevel@tonic-gate * a quick check to see if the padding is correct. 8100Sstevel@tonic-gate */ 8110Sstevel@tonic-gate nexthdr = *(scratch->b_wptr - 1); 8120Sstevel@tonic-gate padlen = *(scratch->b_wptr - 2); 8130Sstevel@tonic-gate lastpad = *(scratch->b_wptr - 3); 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate if (isv4) { 8160Sstevel@tonic-gate /* Fix part of the IP header. */ 8170Sstevel@tonic-gate ipha->ipha_protocol = nexthdr; 8180Sstevel@tonic-gate /* 8190Sstevel@tonic-gate * Reality check the padlen. The explicit - 2 is for the 8200Sstevel@tonic-gate * padding length and the next-header bytes. 8210Sstevel@tonic-gate */ 8220Sstevel@tonic-gate if (padlen >= ntohs(ipha->ipha_length) - sizeof (ipha_t) - 2 - 8230Sstevel@tonic-gate sizeof (esph_t) - ivlen) { 8240Sstevel@tonic-gate ESP_BUMP_STAT(bad_decrypt); 8250Sstevel@tonic-gate ipsec_rl_strlog(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 8260Sstevel@tonic-gate "Possibly corrupt ESP packet."); 8270Sstevel@tonic-gate esp1dbg(("padlen (%d) is greater than:\n", padlen)); 8280Sstevel@tonic-gate esp1dbg(("pkt len(%d) - ip hdr - esp hdr - ivlen(%d) " 8290Sstevel@tonic-gate "= %d.\n", ntohs(ipha->ipha_length), ivlen, 8300Sstevel@tonic-gate (int)(ntohs(ipha->ipha_length) - sizeof (ipha_t) - 8310Sstevel@tonic-gate 2 - sizeof (esph_t) - ivlen))); 8320Sstevel@tonic-gate *counter = &ipdrops_esp_bad_padlen; 8330Sstevel@tonic-gate return (B_FALSE); 8340Sstevel@tonic-gate } 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate /* 8370Sstevel@tonic-gate * Fix the rest of the header. The explicit - 2 is for the 8380Sstevel@tonic-gate * padding length and the next-header bytes. 8390Sstevel@tonic-gate */ 8400Sstevel@tonic-gate ipha->ipha_length = htons(ntohs(ipha->ipha_length) - padlen - 8410Sstevel@tonic-gate 2 - sizeof (esph_t) - ivlen); 8420Sstevel@tonic-gate ipha->ipha_hdr_checksum = 0; 8430Sstevel@tonic-gate ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha); 8440Sstevel@tonic-gate } else { 8450Sstevel@tonic-gate if (ip6h->ip6_nxt == IPPROTO_ESP) { 8460Sstevel@tonic-gate ip6h->ip6_nxt = nexthdr; 8470Sstevel@tonic-gate } else { 8480Sstevel@tonic-gate ip6_pkt_t ipp; 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate bzero(&ipp, sizeof (ipp)); 8510Sstevel@tonic-gate (void) ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL); 8520Sstevel@tonic-gate if (ipp.ipp_dstopts != NULL) { 8530Sstevel@tonic-gate ipp.ipp_dstopts->ip6d_nxt = nexthdr; 8540Sstevel@tonic-gate } else if (ipp.ipp_rthdr != NULL) { 8550Sstevel@tonic-gate ipp.ipp_rthdr->ip6r_nxt = nexthdr; 8560Sstevel@tonic-gate } else if (ipp.ipp_hopopts != NULL) { 8570Sstevel@tonic-gate ipp.ipp_hopopts->ip6h_nxt = nexthdr; 8580Sstevel@tonic-gate } else { 8590Sstevel@tonic-gate /* Panic a DEBUG kernel. */ 8600Sstevel@tonic-gate ASSERT(ipp.ipp_hopopts != NULL); 8610Sstevel@tonic-gate /* Otherwise, pretend it's IP + ESP. */ 8620Sstevel@tonic-gate cmn_err(CE_WARN, "ESP IPv6 headers wrong.\n"); 8630Sstevel@tonic-gate ip6h->ip6_nxt = nexthdr; 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate if (padlen >= ntohs(ip6h->ip6_plen) - 2 - sizeof (esph_t) - 8680Sstevel@tonic-gate ivlen) { 8690Sstevel@tonic-gate ESP_BUMP_STAT(bad_decrypt); 8700Sstevel@tonic-gate ipsec_rl_strlog(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 8710Sstevel@tonic-gate "Possibly corrupt ESP packet."); 8720Sstevel@tonic-gate esp1dbg(("padlen (%d) is greater than:\n", padlen)); 8730Sstevel@tonic-gate esp1dbg(("pkt len(%u) - ip hdr - esp hdr - ivlen(%d)" 8740Sstevel@tonic-gate " = %u.\n", (unsigned)(ntohs(ip6h->ip6_plen) 8750Sstevel@tonic-gate + sizeof (ip6_t)), ivlen, 8760Sstevel@tonic-gate (unsigned)(ntohs(ip6h->ip6_plen) - 2 - 8770Sstevel@tonic-gate sizeof (esph_t) - ivlen))); 8780Sstevel@tonic-gate *counter = &ipdrops_esp_bad_padlen; 8790Sstevel@tonic-gate return (B_FALSE); 8800Sstevel@tonic-gate } 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate /* 8840Sstevel@tonic-gate * Fix the rest of the header. The explicit - 2 is for the 8850Sstevel@tonic-gate * padding length and the next-header bytes. IPv6 is nice, 8860Sstevel@tonic-gate * because there's no hdr checksum! 8870Sstevel@tonic-gate */ 8880Sstevel@tonic-gate ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - padlen - 8890Sstevel@tonic-gate 2 - sizeof (esph_t) - ivlen); 8900Sstevel@tonic-gate } 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate if (ipsecesp_padding_check > 0 && 8930Sstevel@tonic-gate padlen != lastpad && padlen != 0) { 8940Sstevel@tonic-gate ipsec_rl_strlog(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 8950Sstevel@tonic-gate "Possibly corrupt ESP packet."); 8960Sstevel@tonic-gate esp1dbg(("lastpad (%d) not equal to padlen (%d):\n", 8970Sstevel@tonic-gate lastpad, padlen)); 8980Sstevel@tonic-gate ESP_BUMP_STAT(bad_padding); 8990Sstevel@tonic-gate *counter = &ipdrops_esp_bad_padding; 9000Sstevel@tonic-gate return (B_FALSE); 9010Sstevel@tonic-gate } 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate if (ipsecesp_padding_check > 1) { 9040Sstevel@tonic-gate uint8_t *last = (uint8_t *)(scratch->b_wptr - 3); 9050Sstevel@tonic-gate uint8_t lastval = *last; 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate /* 9080Sstevel@tonic-gate * this assert may have to become an if 9090Sstevel@tonic-gate * and a pullup if we start accepting 9100Sstevel@tonic-gate * multi-dblk mblks. Any packet here will 9110Sstevel@tonic-gate * have been pulled up in esp_inbound. 9120Sstevel@tonic-gate */ 9130Sstevel@tonic-gate ASSERT(MBLKL(scratch) >= lastval + 3); 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate while (lastval != 0) { 9160Sstevel@tonic-gate if (lastval != *last) { 9170Sstevel@tonic-gate ipsec_rl_strlog(info.mi_idnum, 0, 0, 9180Sstevel@tonic-gate SL_ERROR | SL_WARN, 9190Sstevel@tonic-gate "Possibly corrupt ESP packet."); 9200Sstevel@tonic-gate esp1dbg(("padding not in correct" 9210Sstevel@tonic-gate " format:\n")); 9220Sstevel@tonic-gate ESP_BUMP_STAT(bad_padding); 9230Sstevel@tonic-gate *counter = &ipdrops_esp_bad_padding; 9240Sstevel@tonic-gate return (B_FALSE); 9250Sstevel@tonic-gate } 9260Sstevel@tonic-gate lastval--; last--; 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate 9300Sstevel@tonic-gate /* Trim off the padding. */ 9310Sstevel@tonic-gate ASSERT(data_mp->b_cont == NULL); 9320Sstevel@tonic-gate data_mp->b_wptr -= (padlen + 2); 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate /* 9350Sstevel@tonic-gate * Remove the ESP header. 9360Sstevel@tonic-gate * 9370Sstevel@tonic-gate * The above assertions about data_mp's size will make this work. 9380Sstevel@tonic-gate * 9390Sstevel@tonic-gate * XXX Question: If I send up and get back a contiguous mblk, 9400Sstevel@tonic-gate * would it be quicker to bcopy over, or keep doing the dupb stuff? 9410Sstevel@tonic-gate * I go with copying for now. 9420Sstevel@tonic-gate */ 9430Sstevel@tonic-gate 9440Sstevel@tonic-gate if (IS_P2ALIGNED(data_mp->b_rptr, sizeof (uint32_t)) && 9450Sstevel@tonic-gate IS_P2ALIGNED(ivlen, sizeof (uint32_t))) { 9460Sstevel@tonic-gate uint8_t *start = data_mp->b_rptr; 9470Sstevel@tonic-gate uint32_t *src, *dst; 9480Sstevel@tonic-gate 9490Sstevel@tonic-gate src = (uint32_t *)(start + divpoint); 9500Sstevel@tonic-gate dst = (uint32_t *)(start + divpoint + sizeof (esph_t) + ivlen); 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate ASSERT(IS_P2ALIGNED(dst, sizeof (uint32_t)) && 9530Sstevel@tonic-gate IS_P2ALIGNED(src, sizeof (uint32_t))); 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate do { 9560Sstevel@tonic-gate src--; 9570Sstevel@tonic-gate dst--; 9580Sstevel@tonic-gate *dst = *src; 9590Sstevel@tonic-gate } while (src != (uint32_t *)start); 9600Sstevel@tonic-gate 9610Sstevel@tonic-gate data_mp->b_rptr = (uchar_t *)dst; 9620Sstevel@tonic-gate } else { 9630Sstevel@tonic-gate uint8_t *start = data_mp->b_rptr; 9640Sstevel@tonic-gate uint8_t *src, *dst; 9650Sstevel@tonic-gate 9660Sstevel@tonic-gate src = start + divpoint; 9670Sstevel@tonic-gate dst = src + sizeof (esph_t) + ivlen; 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate do { 9700Sstevel@tonic-gate src--; 9710Sstevel@tonic-gate dst--; 9720Sstevel@tonic-gate *dst = *src; 9730Sstevel@tonic-gate } while (src != start); 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate data_mp->b_rptr = dst; 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate esp2dbg(("data_mp after inbound ESP adjustment:\n")); 9790Sstevel@tonic-gate esp2dbg((dump_msg(data_mp))); 9800Sstevel@tonic-gate 9810Sstevel@tonic-gate return (B_TRUE); 9820Sstevel@tonic-gate } 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate /* 9850Sstevel@tonic-gate * Updating use times can be tricky business if the ipsa_haspeer flag is 9860Sstevel@tonic-gate * set. This function is called once in an SA's lifetime. 9870Sstevel@tonic-gate * 9880Sstevel@tonic-gate * Caller has to REFRELE "assoc" which is passed in. This function has 9890Sstevel@tonic-gate * to REFRELE any peer SA that is obtained. 9900Sstevel@tonic-gate */ 9910Sstevel@tonic-gate static void 9920Sstevel@tonic-gate esp_set_usetime(ipsa_t *assoc, boolean_t inbound) 9930Sstevel@tonic-gate { 9940Sstevel@tonic-gate ipsa_t *inassoc, *outassoc; 9950Sstevel@tonic-gate isaf_t *bucket; 9960Sstevel@tonic-gate sadb_t *sp; 9970Sstevel@tonic-gate int outhash; 9980Sstevel@tonic-gate boolean_t isv6; 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate /* No peer? No problem! */ 10010Sstevel@tonic-gate if (!assoc->ipsa_haspeer) { 10020Sstevel@tonic-gate sadb_set_usetime(assoc); 10030Sstevel@tonic-gate return; 10040Sstevel@tonic-gate } 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate /* 10070Sstevel@tonic-gate * Otherwise, we want to grab both the original assoc and its peer. 10080Sstevel@tonic-gate * There might be a race for this, but if it's a real race, the times 10090Sstevel@tonic-gate * will be out-of-synch by at most a second, and since our time 10100Sstevel@tonic-gate * granularity is a second, this won't be a problem. 10110Sstevel@tonic-gate * 10120Sstevel@tonic-gate * If we need tight synchronization on the peer SA, then we need to 10130Sstevel@tonic-gate * reconsider. 10140Sstevel@tonic-gate */ 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate /* Use address length to select IPv6/IPv4 */ 10170Sstevel@tonic-gate isv6 = (assoc->ipsa_addrfam == AF_INET6); 10180Sstevel@tonic-gate sp = isv6 ? &esp_sadb.s_v6 : &esp_sadb.s_v4; 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate if (inbound) { 10210Sstevel@tonic-gate inassoc = assoc; 10220Sstevel@tonic-gate if (isv6) { 1023564Ssommerfe outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *) 10240Sstevel@tonic-gate &inassoc->ipsa_dstaddr)); 10250Sstevel@tonic-gate } else { 1026564Ssommerfe outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *) 10270Sstevel@tonic-gate &inassoc->ipsa_dstaddr)); 10280Sstevel@tonic-gate } 10290Sstevel@tonic-gate bucket = &sp->sdb_of[outhash]; 10300Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock); 10310Sstevel@tonic-gate outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi, 10320Sstevel@tonic-gate inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr, 10330Sstevel@tonic-gate inassoc->ipsa_addrfam); 10340Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock); 10350Sstevel@tonic-gate if (outassoc == NULL) { 10360Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */ 10370Sstevel@tonic-gate esp0dbg(("esp_set_usetime: " 10380Sstevel@tonic-gate "can't find peer for inbound.\n")); 10390Sstevel@tonic-gate sadb_set_usetime(inassoc); 10400Sstevel@tonic-gate return; 10410Sstevel@tonic-gate } 10420Sstevel@tonic-gate } else { 10430Sstevel@tonic-gate outassoc = assoc; 1044564Ssommerfe bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi); 10450Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock); 10460Sstevel@tonic-gate inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi, 10470Sstevel@tonic-gate outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr, 10480Sstevel@tonic-gate outassoc->ipsa_addrfam); 10490Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock); 10500Sstevel@tonic-gate if (inassoc == NULL) { 10510Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */ 10520Sstevel@tonic-gate esp0dbg(("esp_set_usetime: " 10530Sstevel@tonic-gate "can't find peer for outbound.\n")); 10540Sstevel@tonic-gate sadb_set_usetime(outassoc); 10550Sstevel@tonic-gate return; 10560Sstevel@tonic-gate } 10570Sstevel@tonic-gate } 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate /* Update usetime on both. */ 10600Sstevel@tonic-gate sadb_set_usetime(inassoc); 10610Sstevel@tonic-gate sadb_set_usetime(outassoc); 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate /* 10640Sstevel@tonic-gate * REFRELE any peer SA. 10650Sstevel@tonic-gate * 10660Sstevel@tonic-gate * Because of the multi-line macro nature of IPSA_REFRELE, keep 10670Sstevel@tonic-gate * them in { }. 10680Sstevel@tonic-gate */ 10690Sstevel@tonic-gate if (inbound) { 10700Sstevel@tonic-gate IPSA_REFRELE(outassoc); 10710Sstevel@tonic-gate } else { 10720Sstevel@tonic-gate IPSA_REFRELE(inassoc); 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate /* 10770Sstevel@tonic-gate * Handle ESP inbound data for IPv4 and IPv6. 10780Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the 10790Sstevel@tonic-gate * mblk chain ipsec_in_mp. 10800Sstevel@tonic-gate */ 10810Sstevel@tonic-gate ipsec_status_t 10820Sstevel@tonic-gate esp_inbound(mblk_t *ipsec_in_mp, void *arg) 10830Sstevel@tonic-gate { 10840Sstevel@tonic-gate mblk_t *data_mp = ipsec_in_mp->b_cont; 10850Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr; 10860Sstevel@tonic-gate esph_t *esph = (esph_t *)arg; 10870Sstevel@tonic-gate ipsa_t *ipsa = ii->ipsec_in_esp_sa; 10880Sstevel@tonic-gate 10890Sstevel@tonic-gate if (ipsa->ipsa_usetime == 0) 10900Sstevel@tonic-gate esp_set_usetime(ipsa, B_TRUE); 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate /* 10930Sstevel@tonic-gate * We may wish to check replay in-range-only here as an optimization. 10940Sstevel@tonic-gate * Include the reality check of ipsa->ipsa_replay > 10950Sstevel@tonic-gate * ipsa->ipsa_replay_wsize for times when it's the first N packets, 10960Sstevel@tonic-gate * where N == ipsa->ipsa_replay_wsize. 10970Sstevel@tonic-gate * 10980Sstevel@tonic-gate * Another check that may come here later is the "collision" check. 10990Sstevel@tonic-gate * If legitimate packets flow quickly enough, this won't be a problem, 11000Sstevel@tonic-gate * but collisions may cause authentication algorithm crunching to 11010Sstevel@tonic-gate * take place when it doesn't need to. 11020Sstevel@tonic-gate */ 11030Sstevel@tonic-gate if (!sadb_replay_peek(ipsa, esph->esph_replay)) { 11040Sstevel@tonic-gate ESP_BUMP_STAT(replay_early_failures); 11050Sstevel@tonic-gate IP_ESP_BUMP_STAT(in_discards); 11060Sstevel@tonic-gate /* 11070Sstevel@tonic-gate * TODO: Extract inbound interface from the IPSEC_IN 11080Sstevel@tonic-gate * message's ii->ipsec_in_rill_index. 11090Sstevel@tonic-gate */ 11100Sstevel@tonic-gate ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL, 11110Sstevel@tonic-gate &ipdrops_esp_early_replay, &esp_dropper); 11120Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate /* 11160Sstevel@tonic-gate * Has this packet already been processed by a hardware 11170Sstevel@tonic-gate * IPsec accelerator? 11180Sstevel@tonic-gate */ 11190Sstevel@tonic-gate if (ii->ipsec_in_accelerated) { 11200Sstevel@tonic-gate ipsec_status_t rv; 11210Sstevel@tonic-gate esp3dbg(("esp_inbound: pkt processed by ill=%d isv6=%d\n", 11220Sstevel@tonic-gate ii->ipsec_in_ill_index, !ii->ipsec_in_v4)); 11230Sstevel@tonic-gate rv = esp_inbound_accelerated(ipsec_in_mp, 11240Sstevel@tonic-gate data_mp, ii->ipsec_in_v4, ipsa); 11250Sstevel@tonic-gate return (rv); 11260Sstevel@tonic-gate } 11270Sstevel@tonic-gate ESP_BUMP_STAT(noaccel); 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate /* 11300Sstevel@tonic-gate * Adjust the IP header's payload length to reflect the removal 11310Sstevel@tonic-gate * of the ICV. 11320Sstevel@tonic-gate */ 11330Sstevel@tonic-gate if (!ii->ipsec_in_v4) { 11340Sstevel@tonic-gate ip6_t *ip6h = (ip6_t *)data_mp->b_rptr; 11350Sstevel@tonic-gate ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 11360Sstevel@tonic-gate ipsa->ipsa_mac_len); 11370Sstevel@tonic-gate } else { 11380Sstevel@tonic-gate ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 11390Sstevel@tonic-gate ipha->ipha_length = htons(ntohs(ipha->ipha_length) - 11400Sstevel@tonic-gate ipsa->ipsa_mac_len); 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate /* submit the request to the crypto framework */ 11440Sstevel@tonic-gate return (esp_submit_req_inbound(ipsec_in_mp, ipsa, 11450Sstevel@tonic-gate (uint8_t *)esph - data_mp->b_rptr)); 11460Sstevel@tonic-gate } 11470Sstevel@tonic-gate 11480Sstevel@tonic-gate /* 11490Sstevel@tonic-gate * Perform the really difficult work of inserting the proposed situation. 11500Sstevel@tonic-gate * Called while holding the algorithm lock. 11510Sstevel@tonic-gate */ 11520Sstevel@tonic-gate static void 11530Sstevel@tonic-gate esp_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs) 11540Sstevel@tonic-gate { 11550Sstevel@tonic-gate sadb_comb_t *comb = (sadb_comb_t *)(prop + 1); 11560Sstevel@tonic-gate ipsec_out_t *io; 11570Sstevel@tonic-gate ipsec_action_t *ap; 11580Sstevel@tonic-gate ipsec_prot_t *prot; 11590Sstevel@tonic-gate 11600Sstevel@tonic-gate ASSERT(MUTEX_HELD(&alg_lock)); 11610Sstevel@tonic-gate io = (ipsec_out_t *)acqrec->ipsacq_mp->b_rptr; 11620Sstevel@tonic-gate ASSERT(io->ipsec_out_type == IPSEC_OUT); 11630Sstevel@tonic-gate 11640Sstevel@tonic-gate prop->sadb_prop_exttype = SADB_EXT_PROPOSAL; 11650Sstevel@tonic-gate prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t)); 11660Sstevel@tonic-gate *(uint32_t *)(&prop->sadb_prop_replay) = 0; /* Quick zero-out! */ 11670Sstevel@tonic-gate 11680Sstevel@tonic-gate prop->sadb_prop_replay = ipsecesp_replay_size; 11690Sstevel@tonic-gate 11700Sstevel@tonic-gate /* 11710Sstevel@tonic-gate * Based upon algorithm properties, and what-not, prioritize 11720Sstevel@tonic-gate * a proposal. If the IPSEC_OUT message has an algorithm specified, 11730Sstevel@tonic-gate * use it first and foremost. 11740Sstevel@tonic-gate * 11750Sstevel@tonic-gate * For each action in policy list 11760Sstevel@tonic-gate * Add combination. If I've hit limit, return. 11770Sstevel@tonic-gate */ 11780Sstevel@tonic-gate 11790Sstevel@tonic-gate for (ap = acqrec->ipsacq_act; ap != NULL; 11800Sstevel@tonic-gate ap = ap->ipa_next) { 11810Sstevel@tonic-gate ipsec_alginfo_t *ealg = NULL; 11820Sstevel@tonic-gate ipsec_alginfo_t *aalg = NULL; 11830Sstevel@tonic-gate 11840Sstevel@tonic-gate if (ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY) 11850Sstevel@tonic-gate continue; 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate prot = &ap->ipa_act.ipa_apply; 11880Sstevel@tonic-gate 11890Sstevel@tonic-gate if (!(prot->ipp_use_esp)) 11900Sstevel@tonic-gate continue; 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate if (prot->ipp_esp_auth_alg != 0) { 11930Sstevel@tonic-gate aalg = ipsec_alglists[IPSEC_ALG_AUTH] 11940Sstevel@tonic-gate [prot->ipp_esp_auth_alg]; 11950Sstevel@tonic-gate if (aalg == NULL || !ALG_VALID(aalg)) 11960Sstevel@tonic-gate continue; 11970Sstevel@tonic-gate } 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate ASSERT(prot->ipp_encr_alg > 0); 12000Sstevel@tonic-gate ealg = ipsec_alglists[IPSEC_ALG_ENCR][prot->ipp_encr_alg]; 12010Sstevel@tonic-gate if (ealg == NULL || !ALG_VALID(ealg)) 12020Sstevel@tonic-gate continue; 12030Sstevel@tonic-gate 12040Sstevel@tonic-gate comb->sadb_comb_flags = 0; 12050Sstevel@tonic-gate comb->sadb_comb_reserved = 0; 12060Sstevel@tonic-gate comb->sadb_comb_encrypt = ealg->alg_id; 12070Sstevel@tonic-gate comb->sadb_comb_encrypt_minbits = prot->ipp_espe_minbits; 12080Sstevel@tonic-gate comb->sadb_comb_encrypt_maxbits = prot->ipp_espe_maxbits; 12090Sstevel@tonic-gate if (aalg == NULL) { 12100Sstevel@tonic-gate comb->sadb_comb_auth = 0; 12110Sstevel@tonic-gate comb->sadb_comb_auth_minbits = 0; 12120Sstevel@tonic-gate comb->sadb_comb_auth_maxbits = 0; 12130Sstevel@tonic-gate } else { 12140Sstevel@tonic-gate comb->sadb_comb_auth = aalg->alg_id; 12150Sstevel@tonic-gate comb->sadb_comb_auth_minbits = prot->ipp_espa_minbits; 12160Sstevel@tonic-gate comb->sadb_comb_auth_maxbits = prot->ipp_espa_maxbits; 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate /* 12200Sstevel@tonic-gate * The following may be based on algorithm 12210Sstevel@tonic-gate * properties, but in the meantime, we just pick 12220Sstevel@tonic-gate * some good, sensible numbers. Key mgmt. can 12230Sstevel@tonic-gate * (and perhaps should) be the place to finalize 12240Sstevel@tonic-gate * such decisions. 12250Sstevel@tonic-gate */ 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate /* 12280Sstevel@tonic-gate * No limits on allocations, since we really don't 12290Sstevel@tonic-gate * support that concept currently. 12300Sstevel@tonic-gate */ 12310Sstevel@tonic-gate comb->sadb_comb_soft_allocations = 0; 12320Sstevel@tonic-gate comb->sadb_comb_hard_allocations = 0; 12330Sstevel@tonic-gate 12340Sstevel@tonic-gate /* 12350Sstevel@tonic-gate * These may want to come from policy rule.. 12360Sstevel@tonic-gate */ 12370Sstevel@tonic-gate comb->sadb_comb_soft_bytes = ipsecesp_default_soft_bytes; 12380Sstevel@tonic-gate comb->sadb_comb_hard_bytes = ipsecesp_default_hard_bytes; 12390Sstevel@tonic-gate comb->sadb_comb_soft_addtime = ipsecesp_default_soft_addtime; 12400Sstevel@tonic-gate comb->sadb_comb_hard_addtime = ipsecesp_default_hard_addtime; 12410Sstevel@tonic-gate comb->sadb_comb_soft_usetime = ipsecesp_default_soft_usetime; 12420Sstevel@tonic-gate comb->sadb_comb_hard_usetime = ipsecesp_default_hard_usetime; 12430Sstevel@tonic-gate 12440Sstevel@tonic-gate prop->sadb_prop_len += SADB_8TO64(sizeof (*comb)); 12450Sstevel@tonic-gate if (--combs == 0) 12460Sstevel@tonic-gate break; /* out of space.. */ 12470Sstevel@tonic-gate comb++; 12480Sstevel@tonic-gate } 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate 12510Sstevel@tonic-gate /* 12520Sstevel@tonic-gate * Prepare and actually send the SADB_ACQUIRE message to PF_KEY. 12530Sstevel@tonic-gate */ 12540Sstevel@tonic-gate static void 12550Sstevel@tonic-gate esp_send_acquire(ipsacq_t *acqrec, mblk_t *extended) 12560Sstevel@tonic-gate { 12570Sstevel@tonic-gate mblk_t *pfkeymp, *msgmp; 12580Sstevel@tonic-gate uint_t allocsize, combs; 12590Sstevel@tonic-gate sadb_msg_t *samsg; 12600Sstevel@tonic-gate sadb_prop_t *prop; 12610Sstevel@tonic-gate uint8_t *cur, *end; 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate ESP_BUMP_STAT(acquire_requests); 12640Sstevel@tonic-gate 12650Sstevel@tonic-gate ASSERT(MUTEX_HELD(&acqrec->ipsacq_lock)); 12660Sstevel@tonic-gate 12670Sstevel@tonic-gate pfkeymp = sadb_keysock_out(0); 12680Sstevel@tonic-gate if (pfkeymp == NULL) { 12690Sstevel@tonic-gate esp0dbg(("esp_send_acquire: 1st allocb() failed.\n")); 12700Sstevel@tonic-gate /* Just bail. */ 12710Sstevel@tonic-gate goto done; 12720Sstevel@tonic-gate } 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate /* 12750Sstevel@tonic-gate * First, allocate a basic ACQUIRE message. Beyond that, 12760Sstevel@tonic-gate * you need to extract certificate info from 12770Sstevel@tonic-gate */ 12780Sstevel@tonic-gate allocsize = sizeof (sadb_msg_t) + sizeof (sadb_address_t) + 12790Sstevel@tonic-gate sizeof (sadb_address_t) + sizeof (sadb_prop_t); 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate switch (acqrec->ipsacq_addrfam) { 12820Sstevel@tonic-gate case AF_INET: 12830Sstevel@tonic-gate allocsize += 2 * sizeof (struct sockaddr_in); 12840Sstevel@tonic-gate break; 12850Sstevel@tonic-gate case AF_INET6: 12860Sstevel@tonic-gate allocsize += 2 * sizeof (struct sockaddr_in6); 12870Sstevel@tonic-gate break; 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate mutex_enter(&alg_lock); 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate combs = ipsec_nalgs[IPSEC_ALG_AUTH] * ipsec_nalgs[IPSEC_ALG_ENCR]; 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate allocsize += combs * sizeof (sadb_comb_t); 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate /* 12970Sstevel@tonic-gate * XXX If there are: 12980Sstevel@tonic-gate * certificate IDs 12990Sstevel@tonic-gate * proxy address 13000Sstevel@tonic-gate * <Others> 13010Sstevel@tonic-gate * add additional allocation size. 13020Sstevel@tonic-gate */ 13030Sstevel@tonic-gate 13040Sstevel@tonic-gate msgmp = allocb(allocsize, BPRI_HI); 13050Sstevel@tonic-gate if (msgmp == NULL) { 13060Sstevel@tonic-gate esp0dbg(("esp_send_acquire: 2nd allocb() failed.\n")); 13070Sstevel@tonic-gate /* Just bail. */ 13080Sstevel@tonic-gate freemsg(pfkeymp); 13090Sstevel@tonic-gate pfkeymp = NULL; 13100Sstevel@tonic-gate goto done; 13110Sstevel@tonic-gate } 13120Sstevel@tonic-gate 13130Sstevel@tonic-gate cur = msgmp->b_rptr; 13140Sstevel@tonic-gate end = cur + allocsize; 13150Sstevel@tonic-gate samsg = (sadb_msg_t *)cur; 13160Sstevel@tonic-gate pfkeymp->b_cont = msgmp; 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate /* Set up ACQUIRE. */ 13190Sstevel@tonic-gate cur = sadb_setup_acquire(cur, end, acqrec); 13200Sstevel@tonic-gate if (cur == NULL) { 13210Sstevel@tonic-gate esp0dbg(("sadb_setup_acquire failed.\n")); 13220Sstevel@tonic-gate /* Just bail. */ 13230Sstevel@tonic-gate freemsg(pfkeymp); 13240Sstevel@tonic-gate pfkeymp = NULL; 13250Sstevel@tonic-gate goto done; 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate samsg->sadb_msg_satype = SADB_SATYPE_ESP; 13280Sstevel@tonic-gate 13290Sstevel@tonic-gate /* XXX Insert proxy address information here. */ 13300Sstevel@tonic-gate 13310Sstevel@tonic-gate /* XXX Insert identity information here. */ 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate /* XXXMLS Insert sensitivity information here. */ 13340Sstevel@tonic-gate 13350Sstevel@tonic-gate /* Insert proposal here. */ 13360Sstevel@tonic-gate 13370Sstevel@tonic-gate prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len); 13380Sstevel@tonic-gate esp_insert_prop(prop, acqrec, combs); 13390Sstevel@tonic-gate samsg->sadb_msg_len += prop->sadb_prop_len; 13400Sstevel@tonic-gate msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len); 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate done: 13430Sstevel@tonic-gate mutex_exit(&alg_lock); 13440Sstevel@tonic-gate 13450Sstevel@tonic-gate /* 13460Sstevel@tonic-gate * Must mutex_exit() before sending PF_KEY message up, in 13470Sstevel@tonic-gate * order to avoid recursive mutex_enter() if there are no registered 13480Sstevel@tonic-gate * listeners. 13490Sstevel@tonic-gate * 13500Sstevel@tonic-gate * Once I've sent the message, I'm cool anyway. 13510Sstevel@tonic-gate */ 13520Sstevel@tonic-gate mutex_exit(&acqrec->ipsacq_lock); 13530Sstevel@tonic-gate if (esp_pfkey_q != NULL && pfkeymp != NULL) { 13540Sstevel@tonic-gate if (extended != NULL) { 13550Sstevel@tonic-gate putnext(esp_pfkey_q, extended); 13560Sstevel@tonic-gate } 13570Sstevel@tonic-gate putnext(esp_pfkey_q, pfkeymp); 13580Sstevel@tonic-gate return; 13590Sstevel@tonic-gate } 13600Sstevel@tonic-gate /* XXX freemsg() works for extended == NULL. */ 13610Sstevel@tonic-gate freemsg(extended); 13620Sstevel@tonic-gate freemsg(pfkeymp); 13630Sstevel@tonic-gate } 13640Sstevel@tonic-gate 13650Sstevel@tonic-gate /* 13660Sstevel@tonic-gate * Handle the SADB_GETSPI message. Create a larval SA. 13670Sstevel@tonic-gate */ 13680Sstevel@tonic-gate static void 13690Sstevel@tonic-gate esp_getspi(mblk_t *mp, keysock_in_t *ksi) 13700Sstevel@tonic-gate { 13710Sstevel@tonic-gate ipsa_t *newbie, *target; 13720Sstevel@tonic-gate isaf_t *outbound, *inbound; 13730Sstevel@tonic-gate int rc, diagnostic; 13740Sstevel@tonic-gate sadb_sa_t *assoc; 13750Sstevel@tonic-gate keysock_out_t *kso; 13760Sstevel@tonic-gate uint32_t newspi; 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate /* 13790Sstevel@tonic-gate * Randomly generate a proposed SPI value 13800Sstevel@tonic-gate */ 13810Sstevel@tonic-gate (void) random_get_pseudo_bytes((uint8_t *)&newspi, sizeof (uint32_t)); 13820Sstevel@tonic-gate newbie = sadb_getspi(ksi, newspi, &diagnostic); 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate if (newbie == NULL) { 13850Sstevel@tonic-gate sadb_pfkey_error(esp_pfkey_q, mp, ENOMEM, diagnostic, 13860Sstevel@tonic-gate ksi->ks_in_serial); 13870Sstevel@tonic-gate return; 13880Sstevel@tonic-gate } else if (newbie == (ipsa_t *)-1) { 13890Sstevel@tonic-gate sadb_pfkey_error(esp_pfkey_q, mp, EINVAL, diagnostic, 13900Sstevel@tonic-gate ksi->ks_in_serial); 13910Sstevel@tonic-gate return; 13920Sstevel@tonic-gate } 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate /* 13950Sstevel@tonic-gate * XXX - We may randomly collide. We really should recover from this. 13960Sstevel@tonic-gate * Unfortunately, that could require spending way-too-much-time 13970Sstevel@tonic-gate * in here. For now, let the user retry. 13980Sstevel@tonic-gate */ 13990Sstevel@tonic-gate 14000Sstevel@tonic-gate if (newbie->ipsa_addrfam == AF_INET6) { 1401564Ssommerfe outbound = OUTBOUND_BUCKET_V6(&esp_sadb.s_v6, 1402564Ssommerfe *(uint32_t *)(newbie->ipsa_dstaddr)); 1403564Ssommerfe inbound = INBOUND_BUCKET(&esp_sadb.s_v6, newbie->ipsa_spi); 14040Sstevel@tonic-gate } else { 14050Sstevel@tonic-gate ASSERT(newbie->ipsa_addrfam == AF_INET); 1406564Ssommerfe outbound = OUTBOUND_BUCKET_V4(&esp_sadb.s_v4, 1407564Ssommerfe *(uint32_t *)(newbie->ipsa_dstaddr)); 1408564Ssommerfe inbound = INBOUND_BUCKET(&esp_sadb.s_v4, newbie->ipsa_spi); 14090Sstevel@tonic-gate } 14100Sstevel@tonic-gate 14110Sstevel@tonic-gate mutex_enter(&outbound->isaf_lock); 14120Sstevel@tonic-gate mutex_enter(&inbound->isaf_lock); 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate /* 14150Sstevel@tonic-gate * Check for collisions (i.e. did sadb_getspi() return with something 14160Sstevel@tonic-gate * that already exists?). 14170Sstevel@tonic-gate * 14180Sstevel@tonic-gate * Try outbound first. Even though SADB_GETSPI is traditionally 14190Sstevel@tonic-gate * for inbound SAs, you never know what a user might do. 14200Sstevel@tonic-gate */ 14210Sstevel@tonic-gate target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi, 14220Sstevel@tonic-gate newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam); 14230Sstevel@tonic-gate if (target == NULL) { 14240Sstevel@tonic-gate target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi, 14250Sstevel@tonic-gate newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, 14260Sstevel@tonic-gate newbie->ipsa_addrfam); 14270Sstevel@tonic-gate } 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate /* 14300Sstevel@tonic-gate * I don't have collisions elsewhere! 14310Sstevel@tonic-gate * (Nor will I because I'm still holding inbound/outbound locks.) 14320Sstevel@tonic-gate */ 14330Sstevel@tonic-gate 14340Sstevel@tonic-gate if (target != NULL) { 14350Sstevel@tonic-gate rc = EEXIST; 14360Sstevel@tonic-gate IPSA_REFRELE(target); 14370Sstevel@tonic-gate } else { 14380Sstevel@tonic-gate /* 14390Sstevel@tonic-gate * sadb_insertassoc() also checks for collisions, so 14400Sstevel@tonic-gate * if there's a colliding entry, rc will be set 14410Sstevel@tonic-gate * to EEXIST. 14420Sstevel@tonic-gate */ 14430Sstevel@tonic-gate rc = sadb_insertassoc(newbie, inbound); 14440Sstevel@tonic-gate (void) drv_getparm(TIME, &newbie->ipsa_hardexpiretime); 14450Sstevel@tonic-gate newbie->ipsa_hardexpiretime += ipsecesp_larval_timeout; 14460Sstevel@tonic-gate } 14470Sstevel@tonic-gate 14480Sstevel@tonic-gate /* 14490Sstevel@tonic-gate * Can exit outbound mutex. Hold inbound until we're done 14500Sstevel@tonic-gate * with newbie. 14510Sstevel@tonic-gate */ 14520Sstevel@tonic-gate mutex_exit(&outbound->isaf_lock); 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate if (rc != 0) { 14550Sstevel@tonic-gate mutex_exit(&inbound->isaf_lock); 14560Sstevel@tonic-gate IPSA_REFRELE(newbie); 14570Sstevel@tonic-gate sadb_pfkey_error(esp_pfkey_q, mp, rc, SADB_X_DIAGNOSTIC_NONE, 14580Sstevel@tonic-gate ksi->ks_in_serial); 14590Sstevel@tonic-gate return; 14600Sstevel@tonic-gate } 14610Sstevel@tonic-gate 14620Sstevel@tonic-gate 14630Sstevel@tonic-gate /* Can write here because I'm still holding the bucket lock. */ 14640Sstevel@tonic-gate newbie->ipsa_type = SADB_SATYPE_ESP; 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate /* 14670Sstevel@tonic-gate * Construct successful return message. We have one thing going 14680Sstevel@tonic-gate * for us in PF_KEY v2. That's the fact that 14690Sstevel@tonic-gate * sizeof (sadb_spirange_t) == sizeof (sadb_sa_t) 14700Sstevel@tonic-gate */ 14710Sstevel@tonic-gate assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE]; 14720Sstevel@tonic-gate assoc->sadb_sa_exttype = SADB_EXT_SA; 14730Sstevel@tonic-gate assoc->sadb_sa_spi = newbie->ipsa_spi; 14740Sstevel@tonic-gate *((uint64_t *)(&assoc->sadb_sa_replay)) = 0; 14750Sstevel@tonic-gate mutex_exit(&inbound->isaf_lock); 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate /* Convert KEYSOCK_IN to KEYSOCK_OUT. */ 14780Sstevel@tonic-gate kso = (keysock_out_t *)ksi; 14790Sstevel@tonic-gate kso->ks_out_len = sizeof (*kso); 14800Sstevel@tonic-gate kso->ks_out_serial = ksi->ks_in_serial; 14810Sstevel@tonic-gate kso->ks_out_type = KEYSOCK_OUT; 14820Sstevel@tonic-gate 14830Sstevel@tonic-gate /* 14840Sstevel@tonic-gate * Can safely putnext() to esp_pfkey_q, because this is a turnaround 14850Sstevel@tonic-gate * from the esp_pfkey_q. 14860Sstevel@tonic-gate */ 14870Sstevel@tonic-gate putnext(esp_pfkey_q, mp); 14880Sstevel@tonic-gate } 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate /* 14910Sstevel@tonic-gate * Insert the ESP header into a packet. Duplicate an mblk, and insert a newly 14920Sstevel@tonic-gate * allocated mblk with the ESP header in between the two. 14930Sstevel@tonic-gate */ 14940Sstevel@tonic-gate static boolean_t 14950Sstevel@tonic-gate esp_insert_esp(mblk_t *mp, mblk_t *esp_mp, uint_t divpoint) 14960Sstevel@tonic-gate { 14970Sstevel@tonic-gate mblk_t *split_mp = mp; 14980Sstevel@tonic-gate uint_t wheretodiv = divpoint; 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate while ((split_mp->b_wptr - split_mp->b_rptr) < wheretodiv) { 15010Sstevel@tonic-gate wheretodiv -= (split_mp->b_wptr - split_mp->b_rptr); 15020Sstevel@tonic-gate split_mp = split_mp->b_cont; 15030Sstevel@tonic-gate ASSERT(split_mp != NULL); 15040Sstevel@tonic-gate } 15050Sstevel@tonic-gate 15060Sstevel@tonic-gate if (split_mp->b_wptr - split_mp->b_rptr != wheretodiv) { 15070Sstevel@tonic-gate mblk_t *scratch; 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate /* "scratch" is the 2nd half, split_mp is the first. */ 15100Sstevel@tonic-gate scratch = dupb(split_mp); 15110Sstevel@tonic-gate if (scratch == NULL) { 15120Sstevel@tonic-gate esp1dbg(("esp_insert_esp: can't allocate scratch.\n")); 15130Sstevel@tonic-gate return (B_FALSE); 15140Sstevel@tonic-gate } 15150Sstevel@tonic-gate /* NOTE: dupb() doesn't set b_cont appropriately. */ 15160Sstevel@tonic-gate scratch->b_cont = split_mp->b_cont; 15170Sstevel@tonic-gate scratch->b_rptr += wheretodiv; 15180Sstevel@tonic-gate split_mp->b_wptr = split_mp->b_rptr + wheretodiv; 15190Sstevel@tonic-gate split_mp->b_cont = scratch; 15200Sstevel@tonic-gate } 15210Sstevel@tonic-gate /* 15220Sstevel@tonic-gate * At this point, split_mp is exactly "wheretodiv" bytes long, and 15230Sstevel@tonic-gate * holds the end of the pre-ESP part of the datagram. 15240Sstevel@tonic-gate */ 15250Sstevel@tonic-gate esp_mp->b_cont = split_mp->b_cont; 15260Sstevel@tonic-gate split_mp->b_cont = esp_mp; 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate return (B_TRUE); 15290Sstevel@tonic-gate } 15300Sstevel@tonic-gate 15310Sstevel@tonic-gate /* 15320Sstevel@tonic-gate * Finish processing of an inbound ESP packet after processing by the 15330Sstevel@tonic-gate * crypto framework. 15340Sstevel@tonic-gate * - Remove the ESP header. 15350Sstevel@tonic-gate * - Send packet back to IP. 15360Sstevel@tonic-gate * If authentication was performed on the packet, this function is called 15370Sstevel@tonic-gate * only if the authentication succeeded. 15380Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the 15390Sstevel@tonic-gate * mblk chain ipsec_in_mp. 15400Sstevel@tonic-gate */ 15410Sstevel@tonic-gate static ipsec_status_t 15420Sstevel@tonic-gate esp_in_done(mblk_t *ipsec_in_mp) 15430Sstevel@tonic-gate { 15440Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr; 15450Sstevel@tonic-gate mblk_t *data_mp; 15460Sstevel@tonic-gate ipsa_t *assoc; 15470Sstevel@tonic-gate uint_t espstart; 15480Sstevel@tonic-gate uint32_t ivlen = 0; 15490Sstevel@tonic-gate uint_t processed_len; 15500Sstevel@tonic-gate esph_t *esph; 15510Sstevel@tonic-gate kstat_named_t *counter; 15520Sstevel@tonic-gate boolean_t is_natt; 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate assoc = ii->ipsec_in_esp_sa; 15550Sstevel@tonic-gate ASSERT(assoc != NULL); 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0); 15580Sstevel@tonic-gate 15590Sstevel@tonic-gate /* get the pointer to the ESP header */ 15600Sstevel@tonic-gate if (assoc->ipsa_encr_alg == SADB_EALG_NULL) { 15610Sstevel@tonic-gate /* authentication-only ESP */ 15620Sstevel@tonic-gate espstart = ii->ipsec_in_crypto_data.cd_offset; 15630Sstevel@tonic-gate processed_len = ii->ipsec_in_crypto_data.cd_length; 15640Sstevel@tonic-gate } else { 15650Sstevel@tonic-gate /* encryption present */ 15660Sstevel@tonic-gate ivlen = assoc->ipsa_iv_len; 15670Sstevel@tonic-gate if (assoc->ipsa_auth_alg == SADB_AALG_NONE) { 15680Sstevel@tonic-gate /* encryption-only ESP */ 15690Sstevel@tonic-gate espstart = ii->ipsec_in_crypto_data.cd_offset - 15700Sstevel@tonic-gate sizeof (esph_t) - assoc->ipsa_iv_len; 15710Sstevel@tonic-gate processed_len = ii->ipsec_in_crypto_data.cd_length + 15720Sstevel@tonic-gate ivlen; 15730Sstevel@tonic-gate } else { 15740Sstevel@tonic-gate /* encryption with authentication */ 15750Sstevel@tonic-gate espstart = ii->ipsec_in_crypto_dual_data.dd_offset1; 15760Sstevel@tonic-gate processed_len = ii->ipsec_in_crypto_dual_data.dd_len2 + 15770Sstevel@tonic-gate ivlen; 15780Sstevel@tonic-gate } 15790Sstevel@tonic-gate } 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate data_mp = ipsec_in_mp->b_cont; 15820Sstevel@tonic-gate esph = (esph_t *)(data_mp->b_rptr + espstart); 15830Sstevel@tonic-gate 15840Sstevel@tonic-gate if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) { 15850Sstevel@tonic-gate /* authentication passed if we reach this point */ 15860Sstevel@tonic-gate ESP_BUMP_STAT(good_auth); 15870Sstevel@tonic-gate data_mp->b_wptr -= assoc->ipsa_mac_len; 15880Sstevel@tonic-gate 15890Sstevel@tonic-gate /* 15900Sstevel@tonic-gate * Check replay window here! 15910Sstevel@tonic-gate * For right now, assume keysock will set the replay window 15920Sstevel@tonic-gate * size to zero for SAs that have an unspecified sender. 15930Sstevel@tonic-gate * This may change... 15940Sstevel@tonic-gate */ 15950Sstevel@tonic-gate 15960Sstevel@tonic-gate if (!sadb_replay_check(assoc, esph->esph_replay)) { 15970Sstevel@tonic-gate /* 15980Sstevel@tonic-gate * Log the event. As of now we print out an event. 15990Sstevel@tonic-gate * Do not print the replay failure number, or else 16000Sstevel@tonic-gate * syslog cannot collate the error messages. Printing 16010Sstevel@tonic-gate * the replay number that failed opens a denial-of- 16020Sstevel@tonic-gate * service attack. 16030Sstevel@tonic-gate */ 16040Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, 16050Sstevel@tonic-gate SL_ERROR | SL_WARN, 16060Sstevel@tonic-gate "Replay failed for ESP spi 0x%x, dst %s.\n", 16070Sstevel@tonic-gate assoc->ipsa_spi, assoc->ipsa_dstaddr, 16080Sstevel@tonic-gate assoc->ipsa_addrfam); 16090Sstevel@tonic-gate ESP_BUMP_STAT(replay_failures); 16100Sstevel@tonic-gate counter = &ipdrops_esp_replay; 16110Sstevel@tonic-gate goto drop_and_bail; 16120Sstevel@tonic-gate } 16130Sstevel@tonic-gate } 16140Sstevel@tonic-gate 16150Sstevel@tonic-gate if (!esp_age_bytes(assoc, processed_len, B_TRUE)) { 16160Sstevel@tonic-gate /* The ipsa has hit hard expiration, LOG and AUDIT. */ 16170Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, 16180Sstevel@tonic-gate SL_ERROR | SL_WARN, 16190Sstevel@tonic-gate "ESP association 0x%x, dst %s had bytes expire.\n", 16200Sstevel@tonic-gate assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam); 16210Sstevel@tonic-gate ESP_BUMP_STAT(bytes_expired); 16220Sstevel@tonic-gate counter = &ipdrops_esp_bytes_expire; 16230Sstevel@tonic-gate goto drop_and_bail; 16240Sstevel@tonic-gate } 16250Sstevel@tonic-gate 16260Sstevel@tonic-gate /* 16270Sstevel@tonic-gate * Remove ESP header and padding from packet. I hope the compiler 16280Sstevel@tonic-gate * spews "branch, predict taken" code for this. 16290Sstevel@tonic-gate */ 16300Sstevel@tonic-gate 16310Sstevel@tonic-gate if (esp_strip_header(data_mp, ii->ipsec_in_v4, ivlen, &counter)) { 16320Sstevel@tonic-gate if (is_natt) 16330Sstevel@tonic-gate return (esp_fix_natt_checksums(data_mp, assoc)); 16340Sstevel@tonic-gate return (IPSEC_STATUS_SUCCESS); 16350Sstevel@tonic-gate } 16360Sstevel@tonic-gate 16370Sstevel@tonic-gate esp1dbg(("esp_in_done: esp_strip_header() failed\n")); 16380Sstevel@tonic-gate drop_and_bail: 16390Sstevel@tonic-gate IP_ESP_BUMP_STAT(in_discards); 16400Sstevel@tonic-gate /* 16410Sstevel@tonic-gate * TODO: Extract inbound interface from the IPSEC_IN message's 16420Sstevel@tonic-gate * ii->ipsec_in_rill_index. 16430Sstevel@tonic-gate */ 16440Sstevel@tonic-gate ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL, counter, &esp_dropper); 16450Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate 16480Sstevel@tonic-gate /* 16490Sstevel@tonic-gate * Called upon failing the inbound ICV check. The message passed as 16500Sstevel@tonic-gate * argument is freed. 16510Sstevel@tonic-gate */ 16520Sstevel@tonic-gate static void 16530Sstevel@tonic-gate esp_log_bad_auth(mblk_t *ipsec_in) 16540Sstevel@tonic-gate { 16550Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr; 16560Sstevel@tonic-gate ipsa_t *assoc = ii->ipsec_in_esp_sa; 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate /* 16590Sstevel@tonic-gate * Log the event. Don't print to the console, block 16600Sstevel@tonic-gate * potential denial-of-service attack. 16610Sstevel@tonic-gate */ 16620Sstevel@tonic-gate ESP_BUMP_STAT(bad_auth); 16630Sstevel@tonic-gate 16640Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 16650Sstevel@tonic-gate "ESP Authentication failed for spi 0x%x, dst %s.\n", 16660Sstevel@tonic-gate assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam); 16670Sstevel@tonic-gate 16680Sstevel@tonic-gate IP_ESP_BUMP_STAT(in_discards); 16690Sstevel@tonic-gate /* 16700Sstevel@tonic-gate * TODO: Extract inbound interface from the IPSEC_IN 16710Sstevel@tonic-gate * message's ii->ipsec_in_rill_index. 16720Sstevel@tonic-gate */ 16730Sstevel@tonic-gate ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, &ipdrops_esp_bad_auth, 16740Sstevel@tonic-gate &esp_dropper); 16750Sstevel@tonic-gate } 16760Sstevel@tonic-gate 16770Sstevel@tonic-gate 16780Sstevel@tonic-gate /* 16790Sstevel@tonic-gate * Invoked for outbound packets after ESP processing. If the packet 16800Sstevel@tonic-gate * also requires AH, performs the AH SA selection and AH processing. 16810Sstevel@tonic-gate * Returns B_TRUE if the AH processing was not needed or if it was 16820Sstevel@tonic-gate * performed successfully. Returns B_FALSE and consumes the passed mblk 16830Sstevel@tonic-gate * if AH processing was required but could not be performed. 16840Sstevel@tonic-gate */ 16850Sstevel@tonic-gate static boolean_t 16860Sstevel@tonic-gate esp_do_outbound_ah(mblk_t *ipsec_mp) 16870Sstevel@tonic-gate { 16880Sstevel@tonic-gate ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 16890Sstevel@tonic-gate ipsec_status_t ipsec_rc; 16900Sstevel@tonic-gate ipsec_action_t *ap; 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate ap = io->ipsec_out_act; 16930Sstevel@tonic-gate if (ap == NULL) { 16940Sstevel@tonic-gate ipsec_policy_t *pp = io->ipsec_out_policy; 16950Sstevel@tonic-gate ap = pp->ipsp_act; 16960Sstevel@tonic-gate } 16970Sstevel@tonic-gate 16980Sstevel@tonic-gate if (!ap->ipa_want_ah) 16990Sstevel@tonic-gate return (B_TRUE); 17000Sstevel@tonic-gate 17010Sstevel@tonic-gate ASSERT(io->ipsec_out_ah_done == B_FALSE); 17020Sstevel@tonic-gate 17030Sstevel@tonic-gate if (io->ipsec_out_ah_sa == NULL) { 17040Sstevel@tonic-gate if (!ipsec_outbound_sa(ipsec_mp, IPPROTO_AH)) { 17050Sstevel@tonic-gate sadb_acquire(ipsec_mp, io, B_TRUE, B_FALSE); 17060Sstevel@tonic-gate return (B_FALSE); 17070Sstevel@tonic-gate } 17080Sstevel@tonic-gate } 17090Sstevel@tonic-gate ASSERT(io->ipsec_out_ah_sa != NULL); 17100Sstevel@tonic-gate 17110Sstevel@tonic-gate io->ipsec_out_ah_done = B_TRUE; 17120Sstevel@tonic-gate ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp); 17130Sstevel@tonic-gate return (ipsec_rc == IPSEC_STATUS_SUCCESS); 17140Sstevel@tonic-gate } 17150Sstevel@tonic-gate 17160Sstevel@tonic-gate 17170Sstevel@tonic-gate /* 17180Sstevel@tonic-gate * Kernel crypto framework callback invoked after completion of async 17190Sstevel@tonic-gate * crypto requests. 17200Sstevel@tonic-gate */ 17210Sstevel@tonic-gate static void 17220Sstevel@tonic-gate esp_kcf_callback(void *arg, int status) 17230Sstevel@tonic-gate { 17240Sstevel@tonic-gate mblk_t *ipsec_mp = (mblk_t *)arg; 17250Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 17260Sstevel@tonic-gate boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN); 17270Sstevel@tonic-gate 17280Sstevel@tonic-gate ASSERT(ipsec_mp->b_cont != NULL); 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate if (status == CRYPTO_SUCCESS) { 17310Sstevel@tonic-gate if (is_inbound) { 17320Sstevel@tonic-gate if (esp_in_done(ipsec_mp) != IPSEC_STATUS_SUCCESS) 17330Sstevel@tonic-gate return; 17340Sstevel@tonic-gate 17350Sstevel@tonic-gate /* finish IPsec processing */ 17360Sstevel@tonic-gate ip_fanout_proto_again(ipsec_mp, NULL, NULL, NULL); 17370Sstevel@tonic-gate } else { 17380Sstevel@tonic-gate /* 17390Sstevel@tonic-gate * If a ICV was computed, it was stored by the 17400Sstevel@tonic-gate * crypto framework at the end of the packet. 17410Sstevel@tonic-gate */ 17420Sstevel@tonic-gate ipha_t *ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 17430Sstevel@tonic-gate 17440Sstevel@tonic-gate /* do AH processing if needed */ 17450Sstevel@tonic-gate if (!esp_do_outbound_ah(ipsec_mp)) 17460Sstevel@tonic-gate return; 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate /* finish IPsec processing */ 17490Sstevel@tonic-gate if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 17500Sstevel@tonic-gate ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL, 17510Sstevel@tonic-gate NULL); 17520Sstevel@tonic-gate } else { 17530Sstevel@tonic-gate ip6_t *ip6h = (ip6_t *)ipha; 17540Sstevel@tonic-gate ip_wput_ipsec_out_v6(NULL, ipsec_mp, ip6h, 17550Sstevel@tonic-gate NULL, NULL); 17560Sstevel@tonic-gate } 17570Sstevel@tonic-gate } 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate } else if (status == CRYPTO_INVALID_MAC) { 17600Sstevel@tonic-gate esp_log_bad_auth(ipsec_mp); 17610Sstevel@tonic-gate 17620Sstevel@tonic-gate } else { 17630Sstevel@tonic-gate esp1dbg(("esp_kcf_callback: crypto failed with 0x%x\n", 17640Sstevel@tonic-gate status)); 17650Sstevel@tonic-gate ESP_BUMP_STAT(crypto_failures); 17660Sstevel@tonic-gate if (is_inbound) 17670Sstevel@tonic-gate IP_ESP_BUMP_STAT(in_discards); 17680Sstevel@tonic-gate else 17690Sstevel@tonic-gate ESP_BUMP_STAT(out_discards); 17700Sstevel@tonic-gate ip_drop_packet(ipsec_mp, is_inbound, NULL, NULL, 17710Sstevel@tonic-gate &ipdrops_esp_crypto_failed, &esp_dropper); 17720Sstevel@tonic-gate } 17730Sstevel@tonic-gate } 17740Sstevel@tonic-gate 17750Sstevel@tonic-gate /* 17760Sstevel@tonic-gate * Invoked on crypto framework failure during inbound and outbound processing. 17770Sstevel@tonic-gate */ 17780Sstevel@tonic-gate static void 17790Sstevel@tonic-gate esp_crypto_failed(mblk_t *mp, boolean_t is_inbound, int kef_rc) 17800Sstevel@tonic-gate { 17810Sstevel@tonic-gate esp1dbg(("crypto failed for %s ESP with 0x%x\n", 17820Sstevel@tonic-gate is_inbound ? "inbound" : "outbound", kef_rc)); 17830Sstevel@tonic-gate ip_drop_packet(mp, is_inbound, NULL, NULL, &ipdrops_esp_crypto_failed, 17840Sstevel@tonic-gate &esp_dropper); 17850Sstevel@tonic-gate ESP_BUMP_STAT(crypto_failures); 17860Sstevel@tonic-gate if (is_inbound) 17870Sstevel@tonic-gate IP_ESP_BUMP_STAT(in_discards); 17880Sstevel@tonic-gate else 17890Sstevel@tonic-gate ESP_BUMP_STAT(out_discards); 17900Sstevel@tonic-gate } 17910Sstevel@tonic-gate 17920Sstevel@tonic-gate #define ESP_INIT_CALLREQ(_cr) { \ 17930Sstevel@tonic-gate (_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_RESTRICTED; \ 17940Sstevel@tonic-gate (_cr)->cr_callback_arg = ipsec_mp; \ 17950Sstevel@tonic-gate (_cr)->cr_callback_func = esp_kcf_callback; \ 17960Sstevel@tonic-gate } 17970Sstevel@tonic-gate 17980Sstevel@tonic-gate #define ESP_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) { \ 17990Sstevel@tonic-gate (mac)->cd_format = CRYPTO_DATA_RAW; \ 18000Sstevel@tonic-gate (mac)->cd_offset = 0; \ 18010Sstevel@tonic-gate (mac)->cd_length = icvlen; \ 18020Sstevel@tonic-gate (mac)->cd_raw.iov_base = (char *)icvbuf; \ 18030Sstevel@tonic-gate (mac)->cd_raw.iov_len = icvlen; \ 18040Sstevel@tonic-gate } 18050Sstevel@tonic-gate 18060Sstevel@tonic-gate #define ESP_INIT_CRYPTO_DATA(data, mp, off, len) { \ 18070Sstevel@tonic-gate if (MBLKL(mp) >= (len) + (off)) { \ 18080Sstevel@tonic-gate (data)->cd_format = CRYPTO_DATA_RAW; \ 18090Sstevel@tonic-gate (data)->cd_raw.iov_base = (char *)(mp)->b_rptr; \ 18100Sstevel@tonic-gate (data)->cd_raw.iov_len = MBLKL(mp); \ 18110Sstevel@tonic-gate (data)->cd_offset = off; \ 18120Sstevel@tonic-gate } else { \ 18130Sstevel@tonic-gate (data)->cd_format = CRYPTO_DATA_MBLK; \ 18140Sstevel@tonic-gate (data)->cd_mp = mp; \ 18150Sstevel@tonic-gate (data)->cd_offset = off; \ 18160Sstevel@tonic-gate } \ 18170Sstevel@tonic-gate (data)->cd_length = len; \ 18180Sstevel@tonic-gate } 18190Sstevel@tonic-gate 18200Sstevel@tonic-gate #define ESP_INIT_CRYPTO_DUAL_DATA(data, mp, off1, len1, off2, len2) { \ 18210Sstevel@tonic-gate (data)->dd_format = CRYPTO_DATA_MBLK; \ 18220Sstevel@tonic-gate (data)->dd_mp = mp; \ 18230Sstevel@tonic-gate (data)->dd_len1 = len1; \ 18240Sstevel@tonic-gate (data)->dd_offset1 = off1; \ 18250Sstevel@tonic-gate (data)->dd_len2 = len2; \ 18260Sstevel@tonic-gate (data)->dd_offset2 = off2; \ 18270Sstevel@tonic-gate } 18280Sstevel@tonic-gate 18290Sstevel@tonic-gate static ipsec_status_t 18300Sstevel@tonic-gate esp_submit_req_inbound(mblk_t *ipsec_mp, ipsa_t *assoc, uint_t esph_offset) 18310Sstevel@tonic-gate { 18320Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 18330Sstevel@tonic-gate boolean_t do_auth; 18340Sstevel@tonic-gate uint_t auth_offset, msg_len, auth_len; 18350Sstevel@tonic-gate crypto_call_req_t call_req; 18360Sstevel@tonic-gate mblk_t *esp_mp; 18370Sstevel@tonic-gate int kef_rc = CRYPTO_FAILED; 18380Sstevel@tonic-gate uint_t icv_len = assoc->ipsa_mac_len; 18390Sstevel@tonic-gate crypto_ctx_template_t auth_ctx_tmpl; 18400Sstevel@tonic-gate boolean_t do_encr; 18410Sstevel@tonic-gate uint_t encr_offset, encr_len; 18420Sstevel@tonic-gate uint_t iv_len = assoc->ipsa_iv_len; 18430Sstevel@tonic-gate crypto_ctx_template_t encr_ctx_tmpl; 18440Sstevel@tonic-gate 18450Sstevel@tonic-gate ASSERT(ii->ipsec_in_type == IPSEC_IN); 18460Sstevel@tonic-gate 18470Sstevel@tonic-gate do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE; 18480Sstevel@tonic-gate do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL; 18490Sstevel@tonic-gate 18500Sstevel@tonic-gate /* 18510Sstevel@tonic-gate * An inbound packet is of the form: 18520Sstevel@tonic-gate * IPSEC_IN -> [IP,options,ESP,IV,data,ICV,pad] 18530Sstevel@tonic-gate */ 18540Sstevel@tonic-gate esp_mp = ipsec_mp->b_cont; 18550Sstevel@tonic-gate msg_len = MBLKL(esp_mp); 18560Sstevel@tonic-gate 18570Sstevel@tonic-gate ESP_INIT_CALLREQ(&call_req); 18580Sstevel@tonic-gate 18590Sstevel@tonic-gate if (do_auth) { 18600Sstevel@tonic-gate /* force asynchronous processing? */ 18610Sstevel@tonic-gate if (ipsec_algs_exec_mode[IPSEC_ALG_AUTH] == 18620Sstevel@tonic-gate IPSEC_ALGS_EXEC_ASYNC) 18630Sstevel@tonic-gate call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE; 18640Sstevel@tonic-gate 18650Sstevel@tonic-gate /* authentication context template */ 18660Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH, 18670Sstevel@tonic-gate auth_ctx_tmpl); 18680Sstevel@tonic-gate 18690Sstevel@tonic-gate /* ICV to be verified */ 18700Sstevel@tonic-gate ESP_INIT_CRYPTO_MAC(&ii->ipsec_in_crypto_mac, 18710Sstevel@tonic-gate icv_len, esp_mp->b_wptr - icv_len); 18720Sstevel@tonic-gate 18730Sstevel@tonic-gate /* authentication starts at the ESP header */ 18740Sstevel@tonic-gate auth_offset = esph_offset; 18750Sstevel@tonic-gate auth_len = msg_len - auth_offset - icv_len; 18760Sstevel@tonic-gate if (!do_encr) { 18770Sstevel@tonic-gate /* authentication only */ 18780Sstevel@tonic-gate /* initialize input data argument */ 18790Sstevel@tonic-gate ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data, 18800Sstevel@tonic-gate esp_mp, auth_offset, auth_len); 18810Sstevel@tonic-gate 18820Sstevel@tonic-gate /* call the crypto framework */ 18830Sstevel@tonic-gate kef_rc = crypto_mac_verify(&assoc->ipsa_amech, 18840Sstevel@tonic-gate &ii->ipsec_in_crypto_data, 18850Sstevel@tonic-gate &assoc->ipsa_kcfauthkey, auth_ctx_tmpl, 18860Sstevel@tonic-gate &ii->ipsec_in_crypto_mac, &call_req); 18870Sstevel@tonic-gate } 18880Sstevel@tonic-gate } 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate if (do_encr) { 18910Sstevel@tonic-gate /* force asynchronous processing? */ 18920Sstevel@tonic-gate if (ipsec_algs_exec_mode[IPSEC_ALG_ENCR] == 18930Sstevel@tonic-gate IPSEC_ALGS_EXEC_ASYNC) 18940Sstevel@tonic-gate call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE; 18950Sstevel@tonic-gate 18960Sstevel@tonic-gate /* encryption template */ 18970Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR, 18980Sstevel@tonic-gate encr_ctx_tmpl); 18990Sstevel@tonic-gate 19000Sstevel@tonic-gate /* skip IV, since it is passed separately */ 19010Sstevel@tonic-gate encr_offset = esph_offset + sizeof (esph_t) + iv_len; 19020Sstevel@tonic-gate encr_len = msg_len - encr_offset; 19030Sstevel@tonic-gate 19040Sstevel@tonic-gate if (!do_auth) { 19050Sstevel@tonic-gate /* decryption only */ 19060Sstevel@tonic-gate /* initialize input data argument */ 19070Sstevel@tonic-gate ESP_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data, 19080Sstevel@tonic-gate esp_mp, encr_offset, encr_len); 19090Sstevel@tonic-gate 19100Sstevel@tonic-gate /* specify IV */ 19110Sstevel@tonic-gate ii->ipsec_in_crypto_data.cd_miscdata = 19120Sstevel@tonic-gate (char *)esp_mp->b_rptr + sizeof (esph_t) + 19130Sstevel@tonic-gate esph_offset; 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate /* call the crypto framework */ 19160Sstevel@tonic-gate kef_rc = crypto_decrypt(&assoc->ipsa_emech, 19170Sstevel@tonic-gate &ii->ipsec_in_crypto_data, 19180Sstevel@tonic-gate &assoc->ipsa_kcfencrkey, encr_ctx_tmpl, 19190Sstevel@tonic-gate NULL, &call_req); 19200Sstevel@tonic-gate } 19210Sstevel@tonic-gate } 19220Sstevel@tonic-gate 19230Sstevel@tonic-gate if (do_auth && do_encr) { 19240Sstevel@tonic-gate /* dual operation */ 19250Sstevel@tonic-gate /* initialize input data argument */ 19260Sstevel@tonic-gate ESP_INIT_CRYPTO_DUAL_DATA(&ii->ipsec_in_crypto_dual_data, 19270Sstevel@tonic-gate esp_mp, auth_offset, auth_len, 19280Sstevel@tonic-gate encr_offset, encr_len - icv_len); 19290Sstevel@tonic-gate 19300Sstevel@tonic-gate /* specify IV */ 19310Sstevel@tonic-gate ii->ipsec_in_crypto_dual_data.dd_miscdata = 19320Sstevel@tonic-gate (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset; 19330Sstevel@tonic-gate 19340Sstevel@tonic-gate /* call the framework */ 19350Sstevel@tonic-gate kef_rc = crypto_mac_verify_decrypt(&assoc->ipsa_amech, 19360Sstevel@tonic-gate &assoc->ipsa_emech, &ii->ipsec_in_crypto_dual_data, 19370Sstevel@tonic-gate &assoc->ipsa_kcfauthkey, &assoc->ipsa_kcfencrkey, 19380Sstevel@tonic-gate auth_ctx_tmpl, encr_ctx_tmpl, &ii->ipsec_in_crypto_mac, 19390Sstevel@tonic-gate NULL, &call_req); 19400Sstevel@tonic-gate } 19410Sstevel@tonic-gate 19420Sstevel@tonic-gate switch (kef_rc) { 19430Sstevel@tonic-gate case CRYPTO_SUCCESS: 19440Sstevel@tonic-gate ESP_BUMP_STAT(crypto_sync); 19450Sstevel@tonic-gate return (esp_in_done(ipsec_mp)); 19460Sstevel@tonic-gate case CRYPTO_QUEUED: 19470Sstevel@tonic-gate /* esp_kcf_callback() will be invoked on completion */ 19480Sstevel@tonic-gate ESP_BUMP_STAT(crypto_async); 19490Sstevel@tonic-gate return (IPSEC_STATUS_PENDING); 19500Sstevel@tonic-gate case CRYPTO_INVALID_MAC: 19510Sstevel@tonic-gate ESP_BUMP_STAT(crypto_sync); 19520Sstevel@tonic-gate esp_log_bad_auth(ipsec_mp); 19530Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 19540Sstevel@tonic-gate } 19550Sstevel@tonic-gate 19560Sstevel@tonic-gate esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc); 19570Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 19580Sstevel@tonic-gate } 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate static ipsec_status_t 19610Sstevel@tonic-gate esp_submit_req_outbound(mblk_t *ipsec_mp, ipsa_t *assoc, uchar_t *icv_buf, 19620Sstevel@tonic-gate uint_t payload_len) 19630Sstevel@tonic-gate { 19640Sstevel@tonic-gate ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 19650Sstevel@tonic-gate uint_t auth_len; 19660Sstevel@tonic-gate crypto_call_req_t call_req; 19670Sstevel@tonic-gate mblk_t *esp_mp; 19680Sstevel@tonic-gate int kef_rc = CRYPTO_FAILED; 19690Sstevel@tonic-gate uint_t icv_len = assoc->ipsa_mac_len; 19700Sstevel@tonic-gate crypto_ctx_template_t auth_ctx_tmpl; 19710Sstevel@tonic-gate boolean_t do_auth; 19720Sstevel@tonic-gate boolean_t do_encr; 19730Sstevel@tonic-gate uint_t iv_len = assoc->ipsa_iv_len; 19740Sstevel@tonic-gate crypto_ctx_template_t encr_ctx_tmpl; 19750Sstevel@tonic-gate boolean_t is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0); 19760Sstevel@tonic-gate size_t esph_offset = (is_natt ? UDPH_SIZE : 0); 19770Sstevel@tonic-gate 19780Sstevel@tonic-gate esp3dbg(("esp_submit_req_outbound:%s", is_natt ? "natt" : "not natt")); 19790Sstevel@tonic-gate 19800Sstevel@tonic-gate ASSERT(io->ipsec_out_type == IPSEC_OUT); 19810Sstevel@tonic-gate 19820Sstevel@tonic-gate do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL; 19830Sstevel@tonic-gate do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE; 19840Sstevel@tonic-gate 19850Sstevel@tonic-gate /* 19860Sstevel@tonic-gate * Outbound IPsec packets are of the form: 19870Sstevel@tonic-gate * IPSEC_OUT -> [IP,options] -> [ESP,IV] -> [data] -> [pad,ICV] 19880Sstevel@tonic-gate * unless it's NATT, then it's 19890Sstevel@tonic-gate * IPSEC_OUT -> [IP,options] -> [udp][ESP,IV] -> [data] -> [pad,ICV] 19900Sstevel@tonic-gate * Get a pointer to the mblk containing the ESP header. 19910Sstevel@tonic-gate */ 19920Sstevel@tonic-gate ASSERT(ipsec_mp->b_cont != NULL && ipsec_mp->b_cont->b_cont != NULL); 19930Sstevel@tonic-gate esp_mp = ipsec_mp->b_cont->b_cont; 19940Sstevel@tonic-gate 19950Sstevel@tonic-gate ESP_INIT_CALLREQ(&call_req); 19960Sstevel@tonic-gate 19970Sstevel@tonic-gate if (do_auth) { 19980Sstevel@tonic-gate /* force asynchronous processing? */ 19990Sstevel@tonic-gate if (ipsec_algs_exec_mode[IPSEC_ALG_AUTH] == 20000Sstevel@tonic-gate IPSEC_ALGS_EXEC_ASYNC) 20010Sstevel@tonic-gate call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE; 20020Sstevel@tonic-gate 20030Sstevel@tonic-gate /* authentication context template */ 20040Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH, 20050Sstevel@tonic-gate auth_ctx_tmpl); 20060Sstevel@tonic-gate 20070Sstevel@tonic-gate /* where to store the computed mac */ 20080Sstevel@tonic-gate ESP_INIT_CRYPTO_MAC(&io->ipsec_out_crypto_mac, 20090Sstevel@tonic-gate icv_len, icv_buf); 20100Sstevel@tonic-gate 20110Sstevel@tonic-gate /* authentication starts at the ESP header */ 2012134Sdanmcd auth_len = payload_len + iv_len + sizeof (esph_t); 20130Sstevel@tonic-gate if (!do_encr) { 20140Sstevel@tonic-gate /* authentication only */ 20150Sstevel@tonic-gate /* initialize input data argument */ 20160Sstevel@tonic-gate ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data, 20170Sstevel@tonic-gate esp_mp, esph_offset, auth_len); 20180Sstevel@tonic-gate 20190Sstevel@tonic-gate /* call the crypto framework */ 20200Sstevel@tonic-gate kef_rc = crypto_mac(&assoc->ipsa_amech, 20210Sstevel@tonic-gate &io->ipsec_out_crypto_data, 20220Sstevel@tonic-gate &assoc->ipsa_kcfauthkey, auth_ctx_tmpl, 20230Sstevel@tonic-gate &io->ipsec_out_crypto_mac, &call_req); 20240Sstevel@tonic-gate } 20250Sstevel@tonic-gate } 20260Sstevel@tonic-gate 20270Sstevel@tonic-gate if (do_encr) { 20280Sstevel@tonic-gate /* force asynchronous processing? */ 20290Sstevel@tonic-gate if (ipsec_algs_exec_mode[IPSEC_ALG_ENCR] == 20300Sstevel@tonic-gate IPSEC_ALGS_EXEC_ASYNC) 20310Sstevel@tonic-gate call_req.cr_flag |= CRYPTO_ALWAYS_QUEUE; 20320Sstevel@tonic-gate 20330Sstevel@tonic-gate /* encryption context template */ 20340Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR, 20350Sstevel@tonic-gate encr_ctx_tmpl); 20360Sstevel@tonic-gate 20370Sstevel@tonic-gate if (!do_auth) { 20380Sstevel@tonic-gate /* encryption only, skip mblk that contains ESP hdr */ 20390Sstevel@tonic-gate /* initialize input data argument */ 20400Sstevel@tonic-gate ESP_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data, 20410Sstevel@tonic-gate esp_mp->b_cont, 0, payload_len); 20420Sstevel@tonic-gate 20430Sstevel@tonic-gate /* specify IV */ 20440Sstevel@tonic-gate io->ipsec_out_crypto_data.cd_miscdata = 20450Sstevel@tonic-gate (char *)esp_mp->b_rptr + sizeof (esph_t) + 20460Sstevel@tonic-gate esph_offset; 20470Sstevel@tonic-gate 20480Sstevel@tonic-gate /* call the crypto framework */ 20490Sstevel@tonic-gate kef_rc = crypto_encrypt(&assoc->ipsa_emech, 20500Sstevel@tonic-gate &io->ipsec_out_crypto_data, 20510Sstevel@tonic-gate &assoc->ipsa_kcfencrkey, encr_ctx_tmpl, 20520Sstevel@tonic-gate NULL, &call_req); 20530Sstevel@tonic-gate } 20540Sstevel@tonic-gate } 20550Sstevel@tonic-gate 20560Sstevel@tonic-gate if (do_auth && do_encr) { 20570Sstevel@tonic-gate /* 20580Sstevel@tonic-gate * Encryption and authentication: 20590Sstevel@tonic-gate * Pass the pointer to the mblk chain starting at the ESP 20600Sstevel@tonic-gate * header to the framework. Skip the ESP header mblk 20610Sstevel@tonic-gate * for encryption, which is reflected by an encryption 20620Sstevel@tonic-gate * offset equal to the length of that mblk. Start 20630Sstevel@tonic-gate * the authentication at the ESP header, i.e. use an 20640Sstevel@tonic-gate * authentication offset of zero. 20650Sstevel@tonic-gate */ 20660Sstevel@tonic-gate ESP_INIT_CRYPTO_DUAL_DATA(&io->ipsec_out_crypto_dual_data, 20670Sstevel@tonic-gate esp_mp, MBLKL(esp_mp), payload_len, esph_offset, auth_len); 20680Sstevel@tonic-gate 20690Sstevel@tonic-gate /* specify IV */ 20700Sstevel@tonic-gate io->ipsec_out_crypto_dual_data.dd_miscdata = 20710Sstevel@tonic-gate (char *)esp_mp->b_rptr + sizeof (esph_t) + esph_offset; 20720Sstevel@tonic-gate 20730Sstevel@tonic-gate /* call the framework */ 20740Sstevel@tonic-gate kef_rc = crypto_encrypt_mac(&assoc->ipsa_emech, 20750Sstevel@tonic-gate &assoc->ipsa_amech, NULL, 20760Sstevel@tonic-gate &assoc->ipsa_kcfencrkey, &assoc->ipsa_kcfauthkey, 20770Sstevel@tonic-gate encr_ctx_tmpl, auth_ctx_tmpl, 20780Sstevel@tonic-gate &io->ipsec_out_crypto_dual_data, 20790Sstevel@tonic-gate &io->ipsec_out_crypto_mac, &call_req); 20800Sstevel@tonic-gate } 20810Sstevel@tonic-gate 20820Sstevel@tonic-gate switch (kef_rc) { 20830Sstevel@tonic-gate case CRYPTO_SUCCESS: 20840Sstevel@tonic-gate ESP_BUMP_STAT(crypto_sync); 20850Sstevel@tonic-gate return (IPSEC_STATUS_SUCCESS); 20860Sstevel@tonic-gate case CRYPTO_QUEUED: 20870Sstevel@tonic-gate /* esp_kcf_callback() will be invoked on completion */ 20880Sstevel@tonic-gate ESP_BUMP_STAT(crypto_async); 20890Sstevel@tonic-gate return (IPSEC_STATUS_PENDING); 20900Sstevel@tonic-gate } 20910Sstevel@tonic-gate 20920Sstevel@tonic-gate esp_crypto_failed(ipsec_mp, B_TRUE, kef_rc); 20930Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 20940Sstevel@tonic-gate } 20950Sstevel@tonic-gate 20960Sstevel@tonic-gate /* 20970Sstevel@tonic-gate * Handle outbound IPsec processing for IPv4 and IPv6 20980Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the 20990Sstevel@tonic-gate * mblk chain ipsec_in_mp. 21000Sstevel@tonic-gate */ 21010Sstevel@tonic-gate static ipsec_status_t 21020Sstevel@tonic-gate esp_outbound(mblk_t *mp) 21030Sstevel@tonic-gate { 21040Sstevel@tonic-gate mblk_t *ipsec_out_mp, *data_mp, *espmp, *tailmp; 21050Sstevel@tonic-gate ipsec_out_t *io; 21060Sstevel@tonic-gate ipha_t *ipha; 21070Sstevel@tonic-gate ip6_t *ip6h; 21080Sstevel@tonic-gate esph_t *esph; 21090Sstevel@tonic-gate uint_t af; 21100Sstevel@tonic-gate uint8_t *nhp; 21110Sstevel@tonic-gate uintptr_t divpoint, datalen, adj, padlen, i, alloclen; 21120Sstevel@tonic-gate uintptr_t esplen = sizeof (esph_t); 21130Sstevel@tonic-gate uint8_t protocol; 21140Sstevel@tonic-gate ipsa_t *assoc; 21150Sstevel@tonic-gate uint_t iv_len = 0, mac_len = 0; 21160Sstevel@tonic-gate uchar_t *icv_buf; 21170Sstevel@tonic-gate udpha_t *udpha; 21180Sstevel@tonic-gate boolean_t is_natt = B_FALSE; 21190Sstevel@tonic-gate 21200Sstevel@tonic-gate ESP_BUMP_STAT(out_requests); 21210Sstevel@tonic-gate 21220Sstevel@tonic-gate ipsec_out_mp = mp; 21230Sstevel@tonic-gate data_mp = ipsec_out_mp->b_cont; 21240Sstevel@tonic-gate 21250Sstevel@tonic-gate /* 21260Sstevel@tonic-gate * <sigh> We have to copy the message here, because TCP (for example) 21270Sstevel@tonic-gate * keeps a dupb() of the message lying around for retransmission. 21280Sstevel@tonic-gate * Since ESP changes the whole of the datagram, we have to create our 21290Sstevel@tonic-gate * own copy lest we clobber TCP's data. Since we have to copy anyway, 21300Sstevel@tonic-gate * we might as well make use of msgpullup() and get the mblk into one 21310Sstevel@tonic-gate * contiguous piece! 21320Sstevel@tonic-gate */ 21330Sstevel@tonic-gate ipsec_out_mp->b_cont = msgpullup(data_mp, -1); 21340Sstevel@tonic-gate if (ipsec_out_mp->b_cont == NULL) { 21350Sstevel@tonic-gate esp0dbg(("esp_outbound: msgpullup() failed, " 21360Sstevel@tonic-gate "dropping packet.\n")); 21370Sstevel@tonic-gate ipsec_out_mp->b_cont = data_mp; 21380Sstevel@tonic-gate /* 21390Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 21400Sstevel@tonic-gate * pass it to ip_drop_packet(). 21410Sstevel@tonic-gate */ 21420Sstevel@tonic-gate ip_drop_packet(ipsec_out_mp, B_FALSE, NULL, NULL, 21430Sstevel@tonic-gate &ipdrops_esp_nomem, &esp_dropper); 21440Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 21450Sstevel@tonic-gate } else { 21460Sstevel@tonic-gate freemsg(data_mp); 21470Sstevel@tonic-gate data_mp = ipsec_out_mp->b_cont; 21480Sstevel@tonic-gate } 21490Sstevel@tonic-gate 21500Sstevel@tonic-gate io = (ipsec_out_t *)ipsec_out_mp->b_rptr; 21510Sstevel@tonic-gate 21520Sstevel@tonic-gate /* 21530Sstevel@tonic-gate * Reality check.... 21540Sstevel@tonic-gate */ 21550Sstevel@tonic-gate 21560Sstevel@tonic-gate ipha = (ipha_t *)data_mp->b_rptr; /* So we can call esp_acquire(). */ 21570Sstevel@tonic-gate 21580Sstevel@tonic-gate if (io->ipsec_out_v4) { 21590Sstevel@tonic-gate af = AF_INET; 21600Sstevel@tonic-gate divpoint = IPH_HDR_LENGTH(ipha); 21610Sstevel@tonic-gate datalen = ntohs(ipha->ipha_length) - divpoint; 21620Sstevel@tonic-gate nhp = (uint8_t *)&ipha->ipha_protocol; 21630Sstevel@tonic-gate } else { 21640Sstevel@tonic-gate ip6_pkt_t ipp; 21650Sstevel@tonic-gate 21660Sstevel@tonic-gate af = AF_INET6; 21670Sstevel@tonic-gate ip6h = (ip6_t *)ipha; 21680Sstevel@tonic-gate bzero(&ipp, sizeof (ipp)); 21690Sstevel@tonic-gate divpoint = ip_find_hdr_v6(data_mp, ip6h, &ipp, NULL); 21700Sstevel@tonic-gate if (ipp.ipp_dstopts != NULL && 21710Sstevel@tonic-gate ipp.ipp_dstopts->ip6d_nxt != IPPROTO_ROUTING) { 21720Sstevel@tonic-gate /* 21730Sstevel@tonic-gate * Destination options are tricky. If we get in here, 21740Sstevel@tonic-gate * then we have a terminal header following the 21750Sstevel@tonic-gate * destination options. We need to adjust backwards 21760Sstevel@tonic-gate * so we insert ESP BEFORE the destination options 21770Sstevel@tonic-gate * bag. (So that the dstopts get encrypted!) 21780Sstevel@tonic-gate * 21790Sstevel@tonic-gate * Since this is for outbound packets only, we know 21800Sstevel@tonic-gate * that non-terminal destination options only precede 21810Sstevel@tonic-gate * routing headers. 21820Sstevel@tonic-gate */ 21830Sstevel@tonic-gate divpoint -= ipp.ipp_dstoptslen; 21840Sstevel@tonic-gate } 21850Sstevel@tonic-gate datalen = ntohs(ip6h->ip6_plen) + sizeof (ip6_t) - divpoint; 21860Sstevel@tonic-gate 21870Sstevel@tonic-gate if (ipp.ipp_rthdr != NULL) { 21880Sstevel@tonic-gate nhp = &ipp.ipp_rthdr->ip6r_nxt; 21890Sstevel@tonic-gate } else if (ipp.ipp_hopopts != NULL) { 21900Sstevel@tonic-gate nhp = &ipp.ipp_hopopts->ip6h_nxt; 21910Sstevel@tonic-gate } else { 21920Sstevel@tonic-gate ASSERT(divpoint == sizeof (ip6_t)); 21930Sstevel@tonic-gate /* It's probably IP + ESP. */ 21940Sstevel@tonic-gate nhp = &ip6h->ip6_nxt; 21950Sstevel@tonic-gate } 21960Sstevel@tonic-gate } 21970Sstevel@tonic-gate assoc = io->ipsec_out_esp_sa; 21980Sstevel@tonic-gate ASSERT(assoc != NULL); 21990Sstevel@tonic-gate 22000Sstevel@tonic-gate if (assoc->ipsa_usetime == 0) 22010Sstevel@tonic-gate esp_set_usetime(assoc, B_FALSE); 22020Sstevel@tonic-gate 22030Sstevel@tonic-gate if (assoc->ipsa_auth_alg != SADB_AALG_NONE) 22040Sstevel@tonic-gate mac_len = assoc->ipsa_mac_len; 22050Sstevel@tonic-gate 22060Sstevel@tonic-gate if (assoc->ipsa_flags & IPSA_F_NATT) { 22070Sstevel@tonic-gate /* wedge in fake UDP */ 22080Sstevel@tonic-gate is_natt = B_TRUE; 22090Sstevel@tonic-gate esplen += UDPH_SIZE; 22100Sstevel@tonic-gate } 22110Sstevel@tonic-gate 22120Sstevel@tonic-gate if (assoc->ipsa_encr_alg != SADB_EALG_NULL) 22130Sstevel@tonic-gate iv_len = assoc->ipsa_iv_len; 2214134Sdanmcd 22150Sstevel@tonic-gate /* 22160Sstevel@tonic-gate * Set up ESP header and encryption padding for ENCR PI request. 22170Sstevel@tonic-gate */ 22180Sstevel@tonic-gate 22190Sstevel@tonic-gate /* 22200Sstevel@tonic-gate * Determine the padding length. Pad to 4-bytes. 22210Sstevel@tonic-gate * 22220Sstevel@tonic-gate * Include the two additional bytes (hence the - 2) for the padding 22230Sstevel@tonic-gate * length and the next header. Take this into account when 22240Sstevel@tonic-gate * calculating the actual length of the padding. 22250Sstevel@tonic-gate */ 22260Sstevel@tonic-gate 22270Sstevel@tonic-gate if (assoc->ipsa_encr_alg != SADB_EALG_NULL) { 22280Sstevel@tonic-gate padlen = ((unsigned)(iv_len - datalen - 2)) % iv_len; 22290Sstevel@tonic-gate } else { 22300Sstevel@tonic-gate padlen = ((unsigned)(sizeof (uint32_t) - datalen - 2)) % 22310Sstevel@tonic-gate sizeof (uint32_t); 22320Sstevel@tonic-gate } 22330Sstevel@tonic-gate 22340Sstevel@tonic-gate /* Allocate ESP header and IV. */ 22350Sstevel@tonic-gate esplen += iv_len; 22360Sstevel@tonic-gate 22370Sstevel@tonic-gate /* 22380Sstevel@tonic-gate * Update association byte-count lifetimes. Don't forget to take 22390Sstevel@tonic-gate * into account the padding length and next-header (hence the + 2). 2240134Sdanmcd * 22410Sstevel@tonic-gate * Use the amount of data fed into the "encryption algorithm". This 22420Sstevel@tonic-gate * is the IV, the data length, the padding length, and the final two 22430Sstevel@tonic-gate * bytes (padlen, and next-header). 22440Sstevel@tonic-gate * 22450Sstevel@tonic-gate */ 22460Sstevel@tonic-gate 2247134Sdanmcd if (!esp_age_bytes(assoc, datalen + padlen + iv_len + 2, B_FALSE)) { 22480Sstevel@tonic-gate /* 22490Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 22500Sstevel@tonic-gate * pass it to ip_drop_packet(). 22510Sstevel@tonic-gate */ 22520Sstevel@tonic-gate ip_drop_packet(mp, B_FALSE, NULL, NULL, 22530Sstevel@tonic-gate &ipdrops_esp_bytes_expire, &esp_dropper); 22540Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 22550Sstevel@tonic-gate } 22560Sstevel@tonic-gate 22570Sstevel@tonic-gate espmp = allocb(esplen, BPRI_HI); 22580Sstevel@tonic-gate if (espmp == NULL) { 22590Sstevel@tonic-gate ESP_BUMP_STAT(out_discards); 22600Sstevel@tonic-gate esp1dbg(("esp_outbound: can't allocate espmp.\n")); 22610Sstevel@tonic-gate /* 22620Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 22630Sstevel@tonic-gate * pass it to ip_drop_packet(). 22640Sstevel@tonic-gate */ 22650Sstevel@tonic-gate ip_drop_packet(mp, B_FALSE, NULL, NULL, &ipdrops_esp_nomem, 22660Sstevel@tonic-gate &esp_dropper); 22670Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 22680Sstevel@tonic-gate } 22690Sstevel@tonic-gate espmp->b_wptr += esplen; 22700Sstevel@tonic-gate esph = (esph_t *)espmp->b_rptr; 22710Sstevel@tonic-gate 22720Sstevel@tonic-gate if (is_natt) { 22730Sstevel@tonic-gate esp3dbg(("esp_outbound: NATT")); 22740Sstevel@tonic-gate 22750Sstevel@tonic-gate udpha = (udpha_t *)espmp->b_rptr; 22760Sstevel@tonic-gate udpha->uha_src_port = htons(IPPORT_IKE_NATT); 22770Sstevel@tonic-gate if (assoc->ipsa_remote_port != 0) 22780Sstevel@tonic-gate udpha->uha_dst_port = assoc->ipsa_remote_port; 22790Sstevel@tonic-gate else 22800Sstevel@tonic-gate udpha->uha_dst_port = htons(IPPORT_IKE_NATT); 22810Sstevel@tonic-gate /* 22820Sstevel@tonic-gate * Set the checksum to 0, so that the ip_wput_ipsec_out() 22830Sstevel@tonic-gate * can do the right thing. 22840Sstevel@tonic-gate */ 22850Sstevel@tonic-gate udpha->uha_checksum = 0; 22860Sstevel@tonic-gate esph = (esph_t *)(udpha + 1); 22870Sstevel@tonic-gate } 22880Sstevel@tonic-gate 22890Sstevel@tonic-gate esph->esph_spi = assoc->ipsa_spi; 22900Sstevel@tonic-gate 22910Sstevel@tonic-gate esph->esph_replay = htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1)); 22920Sstevel@tonic-gate if (esph->esph_replay == 0 && assoc->ipsa_replay_wsize != 0) { 22930Sstevel@tonic-gate /* 22940Sstevel@tonic-gate * XXX We have replay counter wrapping. 22950Sstevel@tonic-gate * We probably want to nuke this SA (and its peer). 22960Sstevel@tonic-gate */ 22970Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, 22980Sstevel@tonic-gate SL_ERROR | SL_CONSOLE | SL_WARN, 22990Sstevel@tonic-gate "Outbound ESP SA (0x%x, %s) has wrapped sequence.\n", 23000Sstevel@tonic-gate esph->esph_spi, assoc->ipsa_dstaddr, af); 23010Sstevel@tonic-gate 23020Sstevel@tonic-gate ESP_BUMP_STAT(out_discards); 23030Sstevel@tonic-gate sadb_replay_delete(assoc); 23040Sstevel@tonic-gate /* 23050Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 23060Sstevel@tonic-gate * pass it to ip_drop_packet(). 23070Sstevel@tonic-gate */ 23080Sstevel@tonic-gate ip_drop_packet(mp, B_FALSE, NULL, NULL, &ipdrops_esp_replay, 23090Sstevel@tonic-gate &esp_dropper); 23100Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 23110Sstevel@tonic-gate } 23120Sstevel@tonic-gate 23130Sstevel@tonic-gate /* 23140Sstevel@tonic-gate * Set the IV to a random quantity. We do not require the 23150Sstevel@tonic-gate * highest quality random bits, but for best security with CBC 23160Sstevel@tonic-gate * mode ciphers, the value must be unlikely to repeat and also 23170Sstevel@tonic-gate * must not be known in advance to an adversary capable of 23180Sstevel@tonic-gate * influencing the plaintext. 23190Sstevel@tonic-gate */ 23200Sstevel@tonic-gate (void) random_get_pseudo_bytes((uint8_t *)(esph + 1), iv_len); 23210Sstevel@tonic-gate 23220Sstevel@tonic-gate /* Fix the IP header. */ 23230Sstevel@tonic-gate alloclen = padlen + 2 + mac_len; 23240Sstevel@tonic-gate adj = alloclen + (espmp->b_wptr - espmp->b_rptr); 23250Sstevel@tonic-gate 23260Sstevel@tonic-gate protocol = *nhp; 23270Sstevel@tonic-gate 23280Sstevel@tonic-gate if (io->ipsec_out_v4) { 23290Sstevel@tonic-gate ipha->ipha_length = htons(ntohs(ipha->ipha_length) + adj); 23300Sstevel@tonic-gate if (is_natt) { 23310Sstevel@tonic-gate *nhp = IPPROTO_UDP; 23320Sstevel@tonic-gate udpha->uha_length = htons(ntohs(ipha->ipha_length) - 23330Sstevel@tonic-gate IPH_HDR_LENGTH(ipha)); 23340Sstevel@tonic-gate } else { 23350Sstevel@tonic-gate *nhp = IPPROTO_ESP; 23360Sstevel@tonic-gate } 23370Sstevel@tonic-gate ipha->ipha_hdr_checksum = 0; 23380Sstevel@tonic-gate ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha); 23390Sstevel@tonic-gate } else { 23400Sstevel@tonic-gate ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) + adj); 23410Sstevel@tonic-gate *nhp = IPPROTO_ESP; 23420Sstevel@tonic-gate } 23430Sstevel@tonic-gate 23440Sstevel@tonic-gate /* I've got the two ESP mblks, now insert them. */ 23450Sstevel@tonic-gate 23460Sstevel@tonic-gate esp2dbg(("data_mp before outbound ESP adjustment:\n")); 23470Sstevel@tonic-gate esp2dbg((dump_msg(data_mp))); 23480Sstevel@tonic-gate 23490Sstevel@tonic-gate if (!esp_insert_esp(data_mp, espmp, divpoint)) { 23500Sstevel@tonic-gate ESP_BUMP_STAT(out_discards); 23510Sstevel@tonic-gate /* NOTE: esp_insert_esp() only fails if there's no memory. */ 23520Sstevel@tonic-gate /* 23530Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 23540Sstevel@tonic-gate * pass it to ip_drop_packet(). 23550Sstevel@tonic-gate */ 23560Sstevel@tonic-gate ip_drop_packet(mp, B_FALSE, NULL, NULL, &ipdrops_esp_nomem, 23570Sstevel@tonic-gate &esp_dropper); 23580Sstevel@tonic-gate freeb(espmp); 23590Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 23600Sstevel@tonic-gate } 23610Sstevel@tonic-gate 23620Sstevel@tonic-gate /* Append padding (and leave room for ICV). */ 23630Sstevel@tonic-gate for (tailmp = data_mp; tailmp->b_cont != NULL; tailmp = tailmp->b_cont) 23640Sstevel@tonic-gate ; 23650Sstevel@tonic-gate if (tailmp->b_wptr + alloclen > tailmp->b_datap->db_lim) { 23660Sstevel@tonic-gate tailmp->b_cont = allocb(alloclen, BPRI_HI); 23670Sstevel@tonic-gate if (tailmp->b_cont == NULL) { 23680Sstevel@tonic-gate ESP_BUMP_STAT(out_discards); 23690Sstevel@tonic-gate esp0dbg(("esp_outbound: Can't allocate tailmp.\n")); 23700Sstevel@tonic-gate /* 23710Sstevel@tonic-gate * TODO: Find the outbound IRE for this packet and 23720Sstevel@tonic-gate * pass it to ip_drop_packet(). 23730Sstevel@tonic-gate */ 23740Sstevel@tonic-gate ip_drop_packet(mp, B_FALSE, NULL, NULL, 23750Sstevel@tonic-gate &ipdrops_esp_nomem, &esp_dropper); 23760Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 23770Sstevel@tonic-gate } 23780Sstevel@tonic-gate tailmp = tailmp->b_cont; 23790Sstevel@tonic-gate } 23800Sstevel@tonic-gate 23810Sstevel@tonic-gate /* 23820Sstevel@tonic-gate * If there's padding, N bytes of padding must be of the form 0x1, 23830Sstevel@tonic-gate * 0x2, 0x3... 0xN. 23840Sstevel@tonic-gate */ 23850Sstevel@tonic-gate for (i = 0; i < padlen; ) { 23860Sstevel@tonic-gate i++; 23870Sstevel@tonic-gate *tailmp->b_wptr++ = i; 23880Sstevel@tonic-gate } 23890Sstevel@tonic-gate *tailmp->b_wptr++ = i; 23900Sstevel@tonic-gate *tailmp->b_wptr++ = protocol; 23910Sstevel@tonic-gate 23920Sstevel@tonic-gate esp2dbg(("data_Mp before encryption:\n")); 23930Sstevel@tonic-gate esp2dbg((dump_msg(data_mp))); 23940Sstevel@tonic-gate 23950Sstevel@tonic-gate /* 23960Sstevel@tonic-gate * The packet is eligible for hardware acceleration if the 23970Sstevel@tonic-gate * following conditions are satisfied: 23980Sstevel@tonic-gate * 23990Sstevel@tonic-gate * 1. the packet will not be fragmented 24000Sstevel@tonic-gate * 2. the provider supports the algorithms specified by SA 24010Sstevel@tonic-gate * 3. there is no pending control message being exchanged 24020Sstevel@tonic-gate * 4. snoop is not attached 24030Sstevel@tonic-gate * 5. the destination address is not a multicast address 24040Sstevel@tonic-gate * 24050Sstevel@tonic-gate * All five of these conditions are checked by IP prior to 24060Sstevel@tonic-gate * sending the packet to ESP. 24070Sstevel@tonic-gate * 24080Sstevel@tonic-gate * But We, and We Alone, can, nay MUST check if the packet 24090Sstevel@tonic-gate * is over NATT, and then disqualify it from hardware 24100Sstevel@tonic-gate * acceleration. 24110Sstevel@tonic-gate */ 24120Sstevel@tonic-gate 24130Sstevel@tonic-gate if (io->ipsec_out_is_capab_ill && !(assoc->ipsa_flags & IPSA_F_NATT)) { 24140Sstevel@tonic-gate return (esp_outbound_accelerated(ipsec_out_mp, mac_len)); 24150Sstevel@tonic-gate } 24160Sstevel@tonic-gate ESP_BUMP_STAT(noaccel); 24170Sstevel@tonic-gate 24180Sstevel@tonic-gate /* 24190Sstevel@tonic-gate * Okay. I've set up the pre-encryption ESP. Let's do it! 24200Sstevel@tonic-gate */ 24210Sstevel@tonic-gate 24220Sstevel@tonic-gate if (mac_len > 0) { 24230Sstevel@tonic-gate ASSERT(tailmp->b_wptr + mac_len <= tailmp->b_datap->db_lim); 24240Sstevel@tonic-gate icv_buf = tailmp->b_wptr; 24250Sstevel@tonic-gate tailmp->b_wptr += mac_len; 24260Sstevel@tonic-gate } else { 24270Sstevel@tonic-gate icv_buf = NULL; 24280Sstevel@tonic-gate } 24290Sstevel@tonic-gate 24300Sstevel@tonic-gate return (esp_submit_req_outbound(ipsec_out_mp, assoc, icv_buf, 24310Sstevel@tonic-gate datalen + padlen + 2)); 24320Sstevel@tonic-gate } 24330Sstevel@tonic-gate 24340Sstevel@tonic-gate /* 24350Sstevel@tonic-gate * IP calls this to validate the ICMP errors that 24360Sstevel@tonic-gate * we got from the network. 24370Sstevel@tonic-gate */ 24380Sstevel@tonic-gate ipsec_status_t 24390Sstevel@tonic-gate ipsecesp_icmp_error(mblk_t *ipsec_mp) 24400Sstevel@tonic-gate { 24410Sstevel@tonic-gate /* 24420Sstevel@tonic-gate * Unless we get an entire packet back, this function is useless. 24430Sstevel@tonic-gate * Why? 24440Sstevel@tonic-gate * 24450Sstevel@tonic-gate * 1.) Partial packets are useless, because the "next header" 24460Sstevel@tonic-gate * is at the end of the decrypted ESP packet. Without the 24470Sstevel@tonic-gate * whole packet, this is useless. 24480Sstevel@tonic-gate * 24490Sstevel@tonic-gate * 2.) If we every use a stateful cipher, such as a stream or a 24500Sstevel@tonic-gate * one-time pad, we can't do anything. 24510Sstevel@tonic-gate * 24520Sstevel@tonic-gate * Since the chances of us getting an entire packet back are very 24530Sstevel@tonic-gate * very small, we discard here. 24540Sstevel@tonic-gate */ 24550Sstevel@tonic-gate IP_ESP_BUMP_STAT(in_discards); 24560Sstevel@tonic-gate ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, &ipdrops_esp_icmp, 24570Sstevel@tonic-gate &esp_dropper); 24580Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 24590Sstevel@tonic-gate } 24600Sstevel@tonic-gate 24610Sstevel@tonic-gate /* 24620Sstevel@tonic-gate * ESP module read put routine. 24630Sstevel@tonic-gate */ 24640Sstevel@tonic-gate /* ARGSUSED */ 24650Sstevel@tonic-gate static void 24660Sstevel@tonic-gate ipsecesp_rput(queue_t *q, mblk_t *mp) 24670Sstevel@tonic-gate { 24680Sstevel@tonic-gate keysock_in_t *ksi; 24690Sstevel@tonic-gate int *addrtype; 24700Sstevel@tonic-gate ire_t *ire; 24710Sstevel@tonic-gate mblk_t *ire_mp, *last_mp; 24720Sstevel@tonic-gate 24730Sstevel@tonic-gate switch (mp->b_datap->db_type) { 24740Sstevel@tonic-gate case M_CTL: 24750Sstevel@tonic-gate /* 24760Sstevel@tonic-gate * IPsec request of some variety from IP. IPSEC_{IN,OUT} 24770Sstevel@tonic-gate * are the common cases, but even ICMP error messages from IP 24780Sstevel@tonic-gate * may rise up here. 24790Sstevel@tonic-gate * 24800Sstevel@tonic-gate * Ummmm, actually, this can also be the reflected KEYSOCK_IN 24810Sstevel@tonic-gate * message, with an IRE_DB_TYPE hung off at the end. 24820Sstevel@tonic-gate */ 24830Sstevel@tonic-gate switch (((ipsec_info_t *)(mp->b_rptr))->ipsec_info_type) { 24840Sstevel@tonic-gate case KEYSOCK_IN: 24850Sstevel@tonic-gate last_mp = mp; 24860Sstevel@tonic-gate while (last_mp->b_cont != NULL && 24870Sstevel@tonic-gate last_mp->b_cont->b_datap->db_type != IRE_DB_TYPE) 24880Sstevel@tonic-gate last_mp = last_mp->b_cont; 24890Sstevel@tonic-gate 24900Sstevel@tonic-gate if (last_mp->b_cont == NULL) { 24910Sstevel@tonic-gate freemsg(mp); 24920Sstevel@tonic-gate break; /* Out of switch. */ 24930Sstevel@tonic-gate } 24940Sstevel@tonic-gate 24950Sstevel@tonic-gate ire_mp = last_mp->b_cont; 24960Sstevel@tonic-gate last_mp->b_cont = NULL; 24970Sstevel@tonic-gate 24980Sstevel@tonic-gate ksi = (keysock_in_t *)mp->b_rptr; 24990Sstevel@tonic-gate 25000Sstevel@tonic-gate if (ksi->ks_in_srctype == KS_IN_ADDR_UNKNOWN) 25010Sstevel@tonic-gate addrtype = &ksi->ks_in_srctype; 25020Sstevel@tonic-gate else if (ksi->ks_in_dsttype == KS_IN_ADDR_UNKNOWN) 25030Sstevel@tonic-gate addrtype = &ksi->ks_in_dsttype; 25040Sstevel@tonic-gate else if (ksi->ks_in_proxytype == KS_IN_ADDR_UNKNOWN) 25050Sstevel@tonic-gate addrtype = &ksi->ks_in_proxytype; 25060Sstevel@tonic-gate 25070Sstevel@tonic-gate ire = (ire_t *)ire_mp->b_rptr; 25080Sstevel@tonic-gate 25090Sstevel@tonic-gate *addrtype = sadb_addrset(ire); 25100Sstevel@tonic-gate 25110Sstevel@tonic-gate freemsg(ire_mp); 25120Sstevel@tonic-gate if (esp_pfkey_q != NULL) { 25130Sstevel@tonic-gate /* 25140Sstevel@tonic-gate * Decrement counter to make up for 25150Sstevel@tonic-gate * auto-increment in ipsecesp_wput(). 25160Sstevel@tonic-gate * I'm running all MT-hot through here, so 25170Sstevel@tonic-gate * don't worry about perimeters and lateral 25180Sstevel@tonic-gate * puts. 25190Sstevel@tonic-gate */ 25200Sstevel@tonic-gate ESP_DEBUMP_STAT(keysock_in); 25210Sstevel@tonic-gate ipsecesp_wput(WR(esp_pfkey_q), mp); 25220Sstevel@tonic-gate } else { 25230Sstevel@tonic-gate freemsg(mp); 25240Sstevel@tonic-gate } 25250Sstevel@tonic-gate break; 25260Sstevel@tonic-gate default: 25270Sstevel@tonic-gate freemsg(mp); 25280Sstevel@tonic-gate break; 25290Sstevel@tonic-gate } 25300Sstevel@tonic-gate break; 25310Sstevel@tonic-gate case M_PROTO: 25320Sstevel@tonic-gate case M_PCPROTO: 25330Sstevel@tonic-gate /* TPI message of some sort. */ 25340Sstevel@tonic-gate switch (*((t_scalar_t *)mp->b_rptr)) { 25350Sstevel@tonic-gate case T_BIND_ACK: 25360Sstevel@tonic-gate esp3dbg(("Thank you IP from ESP for T_BIND_ACK\n")); 25370Sstevel@tonic-gate break; 25380Sstevel@tonic-gate case T_ERROR_ACK: 25390Sstevel@tonic-gate cmn_err(CE_WARN, 25400Sstevel@tonic-gate "ipsecesp: ESP received T_ERROR_ACK from IP."); 25410Sstevel@tonic-gate /* 25420Sstevel@tonic-gate * Make esp_sadb.s_ip_q NULL, and in the 25430Sstevel@tonic-gate * future, perhaps try again. 25440Sstevel@tonic-gate */ 25450Sstevel@tonic-gate esp_sadb.s_ip_q = NULL; 25460Sstevel@tonic-gate break; 25470Sstevel@tonic-gate case T_OK_ACK: 25480Sstevel@tonic-gate /* Probably from a (rarely sent) T_UNBIND_REQ. */ 25490Sstevel@tonic-gate break; 25500Sstevel@tonic-gate default: 25510Sstevel@tonic-gate esp0dbg(("Unknown M_{,PC}PROTO message.\n")); 25520Sstevel@tonic-gate } 25530Sstevel@tonic-gate freemsg(mp); 25540Sstevel@tonic-gate break; 25550Sstevel@tonic-gate default: 25560Sstevel@tonic-gate /* For now, passthru message. */ 25570Sstevel@tonic-gate esp2dbg(("ESP got unknown mblk type %d.\n", 25580Sstevel@tonic-gate mp->b_datap->db_type)); 25590Sstevel@tonic-gate putnext(q, mp); 25600Sstevel@tonic-gate } 25610Sstevel@tonic-gate } 25620Sstevel@tonic-gate 25630Sstevel@tonic-gate /* 25640Sstevel@tonic-gate * Construct an SADB_REGISTER message with the current algorithms. 25650Sstevel@tonic-gate */ 25660Sstevel@tonic-gate static boolean_t 25670Sstevel@tonic-gate esp_register_out(uint32_t sequence, uint32_t pid, uint_t serial) 25680Sstevel@tonic-gate { 25690Sstevel@tonic-gate mblk_t *pfkey_msg_mp, *keysock_out_mp; 25700Sstevel@tonic-gate sadb_msg_t *samsg; 25710Sstevel@tonic-gate sadb_supported_t *sasupp_auth = NULL; 25720Sstevel@tonic-gate sadb_supported_t *sasupp_encr = NULL; 25730Sstevel@tonic-gate sadb_alg_t *saalg; 25740Sstevel@tonic-gate uint_t allocsize = sizeof (*samsg); 25750Sstevel@tonic-gate uint_t i, numalgs_snap; 25760Sstevel@tonic-gate int current_aalgs; 25770Sstevel@tonic-gate ipsec_alginfo_t **authalgs; 25780Sstevel@tonic-gate uint_t num_aalgs; 25790Sstevel@tonic-gate int current_ealgs; 25800Sstevel@tonic-gate ipsec_alginfo_t **encralgs; 25810Sstevel@tonic-gate uint_t num_ealgs; 25820Sstevel@tonic-gate 25830Sstevel@tonic-gate /* Allocate the KEYSOCK_OUT. */ 25840Sstevel@tonic-gate keysock_out_mp = sadb_keysock_out(serial); 25850Sstevel@tonic-gate if (keysock_out_mp == NULL) { 25860Sstevel@tonic-gate esp0dbg(("esp_register_out: couldn't allocate mblk.\n")); 25870Sstevel@tonic-gate return (B_FALSE); 25880Sstevel@tonic-gate } 25890Sstevel@tonic-gate 25900Sstevel@tonic-gate /* 25910Sstevel@tonic-gate * Allocate the PF_KEY message that follows KEYSOCK_OUT. 25920Sstevel@tonic-gate */ 25930Sstevel@tonic-gate 25940Sstevel@tonic-gate mutex_enter(&alg_lock); 25950Sstevel@tonic-gate 25960Sstevel@tonic-gate /* 25970Sstevel@tonic-gate * Fill SADB_REGISTER message's algorithm descriptors. Hold 25980Sstevel@tonic-gate * down the lock while filling it. 25990Sstevel@tonic-gate * 26000Sstevel@tonic-gate * Return only valid algorithms, so the number of algorithms 26010Sstevel@tonic-gate * to send up may be less than the number of algorithm entries 26020Sstevel@tonic-gate * in the table. 26030Sstevel@tonic-gate */ 26040Sstevel@tonic-gate authalgs = ipsec_alglists[IPSEC_ALG_AUTH]; 26050Sstevel@tonic-gate for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++) 26060Sstevel@tonic-gate if (authalgs[i] != NULL && ALG_VALID(authalgs[i])) 26070Sstevel@tonic-gate num_aalgs++; 26080Sstevel@tonic-gate 26090Sstevel@tonic-gate if (num_aalgs != 0) { 26100Sstevel@tonic-gate allocsize += (num_aalgs * sizeof (*saalg)); 26110Sstevel@tonic-gate allocsize += sizeof (*sasupp_auth); 26120Sstevel@tonic-gate } 26130Sstevel@tonic-gate encralgs = ipsec_alglists[IPSEC_ALG_ENCR]; 26140Sstevel@tonic-gate for (num_ealgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++) 26150Sstevel@tonic-gate if (encralgs[i] != NULL && ALG_VALID(encralgs[i])) 26160Sstevel@tonic-gate num_ealgs++; 26170Sstevel@tonic-gate 26180Sstevel@tonic-gate if (num_ealgs != 0) { 26190Sstevel@tonic-gate allocsize += (num_ealgs * sizeof (*saalg)); 26200Sstevel@tonic-gate allocsize += sizeof (*sasupp_encr); 26210Sstevel@tonic-gate } 26220Sstevel@tonic-gate keysock_out_mp->b_cont = allocb(allocsize, BPRI_HI); 26230Sstevel@tonic-gate if (keysock_out_mp->b_cont == NULL) { 26240Sstevel@tonic-gate mutex_exit(&alg_lock); 26250Sstevel@tonic-gate freemsg(keysock_out_mp); 26260Sstevel@tonic-gate return (B_FALSE); 26270Sstevel@tonic-gate } 26280Sstevel@tonic-gate 26290Sstevel@tonic-gate pfkey_msg_mp = keysock_out_mp->b_cont; 26300Sstevel@tonic-gate pfkey_msg_mp->b_wptr += allocsize; 26310Sstevel@tonic-gate if (num_aalgs != 0) { 26320Sstevel@tonic-gate sasupp_auth = (sadb_supported_t *) 26330Sstevel@tonic-gate (pfkey_msg_mp->b_rptr + sizeof (*samsg)); 26340Sstevel@tonic-gate saalg = (sadb_alg_t *)(sasupp_auth + 1); 26350Sstevel@tonic-gate 26360Sstevel@tonic-gate ASSERT(((ulong_t)saalg & 0x7) == 0); 26370Sstevel@tonic-gate 26380Sstevel@tonic-gate numalgs_snap = 0; 26390Sstevel@tonic-gate for (i = 0; 26400Sstevel@tonic-gate ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs)); i++) { 26410Sstevel@tonic-gate if (authalgs[i] == NULL || !ALG_VALID(authalgs[i])) 26420Sstevel@tonic-gate continue; 26430Sstevel@tonic-gate 26440Sstevel@tonic-gate saalg->sadb_alg_id = authalgs[i]->alg_id; 26450Sstevel@tonic-gate saalg->sadb_alg_ivlen = 0; 26460Sstevel@tonic-gate saalg->sadb_alg_minbits = authalgs[i]->alg_ef_minbits; 26470Sstevel@tonic-gate saalg->sadb_alg_maxbits = authalgs[i]->alg_ef_maxbits; 26480Sstevel@tonic-gate saalg->sadb_x_alg_defincr = authalgs[i]->alg_ef_default; 26490Sstevel@tonic-gate saalg->sadb_x_alg_increment = 26500Sstevel@tonic-gate authalgs[i]->alg_increment; 26510Sstevel@tonic-gate numalgs_snap++; 26520Sstevel@tonic-gate saalg++; 26530Sstevel@tonic-gate } 26540Sstevel@tonic-gate ASSERT(numalgs_snap == num_aalgs); 26550Sstevel@tonic-gate #ifdef DEBUG 26560Sstevel@tonic-gate /* 26570Sstevel@tonic-gate * Reality check to make sure I snagged all of the 26580Sstevel@tonic-gate * algorithms. 26590Sstevel@tonic-gate */ 26600Sstevel@tonic-gate for (; i < IPSEC_MAX_ALGS; i++) { 26610Sstevel@tonic-gate if (authalgs[i] != NULL && ALG_VALID(authalgs[i])) { 26620Sstevel@tonic-gate cmn_err(CE_PANIC, "esp_register_out()! " 26630Sstevel@tonic-gate "Missed aalg #%d.\n", i); 26640Sstevel@tonic-gate } 26650Sstevel@tonic-gate } 26660Sstevel@tonic-gate #endif /* DEBUG */ 26670Sstevel@tonic-gate } else { 26680Sstevel@tonic-gate saalg = (sadb_alg_t *)(pfkey_msg_mp->b_rptr + sizeof (*samsg)); 26690Sstevel@tonic-gate } 26700Sstevel@tonic-gate 26710Sstevel@tonic-gate if (num_ealgs != 0) { 26720Sstevel@tonic-gate sasupp_encr = (sadb_supported_t *)saalg; 26730Sstevel@tonic-gate saalg = (sadb_alg_t *)(sasupp_encr + 1); 26740Sstevel@tonic-gate 26750Sstevel@tonic-gate numalgs_snap = 0; 26760Sstevel@tonic-gate for (i = 0; 26770Sstevel@tonic-gate ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_ealgs)); i++) { 26780Sstevel@tonic-gate if (encralgs[i] == NULL || !ALG_VALID(encralgs[i])) 26790Sstevel@tonic-gate continue; 26800Sstevel@tonic-gate saalg->sadb_alg_id = encralgs[i]->alg_id; 26810Sstevel@tonic-gate saalg->sadb_alg_ivlen = encralgs[i]->alg_datalen; 26820Sstevel@tonic-gate saalg->sadb_alg_minbits = encralgs[i]->alg_ef_minbits; 26830Sstevel@tonic-gate saalg->sadb_alg_maxbits = encralgs[i]->alg_ef_maxbits; 26840Sstevel@tonic-gate saalg->sadb_x_alg_defincr = encralgs[i]->alg_ef_default; 26850Sstevel@tonic-gate saalg->sadb_x_alg_increment = 26860Sstevel@tonic-gate encralgs[i]->alg_increment; 26870Sstevel@tonic-gate numalgs_snap++; 26880Sstevel@tonic-gate saalg++; 26890Sstevel@tonic-gate } 26900Sstevel@tonic-gate ASSERT(numalgs_snap == num_ealgs); 26910Sstevel@tonic-gate #ifdef DEBUG 26920Sstevel@tonic-gate /* 26930Sstevel@tonic-gate * Reality check to make sure I snagged all of the 26940Sstevel@tonic-gate * algorithms. 26950Sstevel@tonic-gate */ 26960Sstevel@tonic-gate for (; i < IPSEC_MAX_ALGS; i++) { 26970Sstevel@tonic-gate if (encralgs[i] != NULL && ALG_VALID(encralgs[i])) { 26980Sstevel@tonic-gate cmn_err(CE_PANIC, "esp_register_out()! " 26990Sstevel@tonic-gate "Missed ealg #%d.\n", i); 27000Sstevel@tonic-gate } 27010Sstevel@tonic-gate } 27020Sstevel@tonic-gate #endif /* DEBUG */ 27030Sstevel@tonic-gate } 27040Sstevel@tonic-gate 27050Sstevel@tonic-gate current_aalgs = num_aalgs; 27060Sstevel@tonic-gate current_ealgs = num_ealgs; 27070Sstevel@tonic-gate 27080Sstevel@tonic-gate mutex_exit(&alg_lock); 27090Sstevel@tonic-gate 27100Sstevel@tonic-gate /* Now fill the rest of the SADB_REGISTER message. */ 27110Sstevel@tonic-gate 27120Sstevel@tonic-gate samsg = (sadb_msg_t *)pfkey_msg_mp->b_rptr; 27130Sstevel@tonic-gate samsg->sadb_msg_version = PF_KEY_V2; 27140Sstevel@tonic-gate samsg->sadb_msg_type = SADB_REGISTER; 27150Sstevel@tonic-gate samsg->sadb_msg_errno = 0; 27160Sstevel@tonic-gate samsg->sadb_msg_satype = SADB_SATYPE_ESP; 27170Sstevel@tonic-gate samsg->sadb_msg_len = SADB_8TO64(allocsize); 27180Sstevel@tonic-gate samsg->sadb_msg_reserved = 0; 27190Sstevel@tonic-gate /* 27200Sstevel@tonic-gate * Assume caller has sufficient sequence/pid number info. If it's one 27210Sstevel@tonic-gate * from me over a new alg., I could give two hoots about sequence. 27220Sstevel@tonic-gate */ 27230Sstevel@tonic-gate samsg->sadb_msg_seq = sequence; 27240Sstevel@tonic-gate samsg->sadb_msg_pid = pid; 27250Sstevel@tonic-gate 27260Sstevel@tonic-gate if (sasupp_auth != NULL) { 27270Sstevel@tonic-gate sasupp_auth->sadb_supported_len = 27280Sstevel@tonic-gate SADB_8TO64(sizeof (*sasupp_auth) + 27290Sstevel@tonic-gate sizeof (*saalg) * current_aalgs); 27300Sstevel@tonic-gate sasupp_auth->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH; 27310Sstevel@tonic-gate sasupp_auth->sadb_supported_reserved = 0; 27320Sstevel@tonic-gate } 27330Sstevel@tonic-gate 27340Sstevel@tonic-gate if (sasupp_encr != NULL) { 27350Sstevel@tonic-gate sasupp_encr->sadb_supported_len = 27360Sstevel@tonic-gate SADB_8TO64(sizeof (*sasupp_encr) + 27370Sstevel@tonic-gate sizeof (*saalg) * current_ealgs); 27380Sstevel@tonic-gate sasupp_encr->sadb_supported_exttype = 27390Sstevel@tonic-gate SADB_EXT_SUPPORTED_ENCRYPT; 27400Sstevel@tonic-gate sasupp_encr->sadb_supported_reserved = 0; 27410Sstevel@tonic-gate } 27420Sstevel@tonic-gate 27430Sstevel@tonic-gate if (esp_pfkey_q != NULL) 27440Sstevel@tonic-gate putnext(esp_pfkey_q, keysock_out_mp); 27450Sstevel@tonic-gate else { 27460Sstevel@tonic-gate freemsg(keysock_out_mp); 27470Sstevel@tonic-gate return (B_FALSE); 27480Sstevel@tonic-gate } 27490Sstevel@tonic-gate 27500Sstevel@tonic-gate return (B_TRUE); 27510Sstevel@tonic-gate } 27520Sstevel@tonic-gate 27530Sstevel@tonic-gate /* 27540Sstevel@tonic-gate * Invoked when the algorithm table changes. Causes SADB_REGISTER 27550Sstevel@tonic-gate * messages continaining the current list of algorithms to be 27560Sstevel@tonic-gate * sent up to the ESP listeners. 27570Sstevel@tonic-gate */ 27580Sstevel@tonic-gate void 27590Sstevel@tonic-gate ipsecesp_algs_changed(void) 27600Sstevel@tonic-gate { 27610Sstevel@tonic-gate /* 27620Sstevel@tonic-gate * Time to send a PF_KEY SADB_REGISTER message to ESP listeners 27630Sstevel@tonic-gate * everywhere. (The function itself checks for NULL esp_pfkey_q.) 27640Sstevel@tonic-gate */ 27650Sstevel@tonic-gate (void) esp_register_out(0, 0, 0); 27660Sstevel@tonic-gate } 27670Sstevel@tonic-gate 27680Sstevel@tonic-gate /* 27690Sstevel@tonic-gate * taskq_dispatch handler. 27700Sstevel@tonic-gate */ 27710Sstevel@tonic-gate static void 27720Sstevel@tonic-gate inbound_task(void *arg) 27730Sstevel@tonic-gate { 27740Sstevel@tonic-gate esph_t *esph; 27750Sstevel@tonic-gate mblk_t *mp = (mblk_t *)arg; 27760Sstevel@tonic-gate ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr; 27770Sstevel@tonic-gate int ipsec_rc; 27780Sstevel@tonic-gate 27790Sstevel@tonic-gate esp2dbg(("in ESP inbound_task")); 27800Sstevel@tonic-gate 27810Sstevel@tonic-gate esph = ipsec_inbound_esp_sa(mp); 27820Sstevel@tonic-gate if (esph == NULL) 27830Sstevel@tonic-gate return; 27840Sstevel@tonic-gate ASSERT(ii->ipsec_in_esp_sa != NULL); 27850Sstevel@tonic-gate ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func(mp, esph); 27860Sstevel@tonic-gate if (ipsec_rc != IPSEC_STATUS_SUCCESS) 27870Sstevel@tonic-gate return; 27880Sstevel@tonic-gate ip_fanout_proto_again(mp, NULL, NULL, NULL); 27890Sstevel@tonic-gate } 27900Sstevel@tonic-gate 27910Sstevel@tonic-gate /* 27920Sstevel@tonic-gate * Now that weak-key passed, actually ADD the security association, and 27930Sstevel@tonic-gate * send back a reply ADD message. 27940Sstevel@tonic-gate */ 27950Sstevel@tonic-gate static int 27960Sstevel@tonic-gate esp_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi) 27970Sstevel@tonic-gate { 2798*691Ssommerfe isaf_t *primary, *secondary, *inbound, *outbound; 27990Sstevel@tonic-gate sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 28000Sstevel@tonic-gate sadb_address_t *dstext = 28010Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 28020Sstevel@tonic-gate struct sockaddr_in *dst; 28030Sstevel@tonic-gate struct sockaddr_in6 *dst6; 28040Sstevel@tonic-gate boolean_t is_ipv4, clone = B_FALSE, is_inbound = B_FALSE; 28050Sstevel@tonic-gate uint32_t *dstaddr; 28060Sstevel@tonic-gate ipsa_t *larval = NULL; 28070Sstevel@tonic-gate ipsacq_t *acqrec; 28080Sstevel@tonic-gate iacqf_t *acq_bucket; 28090Sstevel@tonic-gate mblk_t *acq_msgs = NULL; 28100Sstevel@tonic-gate int rc; 28110Sstevel@tonic-gate sadb_t *sp; 28120Sstevel@tonic-gate int outhash; 28130Sstevel@tonic-gate mblk_t *lpkt; 28140Sstevel@tonic-gate 28150Sstevel@tonic-gate /* 28160Sstevel@tonic-gate * Locate the appropriate table(s). 28170Sstevel@tonic-gate */ 28180Sstevel@tonic-gate 28190Sstevel@tonic-gate dst = (struct sockaddr_in *)(dstext + 1); 28200Sstevel@tonic-gate dst6 = (struct sockaddr_in6 *)dst; 28210Sstevel@tonic-gate is_ipv4 = (dst->sin_family == AF_INET); 28220Sstevel@tonic-gate if (is_ipv4) { 28230Sstevel@tonic-gate sp = &esp_sadb.s_v4; 28240Sstevel@tonic-gate dstaddr = (uint32_t *)(&dst->sin_addr); 2825564Ssommerfe outhash = OUTBOUND_HASH_V4(sp, *(ipaddr_t *)dstaddr); 28260Sstevel@tonic-gate } else { 28270Sstevel@tonic-gate sp = &esp_sadb.s_v6; 28280Sstevel@tonic-gate dstaddr = (uint32_t *)(&dst6->sin6_addr); 2829564Ssommerfe outhash = OUTBOUND_HASH_V6(sp, *(in6_addr_t *)dstaddr); 28300Sstevel@tonic-gate } 2831*691Ssommerfe 2832564Ssommerfe inbound = INBOUND_BUCKET(sp, assoc->sadb_sa_spi); 2833*691Ssommerfe outbound = &sp->sdb_of[outhash]; 2834*691Ssommerfe 28350Sstevel@tonic-gate switch (ksi->ks_in_dsttype) { 28360Sstevel@tonic-gate case KS_IN_ADDR_MBCAST: 28370Sstevel@tonic-gate clone = B_TRUE; /* All mcast SAs can be bidirectional */ 28380Sstevel@tonic-gate /* FALLTHRU */ 28390Sstevel@tonic-gate case KS_IN_ADDR_ME: 28400Sstevel@tonic-gate primary = inbound; 2841*691Ssommerfe secondary = outbound; 28420Sstevel@tonic-gate /* 28430Sstevel@tonic-gate * If the source address is either one of mine, or unspecified 28440Sstevel@tonic-gate * (which is best summed up by saying "not 'not mine'"), 28450Sstevel@tonic-gate * then the association is potentially bi-directional, 28460Sstevel@tonic-gate * in that it can be used for inbound traffic and outbound 28470Sstevel@tonic-gate * traffic. The best example of such an SA is a multicast 28480Sstevel@tonic-gate * SA (which allows me to receive the outbound traffic). 28490Sstevel@tonic-gate */ 28500Sstevel@tonic-gate if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME) 28510Sstevel@tonic-gate clone = B_TRUE; 28520Sstevel@tonic-gate is_inbound = B_TRUE; 28530Sstevel@tonic-gate break; 28540Sstevel@tonic-gate case KS_IN_ADDR_NOTME: 2855*691Ssommerfe primary = outbound; 28560Sstevel@tonic-gate secondary = inbound; 28570Sstevel@tonic-gate /* 28580Sstevel@tonic-gate * If the source address literally not mine (either 28590Sstevel@tonic-gate * unspecified or not mine), then this SA may have an 28600Sstevel@tonic-gate * address that WILL be mine after some configuration. 28610Sstevel@tonic-gate * We pay the price for this by making it a bi-directional 28620Sstevel@tonic-gate * SA. 28630Sstevel@tonic-gate */ 28640Sstevel@tonic-gate if (ksi->ks_in_srctype != KS_IN_ADDR_ME) 28650Sstevel@tonic-gate clone = B_TRUE; 28660Sstevel@tonic-gate break; 28670Sstevel@tonic-gate default: 28680Sstevel@tonic-gate samsg->sadb_x_msg_diagnostic = SADB_X_DIAGNOSTIC_BAD_DST; 28690Sstevel@tonic-gate return (EINVAL); 28700Sstevel@tonic-gate } 28710Sstevel@tonic-gate 28720Sstevel@tonic-gate /* 28730Sstevel@tonic-gate * Find a ACQUIRE list entry if possible. If we've added an SA that 28740Sstevel@tonic-gate * suits the needs of an ACQUIRE list entry, we can eliminate the 28750Sstevel@tonic-gate * ACQUIRE list entry and transmit the enqueued packets. Use the 28760Sstevel@tonic-gate * high-bit of the sequence number to queue it. Key off destination 28770Sstevel@tonic-gate * addr, and change acqrec's state. 28780Sstevel@tonic-gate */ 28790Sstevel@tonic-gate 28800Sstevel@tonic-gate if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) { 28810Sstevel@tonic-gate acq_bucket = &sp->sdb_acq[outhash]; 28820Sstevel@tonic-gate mutex_enter(&acq_bucket->iacqf_lock); 28830Sstevel@tonic-gate for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL; 28840Sstevel@tonic-gate acqrec = acqrec->ipsacq_next) { 28850Sstevel@tonic-gate mutex_enter(&acqrec->ipsacq_lock); 28860Sstevel@tonic-gate /* 28870Sstevel@tonic-gate * Q: I only check sequence. Should I check dst? 28880Sstevel@tonic-gate * A: Yes, check dest because those are the packets 28890Sstevel@tonic-gate * that are queued up. 28900Sstevel@tonic-gate */ 28910Sstevel@tonic-gate if (acqrec->ipsacq_seq == samsg->sadb_msg_seq && 28920Sstevel@tonic-gate IPSA_ARE_ADDR_EQUAL(dstaddr, 28930Sstevel@tonic-gate acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam)) 28940Sstevel@tonic-gate break; 28950Sstevel@tonic-gate mutex_exit(&acqrec->ipsacq_lock); 28960Sstevel@tonic-gate } 28970Sstevel@tonic-gate if (acqrec != NULL) { 28980Sstevel@tonic-gate /* 28990Sstevel@tonic-gate * AHA! I found an ACQUIRE record for this SA. 29000Sstevel@tonic-gate * Grab the msg list, and free the acquire record. 29010Sstevel@tonic-gate * I already am holding the lock for this record, 29020Sstevel@tonic-gate * so all I have to do is free it. 29030Sstevel@tonic-gate */ 29040Sstevel@tonic-gate acq_msgs = acqrec->ipsacq_mp; 29050Sstevel@tonic-gate acqrec->ipsacq_mp = NULL; 29060Sstevel@tonic-gate mutex_exit(&acqrec->ipsacq_lock); 29070Sstevel@tonic-gate sadb_destroy_acquire(acqrec); 29080Sstevel@tonic-gate } 29090Sstevel@tonic-gate mutex_exit(&acq_bucket->iacqf_lock); 29100Sstevel@tonic-gate } 29110Sstevel@tonic-gate 29120Sstevel@tonic-gate /* 29130Sstevel@tonic-gate * Find PF_KEY message, and see if I'm an update. If so, find entry 29140Sstevel@tonic-gate * in larval list (if there). 29150Sstevel@tonic-gate */ 29160Sstevel@tonic-gate 29170Sstevel@tonic-gate if (samsg->sadb_msg_type == SADB_UPDATE) { 29180Sstevel@tonic-gate mutex_enter(&inbound->isaf_lock); 29190Sstevel@tonic-gate larval = ipsec_getassocbyspi(inbound, assoc->sadb_sa_spi, 29200Sstevel@tonic-gate ALL_ZEROES_PTR, dstaddr, dst->sin_family); 29210Sstevel@tonic-gate mutex_exit(&inbound->isaf_lock); 29220Sstevel@tonic-gate 29230Sstevel@tonic-gate if (larval == NULL) { 29240Sstevel@tonic-gate esp0dbg(("Larval update, but larval disappeared.\n")); 29250Sstevel@tonic-gate return (ESRCH); 29260Sstevel@tonic-gate } /* Else sadb_common_add unlinks it for me! */ 29270Sstevel@tonic-gate } 29280Sstevel@tonic-gate 29290Sstevel@tonic-gate lpkt = NULL; 29300Sstevel@tonic-gate if (larval != NULL) 29310Sstevel@tonic-gate lpkt = sadb_clear_lpkt(larval); 29320Sstevel@tonic-gate 29330Sstevel@tonic-gate rc = sadb_common_add(esp_sadb.s_ip_q, esp_pfkey_q, mp, samsg, ksi, 29340Sstevel@tonic-gate primary, secondary, larval, clone, is_inbound); 29350Sstevel@tonic-gate 29360Sstevel@tonic-gate if (rc == 0 && lpkt != NULL) { 29370Sstevel@tonic-gate rc = !taskq_dispatch(esp_taskq, inbound_task, 29380Sstevel@tonic-gate (void *) lpkt, TQ_NOSLEEP); 29390Sstevel@tonic-gate } 29400Sstevel@tonic-gate 29410Sstevel@tonic-gate if (rc != 0) { 29420Sstevel@tonic-gate ip_drop_packet(lpkt, B_TRUE, NULL, NULL, 29430Sstevel@tonic-gate &ipdrops_sadb_inlarval_timeout, &esp_dropper); 29440Sstevel@tonic-gate } 29450Sstevel@tonic-gate 29460Sstevel@tonic-gate /* 29470Sstevel@tonic-gate * How much more stack will I create with all of these 29480Sstevel@tonic-gate * esp_outbound() calls? 29490Sstevel@tonic-gate */ 29500Sstevel@tonic-gate 29510Sstevel@tonic-gate while (acq_msgs != NULL) { 29520Sstevel@tonic-gate mblk_t *mp = acq_msgs; 29530Sstevel@tonic-gate 29540Sstevel@tonic-gate acq_msgs = acq_msgs->b_next; 29550Sstevel@tonic-gate mp->b_next = NULL; 29560Sstevel@tonic-gate if (rc == 0) { 29570Sstevel@tonic-gate if (ipsec_outbound_sa(mp, IPPROTO_ESP)) { 29580Sstevel@tonic-gate ((ipsec_out_t *)(mp->b_rptr))-> 29590Sstevel@tonic-gate ipsec_out_esp_done = B_TRUE; 29600Sstevel@tonic-gate if (esp_outbound(mp) == IPSEC_STATUS_SUCCESS) { 29610Sstevel@tonic-gate ipha_t *ipha = (ipha_t *) 29620Sstevel@tonic-gate mp->b_cont->b_rptr; 29630Sstevel@tonic-gate 29640Sstevel@tonic-gate /* do AH processing if needed */ 29650Sstevel@tonic-gate if (!esp_do_outbound_ah(mp)) 29660Sstevel@tonic-gate continue; 29670Sstevel@tonic-gate 29680Sstevel@tonic-gate /* finish IPsec processing */ 29690Sstevel@tonic-gate if (is_ipv4) { 29700Sstevel@tonic-gate ip_wput_ipsec_out(NULL, mp, 29710Sstevel@tonic-gate ipha, NULL, NULL); 29720Sstevel@tonic-gate } else { 29730Sstevel@tonic-gate ip6_t *ip6h = (ip6_t *)ipha; 29740Sstevel@tonic-gate ip_wput_ipsec_out_v6(NULL, 29750Sstevel@tonic-gate mp, ip6h, NULL, NULL); 29760Sstevel@tonic-gate } 29770Sstevel@tonic-gate } 29780Sstevel@tonic-gate continue; 29790Sstevel@tonic-gate } 29800Sstevel@tonic-gate } 29810Sstevel@tonic-gate ESP_BUMP_STAT(out_discards); 29820Sstevel@tonic-gate ip_drop_packet(mp, B_FALSE, NULL, NULL, 29830Sstevel@tonic-gate &ipdrops_sadb_acquire_timeout, &esp_dropper); 29840Sstevel@tonic-gate } 29850Sstevel@tonic-gate 29860Sstevel@tonic-gate return (rc); 29870Sstevel@tonic-gate } 29880Sstevel@tonic-gate 29890Sstevel@tonic-gate /* 29900Sstevel@tonic-gate * Add new ESP security association. This may become a generic AH/ESP 29910Sstevel@tonic-gate * routine eventually. 29920Sstevel@tonic-gate */ 29930Sstevel@tonic-gate static int 29940Sstevel@tonic-gate esp_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic) 29950Sstevel@tonic-gate { 29960Sstevel@tonic-gate sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 29970Sstevel@tonic-gate sadb_address_t *srcext = 29980Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC]; 29990Sstevel@tonic-gate sadb_address_t *dstext = 30000Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 30010Sstevel@tonic-gate sadb_address_t *nttext_loc = 30020Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_LOC]; 30030Sstevel@tonic-gate sadb_address_t *nttext_rem = 30040Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_REM]; 30050Sstevel@tonic-gate sadb_key_t *akey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH]; 30060Sstevel@tonic-gate sadb_key_t *ekey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT]; 30070Sstevel@tonic-gate struct sockaddr_in *src, *dst; 30080Sstevel@tonic-gate struct sockaddr_in *natt_loc, *natt_rem; 30090Sstevel@tonic-gate struct sockaddr_in6 *natt_loc6, *natt_rem6; 30100Sstevel@tonic-gate 30110Sstevel@tonic-gate sadb_lifetime_t *soft = 30120Sstevel@tonic-gate (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT]; 30130Sstevel@tonic-gate sadb_lifetime_t *hard = 30140Sstevel@tonic-gate (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD]; 30150Sstevel@tonic-gate 30160Sstevel@tonic-gate /* I need certain extensions present for an ADD message. */ 30170Sstevel@tonic-gate if (srcext == NULL) { 30180Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC; 30190Sstevel@tonic-gate return (EINVAL); 30200Sstevel@tonic-gate } 30210Sstevel@tonic-gate if (dstext == NULL) { 30220Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST; 30230Sstevel@tonic-gate return (EINVAL); 30240Sstevel@tonic-gate } 30250Sstevel@tonic-gate if (assoc == NULL) { 30260Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA; 30270Sstevel@tonic-gate return (EINVAL); 30280Sstevel@tonic-gate } 30290Sstevel@tonic-gate if (ekey == NULL && assoc->sadb_sa_encrypt != SADB_EALG_NULL) { 30300Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_EKEY; 30310Sstevel@tonic-gate return (EINVAL); 30320Sstevel@tonic-gate } 30330Sstevel@tonic-gate 30340Sstevel@tonic-gate src = (struct sockaddr_in *)(srcext + 1); 30350Sstevel@tonic-gate dst = (struct sockaddr_in *)(dstext + 1); 30360Sstevel@tonic-gate natt_loc = (struct sockaddr_in *)(nttext_loc + 1); 30370Sstevel@tonic-gate natt_loc6 = (struct sockaddr_in6 *)(nttext_loc + 1); 30380Sstevel@tonic-gate natt_rem = (struct sockaddr_in *)(nttext_rem + 1); 30390Sstevel@tonic-gate natt_rem6 = (struct sockaddr_in6 *)(nttext_rem + 1); 30400Sstevel@tonic-gate 30410Sstevel@tonic-gate /* Sundry ADD-specific reality checks. */ 30420Sstevel@tonic-gate /* XXX STATS : Logging/stats here? */ 30430Sstevel@tonic-gate if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) { 30440Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE; 30450Sstevel@tonic-gate return (EINVAL); 30460Sstevel@tonic-gate } 30470Sstevel@tonic-gate if (assoc->sadb_sa_encrypt == SADB_EALG_NONE) { 30480Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG; 30490Sstevel@tonic-gate return (EINVAL); 30500Sstevel@tonic-gate } 30510Sstevel@tonic-gate 30520Sstevel@tonic-gate if (assoc->sadb_sa_encrypt == SADB_EALG_NULL && 30530Sstevel@tonic-gate assoc->sadb_sa_auth == SADB_AALG_NONE) { 30540Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG; 30550Sstevel@tonic-gate return (EINVAL); 30560Sstevel@tonic-gate } 30570Sstevel@tonic-gate 30580Sstevel@tonic-gate if (assoc->sadb_sa_flags & ~(SADB_SAFLAGS_NOREPLAY | 30590Sstevel@tonic-gate SADB_X_SAFLAGS_NATT_LOC | SADB_X_SAFLAGS_NATT_REM)) { 30600Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS; 30610Sstevel@tonic-gate return (EINVAL); 30620Sstevel@tonic-gate } 30630Sstevel@tonic-gate 30640Sstevel@tonic-gate if ((*diagnostic = sadb_hardsoftchk(hard, soft)) != 0) { 30650Sstevel@tonic-gate return (EINVAL); 30660Sstevel@tonic-gate } 30670Sstevel@tonic-gate if (src->sin_family != dst->sin_family) { 30680Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_AF_MISMATCH; 30690Sstevel@tonic-gate return (EINVAL); 30700Sstevel@tonic-gate } 30710Sstevel@tonic-gate 30720Sstevel@tonic-gate 30730Sstevel@tonic-gate if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC) { 30740Sstevel@tonic-gate if (nttext_loc == NULL) { 30750Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_LOC; 30760Sstevel@tonic-gate return (EINVAL); 30770Sstevel@tonic-gate } 30780Sstevel@tonic-gate 30790Sstevel@tonic-gate if (natt_loc->sin_family == AF_INET6 && 30800Sstevel@tonic-gate !IN6_IS_ADDR_V4MAPPED(&natt_loc6->sin6_addr)) { 30810Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC; 30820Sstevel@tonic-gate return (EINVAL); 30830Sstevel@tonic-gate } 30840Sstevel@tonic-gate } 30850Sstevel@tonic-gate 30860Sstevel@tonic-gate if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM) { 30870Sstevel@tonic-gate if (nttext_rem == NULL) { 30880Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_REM; 30890Sstevel@tonic-gate return (EINVAL); 30900Sstevel@tonic-gate } 30910Sstevel@tonic-gate if (natt_rem->sin_family == AF_INET6 && 30920Sstevel@tonic-gate !IN6_IS_ADDR_V4MAPPED(&natt_rem6->sin6_addr)) { 30930Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM; 30940Sstevel@tonic-gate return (EINVAL); 30950Sstevel@tonic-gate } 30960Sstevel@tonic-gate } 30970Sstevel@tonic-gate 30980Sstevel@tonic-gate 30990Sstevel@tonic-gate /* Stuff I don't support, for now. XXX Diagnostic? */ 31000Sstevel@tonic-gate if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL || 31010Sstevel@tonic-gate ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL) 31020Sstevel@tonic-gate return (EOPNOTSUPP); 31030Sstevel@tonic-gate 31040Sstevel@tonic-gate /* 31050Sstevel@tonic-gate * XXX Policy : I'm not checking identities or sensitivity 31060Sstevel@tonic-gate * labels at this time, but if I did, I'd do them here, before I sent 31070Sstevel@tonic-gate * the weak key check up to the algorithm. 31080Sstevel@tonic-gate */ 31090Sstevel@tonic-gate 31100Sstevel@tonic-gate mutex_enter(&alg_lock); 31110Sstevel@tonic-gate 31120Sstevel@tonic-gate /* 31130Sstevel@tonic-gate * First locate the authentication algorithm. 31140Sstevel@tonic-gate */ 31150Sstevel@tonic-gate if (akey != NULL) { 31160Sstevel@tonic-gate ipsec_alginfo_t *aalg; 31170Sstevel@tonic-gate 31180Sstevel@tonic-gate aalg = ipsec_alglists[IPSEC_ALG_AUTH][assoc->sadb_sa_auth]; 31190Sstevel@tonic-gate if (aalg == NULL || !ALG_VALID(aalg)) { 31200Sstevel@tonic-gate mutex_exit(&alg_lock); 31210Sstevel@tonic-gate esp1dbg(("Couldn't find auth alg #%d.\n", 31220Sstevel@tonic-gate assoc->sadb_sa_auth)); 31230Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG; 31240Sstevel@tonic-gate return (EINVAL); 31250Sstevel@tonic-gate } 31260Sstevel@tonic-gate ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID); 31270Sstevel@tonic-gate 31280Sstevel@tonic-gate /* sanity check key sizes */ 31290Sstevel@tonic-gate if (!ipsec_valid_key_size(akey->sadb_key_bits, aalg)) { 31300Sstevel@tonic-gate mutex_exit(&alg_lock); 31310Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS; 31320Sstevel@tonic-gate return (EINVAL); 31330Sstevel@tonic-gate } 31340Sstevel@tonic-gate 31350Sstevel@tonic-gate /* check key and fix parity if needed */ 31360Sstevel@tonic-gate if (ipsec_check_key(aalg->alg_mech_type, akey, B_TRUE, 31370Sstevel@tonic-gate diagnostic) != 0) { 31380Sstevel@tonic-gate mutex_exit(&alg_lock); 31390Sstevel@tonic-gate return (EINVAL); 31400Sstevel@tonic-gate } 31410Sstevel@tonic-gate } 31420Sstevel@tonic-gate 31430Sstevel@tonic-gate /* 31440Sstevel@tonic-gate * Then locate the encryption algorithm. 31450Sstevel@tonic-gate */ 31460Sstevel@tonic-gate if (ekey != NULL) { 31470Sstevel@tonic-gate ipsec_alginfo_t *ealg; 31480Sstevel@tonic-gate 31490Sstevel@tonic-gate ealg = ipsec_alglists[IPSEC_ALG_ENCR][assoc->sadb_sa_encrypt]; 31500Sstevel@tonic-gate if (ealg == NULL || !ALG_VALID(ealg)) { 31510Sstevel@tonic-gate mutex_exit(&alg_lock); 31520Sstevel@tonic-gate esp1dbg(("Couldn't find encr alg #%d.\n", 31530Sstevel@tonic-gate assoc->sadb_sa_encrypt)); 31540Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG; 31550Sstevel@tonic-gate return (EINVAL); 31560Sstevel@tonic-gate } 31570Sstevel@tonic-gate ASSERT(ealg->alg_mech_type != CRYPTO_MECHANISM_INVALID); 31580Sstevel@tonic-gate 31590Sstevel@tonic-gate /* sanity check key sizes */ 31600Sstevel@tonic-gate if (!ipsec_valid_key_size(ekey->sadb_key_bits, ealg)) { 31610Sstevel@tonic-gate mutex_exit(&alg_lock); 31620Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_EKEYBITS; 31630Sstevel@tonic-gate return (EINVAL); 31640Sstevel@tonic-gate } 31650Sstevel@tonic-gate 31660Sstevel@tonic-gate /* check key */ 31670Sstevel@tonic-gate if (ipsec_check_key(ealg->alg_mech_type, ekey, B_FALSE, 31680Sstevel@tonic-gate diagnostic) != 0) { 31690Sstevel@tonic-gate mutex_exit(&alg_lock); 31700Sstevel@tonic-gate return (EINVAL); 31710Sstevel@tonic-gate } 31720Sstevel@tonic-gate } 31730Sstevel@tonic-gate mutex_exit(&alg_lock); 31740Sstevel@tonic-gate 31750Sstevel@tonic-gate return (esp_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi)); 31760Sstevel@tonic-gate } 31770Sstevel@tonic-gate 31780Sstevel@tonic-gate /* 31790Sstevel@tonic-gate * Update a security association. Updates come in two varieties. The first 31800Sstevel@tonic-gate * is an update of lifetimes on a non-larval SA. The second is an update of 31810Sstevel@tonic-gate * a larval SA, which ends up looking a lot more like an add. 31820Sstevel@tonic-gate */ 31830Sstevel@tonic-gate static int 31840Sstevel@tonic-gate esp_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic) 31850Sstevel@tonic-gate { 31860Sstevel@tonic-gate sadb_address_t *dstext = 31870Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 31880Sstevel@tonic-gate struct sockaddr_in *sin; 31890Sstevel@tonic-gate 31900Sstevel@tonic-gate if (dstext == NULL) { 31910Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST; 31920Sstevel@tonic-gate return (EINVAL); 31930Sstevel@tonic-gate } 31940Sstevel@tonic-gate 31950Sstevel@tonic-gate sin = (struct sockaddr_in *)(dstext + 1); 31960Sstevel@tonic-gate return (sadb_update_sa(mp, ksi, 31970Sstevel@tonic-gate (sin->sin_family == AF_INET6) ? &esp_sadb.s_v6 : &esp_sadb.s_v4, 31980Sstevel@tonic-gate diagnostic, esp_pfkey_q, esp_add_sa)); 31990Sstevel@tonic-gate } 32000Sstevel@tonic-gate 32010Sstevel@tonic-gate /* 32020Sstevel@tonic-gate * Delete a security association. This is REALLY likely to be code common to 32030Sstevel@tonic-gate * both AH and ESP. Find the association, then unlink it. 32040Sstevel@tonic-gate */ 32050Sstevel@tonic-gate static int 32060Sstevel@tonic-gate esp_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic) 32070Sstevel@tonic-gate { 32080Sstevel@tonic-gate sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 32090Sstevel@tonic-gate sadb_address_t *dstext = 32100Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 32110Sstevel@tonic-gate sadb_address_t *srcext = 32120Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC]; 32130Sstevel@tonic-gate struct sockaddr_in *sin; 32140Sstevel@tonic-gate 32150Sstevel@tonic-gate if (assoc == NULL) { 32160Sstevel@tonic-gate if (dstext != NULL) { 32170Sstevel@tonic-gate sin = (struct sockaddr_in *)(dstext + 1); 32180Sstevel@tonic-gate } else if (srcext != NULL) { 32190Sstevel@tonic-gate sin = (struct sockaddr_in *)(srcext + 1); 32200Sstevel@tonic-gate } else { 32210Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA; 32220Sstevel@tonic-gate return (EINVAL); 32230Sstevel@tonic-gate } 32240Sstevel@tonic-gate return sadb_purge_sa(mp, ksi, 32250Sstevel@tonic-gate (sin->sin_family == AF_INET6) ? &esp_sadb.s_v6 : 32260Sstevel@tonic-gate &esp_sadb.s_v4, 32270Sstevel@tonic-gate diagnostic, esp_pfkey_q, esp_sadb.s_ip_q); 32280Sstevel@tonic-gate } 32290Sstevel@tonic-gate 32300Sstevel@tonic-gate return (sadb_del_sa(mp, ksi, &esp_sadb, diagnostic, esp_pfkey_q)); 32310Sstevel@tonic-gate } 32320Sstevel@tonic-gate 32330Sstevel@tonic-gate /* 32340Sstevel@tonic-gate * Convert the entire contents of all of ESP's SA tables into PF_KEY SADB_DUMP 32350Sstevel@tonic-gate * messages. 32360Sstevel@tonic-gate */ 32370Sstevel@tonic-gate static void 32380Sstevel@tonic-gate esp_dump(mblk_t *mp, keysock_in_t *ksi) 32390Sstevel@tonic-gate { 32400Sstevel@tonic-gate int error; 32410Sstevel@tonic-gate sadb_msg_t *samsg; 32420Sstevel@tonic-gate 32430Sstevel@tonic-gate /* 32440Sstevel@tonic-gate * Dump each fanout, bailing if error is non-zero. 32450Sstevel@tonic-gate */ 32460Sstevel@tonic-gate 32470Sstevel@tonic-gate error = sadb_dump(esp_pfkey_q, mp, ksi->ks_in_serial, &esp_sadb.s_v4); 32480Sstevel@tonic-gate if (error != 0) 32490Sstevel@tonic-gate goto bail; 32500Sstevel@tonic-gate 32510Sstevel@tonic-gate error = sadb_dump(esp_pfkey_q, mp, ksi->ks_in_serial, &esp_sadb.s_v6); 32520Sstevel@tonic-gate bail: 32530Sstevel@tonic-gate ASSERT(mp->b_cont != NULL); 32540Sstevel@tonic-gate samsg = (sadb_msg_t *)mp->b_cont->b_rptr; 32550Sstevel@tonic-gate samsg->sadb_msg_errno = (uint8_t)error; 32560Sstevel@tonic-gate sadb_pfkey_echo(esp_pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi, 32570Sstevel@tonic-gate NULL); 32580Sstevel@tonic-gate } 32590Sstevel@tonic-gate 32600Sstevel@tonic-gate /* 32610Sstevel@tonic-gate * ESP parsing of PF_KEY messages. Keysock did most of the really silly 32620Sstevel@tonic-gate * error cases. What I receive is a fully-formed, syntactically legal 32630Sstevel@tonic-gate * PF_KEY message. I then need to check semantics... 32640Sstevel@tonic-gate * 32650Sstevel@tonic-gate * This code may become common to AH and ESP. Stay tuned. 32660Sstevel@tonic-gate * 32670Sstevel@tonic-gate * I also make the assumption that db_ref's are cool. If this assumption 32680Sstevel@tonic-gate * is wrong, this means that someone other than keysock or me has been 32690Sstevel@tonic-gate * mucking with PF_KEY messages. 32700Sstevel@tonic-gate */ 32710Sstevel@tonic-gate static void 32720Sstevel@tonic-gate esp_parse_pfkey(mblk_t *mp) 32730Sstevel@tonic-gate { 32740Sstevel@tonic-gate mblk_t *msg = mp->b_cont; 32750Sstevel@tonic-gate sadb_msg_t *samsg; 32760Sstevel@tonic-gate keysock_in_t *ksi; 32770Sstevel@tonic-gate int error; 32780Sstevel@tonic-gate int diagnostic = SADB_X_DIAGNOSTIC_NONE; 32790Sstevel@tonic-gate 32800Sstevel@tonic-gate ASSERT(msg != NULL); 32810Sstevel@tonic-gate samsg = (sadb_msg_t *)msg->b_rptr; 32820Sstevel@tonic-gate ksi = (keysock_in_t *)mp->b_rptr; 32830Sstevel@tonic-gate 32840Sstevel@tonic-gate /* 32850Sstevel@tonic-gate * If applicable, convert unspecified AF_INET6 to unspecified 32860Sstevel@tonic-gate * AF_INET. 32870Sstevel@tonic-gate */ 32880Sstevel@tonic-gate sadb_srcaddrfix(ksi); 32890Sstevel@tonic-gate 32900Sstevel@tonic-gate switch (samsg->sadb_msg_type) { 32910Sstevel@tonic-gate case SADB_ADD: 32920Sstevel@tonic-gate error = esp_add_sa(mp, ksi, &diagnostic); 32930Sstevel@tonic-gate if (error != 0) { 32940Sstevel@tonic-gate sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic, 32950Sstevel@tonic-gate ksi->ks_in_serial); 32960Sstevel@tonic-gate } 32970Sstevel@tonic-gate /* else esp_add_sa() took care of things. */ 32980Sstevel@tonic-gate break; 32990Sstevel@tonic-gate case SADB_DELETE: 33000Sstevel@tonic-gate error = esp_del_sa(mp, ksi, &diagnostic); 33010Sstevel@tonic-gate if (error != 0) { 33020Sstevel@tonic-gate sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic, 33030Sstevel@tonic-gate ksi->ks_in_serial); 33040Sstevel@tonic-gate } 33050Sstevel@tonic-gate /* Else esp_del_sa() took care of things. */ 33060Sstevel@tonic-gate break; 33070Sstevel@tonic-gate case SADB_GET: 33080Sstevel@tonic-gate error = sadb_get_sa(mp, ksi, &esp_sadb, &diagnostic, 33090Sstevel@tonic-gate esp_pfkey_q); 33100Sstevel@tonic-gate if (error != 0) { 33110Sstevel@tonic-gate sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic, 33120Sstevel@tonic-gate ksi->ks_in_serial); 33130Sstevel@tonic-gate } 33140Sstevel@tonic-gate /* Else sadb_get_sa() took care of things. */ 33150Sstevel@tonic-gate break; 33160Sstevel@tonic-gate case SADB_FLUSH: 33170Sstevel@tonic-gate sadbp_flush(&esp_sadb); 33180Sstevel@tonic-gate sadb_pfkey_echo(esp_pfkey_q, mp, samsg, ksi, NULL); 33190Sstevel@tonic-gate break; 33200Sstevel@tonic-gate case SADB_REGISTER: 33210Sstevel@tonic-gate /* 33220Sstevel@tonic-gate * Hmmm, let's do it! Check for extensions (there should 33230Sstevel@tonic-gate * be none), extract the fields, call esp_register_out(), 33240Sstevel@tonic-gate * then either free or report an error. 33250Sstevel@tonic-gate * 33260Sstevel@tonic-gate * Keysock takes care of the PF_KEY bookkeeping for this. 33270Sstevel@tonic-gate */ 33280Sstevel@tonic-gate if (esp_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid, 33290Sstevel@tonic-gate ksi->ks_in_serial)) { 33300Sstevel@tonic-gate freemsg(mp); 33310Sstevel@tonic-gate } else { 33320Sstevel@tonic-gate /* 33330Sstevel@tonic-gate * Only way this path hits is if there is a memory 33340Sstevel@tonic-gate * failure. It will not return B_FALSE because of 33350Sstevel@tonic-gate * lack of esp_pfkey_q if I am in wput(). 33360Sstevel@tonic-gate */ 33370Sstevel@tonic-gate sadb_pfkey_error(esp_pfkey_q, mp, ENOMEM, diagnostic, 33380Sstevel@tonic-gate ksi->ks_in_serial); 33390Sstevel@tonic-gate } 33400Sstevel@tonic-gate break; 33410Sstevel@tonic-gate case SADB_UPDATE: 33420Sstevel@tonic-gate /* 33430Sstevel@tonic-gate * Find a larval, if not there, find a full one and get 33440Sstevel@tonic-gate * strict. 33450Sstevel@tonic-gate */ 33460Sstevel@tonic-gate error = esp_update_sa(mp, ksi, &diagnostic); 33470Sstevel@tonic-gate if (error != 0) { 33480Sstevel@tonic-gate sadb_pfkey_error(esp_pfkey_q, mp, error, diagnostic, 33490Sstevel@tonic-gate ksi->ks_in_serial); 33500Sstevel@tonic-gate } 33510Sstevel@tonic-gate /* else esp_update_sa() took care of things. */ 33520Sstevel@tonic-gate break; 33530Sstevel@tonic-gate case SADB_GETSPI: 33540Sstevel@tonic-gate /* 33550Sstevel@tonic-gate * Reserve a new larval entry. 33560Sstevel@tonic-gate */ 33570Sstevel@tonic-gate esp_getspi(mp, ksi); 33580Sstevel@tonic-gate break; 33590Sstevel@tonic-gate case SADB_ACQUIRE: 33600Sstevel@tonic-gate /* 33610Sstevel@tonic-gate * Find larval and/or ACQUIRE record and kill it (them), I'm 33620Sstevel@tonic-gate * most likely an error. Inbound ACQUIRE messages should only 33630Sstevel@tonic-gate * have the base header. 33640Sstevel@tonic-gate */ 33650Sstevel@tonic-gate sadb_in_acquire(samsg, &esp_sadb, esp_pfkey_q); 33660Sstevel@tonic-gate freemsg(mp); 33670Sstevel@tonic-gate break; 33680Sstevel@tonic-gate case SADB_DUMP: 33690Sstevel@tonic-gate /* 33700Sstevel@tonic-gate * Dump all entries. 33710Sstevel@tonic-gate */ 33720Sstevel@tonic-gate esp_dump(mp, ksi); 33730Sstevel@tonic-gate /* esp_dump will take care of the return message, etc. */ 33740Sstevel@tonic-gate break; 33750Sstevel@tonic-gate case SADB_EXPIRE: 33760Sstevel@tonic-gate /* Should never reach me. */ 33770Sstevel@tonic-gate sadb_pfkey_error(esp_pfkey_q, mp, EOPNOTSUPP, diagnostic, 33780Sstevel@tonic-gate ksi->ks_in_serial); 33790Sstevel@tonic-gate break; 33800Sstevel@tonic-gate default: 33810Sstevel@tonic-gate sadb_pfkey_error(esp_pfkey_q, mp, EINVAL, 33820Sstevel@tonic-gate SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial); 33830Sstevel@tonic-gate break; 33840Sstevel@tonic-gate } 33850Sstevel@tonic-gate } 33860Sstevel@tonic-gate 33870Sstevel@tonic-gate /* 33880Sstevel@tonic-gate * Handle case where PF_KEY says it can't find a keysock for one of my 33890Sstevel@tonic-gate * ACQUIRE messages. 33900Sstevel@tonic-gate */ 33910Sstevel@tonic-gate static void 33920Sstevel@tonic-gate esp_keysock_no_socket(mblk_t *mp) 33930Sstevel@tonic-gate { 33940Sstevel@tonic-gate sadb_msg_t *samsg; 33950Sstevel@tonic-gate keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr; 33960Sstevel@tonic-gate 33970Sstevel@tonic-gate if (mp->b_cont == NULL) { 33980Sstevel@tonic-gate freemsg(mp); 33990Sstevel@tonic-gate return; 34000Sstevel@tonic-gate } 34010Sstevel@tonic-gate samsg = (sadb_msg_t *)mp->b_cont->b_rptr; 34020Sstevel@tonic-gate 34030Sstevel@tonic-gate /* 34040Sstevel@tonic-gate * If keysock can't find any registered, delete the acquire record 34050Sstevel@tonic-gate * immediately, and handle errors. 34060Sstevel@tonic-gate */ 34070Sstevel@tonic-gate if (samsg->sadb_msg_type == SADB_ACQUIRE) { 34080Sstevel@tonic-gate samsg->sadb_msg_errno = kse->ks_err_errno; 34090Sstevel@tonic-gate samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg)); 34100Sstevel@tonic-gate /* 34110Sstevel@tonic-gate * Use the write-side of the esp_pfkey_q, in case there is 34120Sstevel@tonic-gate * no esp_sadb.s_ip_q. 34130Sstevel@tonic-gate */ 34140Sstevel@tonic-gate sadb_in_acquire(samsg, &esp_sadb, WR(esp_pfkey_q)); 34150Sstevel@tonic-gate } 34160Sstevel@tonic-gate 34170Sstevel@tonic-gate freemsg(mp); 34180Sstevel@tonic-gate } 34190Sstevel@tonic-gate 34200Sstevel@tonic-gate /* 34210Sstevel@tonic-gate * First-cut reality check for an inbound PF_KEY message. 34220Sstevel@tonic-gate */ 34230Sstevel@tonic-gate static boolean_t 34240Sstevel@tonic-gate esp_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi) 34250Sstevel@tonic-gate { 34260Sstevel@tonic-gate int diagnostic; 34270Sstevel@tonic-gate 34280Sstevel@tonic-gate if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) { 34290Sstevel@tonic-gate diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT; 34300Sstevel@tonic-gate goto badmsg; 34310Sstevel@tonic-gate } 34320Sstevel@tonic-gate if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL || 34330Sstevel@tonic-gate ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) { 34340Sstevel@tonic-gate diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT; 34350Sstevel@tonic-gate goto badmsg; 34360Sstevel@tonic-gate } 34370Sstevel@tonic-gate if (ksi->ks_in_srctype == KS_IN_ADDR_MBCAST) { 34380Sstevel@tonic-gate diagnostic = SADB_X_DIAGNOSTIC_BAD_SRC; 34390Sstevel@tonic-gate goto badmsg; 34400Sstevel@tonic-gate } 34410Sstevel@tonic-gate if (ksi->ks_in_dsttype == KS_IN_ADDR_UNSPEC) { 34420Sstevel@tonic-gate diagnostic = SADB_X_DIAGNOSTIC_BAD_DST; 34430Sstevel@tonic-gate goto badmsg; 34440Sstevel@tonic-gate } 34450Sstevel@tonic-gate 34460Sstevel@tonic-gate return (B_FALSE); /* False ==> no failures */ 34470Sstevel@tonic-gate 34480Sstevel@tonic-gate badmsg: 34490Sstevel@tonic-gate sadb_pfkey_error(esp_pfkey_q, mp, EINVAL, diagnostic, 34500Sstevel@tonic-gate ksi->ks_in_serial); 34510Sstevel@tonic-gate return (B_TRUE); /* True ==> failures */ 34520Sstevel@tonic-gate } 34530Sstevel@tonic-gate 34540Sstevel@tonic-gate /* 34550Sstevel@tonic-gate * ESP module write put routine. 34560Sstevel@tonic-gate */ 34570Sstevel@tonic-gate static void 34580Sstevel@tonic-gate ipsecesp_wput(queue_t *q, mblk_t *mp) 34590Sstevel@tonic-gate { 34600Sstevel@tonic-gate ipsec_info_t *ii; 34610Sstevel@tonic-gate keysock_in_t *ksi; 34620Sstevel@tonic-gate int rc; 34630Sstevel@tonic-gate struct iocblk *iocp; 34640Sstevel@tonic-gate 34650Sstevel@tonic-gate esp3dbg(("In esp_wput().\n")); 34660Sstevel@tonic-gate 34670Sstevel@tonic-gate /* NOTE: Each case must take care of freeing or passing mp. */ 34680Sstevel@tonic-gate switch (mp->b_datap->db_type) { 34690Sstevel@tonic-gate case M_CTL: 34700Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) { 34710Sstevel@tonic-gate /* Not big enough message. */ 34720Sstevel@tonic-gate freemsg(mp); 34730Sstevel@tonic-gate break; 34740Sstevel@tonic-gate } 34750Sstevel@tonic-gate ii = (ipsec_info_t *)mp->b_rptr; 34760Sstevel@tonic-gate 34770Sstevel@tonic-gate switch (ii->ipsec_info_type) { 34780Sstevel@tonic-gate case KEYSOCK_OUT_ERR: 34790Sstevel@tonic-gate esp1dbg(("Got KEYSOCK_OUT_ERR message.\n")); 34800Sstevel@tonic-gate esp_keysock_no_socket(mp); 34810Sstevel@tonic-gate break; 34820Sstevel@tonic-gate case KEYSOCK_IN: 34830Sstevel@tonic-gate ESP_BUMP_STAT(keysock_in); 34840Sstevel@tonic-gate esp3dbg(("Got KEYSOCK_IN message.\n")); 34850Sstevel@tonic-gate ksi = (keysock_in_t *)ii; 34860Sstevel@tonic-gate /* 34870Sstevel@tonic-gate * Some common reality checks. 34880Sstevel@tonic-gate */ 34890Sstevel@tonic-gate 34900Sstevel@tonic-gate if (esp_pfkey_reality_failures(mp, ksi)) 34910Sstevel@tonic-gate return; 34920Sstevel@tonic-gate 34930Sstevel@tonic-gate /* 34940Sstevel@tonic-gate * Use 'q' instead of esp_sadb.s_ip_q, since 34950Sstevel@tonic-gate * it's the write side already, and it'll go 34960Sstevel@tonic-gate * down to IP. Use esp_pfkey_q because we 34970Sstevel@tonic-gate * wouldn't get here if that weren't set, and 34980Sstevel@tonic-gate * the RD(q) has been done already. 34990Sstevel@tonic-gate */ 35000Sstevel@tonic-gate if (ksi->ks_in_srctype == KS_IN_ADDR_UNKNOWN) { 35010Sstevel@tonic-gate rc = sadb_addrcheck(q, esp_pfkey_q, mp, 35020Sstevel@tonic-gate ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC], 35030Sstevel@tonic-gate ksi->ks_in_serial); 35040Sstevel@tonic-gate if (rc == KS_IN_ADDR_UNKNOWN) 35050Sstevel@tonic-gate return; 35060Sstevel@tonic-gate else 35070Sstevel@tonic-gate ksi->ks_in_srctype = rc; 35080Sstevel@tonic-gate } 35090Sstevel@tonic-gate if (ksi->ks_in_dsttype == KS_IN_ADDR_UNKNOWN) { 35100Sstevel@tonic-gate rc = sadb_addrcheck(q, esp_pfkey_q, mp, 35110Sstevel@tonic-gate ksi->ks_in_extv[SADB_EXT_ADDRESS_DST], 35120Sstevel@tonic-gate ksi->ks_in_serial); 35130Sstevel@tonic-gate if (rc == KS_IN_ADDR_UNKNOWN) 35140Sstevel@tonic-gate return; 35150Sstevel@tonic-gate else 35160Sstevel@tonic-gate ksi->ks_in_dsttype = rc; 35170Sstevel@tonic-gate } 35180Sstevel@tonic-gate /* 35190Sstevel@tonic-gate * XXX Proxy may be a different address family. 35200Sstevel@tonic-gate */ 35210Sstevel@tonic-gate if (ksi->ks_in_proxytype == KS_IN_ADDR_UNKNOWN) { 35220Sstevel@tonic-gate rc = sadb_addrcheck(q, esp_pfkey_q, mp, 35230Sstevel@tonic-gate ksi->ks_in_extv[SADB_EXT_ADDRESS_PROXY], 35240Sstevel@tonic-gate ksi->ks_in_serial); 35250Sstevel@tonic-gate if (rc == KS_IN_ADDR_UNKNOWN) 35260Sstevel@tonic-gate return; 35270Sstevel@tonic-gate else 35280Sstevel@tonic-gate ksi->ks_in_proxytype = rc; 35290Sstevel@tonic-gate } 35300Sstevel@tonic-gate esp_parse_pfkey(mp); 35310Sstevel@tonic-gate break; 35320Sstevel@tonic-gate case KEYSOCK_HELLO: 35330Sstevel@tonic-gate sadb_keysock_hello(&esp_pfkey_q, q, mp, 35340Sstevel@tonic-gate esp_ager, &esp_event, SADB_SATYPE_ESP); 35350Sstevel@tonic-gate break; 35360Sstevel@tonic-gate default: 35370Sstevel@tonic-gate esp2dbg(("Got M_CTL from above of 0x%x.\n", 35380Sstevel@tonic-gate ii->ipsec_info_type)); 35390Sstevel@tonic-gate freemsg(mp); 35400Sstevel@tonic-gate break; 35410Sstevel@tonic-gate } 35420Sstevel@tonic-gate break; 35430Sstevel@tonic-gate case M_IOCTL: 35440Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 35450Sstevel@tonic-gate switch (iocp->ioc_cmd) { 35460Sstevel@tonic-gate case ND_SET: 35470Sstevel@tonic-gate case ND_GET: 35480Sstevel@tonic-gate if (nd_getset(q, ipsecesp_g_nd, mp)) { 35490Sstevel@tonic-gate qreply(q, mp); 35500Sstevel@tonic-gate return; 35510Sstevel@tonic-gate } else { 35520Sstevel@tonic-gate iocp->ioc_error = ENOENT; 35530Sstevel@tonic-gate } 35540Sstevel@tonic-gate /* FALLTHRU */ 35550Sstevel@tonic-gate default: 35560Sstevel@tonic-gate /* We really don't support any other ioctls, do we? */ 35570Sstevel@tonic-gate 35580Sstevel@tonic-gate /* Return EINVAL */ 35590Sstevel@tonic-gate if (iocp->ioc_error != ENOENT) 35600Sstevel@tonic-gate iocp->ioc_error = EINVAL; 35610Sstevel@tonic-gate iocp->ioc_count = 0; 35620Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 35630Sstevel@tonic-gate qreply(q, mp); 35640Sstevel@tonic-gate return; 35650Sstevel@tonic-gate } 35660Sstevel@tonic-gate default: 35670Sstevel@tonic-gate esp3dbg(("Got default message, type %d, passing to IP.\n", 35680Sstevel@tonic-gate mp->b_datap->db_type)); 35690Sstevel@tonic-gate putnext(q, mp); 35700Sstevel@tonic-gate } 35710Sstevel@tonic-gate } 35720Sstevel@tonic-gate 35730Sstevel@tonic-gate /* 35740Sstevel@tonic-gate * Process an outbound ESP packet that can be accelerated by a IPsec 35750Sstevel@tonic-gate * hardware acceleration capable Provider. 35760Sstevel@tonic-gate * The caller already inserted and initialized the ESP header. 35770Sstevel@tonic-gate * This function allocates a tagging M_CTL, and adds room at the end 35780Sstevel@tonic-gate * of the packet to hold the ICV if authentication is needed. 35790Sstevel@tonic-gate * 35800Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the 35810Sstevel@tonic-gate * mblk chain ipsec_out. 35820Sstevel@tonic-gate */ 35830Sstevel@tonic-gate static ipsec_status_t 35840Sstevel@tonic-gate esp_outbound_accelerated(mblk_t *ipsec_out, uint_t icv_len) 35850Sstevel@tonic-gate { 35860Sstevel@tonic-gate ipsec_out_t *io; 35870Sstevel@tonic-gate mblk_t *lastmp; 35880Sstevel@tonic-gate 35890Sstevel@tonic-gate ESP_BUMP_STAT(out_accelerated); 35900Sstevel@tonic-gate 35910Sstevel@tonic-gate io = (ipsec_out_t *)ipsec_out->b_rptr; 35920Sstevel@tonic-gate 35930Sstevel@tonic-gate /* mark packet as being accelerated in IPSEC_OUT */ 35940Sstevel@tonic-gate ASSERT(io->ipsec_out_accelerated == B_FALSE); 35950Sstevel@tonic-gate io->ipsec_out_accelerated = B_TRUE; 35960Sstevel@tonic-gate 35970Sstevel@tonic-gate /* 35980Sstevel@tonic-gate * add room at the end of the packet for the ICV if needed 35990Sstevel@tonic-gate */ 36000Sstevel@tonic-gate if (icv_len > 0) { 36010Sstevel@tonic-gate /* go to last mblk */ 36020Sstevel@tonic-gate lastmp = ipsec_out; /* For following while loop. */ 36030Sstevel@tonic-gate do { 36040Sstevel@tonic-gate lastmp = lastmp->b_cont; 36050Sstevel@tonic-gate } while (lastmp->b_cont != NULL); 36060Sstevel@tonic-gate 36070Sstevel@tonic-gate /* if not enough available room, allocate new mblk */ 36080Sstevel@tonic-gate if ((lastmp->b_wptr + icv_len) > lastmp->b_datap->db_lim) { 36090Sstevel@tonic-gate lastmp->b_cont = allocb(icv_len, BPRI_HI); 36100Sstevel@tonic-gate if (lastmp->b_cont == NULL) { 36110Sstevel@tonic-gate ESP_BUMP_STAT(out_discards); 36120Sstevel@tonic-gate ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL, 36130Sstevel@tonic-gate &ipdrops_esp_nomem, &esp_dropper); 36140Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 36150Sstevel@tonic-gate } 36160Sstevel@tonic-gate lastmp = lastmp->b_cont; 36170Sstevel@tonic-gate } 36180Sstevel@tonic-gate lastmp->b_wptr += icv_len; 36190Sstevel@tonic-gate } 36200Sstevel@tonic-gate 36210Sstevel@tonic-gate return (IPSEC_STATUS_SUCCESS); 36220Sstevel@tonic-gate } 36230Sstevel@tonic-gate 36240Sstevel@tonic-gate /* 36250Sstevel@tonic-gate * Process an inbound accelerated ESP packet. 36260Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the 36270Sstevel@tonic-gate * mblk chain ipsec_in. 36280Sstevel@tonic-gate */ 36290Sstevel@tonic-gate static ipsec_status_t 36300Sstevel@tonic-gate esp_inbound_accelerated(mblk_t *ipsec_in, mblk_t *data_mp, boolean_t isv4, 36310Sstevel@tonic-gate ipsa_t *assoc) 36320Sstevel@tonic-gate { 36330Sstevel@tonic-gate ipsec_in_t *ii; 36340Sstevel@tonic-gate mblk_t *hada_mp; 36350Sstevel@tonic-gate uint32_t icv_len = 0; 36360Sstevel@tonic-gate da_ipsec_t *hada; 36370Sstevel@tonic-gate ipha_t *ipha; 36380Sstevel@tonic-gate ip6_t *ip6h; 36390Sstevel@tonic-gate kstat_named_t *counter; 36400Sstevel@tonic-gate 36410Sstevel@tonic-gate ESP_BUMP_STAT(in_accelerated); 36420Sstevel@tonic-gate 36430Sstevel@tonic-gate ii = (ipsec_in_t *)ipsec_in->b_rptr; 36440Sstevel@tonic-gate hada_mp = ii->ipsec_in_da; 36450Sstevel@tonic-gate ASSERT(hada_mp != NULL); 36460Sstevel@tonic-gate hada = (da_ipsec_t *)hada_mp->b_rptr; 36470Sstevel@tonic-gate 36480Sstevel@tonic-gate /* 36490Sstevel@tonic-gate * We only support one level of decapsulation in hardware, so 36500Sstevel@tonic-gate * nuke the pointer. 36510Sstevel@tonic-gate */ 36520Sstevel@tonic-gate ii->ipsec_in_da = NULL; 36530Sstevel@tonic-gate ii->ipsec_in_accelerated = B_FALSE; 36540Sstevel@tonic-gate 36550Sstevel@tonic-gate if (assoc->ipsa_auth_alg != IPSA_AALG_NONE) { 36560Sstevel@tonic-gate /* 36570Sstevel@tonic-gate * ESP with authentication. We expect the Provider to have 36580Sstevel@tonic-gate * computed the ICV and placed it in the hardware acceleration 36590Sstevel@tonic-gate * data attributes. 36600Sstevel@tonic-gate * 36610Sstevel@tonic-gate * Extract ICV length from attributes M_CTL and sanity check 36620Sstevel@tonic-gate * its value. We allow the mblk to be smaller than da_ipsec_t 36630Sstevel@tonic-gate * for a small ICV, as long as the entire ICV fits within the 36640Sstevel@tonic-gate * mblk. 36650Sstevel@tonic-gate * 36660Sstevel@tonic-gate * Also ensures that the ICV length computed by Provider 36670Sstevel@tonic-gate * corresponds to the ICV length of the agorithm specified by 36680Sstevel@tonic-gate * the SA. 36690Sstevel@tonic-gate */ 36700Sstevel@tonic-gate icv_len = hada->da_icv_len; 36710Sstevel@tonic-gate if ((icv_len != assoc->ipsa_mac_len) || 36720Sstevel@tonic-gate (icv_len > DA_ICV_MAX_LEN) || (MBLKL(hada_mp) < 36730Sstevel@tonic-gate (sizeof (da_ipsec_t) - DA_ICV_MAX_LEN + icv_len))) { 36740Sstevel@tonic-gate esp0dbg(("esp_inbound_accelerated: " 36750Sstevel@tonic-gate "ICV len (%u) incorrect or mblk too small (%u)\n", 36760Sstevel@tonic-gate icv_len, (uint32_t)(MBLKL(hada_mp)))); 36770Sstevel@tonic-gate counter = &ipdrops_esp_bad_auth; 36780Sstevel@tonic-gate goto esp_in_discard; 36790Sstevel@tonic-gate } 36800Sstevel@tonic-gate } 36810Sstevel@tonic-gate 36820Sstevel@tonic-gate /* get pointers to IP header */ 36830Sstevel@tonic-gate if (isv4) { 36840Sstevel@tonic-gate ipha = (ipha_t *)data_mp->b_rptr; 36850Sstevel@tonic-gate } else { 36860Sstevel@tonic-gate ip6h = (ip6_t *)data_mp->b_rptr; 36870Sstevel@tonic-gate } 36880Sstevel@tonic-gate 36890Sstevel@tonic-gate /* 36900Sstevel@tonic-gate * Compare ICV in ESP packet vs ICV computed by adapter. 36910Sstevel@tonic-gate * We also remove the ICV from the end of the packet since 36920Sstevel@tonic-gate * it will no longer be needed. 36930Sstevel@tonic-gate * 36940Sstevel@tonic-gate * Assume that esp_inbound() already ensured that the pkt 36950Sstevel@tonic-gate * was in one mblk. 36960Sstevel@tonic-gate */ 36970Sstevel@tonic-gate ASSERT(data_mp->b_cont == NULL); 36980Sstevel@tonic-gate data_mp->b_wptr -= icv_len; 36990Sstevel@tonic-gate /* adjust IP header */ 37000Sstevel@tonic-gate if (isv4) 37010Sstevel@tonic-gate ipha->ipha_length = htons(ntohs(ipha->ipha_length) - icv_len); 37020Sstevel@tonic-gate else 37030Sstevel@tonic-gate ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - icv_len); 37040Sstevel@tonic-gate if (icv_len && bcmp(hada->da_icv, data_mp->b_wptr, icv_len)) { 37050Sstevel@tonic-gate int af; 37060Sstevel@tonic-gate void *addr; 37070Sstevel@tonic-gate 37080Sstevel@tonic-gate if (isv4) { 37090Sstevel@tonic-gate addr = &ipha->ipha_dst; 37100Sstevel@tonic-gate af = AF_INET; 37110Sstevel@tonic-gate } else { 37120Sstevel@tonic-gate addr = &ip6h->ip6_dst; 37130Sstevel@tonic-gate af = AF_INET6; 37140Sstevel@tonic-gate } 37150Sstevel@tonic-gate 37160Sstevel@tonic-gate /* 37170Sstevel@tonic-gate * Log the event. Don't print to the console, block 37180Sstevel@tonic-gate * potential denial-of-service attack. 37190Sstevel@tonic-gate */ 37200Sstevel@tonic-gate ESP_BUMP_STAT(bad_auth); 37210Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 37220Sstevel@tonic-gate "ESP Authentication failed spi %x, dst_addr %s", 37230Sstevel@tonic-gate assoc->ipsa_spi, addr, af); 37240Sstevel@tonic-gate counter = &ipdrops_esp_bad_auth; 37250Sstevel@tonic-gate goto esp_in_discard; 37260Sstevel@tonic-gate } 37270Sstevel@tonic-gate 37280Sstevel@tonic-gate esp3dbg(("esp_inbound_accelerated: ESP authentication succeeded, " 37290Sstevel@tonic-gate "checking replay\n")); 37300Sstevel@tonic-gate 37310Sstevel@tonic-gate ipsec_in->b_cont = data_mp; 37320Sstevel@tonic-gate 37330Sstevel@tonic-gate /* 37340Sstevel@tonic-gate * Remove ESP header and padding from packet. 37350Sstevel@tonic-gate */ 37360Sstevel@tonic-gate if (!esp_strip_header(data_mp, ii->ipsec_in_v4, assoc->ipsa_iv_len, 37370Sstevel@tonic-gate &counter)) { 37380Sstevel@tonic-gate esp1dbg(("esp_inbound_accelerated: " 37390Sstevel@tonic-gate "esp_strip_header() failed\n")); 37400Sstevel@tonic-gate goto esp_in_discard; 37410Sstevel@tonic-gate } 37420Sstevel@tonic-gate 37430Sstevel@tonic-gate freeb(hada_mp); 37440Sstevel@tonic-gate 37450Sstevel@tonic-gate /* 37460Sstevel@tonic-gate * Account for usage.. 37470Sstevel@tonic-gate */ 37480Sstevel@tonic-gate if (!esp_age_bytes(assoc, msgdsize(data_mp), B_TRUE)) { 37490Sstevel@tonic-gate /* The ipsa has hit hard expiration, LOG and AUDIT. */ 37500Sstevel@tonic-gate ESP_BUMP_STAT(bytes_expired); 37510Sstevel@tonic-gate IP_ESP_BUMP_STAT(in_discards); 37520Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 37530Sstevel@tonic-gate "ESP association 0x%x, dst %s had bytes expire.\n", 37540Sstevel@tonic-gate assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam); 37550Sstevel@tonic-gate ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, 37560Sstevel@tonic-gate &ipdrops_esp_bytes_expire, &esp_dropper); 37570Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 37580Sstevel@tonic-gate } 37590Sstevel@tonic-gate 37600Sstevel@tonic-gate /* done processing the packet */ 37610Sstevel@tonic-gate return (IPSEC_STATUS_SUCCESS); 37620Sstevel@tonic-gate 37630Sstevel@tonic-gate esp_in_discard: 37640Sstevel@tonic-gate IP_ESP_BUMP_STAT(in_discards); 37650Sstevel@tonic-gate freeb(hada_mp); 37660Sstevel@tonic-gate 37670Sstevel@tonic-gate ipsec_in->b_cont = data_mp; /* For ip_drop_packet()'s sake... */ 37680Sstevel@tonic-gate ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter, &esp_dropper); 37690Sstevel@tonic-gate 37700Sstevel@tonic-gate return (IPSEC_STATUS_FAILED); 37710Sstevel@tonic-gate } 37720Sstevel@tonic-gate 37730Sstevel@tonic-gate /* 37740Sstevel@tonic-gate * Wrapper to allow IP to trigger an ESP association failure message 37750Sstevel@tonic-gate * during inbound SA selection. 37760Sstevel@tonic-gate */ 37770Sstevel@tonic-gate void 37780Sstevel@tonic-gate ipsecesp_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt, 37790Sstevel@tonic-gate uint32_t spi, void *addr, int af) 37800Sstevel@tonic-gate { 37810Sstevel@tonic-gate if (ipsecesp_log_unknown_spi) { 37820Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi, 37830Sstevel@tonic-gate addr, af); 37840Sstevel@tonic-gate } 37850Sstevel@tonic-gate 37860Sstevel@tonic-gate ip_drop_packet(mp, B_TRUE, NULL, NULL, &ipdrops_esp_no_sa, 37870Sstevel@tonic-gate &esp_dropper); 37880Sstevel@tonic-gate } 37890Sstevel@tonic-gate 37900Sstevel@tonic-gate /* 37910Sstevel@tonic-gate * Initialize the ESP input and output processing functions. 37920Sstevel@tonic-gate */ 37930Sstevel@tonic-gate void 37940Sstevel@tonic-gate ipsecesp_init_funcs(ipsa_t *sa) 37950Sstevel@tonic-gate { 37960Sstevel@tonic-gate if (sa->ipsa_output_func == NULL) 37970Sstevel@tonic-gate sa->ipsa_output_func = esp_outbound; 37980Sstevel@tonic-gate if (sa->ipsa_input_func == NULL) 37990Sstevel@tonic-gate sa->ipsa_input_func = esp_inbound; 38000Sstevel@tonic-gate } 3801