10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52751Sdanmcd * Common Development and Distribution License (the "License").
62751Sdanmcd * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
2211446Sdanmcd@sun.com * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/stream.h>
280Sstevel@tonic-gate #include <sys/stropts.h>
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <sys/strlog.h>
310Sstevel@tonic-gate #include <sys/tihdr.h>
320Sstevel@tonic-gate #include <sys/socket.h>
330Sstevel@tonic-gate #include <sys/ddi.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
363448Sdh155122 #include <sys/zone.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/cmn_err.h>
390Sstevel@tonic-gate #include <sys/vtrace.h>
400Sstevel@tonic-gate #include <sys/debug.h>
410Sstevel@tonic-gate #include <sys/atomic.h>
420Sstevel@tonic-gate #include <sys/strsun.h>
430Sstevel@tonic-gate #include <sys/random.h>
440Sstevel@tonic-gate #include <netinet/in.h>
450Sstevel@tonic-gate #include <net/if.h>
460Sstevel@tonic-gate #include <netinet/ip6.h>
470Sstevel@tonic-gate #include <net/pfkeyv2.h>
4810824SMark.Fenwick@Sun.COM #include <net/pfpolicy.h>
490Sstevel@tonic-gate
500Sstevel@tonic-gate #include <inet/common.h>
510Sstevel@tonic-gate #include <inet/mi.h>
520Sstevel@tonic-gate #include <inet/nd.h>
530Sstevel@tonic-gate #include <inet/ip.h>
544987Sdanmcd #include <inet/ip_impl.h>
550Sstevel@tonic-gate #include <inet/ip6.h>
5611042SErik.Nordmark@Sun.COM #include <inet/ip_if.h>
5711042SErik.Nordmark@Sun.COM #include <inet/ip_ndp.h>
580Sstevel@tonic-gate #include <inet/sadb.h>
590Sstevel@tonic-gate #include <inet/ipsec_info.h>
600Sstevel@tonic-gate #include <inet/ipsec_impl.h>
610Sstevel@tonic-gate #include <inet/ipsecesp.h>
620Sstevel@tonic-gate #include <inet/ipdrop.h>
630Sstevel@tonic-gate #include <inet/tcp.h>
640Sstevel@tonic-gate #include <sys/kstat.h>
650Sstevel@tonic-gate #include <sys/policy.h>
660Sstevel@tonic-gate #include <sys/strsun.h>
6710934Ssommerfeld@sun.com #include <sys/strsubr.h>
680Sstevel@tonic-gate #include <inet/udp_impl.h>
690Sstevel@tonic-gate #include <sys/taskq.h>
703448Sdh155122 #include <sys/note.h>
710Sstevel@tonic-gate
7210934Ssommerfeld@sun.com #include <sys/tsol/tnet.h>
7310934Ssommerfeld@sun.com
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 */
793448Sdh155122 static ipsecespparam_t lcl_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"},
964987Sdanmcd { 0, 600, 20, "ipsecesp_nat_keepalive_interval"},
970Sstevel@tonic-gate };
983448Sdh155122 #define ipsecesp_debug ipsecesp_params[0].ipsecesp_param_value
993448Sdh155122 #define ipsecesp_age_interval ipsecesp_params[1].ipsecesp_param_value
1003448Sdh155122 #define ipsecesp_age_int_max ipsecesp_params[1].ipsecesp_param_max
1013448Sdh155122 #define ipsecesp_reap_delay ipsecesp_params[2].ipsecesp_param_value
1023448Sdh155122 #define ipsecesp_replay_size ipsecesp_params[3].ipsecesp_param_value
1033448Sdh155122 #define ipsecesp_acquire_timeout \
1043448Sdh155122 ipsecesp_params[4].ipsecesp_param_value
1053448Sdh155122 #define ipsecesp_larval_timeout \
1063448Sdh155122 ipsecesp_params[5].ipsecesp_param_value
1073448Sdh155122 #define ipsecesp_default_soft_bytes \
1083448Sdh155122 ipsecesp_params[6].ipsecesp_param_value
1093448Sdh155122 #define ipsecesp_default_hard_bytes \
1103448Sdh155122 ipsecesp_params[7].ipsecesp_param_value
1113448Sdh155122 #define ipsecesp_default_soft_addtime \
1123448Sdh155122 ipsecesp_params[8].ipsecesp_param_value
1133448Sdh155122 #define ipsecesp_default_hard_addtime \
1143448Sdh155122 ipsecesp_params[9].ipsecesp_param_value
1153448Sdh155122 #define ipsecesp_default_soft_usetime \
1163448Sdh155122 ipsecesp_params[10].ipsecesp_param_value
1173448Sdh155122 #define ipsecesp_default_hard_usetime \
1183448Sdh155122 ipsecesp_params[11].ipsecesp_param_value
1193448Sdh155122 #define ipsecesp_log_unknown_spi \
1203448Sdh155122 ipsecesp_params[12].ipsecesp_param_value
1213448Sdh155122 #define ipsecesp_padding_check \
1223448Sdh155122 ipsecesp_params[13].ipsecesp_param_value
1234987Sdanmcd /* For ipsecesp_nat_keepalive_interval, see ipsecesp.h. */
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate #define esp0dbg(a) printf a
1260Sstevel@tonic-gate /* NOTE: != 0 instead of > 0 so lint doesn't complain. */
1273448Sdh155122 #define esp1dbg(espstack, a) if (espstack->ipsecesp_debug != 0) printf a
1283448Sdh155122 #define esp2dbg(espstack, a) if (espstack->ipsecesp_debug > 1) printf a
1293448Sdh155122 #define esp3dbg(espstack, a) if (espstack->ipsecesp_debug > 2) printf a
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate static int ipsecesp_open(queue_t *, dev_t *, int, int, cred_t *);
1320Sstevel@tonic-gate static int ipsecesp_close(queue_t *);
1330Sstevel@tonic-gate static void ipsecesp_wput(queue_t *, mblk_t *);
1343448Sdh155122 static void *ipsecesp_stack_init(netstackid_t stackid, netstack_t *ns);
1353448Sdh155122 static void ipsecesp_stack_fini(netstackid_t stackid, void *arg);
1363448Sdh155122 static void esp_send_acquire(ipsacq_t *, mblk_t *, netstack_t *);
1370Sstevel@tonic-gate
1384987Sdanmcd static void esp_prepare_udp(netstack_t *, mblk_t *, ipha_t *);
13911042SErik.Nordmark@Sun.COM static void esp_outbound_finish(mblk_t *, ip_xmit_attr_t *);
14011042SErik.Nordmark@Sun.COM static void esp_inbound_restart(mblk_t *, ip_recv_attr_t *);
1410Sstevel@tonic-gate
1423448Sdh155122 static boolean_t esp_register_out(uint32_t, uint32_t, uint_t,
14311042SErik.Nordmark@Sun.COM ipsecesp_stack_t *, cred_t *);
1440Sstevel@tonic-gate static boolean_t esp_strip_header(mblk_t *, boolean_t, uint32_t,
1453448Sdh155122 kstat_named_t **, ipsecesp_stack_t *);
14611042SErik.Nordmark@Sun.COM static mblk_t *esp_submit_req_inbound(mblk_t *, ip_recv_attr_t *,
14711042SErik.Nordmark@Sun.COM ipsa_t *, uint_t);
14811042SErik.Nordmark@Sun.COM static mblk_t *esp_submit_req_outbound(mblk_t *, ip_xmit_attr_t *,
14911042SErik.Nordmark@Sun.COM ipsa_t *, uchar_t *, uint_t);
1507749SThejaswini.Singarajipura@Sun.COM
1513448Sdh155122 /* Setable in /etc/system */
1523448Sdh155122 uint32_t esp_hash_size = IPSEC_DEFAULT_HASH_SIZE;
1533448Sdh155122
1540Sstevel@tonic-gate static struct module_info info = {
1550Sstevel@tonic-gate 5137, "ipsecesp", 0, INFPSZ, 65536, 1024
1560Sstevel@tonic-gate };
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate static struct qinit rinit = {
15911042SErik.Nordmark@Sun.COM (pfi_t)putnext, NULL, ipsecesp_open, ipsecesp_close, NULL, &info,
1600Sstevel@tonic-gate NULL
1610Sstevel@tonic-gate };
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate static struct qinit winit = {
1640Sstevel@tonic-gate (pfi_t)ipsecesp_wput, NULL, ipsecesp_open, ipsecesp_close, NULL, &info,
1650Sstevel@tonic-gate NULL
1660Sstevel@tonic-gate };
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate struct streamtab ipsecespinfo = {
1690Sstevel@tonic-gate &rinit, &winit, NULL, NULL
1700Sstevel@tonic-gate };
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate static taskq_t *esp_taskq;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate * OTOH, this one is set at open/close, and I'm D_MTQPAIR for now.
1760Sstevel@tonic-gate *
1770Sstevel@tonic-gate * Question: Do I need this, given that all instance's esps->esps_wq point
1780Sstevel@tonic-gate * to IP?
1790Sstevel@tonic-gate *
1800Sstevel@tonic-gate * Answer: Yes, because I need to know which queue is BOUND to
1810Sstevel@tonic-gate * IPPROTO_ESP
1820Sstevel@tonic-gate */
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
1893448Sdh155122 typedef struct esp_kstats_s {
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_crypto_sync;
2020Sstevel@tonic-gate kstat_named_t esp_stat_crypto_async;
2030Sstevel@tonic-gate kstat_named_t esp_stat_crypto_failures;
2040Sstevel@tonic-gate kstat_named_t esp_stat_num_ealgs;
2050Sstevel@tonic-gate kstat_named_t esp_stat_bad_decrypt;
2067066Sdanmcd kstat_named_t esp_stat_sa_port_renumbers;
2070Sstevel@tonic-gate } esp_kstats_t;
2080Sstevel@tonic-gate
2093448Sdh155122 /*
2103448Sdh155122 * espstack->esp_kstats is equal to espstack->esp_ksp->ks_data if
2113448Sdh155122 * kstat_create_netstack for espstack->esp_ksp succeeds, but when it
2123448Sdh155122 * fails, it will be NULL. Note this is done for all stack instances,
2133448Sdh155122 * so it *could* fail. hence a non-NULL checking is done for
2143448Sdh155122 * ESP_BUMP_STAT and ESP_DEBUMP_STAT
2153448Sdh155122 */
2163448Sdh155122 #define ESP_BUMP_STAT(espstack, x) \
2173448Sdh155122 do { \
2183448Sdh155122 if (espstack->esp_kstats != NULL) \
2193448Sdh155122 (espstack->esp_kstats->esp_stat_ ## x).value.ui64++; \
2203448Sdh155122 _NOTE(CONSTCOND) \
2213448Sdh155122 } while (0)
2223448Sdh155122
2233448Sdh155122 #define ESP_DEBUMP_STAT(espstack, x) \
2243448Sdh155122 do { \
2253448Sdh155122 if (espstack->esp_kstats != NULL) \
2263448Sdh155122 (espstack->esp_kstats->esp_stat_ ## x).value.ui64--; \
2273448Sdh155122 _NOTE(CONSTCOND) \
2283448Sdh155122 } while (0)
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate static int esp_kstat_update(kstat_t *, int);
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate static boolean_t
esp_kstat_init(ipsecesp_stack_t * espstack,netstackid_t stackid)2333448Sdh155122 esp_kstat_init(ipsecesp_stack_t *espstack, netstackid_t stackid)
2340Sstevel@tonic-gate {
2353448Sdh155122 espstack->esp_ksp = kstat_create_netstack("ipsecesp", 0, "esp_stat",
2363448Sdh155122 "net", KSTAT_TYPE_NAMED,
2373448Sdh155122 sizeof (esp_kstats_t) / sizeof (kstat_named_t),
2383448Sdh155122 KSTAT_FLAG_PERSISTENT, stackid);
2393448Sdh155122
2403448Sdh155122 if (espstack->esp_ksp == NULL || espstack->esp_ksp->ks_data == NULL)
2410Sstevel@tonic-gate return (B_FALSE);
2420Sstevel@tonic-gate
2433448Sdh155122 espstack->esp_kstats = espstack->esp_ksp->ks_data;
2443448Sdh155122
2453448Sdh155122 espstack->esp_ksp->ks_update = esp_kstat_update;
2463448Sdh155122 espstack->esp_ksp->ks_private = (void *)(uintptr_t)stackid;
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate #define K64 KSTAT_DATA_UINT64
2493448Sdh155122 #define KI(x) kstat_named_init(&(espstack->esp_kstats->esp_stat_##x), #x, K64)
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate KI(num_aalgs);
2520Sstevel@tonic-gate KI(num_ealgs);
2530Sstevel@tonic-gate KI(good_auth);
2540Sstevel@tonic-gate KI(bad_auth);
2550Sstevel@tonic-gate KI(bad_padding);
2560Sstevel@tonic-gate KI(replay_failures);
2570Sstevel@tonic-gate KI(replay_early_failures);
2580Sstevel@tonic-gate KI(keysock_in);
2590Sstevel@tonic-gate KI(out_requests);
2600Sstevel@tonic-gate KI(acquire_requests);
2610Sstevel@tonic-gate KI(bytes_expired);
2620Sstevel@tonic-gate KI(out_discards);
2630Sstevel@tonic-gate KI(crypto_sync);
2640Sstevel@tonic-gate KI(crypto_async);
2650Sstevel@tonic-gate KI(crypto_failures);
2660Sstevel@tonic-gate KI(bad_decrypt);
2677066Sdanmcd KI(sa_port_renumbers);
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate #undef KI
2700Sstevel@tonic-gate #undef K64
2710Sstevel@tonic-gate
2723448Sdh155122 kstat_install(espstack->esp_ksp);
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate return (B_TRUE);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate static int
esp_kstat_update(kstat_t * kp,int rw)2780Sstevel@tonic-gate esp_kstat_update(kstat_t *kp, int rw)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate esp_kstats_t *ekp;
2813448Sdh155122 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
2823448Sdh155122 netstack_t *ns;
2833448Sdh155122 ipsec_stack_t *ipss;
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate if ((kp == NULL) || (kp->ks_data == NULL))
2860Sstevel@tonic-gate return (EIO);
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate if (rw == KSTAT_WRITE)
2890Sstevel@tonic-gate return (EACCES);
2900Sstevel@tonic-gate
2913448Sdh155122 ns = netstack_find_by_stackid(stackid);
2923448Sdh155122 if (ns == NULL)
2933448Sdh155122 return (-1);
2943448Sdh155122 ipss = ns->netstack_ipsec;
2953448Sdh155122 if (ipss == NULL) {
2963448Sdh155122 netstack_rele(ns);
2973448Sdh155122 return (-1);
2983448Sdh155122 }
2990Sstevel@tonic-gate ekp = (esp_kstats_t *)kp->ks_data;
3003448Sdh155122
3013448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock);
3023448Sdh155122 ekp->esp_stat_num_aalgs.value.ui64 =
3033448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
3043448Sdh155122 ekp->esp_stat_num_ealgs.value.ui64 =
3053448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
3063448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
3073448Sdh155122
3083448Sdh155122 netstack_rele(ns);
3090Sstevel@tonic-gate return (0);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate #ifdef DEBUG
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate * Debug routine, useful to see pre-encryption data.
3150Sstevel@tonic-gate */
3160Sstevel@tonic-gate static char *
dump_msg(mblk_t * mp)3170Sstevel@tonic-gate dump_msg(mblk_t *mp)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate char tmp_str[3], tmp_line[256];
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate while (mp != NULL) {
3220Sstevel@tonic-gate unsigned char *ptr;
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate printf("mblk address 0x%p, length %ld, db_ref %d "
3250Sstevel@tonic-gate "type %d, base 0x%p, lim 0x%p\n",
3260Sstevel@tonic-gate (void *) mp, (long)(mp->b_wptr - mp->b_rptr),
3270Sstevel@tonic-gate mp->b_datap->db_ref, mp->b_datap->db_type,
3280Sstevel@tonic-gate (void *)mp->b_datap->db_base, (void *)mp->b_datap->db_lim);
3290Sstevel@tonic-gate ptr = mp->b_rptr;
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate tmp_line[0] = '\0';
3320Sstevel@tonic-gate while (ptr < mp->b_wptr) {
3330Sstevel@tonic-gate uint_t diff;
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate diff = (ptr - mp->b_rptr);
3360Sstevel@tonic-gate if (!(diff & 0x1f)) {
3370Sstevel@tonic-gate if (strlen(tmp_line) > 0) {
3380Sstevel@tonic-gate printf("bytes: %s\n", tmp_line);
3390Sstevel@tonic-gate tmp_line[0] = '\0';
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate if (!(diff & 0x3))
3430Sstevel@tonic-gate (void) strcat(tmp_line, " ");
3440Sstevel@tonic-gate (void) sprintf(tmp_str, "%02x", *ptr);
3450Sstevel@tonic-gate (void) strcat(tmp_line, tmp_str);
3460Sstevel@tonic-gate ptr++;
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate if (strlen(tmp_line) > 0)
3490Sstevel@tonic-gate printf("bytes: %s\n", tmp_line);
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate mp = mp->b_cont;
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate return ("\n");
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate #else /* DEBUG */
3580Sstevel@tonic-gate static char *
dump_msg(mblk_t * mp)3590Sstevel@tonic-gate dump_msg(mblk_t *mp)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate printf("Find value of mp %p.\n", mp);
3620Sstevel@tonic-gate return ("\n");
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate #endif /* DEBUG */
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate * Don't have to lock age_interval, as only one thread will access it at
3680Sstevel@tonic-gate * a time, because I control the one function that does with timeout().
3690Sstevel@tonic-gate */
3700Sstevel@tonic-gate static void
esp_ager(void * arg)3713448Sdh155122 esp_ager(void *arg)
3720Sstevel@tonic-gate {
3733448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)arg;
3743448Sdh155122 netstack_t *ns = espstack->ipsecesp_netstack;
3750Sstevel@tonic-gate hrtime_t begin = gethrtime();
3760Sstevel@tonic-gate
3773448Sdh155122 sadb_ager(&espstack->esp_sadb.s_v4, espstack->esp_pfkey_q,
37811042SErik.Nordmark@Sun.COM espstack->ipsecesp_reap_delay, ns);
3793448Sdh155122 sadb_ager(&espstack->esp_sadb.s_v6, espstack->esp_pfkey_q,
38011042SErik.Nordmark@Sun.COM espstack->ipsecesp_reap_delay, ns);
3813448Sdh155122
3823448Sdh155122 espstack->esp_event = sadb_retimeout(begin, espstack->esp_pfkey_q,
3833448Sdh155122 esp_ager, espstack,
3843448Sdh155122 &espstack->ipsecesp_age_interval, espstack->ipsecesp_age_int_max,
3853448Sdh155122 info.mi_idnum);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate * Get an ESP NDD parameter.
3900Sstevel@tonic-gate */
3910Sstevel@tonic-gate /* ARGSUSED */
3920Sstevel@tonic-gate static int
ipsecesp_param_get(q,mp,cp,cr)3930Sstevel@tonic-gate ipsecesp_param_get(q, mp, cp, cr)
3940Sstevel@tonic-gate queue_t *q;
3950Sstevel@tonic-gate mblk_t *mp;
3960Sstevel@tonic-gate caddr_t cp;
3970Sstevel@tonic-gate cred_t *cr;
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate ipsecespparam_t *ipsecesppa = (ipsecespparam_t *)cp;
4000Sstevel@tonic-gate uint_t value;
4013448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)q->q_ptr;
4023448Sdh155122
4033448Sdh155122 mutex_enter(&espstack->ipsecesp_param_lock);
4040Sstevel@tonic-gate value = ipsecesppa->ipsecesp_param_value;
4053448Sdh155122 mutex_exit(&espstack->ipsecesp_param_lock);
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate (void) mi_mpprintf(mp, "%u", value);
4080Sstevel@tonic-gate return (0);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate /*
4120Sstevel@tonic-gate * This routine sets an NDD variable in a ipsecespparam_t structure.
4130Sstevel@tonic-gate */
4140Sstevel@tonic-gate /* ARGSUSED */
4150Sstevel@tonic-gate static int
ipsecesp_param_set(q,mp,value,cp,cr)4160Sstevel@tonic-gate ipsecesp_param_set(q, mp, value, cp, cr)
4170Sstevel@tonic-gate queue_t *q;
4180Sstevel@tonic-gate mblk_t *mp;
4190Sstevel@tonic-gate char *value;
4200Sstevel@tonic-gate caddr_t cp;
4210Sstevel@tonic-gate cred_t *cr;
4220Sstevel@tonic-gate {
4230Sstevel@tonic-gate ulong_t new_value;
4240Sstevel@tonic-gate ipsecespparam_t *ipsecesppa = (ipsecespparam_t *)cp;
4253448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)q->q_ptr;
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate /*
4280Sstevel@tonic-gate * Fail the request if the new value does not lie within the
4290Sstevel@tonic-gate * required bounds.
4300Sstevel@tonic-gate */
4310Sstevel@tonic-gate if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
4320Sstevel@tonic-gate new_value < ipsecesppa->ipsecesp_param_min ||
4330Sstevel@tonic-gate new_value > ipsecesppa->ipsecesp_param_max) {
4340Sstevel@tonic-gate return (EINVAL);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate /* Set the new value */
4383448Sdh155122 mutex_enter(&espstack->ipsecesp_param_lock);
4390Sstevel@tonic-gate ipsecesppa->ipsecesp_param_value = new_value;
4403448Sdh155122 mutex_exit(&espstack->ipsecesp_param_lock);
4410Sstevel@tonic-gate return (0);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate /*
4450Sstevel@tonic-gate * Using lifetime NDD variables, fill in an extended combination's
4460Sstevel@tonic-gate * lifetime information.
4470Sstevel@tonic-gate */
4480Sstevel@tonic-gate void
ipsecesp_fill_defs(sadb_x_ecomb_t * ecomb,netstack_t * ns)4493448Sdh155122 ipsecesp_fill_defs(sadb_x_ecomb_t *ecomb, netstack_t *ns)
4500Sstevel@tonic-gate {
4513448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
4523448Sdh155122
4533448Sdh155122 ecomb->sadb_x_ecomb_soft_bytes = espstack->ipsecesp_default_soft_bytes;
4543448Sdh155122 ecomb->sadb_x_ecomb_hard_bytes = espstack->ipsecesp_default_hard_bytes;
4553448Sdh155122 ecomb->sadb_x_ecomb_soft_addtime =
4563448Sdh155122 espstack->ipsecesp_default_soft_addtime;
4573448Sdh155122 ecomb->sadb_x_ecomb_hard_addtime =
4583448Sdh155122 espstack->ipsecesp_default_hard_addtime;
4593448Sdh155122 ecomb->sadb_x_ecomb_soft_usetime =
4603448Sdh155122 espstack->ipsecesp_default_soft_usetime;
4613448Sdh155122 ecomb->sadb_x_ecomb_hard_usetime =
4623448Sdh155122 espstack->ipsecesp_default_hard_usetime;
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate * Initialize things for ESP at module load time.
4670Sstevel@tonic-gate */
4680Sstevel@tonic-gate boolean_t
ipsecesp_ddi_init(void)4690Sstevel@tonic-gate ipsecesp_ddi_init(void)
4700Sstevel@tonic-gate {
4713448Sdh155122 esp_taskq = taskq_create("esp_taskq", 1, minclsyspri,
4723448Sdh155122 IPSEC_TASKQ_MIN, IPSEC_TASKQ_MAX, 0);
4733448Sdh155122
4743448Sdh155122 /*
4753448Sdh155122 * We want to be informed each time a stack is created or
4763448Sdh155122 * destroyed in the kernel, so we can maintain the
4773448Sdh155122 * set of ipsecesp_stack_t's.
4783448Sdh155122 */
4793448Sdh155122 netstack_register(NS_IPSECESP, ipsecesp_stack_init, NULL,
4803448Sdh155122 ipsecesp_stack_fini);
4813448Sdh155122
4823448Sdh155122 return (B_TRUE);
4833448Sdh155122 }
4843448Sdh155122
4853448Sdh155122 /*
4863448Sdh155122 * Walk through the param array specified registering each element with the
4873448Sdh155122 * named dispatch handler.
4883448Sdh155122 */
4893448Sdh155122 static boolean_t
ipsecesp_param_register(IDP * ndp,ipsecespparam_t * espp,int cnt)4903448Sdh155122 ipsecesp_param_register(IDP *ndp, ipsecespparam_t *espp, int cnt)
4913448Sdh155122 {
4923448Sdh155122 for (; cnt-- > 0; espp++) {
4930Sstevel@tonic-gate if (espp->ipsecesp_param_name != NULL &&
4940Sstevel@tonic-gate espp->ipsecesp_param_name[0]) {
4953448Sdh155122 if (!nd_load(ndp,
4963448Sdh155122 espp->ipsecesp_param_name,
4970Sstevel@tonic-gate ipsecesp_param_get, ipsecesp_param_set,
4980Sstevel@tonic-gate (caddr_t)espp)) {
4993448Sdh155122 nd_free(ndp);
5000Sstevel@tonic-gate return (B_FALSE);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate return (B_TRUE);
5050Sstevel@tonic-gate }
5063448Sdh155122 /*
5073448Sdh155122 * Initialize things for ESP for each stack instance
5083448Sdh155122 */
5093448Sdh155122 static void *
ipsecesp_stack_init(netstackid_t stackid,netstack_t * ns)5103448Sdh155122 ipsecesp_stack_init(netstackid_t stackid, netstack_t *ns)
5113448Sdh155122 {
5123448Sdh155122 ipsecesp_stack_t *espstack;
5133448Sdh155122 ipsecespparam_t *espp;
5143448Sdh155122
5153448Sdh155122 espstack = (ipsecesp_stack_t *)kmem_zalloc(sizeof (*espstack),
5163448Sdh155122 KM_SLEEP);
5173448Sdh155122 espstack->ipsecesp_netstack = ns;
5183448Sdh155122
5193448Sdh155122 espp = (ipsecespparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
5203448Sdh155122 espstack->ipsecesp_params = espp;
5213448Sdh155122 bcopy(lcl_param_arr, espp, sizeof (lcl_param_arr));
5223448Sdh155122
5233448Sdh155122 (void) ipsecesp_param_register(&espstack->ipsecesp_g_nd, espp,
5243448Sdh155122 A_CNT(lcl_param_arr));
5253448Sdh155122
5263448Sdh155122 (void) esp_kstat_init(espstack, stackid);
5273448Sdh155122
5283448Sdh155122 espstack->esp_sadb.s_acquire_timeout =
5293448Sdh155122 &espstack->ipsecesp_acquire_timeout;
5303448Sdh155122 espstack->esp_sadb.s_acqfn = esp_send_acquire;
5313448Sdh155122 sadbp_init("ESP", &espstack->esp_sadb, SADB_SATYPE_ESP, esp_hash_size,
5323448Sdh155122 espstack->ipsecesp_netstack);
5333448Sdh155122
5343448Sdh155122 mutex_init(&espstack->ipsecesp_param_lock, NULL, MUTEX_DEFAULT, 0);
5353448Sdh155122
5363448Sdh155122 ip_drop_register(&espstack->esp_dropper, "IPsec ESP");
5373448Sdh155122 return (espstack);
5383448Sdh155122 }
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate * Destroy things for ESP at module unload time.
5420Sstevel@tonic-gate */
5430Sstevel@tonic-gate void
ipsecesp_ddi_destroy(void)5440Sstevel@tonic-gate ipsecesp_ddi_destroy(void)
5450Sstevel@tonic-gate {
5463448Sdh155122 netstack_unregister(NS_IPSECESP);
5470Sstevel@tonic-gate taskq_destroy(esp_taskq);
5483448Sdh155122 }
5493448Sdh155122
5503448Sdh155122 /*
5513448Sdh155122 * Destroy things for ESP for one stack instance
5523448Sdh155122 */
5533448Sdh155122 static void
ipsecesp_stack_fini(netstackid_t stackid,void * arg)5543448Sdh155122 ipsecesp_stack_fini(netstackid_t stackid, void *arg)
5553448Sdh155122 {
5563448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)arg;
5573448Sdh155122
5583448Sdh155122 if (espstack->esp_pfkey_q != NULL) {
5593448Sdh155122 (void) quntimeout(espstack->esp_pfkey_q, espstack->esp_event);
5603448Sdh155122 }
5613448Sdh155122 espstack->esp_sadb.s_acqfn = NULL;
5623448Sdh155122 espstack->esp_sadb.s_acquire_timeout = NULL;
5633448Sdh155122 sadbp_destroy(&espstack->esp_sadb, espstack->ipsecesp_netstack);
5643448Sdh155122 ip_drop_unregister(&espstack->esp_dropper);
5653448Sdh155122 mutex_destroy(&espstack->ipsecesp_param_lock);
5663448Sdh155122 nd_free(&espstack->ipsecesp_g_nd);
5673448Sdh155122
5683448Sdh155122 kmem_free(espstack->ipsecesp_params, sizeof (lcl_param_arr));
5693448Sdh155122 espstack->ipsecesp_params = NULL;
5703448Sdh155122 kstat_delete_netstack(espstack->esp_ksp, stackid);
5713448Sdh155122 espstack->esp_ksp = NULL;
5723448Sdh155122 espstack->esp_kstats = NULL;
5733448Sdh155122 kmem_free(espstack, sizeof (*espstack));
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate /*
57711042SErik.Nordmark@Sun.COM * ESP module open routine, which is here for keysock plumbing.
57811042SErik.Nordmark@Sun.COM * Keysock is pushed over {AH,ESP} which is an artifact from the Bad Old
57911042SErik.Nordmark@Sun.COM * Days of export control, and fears that ESP would not be allowed
58011042SErik.Nordmark@Sun.COM * to be shipped at all by default. Eventually, keysock should
58111042SErik.Nordmark@Sun.COM * either access AH and ESP via modstubs or krtld dependencies, or
58211042SErik.Nordmark@Sun.COM * perhaps be folded in with AH and ESP into a single IPsec/netsec
58311042SErik.Nordmark@Sun.COM * module ("netsec" if PF_KEY provides more than AH/ESP keying tables).
5840Sstevel@tonic-gate */
5850Sstevel@tonic-gate /* ARGSUSED */
5860Sstevel@tonic-gate static int
ipsecesp_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)5870Sstevel@tonic-gate ipsecesp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5880Sstevel@tonic-gate {
5893448Sdh155122 netstack_t *ns;
5903448Sdh155122 ipsecesp_stack_t *espstack;
5913448Sdh155122
5927118Ssommerfe if (secpolicy_ip_config(credp, B_FALSE) != 0)
5930Sstevel@tonic-gate return (EPERM);
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate if (q->q_ptr != NULL)
5960Sstevel@tonic-gate return (0); /* Re-open of an already open instance. */
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate if (sflag != MODOPEN)
5990Sstevel@tonic-gate return (EINVAL);
6000Sstevel@tonic-gate
6013448Sdh155122 ns = netstack_find_by_cred(credp);
6023448Sdh155122 ASSERT(ns != NULL);
6033448Sdh155122 espstack = ns->netstack_ipsecesp;
6043448Sdh155122 ASSERT(espstack != NULL);
6053448Sdh155122
6063448Sdh155122 q->q_ptr = espstack;
6073448Sdh155122 WR(q)->q_ptr = q->q_ptr;
6083448Sdh155122
60911042SErik.Nordmark@Sun.COM qprocson(q);
6100Sstevel@tonic-gate return (0);
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate
6130Sstevel@tonic-gate /*
6140Sstevel@tonic-gate * ESP module close routine.
6150Sstevel@tonic-gate */
6160Sstevel@tonic-gate static int
ipsecesp_close(queue_t * q)6170Sstevel@tonic-gate ipsecesp_close(queue_t *q)
6180Sstevel@tonic-gate {
6193448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)q->q_ptr;
6203448Sdh155122
6210Sstevel@tonic-gate /*
6220Sstevel@tonic-gate * Clean up q_ptr, if needed.
6230Sstevel@tonic-gate */
6240Sstevel@tonic-gate qprocsoff(q);
6250Sstevel@tonic-gate
6260Sstevel@tonic-gate /* Keysock queue check is safe, because of OCEXCL perimeter. */
6270Sstevel@tonic-gate
6283448Sdh155122 if (q == espstack->esp_pfkey_q) {
6293448Sdh155122 esp1dbg(espstack,
6303448Sdh155122 ("ipsecesp_close: Ummm... keysock is closing ESP.\n"));
6313448Sdh155122 espstack->esp_pfkey_q = NULL;
6320Sstevel@tonic-gate /* Detach qtimeouts. */
6333448Sdh155122 (void) quntimeout(q, espstack->esp_event);
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate
6363448Sdh155122 netstack_rele(espstack->ipsecesp_netstack);
6370Sstevel@tonic-gate return (0);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate /*
6410Sstevel@tonic-gate * Add a number of bytes to what the SA has protected so far. Return
6420Sstevel@tonic-gate * B_TRUE if the SA can still protect that many bytes.
6430Sstevel@tonic-gate *
6440Sstevel@tonic-gate * Caller must REFRELE the passed-in assoc. This function must REFRELE
6450Sstevel@tonic-gate * any obtained peer SA.
6460Sstevel@tonic-gate */
6470Sstevel@tonic-gate static boolean_t
esp_age_bytes(ipsa_t * assoc,uint64_t bytes,boolean_t inbound)6480Sstevel@tonic-gate esp_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate ipsa_t *inassoc, *outassoc;
6510Sstevel@tonic-gate isaf_t *bucket;
6520Sstevel@tonic-gate boolean_t inrc, outrc, isv6;
6530Sstevel@tonic-gate sadb_t *sp;
6540Sstevel@tonic-gate int outhash;
6553448Sdh155122 netstack_t *ns = assoc->ipsa_netstack;
6563448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate /* No peer? No problem! */
6590Sstevel@tonic-gate if (!assoc->ipsa_haspeer) {
6603448Sdh155122 return (sadb_age_bytes(espstack->esp_pfkey_q, assoc, bytes,
6610Sstevel@tonic-gate B_TRUE));
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate /*
6650Sstevel@tonic-gate * Otherwise, we want to grab both the original assoc and its peer.
6660Sstevel@tonic-gate * There might be a race for this, but if it's a real race, two
6670Sstevel@tonic-gate * expire messages may occur. We limit this by only sending the
6680Sstevel@tonic-gate * expire message on one of the peers, we'll pick the inbound
6690Sstevel@tonic-gate * arbitrarily.
6700Sstevel@tonic-gate *
6710Sstevel@tonic-gate * If we need tight synchronization on the peer SA, then we need to
6720Sstevel@tonic-gate * reconsider.
6730Sstevel@tonic-gate */
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate /* Use address length to select IPv6/IPv4 */
6760Sstevel@tonic-gate isv6 = (assoc->ipsa_addrfam == AF_INET6);
6773448Sdh155122 sp = isv6 ? &espstack->esp_sadb.s_v6 : &espstack->esp_sadb.s_v4;
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate if (inbound) {
6800Sstevel@tonic-gate inassoc = assoc;
6810Sstevel@tonic-gate if (isv6) {
682564Ssommerfe outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *)
6830Sstevel@tonic-gate &inassoc->ipsa_dstaddr));
6840Sstevel@tonic-gate } else {
685564Ssommerfe outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *)
6864987Sdanmcd &inassoc->ipsa_dstaddr));
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate bucket = &sp->sdb_of[outhash];
6890Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock);
6900Sstevel@tonic-gate outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
6910Sstevel@tonic-gate inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
6920Sstevel@tonic-gate inassoc->ipsa_addrfam);
6930Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock);
6940Sstevel@tonic-gate if (outassoc == NULL) {
6950Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */
6960Sstevel@tonic-gate esp0dbg(("esp_age_bytes: "
6970Sstevel@tonic-gate "can't find peer for inbound.\n"));
6983448Sdh155122 return (sadb_age_bytes(espstack->esp_pfkey_q, inassoc,
6990Sstevel@tonic-gate bytes, B_TRUE));
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate } else {
7020Sstevel@tonic-gate outassoc = assoc;
703564Ssommerfe bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
7040Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock);
7050Sstevel@tonic-gate inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
7060Sstevel@tonic-gate outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
7070Sstevel@tonic-gate outassoc->ipsa_addrfam);
7080Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock);
7090Sstevel@tonic-gate if (inassoc == NULL) {
7100Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */
7110Sstevel@tonic-gate esp0dbg(("esp_age_bytes: "
7120Sstevel@tonic-gate "can't find peer for outbound.\n"));
7133448Sdh155122 return (sadb_age_bytes(espstack->esp_pfkey_q, outassoc,
7140Sstevel@tonic-gate bytes, B_TRUE));
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate
7183448Sdh155122 inrc = sadb_age_bytes(espstack->esp_pfkey_q, inassoc, bytes, B_TRUE);
7193448Sdh155122 outrc = sadb_age_bytes(espstack->esp_pfkey_q, outassoc, bytes, B_FALSE);
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate /*
7220Sstevel@tonic-gate * REFRELE any peer SA.
7230Sstevel@tonic-gate *
7240Sstevel@tonic-gate * Because of the multi-line macro nature of IPSA_REFRELE, keep
7250Sstevel@tonic-gate * them in { }.
7260Sstevel@tonic-gate */
7270Sstevel@tonic-gate if (inbound) {
7280Sstevel@tonic-gate IPSA_REFRELE(outassoc);
7290Sstevel@tonic-gate } else {
7300Sstevel@tonic-gate IPSA_REFRELE(inassoc);
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate
7330Sstevel@tonic-gate return (inrc && outrc);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate /*
7370Sstevel@tonic-gate * Do incoming NAT-T manipulations for packet.
73811042SErik.Nordmark@Sun.COM * Returns NULL if the mblk chain is consumed.
7390Sstevel@tonic-gate */
74011042SErik.Nordmark@Sun.COM static mblk_t *
esp_fix_natt_checksums(mblk_t * data_mp,ipsa_t * assoc)7410Sstevel@tonic-gate esp_fix_natt_checksums(mblk_t *data_mp, ipsa_t *assoc)
7420Sstevel@tonic-gate {
7430Sstevel@tonic-gate ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
74411042SErik.Nordmark@Sun.COM tcpha_t *tcpha;
7450Sstevel@tonic-gate udpha_t *udpha;
7460Sstevel@tonic-gate /* Initialize to our inbound cksum adjustment... */
7470Sstevel@tonic-gate uint32_t sum = assoc->ipsa_inbound_cksum;
7480Sstevel@tonic-gate
7490Sstevel@tonic-gate switch (ipha->ipha_protocol) {
7500Sstevel@tonic-gate case IPPROTO_TCP:
75111042SErik.Nordmark@Sun.COM tcpha = (tcpha_t *)(data_mp->b_rptr +
7520Sstevel@tonic-gate IPH_HDR_LENGTH(ipha));
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate #define DOWN_SUM(x) (x) = ((x) & 0xFFFF) + ((x) >> 16)
75511042SErik.Nordmark@Sun.COM sum += ~ntohs(tcpha->tha_sum) & 0xFFFF;
7560Sstevel@tonic-gate DOWN_SUM(sum);
7570Sstevel@tonic-gate DOWN_SUM(sum);
75811042SErik.Nordmark@Sun.COM tcpha->tha_sum = ~htons(sum);
7590Sstevel@tonic-gate break;
7600Sstevel@tonic-gate case IPPROTO_UDP:
7610Sstevel@tonic-gate udpha = (udpha_t *)(data_mp->b_rptr + IPH_HDR_LENGTH(ipha));
7620Sstevel@tonic-gate
7630Sstevel@tonic-gate if (udpha->uha_checksum != 0) {
7640Sstevel@tonic-gate /* Adujst if the inbound one was not zero. */
7650Sstevel@tonic-gate sum += ~ntohs(udpha->uha_checksum) & 0xFFFF;
7660Sstevel@tonic-gate DOWN_SUM(sum);
7670Sstevel@tonic-gate DOWN_SUM(sum);
7680Sstevel@tonic-gate udpha->uha_checksum = ~htons(sum);
7690Sstevel@tonic-gate if (udpha->uha_checksum == 0)
7700Sstevel@tonic-gate udpha->uha_checksum = 0xFFFF;
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate #undef DOWN_SUM
7730Sstevel@tonic-gate break;
7740Sstevel@tonic-gate case IPPROTO_IP:
7750Sstevel@tonic-gate /*
7760Sstevel@tonic-gate * This case is only an issue for self-encapsulated
7770Sstevel@tonic-gate * packets. So for now, fall through.
7780Sstevel@tonic-gate */
7790Sstevel@tonic-gate break;
7800Sstevel@tonic-gate }
78111042SErik.Nordmark@Sun.COM return (data_mp);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate /*
7863192Sdanmcd * Strip ESP header, check padding, and fix IP header.
7870Sstevel@tonic-gate * Returns B_TRUE on success, B_FALSE if an error occured.
7880Sstevel@tonic-gate */
7890Sstevel@tonic-gate static boolean_t
esp_strip_header(mblk_t * data_mp,boolean_t isv4,uint32_t ivlen,kstat_named_t ** counter,ipsecesp_stack_t * espstack)7900Sstevel@tonic-gate esp_strip_header(mblk_t *data_mp, boolean_t isv4, uint32_t ivlen,
7913448Sdh155122 kstat_named_t **counter, ipsecesp_stack_t *espstack)
7920Sstevel@tonic-gate {
7930Sstevel@tonic-gate ipha_t *ipha;
7940Sstevel@tonic-gate ip6_t *ip6h;
7950Sstevel@tonic-gate uint_t divpoint;
7960Sstevel@tonic-gate mblk_t *scratch;
7970Sstevel@tonic-gate uint8_t nexthdr, padlen;
7980Sstevel@tonic-gate uint8_t lastpad;
7993448Sdh155122 ipsec_stack_t *ipss = espstack->ipsecesp_netstack->netstack_ipsec;
8003192Sdanmcd uint8_t *lastbyte;
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate /*
8030Sstevel@tonic-gate * Strip ESP data and fix IP header.
8040Sstevel@tonic-gate *
8050Sstevel@tonic-gate * XXX In case the beginning of esp_inbound() changes to not do a
8060Sstevel@tonic-gate * pullup, this part of the code can remain unchanged.
8070Sstevel@tonic-gate */
8080Sstevel@tonic-gate if (isv4) {
8090Sstevel@tonic-gate ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ipha_t));
8100Sstevel@tonic-gate ipha = (ipha_t *)data_mp->b_rptr;
8110Sstevel@tonic-gate ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (esph_t) +
8120Sstevel@tonic-gate IPH_HDR_LENGTH(ipha));
8130Sstevel@tonic-gate divpoint = IPH_HDR_LENGTH(ipha);
8140Sstevel@tonic-gate } else {
8150Sstevel@tonic-gate ASSERT((data_mp->b_wptr - data_mp->b_rptr) >= sizeof (ip6_t));
8160Sstevel@tonic-gate ip6h = (ip6_t *)data_mp->b_rptr;
8170Sstevel@tonic-gate divpoint = ip_hdr_length_v6(data_mp, ip6h);
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate scratch = data_mp;
8210Sstevel@tonic-gate while (scratch->b_cont != NULL)
8220Sstevel@tonic-gate scratch = scratch->b_cont;
8230Sstevel@tonic-gate
8240Sstevel@tonic-gate ASSERT((scratch->b_wptr - scratch->b_rptr) >= 3);
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate /*
8270Sstevel@tonic-gate * "Next header" and padding length are the last two bytes in the
8280Sstevel@tonic-gate * ESP-protected datagram, thus the explicit - 1 and - 2.
8290Sstevel@tonic-gate * lastpad is the last byte of the padding, which can be used for
8300Sstevel@tonic-gate * a quick check to see if the padding is correct.
8310Sstevel@tonic-gate */
8323192Sdanmcd lastbyte = scratch->b_wptr - 1;
8333192Sdanmcd nexthdr = *lastbyte--;
8343192Sdanmcd padlen = *lastbyte--;
8350Sstevel@tonic-gate
8360Sstevel@tonic-gate if (isv4) {
8370Sstevel@tonic-gate /* Fix part of the IP header. */
8380Sstevel@tonic-gate ipha->ipha_protocol = nexthdr;
8390Sstevel@tonic-gate /*
8400Sstevel@tonic-gate * Reality check the padlen. The explicit - 2 is for the
8410Sstevel@tonic-gate * padding length and the next-header bytes.
8420Sstevel@tonic-gate */
8430Sstevel@tonic-gate if (padlen >= ntohs(ipha->ipha_length) - sizeof (ipha_t) - 2 -
8440Sstevel@tonic-gate sizeof (esph_t) - ivlen) {
8453448Sdh155122 ESP_BUMP_STAT(espstack, bad_decrypt);
8463448Sdh155122 ipsec_rl_strlog(espstack->ipsecesp_netstack,
8473448Sdh155122 info.mi_idnum, 0, 0,
8483448Sdh155122 SL_ERROR | SL_WARN,
8493192Sdanmcd "Corrupt ESP packet (padlen too big).\n");
8503448Sdh155122 esp1dbg(espstack, ("padlen (%d) is greater than:\n",
8514987Sdanmcd padlen));
8523448Sdh155122 esp1dbg(espstack, ("pkt len(%d) - ip hdr - esp "
8533448Sdh155122 "hdr - ivlen(%d) = %d.\n",
8543448Sdh155122 ntohs(ipha->ipha_length), ivlen,
8550Sstevel@tonic-gate (int)(ntohs(ipha->ipha_length) - sizeof (ipha_t) -
8563448Sdh155122 2 - sizeof (esph_t) - ivlen)));
8573448Sdh155122 *counter = DROPPER(ipss, ipds_esp_bad_padlen);
8580Sstevel@tonic-gate return (B_FALSE);
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate /*
8620Sstevel@tonic-gate * Fix the rest of the header. The explicit - 2 is for the
8630Sstevel@tonic-gate * padding length and the next-header bytes.
8640Sstevel@tonic-gate */
8650Sstevel@tonic-gate ipha->ipha_length = htons(ntohs(ipha->ipha_length) - padlen -
8660Sstevel@tonic-gate 2 - sizeof (esph_t) - ivlen);
8670Sstevel@tonic-gate ipha->ipha_hdr_checksum = 0;
8680Sstevel@tonic-gate ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
8690Sstevel@tonic-gate } else {
8700Sstevel@tonic-gate if (ip6h->ip6_nxt == IPPROTO_ESP) {
8710Sstevel@tonic-gate ip6h->ip6_nxt = nexthdr;
8720Sstevel@tonic-gate } else {
87311042SErik.Nordmark@Sun.COM ip_pkt_t ipp;
8740Sstevel@tonic-gate
8750Sstevel@tonic-gate bzero(&ipp, sizeof (ipp));
87611042SErik.Nordmark@Sun.COM (void) ip_find_hdr_v6(data_mp, ip6h, B_FALSE, &ipp,
87711042SErik.Nordmark@Sun.COM NULL);
8780Sstevel@tonic-gate if (ipp.ipp_dstopts != NULL) {
8790Sstevel@tonic-gate ipp.ipp_dstopts->ip6d_nxt = nexthdr;
8800Sstevel@tonic-gate } else if (ipp.ipp_rthdr != NULL) {
8810Sstevel@tonic-gate ipp.ipp_rthdr->ip6r_nxt = nexthdr;
8820Sstevel@tonic-gate } else if (ipp.ipp_hopopts != NULL) {
8830Sstevel@tonic-gate ipp.ipp_hopopts->ip6h_nxt = nexthdr;
8840Sstevel@tonic-gate } else {
8850Sstevel@tonic-gate /* Panic a DEBUG kernel. */
8860Sstevel@tonic-gate ASSERT(ipp.ipp_hopopts != NULL);
8870Sstevel@tonic-gate /* Otherwise, pretend it's IP + ESP. */
8880Sstevel@tonic-gate cmn_err(CE_WARN, "ESP IPv6 headers wrong.\n");
8890Sstevel@tonic-gate ip6h->ip6_nxt = nexthdr;
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate
8930Sstevel@tonic-gate if (padlen >= ntohs(ip6h->ip6_plen) - 2 - sizeof (esph_t) -
8940Sstevel@tonic-gate ivlen) {
8953448Sdh155122 ESP_BUMP_STAT(espstack, bad_decrypt);
8963448Sdh155122 ipsec_rl_strlog(espstack->ipsecesp_netstack,
8973448Sdh155122 info.mi_idnum, 0, 0,
8983448Sdh155122 SL_ERROR | SL_WARN,
8993192Sdanmcd "Corrupt ESP packet (v6 padlen too big).\n");
9003448Sdh155122 esp1dbg(espstack, ("padlen (%d) is greater than:\n",
9014987Sdanmcd padlen));
9024987Sdanmcd esp1dbg(espstack,
9034987Sdanmcd ("pkt len(%u) - ip hdr - esp hdr - ivlen(%d) = "
9044987Sdanmcd "%u.\n", (unsigned)(ntohs(ip6h->ip6_plen)
9054987Sdanmcd + sizeof (ip6_t)), ivlen,
9064987Sdanmcd (unsigned)(ntohs(ip6h->ip6_plen) - 2 -
9074987Sdanmcd sizeof (esph_t) - ivlen)));
9083448Sdh155122 *counter = DROPPER(ipss, ipds_esp_bad_padlen);
9090Sstevel@tonic-gate return (B_FALSE);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate
9130Sstevel@tonic-gate /*
9140Sstevel@tonic-gate * Fix the rest of the header. The explicit - 2 is for the
9150Sstevel@tonic-gate * padding length and the next-header bytes. IPv6 is nice,
9160Sstevel@tonic-gate * because there's no hdr checksum!
9170Sstevel@tonic-gate */
9180Sstevel@tonic-gate ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - padlen -
9190Sstevel@tonic-gate 2 - sizeof (esph_t) - ivlen);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate
9223448Sdh155122 if (espstack->ipsecesp_padding_check > 0 && padlen > 0) {
9233192Sdanmcd /*
9243192Sdanmcd * Weak padding check: compare last-byte to length, they
9253192Sdanmcd * should be equal.
9263192Sdanmcd */
9273192Sdanmcd lastpad = *lastbyte--;
9283192Sdanmcd
9293192Sdanmcd if (padlen != lastpad) {
9303448Sdh155122 ipsec_rl_strlog(espstack->ipsecesp_netstack,
9313448Sdh155122 info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
9323192Sdanmcd "Corrupt ESP packet (lastpad != padlen).\n");
9333448Sdh155122 esp1dbg(espstack,
9343448Sdh155122 ("lastpad (%d) not equal to padlen (%d):\n",
9353448Sdh155122 lastpad, padlen));
9363448Sdh155122 ESP_BUMP_STAT(espstack, bad_padding);
9373448Sdh155122 *counter = DROPPER(ipss, ipds_esp_bad_padding);
9383192Sdanmcd return (B_FALSE);
9393192Sdanmcd }
9400Sstevel@tonic-gate
9410Sstevel@tonic-gate /*
9423192Sdanmcd * Strong padding check: Check all pad bytes to see that
9433192Sdanmcd * they're ascending. Go backwards using a descending counter
9443192Sdanmcd * to verify. padlen == 1 is checked by previous block, so
9453192Sdanmcd * only bother if we've more than 1 byte of padding.
9463192Sdanmcd * Consequently, start the check one byte before the location
9473192Sdanmcd * of "lastpad".
9480Sstevel@tonic-gate */
9493448Sdh155122 if (espstack->ipsecesp_padding_check > 1) {
9503192Sdanmcd /*
9513192Sdanmcd * This assert may have to become an if and a pullup
9523192Sdanmcd * if we start accepting multi-dblk mblks. For now,
9533192Sdanmcd * though, any packet here will have been pulled up in
9543192Sdanmcd * esp_inbound.
9553192Sdanmcd */
9563192Sdanmcd ASSERT(MBLKL(scratch) >= lastpad + 3);
9573192Sdanmcd
9583192Sdanmcd /*
9593192Sdanmcd * Use "--lastpad" because we already checked the very
9603192Sdanmcd * last pad byte previously.
9613192Sdanmcd */
9623192Sdanmcd while (--lastpad != 0) {
9633192Sdanmcd if (lastpad != *lastbyte) {
9643448Sdh155122 ipsec_rl_strlog(
9653448Sdh155122 espstack->ipsecesp_netstack,
9663448Sdh155122 info.mi_idnum, 0, 0,
9673192Sdanmcd SL_ERROR | SL_WARN, "Corrupt ESP "
9683192Sdanmcd "packet (bad padding).\n");
9693448Sdh155122 esp1dbg(espstack,
9703448Sdh155122 ("padding not in correct"
9713448Sdh155122 " format:\n"));
9723448Sdh155122 ESP_BUMP_STAT(espstack, bad_padding);
9733448Sdh155122 *counter = DROPPER(ipss,
9743448Sdh155122 ipds_esp_bad_padding);
9753192Sdanmcd return (B_FALSE);
9763192Sdanmcd }
9773192Sdanmcd lastbyte--;
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate }
9810Sstevel@tonic-gate
9820Sstevel@tonic-gate /* Trim off the padding. */
9830Sstevel@tonic-gate ASSERT(data_mp->b_cont == NULL);
9840Sstevel@tonic-gate data_mp->b_wptr -= (padlen + 2);
9850Sstevel@tonic-gate
9860Sstevel@tonic-gate /*
9870Sstevel@tonic-gate * Remove the ESP header.
9880Sstevel@tonic-gate *
9890Sstevel@tonic-gate * The above assertions about data_mp's size will make this work.
9900Sstevel@tonic-gate *
9910Sstevel@tonic-gate * XXX Question: If I send up and get back a contiguous mblk,
9920Sstevel@tonic-gate * would it be quicker to bcopy over, or keep doing the dupb stuff?
9930Sstevel@tonic-gate * I go with copying for now.
9940Sstevel@tonic-gate */
9950Sstevel@tonic-gate
9960Sstevel@tonic-gate if (IS_P2ALIGNED(data_mp->b_rptr, sizeof (uint32_t)) &&
9970Sstevel@tonic-gate IS_P2ALIGNED(ivlen, sizeof (uint32_t))) {
9980Sstevel@tonic-gate uint8_t *start = data_mp->b_rptr;
9990Sstevel@tonic-gate uint32_t *src, *dst;
10000Sstevel@tonic-gate
10010Sstevel@tonic-gate src = (uint32_t *)(start + divpoint);
10020Sstevel@tonic-gate dst = (uint32_t *)(start + divpoint + sizeof (esph_t) + ivlen);
10030Sstevel@tonic-gate
10040Sstevel@tonic-gate ASSERT(IS_P2ALIGNED(dst, sizeof (uint32_t)) &&
10050Sstevel@tonic-gate IS_P2ALIGNED(src, sizeof (uint32_t)));
10060Sstevel@tonic-gate
10070Sstevel@tonic-gate do {
10080Sstevel@tonic-gate src--;
10090Sstevel@tonic-gate dst--;
10100Sstevel@tonic-gate *dst = *src;
10110Sstevel@tonic-gate } while (src != (uint32_t *)start);
10120Sstevel@tonic-gate
10130Sstevel@tonic-gate data_mp->b_rptr = (uchar_t *)dst;
10140Sstevel@tonic-gate } else {
10150Sstevel@tonic-gate uint8_t *start = data_mp->b_rptr;
10160Sstevel@tonic-gate uint8_t *src, *dst;
10170Sstevel@tonic-gate
10180Sstevel@tonic-gate src = start + divpoint;
10190Sstevel@tonic-gate dst = src + sizeof (esph_t) + ivlen;
10200Sstevel@tonic-gate
10210Sstevel@tonic-gate do {
10220Sstevel@tonic-gate src--;
10230Sstevel@tonic-gate dst--;
10240Sstevel@tonic-gate *dst = *src;
10250Sstevel@tonic-gate } while (src != start);
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate data_mp->b_rptr = dst;
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate
10303448Sdh155122 esp2dbg(espstack, ("data_mp after inbound ESP adjustment:\n"));
10313448Sdh155122 esp2dbg(espstack, (dump_msg(data_mp)));
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate return (B_TRUE);
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate
10360Sstevel@tonic-gate /*
10370Sstevel@tonic-gate * Updating use times can be tricky business if the ipsa_haspeer flag is
10380Sstevel@tonic-gate * set. This function is called once in an SA's lifetime.
10390Sstevel@tonic-gate *
10400Sstevel@tonic-gate * Caller has to REFRELE "assoc" which is passed in. This function has
10410Sstevel@tonic-gate * to REFRELE any peer SA that is obtained.
10420Sstevel@tonic-gate */
10430Sstevel@tonic-gate static void
esp_set_usetime(ipsa_t * assoc,boolean_t inbound)10440Sstevel@tonic-gate esp_set_usetime(ipsa_t *assoc, boolean_t inbound)
10450Sstevel@tonic-gate {
10460Sstevel@tonic-gate ipsa_t *inassoc, *outassoc;
10470Sstevel@tonic-gate isaf_t *bucket;
10480Sstevel@tonic-gate sadb_t *sp;
10490Sstevel@tonic-gate int outhash;
10500Sstevel@tonic-gate boolean_t isv6;
10513448Sdh155122 netstack_t *ns = assoc->ipsa_netstack;
10523448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate /* No peer? No problem! */
10550Sstevel@tonic-gate if (!assoc->ipsa_haspeer) {
10560Sstevel@tonic-gate sadb_set_usetime(assoc);
10570Sstevel@tonic-gate return;
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate
10600Sstevel@tonic-gate /*
10610Sstevel@tonic-gate * Otherwise, we want to grab both the original assoc and its peer.
10620Sstevel@tonic-gate * There might be a race for this, but if it's a real race, the times
10630Sstevel@tonic-gate * will be out-of-synch by at most a second, and since our time
10640Sstevel@tonic-gate * granularity is a second, this won't be a problem.
10650Sstevel@tonic-gate *
10660Sstevel@tonic-gate * If we need tight synchronization on the peer SA, then we need to
10670Sstevel@tonic-gate * reconsider.
10680Sstevel@tonic-gate */
10690Sstevel@tonic-gate
10700Sstevel@tonic-gate /* Use address length to select IPv6/IPv4 */
10710Sstevel@tonic-gate isv6 = (assoc->ipsa_addrfam == AF_INET6);
10723448Sdh155122 sp = isv6 ? &espstack->esp_sadb.s_v6 : &espstack->esp_sadb.s_v4;
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate if (inbound) {
10750Sstevel@tonic-gate inassoc = assoc;
10760Sstevel@tonic-gate if (isv6) {
1077564Ssommerfe outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *)
10780Sstevel@tonic-gate &inassoc->ipsa_dstaddr));
10790Sstevel@tonic-gate } else {
1080564Ssommerfe outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *)
10814987Sdanmcd &inassoc->ipsa_dstaddr));
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate bucket = &sp->sdb_of[outhash];
10840Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock);
10850Sstevel@tonic-gate outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
10860Sstevel@tonic-gate inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
10870Sstevel@tonic-gate inassoc->ipsa_addrfam);
10880Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock);
10890Sstevel@tonic-gate if (outassoc == NULL) {
10900Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */
10910Sstevel@tonic-gate esp0dbg(("esp_set_usetime: "
10920Sstevel@tonic-gate "can't find peer for inbound.\n"));
10930Sstevel@tonic-gate sadb_set_usetime(inassoc);
10940Sstevel@tonic-gate return;
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate } else {
10970Sstevel@tonic-gate outassoc = assoc;
1098564Ssommerfe bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
10990Sstevel@tonic-gate mutex_enter(&bucket->isaf_lock);
11000Sstevel@tonic-gate inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
11010Sstevel@tonic-gate outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
11020Sstevel@tonic-gate outassoc->ipsa_addrfam);
11030Sstevel@tonic-gate mutex_exit(&bucket->isaf_lock);
11040Sstevel@tonic-gate if (inassoc == NULL) {
11050Sstevel@tonic-gate /* Q: Do we wish to set haspeer == B_FALSE? */
11060Sstevel@tonic-gate esp0dbg(("esp_set_usetime: "
11070Sstevel@tonic-gate "can't find peer for outbound.\n"));
11080Sstevel@tonic-gate sadb_set_usetime(outassoc);
11090Sstevel@tonic-gate return;
11100Sstevel@tonic-gate }
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate
11130Sstevel@tonic-gate /* Update usetime on both. */
11140Sstevel@tonic-gate sadb_set_usetime(inassoc);
11150Sstevel@tonic-gate sadb_set_usetime(outassoc);
11160Sstevel@tonic-gate
11170Sstevel@tonic-gate /*
11180Sstevel@tonic-gate * REFRELE any peer SA.
11190Sstevel@tonic-gate *
11200Sstevel@tonic-gate * Because of the multi-line macro nature of IPSA_REFRELE, keep
11210Sstevel@tonic-gate * them in { }.
11220Sstevel@tonic-gate */
11230Sstevel@tonic-gate if (inbound) {
11240Sstevel@tonic-gate IPSA_REFRELE(outassoc);
11250Sstevel@tonic-gate } else {
11260Sstevel@tonic-gate IPSA_REFRELE(inassoc);
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate
11300Sstevel@tonic-gate /*
11310Sstevel@tonic-gate * Handle ESP inbound data for IPv4 and IPv6.
11320Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the
113311042SErik.Nordmark@Sun.COM * mblk chain data_mp.
11340Sstevel@tonic-gate */
113511042SErik.Nordmark@Sun.COM mblk_t *
esp_inbound(mblk_t * data_mp,void * arg,ip_recv_attr_t * ira)113611042SErik.Nordmark@Sun.COM esp_inbound(mblk_t *data_mp, void *arg, ip_recv_attr_t *ira)
11370Sstevel@tonic-gate {
11380Sstevel@tonic-gate esph_t *esph = (esph_t *)arg;
113911042SErik.Nordmark@Sun.COM ipsa_t *ipsa = ira->ira_ipsec_esp_sa;
114011042SErik.Nordmark@Sun.COM netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
11413448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
11423448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec;
11430Sstevel@tonic-gate
11440Sstevel@tonic-gate /*
11450Sstevel@tonic-gate * We may wish to check replay in-range-only here as an optimization.
11460Sstevel@tonic-gate * Include the reality check of ipsa->ipsa_replay >
11470Sstevel@tonic-gate * ipsa->ipsa_replay_wsize for times when it's the first N packets,
11480Sstevel@tonic-gate * where N == ipsa->ipsa_replay_wsize.
11490Sstevel@tonic-gate *
11500Sstevel@tonic-gate * Another check that may come here later is the "collision" check.
11510Sstevel@tonic-gate * If legitimate packets flow quickly enough, this won't be a problem,
11520Sstevel@tonic-gate * but collisions may cause authentication algorithm crunching to
11530Sstevel@tonic-gate * take place when it doesn't need to.
11540Sstevel@tonic-gate */
11550Sstevel@tonic-gate if (!sadb_replay_peek(ipsa, esph->esph_replay)) {
11563448Sdh155122 ESP_BUMP_STAT(espstack, replay_early_failures);
11573448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards);
115811042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
11593448Sdh155122 DROPPER(ipss, ipds_esp_early_replay),
11603448Sdh155122 &espstack->esp_dropper);
116111042SErik.Nordmark@Sun.COM BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
116211042SErik.Nordmark@Sun.COM return (NULL);
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate
11650Sstevel@tonic-gate /*
11660Sstevel@tonic-gate * Adjust the IP header's payload length to reflect the removal
11670Sstevel@tonic-gate * of the ICV.
11680Sstevel@tonic-gate */
116911042SErik.Nordmark@Sun.COM if (!(ira->ira_flags & IRAF_IS_IPV4)) {
11700Sstevel@tonic-gate ip6_t *ip6h = (ip6_t *)data_mp->b_rptr;
11710Sstevel@tonic-gate ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) -
11720Sstevel@tonic-gate ipsa->ipsa_mac_len);
11730Sstevel@tonic-gate } else {
11740Sstevel@tonic-gate ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
11750Sstevel@tonic-gate ipha->ipha_length = htons(ntohs(ipha->ipha_length) -
11760Sstevel@tonic-gate ipsa->ipsa_mac_len);
11770Sstevel@tonic-gate }
11780Sstevel@tonic-gate
11790Sstevel@tonic-gate /* submit the request to the crypto framework */
118011042SErik.Nordmark@Sun.COM return (esp_submit_req_inbound(data_mp, ira, ipsa,
11810Sstevel@tonic-gate (uint8_t *)esph - data_mp->b_rptr));
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate
11840Sstevel@tonic-gate /*
11850Sstevel@tonic-gate * Perform the really difficult work of inserting the proposed situation.
11860Sstevel@tonic-gate * Called while holding the algorithm lock.
11870Sstevel@tonic-gate */
11880Sstevel@tonic-gate static void
esp_insert_prop(sadb_prop_t * prop,ipsacq_t * acqrec,uint_t combs,netstack_t * ns)118911042SErik.Nordmark@Sun.COM esp_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs,
119011042SErik.Nordmark@Sun.COM netstack_t *ns)
11910Sstevel@tonic-gate {
11920Sstevel@tonic-gate sadb_comb_t *comb = (sadb_comb_t *)(prop + 1);
11930Sstevel@tonic-gate ipsec_action_t *ap;
11940Sstevel@tonic-gate ipsec_prot_t *prot;
119511042SErik.Nordmark@Sun.COM ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
119611042SErik.Nordmark@Sun.COM ipsec_stack_t *ipss = ns->netstack_ipsec;
119711042SErik.Nordmark@Sun.COM
11983448Sdh155122 ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
11990Sstevel@tonic-gate
12000Sstevel@tonic-gate prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
12010Sstevel@tonic-gate prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t));
12020Sstevel@tonic-gate *(uint32_t *)(&prop->sadb_prop_replay) = 0; /* Quick zero-out! */
12030Sstevel@tonic-gate
12043448Sdh155122 prop->sadb_prop_replay = espstack->ipsecesp_replay_size;
12050Sstevel@tonic-gate
12060Sstevel@tonic-gate /*
120711042SErik.Nordmark@Sun.COM * Based upon algorithm properties, and what-not, prioritize a
120811042SErik.Nordmark@Sun.COM * proposal, based on the ordering of the ESP algorithms in the
120911042SErik.Nordmark@Sun.COM * alternatives in the policy rule or socket that was placed
121011042SErik.Nordmark@Sun.COM * in the acquire record.
12110Sstevel@tonic-gate *
12120Sstevel@tonic-gate * For each action in policy list
12130Sstevel@tonic-gate * Add combination. If I've hit limit, return.
12140Sstevel@tonic-gate */
12150Sstevel@tonic-gate
12160Sstevel@tonic-gate for (ap = acqrec->ipsacq_act; ap != NULL;
12170Sstevel@tonic-gate ap = ap->ipa_next) {
12180Sstevel@tonic-gate ipsec_alginfo_t *ealg = NULL;
12190Sstevel@tonic-gate ipsec_alginfo_t *aalg = NULL;
12200Sstevel@tonic-gate
12210Sstevel@tonic-gate if (ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY)
12220Sstevel@tonic-gate continue;
12230Sstevel@tonic-gate
12240Sstevel@tonic-gate prot = &ap->ipa_act.ipa_apply;
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate if (!(prot->ipp_use_esp))
12270Sstevel@tonic-gate continue;
12280Sstevel@tonic-gate
12290Sstevel@tonic-gate if (prot->ipp_esp_auth_alg != 0) {
12303448Sdh155122 aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
12310Sstevel@tonic-gate [prot->ipp_esp_auth_alg];
12320Sstevel@tonic-gate if (aalg == NULL || !ALG_VALID(aalg))
12330Sstevel@tonic-gate continue;
12340Sstevel@tonic-gate }
12350Sstevel@tonic-gate
12360Sstevel@tonic-gate ASSERT(prot->ipp_encr_alg > 0);
12373448Sdh155122 ealg = ipss->ipsec_alglists[IPSEC_ALG_ENCR]
12383448Sdh155122 [prot->ipp_encr_alg];
12390Sstevel@tonic-gate if (ealg == NULL || !ALG_VALID(ealg))
12400Sstevel@tonic-gate continue;
12410Sstevel@tonic-gate
12420Sstevel@tonic-gate comb->sadb_comb_flags = 0;
12430Sstevel@tonic-gate comb->sadb_comb_reserved = 0;
12440Sstevel@tonic-gate comb->sadb_comb_encrypt = ealg->alg_id;
12452751Sdanmcd comb->sadb_comb_encrypt_minbits =
12462751Sdanmcd MAX(prot->ipp_espe_minbits, ealg->alg_ef_minbits);
12472751Sdanmcd comb->sadb_comb_encrypt_maxbits =
12482751Sdanmcd MIN(prot->ipp_espe_maxbits, ealg->alg_ef_maxbits);
124910824SMark.Fenwick@Sun.COM
12500Sstevel@tonic-gate if (aalg == NULL) {
12510Sstevel@tonic-gate comb->sadb_comb_auth = 0;
12520Sstevel@tonic-gate comb->sadb_comb_auth_minbits = 0;
12530Sstevel@tonic-gate comb->sadb_comb_auth_maxbits = 0;
12540Sstevel@tonic-gate } else {
12550Sstevel@tonic-gate comb->sadb_comb_auth = aalg->alg_id;
12562751Sdanmcd comb->sadb_comb_auth_minbits =
12572751Sdanmcd MAX(prot->ipp_espa_minbits, aalg->alg_ef_minbits);
12582751Sdanmcd comb->sadb_comb_auth_maxbits =
12592751Sdanmcd MIN(prot->ipp_espa_maxbits, aalg->alg_ef_maxbits);
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate
12620Sstevel@tonic-gate /*
12630Sstevel@tonic-gate * The following may be based on algorithm
12640Sstevel@tonic-gate * properties, but in the meantime, we just pick
12650Sstevel@tonic-gate * some good, sensible numbers. Key mgmt. can
12660Sstevel@tonic-gate * (and perhaps should) be the place to finalize
12670Sstevel@tonic-gate * such decisions.
12680Sstevel@tonic-gate */
12690Sstevel@tonic-gate
12700Sstevel@tonic-gate /*
12710Sstevel@tonic-gate * No limits on allocations, since we really don't
12720Sstevel@tonic-gate * support that concept currently.
12730Sstevel@tonic-gate */
12740Sstevel@tonic-gate comb->sadb_comb_soft_allocations = 0;
12750Sstevel@tonic-gate comb->sadb_comb_hard_allocations = 0;
12760Sstevel@tonic-gate
12770Sstevel@tonic-gate /*
12780Sstevel@tonic-gate * These may want to come from policy rule..
12790Sstevel@tonic-gate */
12803448Sdh155122 comb->sadb_comb_soft_bytes =
12813448Sdh155122 espstack->ipsecesp_default_soft_bytes;
12823448Sdh155122 comb->sadb_comb_hard_bytes =
12833448Sdh155122 espstack->ipsecesp_default_hard_bytes;
12843448Sdh155122 comb->sadb_comb_soft_addtime =
12853448Sdh155122 espstack->ipsecesp_default_soft_addtime;
12863448Sdh155122 comb->sadb_comb_hard_addtime =
12873448Sdh155122 espstack->ipsecesp_default_hard_addtime;
12883448Sdh155122 comb->sadb_comb_soft_usetime =
12893448Sdh155122 espstack->ipsecesp_default_soft_usetime;
12903448Sdh155122 comb->sadb_comb_hard_usetime =
12913448Sdh155122 espstack->ipsecesp_default_hard_usetime;
12920Sstevel@tonic-gate
12930Sstevel@tonic-gate prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
12940Sstevel@tonic-gate if (--combs == 0)
12950Sstevel@tonic-gate break; /* out of space.. */
12960Sstevel@tonic-gate comb++;
12970Sstevel@tonic-gate }
12980Sstevel@tonic-gate }
12990Sstevel@tonic-gate
13000Sstevel@tonic-gate /*
13010Sstevel@tonic-gate * Prepare and actually send the SADB_ACQUIRE message to PF_KEY.
13020Sstevel@tonic-gate */
13030Sstevel@tonic-gate static void
esp_send_acquire(ipsacq_t * acqrec,mblk_t * extended,netstack_t * ns)13043448Sdh155122 esp_send_acquire(ipsacq_t *acqrec, mblk_t *extended, netstack_t *ns)
13050Sstevel@tonic-gate {
13063055Sdanmcd uint_t combs;
13070Sstevel@tonic-gate sadb_msg_t *samsg;
13080Sstevel@tonic-gate sadb_prop_t *prop;
13093055Sdanmcd mblk_t *pfkeymp, *msgmp;
13103448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
13113448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec;
13123448Sdh155122
13133448Sdh155122 ESP_BUMP_STAT(espstack, acquire_requests);
13143448Sdh155122
13157073Spwernau if (espstack->esp_pfkey_q == NULL) {
13167073Spwernau mutex_exit(&acqrec->ipsacq_lock);
13173055Sdanmcd return;
13187073Spwernau }
13190Sstevel@tonic-gate
13200Sstevel@tonic-gate /* Set up ACQUIRE. */
13213448Sdh155122 pfkeymp = sadb_setup_acquire(acqrec, SADB_SATYPE_ESP,
13223448Sdh155122 ns->netstack_ipsec);
13233055Sdanmcd if (pfkeymp == NULL) {
13240Sstevel@tonic-gate esp0dbg(("sadb_setup_acquire failed.\n"));
13257073Spwernau mutex_exit(&acqrec->ipsacq_lock);
13263055Sdanmcd return;
13270Sstevel@tonic-gate }
13283448Sdh155122 ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
13293448Sdh155122 combs = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] *
13303448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
13313055Sdanmcd msgmp = pfkeymp->b_cont;
13323055Sdanmcd samsg = (sadb_msg_t *)(msgmp->b_rptr);
13330Sstevel@tonic-gate
13340Sstevel@tonic-gate /* Insert proposal here. */
13350Sstevel@tonic-gate
13360Sstevel@tonic-gate prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len);
133711042SErik.Nordmark@Sun.COM esp_insert_prop(prop, acqrec, combs, ns);
13380Sstevel@tonic-gate samsg->sadb_msg_len += prop->sadb_prop_len;
13390Sstevel@tonic-gate msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len);
13400Sstevel@tonic-gate
13413448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
13420Sstevel@tonic-gate
13430Sstevel@tonic-gate /*
13440Sstevel@tonic-gate * Must mutex_exit() before sending PF_KEY message up, in
13450Sstevel@tonic-gate * order to avoid recursive mutex_enter() if there are no registered
13460Sstevel@tonic-gate * listeners.
13470Sstevel@tonic-gate *
13480Sstevel@tonic-gate * Once I've sent the message, I'm cool anyway.
13490Sstevel@tonic-gate */
13500Sstevel@tonic-gate mutex_exit(&acqrec->ipsacq_lock);
13513055Sdanmcd if (extended != NULL) {
13523448Sdh155122 putnext(espstack->esp_pfkey_q, extended);
13530Sstevel@tonic-gate }
13543448Sdh155122 putnext(espstack->esp_pfkey_q, pfkeymp);
13550Sstevel@tonic-gate }
13560Sstevel@tonic-gate
135710934Ssommerfeld@sun.com /* XXX refactor me */
13580Sstevel@tonic-gate /*
13590Sstevel@tonic-gate * Handle the SADB_GETSPI message. Create a larval SA.
13600Sstevel@tonic-gate */
13610Sstevel@tonic-gate static void
esp_getspi(mblk_t * mp,keysock_in_t * ksi,ipsecesp_stack_t * espstack)13623448Sdh155122 esp_getspi(mblk_t *mp, keysock_in_t *ksi, ipsecesp_stack_t *espstack)
13630Sstevel@tonic-gate {
13640Sstevel@tonic-gate ipsa_t *newbie, *target;
13650Sstevel@tonic-gate isaf_t *outbound, *inbound;
13660Sstevel@tonic-gate int rc, diagnostic;
13670Sstevel@tonic-gate sadb_sa_t *assoc;
13680Sstevel@tonic-gate keysock_out_t *kso;
13690Sstevel@tonic-gate uint32_t newspi;
13700Sstevel@tonic-gate
13710Sstevel@tonic-gate /*
13720Sstevel@tonic-gate * Randomly generate a proposed SPI value
13730Sstevel@tonic-gate */
13747749SThejaswini.Singarajipura@Sun.COM if (cl_inet_getspi != NULL) {
13758392SHuafeng.Lv@Sun.COM cl_inet_getspi(espstack->ipsecesp_netstack->netstack_stackid,
13768392SHuafeng.Lv@Sun.COM IPPROTO_ESP, (uint8_t *)&newspi, sizeof (uint32_t), NULL);
13777749SThejaswini.Singarajipura@Sun.COM } else {
13787749SThejaswini.Singarajipura@Sun.COM (void) random_get_pseudo_bytes((uint8_t *)&newspi,
13797749SThejaswini.Singarajipura@Sun.COM sizeof (uint32_t));
13807749SThejaswini.Singarajipura@Sun.COM }
13813448Sdh155122 newbie = sadb_getspi(ksi, newspi, &diagnostic,
13827749SThejaswini.Singarajipura@Sun.COM espstack->ipsecesp_netstack, IPPROTO_ESP);
13830Sstevel@tonic-gate
13840Sstevel@tonic-gate if (newbie == NULL) {
13853448Sdh155122 sadb_pfkey_error(espstack->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) {
13893448Sdh155122 sadb_pfkey_error(espstack->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) {
14013448Sdh155122 outbound = OUTBOUND_BUCKET_V6(&espstack->esp_sadb.s_v6,
1402564Ssommerfe *(uint32_t *)(newbie->ipsa_dstaddr));
14033448Sdh155122 inbound = INBOUND_BUCKET(&espstack->esp_sadb.s_v6,
14043448Sdh155122 newbie->ipsa_spi);
14050Sstevel@tonic-gate } else {
14060Sstevel@tonic-gate ASSERT(newbie->ipsa_addrfam == AF_INET);
14073448Sdh155122 outbound = OUTBOUND_BUCKET_V4(&espstack->esp_sadb.s_v4,
1408564Ssommerfe *(uint32_t *)(newbie->ipsa_dstaddr));
14093448Sdh155122 inbound = INBOUND_BUCKET(&espstack->esp_sadb.s_v4,
14103448Sdh155122 newbie->ipsa_spi);
14110Sstevel@tonic-gate }
14120Sstevel@tonic-gate
14130Sstevel@tonic-gate mutex_enter(&outbound->isaf_lock);
14140Sstevel@tonic-gate mutex_enter(&inbound->isaf_lock);
14150Sstevel@tonic-gate
14160Sstevel@tonic-gate /*
14170Sstevel@tonic-gate * Check for collisions (i.e. did sadb_getspi() return with something
14180Sstevel@tonic-gate * that already exists?).
14190Sstevel@tonic-gate *
14200Sstevel@tonic-gate * Try outbound first. Even though SADB_GETSPI is traditionally
14210Sstevel@tonic-gate * for inbound SAs, you never know what a user might do.
14220Sstevel@tonic-gate */
14230Sstevel@tonic-gate target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi,
14240Sstevel@tonic-gate newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam);
14250Sstevel@tonic-gate if (target == NULL) {
14260Sstevel@tonic-gate target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi,
14270Sstevel@tonic-gate newbie->ipsa_srcaddr, newbie->ipsa_dstaddr,
14280Sstevel@tonic-gate newbie->ipsa_addrfam);
14290Sstevel@tonic-gate }
14300Sstevel@tonic-gate
14310Sstevel@tonic-gate /*
14320Sstevel@tonic-gate * I don't have collisions elsewhere!
14330Sstevel@tonic-gate * (Nor will I because I'm still holding inbound/outbound locks.)
14340Sstevel@tonic-gate */
14350Sstevel@tonic-gate
14360Sstevel@tonic-gate if (target != NULL) {
14370Sstevel@tonic-gate rc = EEXIST;
14380Sstevel@tonic-gate IPSA_REFRELE(target);
14390Sstevel@tonic-gate } else {
14400Sstevel@tonic-gate /*
14410Sstevel@tonic-gate * sadb_insertassoc() also checks for collisions, so
14420Sstevel@tonic-gate * if there's a colliding entry, rc will be set
14430Sstevel@tonic-gate * to EEXIST.
14440Sstevel@tonic-gate */
14450Sstevel@tonic-gate rc = sadb_insertassoc(newbie, inbound);
14464987Sdanmcd newbie->ipsa_hardexpiretime = gethrestime_sec();
14473448Sdh155122 newbie->ipsa_hardexpiretime +=
14483448Sdh155122 espstack->ipsecesp_larval_timeout;
14490Sstevel@tonic-gate }
14500Sstevel@tonic-gate
14510Sstevel@tonic-gate /*
14520Sstevel@tonic-gate * Can exit outbound mutex. Hold inbound until we're done
14530Sstevel@tonic-gate * with newbie.
14540Sstevel@tonic-gate */
14550Sstevel@tonic-gate mutex_exit(&outbound->isaf_lock);
14560Sstevel@tonic-gate
14570Sstevel@tonic-gate if (rc != 0) {
14580Sstevel@tonic-gate mutex_exit(&inbound->isaf_lock);
14590Sstevel@tonic-gate IPSA_REFRELE(newbie);
14603448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, rc,
14613448Sdh155122 SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial);
14620Sstevel@tonic-gate return;
14630Sstevel@tonic-gate }
14640Sstevel@tonic-gate
14650Sstevel@tonic-gate
14660Sstevel@tonic-gate /* Can write here because I'm still holding the bucket lock. */
14670Sstevel@tonic-gate newbie->ipsa_type = SADB_SATYPE_ESP;
14680Sstevel@tonic-gate
14690Sstevel@tonic-gate /*
14706668Smarkfen * Construct successful return message. We have one thing going
14710Sstevel@tonic-gate * for us in PF_KEY v2. That's the fact that
14720Sstevel@tonic-gate * sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
14730Sstevel@tonic-gate */
14740Sstevel@tonic-gate assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
14750Sstevel@tonic-gate assoc->sadb_sa_exttype = SADB_EXT_SA;
14760Sstevel@tonic-gate assoc->sadb_sa_spi = newbie->ipsa_spi;
14770Sstevel@tonic-gate *((uint64_t *)(&assoc->sadb_sa_replay)) = 0;
14780Sstevel@tonic-gate mutex_exit(&inbound->isaf_lock);
14790Sstevel@tonic-gate
14800Sstevel@tonic-gate /* Convert KEYSOCK_IN to KEYSOCK_OUT. */
14810Sstevel@tonic-gate kso = (keysock_out_t *)ksi;
14820Sstevel@tonic-gate kso->ks_out_len = sizeof (*kso);
14830Sstevel@tonic-gate kso->ks_out_serial = ksi->ks_in_serial;
14840Sstevel@tonic-gate kso->ks_out_type = KEYSOCK_OUT;
14850Sstevel@tonic-gate
14860Sstevel@tonic-gate /*
14870Sstevel@tonic-gate * Can safely putnext() to esp_pfkey_q, because this is a turnaround
14880Sstevel@tonic-gate * from the esp_pfkey_q.
14890Sstevel@tonic-gate */
14903448Sdh155122 putnext(espstack->esp_pfkey_q, mp);
14910Sstevel@tonic-gate }
14920Sstevel@tonic-gate
14930Sstevel@tonic-gate /*
14940Sstevel@tonic-gate * Insert the ESP header into a packet. Duplicate an mblk, and insert a newly
14950Sstevel@tonic-gate * allocated mblk with the ESP header in between the two.
14960Sstevel@tonic-gate */
14970Sstevel@tonic-gate static boolean_t
esp_insert_esp(mblk_t * mp,mblk_t * esp_mp,uint_t divpoint,ipsecesp_stack_t * espstack)14983448Sdh155122 esp_insert_esp(mblk_t *mp, mblk_t *esp_mp, uint_t divpoint,
14993448Sdh155122 ipsecesp_stack_t *espstack)
15000Sstevel@tonic-gate {
15010Sstevel@tonic-gate mblk_t *split_mp = mp;
15020Sstevel@tonic-gate uint_t wheretodiv = divpoint;
15030Sstevel@tonic-gate
15040Sstevel@tonic-gate while ((split_mp->b_wptr - split_mp->b_rptr) < wheretodiv) {
15050Sstevel@tonic-gate wheretodiv -= (split_mp->b_wptr - split_mp->b_rptr);
15060Sstevel@tonic-gate split_mp = split_mp->b_cont;
15070Sstevel@tonic-gate ASSERT(split_mp != NULL);
15080Sstevel@tonic-gate }
15090Sstevel@tonic-gate
15100Sstevel@tonic-gate if (split_mp->b_wptr - split_mp->b_rptr != wheretodiv) {
15110Sstevel@tonic-gate mblk_t *scratch;
15120Sstevel@tonic-gate
15130Sstevel@tonic-gate /* "scratch" is the 2nd half, split_mp is the first. */
15140Sstevel@tonic-gate scratch = dupb(split_mp);
15150Sstevel@tonic-gate if (scratch == NULL) {
15163448Sdh155122 esp1dbg(espstack,
15173448Sdh155122 ("esp_insert_esp: can't allocate scratch.\n"));
15180Sstevel@tonic-gate return (B_FALSE);
15190Sstevel@tonic-gate }
15200Sstevel@tonic-gate /* NOTE: dupb() doesn't set b_cont appropriately. */
15210Sstevel@tonic-gate scratch->b_cont = split_mp->b_cont;
15220Sstevel@tonic-gate scratch->b_rptr += wheretodiv;
15230Sstevel@tonic-gate split_mp->b_wptr = split_mp->b_rptr + wheretodiv;
15240Sstevel@tonic-gate split_mp->b_cont = scratch;
15250Sstevel@tonic-gate }
15260Sstevel@tonic-gate /*
15270Sstevel@tonic-gate * At this point, split_mp is exactly "wheretodiv" bytes long, and
15280Sstevel@tonic-gate * holds the end of the pre-ESP part of the datagram.
15290Sstevel@tonic-gate */
15300Sstevel@tonic-gate esp_mp->b_cont = split_mp->b_cont;
15310Sstevel@tonic-gate split_mp->b_cont = esp_mp;
15320Sstevel@tonic-gate
15330Sstevel@tonic-gate return (B_TRUE);
15340Sstevel@tonic-gate }
15350Sstevel@tonic-gate
15360Sstevel@tonic-gate /*
15377066Sdanmcd * Section 7 of RFC 3947 says:
15387066Sdanmcd *
15397066Sdanmcd * 7. Recovering from the Expiring NAT Mappings
15407066Sdanmcd *
15417066Sdanmcd * There are cases where NAT box decides to remove mappings that are still
15427066Sdanmcd * alive (for example, when the keepalive interval is too long, or when the
15437066Sdanmcd * NAT box is rebooted). To recover from this, ends that are NOT behind
15447066Sdanmcd * NAT SHOULD use the last valid UDP encapsulated IKE or IPsec packet from
15457066Sdanmcd * the other end to determine which IP and port addresses should be used.
15467066Sdanmcd * The host behind dynamic NAT MUST NOT do this, as otherwise it opens a
15477066Sdanmcd * DoS attack possibility because the IP address or port of the other host
15487066Sdanmcd * will not change (it is not behind NAT).
15497066Sdanmcd *
15507066Sdanmcd * Keepalives cannot be used for these purposes, as they are not
15517066Sdanmcd * authenticated, but any IKE authenticated IKE packet or ESP packet can be
15527066Sdanmcd * used to detect whether the IP address or the port has changed.
15537066Sdanmcd *
15547066Sdanmcd * The following function will check an SA and its explicitly-set pair to see
15557066Sdanmcd * if the NAT-T remote port matches the received packet (which must have
15567066Sdanmcd * passed ESP authentication, see esp_in_done() for the caller context). If
15577066Sdanmcd * there is a mismatch, the SAs are updated. It is not important if we race
15587066Sdanmcd * with a transmitting thread, as if there is a transmitting thread, it will
15597066Sdanmcd * merely emit a packet that will most-likely be dropped.
15607066Sdanmcd *
15617066Sdanmcd * "ports" are ordered src,dst, and assoc is an inbound SA, where src should
15627066Sdanmcd * match ipsa_remote_nat_port and dst should match ipsa_local_nat_port.
15637066Sdanmcd */
15647066Sdanmcd #ifdef _LITTLE_ENDIAN
15657066Sdanmcd #define FIRST_16(x) ((x) & 0xFFFF)
15667066Sdanmcd #define NEXT_16(x) (((x) >> 16) & 0xFFFF)
15677066Sdanmcd #else
15687066Sdanmcd #define FIRST_16(x) (((x) >> 16) & 0xFFFF)
15697066Sdanmcd #define NEXT_16(x) ((x) & 0xFFFF)
15707066Sdanmcd #endif
15717066Sdanmcd static void
esp_port_freshness(uint32_t ports,ipsa_t * assoc)15727066Sdanmcd esp_port_freshness(uint32_t ports, ipsa_t *assoc)
15737066Sdanmcd {
15747066Sdanmcd uint16_t remote = FIRST_16(ports);
15757066Sdanmcd uint16_t local = NEXT_16(ports);
15767066Sdanmcd ipsa_t *outbound_peer;
15777066Sdanmcd isaf_t *bucket;
15787066Sdanmcd ipsecesp_stack_t *espstack = assoc->ipsa_netstack->netstack_ipsecesp;
15797066Sdanmcd
15807066Sdanmcd /* We found a conn_t, therefore local != 0. */
15817066Sdanmcd ASSERT(local != 0);
15827066Sdanmcd /* Assume an IPv4 SA. */
15837066Sdanmcd ASSERT(assoc->ipsa_addrfam == AF_INET);
15847066Sdanmcd
15857066Sdanmcd /*
15867066Sdanmcd * On-the-wire rport == 0 means something's very wrong.
15877066Sdanmcd * An unpaired SA is also useless to us.
15887066Sdanmcd * If we are behind the NAT, don't bother.
15897066Sdanmcd * A zero local NAT port defaults to 4500, so check that too.
15907066Sdanmcd * And, of course, if the ports already match, we don't need to
15917066Sdanmcd * bother.
15927066Sdanmcd */
15937066Sdanmcd if (remote == 0 || assoc->ipsa_otherspi == 0 ||
15947066Sdanmcd (assoc->ipsa_flags & IPSA_F_BEHIND_NAT) ||
15957066Sdanmcd (assoc->ipsa_remote_nat_port == 0 &&
15967066Sdanmcd remote == htons(IPPORT_IKE_NATT)) ||
15977066Sdanmcd remote == assoc->ipsa_remote_nat_port)
15987066Sdanmcd return;
15997066Sdanmcd
16007066Sdanmcd /* Try and snag the peer. NOTE: Assume IPv4 for now. */
16017066Sdanmcd bucket = OUTBOUND_BUCKET_V4(&(espstack->esp_sadb.s_v4),
16027066Sdanmcd assoc->ipsa_srcaddr[0]);
16037066Sdanmcd mutex_enter(&bucket->isaf_lock);
16047066Sdanmcd outbound_peer = ipsec_getassocbyspi(bucket, assoc->ipsa_otherspi,
16057066Sdanmcd assoc->ipsa_dstaddr, assoc->ipsa_srcaddr, AF_INET);
16067066Sdanmcd mutex_exit(&bucket->isaf_lock);
16077066Sdanmcd
16087066Sdanmcd /* We probably lost a race to a deleting or expiring thread. */
16097066Sdanmcd if (outbound_peer == NULL)
16107066Sdanmcd return;
16117066Sdanmcd
16127066Sdanmcd /*
16137066Sdanmcd * Hold the mutexes for both SAs so we don't race another inbound
16147066Sdanmcd * thread. A lock-entry order shouldn't matter, since all other
16157066Sdanmcd * per-ipsa locks are individually held-then-released.
16167066Sdanmcd *
16177066Sdanmcd * Luckily, this has nothing to do with the remote-NAT address,
16187066Sdanmcd * so we don't have to re-scribble the cached-checksum differential.
16197066Sdanmcd */
16207066Sdanmcd mutex_enter(&outbound_peer->ipsa_lock);
16217066Sdanmcd mutex_enter(&assoc->ipsa_lock);
16227066Sdanmcd outbound_peer->ipsa_remote_nat_port = assoc->ipsa_remote_nat_port =
16237066Sdanmcd remote;
16247066Sdanmcd mutex_exit(&assoc->ipsa_lock);
16257066Sdanmcd mutex_exit(&outbound_peer->ipsa_lock);
16267066Sdanmcd IPSA_REFRELE(outbound_peer);
16277066Sdanmcd ESP_BUMP_STAT(espstack, sa_port_renumbers);
16287066Sdanmcd }
16297066Sdanmcd /*
16300Sstevel@tonic-gate * Finish processing of an inbound ESP packet after processing by the
16310Sstevel@tonic-gate * crypto framework.
16320Sstevel@tonic-gate * - Remove the ESP header.
16330Sstevel@tonic-gate * - Send packet back to IP.
16340Sstevel@tonic-gate * If authentication was performed on the packet, this function is called
16350Sstevel@tonic-gate * only if the authentication succeeded.
16360Sstevel@tonic-gate * On success returns B_TRUE, on failure returns B_FALSE and frees the
163711042SErik.Nordmark@Sun.COM * mblk chain data_mp.
16380Sstevel@tonic-gate */
163911042SErik.Nordmark@Sun.COM static mblk_t *
esp_in_done(mblk_t * data_mp,ip_recv_attr_t * ira,ipsec_crypto_t * ic)164011042SErik.Nordmark@Sun.COM esp_in_done(mblk_t *data_mp, ip_recv_attr_t *ira, ipsec_crypto_t *ic)
16410Sstevel@tonic-gate {
16420Sstevel@tonic-gate ipsa_t *assoc;
16430Sstevel@tonic-gate uint_t espstart;
16440Sstevel@tonic-gate uint32_t ivlen = 0;
16450Sstevel@tonic-gate uint_t processed_len;
16460Sstevel@tonic-gate esph_t *esph;
16470Sstevel@tonic-gate kstat_named_t *counter;
16480Sstevel@tonic-gate boolean_t is_natt;
164911042SErik.Nordmark@Sun.COM netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
16503448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
16513448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec;
16520Sstevel@tonic-gate
165311042SErik.Nordmark@Sun.COM assoc = ira->ira_ipsec_esp_sa;
16540Sstevel@tonic-gate ASSERT(assoc != NULL);
16550Sstevel@tonic-gate
16560Sstevel@tonic-gate is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0);
16570Sstevel@tonic-gate
16580Sstevel@tonic-gate /* get the pointer to the ESP header */
16590Sstevel@tonic-gate if (assoc->ipsa_encr_alg == SADB_EALG_NULL) {
16600Sstevel@tonic-gate /* authentication-only ESP */
166111042SErik.Nordmark@Sun.COM espstart = ic->ic_crypto_data.cd_offset;
166211042SErik.Nordmark@Sun.COM processed_len = ic->ic_crypto_data.cd_length;
16630Sstevel@tonic-gate } else {
16640Sstevel@tonic-gate /* encryption present */
16650Sstevel@tonic-gate ivlen = assoc->ipsa_iv_len;
16660Sstevel@tonic-gate if (assoc->ipsa_auth_alg == SADB_AALG_NONE) {
16670Sstevel@tonic-gate /* encryption-only ESP */
166811042SErik.Nordmark@Sun.COM espstart = ic->ic_crypto_data.cd_offset -
16694987Sdanmcd sizeof (esph_t) - assoc->ipsa_iv_len;
167011042SErik.Nordmark@Sun.COM processed_len = ic->ic_crypto_data.cd_length +
16714987Sdanmcd ivlen;
16720Sstevel@tonic-gate } else {
16730Sstevel@tonic-gate /* encryption with authentication */
167411042SErik.Nordmark@Sun.COM espstart = ic->ic_crypto_dual_data.dd_offset1;
167511042SErik.Nordmark@Sun.COM processed_len = ic->ic_crypto_dual_data.dd_len2 +
16760Sstevel@tonic-gate ivlen;
16770Sstevel@tonic-gate }
16780Sstevel@tonic-gate }
16790Sstevel@tonic-gate
16800Sstevel@tonic-gate esph = (esph_t *)(data_mp->b_rptr + espstart);
16810Sstevel@tonic-gate
168210824SMark.Fenwick@Sun.COM if (assoc->ipsa_auth_alg != IPSA_AALG_NONE ||
168310824SMark.Fenwick@Sun.COM (assoc->ipsa_flags & IPSA_F_COMBINED)) {
168410824SMark.Fenwick@Sun.COM /*
168510824SMark.Fenwick@Sun.COM * Authentication passed if we reach this point.
168610824SMark.Fenwick@Sun.COM * Packets with authentication will have the ICV
168710824SMark.Fenwick@Sun.COM * after the crypto data. Adjust b_wptr before
168810824SMark.Fenwick@Sun.COM * making padlen checks.
168910824SMark.Fenwick@Sun.COM */
16903448Sdh155122 ESP_BUMP_STAT(espstack, good_auth);
16910Sstevel@tonic-gate data_mp->b_wptr -= assoc->ipsa_mac_len;
16920Sstevel@tonic-gate
16930Sstevel@tonic-gate /*
16940Sstevel@tonic-gate * Check replay window here!
16950Sstevel@tonic-gate * For right now, assume keysock will set the replay window
16960Sstevel@tonic-gate * size to zero for SAs that have an unspecified sender.
16970Sstevel@tonic-gate * This may change...
16980Sstevel@tonic-gate */
16990Sstevel@tonic-gate
17000Sstevel@tonic-gate if (!sadb_replay_check(assoc, esph->esph_replay)) {
17010Sstevel@tonic-gate /*
17020Sstevel@tonic-gate * Log the event. As of now we print out an event.
17030Sstevel@tonic-gate * Do not print the replay failure number, or else
17040Sstevel@tonic-gate * syslog cannot collate the error messages. Printing
17050Sstevel@tonic-gate * the replay number that failed opens a denial-of-
17060Sstevel@tonic-gate * service attack.
17070Sstevel@tonic-gate */
17080Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0,
17090Sstevel@tonic-gate SL_ERROR | SL_WARN,
17100Sstevel@tonic-gate "Replay failed for ESP spi 0x%x, dst %s.\n",
17110Sstevel@tonic-gate assoc->ipsa_spi, assoc->ipsa_dstaddr,
17123448Sdh155122 assoc->ipsa_addrfam, espstack->ipsecesp_netstack);
17133448Sdh155122 ESP_BUMP_STAT(espstack, replay_failures);
17143448Sdh155122 counter = DROPPER(ipss, ipds_esp_replay);
17150Sstevel@tonic-gate goto drop_and_bail;
17160Sstevel@tonic-gate }
17177066Sdanmcd
171811042SErik.Nordmark@Sun.COM if (is_natt) {
171911042SErik.Nordmark@Sun.COM ASSERT(ira->ira_flags & IRAF_ESP_UDP_PORTS);
172011042SErik.Nordmark@Sun.COM ASSERT(ira->ira_esp_udp_ports != 0);
172111042SErik.Nordmark@Sun.COM esp_port_freshness(ira->ira_esp_udp_ports, assoc);
172211042SErik.Nordmark@Sun.COM }
17230Sstevel@tonic-gate }
17240Sstevel@tonic-gate
17254987Sdanmcd esp_set_usetime(assoc, B_TRUE);
17264987Sdanmcd
17270Sstevel@tonic-gate if (!esp_age_bytes(assoc, processed_len, B_TRUE)) {
17280Sstevel@tonic-gate /* The ipsa has hit hard expiration, LOG and AUDIT. */
17290Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0,
17300Sstevel@tonic-gate SL_ERROR | SL_WARN,
17310Sstevel@tonic-gate "ESP association 0x%x, dst %s had bytes expire.\n",
17323448Sdh155122 assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
17333448Sdh155122 espstack->ipsecesp_netstack);
17343448Sdh155122 ESP_BUMP_STAT(espstack, bytes_expired);
17353448Sdh155122 counter = DROPPER(ipss, ipds_esp_bytes_expire);
17360Sstevel@tonic-gate goto drop_and_bail;
17370Sstevel@tonic-gate }
17380Sstevel@tonic-gate
17390Sstevel@tonic-gate /*
17400Sstevel@tonic-gate * Remove ESP header and padding from packet. I hope the compiler
17410Sstevel@tonic-gate * spews "branch, predict taken" code for this.
17420Sstevel@tonic-gate */
17430Sstevel@tonic-gate
174411042SErik.Nordmark@Sun.COM if (esp_strip_header(data_mp, (ira->ira_flags & IRAF_IS_IPV4),
174511042SErik.Nordmark@Sun.COM ivlen, &counter, espstack)) {
174611042SErik.Nordmark@Sun.COM
174711042SErik.Nordmark@Sun.COM if (is_system_labeled() && assoc->ipsa_tsl != NULL) {
174811042SErik.Nordmark@Sun.COM if (!ip_recv_attr_replace_label(ira, assoc->ipsa_tsl)) {
174911042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
175011042SErik.Nordmark@Sun.COM DROPPER(ipss, ipds_ah_nomem),
175111042SErik.Nordmark@Sun.COM &espstack->esp_dropper);
175211042SErik.Nordmark@Sun.COM BUMP_MIB(ira->ira_ill->ill_ip_mib,
175311042SErik.Nordmark@Sun.COM ipIfStatsInDiscards);
175411042SErik.Nordmark@Sun.COM return (NULL);
175510934Ssommerfeld@sun.com }
175610934Ssommerfeld@sun.com }
17570Sstevel@tonic-gate if (is_natt)
17580Sstevel@tonic-gate return (esp_fix_natt_checksums(data_mp, assoc));
17597885Sdanmcd@sun.com
17607885Sdanmcd@sun.com if (assoc->ipsa_state == IPSA_STATE_IDLE) {
17617885Sdanmcd@sun.com /*
17627885Sdanmcd@sun.com * Cluster buffering case. Tell caller that we're
17637885Sdanmcd@sun.com * handling the packet.
17647885Sdanmcd@sun.com */
176511042SErik.Nordmark@Sun.COM sadb_buf_pkt(assoc, data_mp, ira);
176611042SErik.Nordmark@Sun.COM return (NULL);
17677885Sdanmcd@sun.com }
17687885Sdanmcd@sun.com
176911042SErik.Nordmark@Sun.COM return (data_mp);
17700Sstevel@tonic-gate }
17710Sstevel@tonic-gate
17723448Sdh155122 esp1dbg(espstack, ("esp_in_done: esp_strip_header() failed\n"));
17730Sstevel@tonic-gate drop_and_bail:
17743448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards);
177511042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_TRUE, ira->ira_ill, counter,
17763448Sdh155122 &espstack->esp_dropper);
177711042SErik.Nordmark@Sun.COM BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
177811042SErik.Nordmark@Sun.COM return (NULL);
17790Sstevel@tonic-gate }
17800Sstevel@tonic-gate
17810Sstevel@tonic-gate /*
17820Sstevel@tonic-gate * Called upon failing the inbound ICV check. The message passed as
17830Sstevel@tonic-gate * argument is freed.
17840Sstevel@tonic-gate */
17850Sstevel@tonic-gate static void
esp_log_bad_auth(mblk_t * mp,ip_recv_attr_t * ira)178611042SErik.Nordmark@Sun.COM esp_log_bad_auth(mblk_t *mp, ip_recv_attr_t *ira)
17870Sstevel@tonic-gate {
178811042SErik.Nordmark@Sun.COM ipsa_t *assoc = ira->ira_ipsec_esp_sa;
178911042SErik.Nordmark@Sun.COM netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
17903448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
17913448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec;
17920Sstevel@tonic-gate
17930Sstevel@tonic-gate /*
17940Sstevel@tonic-gate * Log the event. Don't print to the console, block
17950Sstevel@tonic-gate * potential denial-of-service attack.
17960Sstevel@tonic-gate */
17973448Sdh155122 ESP_BUMP_STAT(espstack, bad_auth);
17980Sstevel@tonic-gate
17990Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
18000Sstevel@tonic-gate "ESP Authentication failed for spi 0x%x, dst %s.\n",
18013448Sdh155122 assoc->ipsa_spi, assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
18023448Sdh155122 espstack->ipsecesp_netstack);
18033448Sdh155122
18043448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards);
180511042SErik.Nordmark@Sun.COM ip_drop_packet(mp, B_TRUE, ira->ira_ill,
18063448Sdh155122 DROPPER(ipss, ipds_esp_bad_auth),
18073448Sdh155122 &espstack->esp_dropper);
18080Sstevel@tonic-gate }
18090Sstevel@tonic-gate
18100Sstevel@tonic-gate
18110Sstevel@tonic-gate /*
18120Sstevel@tonic-gate * Invoked for outbound packets after ESP processing. If the packet
18130Sstevel@tonic-gate * also requires AH, performs the AH SA selection and AH processing.
18140Sstevel@tonic-gate * Returns B_TRUE if the AH processing was not needed or if it was
18150Sstevel@tonic-gate * performed successfully. Returns B_FALSE and consumes the passed mblk
18160Sstevel@tonic-gate * if AH processing was required but could not be performed.
181711042SErik.Nordmark@Sun.COM *
181811042SErik.Nordmark@Sun.COM * Returns data_mp unless data_mp was consumed/queued.
18190Sstevel@tonic-gate */
182011042SErik.Nordmark@Sun.COM static mblk_t *
esp_do_outbound_ah(mblk_t * data_mp,ip_xmit_attr_t * ixa)182111042SErik.Nordmark@Sun.COM esp_do_outbound_ah(mblk_t *data_mp, ip_xmit_attr_t *ixa)
18220Sstevel@tonic-gate {
18230Sstevel@tonic-gate ipsec_action_t *ap;
18240Sstevel@tonic-gate
182511042SErik.Nordmark@Sun.COM ap = ixa->ixa_ipsec_action;
18260Sstevel@tonic-gate if (ap == NULL) {
182711042SErik.Nordmark@Sun.COM ipsec_policy_t *pp = ixa->ixa_ipsec_policy;
18280Sstevel@tonic-gate ap = pp->ipsp_act;
18290Sstevel@tonic-gate }
18300Sstevel@tonic-gate
18310Sstevel@tonic-gate if (!ap->ipa_want_ah)
183211042SErik.Nordmark@Sun.COM return (data_mp);
183311042SErik.Nordmark@Sun.COM
183411042SErik.Nordmark@Sun.COM /*
183511042SErik.Nordmark@Sun.COM * Normally the AH SA would have already been put in place
183611042SErik.Nordmark@Sun.COM * but it could have been flushed so we need to look for it.
183711042SErik.Nordmark@Sun.COM */
183811042SErik.Nordmark@Sun.COM if (ixa->ixa_ipsec_ah_sa == NULL) {
183911042SErik.Nordmark@Sun.COM if (!ipsec_outbound_sa(data_mp, ixa, IPPROTO_AH)) {
184011042SErik.Nordmark@Sun.COM sadb_acquire(data_mp, ixa, B_TRUE, B_FALSE);
184111042SErik.Nordmark@Sun.COM return (NULL);
18420Sstevel@tonic-gate }
18430Sstevel@tonic-gate }
184411042SErik.Nordmark@Sun.COM ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
184511042SErik.Nordmark@Sun.COM
184611042SErik.Nordmark@Sun.COM data_mp = ixa->ixa_ipsec_ah_sa->ipsa_output_func(data_mp, ixa);
184711042SErik.Nordmark@Sun.COM return (data_mp);
18480Sstevel@tonic-gate }
18490Sstevel@tonic-gate
18500Sstevel@tonic-gate
18510Sstevel@tonic-gate /*
18520Sstevel@tonic-gate * Kernel crypto framework callback invoked after completion of async
185311042SErik.Nordmark@Sun.COM * crypto requests for outbound packets.
18540Sstevel@tonic-gate */
18550Sstevel@tonic-gate static void
esp_kcf_callback_outbound(void * arg,int status)185611042SErik.Nordmark@Sun.COM esp_kcf_callback_outbound(void *arg, int status)
18570Sstevel@tonic-gate {
185811042SErik.Nordmark@Sun.COM mblk_t *mp = (mblk_t *)arg;
185911042SErik.Nordmark@Sun.COM mblk_t *async_mp;
186011042SErik.Nordmark@Sun.COM netstack_t *ns;
186111042SErik.Nordmark@Sun.COM ipsec_stack_t *ipss;
186211042SErik.Nordmark@Sun.COM ipsecesp_stack_t *espstack;
186311042SErik.Nordmark@Sun.COM mblk_t *data_mp;
186411042SErik.Nordmark@Sun.COM ip_xmit_attr_t ixas;
186511042SErik.Nordmark@Sun.COM ipsec_crypto_t *ic;
186611042SErik.Nordmark@Sun.COM ill_t *ill;
186711042SErik.Nordmark@Sun.COM
186811042SErik.Nordmark@Sun.COM /*
186911042SErik.Nordmark@Sun.COM * First remove the ipsec_crypto_t mblk
187011042SErik.Nordmark@Sun.COM * Note that we need to ipsec_free_crypto_data(mp) once done with ic.
187111042SErik.Nordmark@Sun.COM */
187211042SErik.Nordmark@Sun.COM async_mp = ipsec_remove_crypto_data(mp, &ic);
187311042SErik.Nordmark@Sun.COM ASSERT(async_mp != NULL);
187411042SErik.Nordmark@Sun.COM
187511042SErik.Nordmark@Sun.COM /*
187611042SErik.Nordmark@Sun.COM * Extract the ip_xmit_attr_t from the first mblk.
187711042SErik.Nordmark@Sun.COM * Verifies that the netstack and ill is still around; could
187811042SErik.Nordmark@Sun.COM * have vanished while kEf was doing its work.
187911042SErik.Nordmark@Sun.COM * On succesful return we have a nce_t and the ill/ipst can't
188011042SErik.Nordmark@Sun.COM * disappear until we do the nce_refrele in ixa_cleanup.
188111042SErik.Nordmark@Sun.COM */
188211042SErik.Nordmark@Sun.COM data_mp = async_mp->b_cont;
188311042SErik.Nordmark@Sun.COM async_mp->b_cont = NULL;
188411042SErik.Nordmark@Sun.COM if (!ip_xmit_attr_from_mblk(async_mp, &ixas)) {
188511042SErik.Nordmark@Sun.COM /* Disappeared on us - no ill/ipst for MIB */
188611042SErik.Nordmark@Sun.COM /* We have nowhere to do stats since ixa_ipst could be NULL */
188711042SErik.Nordmark@Sun.COM if (ixas.ixa_nce != NULL) {
188811042SErik.Nordmark@Sun.COM ill = ixas.ixa_nce->nce_ill;
188911042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
189011042SErik.Nordmark@Sun.COM ip_drop_output("ipIfStatsOutDiscards", data_mp, ill);
189111042SErik.Nordmark@Sun.COM }
189211042SErik.Nordmark@Sun.COM freemsg(data_mp);
189311042SErik.Nordmark@Sun.COM goto done;
189411042SErik.Nordmark@Sun.COM }
189511042SErik.Nordmark@Sun.COM ns = ixas.ixa_ipst->ips_netstack;
189611042SErik.Nordmark@Sun.COM espstack = ns->netstack_ipsecesp;
189711042SErik.Nordmark@Sun.COM ipss = ns->netstack_ipsec;
189811042SErik.Nordmark@Sun.COM ill = ixas.ixa_nce->nce_ill;
189911042SErik.Nordmark@Sun.COM
190011042SErik.Nordmark@Sun.COM if (status == CRYPTO_SUCCESS) {
190111042SErik.Nordmark@Sun.COM /*
190211042SErik.Nordmark@Sun.COM * If a ICV was computed, it was stored by the
190311042SErik.Nordmark@Sun.COM * crypto framework at the end of the packet.
190411042SErik.Nordmark@Sun.COM */
190511042SErik.Nordmark@Sun.COM ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
190611042SErik.Nordmark@Sun.COM
190711042SErik.Nordmark@Sun.COM esp_set_usetime(ixas.ixa_ipsec_esp_sa, B_FALSE);
190811042SErik.Nordmark@Sun.COM /* NAT-T packet. */
190911042SErik.Nordmark@Sun.COM if (IPH_HDR_VERSION(ipha) == IP_VERSION &&
191011042SErik.Nordmark@Sun.COM ipha->ipha_protocol == IPPROTO_UDP)
191111042SErik.Nordmark@Sun.COM esp_prepare_udp(ns, data_mp, ipha);
191211042SErik.Nordmark@Sun.COM
191311042SErik.Nordmark@Sun.COM /* do AH processing if needed */
191411042SErik.Nordmark@Sun.COM data_mp = esp_do_outbound_ah(data_mp, &ixas);
191511042SErik.Nordmark@Sun.COM if (data_mp == NULL)
191611042SErik.Nordmark@Sun.COM goto done;
191711042SErik.Nordmark@Sun.COM
191811042SErik.Nordmark@Sun.COM (void) ip_output_post_ipsec(data_mp, &ixas);
191911042SErik.Nordmark@Sun.COM } else {
192011042SErik.Nordmark@Sun.COM /* Outbound shouldn't see invalid MAC */
192111042SErik.Nordmark@Sun.COM ASSERT(status != CRYPTO_INVALID_MAC);
192211042SErik.Nordmark@Sun.COM
192311042SErik.Nordmark@Sun.COM esp1dbg(espstack,
192411042SErik.Nordmark@Sun.COM ("esp_kcf_callback_outbound: crypto failed with 0x%x\n",
192511042SErik.Nordmark@Sun.COM status));
192611042SErik.Nordmark@Sun.COM ESP_BUMP_STAT(espstack, crypto_failures);
192711042SErik.Nordmark@Sun.COM ESP_BUMP_STAT(espstack, out_discards);
192811042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, ill,
192911042SErik.Nordmark@Sun.COM DROPPER(ipss, ipds_esp_crypto_failed),
193011042SErik.Nordmark@Sun.COM &espstack->esp_dropper);
193111042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
193211042SErik.Nordmark@Sun.COM }
193311042SErik.Nordmark@Sun.COM done:
193411042SErik.Nordmark@Sun.COM ixa_cleanup(&ixas);
193511042SErik.Nordmark@Sun.COM (void) ipsec_free_crypto_data(mp);
193611042SErik.Nordmark@Sun.COM }
193711042SErik.Nordmark@Sun.COM
193811042SErik.Nordmark@Sun.COM /*
193911042SErik.Nordmark@Sun.COM * Kernel crypto framework callback invoked after completion of async
194011042SErik.Nordmark@Sun.COM * crypto requests for inbound packets.
194111042SErik.Nordmark@Sun.COM */
194211042SErik.Nordmark@Sun.COM static void
esp_kcf_callback_inbound(void * arg,int status)194311042SErik.Nordmark@Sun.COM esp_kcf_callback_inbound(void *arg, int status)
194411042SErik.Nordmark@Sun.COM {
194511042SErik.Nordmark@Sun.COM mblk_t *mp = (mblk_t *)arg;
194611042SErik.Nordmark@Sun.COM mblk_t *async_mp;
194711042SErik.Nordmark@Sun.COM netstack_t *ns;
19483448Sdh155122 ipsecesp_stack_t *espstack;
19493448Sdh155122 ipsec_stack_t *ipss;
195011042SErik.Nordmark@Sun.COM mblk_t *data_mp;
195111042SErik.Nordmark@Sun.COM ip_recv_attr_t iras;
195211042SErik.Nordmark@Sun.COM ipsec_crypto_t *ic;
195311042SErik.Nordmark@Sun.COM
195411042SErik.Nordmark@Sun.COM /*
195511042SErik.Nordmark@Sun.COM * First remove the ipsec_crypto_t mblk
195611042SErik.Nordmark@Sun.COM * Note that we need to ipsec_free_crypto_data(mp) once done with ic.
195711042SErik.Nordmark@Sun.COM */
195811042SErik.Nordmark@Sun.COM async_mp = ipsec_remove_crypto_data(mp, &ic);
195911042SErik.Nordmark@Sun.COM ASSERT(async_mp != NULL);
19603448Sdh155122
19613448Sdh155122 /*
196211042SErik.Nordmark@Sun.COM * Extract the ip_recv_attr_t from the first mblk.
196311042SErik.Nordmark@Sun.COM * Verifies that the netstack and ill is still around; could
196411042SErik.Nordmark@Sun.COM * have vanished while kEf was doing its work.
19653448Sdh155122 */
196611042SErik.Nordmark@Sun.COM data_mp = async_mp->b_cont;
196711042SErik.Nordmark@Sun.COM async_mp->b_cont = NULL;
196811042SErik.Nordmark@Sun.COM if (!ip_recv_attr_from_mblk(async_mp, &iras)) {
196911042SErik.Nordmark@Sun.COM /* The ill or ip_stack_t disappeared on us */
197011042SErik.Nordmark@Sun.COM ip_drop_input("ip_recv_attr_from_mblk", data_mp, NULL);
197111042SErik.Nordmark@Sun.COM freemsg(data_mp);
197211042SErik.Nordmark@Sun.COM goto done;
19733448Sdh155122 }
19743448Sdh155122
197511042SErik.Nordmark@Sun.COM ns = iras.ira_ill->ill_ipst->ips_netstack;
19763448Sdh155122 espstack = ns->netstack_ipsecesp;
19773448Sdh155122 ipss = ns->netstack_ipsec;
19783448Sdh155122
19790Sstevel@tonic-gate if (status == CRYPTO_SUCCESS) {
198011042SErik.Nordmark@Sun.COM data_mp = esp_in_done(data_mp, &iras, ic);
198111042SErik.Nordmark@Sun.COM if (data_mp == NULL)
198211042SErik.Nordmark@Sun.COM goto done;
198311042SErik.Nordmark@Sun.COM
198411042SErik.Nordmark@Sun.COM /* finish IPsec processing */
198511042SErik.Nordmark@Sun.COM ip_input_post_ipsec(data_mp, &iras);
19860Sstevel@tonic-gate } else if (status == CRYPTO_INVALID_MAC) {
198711042SErik.Nordmark@Sun.COM esp_log_bad_auth(data_mp, &iras);
19880Sstevel@tonic-gate } else {
19893448Sdh155122 esp1dbg(espstack,
19903448Sdh155122 ("esp_kcf_callback: crypto failed with 0x%x\n",
19910Sstevel@tonic-gate status));
19923448Sdh155122 ESP_BUMP_STAT(espstack, crypto_failures);
199311042SErik.Nordmark@Sun.COM IP_ESP_BUMP_STAT(ipss, in_discards);
199411042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_TRUE, iras.ira_ill,
19953448Sdh155122 DROPPER(ipss, ipds_esp_crypto_failed),
19963448Sdh155122 &espstack->esp_dropper);
199711042SErik.Nordmark@Sun.COM BUMP_MIB(iras.ira_ill->ill_ip_mib, ipIfStatsInDiscards);
19980Sstevel@tonic-gate }
199911042SErik.Nordmark@Sun.COM done:
200011042SErik.Nordmark@Sun.COM ira_cleanup(&iras, B_TRUE);
200111042SErik.Nordmark@Sun.COM (void) ipsec_free_crypto_data(mp);
20020Sstevel@tonic-gate }
20030Sstevel@tonic-gate
20040Sstevel@tonic-gate /*
20050Sstevel@tonic-gate * Invoked on crypto framework failure during inbound and outbound processing.
20060Sstevel@tonic-gate */
20070Sstevel@tonic-gate static void
esp_crypto_failed(mblk_t * data_mp,boolean_t is_inbound,int kef_rc,ill_t * ill,ipsecesp_stack_t * espstack)200811042SErik.Nordmark@Sun.COM esp_crypto_failed(mblk_t *data_mp, boolean_t is_inbound, int kef_rc,
200911042SErik.Nordmark@Sun.COM ill_t *ill, ipsecesp_stack_t *espstack)
20100Sstevel@tonic-gate {
20113448Sdh155122 ipsec_stack_t *ipss = espstack->ipsecesp_netstack->netstack_ipsec;
20123448Sdh155122
20133448Sdh155122 esp1dbg(espstack, ("crypto failed for %s ESP with 0x%x\n",
20140Sstevel@tonic-gate is_inbound ? "inbound" : "outbound", kef_rc));
201511042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, is_inbound, ill,
20163448Sdh155122 DROPPER(ipss, ipds_esp_crypto_failed),
20173448Sdh155122 &espstack->esp_dropper);
20183448Sdh155122 ESP_BUMP_STAT(espstack, crypto_failures);
20190Sstevel@tonic-gate if (is_inbound)
20203448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards);
20210Sstevel@tonic-gate else
20223448Sdh155122 ESP_BUMP_STAT(espstack, out_discards);
20230Sstevel@tonic-gate }
20240Sstevel@tonic-gate
202511042SErik.Nordmark@Sun.COM /*
202611042SErik.Nordmark@Sun.COM * A statement-equivalent macro, _cr MUST point to a modifiable
202711042SErik.Nordmark@Sun.COM * crypto_call_req_t.
202811042SErik.Nordmark@Sun.COM */
202911042SErik.Nordmark@Sun.COM #define ESP_INIT_CALLREQ(_cr, _mp, _callback) \
203011042SErik.Nordmark@Sun.COM (_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_ALWAYS_QUEUE; \
203111042SErik.Nordmark@Sun.COM (_cr)->cr_callback_arg = (_mp); \
203211042SErik.Nordmark@Sun.COM (_cr)->cr_callback_func = (_callback)
20330Sstevel@tonic-gate
20340Sstevel@tonic-gate #define ESP_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) { \
20350Sstevel@tonic-gate (mac)->cd_format = CRYPTO_DATA_RAW; \
20360Sstevel@tonic-gate (mac)->cd_offset = 0; \
20370Sstevel@tonic-gate (mac)->cd_length = icvlen; \
20380Sstevel@tonic-gate (mac)->cd_raw.iov_base = (char *)icvbuf; \
20390Sstevel@tonic-gate (mac)->cd_raw.iov_len = icvlen; \
20400Sstevel@tonic-gate }
20410Sstevel@tonic-gate
20420Sstevel@tonic-gate #define ESP_INIT_CRYPTO_DATA(data, mp, off, len) { \
20430Sstevel@tonic-gate if (MBLKL(mp) >= (len) + (off)) { \
20440Sstevel@tonic-gate (data)->cd_format = CRYPTO_DATA_RAW; \
20450Sstevel@tonic-gate (data)->cd_raw.iov_base = (char *)(mp)->b_rptr; \
20460Sstevel@tonic-gate (data)->cd_raw.iov_len = MBLKL(mp); \
20470Sstevel@tonic-gate (data)->cd_offset = off; \
20480Sstevel@tonic-gate } else { \
20490Sstevel@tonic-gate (data)->cd_format = CRYPTO_DATA_MBLK; \
20500Sstevel@tonic-gate (data)->cd_mp = mp; \
20510Sstevel@tonic-gate (data)->cd_offset = off; \
20520Sstevel@tonic-gate } \
20530Sstevel@tonic-gate (data)->cd_length = len; \
20540Sstevel@tonic-gate }
20550Sstevel@tonic-gate
20560Sstevel@tonic-gate #define ESP_INIT_CRYPTO_DUAL_DATA(data, mp, off1, len1, off2, len2) { \
20570Sstevel@tonic-gate (data)->dd_format = CRYPTO_DATA_MBLK; \
20580Sstevel@tonic-gate (data)->dd_mp = mp; \
20590Sstevel@tonic-gate (data)->dd_len1 = len1; \
20600Sstevel@tonic-gate (data)->dd_offset1 = off1; \
20610Sstevel@tonic-gate (data)->dd_len2 = len2; \
20620Sstevel@tonic-gate (data)->dd_offset2 = off2; \
20630Sstevel@tonic-gate }
20640Sstevel@tonic-gate
206511042SErik.Nordmark@Sun.COM /*
206611042SErik.Nordmark@Sun.COM * Returns data_mp if successfully completed the request. Returns
206711042SErik.Nordmark@Sun.COM * NULL if it failed (and increments InDiscards) or if it is pending.
206811042SErik.Nordmark@Sun.COM */
206911042SErik.Nordmark@Sun.COM static mblk_t *
esp_submit_req_inbound(mblk_t * esp_mp,ip_recv_attr_t * ira,ipsa_t * assoc,uint_t esph_offset)207011042SErik.Nordmark@Sun.COM esp_submit_req_inbound(mblk_t *esp_mp, ip_recv_attr_t *ira,
207111042SErik.Nordmark@Sun.COM ipsa_t *assoc, uint_t esph_offset)
20720Sstevel@tonic-gate {
20730Sstevel@tonic-gate uint_t auth_offset, msg_len, auth_len;
207411042SErik.Nordmark@Sun.COM crypto_call_req_t call_req, *callrp;
207511042SErik.Nordmark@Sun.COM mblk_t *mp;
207610824SMark.Fenwick@Sun.COM esph_t *esph_ptr;
207711042SErik.Nordmark@Sun.COM int kef_rc;
20780Sstevel@tonic-gate uint_t icv_len = assoc->ipsa_mac_len;
20790Sstevel@tonic-gate crypto_ctx_template_t auth_ctx_tmpl;
208011042SErik.Nordmark@Sun.COM boolean_t do_auth, do_encr, force;
20810Sstevel@tonic-gate uint_t encr_offset, encr_len;
20820Sstevel@tonic-gate uint_t iv_len = assoc->ipsa_iv_len;
20830Sstevel@tonic-gate crypto_ctx_template_t encr_ctx_tmpl;
208411042SErik.Nordmark@Sun.COM ipsec_crypto_t *ic, icstack;
208510824SMark.Fenwick@Sun.COM uchar_t *iv_ptr;
208611042SErik.Nordmark@Sun.COM netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
208711042SErik.Nordmark@Sun.COM ipsec_stack_t *ipss = ns->netstack_ipsec;
208811042SErik.Nordmark@Sun.COM ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
20893448Sdh155122
20900Sstevel@tonic-gate do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE;
20910Sstevel@tonic-gate do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL;
209211042SErik.Nordmark@Sun.COM force = (assoc->ipsa_flags & IPSA_F_ASYNC);
209311042SErik.Nordmark@Sun.COM
209411042SErik.Nordmark@Sun.COM #ifdef IPSEC_LATENCY_TEST
209511042SErik.Nordmark@Sun.COM kef_rc = CRYPTO_SUCCESS;
209611042SErik.Nordmark@Sun.COM #else
209711042SErik.Nordmark@Sun.COM kef_rc = CRYPTO_FAILED;
209811042SErik.Nordmark@Sun.COM #endif
20990Sstevel@tonic-gate
21000Sstevel@tonic-gate /*
21010Sstevel@tonic-gate * An inbound packet is of the form:
210211042SErik.Nordmark@Sun.COM * [IP,options,ESP,IV,data,ICV,pad]
21030Sstevel@tonic-gate */
210410824SMark.Fenwick@Sun.COM esph_ptr = (esph_t *)(esp_mp->b_rptr + esph_offset);
210510824SMark.Fenwick@Sun.COM iv_ptr = (uchar_t *)(esph_ptr + 1);
210610824SMark.Fenwick@Sun.COM /* Packet length starting at IP header ending after ESP ICV. */
21070Sstevel@tonic-gate msg_len = MBLKL(esp_mp);
21080Sstevel@tonic-gate
210910824SMark.Fenwick@Sun.COM encr_offset = esph_offset + sizeof (esph_t) + iv_len;
211010824SMark.Fenwick@Sun.COM encr_len = msg_len - encr_offset;
211110824SMark.Fenwick@Sun.COM
211210824SMark.Fenwick@Sun.COM /*
211310824SMark.Fenwick@Sun.COM * Counter mode algs need a nonce. This is setup in sadb_common_add().
211410824SMark.Fenwick@Sun.COM * If for some reason we are using a SA which does not have a nonce
211510824SMark.Fenwick@Sun.COM * then we must fail here.
211610824SMark.Fenwick@Sun.COM */
211710824SMark.Fenwick@Sun.COM if ((assoc->ipsa_flags & IPSA_F_COUNTERMODE) &&
211810824SMark.Fenwick@Sun.COM (assoc->ipsa_nonce == NULL)) {
211911042SErik.Nordmark@Sun.COM ip_drop_packet(esp_mp, B_TRUE, ira->ira_ill,
212010824SMark.Fenwick@Sun.COM DROPPER(ipss, ipds_esp_nomem), &espstack->esp_dropper);
212111042SErik.Nordmark@Sun.COM return (NULL);
212211042SErik.Nordmark@Sun.COM }
212311042SErik.Nordmark@Sun.COM
212411042SErik.Nordmark@Sun.COM if (force) {
212511042SErik.Nordmark@Sun.COM /* We are doing asynch; allocate mblks to hold state */
212611042SErik.Nordmark@Sun.COM if ((mp = ip_recv_attr_to_mblk(ira)) == NULL ||
212711042SErik.Nordmark@Sun.COM (mp = ipsec_add_crypto_data(mp, &ic)) == NULL) {
212811042SErik.Nordmark@Sun.COM BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
212911042SErik.Nordmark@Sun.COM ip_drop_input("ipIfStatsInDiscards", esp_mp,
213011042SErik.Nordmark@Sun.COM ira->ira_ill);
213111042SErik.Nordmark@Sun.COM return (NULL);
213211042SErik.Nordmark@Sun.COM }
213311042SErik.Nordmark@Sun.COM linkb(mp, esp_mp);
213411042SErik.Nordmark@Sun.COM callrp = &call_req;
213511042SErik.Nordmark@Sun.COM ESP_INIT_CALLREQ(callrp, mp, esp_kcf_callback_inbound);
213611042SErik.Nordmark@Sun.COM } else {
213711042SErik.Nordmark@Sun.COM /*
213811042SErik.Nordmark@Sun.COM * If we know we are going to do sync then ipsec_crypto_t
213911042SErik.Nordmark@Sun.COM * should be on the stack.
214011042SErik.Nordmark@Sun.COM */
214111042SErik.Nordmark@Sun.COM ic = &icstack;
214211042SErik.Nordmark@Sun.COM bzero(ic, sizeof (*ic));
214311042SErik.Nordmark@Sun.COM callrp = NULL;
214410824SMark.Fenwick@Sun.COM }
214510824SMark.Fenwick@Sun.COM
21460Sstevel@tonic-gate if (do_auth) {
21470Sstevel@tonic-gate /* authentication context template */
21480Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH,
21490Sstevel@tonic-gate auth_ctx_tmpl);
21500Sstevel@tonic-gate
21510Sstevel@tonic-gate /* ICV to be verified */
215211042SErik.Nordmark@Sun.COM ESP_INIT_CRYPTO_MAC(&ic->ic_crypto_mac,
21530Sstevel@tonic-gate icv_len, esp_mp->b_wptr - icv_len);
21540Sstevel@tonic-gate
21550Sstevel@tonic-gate /* authentication starts at the ESP header */
21560Sstevel@tonic-gate auth_offset = esph_offset;
21570Sstevel@tonic-gate auth_len = msg_len - auth_offset - icv_len;
21580Sstevel@tonic-gate if (!do_encr) {
21590Sstevel@tonic-gate /* authentication only */
21600Sstevel@tonic-gate /* initialize input data argument */
216111042SErik.Nordmark@Sun.COM ESP_INIT_CRYPTO_DATA(&ic->ic_crypto_data,
21620Sstevel@tonic-gate esp_mp, auth_offset, auth_len);
21630Sstevel@tonic-gate
21640Sstevel@tonic-gate /* call the crypto framework */
21650Sstevel@tonic-gate kef_rc = crypto_mac_verify(&assoc->ipsa_amech,
216611042SErik.Nordmark@Sun.COM &ic->ic_crypto_data,
21670Sstevel@tonic-gate &assoc->ipsa_kcfauthkey, auth_ctx_tmpl,
216811042SErik.Nordmark@Sun.COM &ic->ic_crypto_mac, callrp);
21690Sstevel@tonic-gate }
21700Sstevel@tonic-gate }
21710Sstevel@tonic-gate
21720Sstevel@tonic-gate if (do_encr) {
21730Sstevel@tonic-gate /* encryption template */
21740Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR,
21750Sstevel@tonic-gate encr_ctx_tmpl);
21760Sstevel@tonic-gate
217710824SMark.Fenwick@Sun.COM /* Call the nonce update function. Also passes in IV */
217810824SMark.Fenwick@Sun.COM (assoc->ipsa_noncefunc)(assoc, (uchar_t *)esph_ptr, encr_len,
217911042SErik.Nordmark@Sun.COM iv_ptr, &ic->ic_cmm, &ic->ic_crypto_data);
21800Sstevel@tonic-gate
21810Sstevel@tonic-gate if (!do_auth) {
21820Sstevel@tonic-gate /* decryption only */
21830Sstevel@tonic-gate /* initialize input data argument */
218411042SErik.Nordmark@Sun.COM ESP_INIT_CRYPTO_DATA(&ic->ic_crypto_data,
21850Sstevel@tonic-gate esp_mp, encr_offset, encr_len);
21860Sstevel@tonic-gate
21870Sstevel@tonic-gate /* call the crypto framework */
218810824SMark.Fenwick@Sun.COM kef_rc = crypto_decrypt((crypto_mechanism_t *)
218911042SErik.Nordmark@Sun.COM &ic->ic_cmm, &ic->ic_crypto_data,
21900Sstevel@tonic-gate &assoc->ipsa_kcfencrkey, encr_ctx_tmpl,
219111042SErik.Nordmark@Sun.COM NULL, callrp);
21920Sstevel@tonic-gate }
21930Sstevel@tonic-gate }
21940Sstevel@tonic-gate
21950Sstevel@tonic-gate if (do_auth && do_encr) {
21960Sstevel@tonic-gate /* dual operation */
21970Sstevel@tonic-gate /* initialize input data argument */
219811042SErik.Nordmark@Sun.COM ESP_INIT_CRYPTO_DUAL_DATA(&ic->ic_crypto_dual_data,
21990Sstevel@tonic-gate esp_mp, auth_offset, auth_len,
22000Sstevel@tonic-gate encr_offset, encr_len - icv_len);
22010Sstevel@tonic-gate
22020Sstevel@tonic-gate /* specify IV */
220311042SErik.Nordmark@Sun.COM ic->ic_crypto_dual_data.dd_miscdata = (char *)iv_ptr;
22040Sstevel@tonic-gate
22050Sstevel@tonic-gate /* call the framework */
22060Sstevel@tonic-gate kef_rc = crypto_mac_verify_decrypt(&assoc->ipsa_amech,
220711042SErik.Nordmark@Sun.COM &assoc->ipsa_emech, &ic->ic_crypto_dual_data,
22080Sstevel@tonic-gate &assoc->ipsa_kcfauthkey, &assoc->ipsa_kcfencrkey,
220911042SErik.Nordmark@Sun.COM auth_ctx_tmpl, encr_ctx_tmpl, &ic->ic_crypto_mac,
221011042SErik.Nordmark@Sun.COM NULL, callrp);
22110Sstevel@tonic-gate }
22120Sstevel@tonic-gate
22130Sstevel@tonic-gate switch (kef_rc) {
22140Sstevel@tonic-gate case CRYPTO_SUCCESS:
22153448Sdh155122 ESP_BUMP_STAT(espstack, crypto_sync);
221611042SErik.Nordmark@Sun.COM esp_mp = esp_in_done(esp_mp, ira, ic);
221711042SErik.Nordmark@Sun.COM if (force) {
221811042SErik.Nordmark@Sun.COM /* Free mp after we are done with ic */
221911042SErik.Nordmark@Sun.COM mp = ipsec_free_crypto_data(mp);
222011042SErik.Nordmark@Sun.COM (void) ip_recv_attr_free_mblk(mp);
222111042SErik.Nordmark@Sun.COM }
222211042SErik.Nordmark@Sun.COM return (esp_mp);
22230Sstevel@tonic-gate case CRYPTO_QUEUED:
222411042SErik.Nordmark@Sun.COM /* esp_kcf_callback_inbound() will be invoked on completion */
22253448Sdh155122 ESP_BUMP_STAT(espstack, crypto_async);
222611042SErik.Nordmark@Sun.COM return (NULL);
22270Sstevel@tonic-gate case CRYPTO_INVALID_MAC:
222811042SErik.Nordmark@Sun.COM if (force) {
222911042SErik.Nordmark@Sun.COM mp = ipsec_free_crypto_data(mp);
223011042SErik.Nordmark@Sun.COM esp_mp = ip_recv_attr_free_mblk(mp);
223111042SErik.Nordmark@Sun.COM }
22323448Sdh155122 ESP_BUMP_STAT(espstack, crypto_sync);
223311042SErik.Nordmark@Sun.COM BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
223411042SErik.Nordmark@Sun.COM esp_log_bad_auth(esp_mp, ira);
223511042SErik.Nordmark@Sun.COM /* esp_mp was passed to ip_drop_packet */
223611042SErik.Nordmark@Sun.COM return (NULL);
22370Sstevel@tonic-gate }
22380Sstevel@tonic-gate
223911216Sdanmcd@sun.com if (force) {
224011216Sdanmcd@sun.com mp = ipsec_free_crypto_data(mp);
224111216Sdanmcd@sun.com esp_mp = ip_recv_attr_free_mblk(mp);
224211216Sdanmcd@sun.com }
224311042SErik.Nordmark@Sun.COM BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
224411042SErik.Nordmark@Sun.COM esp_crypto_failed(esp_mp, B_TRUE, kef_rc, ira->ira_ill, espstack);
224511042SErik.Nordmark@Sun.COM /* esp_mp was passed to ip_drop_packet */
224611042SErik.Nordmark@Sun.COM return (NULL);
22470Sstevel@tonic-gate }
22480Sstevel@tonic-gate
22494987Sdanmcd /*
22504987Sdanmcd * Compute the IP and UDP checksums -- common code for both keepalives and
22514987Sdanmcd * actual ESP-in-UDP packets. Be flexible with multiple mblks because ESP
22524987Sdanmcd * uses mblk-insertion to insert the UDP header.
22534987Sdanmcd * TODO - If there is an easy way to prep a packet for HW checksums, make
22544987Sdanmcd * it happen here.
225511042SErik.Nordmark@Sun.COM * Note that this is used before both before calling ip_output_simple and
225611042SErik.Nordmark@Sun.COM * in the esp datapath. The former could use IXAF_SET_ULP_CKSUM but not the
225711042SErik.Nordmark@Sun.COM * latter.
22584987Sdanmcd */
22594987Sdanmcd static void
esp_prepare_udp(netstack_t * ns,mblk_t * mp,ipha_t * ipha)22604987Sdanmcd esp_prepare_udp(netstack_t *ns, mblk_t *mp, ipha_t *ipha)
22614987Sdanmcd {
22624987Sdanmcd int offset;
22634987Sdanmcd uint32_t cksum;
22644987Sdanmcd uint16_t *arr;
22654987Sdanmcd mblk_t *udpmp = mp;
22666352Swy83408 uint_t hlen = IPH_HDR_LENGTH(ipha);
22674987Sdanmcd
22684987Sdanmcd ASSERT(MBLKL(mp) >= sizeof (ipha_t));
22694987Sdanmcd
22704987Sdanmcd ipha->ipha_hdr_checksum = 0;
22714987Sdanmcd ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
22724987Sdanmcd
22734987Sdanmcd if (ns->netstack_udp->us_do_checksum) {
22744987Sdanmcd ASSERT(MBLKL(udpmp) >= sizeof (udpha_t));
22754987Sdanmcd /* arr points to the IP header. */
22764987Sdanmcd arr = (uint16_t *)ipha;
22774987Sdanmcd IP_STAT(ns->netstack_ip, ip_out_sw_cksum);
227811042SErik.Nordmark@Sun.COM IP_STAT_UPDATE(ns->netstack_ip, ip_out_sw_cksum_bytes,
22796352Swy83408 ntohs(htons(ipha->ipha_length) - hlen));
22804987Sdanmcd /* arr[6-9] are the IP addresses. */
22814987Sdanmcd cksum = IP_UDP_CSUM_COMP + arr[6] + arr[7] + arr[8] + arr[9] +
22826352Swy83408 ntohs(htons(ipha->ipha_length) - hlen);
22836352Swy83408 cksum = IP_CSUM(mp, hlen, cksum);
22846352Swy83408 offset = hlen + UDP_CHECKSUM_OFFSET;
22854987Sdanmcd while (offset >= MBLKL(udpmp)) {
22864987Sdanmcd offset -= MBLKL(udpmp);
22874987Sdanmcd udpmp = udpmp->b_cont;
22884987Sdanmcd }
22894987Sdanmcd /* arr points to the UDP header's checksum field. */
22904987Sdanmcd arr = (uint16_t *)(udpmp->b_rptr + offset);
22914987Sdanmcd *arr = cksum;
22924987Sdanmcd }
22934987Sdanmcd }
22944987Sdanmcd
22954987Sdanmcd /*
22968704Sdanmcd@sun.com * taskq handler so we can send the NAT-T keepalive on a separate thread.
22978704Sdanmcd@sun.com */
22988704Sdanmcd@sun.com static void
actually_send_keepalive(void * arg)22998704Sdanmcd@sun.com actually_send_keepalive(void *arg)
23008704Sdanmcd@sun.com {
230111042SErik.Nordmark@Sun.COM mblk_t *mp = (mblk_t *)arg;
230211042SErik.Nordmark@Sun.COM ip_xmit_attr_t ixas;
230311042SErik.Nordmark@Sun.COM netstack_t *ns;
230411042SErik.Nordmark@Sun.COM netstackid_t stackid;
230511042SErik.Nordmark@Sun.COM
230611042SErik.Nordmark@Sun.COM stackid = (netstackid_t)(uintptr_t)mp->b_prev;
230711042SErik.Nordmark@Sun.COM mp->b_prev = NULL;
230811042SErik.Nordmark@Sun.COM ns = netstack_find_by_stackid(stackid);
230911042SErik.Nordmark@Sun.COM if (ns == NULL) {
231011042SErik.Nordmark@Sun.COM /* Disappeared */
231111042SErik.Nordmark@Sun.COM ip_drop_output("ipIfStatsOutDiscards", mp, NULL);
231211042SErik.Nordmark@Sun.COM freemsg(mp);
23138704Sdanmcd@sun.com return;
23148704Sdanmcd@sun.com }
23158704Sdanmcd@sun.com
231611042SErik.Nordmark@Sun.COM bzero(&ixas, sizeof (ixas));
231711042SErik.Nordmark@Sun.COM ixas.ixa_zoneid = ALL_ZONES;
231811042SErik.Nordmark@Sun.COM ixas.ixa_cred = kcred;
231911042SErik.Nordmark@Sun.COM ixas.ixa_cpid = NOPID;
232011042SErik.Nordmark@Sun.COM ixas.ixa_tsl = NULL;
232111042SErik.Nordmark@Sun.COM ixas.ixa_ipst = ns->netstack_ip;
232211042SErik.Nordmark@Sun.COM /* No ULP checksum; done by esp_prepare_udp */
2323*11681SSowmini.Varadhan@Sun.COM ixas.ixa_flags = (IXAF_IS_IPV4 | IXAF_NO_IPSEC | IXAF_VERIFY_SOURCE);
232411042SErik.Nordmark@Sun.COM
232511042SErik.Nordmark@Sun.COM (void) ip_output_simple(mp, &ixas);
232611042SErik.Nordmark@Sun.COM ixa_cleanup(&ixas);
23278704Sdanmcd@sun.com netstack_rele(ns);
23288704Sdanmcd@sun.com }
23298704Sdanmcd@sun.com
23308704Sdanmcd@sun.com /*
233111042SErik.Nordmark@Sun.COM * Send a one-byte UDP NAT-T keepalive.
23324987Sdanmcd */
23334987Sdanmcd void
ipsecesp_send_keepalive(ipsa_t * assoc)23344987Sdanmcd ipsecesp_send_keepalive(ipsa_t *assoc)
23354987Sdanmcd {
233611042SErik.Nordmark@Sun.COM mblk_t *mp;
233711042SErik.Nordmark@Sun.COM ipha_t *ipha;
233811042SErik.Nordmark@Sun.COM udpha_t *udpha;
233911042SErik.Nordmark@Sun.COM netstack_t *ns = assoc->ipsa_netstack;
23404987Sdanmcd
23417373Ssommerfeld@sun.com ASSERT(MUTEX_NOT_HELD(&assoc->ipsa_lock));
23424987Sdanmcd
23434987Sdanmcd mp = allocb(sizeof (ipha_t) + sizeof (udpha_t) + 1, BPRI_HI);
23444987Sdanmcd if (mp == NULL)
23454987Sdanmcd return;
23464987Sdanmcd ipha = (ipha_t *)mp->b_rptr;
23474987Sdanmcd ipha->ipha_version_and_hdr_length = IP_SIMPLE_HDR_VERSION;
23484987Sdanmcd ipha->ipha_type_of_service = 0;
23494987Sdanmcd ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (udpha_t) + 1);
23504987Sdanmcd /* Use the low-16 of the SPI so we have some clue where it came from. */
23514987Sdanmcd ipha->ipha_ident = *(((uint16_t *)(&assoc->ipsa_spi)) + 1);
23524987Sdanmcd ipha->ipha_fragment_offset_and_flags = 0; /* Too small to fragment! */
23534987Sdanmcd ipha->ipha_ttl = 0xFF;
23544987Sdanmcd ipha->ipha_protocol = IPPROTO_UDP;
23554987Sdanmcd ipha->ipha_hdr_checksum = 0;
23564987Sdanmcd ipha->ipha_src = assoc->ipsa_srcaddr[0];
23574987Sdanmcd ipha->ipha_dst = assoc->ipsa_dstaddr[0];
23584987Sdanmcd udpha = (udpha_t *)(ipha + 1);
23594987Sdanmcd udpha->uha_src_port = (assoc->ipsa_local_nat_port != 0) ?
23604987Sdanmcd assoc->ipsa_local_nat_port : htons(IPPORT_IKE_NATT);
23614987Sdanmcd udpha->uha_dst_port = (assoc->ipsa_remote_nat_port != 0) ?
23624987Sdanmcd assoc->ipsa_remote_nat_port : htons(IPPORT_IKE_NATT);
23634987Sdanmcd udpha->uha_length = htons(sizeof (udpha_t) + 1);
23644987Sdanmcd udpha->uha_checksum = 0;
23654987Sdanmcd mp->b_wptr = (uint8_t *)(udpha + 1);
23664987Sdanmcd *(mp->b_wptr++) = 0xFF;
23674987Sdanmcd
236811042SErik.Nordmark@Sun.COM esp_prepare_udp(ns, mp, ipha);
236911042SErik.Nordmark@Sun.COM
23708704Sdanmcd@sun.com /*
23718704Sdanmcd@sun.com * We're holding an isaf_t bucket lock, so pawn off the actual
23728704Sdanmcd@sun.com * packet transmission to another thread. Just in case syncq
23738704Sdanmcd@sun.com * processing causes a same-bucket packet to be processed.
23748704Sdanmcd@sun.com */
237511042SErik.Nordmark@Sun.COM mp->b_prev = (mblk_t *)(uintptr_t)ns->netstack_stackid;
237611042SErik.Nordmark@Sun.COM
237711042SErik.Nordmark@Sun.COM if (taskq_dispatch(esp_taskq, actually_send_keepalive, mp,
23788704Sdanmcd@sun.com TQ_NOSLEEP) == 0) {
23798704Sdanmcd@sun.com /* Assume no memory if taskq_dispatch() fails. */
238011042SErik.Nordmark@Sun.COM mp->b_prev = NULL;
238111042SErik.Nordmark@Sun.COM ip_drop_packet(mp, B_FALSE, NULL,
238211042SErik.Nordmark@Sun.COM DROPPER(ns->netstack_ipsec, ipds_esp_nomem),
238311042SErik.Nordmark@Sun.COM &ns->netstack_ipsecesp->esp_dropper);
23848704Sdanmcd@sun.com }
23854987Sdanmcd }
23864987Sdanmcd
238711042SErik.Nordmark@Sun.COM /*
238811042SErik.Nordmark@Sun.COM * Returns mp if successfully completed the request. Returns
238911042SErik.Nordmark@Sun.COM * NULL if it failed (and increments InDiscards) or if it is pending.
239011042SErik.Nordmark@Sun.COM */
239111042SErik.Nordmark@Sun.COM static mblk_t *
esp_submit_req_outbound(mblk_t * data_mp,ip_xmit_attr_t * ixa,ipsa_t * assoc,uchar_t * icv_buf,uint_t payload_len)239211042SErik.Nordmark@Sun.COM esp_submit_req_outbound(mblk_t *data_mp, ip_xmit_attr_t *ixa, ipsa_t *assoc,
239311042SErik.Nordmark@Sun.COM uchar_t *icv_buf, uint_t payload_len)
23940Sstevel@tonic-gate {
23950Sstevel@tonic-gate uint_t auth_len;
239611042SErik.Nordmark@Sun.COM crypto_call_req_t call_req, *callrp;
239711042SErik.Nordmark@Sun.COM mblk_t *esp_mp;
239810824SMark.Fenwick@Sun.COM esph_t *esph_ptr;
239911042SErik.Nordmark@Sun.COM mblk_t *mp;
24000Sstevel@tonic-gate int kef_rc = CRYPTO_FAILED;
24010Sstevel@tonic-gate uint_t icv_len = assoc->ipsa_mac_len;
24020Sstevel@tonic-gate crypto_ctx_template_t auth_ctx_tmpl;
240311042SErik.Nordmark@Sun.COM boolean_t do_auth, do_encr, force;
24040Sstevel@tonic-gate uint_t iv_len = assoc->ipsa_iv_len;
24050Sstevel@tonic-gate crypto_ctx_template_t encr_ctx_tmpl;
24060Sstevel@tonic-gate boolean_t is_natt = ((assoc->ipsa_flags & IPSA_F_NATT) != 0);
24070Sstevel@tonic-gate size_t esph_offset = (is_natt ? UDPH_SIZE : 0);
240811042SErik.Nordmark@Sun.COM netstack_t *ns = ixa->ixa_ipst->ips_netstack;
24093448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
241011042SErik.Nordmark@Sun.COM ipsec_crypto_t *ic, icstack;
241111042SErik.Nordmark@Sun.COM uchar_t *iv_ptr;
241211042SErik.Nordmark@Sun.COM crypto_data_t *cd_ptr = NULL;
241311042SErik.Nordmark@Sun.COM ill_t *ill = ixa->ixa_nce->nce_ill;
24143448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec;
24153448Sdh155122
24163448Sdh155122 esp3dbg(espstack, ("esp_submit_req_outbound:%s",
24174987Sdanmcd is_natt ? "natt" : "not natt"));
24180Sstevel@tonic-gate
24190Sstevel@tonic-gate do_encr = assoc->ipsa_encr_alg != SADB_EALG_NULL;
24200Sstevel@tonic-gate do_auth = assoc->ipsa_auth_alg != SADB_AALG_NONE;
242111042SErik.Nordmark@Sun.COM force = (assoc->ipsa_flags & IPSA_F_ASYNC);
242211042SErik.Nordmark@Sun.COM
242311042SErik.Nordmark@Sun.COM #ifdef IPSEC_LATENCY_TEST
242411042SErik.Nordmark@Sun.COM kef_rc = CRYPTO_SUCCESS;
242511042SErik.Nordmark@Sun.COM #else
242611042SErik.Nordmark@Sun.COM kef_rc = CRYPTO_FAILED;
242711042SErik.Nordmark@Sun.COM #endif
24280Sstevel@tonic-gate
24290Sstevel@tonic-gate /*
24300Sstevel@tonic-gate * Outbound IPsec packets are of the form:
243111042SErik.Nordmark@Sun.COM * [IP,options] -> [ESP,IV] -> [data] -> [pad,ICV]
24320Sstevel@tonic-gate * unless it's NATT, then it's
243311042SErik.Nordmark@Sun.COM * [IP,options] -> [udp][ESP,IV] -> [data] -> [pad,ICV]
24340Sstevel@tonic-gate * Get a pointer to the mblk containing the ESP header.
24350Sstevel@tonic-gate */
243611042SErik.Nordmark@Sun.COM ASSERT(data_mp->b_cont != NULL);
243711042SErik.Nordmark@Sun.COM esp_mp = data_mp->b_cont;
243810824SMark.Fenwick@Sun.COM esph_ptr = (esph_t *)(esp_mp->b_rptr + esph_offset);
243910824SMark.Fenwick@Sun.COM iv_ptr = (uchar_t *)(esph_ptr + 1);
244010824SMark.Fenwick@Sun.COM
244110824SMark.Fenwick@Sun.COM /*
244210824SMark.Fenwick@Sun.COM * Combined mode algs need a nonce. This is setup in sadb_common_add().
244310824SMark.Fenwick@Sun.COM * If for some reason we are using a SA which does not have a nonce
244410824SMark.Fenwick@Sun.COM * then we must fail here.
244510824SMark.Fenwick@Sun.COM */
244610824SMark.Fenwick@Sun.COM if ((assoc->ipsa_flags & IPSA_F_COUNTERMODE) &&
244710824SMark.Fenwick@Sun.COM (assoc->ipsa_nonce == NULL)) {
244811042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, NULL,
244910824SMark.Fenwick@Sun.COM DROPPER(ipss, ipds_esp_nomem), &espstack->esp_dropper);
245011042SErik.Nordmark@Sun.COM return (NULL);
245110824SMark.Fenwick@Sun.COM }
24520Sstevel@tonic-gate
245311042SErik.Nordmark@Sun.COM if (force) {
245411042SErik.Nordmark@Sun.COM /* We are doing asynch; allocate mblks to hold state */
245511042SErik.Nordmark@Sun.COM if ((mp = ip_xmit_attr_to_mblk(ixa)) == NULL ||
245611042SErik.Nordmark@Sun.COM (mp = ipsec_add_crypto_data(mp, &ic)) == NULL) {
245711042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
245811042SErik.Nordmark@Sun.COM ip_drop_output("ipIfStatsOutDiscards", data_mp, ill);
245911042SErik.Nordmark@Sun.COM freemsg(data_mp);
246011042SErik.Nordmark@Sun.COM return (NULL);
246111042SErik.Nordmark@Sun.COM }
246211042SErik.Nordmark@Sun.COM
246311042SErik.Nordmark@Sun.COM linkb(mp, data_mp);
246411042SErik.Nordmark@Sun.COM callrp = &call_req;
246511042SErik.Nordmark@Sun.COM ESP_INIT_CALLREQ(callrp, mp, esp_kcf_callback_outbound);
246611042SErik.Nordmark@Sun.COM } else {
246711042SErik.Nordmark@Sun.COM /*
246811042SErik.Nordmark@Sun.COM * If we know we are going to do sync then ipsec_crypto_t
246911042SErik.Nordmark@Sun.COM * should be on the stack.
247011042SErik.Nordmark@Sun.COM */
247111042SErik.Nordmark@Sun.COM ic = &icstack;
247211042SErik.Nordmark@Sun.COM bzero(ic, sizeof (*ic));
247311042SErik.Nordmark@Sun.COM callrp = NULL;
247411042SErik.Nordmark@Sun.COM }
247511042SErik.Nordmark@Sun.COM
24760Sstevel@tonic-gate
24770Sstevel@tonic-gate if (do_auth) {
24780Sstevel@tonic-gate /* authentication context template */
24790Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH,
24800Sstevel@tonic-gate auth_ctx_tmpl);
24810Sstevel@tonic-gate
24820Sstevel@tonic-gate /* where to store the computed mac */
248311042SErik.Nordmark@Sun.COM ESP_INIT_CRYPTO_MAC(&ic->ic_crypto_mac,
24840Sstevel@tonic-gate icv_len, icv_buf);
24850Sstevel@tonic-gate
24860Sstevel@tonic-gate /* authentication starts at the ESP header */
2487134Sdanmcd auth_len = payload_len + iv_len + sizeof (esph_t);
24880Sstevel@tonic-gate if (!do_encr) {
24890Sstevel@tonic-gate /* authentication only */
24900Sstevel@tonic-gate /* initialize input data argument */
249111042SErik.Nordmark@Sun.COM ESP_INIT_CRYPTO_DATA(&ic->ic_crypto_data,
24920Sstevel@tonic-gate esp_mp, esph_offset, auth_len);
24930Sstevel@tonic-gate
24940Sstevel@tonic-gate /* call the crypto framework */
24950Sstevel@tonic-gate kef_rc = crypto_mac(&assoc->ipsa_amech,
249611042SErik.Nordmark@Sun.COM &ic->ic_crypto_data,
24970Sstevel@tonic-gate &assoc->ipsa_kcfauthkey, auth_ctx_tmpl,
249811042SErik.Nordmark@Sun.COM &ic->ic_crypto_mac, callrp);
24990Sstevel@tonic-gate }
25000Sstevel@tonic-gate }
25010Sstevel@tonic-gate
25020Sstevel@tonic-gate if (do_encr) {
25030Sstevel@tonic-gate /* encryption context template */
25040Sstevel@tonic-gate IPSEC_CTX_TMPL(assoc, ipsa_encrtmpl, IPSEC_ALG_ENCR,
25050Sstevel@tonic-gate encr_ctx_tmpl);
250610824SMark.Fenwick@Sun.COM /* Call the nonce update function. */
250710824SMark.Fenwick@Sun.COM (assoc->ipsa_noncefunc)(assoc, (uchar_t *)esph_ptr, payload_len,
250811042SErik.Nordmark@Sun.COM iv_ptr, &ic->ic_cmm, &ic->ic_crypto_data);
25090Sstevel@tonic-gate
25100Sstevel@tonic-gate if (!do_auth) {
25110Sstevel@tonic-gate /* encryption only, skip mblk that contains ESP hdr */
25120Sstevel@tonic-gate /* initialize input data argument */
251311042SErik.Nordmark@Sun.COM ESP_INIT_CRYPTO_DATA(&ic->ic_crypto_data,
251411042SErik.Nordmark@Sun.COM esp_mp->b_cont, 0, payload_len);
251510824SMark.Fenwick@Sun.COM
251610824SMark.Fenwick@Sun.COM /*
251710824SMark.Fenwick@Sun.COM * For combined mode ciphers, the ciphertext is the same
251810824SMark.Fenwick@Sun.COM * size as the clear text, the ICV should follow the
251910824SMark.Fenwick@Sun.COM * ciphertext. To convince the kcf to allow in-line
252010824SMark.Fenwick@Sun.COM * encryption, with an ICV, use ipsec_out_crypto_mac
252110824SMark.Fenwick@Sun.COM * to point to the same buffer as the data. The calling
252210824SMark.Fenwick@Sun.COM * function need to ensure the buffer is large enough to
252310824SMark.Fenwick@Sun.COM * include the ICV.
252410824SMark.Fenwick@Sun.COM *
252510824SMark.Fenwick@Sun.COM * The IV is already written to the packet buffer, the
252610824SMark.Fenwick@Sun.COM * nonce setup function copied it to the params struct
252710824SMark.Fenwick@Sun.COM * for the cipher to use.
252810824SMark.Fenwick@Sun.COM */
252910824SMark.Fenwick@Sun.COM if (assoc->ipsa_flags & IPSA_F_COMBINED) {
253011042SErik.Nordmark@Sun.COM bcopy(&ic->ic_crypto_data,
253111042SErik.Nordmark@Sun.COM &ic->ic_crypto_mac,
253210824SMark.Fenwick@Sun.COM sizeof (crypto_data_t));
253311042SErik.Nordmark@Sun.COM ic->ic_crypto_mac.cd_length =
253410824SMark.Fenwick@Sun.COM payload_len + icv_len;
253511042SErik.Nordmark@Sun.COM cd_ptr = &ic->ic_crypto_mac;
253610824SMark.Fenwick@Sun.COM }
25370Sstevel@tonic-gate
25380Sstevel@tonic-gate /* call the crypto framework */
253910824SMark.Fenwick@Sun.COM kef_rc = crypto_encrypt((crypto_mechanism_t *)
254011042SErik.Nordmark@Sun.COM &ic->ic_cmm, &ic->ic_crypto_data,
25410Sstevel@tonic-gate &assoc->ipsa_kcfencrkey, encr_ctx_tmpl,
254211042SErik.Nordmark@Sun.COM cd_ptr, callrp);
254310824SMark.Fenwick@Sun.COM
25440Sstevel@tonic-gate }
25450Sstevel@tonic-gate }
25460Sstevel@tonic-gate
25470Sstevel@tonic-gate if (do_auth && do_encr) {
25480Sstevel@tonic-gate /*
25490Sstevel@tonic-gate * Encryption and authentication:
25500Sstevel@tonic-gate * Pass the pointer to the mblk chain starting at the ESP
25510Sstevel@tonic-gate * header to the framework. Skip the ESP header mblk
25520Sstevel@tonic-gate * for encryption, which is reflected by an encryption
25530Sstevel@tonic-gate * offset equal to the length of that mblk. Start
25540Sstevel@tonic-gate * the authentication at the ESP header, i.e. use an
25550Sstevel@tonic-gate * authentication offset of zero.
25560Sstevel@tonic-gate */
255711042SErik.Nordmark@Sun.COM ESP_INIT_CRYPTO_DUAL_DATA(&ic->ic_crypto_dual_data,
25580Sstevel@tonic-gate esp_mp, MBLKL(esp_mp), payload_len, esph_offset, auth_len);
25590Sstevel@tonic-gate
25600Sstevel@tonic-gate /* specify IV */
256111042SErik.Nordmark@Sun.COM ic->ic_crypto_dual_data.dd_miscdata = (char *)iv_ptr;
25620Sstevel@tonic-gate
25630Sstevel@tonic-gate /* call the framework */
25640Sstevel@tonic-gate kef_rc = crypto_encrypt_mac(&assoc->ipsa_emech,
25650Sstevel@tonic-gate &assoc->ipsa_amech, NULL,
25660Sstevel@tonic-gate &assoc->ipsa_kcfencrkey, &assoc->ipsa_kcfauthkey,
25670Sstevel@tonic-gate encr_ctx_tmpl, auth_ctx_tmpl,
256811042SErik.Nordmark@Sun.COM &ic->ic_crypto_dual_data,
256911042SErik.Nordmark@Sun.COM &ic->ic_crypto_mac, callrp);
25700Sstevel@tonic-gate }
25710Sstevel@tonic-gate
25720Sstevel@tonic-gate switch (kef_rc) {
25730Sstevel@tonic-gate case CRYPTO_SUCCESS:
25743448Sdh155122 ESP_BUMP_STAT(espstack, crypto_sync);
25754987Sdanmcd esp_set_usetime(assoc, B_FALSE);
257611042SErik.Nordmark@Sun.COM if (force) {
257711042SErik.Nordmark@Sun.COM mp = ipsec_free_crypto_data(mp);
257811042SErik.Nordmark@Sun.COM data_mp = ip_xmit_attr_free_mblk(mp);
257911042SErik.Nordmark@Sun.COM }
25804987Sdanmcd if (is_natt)
258111042SErik.Nordmark@Sun.COM esp_prepare_udp(ns, data_mp, (ipha_t *)data_mp->b_rptr);
258211042SErik.Nordmark@Sun.COM return (data_mp);
25830Sstevel@tonic-gate case CRYPTO_QUEUED:
258411042SErik.Nordmark@Sun.COM /* esp_kcf_callback_outbound() will be invoked on completion */
25853448Sdh155122 ESP_BUMP_STAT(espstack, crypto_async);
258611042SErik.Nordmark@Sun.COM return (NULL);
25870Sstevel@tonic-gate }
25880Sstevel@tonic-gate
258911042SErik.Nordmark@Sun.COM if (force) {
259011042SErik.Nordmark@Sun.COM mp = ipsec_free_crypto_data(mp);
259111042SErik.Nordmark@Sun.COM data_mp = ip_xmit_attr_free_mblk(mp);
259211042SErik.Nordmark@Sun.COM }
259311042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
259411042SErik.Nordmark@Sun.COM esp_crypto_failed(data_mp, B_FALSE, kef_rc, NULL, espstack);
259511042SErik.Nordmark@Sun.COM /* data_mp was passed to ip_drop_packet */
259611042SErik.Nordmark@Sun.COM return (NULL);
25970Sstevel@tonic-gate }
25980Sstevel@tonic-gate
25990Sstevel@tonic-gate /*
26000Sstevel@tonic-gate * Handle outbound IPsec processing for IPv4 and IPv6
260111042SErik.Nordmark@Sun.COM *
260211042SErik.Nordmark@Sun.COM * Returns data_mp if successfully completed the request. Returns
260311042SErik.Nordmark@Sun.COM * NULL if it failed (and increments InDiscards) or if it is pending.
26040Sstevel@tonic-gate */
260511042SErik.Nordmark@Sun.COM static mblk_t *
esp_outbound(mblk_t * data_mp,ip_xmit_attr_t * ixa)260611042SErik.Nordmark@Sun.COM esp_outbound(mblk_t *data_mp, ip_xmit_attr_t *ixa)
26070Sstevel@tonic-gate {
260811042SErik.Nordmark@Sun.COM mblk_t *espmp, *tailmp;
26090Sstevel@tonic-gate ipha_t *ipha;
26100Sstevel@tonic-gate ip6_t *ip6h;
261110824SMark.Fenwick@Sun.COM esph_t *esph_ptr, *iv_ptr;
26120Sstevel@tonic-gate uint_t af;
26130Sstevel@tonic-gate uint8_t *nhp;
26140Sstevel@tonic-gate uintptr_t divpoint, datalen, adj, padlen, i, alloclen;
26150Sstevel@tonic-gate uintptr_t esplen = sizeof (esph_t);
26160Sstevel@tonic-gate uint8_t protocol;
26170Sstevel@tonic-gate ipsa_t *assoc;
261810824SMark.Fenwick@Sun.COM uint_t iv_len, block_size, mac_len = 0;
26190Sstevel@tonic-gate uchar_t *icv_buf;
26200Sstevel@tonic-gate udpha_t *udpha;
26210Sstevel@tonic-gate boolean_t is_natt = B_FALSE;
262211042SErik.Nordmark@Sun.COM netstack_t *ns = ixa->ixa_ipst->ips_netstack;
262311042SErik.Nordmark@Sun.COM ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
262411042SErik.Nordmark@Sun.COM ipsec_stack_t *ipss = ns->netstack_ipsec;
262511042SErik.Nordmark@Sun.COM ill_t *ill = ixa->ixa_nce->nce_ill;
262611042SErik.Nordmark@Sun.COM boolean_t need_refrele = B_FALSE;
26273448Sdh155122
26283448Sdh155122 ESP_BUMP_STAT(espstack, out_requests);
26293448Sdh155122
26300Sstevel@tonic-gate /*
26310Sstevel@tonic-gate * <sigh> We have to copy the message here, because TCP (for example)
26320Sstevel@tonic-gate * keeps a dupb() of the message lying around for retransmission.
26330Sstevel@tonic-gate * Since ESP changes the whole of the datagram, we have to create our
26340Sstevel@tonic-gate * own copy lest we clobber TCP's data. Since we have to copy anyway,
26350Sstevel@tonic-gate * we might as well make use of msgpullup() and get the mblk into one
26360Sstevel@tonic-gate * contiguous piece!
26370Sstevel@tonic-gate */
263811042SErik.Nordmark@Sun.COM tailmp = msgpullup(data_mp, -1);
263911042SErik.Nordmark@Sun.COM if (tailmp == NULL) {
26400Sstevel@tonic-gate esp0dbg(("esp_outbound: msgpullup() failed, "
26410Sstevel@tonic-gate "dropping packet.\n"));
264211042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, ill,
26433448Sdh155122 DROPPER(ipss, ipds_esp_nomem),
26443448Sdh155122 &espstack->esp_dropper);
264511042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
264611042SErik.Nordmark@Sun.COM return (NULL);
26470Sstevel@tonic-gate }
264811042SErik.Nordmark@Sun.COM freemsg(data_mp);
264911042SErik.Nordmark@Sun.COM data_mp = tailmp;
265011042SErik.Nordmark@Sun.COM
265111042SErik.Nordmark@Sun.COM assoc = ixa->ixa_ipsec_esp_sa;
265210934Ssommerfeld@sun.com ASSERT(assoc != NULL);
265310934Ssommerfeld@sun.com
265410934Ssommerfeld@sun.com /*
265510934Ssommerfeld@sun.com * Get the outer IP header in shape to escape this system..
265610934Ssommerfeld@sun.com */
265711042SErik.Nordmark@Sun.COM if (is_system_labeled() && (assoc->ipsa_otsl != NULL)) {
265811042SErik.Nordmark@Sun.COM /*
265911042SErik.Nordmark@Sun.COM * Need to update packet with any CIPSO option and update
266011042SErik.Nordmark@Sun.COM * ixa_tsl to capture the new label.
266111042SErik.Nordmark@Sun.COM * We allocate a separate ixa for that purpose.
266211042SErik.Nordmark@Sun.COM */
266311042SErik.Nordmark@Sun.COM ixa = ip_xmit_attr_duplicate(ixa);
266411042SErik.Nordmark@Sun.COM if (ixa == NULL) {
266511042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, ill,
266611042SErik.Nordmark@Sun.COM DROPPER(ipss, ipds_esp_nomem),
266710934Ssommerfeld@sun.com &espstack->esp_dropper);
266811042SErik.Nordmark@Sun.COM return (NULL);
266910934Ssommerfeld@sun.com }
267011042SErik.Nordmark@Sun.COM need_refrele = B_TRUE;
267111042SErik.Nordmark@Sun.COM
267211042SErik.Nordmark@Sun.COM label_hold(assoc->ipsa_otsl);
267311042SErik.Nordmark@Sun.COM ip_xmit_attr_replace_tsl(ixa, assoc->ipsa_otsl);
267411042SErik.Nordmark@Sun.COM
267511042SErik.Nordmark@Sun.COM data_mp = sadb_whack_label(data_mp, assoc, ixa,
267611042SErik.Nordmark@Sun.COM DROPPER(ipss, ipds_esp_nomem), &espstack->esp_dropper);
267711042SErik.Nordmark@Sun.COM if (data_mp == NULL) {
267811042SErik.Nordmark@Sun.COM /* Packet dropped by sadb_whack_label */
267911042SErik.Nordmark@Sun.COM ixa_refrele(ixa);
268011042SErik.Nordmark@Sun.COM return (NULL);
268111042SErik.Nordmark@Sun.COM }
268210934Ssommerfeld@sun.com }
268310934Ssommerfeld@sun.com
26840Sstevel@tonic-gate /*
26850Sstevel@tonic-gate * Reality check....
26860Sstevel@tonic-gate */
26870Sstevel@tonic-gate ipha = (ipha_t *)data_mp->b_rptr; /* So we can call esp_acquire(). */
26880Sstevel@tonic-gate
268911042SErik.Nordmark@Sun.COM if (ixa->ixa_flags & IXAF_IS_IPV4) {
269011042SErik.Nordmark@Sun.COM ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
269111042SErik.Nordmark@Sun.COM
26920Sstevel@tonic-gate af = AF_INET;
26930Sstevel@tonic-gate divpoint = IPH_HDR_LENGTH(ipha);
26940Sstevel@tonic-gate datalen = ntohs(ipha->ipha_length) - divpoint;
26950Sstevel@tonic-gate nhp = (uint8_t *)&ipha->ipha_protocol;
26960Sstevel@tonic-gate } else {
269711042SErik.Nordmark@Sun.COM ip_pkt_t ipp;
269811042SErik.Nordmark@Sun.COM
269911042SErik.Nordmark@Sun.COM ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
27000Sstevel@tonic-gate
27010Sstevel@tonic-gate af = AF_INET6;
27020Sstevel@tonic-gate ip6h = (ip6_t *)ipha;
27030Sstevel@tonic-gate bzero(&ipp, sizeof (ipp));
270411042SErik.Nordmark@Sun.COM divpoint = ip_find_hdr_v6(data_mp, ip6h, B_FALSE, &ipp, NULL);
27050Sstevel@tonic-gate if (ipp.ipp_dstopts != NULL &&
27060Sstevel@tonic-gate ipp.ipp_dstopts->ip6d_nxt != IPPROTO_ROUTING) {
27070Sstevel@tonic-gate /*
27080Sstevel@tonic-gate * Destination options are tricky. If we get in here,
27090Sstevel@tonic-gate * then we have a terminal header following the
27100Sstevel@tonic-gate * destination options. We need to adjust backwards
27110Sstevel@tonic-gate * so we insert ESP BEFORE the destination options
27120Sstevel@tonic-gate * bag. (So that the dstopts get encrypted!)
27130Sstevel@tonic-gate *
27140Sstevel@tonic-gate * Since this is for outbound packets only, we know
27150Sstevel@tonic-gate * that non-terminal destination options only precede
27160Sstevel@tonic-gate * routing headers.
27170Sstevel@tonic-gate */
27180Sstevel@tonic-gate divpoint -= ipp.ipp_dstoptslen;
27190Sstevel@tonic-gate }
27200Sstevel@tonic-gate datalen = ntohs(ip6h->ip6_plen) + sizeof (ip6_t) - divpoint;
27210Sstevel@tonic-gate
27220Sstevel@tonic-gate if (ipp.ipp_rthdr != NULL) {
27230Sstevel@tonic-gate nhp = &ipp.ipp_rthdr->ip6r_nxt;
27240Sstevel@tonic-gate } else if (ipp.ipp_hopopts != NULL) {
27250Sstevel@tonic-gate nhp = &ipp.ipp_hopopts->ip6h_nxt;
27260Sstevel@tonic-gate } else {
27270Sstevel@tonic-gate ASSERT(divpoint == sizeof (ip6_t));
27280Sstevel@tonic-gate /* It's probably IP + ESP. */
27290Sstevel@tonic-gate nhp = &ip6h->ip6_nxt;
27300Sstevel@tonic-gate }
27310Sstevel@tonic-gate }
27320Sstevel@tonic-gate
273310824SMark.Fenwick@Sun.COM mac_len = assoc->ipsa_mac_len;
27340Sstevel@tonic-gate
27350Sstevel@tonic-gate if (assoc->ipsa_flags & IPSA_F_NATT) {
273610934Ssommerfeld@sun.com /* wedge in UDP header */
27370Sstevel@tonic-gate is_natt = B_TRUE;
27380Sstevel@tonic-gate esplen += UDPH_SIZE;
27390Sstevel@tonic-gate }
27400Sstevel@tonic-gate
27410Sstevel@tonic-gate /*
27420Sstevel@tonic-gate * Set up ESP header and encryption padding for ENCR PI request.
27430Sstevel@tonic-gate */
27440Sstevel@tonic-gate
27453192Sdanmcd /* Determine the padding length. Pad to 4-bytes for no-encryption. */
27460Sstevel@tonic-gate if (assoc->ipsa_encr_alg != SADB_EALG_NULL) {
27473192Sdanmcd iv_len = assoc->ipsa_iv_len;
274810824SMark.Fenwick@Sun.COM block_size = assoc->ipsa_datalen;
27493192Sdanmcd
27503192Sdanmcd /*
275110824SMark.Fenwick@Sun.COM * Pad the data to the length of the cipher block size.
27523192Sdanmcd * Include the two additional bytes (hence the - 2) for the
27533192Sdanmcd * padding length and the next header. Take this into account
27543192Sdanmcd * when calculating the actual length of the padding.
27553192Sdanmcd */
27563192Sdanmcd ASSERT(ISP2(iv_len));
275710824SMark.Fenwick@Sun.COM padlen = ((unsigned)(block_size - datalen - 2)) &
275810824SMark.Fenwick@Sun.COM (block_size - 1);
27590Sstevel@tonic-gate } else {
27603192Sdanmcd iv_len = 0;
27613192Sdanmcd padlen = ((unsigned)(sizeof (uint32_t) - datalen - 2)) &
27623192Sdanmcd (sizeof (uint32_t) - 1);
27630Sstevel@tonic-gate }
27640Sstevel@tonic-gate
27650Sstevel@tonic-gate /* Allocate ESP header and IV. */
27660Sstevel@tonic-gate esplen += iv_len;
27670Sstevel@tonic-gate
27680Sstevel@tonic-gate /*
27690Sstevel@tonic-gate * Update association byte-count lifetimes. Don't forget to take
27700Sstevel@tonic-gate * into account the padding length and next-header (hence the + 2).
2771134Sdanmcd *
27720Sstevel@tonic-gate * Use the amount of data fed into the "encryption algorithm". This
27730Sstevel@tonic-gate * is the IV, the data length, the padding length, and the final two
27740Sstevel@tonic-gate * bytes (padlen, and next-header).
27750Sstevel@tonic-gate *
27760Sstevel@tonic-gate */
27770Sstevel@tonic-gate
2778134Sdanmcd if (!esp_age_bytes(assoc, datalen + padlen + iv_len + 2, B_FALSE)) {
277911042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, ill,
27803448Sdh155122 DROPPER(ipss, ipds_esp_bytes_expire),
27813448Sdh155122 &espstack->esp_dropper);
278211042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
278311042SErik.Nordmark@Sun.COM if (need_refrele)
278411042SErik.Nordmark@Sun.COM ixa_refrele(ixa);
278511042SErik.Nordmark@Sun.COM return (NULL);
27860Sstevel@tonic-gate }
27870Sstevel@tonic-gate
27880Sstevel@tonic-gate espmp = allocb(esplen, BPRI_HI);
27890Sstevel@tonic-gate if (espmp == NULL) {
27903448Sdh155122 ESP_BUMP_STAT(espstack, out_discards);
27913448Sdh155122 esp1dbg(espstack, ("esp_outbound: can't allocate espmp.\n"));
279211042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, ill,
27933448Sdh155122 DROPPER(ipss, ipds_esp_nomem),
27943448Sdh155122 &espstack->esp_dropper);
279511042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
279611042SErik.Nordmark@Sun.COM if (need_refrele)
279711042SErik.Nordmark@Sun.COM ixa_refrele(ixa);
279811042SErik.Nordmark@Sun.COM return (NULL);
27990Sstevel@tonic-gate }
28000Sstevel@tonic-gate espmp->b_wptr += esplen;
280110824SMark.Fenwick@Sun.COM esph_ptr = (esph_t *)espmp->b_rptr;
28020Sstevel@tonic-gate
28030Sstevel@tonic-gate if (is_natt) {
28043448Sdh155122 esp3dbg(espstack, ("esp_outbound: NATT"));
28050Sstevel@tonic-gate
28060Sstevel@tonic-gate udpha = (udpha_t *)espmp->b_rptr;
28074987Sdanmcd udpha->uha_src_port = (assoc->ipsa_local_nat_port != 0) ?
28084987Sdanmcd assoc->ipsa_local_nat_port : htons(IPPORT_IKE_NATT);
28094987Sdanmcd udpha->uha_dst_port = (assoc->ipsa_remote_nat_port != 0) ?
28104987Sdanmcd assoc->ipsa_remote_nat_port : htons(IPPORT_IKE_NATT);
28110Sstevel@tonic-gate /*
28124987Sdanmcd * Set the checksum to 0, so that the esp_prepare_udp() call
28130Sstevel@tonic-gate * can do the right thing.
28140Sstevel@tonic-gate */
28150Sstevel@tonic-gate udpha->uha_checksum = 0;
281610824SMark.Fenwick@Sun.COM esph_ptr = (esph_t *)(udpha + 1);
28170Sstevel@tonic-gate }
28180Sstevel@tonic-gate
281910824SMark.Fenwick@Sun.COM esph_ptr->esph_spi = assoc->ipsa_spi;
282010824SMark.Fenwick@Sun.COM
282110824SMark.Fenwick@Sun.COM esph_ptr->esph_replay = htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1));
282210824SMark.Fenwick@Sun.COM if (esph_ptr->esph_replay == 0 && assoc->ipsa_replay_wsize != 0) {
28230Sstevel@tonic-gate /*
28240Sstevel@tonic-gate * XXX We have replay counter wrapping.
28250Sstevel@tonic-gate * We probably want to nuke this SA (and its peer).
28260Sstevel@tonic-gate */
28270Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, 0,
28280Sstevel@tonic-gate SL_ERROR | SL_CONSOLE | SL_WARN,
28290Sstevel@tonic-gate "Outbound ESP SA (0x%x, %s) has wrapped sequence.\n",
283010824SMark.Fenwick@Sun.COM esph_ptr->esph_spi, assoc->ipsa_dstaddr, af,
28313448Sdh155122 espstack->ipsecesp_netstack);
28323448Sdh155122
28333448Sdh155122 ESP_BUMP_STAT(espstack, out_discards);
28340Sstevel@tonic-gate sadb_replay_delete(assoc);
283511042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, ill,
28363448Sdh155122 DROPPER(ipss, ipds_esp_replay),
28373448Sdh155122 &espstack->esp_dropper);
283811042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
283911042SErik.Nordmark@Sun.COM if (need_refrele)
284011042SErik.Nordmark@Sun.COM ixa_refrele(ixa);
284111042SErik.Nordmark@Sun.COM return (NULL);
28420Sstevel@tonic-gate }
28430Sstevel@tonic-gate
284410824SMark.Fenwick@Sun.COM iv_ptr = (esph_ptr + 1);
28450Sstevel@tonic-gate /*
284610824SMark.Fenwick@Sun.COM * iv_ptr points to the mblk which will contain the IV once we have
284710824SMark.Fenwick@Sun.COM * written it there. This mblk will be part of a mblk chain that
284810824SMark.Fenwick@Sun.COM * will make up the packet.
284910824SMark.Fenwick@Sun.COM *
285010824SMark.Fenwick@Sun.COM * For counter mode algorithms, the IV is a 64 bit quantity, it
285110824SMark.Fenwick@Sun.COM * must NEVER repeat in the lifetime of the SA, otherwise an
285210824SMark.Fenwick@Sun.COM * attacker who had recorded enough packets might be able to
285310824SMark.Fenwick@Sun.COM * determine some clear text.
285410824SMark.Fenwick@Sun.COM *
285510824SMark.Fenwick@Sun.COM * To ensure this does not happen, the IV is stored in the SA and
285610824SMark.Fenwick@Sun.COM * incremented for each packet, the IV is then copied into the
285710824SMark.Fenwick@Sun.COM * "packet" for transmission to the receiving system. The IV will
285810824SMark.Fenwick@Sun.COM * also be copied into the nonce, when the packet is encrypted.
285910824SMark.Fenwick@Sun.COM *
286010824SMark.Fenwick@Sun.COM * CBC mode algorithms use a random IV for each packet. We do not
286110824SMark.Fenwick@Sun.COM * require the highest quality random bits, but for best security
286210824SMark.Fenwick@Sun.COM * with CBC mode ciphers, the value must be unlikely to repeat and
286310824SMark.Fenwick@Sun.COM * must not be known in advance to an adversary capable of influencing
286410824SMark.Fenwick@Sun.COM * the clear text.
28650Sstevel@tonic-gate */
286610824SMark.Fenwick@Sun.COM if (!update_iv((uint8_t *)iv_ptr, espstack->esp_pfkey_q, assoc,
286710824SMark.Fenwick@Sun.COM espstack)) {
286811042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, ill,
286910824SMark.Fenwick@Sun.COM DROPPER(ipss, ipds_esp_iv_wrap), &espstack->esp_dropper);
287011042SErik.Nordmark@Sun.COM if (need_refrele)
287111042SErik.Nordmark@Sun.COM ixa_refrele(ixa);
287211042SErik.Nordmark@Sun.COM return (NULL);
287310824SMark.Fenwick@Sun.COM }
28740Sstevel@tonic-gate
28750Sstevel@tonic-gate /* Fix the IP header. */
28760Sstevel@tonic-gate alloclen = padlen + 2 + mac_len;
28770Sstevel@tonic-gate adj = alloclen + (espmp->b_wptr - espmp->b_rptr);
28780Sstevel@tonic-gate
28790Sstevel@tonic-gate protocol = *nhp;
28800Sstevel@tonic-gate
288111042SErik.Nordmark@Sun.COM if (ixa->ixa_flags & IXAF_IS_IPV4) {
28820Sstevel@tonic-gate ipha->ipha_length = htons(ntohs(ipha->ipha_length) + adj);
28830Sstevel@tonic-gate if (is_natt) {
28840Sstevel@tonic-gate *nhp = IPPROTO_UDP;
28850Sstevel@tonic-gate udpha->uha_length = htons(ntohs(ipha->ipha_length) -
28860Sstevel@tonic-gate IPH_HDR_LENGTH(ipha));
28870Sstevel@tonic-gate } else {
28880Sstevel@tonic-gate *nhp = IPPROTO_ESP;
28890Sstevel@tonic-gate }
28900Sstevel@tonic-gate ipha->ipha_hdr_checksum = 0;
28910Sstevel@tonic-gate ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
28920Sstevel@tonic-gate } else {
28930Sstevel@tonic-gate ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) + adj);
28940Sstevel@tonic-gate *nhp = IPPROTO_ESP;
28950Sstevel@tonic-gate }
28960Sstevel@tonic-gate
28970Sstevel@tonic-gate /* I've got the two ESP mblks, now insert them. */
28980Sstevel@tonic-gate
28993448Sdh155122 esp2dbg(espstack, ("data_mp before outbound ESP adjustment:\n"));
29003448Sdh155122 esp2dbg(espstack, (dump_msg(data_mp)));
29013448Sdh155122
29023448Sdh155122 if (!esp_insert_esp(data_mp, espmp, divpoint, espstack)) {
29033448Sdh155122 ESP_BUMP_STAT(espstack, out_discards);
29040Sstevel@tonic-gate /* NOTE: esp_insert_esp() only fails if there's no memory. */
290511042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, ill,
29063448Sdh155122 DROPPER(ipss, ipds_esp_nomem),
29073448Sdh155122 &espstack->esp_dropper);
29080Sstevel@tonic-gate freeb(espmp);
290911042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
291011042SErik.Nordmark@Sun.COM if (need_refrele)
291111042SErik.Nordmark@Sun.COM ixa_refrele(ixa);
291211042SErik.Nordmark@Sun.COM return (NULL);
29130Sstevel@tonic-gate }
29140Sstevel@tonic-gate
29150Sstevel@tonic-gate /* Append padding (and leave room for ICV). */
29160Sstevel@tonic-gate for (tailmp = data_mp; tailmp->b_cont != NULL; tailmp = tailmp->b_cont)
29170Sstevel@tonic-gate ;
29180Sstevel@tonic-gate if (tailmp->b_wptr + alloclen > tailmp->b_datap->db_lim) {
29190Sstevel@tonic-gate tailmp->b_cont = allocb(alloclen, BPRI_HI);
29200Sstevel@tonic-gate if (tailmp->b_cont == NULL) {
29213448Sdh155122 ESP_BUMP_STAT(espstack, out_discards);
29220Sstevel@tonic-gate esp0dbg(("esp_outbound: Can't allocate tailmp.\n"));
292311042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, ill,
29243448Sdh155122 DROPPER(ipss, ipds_esp_nomem),
29253448Sdh155122 &espstack->esp_dropper);
292611042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
292711042SErik.Nordmark@Sun.COM if (need_refrele)
292811042SErik.Nordmark@Sun.COM ixa_refrele(ixa);
292911042SErik.Nordmark@Sun.COM return (NULL);
29300Sstevel@tonic-gate }
29310Sstevel@tonic-gate tailmp = tailmp->b_cont;
29320Sstevel@tonic-gate }
29330Sstevel@tonic-gate
29340Sstevel@tonic-gate /*
29350Sstevel@tonic-gate * If there's padding, N bytes of padding must be of the form 0x1,
29360Sstevel@tonic-gate * 0x2, 0x3... 0xN.
29370Sstevel@tonic-gate */
29380Sstevel@tonic-gate for (i = 0; i < padlen; ) {
29390Sstevel@tonic-gate i++;
29400Sstevel@tonic-gate *tailmp->b_wptr++ = i;
29410Sstevel@tonic-gate }
29420Sstevel@tonic-gate *tailmp->b_wptr++ = i;
29430Sstevel@tonic-gate *tailmp->b_wptr++ = protocol;
29440Sstevel@tonic-gate
29453448Sdh155122 esp2dbg(espstack, ("data_Mp before encryption:\n"));
29463448Sdh155122 esp2dbg(espstack, (dump_msg(data_mp)));
29470Sstevel@tonic-gate
29480Sstevel@tonic-gate /*
29490Sstevel@tonic-gate * Okay. I've set up the pre-encryption ESP. Let's do it!
29500Sstevel@tonic-gate */
29510Sstevel@tonic-gate
29520Sstevel@tonic-gate if (mac_len > 0) {
29530Sstevel@tonic-gate ASSERT(tailmp->b_wptr + mac_len <= tailmp->b_datap->db_lim);
29540Sstevel@tonic-gate icv_buf = tailmp->b_wptr;
29550Sstevel@tonic-gate tailmp->b_wptr += mac_len;
29560Sstevel@tonic-gate } else {
29570Sstevel@tonic-gate icv_buf = NULL;
29580Sstevel@tonic-gate }
29590Sstevel@tonic-gate
296011042SErik.Nordmark@Sun.COM data_mp = esp_submit_req_outbound(data_mp, ixa, assoc, icv_buf,
296111042SErik.Nordmark@Sun.COM datalen + padlen + 2);
296211042SErik.Nordmark@Sun.COM if (need_refrele)
296311042SErik.Nordmark@Sun.COM ixa_refrele(ixa);
296411042SErik.Nordmark@Sun.COM return (data_mp);
29650Sstevel@tonic-gate }
29660Sstevel@tonic-gate
29670Sstevel@tonic-gate /*
29680Sstevel@tonic-gate * IP calls this to validate the ICMP errors that
29690Sstevel@tonic-gate * we got from the network.
29700Sstevel@tonic-gate */
297111042SErik.Nordmark@Sun.COM mblk_t *
ipsecesp_icmp_error(mblk_t * data_mp,ip_recv_attr_t * ira)297211042SErik.Nordmark@Sun.COM ipsecesp_icmp_error(mblk_t *data_mp, ip_recv_attr_t *ira)
29730Sstevel@tonic-gate {
297411042SErik.Nordmark@Sun.COM netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
297511042SErik.Nordmark@Sun.COM ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
297611042SErik.Nordmark@Sun.COM ipsec_stack_t *ipss = ns->netstack_ipsec;
29773448Sdh155122
29780Sstevel@tonic-gate /*
29790Sstevel@tonic-gate * Unless we get an entire packet back, this function is useless.
29800Sstevel@tonic-gate * Why?
29810Sstevel@tonic-gate *
29820Sstevel@tonic-gate * 1.) Partial packets are useless, because the "next header"
29830Sstevel@tonic-gate * is at the end of the decrypted ESP packet. Without the
29840Sstevel@tonic-gate * whole packet, this is useless.
29850Sstevel@tonic-gate *
29860Sstevel@tonic-gate * 2.) If we every use a stateful cipher, such as a stream or a
29870Sstevel@tonic-gate * one-time pad, we can't do anything.
29880Sstevel@tonic-gate *
29890Sstevel@tonic-gate * Since the chances of us getting an entire packet back are very
29900Sstevel@tonic-gate * very small, we discard here.
29910Sstevel@tonic-gate */
29923448Sdh155122 IP_ESP_BUMP_STAT(ipss, in_discards);
299311042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
29943448Sdh155122 DROPPER(ipss, ipds_esp_icmp),
29953448Sdh155122 &espstack->esp_dropper);
299611042SErik.Nordmark@Sun.COM return (NULL);
29970Sstevel@tonic-gate }
29980Sstevel@tonic-gate
29990Sstevel@tonic-gate /*
30000Sstevel@tonic-gate * Construct an SADB_REGISTER message with the current algorithms.
300110824SMark.Fenwick@Sun.COM * This function gets called when 'ipsecalgs -s' is run or when
300210824SMark.Fenwick@Sun.COM * in.iked (or other KMD) starts.
30030Sstevel@tonic-gate */
30040Sstevel@tonic-gate static boolean_t
esp_register_out(uint32_t sequence,uint32_t pid,uint_t serial,ipsecesp_stack_t * espstack,cred_t * cr)30053448Sdh155122 esp_register_out(uint32_t sequence, uint32_t pid, uint_t serial,
300611042SErik.Nordmark@Sun.COM ipsecesp_stack_t *espstack, cred_t *cr)
30070Sstevel@tonic-gate {
30080Sstevel@tonic-gate mblk_t *pfkey_msg_mp, *keysock_out_mp;
30090Sstevel@tonic-gate sadb_msg_t *samsg;
30100Sstevel@tonic-gate sadb_supported_t *sasupp_auth = NULL;
30110Sstevel@tonic-gate sadb_supported_t *sasupp_encr = NULL;
30120Sstevel@tonic-gate sadb_alg_t *saalg;
30130Sstevel@tonic-gate uint_t allocsize = sizeof (*samsg);
30140Sstevel@tonic-gate uint_t i, numalgs_snap;
30150Sstevel@tonic-gate int current_aalgs;
30160Sstevel@tonic-gate ipsec_alginfo_t **authalgs;
30170Sstevel@tonic-gate uint_t num_aalgs;
30180Sstevel@tonic-gate int current_ealgs;
30190Sstevel@tonic-gate ipsec_alginfo_t **encralgs;
30200Sstevel@tonic-gate uint_t num_ealgs;
30213448Sdh155122 ipsec_stack_t *ipss = espstack->ipsecesp_netstack->netstack_ipsec;
302210934Ssommerfeld@sun.com sadb_sens_t *sens;
302310934Ssommerfeld@sun.com size_t sens_len = 0;
302410934Ssommerfeld@sun.com sadb_ext_t *nextext;
302511042SErik.Nordmark@Sun.COM ts_label_t *sens_tsl = NULL;
30260Sstevel@tonic-gate
30270Sstevel@tonic-gate /* Allocate the KEYSOCK_OUT. */
30280Sstevel@tonic-gate keysock_out_mp = sadb_keysock_out(serial);
30290Sstevel@tonic-gate if (keysock_out_mp == NULL) {
30300Sstevel@tonic-gate esp0dbg(("esp_register_out: couldn't allocate mblk.\n"));
30310Sstevel@tonic-gate return (B_FALSE);
30320Sstevel@tonic-gate }
30330Sstevel@tonic-gate
303411042SErik.Nordmark@Sun.COM if (is_system_labeled() && (cr != NULL)) {
303511042SErik.Nordmark@Sun.COM sens_tsl = crgetlabel(cr);
303611042SErik.Nordmark@Sun.COM if (sens_tsl != NULL) {
303711042SErik.Nordmark@Sun.COM sens_len = sadb_sens_len_from_label(sens_tsl);
303810934Ssommerfeld@sun.com allocsize += sens_len;
303910934Ssommerfeld@sun.com }
304010934Ssommerfeld@sun.com }
304110934Ssommerfeld@sun.com
30420Sstevel@tonic-gate /*
30430Sstevel@tonic-gate * Allocate the PF_KEY message that follows KEYSOCK_OUT.
30440Sstevel@tonic-gate */
30450Sstevel@tonic-gate
30463448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock);
30470Sstevel@tonic-gate /*
30480Sstevel@tonic-gate * Fill SADB_REGISTER message's algorithm descriptors. Hold
30490Sstevel@tonic-gate * down the lock while filling it.
30500Sstevel@tonic-gate *
30510Sstevel@tonic-gate * Return only valid algorithms, so the number of algorithms
30520Sstevel@tonic-gate * to send up may be less than the number of algorithm entries
30530Sstevel@tonic-gate * in the table.
30540Sstevel@tonic-gate */
30553448Sdh155122 authalgs = ipss->ipsec_alglists[IPSEC_ALG_AUTH];
30560Sstevel@tonic-gate for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
30570Sstevel@tonic-gate if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
30580Sstevel@tonic-gate num_aalgs++;
30590Sstevel@tonic-gate
30600Sstevel@tonic-gate if (num_aalgs != 0) {
30610Sstevel@tonic-gate allocsize += (num_aalgs * sizeof (*saalg));
30620Sstevel@tonic-gate allocsize += sizeof (*sasupp_auth);
30630Sstevel@tonic-gate }
30643448Sdh155122 encralgs = ipss->ipsec_alglists[IPSEC_ALG_ENCR];
30650Sstevel@tonic-gate for (num_ealgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
30660Sstevel@tonic-gate if (encralgs[i] != NULL && ALG_VALID(encralgs[i]))
30670Sstevel@tonic-gate num_ealgs++;
30680Sstevel@tonic-gate
30690Sstevel@tonic-gate if (num_ealgs != 0) {
30700Sstevel@tonic-gate allocsize += (num_ealgs * sizeof (*saalg));
30710Sstevel@tonic-gate allocsize += sizeof (*sasupp_encr);
30720Sstevel@tonic-gate }
30730Sstevel@tonic-gate keysock_out_mp->b_cont = allocb(allocsize, BPRI_HI);
30740Sstevel@tonic-gate if (keysock_out_mp->b_cont == NULL) {
30753448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
30760Sstevel@tonic-gate freemsg(keysock_out_mp);
30770Sstevel@tonic-gate return (B_FALSE);
30780Sstevel@tonic-gate }
30790Sstevel@tonic-gate pfkey_msg_mp = keysock_out_mp->b_cont;
30800Sstevel@tonic-gate pfkey_msg_mp->b_wptr += allocsize;
308110934Ssommerfeld@sun.com
308210934Ssommerfeld@sun.com nextext = (sadb_ext_t *)(pfkey_msg_mp->b_rptr + sizeof (*samsg));
308310934Ssommerfeld@sun.com
30840Sstevel@tonic-gate if (num_aalgs != 0) {
308510934Ssommerfeld@sun.com sasupp_auth = (sadb_supported_t *)nextext;
30860Sstevel@tonic-gate saalg = (sadb_alg_t *)(sasupp_auth + 1);
30870Sstevel@tonic-gate
30880Sstevel@tonic-gate ASSERT(((ulong_t)saalg & 0x7) == 0);
30890Sstevel@tonic-gate
30900Sstevel@tonic-gate numalgs_snap = 0;
30910Sstevel@tonic-gate for (i = 0;
30923448Sdh155122 ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs));
30933448Sdh155122 i++) {
30940Sstevel@tonic-gate if (authalgs[i] == NULL || !ALG_VALID(authalgs[i]))
30950Sstevel@tonic-gate continue;
30960Sstevel@tonic-gate
30970Sstevel@tonic-gate saalg->sadb_alg_id = authalgs[i]->alg_id;
30980Sstevel@tonic-gate saalg->sadb_alg_ivlen = 0;
30990Sstevel@tonic-gate saalg->sadb_alg_minbits = authalgs[i]->alg_ef_minbits;
31000Sstevel@tonic-gate saalg->sadb_alg_maxbits = authalgs[i]->alg_ef_maxbits;
31010Sstevel@tonic-gate saalg->sadb_x_alg_increment =
31020Sstevel@tonic-gate authalgs[i]->alg_increment;
310310824SMark.Fenwick@Sun.COM saalg->sadb_x_alg_saltbits = SADB_8TO1(
310410824SMark.Fenwick@Sun.COM authalgs[i]->alg_saltlen);
31050Sstevel@tonic-gate numalgs_snap++;
31060Sstevel@tonic-gate saalg++;
31070Sstevel@tonic-gate }
31080Sstevel@tonic-gate ASSERT(numalgs_snap == num_aalgs);
31090Sstevel@tonic-gate #ifdef DEBUG
31100Sstevel@tonic-gate /*
31110Sstevel@tonic-gate * Reality check to make sure I snagged all of the
31120Sstevel@tonic-gate * algorithms.
31130Sstevel@tonic-gate */
31140Sstevel@tonic-gate for (; i < IPSEC_MAX_ALGS; i++) {
31150Sstevel@tonic-gate if (authalgs[i] != NULL && ALG_VALID(authalgs[i])) {
31160Sstevel@tonic-gate cmn_err(CE_PANIC, "esp_register_out()! "
31170Sstevel@tonic-gate "Missed aalg #%d.\n", i);
31180Sstevel@tonic-gate }
31190Sstevel@tonic-gate }
31200Sstevel@tonic-gate #endif /* DEBUG */
312110934Ssommerfeld@sun.com nextext = (sadb_ext_t *)saalg;
31220Sstevel@tonic-gate }
31230Sstevel@tonic-gate
31240Sstevel@tonic-gate if (num_ealgs != 0) {
312510934Ssommerfeld@sun.com sasupp_encr = (sadb_supported_t *)nextext;
31260Sstevel@tonic-gate saalg = (sadb_alg_t *)(sasupp_encr + 1);
31270Sstevel@tonic-gate
31280Sstevel@tonic-gate numalgs_snap = 0;
31290Sstevel@tonic-gate for (i = 0;
31300Sstevel@tonic-gate ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_ealgs)); i++) {
31310Sstevel@tonic-gate if (encralgs[i] == NULL || !ALG_VALID(encralgs[i]))
31320Sstevel@tonic-gate continue;
31330Sstevel@tonic-gate saalg->sadb_alg_id = encralgs[i]->alg_id;
313410824SMark.Fenwick@Sun.COM saalg->sadb_alg_ivlen = encralgs[i]->alg_ivlen;
31350Sstevel@tonic-gate saalg->sadb_alg_minbits = encralgs[i]->alg_ef_minbits;
31360Sstevel@tonic-gate saalg->sadb_alg_maxbits = encralgs[i]->alg_ef_maxbits;
313710824SMark.Fenwick@Sun.COM /*
313810824SMark.Fenwick@Sun.COM * We could advertise the ICV length, except there
313910824SMark.Fenwick@Sun.COM * is not a value in sadb_x_algb to do this.
314010824SMark.Fenwick@Sun.COM * saalg->sadb_alg_maclen = encralgs[i]->alg_maclen;
314110824SMark.Fenwick@Sun.COM */
31420Sstevel@tonic-gate saalg->sadb_x_alg_increment =
31430Sstevel@tonic-gate encralgs[i]->alg_increment;
314410824SMark.Fenwick@Sun.COM saalg->sadb_x_alg_saltbits =
314510824SMark.Fenwick@Sun.COM SADB_8TO1(encralgs[i]->alg_saltlen);
314610824SMark.Fenwick@Sun.COM
31470Sstevel@tonic-gate numalgs_snap++;
31480Sstevel@tonic-gate saalg++;
31490Sstevel@tonic-gate }
31500Sstevel@tonic-gate ASSERT(numalgs_snap == num_ealgs);
31510Sstevel@tonic-gate #ifdef DEBUG
31520Sstevel@tonic-gate /*
31530Sstevel@tonic-gate * Reality check to make sure I snagged all of the
31540Sstevel@tonic-gate * algorithms.
31550Sstevel@tonic-gate */
31560Sstevel@tonic-gate for (; i < IPSEC_MAX_ALGS; i++) {
31570Sstevel@tonic-gate if (encralgs[i] != NULL && ALG_VALID(encralgs[i])) {
31580Sstevel@tonic-gate cmn_err(CE_PANIC, "esp_register_out()! "
31590Sstevel@tonic-gate "Missed ealg #%d.\n", i);
31600Sstevel@tonic-gate }
31610Sstevel@tonic-gate }
31620Sstevel@tonic-gate #endif /* DEBUG */
316310934Ssommerfeld@sun.com nextext = (sadb_ext_t *)saalg;
31640Sstevel@tonic-gate }
31650Sstevel@tonic-gate
31660Sstevel@tonic-gate current_aalgs = num_aalgs;
31670Sstevel@tonic-gate current_ealgs = num_ealgs;
31680Sstevel@tonic-gate
31693448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
31700Sstevel@tonic-gate
317111042SErik.Nordmark@Sun.COM if (sens_tsl != NULL) {
317210934Ssommerfeld@sun.com sens = (sadb_sens_t *)nextext;
317311042SErik.Nordmark@Sun.COM sadb_sens_from_label(sens, SADB_EXT_SENSITIVITY,
317411042SErik.Nordmark@Sun.COM sens_tsl, sens_len);
317510934Ssommerfeld@sun.com
317610934Ssommerfeld@sun.com nextext = (sadb_ext_t *)(((uint8_t *)sens) + sens_len);
317710934Ssommerfeld@sun.com }
317810934Ssommerfeld@sun.com
31790Sstevel@tonic-gate /* Now fill the rest of the SADB_REGISTER message. */
31800Sstevel@tonic-gate
31810Sstevel@tonic-gate samsg = (sadb_msg_t *)pfkey_msg_mp->b_rptr;
31820Sstevel@tonic-gate samsg->sadb_msg_version = PF_KEY_V2;
31830Sstevel@tonic-gate samsg->sadb_msg_type = SADB_REGISTER;
31840Sstevel@tonic-gate samsg->sadb_msg_errno = 0;
31850Sstevel@tonic-gate samsg->sadb_msg_satype = SADB_SATYPE_ESP;
31860Sstevel@tonic-gate samsg->sadb_msg_len = SADB_8TO64(allocsize);
31870Sstevel@tonic-gate samsg->sadb_msg_reserved = 0;
31880Sstevel@tonic-gate /*
31890Sstevel@tonic-gate * Assume caller has sufficient sequence/pid number info. If it's one
31900Sstevel@tonic-gate * from me over a new alg., I could give two hoots about sequence.
31910Sstevel@tonic-gate */
31920Sstevel@tonic-gate samsg->sadb_msg_seq = sequence;
31930Sstevel@tonic-gate samsg->sadb_msg_pid = pid;
31940Sstevel@tonic-gate
31950Sstevel@tonic-gate if (sasupp_auth != NULL) {
31964987Sdanmcd sasupp_auth->sadb_supported_len = SADB_8TO64(
31974987Sdanmcd sizeof (*sasupp_auth) + sizeof (*saalg) * current_aalgs);
31980Sstevel@tonic-gate sasupp_auth->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
31990Sstevel@tonic-gate sasupp_auth->sadb_supported_reserved = 0;
32000Sstevel@tonic-gate }
32010Sstevel@tonic-gate
32020Sstevel@tonic-gate if (sasupp_encr != NULL) {
32034987Sdanmcd sasupp_encr->sadb_supported_len = SADB_8TO64(
32044987Sdanmcd sizeof (*sasupp_encr) + sizeof (*saalg) * current_ealgs);
32050Sstevel@tonic-gate sasupp_encr->sadb_supported_exttype =
32060Sstevel@tonic-gate SADB_EXT_SUPPORTED_ENCRYPT;
32070Sstevel@tonic-gate sasupp_encr->sadb_supported_reserved = 0;
32080Sstevel@tonic-gate }
32090Sstevel@tonic-gate
32103448Sdh155122 if (espstack->esp_pfkey_q != NULL)
32113448Sdh155122 putnext(espstack->esp_pfkey_q, keysock_out_mp);
32120Sstevel@tonic-gate else {
32130Sstevel@tonic-gate freemsg(keysock_out_mp);
32140Sstevel@tonic-gate return (B_FALSE);
32150Sstevel@tonic-gate }
32160Sstevel@tonic-gate
32170Sstevel@tonic-gate return (B_TRUE);
32180Sstevel@tonic-gate }
32190Sstevel@tonic-gate
32200Sstevel@tonic-gate /*
32210Sstevel@tonic-gate * Invoked when the algorithm table changes. Causes SADB_REGISTER
32220Sstevel@tonic-gate * messages continaining the current list of algorithms to be
32230Sstevel@tonic-gate * sent up to the ESP listeners.
32240Sstevel@tonic-gate */
32250Sstevel@tonic-gate void
ipsecesp_algs_changed(netstack_t * ns)32263448Sdh155122 ipsecesp_algs_changed(netstack_t *ns)
32270Sstevel@tonic-gate {
32283448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
32293448Sdh155122
32300Sstevel@tonic-gate /*
32310Sstevel@tonic-gate * Time to send a PF_KEY SADB_REGISTER message to ESP listeners
32320Sstevel@tonic-gate * everywhere. (The function itself checks for NULL esp_pfkey_q.)
32330Sstevel@tonic-gate */
323410934Ssommerfeld@sun.com (void) esp_register_out(0, 0, 0, espstack, NULL);
32350Sstevel@tonic-gate }
32360Sstevel@tonic-gate
32370Sstevel@tonic-gate /*
32388704Sdanmcd@sun.com * Stub function that taskq_dispatch() invokes to take the mblk (in arg)
323911042SErik.Nordmark@Sun.COM * and send() it into ESP and IP again.
32400Sstevel@tonic-gate */
32410Sstevel@tonic-gate static void
inbound_task(void * arg)32420Sstevel@tonic-gate inbound_task(void *arg)
32430Sstevel@tonic-gate {
324411042SErik.Nordmark@Sun.COM mblk_t *mp = (mblk_t *)arg;
324511042SErik.Nordmark@Sun.COM mblk_t *async_mp;
324611042SErik.Nordmark@Sun.COM ip_recv_attr_t iras;
324711042SErik.Nordmark@Sun.COM
324811042SErik.Nordmark@Sun.COM async_mp = mp;
324911042SErik.Nordmark@Sun.COM mp = async_mp->b_cont;
325011042SErik.Nordmark@Sun.COM async_mp->b_cont = NULL;
325111042SErik.Nordmark@Sun.COM if (!ip_recv_attr_from_mblk(async_mp, &iras)) {
325211042SErik.Nordmark@Sun.COM /* The ill or ip_stack_t disappeared on us */
325311042SErik.Nordmark@Sun.COM ip_drop_input("ip_recv_attr_from_mblk", mp, NULL);
32548704Sdanmcd@sun.com freemsg(mp);
325511042SErik.Nordmark@Sun.COM goto done;
32568704Sdanmcd@sun.com }
32578704Sdanmcd@sun.com
325811042SErik.Nordmark@Sun.COM esp_inbound_restart(mp, &iras);
325911042SErik.Nordmark@Sun.COM done:
326011042SErik.Nordmark@Sun.COM ira_cleanup(&iras, B_TRUE);
326111042SErik.Nordmark@Sun.COM }
326211042SErik.Nordmark@Sun.COM
326311042SErik.Nordmark@Sun.COM /*
326411042SErik.Nordmark@Sun.COM * Restart ESP after the SA has been added.
326511042SErik.Nordmark@Sun.COM */
326611042SErik.Nordmark@Sun.COM static void
esp_inbound_restart(mblk_t * mp,ip_recv_attr_t * ira)326711042SErik.Nordmark@Sun.COM esp_inbound_restart(mblk_t *mp, ip_recv_attr_t *ira)
326811042SErik.Nordmark@Sun.COM {
326911042SErik.Nordmark@Sun.COM esph_t *esph;
327011042SErik.Nordmark@Sun.COM netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
327111042SErik.Nordmark@Sun.COM ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
32728704Sdanmcd@sun.com
32733448Sdh155122 esp2dbg(espstack, ("in ESP inbound_task"));
32743448Sdh155122 ASSERT(espstack != NULL);
32753448Sdh155122
327611042SErik.Nordmark@Sun.COM mp = ipsec_inbound_esp_sa(mp, ira, &esph);
327711042SErik.Nordmark@Sun.COM if (mp == NULL)
327811042SErik.Nordmark@Sun.COM return;
327911042SErik.Nordmark@Sun.COM
328011042SErik.Nordmark@Sun.COM ASSERT(esph != NULL);
328111042SErik.Nordmark@Sun.COM ASSERT(ira->ira_flags & IRAF_IPSEC_SECURE);
328211042SErik.Nordmark@Sun.COM ASSERT(ira->ira_ipsec_esp_sa != NULL);
328311042SErik.Nordmark@Sun.COM
328411042SErik.Nordmark@Sun.COM mp = ira->ira_ipsec_esp_sa->ipsa_input_func(mp, esph, ira);
328511042SErik.Nordmark@Sun.COM if (mp == NULL) {
328611042SErik.Nordmark@Sun.COM /*
328711042SErik.Nordmark@Sun.COM * Either it failed or is pending. In the former case
328811042SErik.Nordmark@Sun.COM * ipIfStatsInDiscards was increased.
328911042SErik.Nordmark@Sun.COM */
329011042SErik.Nordmark@Sun.COM return;
32918704Sdanmcd@sun.com }
329211042SErik.Nordmark@Sun.COM
329311042SErik.Nordmark@Sun.COM ip_input_post_ipsec(mp, ira);
32940Sstevel@tonic-gate }
32950Sstevel@tonic-gate
32960Sstevel@tonic-gate /*
32970Sstevel@tonic-gate * Now that weak-key passed, actually ADD the security association, and
32980Sstevel@tonic-gate * send back a reply ADD message.
32990Sstevel@tonic-gate */
33000Sstevel@tonic-gate static int
esp_add_sa_finish(mblk_t * mp,sadb_msg_t * samsg,keysock_in_t * ksi,int * diagnostic,ipsecesp_stack_t * espstack)33013055Sdanmcd esp_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi,
33023448Sdh155122 int *diagnostic, ipsecesp_stack_t *espstack)
33030Sstevel@tonic-gate {
330410934Ssommerfeld@sun.com isaf_t *primary = NULL, *secondary;
330510934Ssommerfeld@sun.com boolean_t clone = B_FALSE, is_inbound = B_FALSE;
33060Sstevel@tonic-gate ipsa_t *larval = NULL;
33070Sstevel@tonic-gate ipsacq_t *acqrec;
33080Sstevel@tonic-gate iacqf_t *acq_bucket;
33090Sstevel@tonic-gate mblk_t *acq_msgs = NULL;
33100Sstevel@tonic-gate int rc;
33110Sstevel@tonic-gate mblk_t *lpkt;
331210934Ssommerfeld@sun.com int error;
331310934Ssommerfeld@sun.com ipsa_query_t sq;
33143448Sdh155122 ipsec_stack_t *ipss = espstack->ipsecesp_netstack->netstack_ipsec;
33150Sstevel@tonic-gate
33160Sstevel@tonic-gate /*
33170Sstevel@tonic-gate * Locate the appropriate table(s).
33180Sstevel@tonic-gate */
331910934Ssommerfeld@sun.com sq.spp = &espstack->esp_sadb; /* XXX */
332010934Ssommerfeld@sun.com error = sadb_form_query(ksi, IPSA_Q_SA|IPSA_Q_DST,
332110934Ssommerfeld@sun.com IPSA_Q_SA|IPSA_Q_DST|IPSA_Q_INBOUND|IPSA_Q_OUTBOUND,
332210934Ssommerfeld@sun.com &sq, diagnostic);
332310934Ssommerfeld@sun.com if (error)
332410934Ssommerfeld@sun.com return (error);
3325691Ssommerfe
33266668Smarkfen /*
33276668Smarkfen * Use the direction flags provided by the KMD to determine
33286668Smarkfen * if the inbound or outbound table should be the primary
33296668Smarkfen * for this SA. If these flags were absent then make this
33306668Smarkfen * decision based on the addresses.
33316668Smarkfen */
333210934Ssommerfeld@sun.com if (sq.assoc->sadb_sa_flags & IPSA_F_INBOUND) {
333310934Ssommerfeld@sun.com primary = sq.inbound;
333410934Ssommerfeld@sun.com secondary = sq.outbound;
33356668Smarkfen is_inbound = B_TRUE;
333610934Ssommerfeld@sun.com if (sq.assoc->sadb_sa_flags & IPSA_F_OUTBOUND)
33376668Smarkfen clone = B_TRUE;
333810934Ssommerfeld@sun.com } else if (sq.assoc->sadb_sa_flags & IPSA_F_OUTBOUND) {
333910934Ssommerfeld@sun.com primary = sq.outbound;
334010934Ssommerfeld@sun.com secondary = sq.inbound;
33416668Smarkfen }
33426668Smarkfen
33436668Smarkfen if (primary == NULL) {
33446668Smarkfen /*
33456668Smarkfen * The KMD did not set a direction flag, determine which
33466668Smarkfen * table to insert the SA into based on addresses.
33476668Smarkfen */
33486668Smarkfen switch (ksi->ks_in_dsttype) {
33496668Smarkfen case KS_IN_ADDR_MBCAST:
33506668Smarkfen clone = B_TRUE; /* All mcast SAs can be bidirectional */
335110934Ssommerfeld@sun.com sq.assoc->sadb_sa_flags |= IPSA_F_OUTBOUND;
33526668Smarkfen /* FALLTHRU */
33530Sstevel@tonic-gate /*
33540Sstevel@tonic-gate * If the source address is either one of mine, or unspecified
33550Sstevel@tonic-gate * (which is best summed up by saying "not 'not mine'"),
33560Sstevel@tonic-gate * then the association is potentially bi-directional,
33570Sstevel@tonic-gate * in that it can be used for inbound traffic and outbound
33580Sstevel@tonic-gate * traffic. The best example of such an SA is a multicast
33590Sstevel@tonic-gate * SA (which allows me to receive the outbound traffic).
33600Sstevel@tonic-gate */
33616668Smarkfen case KS_IN_ADDR_ME:
336210934Ssommerfeld@sun.com sq.assoc->sadb_sa_flags |= IPSA_F_INBOUND;
336310934Ssommerfeld@sun.com primary = sq.inbound;
336410934Ssommerfeld@sun.com secondary = sq.outbound;
33656668Smarkfen if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME)
33666668Smarkfen clone = B_TRUE;
33676668Smarkfen is_inbound = B_TRUE;
33686668Smarkfen break;
33690Sstevel@tonic-gate /*
33700Sstevel@tonic-gate * If the source address literally not mine (either
33710Sstevel@tonic-gate * unspecified or not mine), then this SA may have an
33720Sstevel@tonic-gate * address that WILL be mine after some configuration.
33730Sstevel@tonic-gate * We pay the price for this by making it a bi-directional
33740Sstevel@tonic-gate * SA.
33750Sstevel@tonic-gate */
33766668Smarkfen case KS_IN_ADDR_NOTME:
337710934Ssommerfeld@sun.com sq.assoc->sadb_sa_flags |= IPSA_F_OUTBOUND;
337810934Ssommerfeld@sun.com primary = sq.outbound;
337910934Ssommerfeld@sun.com secondary = sq.inbound;
33806668Smarkfen if (ksi->ks_in_srctype != KS_IN_ADDR_ME) {
338110934Ssommerfeld@sun.com sq.assoc->sadb_sa_flags |= IPSA_F_INBOUND;
33826668Smarkfen clone = B_TRUE;
33836668Smarkfen }
33846668Smarkfen break;
33856668Smarkfen default:
33866668Smarkfen *diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
33876668Smarkfen return (EINVAL);
33886668Smarkfen }
33890Sstevel@tonic-gate }
33900Sstevel@tonic-gate
33910Sstevel@tonic-gate /*
33920Sstevel@tonic-gate * Find a ACQUIRE list entry if possible. If we've added an SA that
33930Sstevel@tonic-gate * suits the needs of an ACQUIRE list entry, we can eliminate the
33940Sstevel@tonic-gate * ACQUIRE list entry and transmit the enqueued packets. Use the
33950Sstevel@tonic-gate * high-bit of the sequence number to queue it. Key off destination
33960Sstevel@tonic-gate * addr, and change acqrec's state.
33970Sstevel@tonic-gate */
33980Sstevel@tonic-gate
33990Sstevel@tonic-gate if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) {
340010934Ssommerfeld@sun.com acq_bucket = &(sq.sp->sdb_acq[sq.outhash]);
34010Sstevel@tonic-gate mutex_enter(&acq_bucket->iacqf_lock);
34020Sstevel@tonic-gate for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL;
34030Sstevel@tonic-gate acqrec = acqrec->ipsacq_next) {
34040Sstevel@tonic-gate mutex_enter(&acqrec->ipsacq_lock);
34050Sstevel@tonic-gate /*
34060Sstevel@tonic-gate * Q: I only check sequence. Should I check dst?
34070Sstevel@tonic-gate * A: Yes, check dest because those are the packets
34080Sstevel@tonic-gate * that are queued up.
34090Sstevel@tonic-gate */
34100Sstevel@tonic-gate if (acqrec->ipsacq_seq == samsg->sadb_msg_seq &&
341110934Ssommerfeld@sun.com IPSA_ARE_ADDR_EQUAL(sq.dstaddr,
34124987Sdanmcd acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam))
34130Sstevel@tonic-gate break;
34140Sstevel@tonic-gate mutex_exit(&acqrec->ipsacq_lock);
34150Sstevel@tonic-gate }
34160Sstevel@tonic-gate if (acqrec != NULL) {
34170Sstevel@tonic-gate /*
34180Sstevel@tonic-gate * AHA! I found an ACQUIRE record for this SA.
34190Sstevel@tonic-gate * Grab the msg list, and free the acquire record.
34200Sstevel@tonic-gate * I already am holding the lock for this record,
34210Sstevel@tonic-gate * so all I have to do is free it.
34220Sstevel@tonic-gate */
34230Sstevel@tonic-gate acq_msgs = acqrec->ipsacq_mp;
34240Sstevel@tonic-gate acqrec->ipsacq_mp = NULL;
34250Sstevel@tonic-gate mutex_exit(&acqrec->ipsacq_lock);
34263448Sdh155122 sadb_destroy_acquire(acqrec,
34273448Sdh155122 espstack->ipsecesp_netstack);
34280Sstevel@tonic-gate }
34290Sstevel@tonic-gate mutex_exit(&acq_bucket->iacqf_lock);
34300Sstevel@tonic-gate }
34310Sstevel@tonic-gate
34320Sstevel@tonic-gate /*
34330Sstevel@tonic-gate * Find PF_KEY message, and see if I'm an update. If so, find entry
34340Sstevel@tonic-gate * in larval list (if there).
34350Sstevel@tonic-gate */
34360Sstevel@tonic-gate if (samsg->sadb_msg_type == SADB_UPDATE) {
343710934Ssommerfeld@sun.com mutex_enter(&sq.inbound->isaf_lock);
343810934Ssommerfeld@sun.com larval = ipsec_getassocbyspi(sq.inbound, sq.assoc->sadb_sa_spi,
343910934Ssommerfeld@sun.com ALL_ZEROES_PTR, sq.dstaddr, sq.dst->sin_family);
344010934Ssommerfeld@sun.com mutex_exit(&sq.inbound->isaf_lock);
34410Sstevel@tonic-gate
34427110Sdanmcd if ((larval == NULL) ||
34437110Sdanmcd (larval->ipsa_state != IPSA_STATE_LARVAL)) {
34446668Smarkfen *diagnostic = SADB_X_DIAGNOSTIC_SA_NOTFOUND;
34457110Sdanmcd if (larval != NULL) {
34467110Sdanmcd IPSA_REFRELE(larval);
34477110Sdanmcd }
34480Sstevel@tonic-gate esp0dbg(("Larval update, but larval disappeared.\n"));
34490Sstevel@tonic-gate return (ESRCH);
34500Sstevel@tonic-gate } /* Else sadb_common_add unlinks it for me! */
34510Sstevel@tonic-gate }
34520Sstevel@tonic-gate
345311446Sdanmcd@sun.com if (larval != NULL) {
345411446Sdanmcd@sun.com /*
345511446Sdanmcd@sun.com * Hold again, because sadb_common_add() consumes a reference,
345611446Sdanmcd@sun.com * and we don't want to clear_lpkt() without a reference.
345711446Sdanmcd@sun.com */
345811446Sdanmcd@sun.com IPSA_REFHOLD(larval);
345911446Sdanmcd@sun.com }
34600Sstevel@tonic-gate
346111042SErik.Nordmark@Sun.COM rc = sadb_common_add(espstack->esp_pfkey_q,
34623448Sdh155122 mp, samsg, ksi, primary, secondary, larval, clone, is_inbound,
34636668Smarkfen diagnostic, espstack->ipsecesp_netstack, &espstack->esp_sadb);
34640Sstevel@tonic-gate
346511446Sdanmcd@sun.com if (larval != NULL) {
346611042SErik.Nordmark@Sun.COM if (rc == 0) {
346711446Sdanmcd@sun.com lpkt = sadb_clear_lpkt(larval);
346811446Sdanmcd@sun.com if (lpkt != NULL) {
346911446Sdanmcd@sun.com rc = !taskq_dispatch(esp_taskq, inbound_task,
347011446Sdanmcd@sun.com lpkt, TQ_NOSLEEP);
347111446Sdanmcd@sun.com }
347211042SErik.Nordmark@Sun.COM }
347311446Sdanmcd@sun.com IPSA_REFRELE(larval);
34740Sstevel@tonic-gate }
34750Sstevel@tonic-gate
34760Sstevel@tonic-gate /*
34770Sstevel@tonic-gate * How much more stack will I create with all of these
34780Sstevel@tonic-gate * esp_outbound() calls?
34790Sstevel@tonic-gate */
34800Sstevel@tonic-gate
348111042SErik.Nordmark@Sun.COM /* Handle the packets queued waiting for the SA */
34820Sstevel@tonic-gate while (acq_msgs != NULL) {
348311042SErik.Nordmark@Sun.COM mblk_t *asyncmp;
348411042SErik.Nordmark@Sun.COM mblk_t *data_mp;
348511042SErik.Nordmark@Sun.COM ip_xmit_attr_t ixas;
348611042SErik.Nordmark@Sun.COM ill_t *ill;
348711042SErik.Nordmark@Sun.COM
348811042SErik.Nordmark@Sun.COM asyncmp = acq_msgs;
34890Sstevel@tonic-gate acq_msgs = acq_msgs->b_next;
349011042SErik.Nordmark@Sun.COM asyncmp->b_next = NULL;
349111042SErik.Nordmark@Sun.COM
349211042SErik.Nordmark@Sun.COM /*
349311042SErik.Nordmark@Sun.COM * Extract the ip_xmit_attr_t from the first mblk.
349411042SErik.Nordmark@Sun.COM * Verifies that the netstack and ill is still around; could
349511042SErik.Nordmark@Sun.COM * have vanished while iked was doing its work.
349611042SErik.Nordmark@Sun.COM * On succesful return we have a nce_t and the ill/ipst can't
349711042SErik.Nordmark@Sun.COM * disappear until we do the nce_refrele in ixa_cleanup.
349811042SErik.Nordmark@Sun.COM */
349911042SErik.Nordmark@Sun.COM data_mp = asyncmp->b_cont;
350011042SErik.Nordmark@Sun.COM asyncmp->b_cont = NULL;
350111042SErik.Nordmark@Sun.COM if (!ip_xmit_attr_from_mblk(asyncmp, &ixas)) {
350211042SErik.Nordmark@Sun.COM ESP_BUMP_STAT(espstack, out_discards);
350311042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, NULL,
350411042SErik.Nordmark@Sun.COM DROPPER(ipss, ipds_sadb_acquire_timeout),
350511042SErik.Nordmark@Sun.COM &espstack->esp_dropper);
350611042SErik.Nordmark@Sun.COM } else if (rc != 0) {
350711042SErik.Nordmark@Sun.COM ill = ixas.ixa_nce->nce_ill;
350811042SErik.Nordmark@Sun.COM ESP_BUMP_STAT(espstack, out_discards);
350911042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, ill,
351011042SErik.Nordmark@Sun.COM DROPPER(ipss, ipds_sadb_acquire_timeout),
351111042SErik.Nordmark@Sun.COM &espstack->esp_dropper);
351211042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
351311042SErik.Nordmark@Sun.COM } else {
351411042SErik.Nordmark@Sun.COM esp_outbound_finish(data_mp, &ixas);
35150Sstevel@tonic-gate }
351611042SErik.Nordmark@Sun.COM ixa_cleanup(&ixas);
35170Sstevel@tonic-gate }
35180Sstevel@tonic-gate
35190Sstevel@tonic-gate return (rc);
35200Sstevel@tonic-gate }
35210Sstevel@tonic-gate
35220Sstevel@tonic-gate /*
352311042SErik.Nordmark@Sun.COM * Process one of the queued messages (from ipsacq_mp) once the SA
352411042SErik.Nordmark@Sun.COM * has been added.
352511042SErik.Nordmark@Sun.COM */
352611042SErik.Nordmark@Sun.COM static void
esp_outbound_finish(mblk_t * data_mp,ip_xmit_attr_t * ixa)352711042SErik.Nordmark@Sun.COM esp_outbound_finish(mblk_t *data_mp, ip_xmit_attr_t *ixa)
352811042SErik.Nordmark@Sun.COM {
352911042SErik.Nordmark@Sun.COM netstack_t *ns = ixa->ixa_ipst->ips_netstack;
353011042SErik.Nordmark@Sun.COM ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
353111042SErik.Nordmark@Sun.COM ipsec_stack_t *ipss = ns->netstack_ipsec;
353211042SErik.Nordmark@Sun.COM ill_t *ill = ixa->ixa_nce->nce_ill;
353311042SErik.Nordmark@Sun.COM
353411042SErik.Nordmark@Sun.COM if (!ipsec_outbound_sa(data_mp, ixa, IPPROTO_ESP)) {
353511042SErik.Nordmark@Sun.COM ESP_BUMP_STAT(espstack, out_discards);
353611042SErik.Nordmark@Sun.COM ip_drop_packet(data_mp, B_FALSE, ill,
353711042SErik.Nordmark@Sun.COM DROPPER(ipss, ipds_sadb_acquire_timeout),
353811042SErik.Nordmark@Sun.COM &espstack->esp_dropper);
353911042SErik.Nordmark@Sun.COM BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
354011042SErik.Nordmark@Sun.COM return;
354111042SErik.Nordmark@Sun.COM }
354211042SErik.Nordmark@Sun.COM
354311042SErik.Nordmark@Sun.COM data_mp = esp_outbound(data_mp, ixa);
354411042SErik.Nordmark@Sun.COM if (data_mp == NULL)
354511042SErik.Nordmark@Sun.COM return;
354611042SErik.Nordmark@Sun.COM
354711042SErik.Nordmark@Sun.COM /* do AH processing if needed */
354811042SErik.Nordmark@Sun.COM data_mp = esp_do_outbound_ah(data_mp, ixa);
354911042SErik.Nordmark@Sun.COM if (data_mp == NULL)
355011042SErik.Nordmark@Sun.COM return;
355111042SErik.Nordmark@Sun.COM
355211042SErik.Nordmark@Sun.COM (void) ip_output_post_ipsec(data_mp, ixa);
355311042SErik.Nordmark@Sun.COM }
355411042SErik.Nordmark@Sun.COM
355511042SErik.Nordmark@Sun.COM /*
35560Sstevel@tonic-gate * Add new ESP security association. This may become a generic AH/ESP
35570Sstevel@tonic-gate * routine eventually.
35580Sstevel@tonic-gate */
35590Sstevel@tonic-gate static int
esp_add_sa(mblk_t * mp,keysock_in_t * ksi,int * diagnostic,netstack_t * ns)35603448Sdh155122 esp_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic, netstack_t *ns)
35610Sstevel@tonic-gate {
35620Sstevel@tonic-gate sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
35630Sstevel@tonic-gate sadb_address_t *srcext =
35640Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
35650Sstevel@tonic-gate sadb_address_t *dstext =
35660Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
35673055Sdanmcd sadb_address_t *isrcext =
35683055Sdanmcd (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC];
35693055Sdanmcd sadb_address_t *idstext =
35703055Sdanmcd (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_DST];
35710Sstevel@tonic-gate sadb_address_t *nttext_loc =
35720Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_LOC];
35730Sstevel@tonic-gate sadb_address_t *nttext_rem =
35740Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_REM];
35750Sstevel@tonic-gate sadb_key_t *akey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH];
35760Sstevel@tonic-gate sadb_key_t *ekey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT];
35770Sstevel@tonic-gate struct sockaddr_in *src, *dst;
35780Sstevel@tonic-gate struct sockaddr_in *natt_loc, *natt_rem;
35790Sstevel@tonic-gate struct sockaddr_in6 *natt_loc6, *natt_rem6;
35800Sstevel@tonic-gate sadb_lifetime_t *soft =
35810Sstevel@tonic-gate (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT];
35820Sstevel@tonic-gate sadb_lifetime_t *hard =
35830Sstevel@tonic-gate (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD];
35847749SThejaswini.Singarajipura@Sun.COM sadb_lifetime_t *idle =
35857749SThejaswini.Singarajipura@Sun.COM (sadb_lifetime_t *)ksi->ks_in_extv[SADB_X_EXT_LIFETIME_IDLE];
35863448Sdh155122 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
35873448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec;
35880Sstevel@tonic-gate
358910934Ssommerfeld@sun.com
359010934Ssommerfeld@sun.com
35910Sstevel@tonic-gate /* I need certain extensions present for an ADD message. */
35920Sstevel@tonic-gate if (srcext == NULL) {
35930Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
35940Sstevel@tonic-gate return (EINVAL);
35950Sstevel@tonic-gate }
35960Sstevel@tonic-gate if (dstext == NULL) {
35970Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
35980Sstevel@tonic-gate return (EINVAL);
35990Sstevel@tonic-gate }
36003055Sdanmcd if (isrcext == NULL && idstext != NULL) {
36013055Sdanmcd *diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC;
36023055Sdanmcd return (EINVAL);
36033055Sdanmcd }
36043055Sdanmcd if (isrcext != NULL && idstext == NULL) {
36053055Sdanmcd *diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST;
36063055Sdanmcd return (EINVAL);
36073055Sdanmcd }
36080Sstevel@tonic-gate if (assoc == NULL) {
36090Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
36100Sstevel@tonic-gate return (EINVAL);
36110Sstevel@tonic-gate }
36120Sstevel@tonic-gate if (ekey == NULL && assoc->sadb_sa_encrypt != SADB_EALG_NULL) {
36130Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_EKEY;
36140Sstevel@tonic-gate return (EINVAL);
36150Sstevel@tonic-gate }
36160Sstevel@tonic-gate
36170Sstevel@tonic-gate src = (struct sockaddr_in *)(srcext + 1);
36180Sstevel@tonic-gate dst = (struct sockaddr_in *)(dstext + 1);
36190Sstevel@tonic-gate natt_loc = (struct sockaddr_in *)(nttext_loc + 1);
36200Sstevel@tonic-gate natt_loc6 = (struct sockaddr_in6 *)(nttext_loc + 1);
36210Sstevel@tonic-gate natt_rem = (struct sockaddr_in *)(nttext_rem + 1);
36220Sstevel@tonic-gate natt_rem6 = (struct sockaddr_in6 *)(nttext_rem + 1);
36230Sstevel@tonic-gate
36240Sstevel@tonic-gate /* Sundry ADD-specific reality checks. */
36250Sstevel@tonic-gate /* XXX STATS : Logging/stats here? */
36267749SThejaswini.Singarajipura@Sun.COM
36277749SThejaswini.Singarajipura@Sun.COM if ((assoc->sadb_sa_state != SADB_SASTATE_MATURE) &&
36287749SThejaswini.Singarajipura@Sun.COM (assoc->sadb_sa_state != SADB_X_SASTATE_ACTIVE_ELSEWHERE)) {
36290Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
36300Sstevel@tonic-gate return (EINVAL);
36310Sstevel@tonic-gate }
36320Sstevel@tonic-gate if (assoc->sadb_sa_encrypt == SADB_EALG_NONE) {
36330Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG;
36340Sstevel@tonic-gate return (EINVAL);
36350Sstevel@tonic-gate }
36360Sstevel@tonic-gate
363711042SErik.Nordmark@Sun.COM #ifndef IPSEC_LATENCY_TEST
36380Sstevel@tonic-gate if (assoc->sadb_sa_encrypt == SADB_EALG_NULL &&
36390Sstevel@tonic-gate assoc->sadb_sa_auth == SADB_AALG_NONE) {
36400Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
36410Sstevel@tonic-gate return (EINVAL);
36420Sstevel@tonic-gate }
364311042SErik.Nordmark@Sun.COM #endif
36440Sstevel@tonic-gate
36457110Sdanmcd if (assoc->sadb_sa_flags & ~espstack->esp_sadb.s_addflags) {
36460Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS;
36470Sstevel@tonic-gate return (EINVAL);
36480Sstevel@tonic-gate }
36490Sstevel@tonic-gate
36507749SThejaswini.Singarajipura@Sun.COM if ((*diagnostic = sadb_hardsoftchk(hard, soft, idle)) != 0) {
36510Sstevel@tonic-gate return (EINVAL);
36520Sstevel@tonic-gate }
36533055Sdanmcd ASSERT(src->sin_family == dst->sin_family);
36540Sstevel@tonic-gate
36550Sstevel@tonic-gate if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_LOC) {
36560Sstevel@tonic-gate if (nttext_loc == NULL) {
36570Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_LOC;
36580Sstevel@tonic-gate return (EINVAL);
36590Sstevel@tonic-gate }
36600Sstevel@tonic-gate
36610Sstevel@tonic-gate if (natt_loc->sin_family == AF_INET6 &&
36620Sstevel@tonic-gate !IN6_IS_ADDR_V4MAPPED(&natt_loc6->sin6_addr)) {
36630Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC;
36640Sstevel@tonic-gate return (EINVAL);
36650Sstevel@tonic-gate }
36660Sstevel@tonic-gate }
36670Sstevel@tonic-gate
36680Sstevel@tonic-gate if (assoc->sadb_sa_flags & SADB_X_SAFLAGS_NATT_REM) {
36690Sstevel@tonic-gate if (nttext_rem == NULL) {
36700Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_NATT_REM;
36710Sstevel@tonic-gate return (EINVAL);
36720Sstevel@tonic-gate }
36730Sstevel@tonic-gate if (natt_rem->sin_family == AF_INET6 &&
36740Sstevel@tonic-gate !IN6_IS_ADDR_V4MAPPED(&natt_rem6->sin6_addr)) {
36750Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM;
36760Sstevel@tonic-gate return (EINVAL);
36770Sstevel@tonic-gate }
36780Sstevel@tonic-gate }
36790Sstevel@tonic-gate
36800Sstevel@tonic-gate
36810Sstevel@tonic-gate /* Stuff I don't support, for now. XXX Diagnostic? */
368210934Ssommerfeld@sun.com if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL)
36830Sstevel@tonic-gate return (EOPNOTSUPP);
36840Sstevel@tonic-gate
368510934Ssommerfeld@sun.com if ((*diagnostic = sadb_labelchk(ksi)) != 0)
368610934Ssommerfeld@sun.com return (EINVAL);
368710934Ssommerfeld@sun.com
36880Sstevel@tonic-gate /*
368910934Ssommerfeld@sun.com * XXX Policy : I'm not checking identities at this time,
369010934Ssommerfeld@sun.com * but if I did, I'd do them here, before I sent
36910Sstevel@tonic-gate * the weak key check up to the algorithm.
36920Sstevel@tonic-gate */
36930Sstevel@tonic-gate
36943448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock);
36950Sstevel@tonic-gate
36960Sstevel@tonic-gate /*
36970Sstevel@tonic-gate * First locate the authentication algorithm.
36980Sstevel@tonic-gate */
369911042SErik.Nordmark@Sun.COM #ifdef IPSEC_LATENCY_TEST
370011042SErik.Nordmark@Sun.COM if (akey != NULL && assoc->sadb_sa_auth != SADB_AALG_NONE) {
370111042SErik.Nordmark@Sun.COM #else
37020Sstevel@tonic-gate if (akey != NULL) {
370311042SErik.Nordmark@Sun.COM #endif
37040Sstevel@tonic-gate ipsec_alginfo_t *aalg;
37050Sstevel@tonic-gate
37063448Sdh155122 aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
37073448Sdh155122 [assoc->sadb_sa_auth];
37080Sstevel@tonic-gate if (aalg == NULL || !ALG_VALID(aalg)) {
37093448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
37103448Sdh155122 esp1dbg(espstack, ("Couldn't find auth alg #%d.\n",
37110Sstevel@tonic-gate assoc->sadb_sa_auth));
37120Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
37130Sstevel@tonic-gate return (EINVAL);
37140Sstevel@tonic-gate }
37152810Smarkfen
37162810Smarkfen /*
37172810Smarkfen * Sanity check key sizes.
37182810Smarkfen * Note: It's not possible to use SADB_AALG_NONE because
37192810Smarkfen * this auth_alg is not defined with ALG_FLAG_VALID. If this
37202810Smarkfen * ever changes, the same check for SADB_AALG_NONE and
37212810Smarkfen * a auth_key != NULL should be made here ( see below).
37222810Smarkfen */
37230Sstevel@tonic-gate if (!ipsec_valid_key_size(akey->sadb_key_bits, aalg)) {
37243448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
37250Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS;
37260Sstevel@tonic-gate return (EINVAL);
37270Sstevel@tonic-gate }
37282810Smarkfen ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
37290Sstevel@tonic-gate
37300Sstevel@tonic-gate /* check key and fix parity if needed */
37310Sstevel@tonic-gate if (ipsec_check_key(aalg->alg_mech_type, akey, B_TRUE,
37320Sstevel@tonic-gate diagnostic) != 0) {
37333448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
37340Sstevel@tonic-gate return (EINVAL);
37350Sstevel@tonic-gate }
37360Sstevel@tonic-gate }
37370Sstevel@tonic-gate
37380Sstevel@tonic-gate /*
37390Sstevel@tonic-gate * Then locate the encryption algorithm.
37400Sstevel@tonic-gate */
37410Sstevel@tonic-gate if (ekey != NULL) {
374210824SMark.Fenwick@Sun.COM uint_t keybits;
37430Sstevel@tonic-gate ipsec_alginfo_t *ealg;
37440Sstevel@tonic-gate
37453448Sdh155122 ealg = ipss->ipsec_alglists[IPSEC_ALG_ENCR]
37463448Sdh155122 [assoc->sadb_sa_encrypt];
37470Sstevel@tonic-gate if (ealg == NULL || !ALG_VALID(ealg)) {
37483448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
37493448Sdh155122 esp1dbg(espstack, ("Couldn't find encr alg #%d.\n",
37500Sstevel@tonic-gate assoc->sadb_sa_encrypt));
37510Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_EALG;
37520Sstevel@tonic-gate return (EINVAL);
37530Sstevel@tonic-gate }
37542810Smarkfen
37552810Smarkfen /*
37562810Smarkfen * Sanity check key sizes. If the encryption algorithm is
37572810Smarkfen * SADB_EALG_NULL but the encryption key is NOT
37582810Smarkfen * NULL then complain.
375910824SMark.Fenwick@Sun.COM *
376010824SMark.Fenwick@Sun.COM * The keying material includes salt bits if required by
376110824SMark.Fenwick@Sun.COM * algorithm and optionally the Initial IV, check the
376210824SMark.Fenwick@Sun.COM * length of whats left.
37632810Smarkfen */
376410824SMark.Fenwick@Sun.COM keybits = ekey->sadb_key_bits;
376510824SMark.Fenwick@Sun.COM keybits -= ekey->sadb_key_reserved;
376610824SMark.Fenwick@Sun.COM keybits -= SADB_8TO1(ealg->alg_saltlen);
37672810Smarkfen if ((assoc->sadb_sa_encrypt == SADB_EALG_NULL) ||
376810824SMark.Fenwick@Sun.COM (!ipsec_valid_key_size(keybits, ealg))) {
37693448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
37700Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_BAD_EKEYBITS;
37710Sstevel@tonic-gate return (EINVAL);
37720Sstevel@tonic-gate }
37732810Smarkfen ASSERT(ealg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
37740Sstevel@tonic-gate
37750Sstevel@tonic-gate /* check key */
37760Sstevel@tonic-gate if (ipsec_check_key(ealg->alg_mech_type, ekey, B_FALSE,
37770Sstevel@tonic-gate diagnostic) != 0) {
37783448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
37790Sstevel@tonic-gate return (EINVAL);
37800Sstevel@tonic-gate }
37810Sstevel@tonic-gate }
37823448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
37830Sstevel@tonic-gate
37843055Sdanmcd return (esp_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi,
37854987Sdanmcd diagnostic, espstack));
37860Sstevel@tonic-gate }
37870Sstevel@tonic-gate
37880Sstevel@tonic-gate /*
37890Sstevel@tonic-gate * Update a security association. Updates come in two varieties. The first
37900Sstevel@tonic-gate * is an update of lifetimes on a non-larval SA. The second is an update of
37910Sstevel@tonic-gate * a larval SA, which ends up looking a lot more like an add.
37920Sstevel@tonic-gate */
37930Sstevel@tonic-gate static int
37943448Sdh155122 esp_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
37956668Smarkfen ipsecesp_stack_t *espstack, uint8_t sadb_msg_type)
37960Sstevel@tonic-gate {
37977749SThejaswini.Singarajipura@Sun.COM sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
37987749SThejaswini.Singarajipura@Sun.COM mblk_t *buf_pkt;
37997749SThejaswini.Singarajipura@Sun.COM int rcode;
38007749SThejaswini.Singarajipura@Sun.COM
38010Sstevel@tonic-gate sadb_address_t *dstext =
38020Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
38030Sstevel@tonic-gate
38040Sstevel@tonic-gate if (dstext == NULL) {
38050Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
38060Sstevel@tonic-gate return (EINVAL);
38070Sstevel@tonic-gate }
38080Sstevel@tonic-gate
38097749SThejaswini.Singarajipura@Sun.COM rcode = sadb_update_sa(mp, ksi, &buf_pkt, &espstack->esp_sadb,
38107749SThejaswini.Singarajipura@Sun.COM diagnostic, espstack->esp_pfkey_q, esp_add_sa,
38117749SThejaswini.Singarajipura@Sun.COM espstack->ipsecesp_netstack, sadb_msg_type);
38127749SThejaswini.Singarajipura@Sun.COM
38137749SThejaswini.Singarajipura@Sun.COM if ((assoc->sadb_sa_state != SADB_X_SASTATE_ACTIVE) ||
38147749SThejaswini.Singarajipura@Sun.COM (rcode != 0)) {
38157749SThejaswini.Singarajipura@Sun.COM return (rcode);
38167749SThejaswini.Singarajipura@Sun.COM }
38177749SThejaswini.Singarajipura@Sun.COM
38188704Sdanmcd@sun.com HANDLE_BUF_PKT(esp_taskq, espstack->ipsecesp_netstack->netstack_ipsec,
38197749SThejaswini.Singarajipura@Sun.COM espstack->esp_dropper, buf_pkt);
38207749SThejaswini.Singarajipura@Sun.COM
38217749SThejaswini.Singarajipura@Sun.COM return (rcode);
38220Sstevel@tonic-gate }
38230Sstevel@tonic-gate
382410934Ssommerfeld@sun.com /* XXX refactor me */
38250Sstevel@tonic-gate /*
38260Sstevel@tonic-gate * Delete a security association. This is REALLY likely to be code common to
38270Sstevel@tonic-gate * both AH and ESP. Find the association, then unlink it.
38280Sstevel@tonic-gate */
38290Sstevel@tonic-gate static int
38303448Sdh155122 esp_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
38316668Smarkfen ipsecesp_stack_t *espstack, uint8_t sadb_msg_type)
38320Sstevel@tonic-gate {
38330Sstevel@tonic-gate sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
38340Sstevel@tonic-gate sadb_address_t *dstext =
38350Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
38360Sstevel@tonic-gate sadb_address_t *srcext =
38370Sstevel@tonic-gate (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
38380Sstevel@tonic-gate struct sockaddr_in *sin;
38390Sstevel@tonic-gate
38400Sstevel@tonic-gate if (assoc == NULL) {
38410Sstevel@tonic-gate if (dstext != NULL) {
38420Sstevel@tonic-gate sin = (struct sockaddr_in *)(dstext + 1);
38430Sstevel@tonic-gate } else if (srcext != NULL) {
38440Sstevel@tonic-gate sin = (struct sockaddr_in *)(srcext + 1);
38450Sstevel@tonic-gate } else {
38460Sstevel@tonic-gate *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
38470Sstevel@tonic-gate return (EINVAL);
38480Sstevel@tonic-gate }
38493055Sdanmcd return (sadb_purge_sa(mp, ksi,
38503448Sdh155122 (sin->sin_family == AF_INET6) ? &espstack->esp_sadb.s_v6 :
385110934Ssommerfeld@sun.com &espstack->esp_sadb.s_v4, diagnostic,
385211042SErik.Nordmark@Sun.COM espstack->esp_pfkey_q));
38530Sstevel@tonic-gate }
38540Sstevel@tonic-gate
38556668Smarkfen return (sadb_delget_sa(mp, ksi, &espstack->esp_sadb, diagnostic,
38566668Smarkfen espstack->esp_pfkey_q, sadb_msg_type));
38570Sstevel@tonic-gate }
38580Sstevel@tonic-gate
385910934Ssommerfeld@sun.com /* XXX refactor me */
38600Sstevel@tonic-gate /*
38610Sstevel@tonic-gate * Convert the entire contents of all of ESP's SA tables into PF_KEY SADB_DUMP
38620Sstevel@tonic-gate * messages.
38630Sstevel@tonic-gate */
38640Sstevel@tonic-gate static void
38653448Sdh155122 esp_dump(mblk_t *mp, keysock_in_t *ksi, ipsecesp_stack_t *espstack)
38660Sstevel@tonic-gate {
38670Sstevel@tonic-gate int error;
38680Sstevel@tonic-gate sadb_msg_t *samsg;
38690Sstevel@tonic-gate
38700Sstevel@tonic-gate /*
38710Sstevel@tonic-gate * Dump each fanout, bailing if error is non-zero.
38720Sstevel@tonic-gate */
38730Sstevel@tonic-gate
38747749SThejaswini.Singarajipura@Sun.COM error = sadb_dump(espstack->esp_pfkey_q, mp, ksi,
38753448Sdh155122 &espstack->esp_sadb.s_v4);
38760Sstevel@tonic-gate if (error != 0)
38770Sstevel@tonic-gate goto bail;
38780Sstevel@tonic-gate
38797749SThejaswini.Singarajipura@Sun.COM error = sadb_dump(espstack->esp_pfkey_q, mp, ksi,
38803448Sdh155122 &espstack->esp_sadb.s_v6);
38810Sstevel@tonic-gate bail:
38820Sstevel@tonic-gate ASSERT(mp->b_cont != NULL);
38830Sstevel@tonic-gate samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
38840Sstevel@tonic-gate samsg->sadb_msg_errno = (uint8_t)error;
38853448Sdh155122 sadb_pfkey_echo(espstack->esp_pfkey_q, mp,
38863448Sdh155122 (sadb_msg_t *)mp->b_cont->b_rptr, ksi, NULL);
38870Sstevel@tonic-gate }
38880Sstevel@tonic-gate
38890Sstevel@tonic-gate /*
38903055Sdanmcd * First-cut reality check for an inbound PF_KEY message.
38913055Sdanmcd */
38923055Sdanmcd static boolean_t
38933448Sdh155122 esp_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi,
38943448Sdh155122 ipsecesp_stack_t *espstack)
38953055Sdanmcd {
38963055Sdanmcd int diagnostic;
38973055Sdanmcd
38983055Sdanmcd if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) {
38993055Sdanmcd diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT;
39003055Sdanmcd goto badmsg;
39013055Sdanmcd }
39023055Sdanmcd if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL ||
39033055Sdanmcd ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) {
39043055Sdanmcd diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT;
39053055Sdanmcd goto badmsg;
39063055Sdanmcd }
39073055Sdanmcd return (B_FALSE); /* False ==> no failures */
39083055Sdanmcd
39093055Sdanmcd badmsg:
39103448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, EINVAL, diagnostic,
39113055Sdanmcd ksi->ks_in_serial);
39123055Sdanmcd return (B_TRUE); /* True ==> failures */
39133055Sdanmcd }
39143055Sdanmcd
39153055Sdanmcd /*
39160Sstevel@tonic-gate * ESP parsing of PF_KEY messages. Keysock did most of the really silly
39170Sstevel@tonic-gate * error cases. What I receive is a fully-formed, syntactically legal
39180Sstevel@tonic-gate * PF_KEY message. I then need to check semantics...
39190Sstevel@tonic-gate *
39200Sstevel@tonic-gate * This code may become common to AH and ESP. Stay tuned.
39210Sstevel@tonic-gate *
39220Sstevel@tonic-gate * I also make the assumption that db_ref's are cool. If this assumption
39230Sstevel@tonic-gate * is wrong, this means that someone other than keysock or me has been
39240Sstevel@tonic-gate * mucking with PF_KEY messages.
39250Sstevel@tonic-gate */
39260Sstevel@tonic-gate static void
39273448Sdh155122 esp_parse_pfkey(mblk_t *mp, ipsecesp_stack_t *espstack)
39280Sstevel@tonic-gate {
39290Sstevel@tonic-gate mblk_t *msg = mp->b_cont;
39300Sstevel@tonic-gate sadb_msg_t *samsg;
39310Sstevel@tonic-gate keysock_in_t *ksi;
39320Sstevel@tonic-gate int error;
39330Sstevel@tonic-gate int diagnostic = SADB_X_DIAGNOSTIC_NONE;
39340Sstevel@tonic-gate
39350Sstevel@tonic-gate ASSERT(msg != NULL);
39363448Sdh155122
39370Sstevel@tonic-gate samsg = (sadb_msg_t *)msg->b_rptr;
39380Sstevel@tonic-gate ksi = (keysock_in_t *)mp->b_rptr;
39390Sstevel@tonic-gate
39400Sstevel@tonic-gate /*
39410Sstevel@tonic-gate * If applicable, convert unspecified AF_INET6 to unspecified
39423055Sdanmcd * AF_INET. And do other address reality checks.
39430Sstevel@tonic-gate */
39443448Sdh155122 if (!sadb_addrfix(ksi, espstack->esp_pfkey_q, mp,
39453448Sdh155122 espstack->ipsecesp_netstack) ||
39463448Sdh155122 esp_pfkey_reality_failures(mp, ksi, espstack)) {
39473055Sdanmcd return;
39483055Sdanmcd }
39490Sstevel@tonic-gate
39500Sstevel@tonic-gate switch (samsg->sadb_msg_type) {
39510Sstevel@tonic-gate case SADB_ADD:
39523448Sdh155122 error = esp_add_sa(mp, ksi, &diagnostic,
39533448Sdh155122 espstack->ipsecesp_netstack);
39540Sstevel@tonic-gate if (error != 0) {
39553448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
39563448Sdh155122 diagnostic, ksi->ks_in_serial);
39570Sstevel@tonic-gate }
39580Sstevel@tonic-gate /* else esp_add_sa() took care of things. */
39590Sstevel@tonic-gate break;
39600Sstevel@tonic-gate case SADB_DELETE:
39616668Smarkfen case SADB_X_DELPAIR:
39627749SThejaswini.Singarajipura@Sun.COM case SADB_X_DELPAIR_STATE:
39636668Smarkfen error = esp_del_sa(mp, ksi, &diagnostic, espstack,
39646668Smarkfen samsg->sadb_msg_type);
39650Sstevel@tonic-gate if (error != 0) {
39663448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
39673448Sdh155122 diagnostic, ksi->ks_in_serial);
39680Sstevel@tonic-gate }
39690Sstevel@tonic-gate /* Else esp_del_sa() took care of things. */
39700Sstevel@tonic-gate break;
39710Sstevel@tonic-gate case SADB_GET:
39726668Smarkfen error = sadb_delget_sa(mp, ksi, &espstack->esp_sadb,
39736668Smarkfen &diagnostic, espstack->esp_pfkey_q, samsg->sadb_msg_type);
39740Sstevel@tonic-gate if (error != 0) {
39753448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
39763448Sdh155122 diagnostic, ksi->ks_in_serial);
39770Sstevel@tonic-gate }
39780Sstevel@tonic-gate /* Else sadb_get_sa() took care of things. */
39790Sstevel@tonic-gate break;
39800Sstevel@tonic-gate case SADB_FLUSH:
39813448Sdh155122 sadbp_flush(&espstack->esp_sadb, espstack->ipsecesp_netstack);
39823448Sdh155122 sadb_pfkey_echo(espstack->esp_pfkey_q, mp, samsg, ksi, NULL);
39830Sstevel@tonic-gate break;
39840Sstevel@tonic-gate case SADB_REGISTER:
39850Sstevel@tonic-gate /*
39860Sstevel@tonic-gate * Hmmm, let's do it! Check for extensions (there should
39870Sstevel@tonic-gate * be none), extract the fields, call esp_register_out(),
39880Sstevel@tonic-gate * then either free or report an error.
39890Sstevel@tonic-gate *
39900Sstevel@tonic-gate * Keysock takes care of the PF_KEY bookkeeping for this.
39910Sstevel@tonic-gate */
39920Sstevel@tonic-gate if (esp_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid,
399311042SErik.Nordmark@Sun.COM ksi->ks_in_serial, espstack, msg_getcred(mp, NULL))) {
39940Sstevel@tonic-gate freemsg(mp);
39950Sstevel@tonic-gate } else {
39960Sstevel@tonic-gate /*
39970Sstevel@tonic-gate * Only way this path hits is if there is a memory
39980Sstevel@tonic-gate * failure. It will not return B_FALSE because of
39990Sstevel@tonic-gate * lack of esp_pfkey_q if I am in wput().
40000Sstevel@tonic-gate */
40013448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, ENOMEM,
40023448Sdh155122 diagnostic, ksi->ks_in_serial);
40030Sstevel@tonic-gate }
40040Sstevel@tonic-gate break;
40050Sstevel@tonic-gate case SADB_UPDATE:
40066668Smarkfen case SADB_X_UPDATEPAIR:
40070Sstevel@tonic-gate /*
40080Sstevel@tonic-gate * Find a larval, if not there, find a full one and get
40090Sstevel@tonic-gate * strict.
40100Sstevel@tonic-gate */
40116668Smarkfen error = esp_update_sa(mp, ksi, &diagnostic, espstack,
40126668Smarkfen samsg->sadb_msg_type);
40130Sstevel@tonic-gate if (error != 0) {
40143448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, error,
40153448Sdh155122 diagnostic, ksi->ks_in_serial);
40160Sstevel@tonic-gate }
40170Sstevel@tonic-gate /* else esp_update_sa() took care of things. */
40180Sstevel@tonic-gate break;
40190Sstevel@tonic-gate case SADB_GETSPI:
40200Sstevel@tonic-gate /*
40210Sstevel@tonic-gate * Reserve a new larval entry.
40220Sstevel@tonic-gate */
40233448Sdh155122 esp_getspi(mp, ksi, espstack);
40240Sstevel@tonic-gate break;
40250Sstevel@tonic-gate case SADB_ACQUIRE:
40260Sstevel@tonic-gate /*
40270Sstevel@tonic-gate * Find larval and/or ACQUIRE record and kill it (them), I'm
40280Sstevel@tonic-gate * most likely an error. Inbound ACQUIRE messages should only
40290Sstevel@tonic-gate * have the base header.
40300Sstevel@tonic-gate */
40313448Sdh155122 sadb_in_acquire(samsg, &espstack->esp_sadb,
40323448Sdh155122 espstack->esp_pfkey_q, espstack->ipsecesp_netstack);
40330Sstevel@tonic-gate freemsg(mp);
40340Sstevel@tonic-gate break;
40350Sstevel@tonic-gate case SADB_DUMP:
40360Sstevel@tonic-gate /*
40370Sstevel@tonic-gate * Dump all entries.
40380Sstevel@tonic-gate */
40393448Sdh155122 esp_dump(mp, ksi, espstack);
40400Sstevel@tonic-gate /* esp_dump will take care of the return message, etc. */
40410Sstevel@tonic-gate break;
40420Sstevel@tonic-gate case SADB_EXPIRE:
40430Sstevel@tonic-gate /* Should never reach me. */
40443448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, EOPNOTSUPP,
40453448Sdh155122 diagnostic, ksi->ks_in_serial);
40460Sstevel@tonic-gate break;
40470Sstevel@tonic-gate default:
40483448Sdh155122 sadb_pfkey_error(espstack->esp_pfkey_q, mp, EINVAL,
40490Sstevel@tonic-gate SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial);
40500Sstevel@tonic-gate break;
40510Sstevel@tonic-gate }
40520Sstevel@tonic-gate }
40530Sstevel@tonic-gate
40540Sstevel@tonic-gate /*
40550Sstevel@tonic-gate * Handle case where PF_KEY says it can't find a keysock for one of my
40560Sstevel@tonic-gate * ACQUIRE messages.
40570Sstevel@tonic-gate */
40580Sstevel@tonic-gate static void
40593448Sdh155122 esp_keysock_no_socket(mblk_t *mp, ipsecesp_stack_t *espstack)
40600Sstevel@tonic-gate {
40610Sstevel@tonic-gate sadb_msg_t *samsg;
40620Sstevel@tonic-gate keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr;
40630Sstevel@tonic-gate
40640Sstevel@tonic-gate if (mp->b_cont == NULL) {
40650Sstevel@tonic-gate freemsg(mp);
40660Sstevel@tonic-gate return;
40670Sstevel@tonic-gate }
40680Sstevel@tonic-gate samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
40690Sstevel@tonic-gate
40700Sstevel@tonic-gate /*
40710Sstevel@tonic-gate * If keysock can't find any registered, delete the acquire record
40720Sstevel@tonic-gate * immediately, and handle errors.
40730Sstevel@tonic-gate */
40740Sstevel@tonic-gate if (samsg->sadb_msg_type == SADB_ACQUIRE) {
40750Sstevel@tonic-gate samsg->sadb_msg_errno = kse->ks_err_errno;
40760Sstevel@tonic-gate samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg));
40770Sstevel@tonic-gate /*
407811042SErik.Nordmark@Sun.COM * Use the write-side of the esp_pfkey_q
40790Sstevel@tonic-gate */
40803448Sdh155122 sadb_in_acquire(samsg, &espstack->esp_sadb,
40813448Sdh155122 WR(espstack->esp_pfkey_q), espstack->ipsecesp_netstack);
40820Sstevel@tonic-gate }
40830Sstevel@tonic-gate
40840Sstevel@tonic-gate freemsg(mp);
40850Sstevel@tonic-gate }
40860Sstevel@tonic-gate
40870Sstevel@tonic-gate /*
40880Sstevel@tonic-gate * ESP module write put routine.
40890Sstevel@tonic-gate */
40900Sstevel@tonic-gate static void
40910Sstevel@tonic-gate ipsecesp_wput(queue_t *q, mblk_t *mp)
40920Sstevel@tonic-gate {
40930Sstevel@tonic-gate ipsec_info_t *ii;
40940Sstevel@tonic-gate struct iocblk *iocp;
40953448Sdh155122 ipsecesp_stack_t *espstack = (ipsecesp_stack_t *)q->q_ptr;
40963448Sdh155122
40973448Sdh155122 esp3dbg(espstack, ("In esp_wput().\n"));
40980Sstevel@tonic-gate
40990Sstevel@tonic-gate /* NOTE: Each case must take care of freeing or passing mp. */
41000Sstevel@tonic-gate switch (mp->b_datap->db_type) {
41010Sstevel@tonic-gate case M_CTL:
41020Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) {
41030Sstevel@tonic-gate /* Not big enough message. */
41040Sstevel@tonic-gate freemsg(mp);
41050Sstevel@tonic-gate break;
41060Sstevel@tonic-gate }
41070Sstevel@tonic-gate ii = (ipsec_info_t *)mp->b_rptr;
41080Sstevel@tonic-gate
41090Sstevel@tonic-gate switch (ii->ipsec_info_type) {
41100Sstevel@tonic-gate case KEYSOCK_OUT_ERR:
41113448Sdh155122 esp1dbg(espstack, ("Got KEYSOCK_OUT_ERR message.\n"));
41123448Sdh155122 esp_keysock_no_socket(mp, espstack);
41130Sstevel@tonic-gate break;
41140Sstevel@tonic-gate case KEYSOCK_IN:
41153448Sdh155122 ESP_BUMP_STAT(espstack, keysock_in);
41163448Sdh155122 esp3dbg(espstack, ("Got KEYSOCK_IN message.\n"));
41173055Sdanmcd
41183055Sdanmcd /* Parse the message. */
41193448Sdh155122 esp_parse_pfkey(mp, espstack);
41200Sstevel@tonic-gate break;
41210Sstevel@tonic-gate case KEYSOCK_HELLO:
41223448Sdh155122 sadb_keysock_hello(&espstack->esp_pfkey_q, q, mp,
41233448Sdh155122 esp_ager, (void *)espstack, &espstack->esp_event,
41243448Sdh155122 SADB_SATYPE_ESP);
41250Sstevel@tonic-gate break;
41260Sstevel@tonic-gate default:
41273448Sdh155122 esp2dbg(espstack, ("Got M_CTL from above of 0x%x.\n",
41280Sstevel@tonic-gate ii->ipsec_info_type));
41290Sstevel@tonic-gate freemsg(mp);
41300Sstevel@tonic-gate break;
41310Sstevel@tonic-gate }
41320Sstevel@tonic-gate break;
41330Sstevel@tonic-gate case M_IOCTL:
41340Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr;
41350Sstevel@tonic-gate switch (iocp->ioc_cmd) {
41360Sstevel@tonic-gate case ND_SET:
41370Sstevel@tonic-gate case ND_GET:
41383448Sdh155122 if (nd_getset(q, espstack->ipsecesp_g_nd, mp)) {
41390Sstevel@tonic-gate qreply(q, mp);
41400Sstevel@tonic-gate return;
41410Sstevel@tonic-gate } else {
41420Sstevel@tonic-gate iocp->ioc_error = ENOENT;
41430Sstevel@tonic-gate }
41440Sstevel@tonic-gate /* FALLTHRU */
41450Sstevel@tonic-gate default:
41460Sstevel@tonic-gate /* We really don't support any other ioctls, do we? */
41470Sstevel@tonic-gate
41480Sstevel@tonic-gate /* Return EINVAL */
41490Sstevel@tonic-gate if (iocp->ioc_error != ENOENT)
41500Sstevel@tonic-gate iocp->ioc_error = EINVAL;
41510Sstevel@tonic-gate iocp->ioc_count = 0;
41520Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK;
41530Sstevel@tonic-gate qreply(q, mp);
41540Sstevel@tonic-gate return;
41550Sstevel@tonic-gate }
41560Sstevel@tonic-gate default:
41573448Sdh155122 esp3dbg(espstack,
41583448Sdh155122 ("Got default message, type %d, passing to IP.\n",
41590Sstevel@tonic-gate mp->b_datap->db_type));
41600Sstevel@tonic-gate putnext(q, mp);
41610Sstevel@tonic-gate }
41620Sstevel@tonic-gate }
41630Sstevel@tonic-gate
41640Sstevel@tonic-gate /*
41650Sstevel@tonic-gate * Wrapper to allow IP to trigger an ESP association failure message
41660Sstevel@tonic-gate * during inbound SA selection.
41670Sstevel@tonic-gate */
41680Sstevel@tonic-gate void
41690Sstevel@tonic-gate ipsecesp_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt,
417011042SErik.Nordmark@Sun.COM uint32_t spi, void *addr, int af, ip_recv_attr_t *ira)
41710Sstevel@tonic-gate {
417211042SErik.Nordmark@Sun.COM netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
417311042SErik.Nordmark@Sun.COM ipsecesp_stack_t *espstack = ns->netstack_ipsecesp;
417411042SErik.Nordmark@Sun.COM ipsec_stack_t *ipss = ns->netstack_ipsec;
41753448Sdh155122
41763448Sdh155122 if (espstack->ipsecesp_log_unknown_spi) {
41770Sstevel@tonic-gate ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi,
41783448Sdh155122 addr, af, espstack->ipsecesp_netstack);
41790Sstevel@tonic-gate }
41800Sstevel@tonic-gate
418111042SErik.Nordmark@Sun.COM ip_drop_packet(mp, B_TRUE, ira->ira_ill,
41823448Sdh155122 DROPPER(ipss, ipds_esp_no_sa),
41833448Sdh155122 &espstack->esp_dropper);
41840Sstevel@tonic-gate }
41850Sstevel@tonic-gate
41860Sstevel@tonic-gate /*
41870Sstevel@tonic-gate * Initialize the ESP input and output processing functions.
41880Sstevel@tonic-gate */
41890Sstevel@tonic-gate void
41900Sstevel@tonic-gate ipsecesp_init_funcs(ipsa_t *sa)
41910Sstevel@tonic-gate {
41920Sstevel@tonic-gate if (sa->ipsa_output_func == NULL)
41930Sstevel@tonic-gate sa->ipsa_output_func = esp_outbound;
41940Sstevel@tonic-gate if (sa->ipsa_input_func == NULL)
41950Sstevel@tonic-gate sa->ipsa_input_func = esp_inbound;
41960Sstevel@tonic-gate }
4197