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 53055Sdanmcd * Common Development and Distribution License (the "License"). 63055Sdanmcd * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*3448Sdh155122 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/param.h> 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/stream.h> 310Sstevel@tonic-gate #include <sys/strsubr.h> 320Sstevel@tonic-gate #include <sys/strsun.h> 330Sstevel@tonic-gate #include <sys/stropts.h> 34*3448Sdh155122 #include <sys/zone.h> 350Sstevel@tonic-gate #include <sys/vnode.h> 360Sstevel@tonic-gate #include <sys/sysmacros.h> 370Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 380Sstevel@tonic-gate #include <sys/tihdr.h> 390Sstevel@tonic-gate #include <sys/ddi.h> 400Sstevel@tonic-gate #include <sys/sunddi.h> 410Sstevel@tonic-gate #include <sys/mkdev.h> 420Sstevel@tonic-gate #include <sys/debug.h> 430Sstevel@tonic-gate #include <sys/kmem.h> 440Sstevel@tonic-gate #include <sys/cmn_err.h> 450Sstevel@tonic-gate #include <sys/suntpi.h> 460Sstevel@tonic-gate #include <sys/policy.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate #include <sys/socket.h> 490Sstevel@tonic-gate #include <netinet/in.h> 500Sstevel@tonic-gate #include <net/pfkeyv2.h> 510Sstevel@tonic-gate #include <net/pfpolicy.h> 520Sstevel@tonic-gate 530Sstevel@tonic-gate #include <inet/common.h> 540Sstevel@tonic-gate #include <netinet/ip6.h> 550Sstevel@tonic-gate #include <inet/ip.h> 560Sstevel@tonic-gate #include <inet/ip6.h> 570Sstevel@tonic-gate #include <inet/mi.h> 580Sstevel@tonic-gate #include <inet/nd.h> 593055Sdanmcd #include <inet/ip_if.h> 603055Sdanmcd #include <inet/tun.h> 610Sstevel@tonic-gate #include <inet/optcom.h> 620Sstevel@tonic-gate #include <inet/ipsec_info.h> 630Sstevel@tonic-gate #include <inet/ipsec_impl.h> 640Sstevel@tonic-gate #include <inet/spdsock.h> 650Sstevel@tonic-gate #include <inet/sadb.h> 660Sstevel@tonic-gate 670Sstevel@tonic-gate #include <sys/isa_defs.h> 680Sstevel@tonic-gate 690Sstevel@tonic-gate /* 700Sstevel@tonic-gate * This is a transport provider for the PF_POLICY IPsec policy 710Sstevel@tonic-gate * management socket, which provides a management interface into the 720Sstevel@tonic-gate * SPD, allowing policy rules to be added, deleted, and queried. 730Sstevel@tonic-gate * 740Sstevel@tonic-gate * This effectively replaces the old private SIOC*IPSECONFIG ioctls 750Sstevel@tonic-gate * with an extensible interface which will hopefully be public some 760Sstevel@tonic-gate * day. 770Sstevel@tonic-gate * 780Sstevel@tonic-gate * See <net/pfpolicy.h> for more details on the protocol. 790Sstevel@tonic-gate * 800Sstevel@tonic-gate * We link against drv/ip and call directly into it to manipulate the 810Sstevel@tonic-gate * SPD; see ipsec_impl.h for the policy data structures and spd.c for 820Sstevel@tonic-gate * the code which maintains them. 830Sstevel@tonic-gate * 840Sstevel@tonic-gate * The MT model of this is QPAIR with the addition of some explicit 850Sstevel@tonic-gate * locking to protect system-wide policy data structures. 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate 880Sstevel@tonic-gate static vmem_t *spdsock_vmem; /* for minor numbers. */ 890Sstevel@tonic-gate 900Sstevel@tonic-gate #define ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t)) 910Sstevel@tonic-gate 920Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages (from rts.c...) */ 930Sstevel@tonic-gate static struct T_info_ack spdsock_g_t_info_ack = { 940Sstevel@tonic-gate T_INFO_ACK, 950Sstevel@tonic-gate T_INFINITE, /* TSDU_size. Maximum size messages. */ 960Sstevel@tonic-gate T_INVALID, /* ETSDU_size. No expedited data. */ 970Sstevel@tonic-gate T_INVALID, /* CDATA_size. No connect data. */ 980Sstevel@tonic-gate T_INVALID, /* DDATA_size. No disconnect data. */ 990Sstevel@tonic-gate 0, /* ADDR_size. */ 1000Sstevel@tonic-gate 0, /* OPT_size. No user-settable options */ 1010Sstevel@tonic-gate 64 * 1024, /* TIDU_size. spdsock allows maximum size messages. */ 1020Sstevel@tonic-gate T_COTS, /* SERV_type. spdsock supports connection oriented. */ 1030Sstevel@tonic-gate TS_UNBND, /* CURRENT_state. This is set from spdsock_state. */ 1040Sstevel@tonic-gate (XPG4_1) /* Provider flags */ 1050Sstevel@tonic-gate }; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */ 108*3448Sdh155122 typedef struct spdsockparam_s { 1090Sstevel@tonic-gate uint_t spdsock_param_min; 1100Sstevel@tonic-gate uint_t spdsock_param_max; 1110Sstevel@tonic-gate uint_t spdsock_param_value; 1120Sstevel@tonic-gate char *spdsock_param_name; 1130Sstevel@tonic-gate } spdsockparam_t; 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate /* 1160Sstevel@tonic-gate * Table of NDD variables supported by spdsock. These are loaded into 1170Sstevel@tonic-gate * spdsock_g_nd in spdsock_init_nd. 1180Sstevel@tonic-gate * All of these are alterable, within the min/max values given, at run time. 1190Sstevel@tonic-gate */ 120*3448Sdh155122 static spdsockparam_t lcl_param_arr[] = { 1210Sstevel@tonic-gate /* min max value name */ 1220Sstevel@tonic-gate { 4096, 65536, 8192, "spdsock_xmit_hiwat"}, 1230Sstevel@tonic-gate { 0, 65536, 1024, "spdsock_xmit_lowat"}, 1240Sstevel@tonic-gate { 4096, 65536, 8192, "spdsock_recv_hiwat"}, 1250Sstevel@tonic-gate { 65536, 1024*1024*1024, 256*1024, "spdsock_max_buf"}, 1260Sstevel@tonic-gate { 0, 3, 0, "spdsock_debug"}, 1270Sstevel@tonic-gate }; 128*3448Sdh155122 #define spds_xmit_hiwat spds_params[0].spdsock_param_value 129*3448Sdh155122 #define spds_xmit_lowat spds_params[1].spdsock_param_value 130*3448Sdh155122 #define spds_recv_hiwat spds_params[2].spdsock_param_value 131*3448Sdh155122 #define spds_max_buf spds_params[3].spdsock_param_value 132*3448Sdh155122 #define spds_debug spds_params[4].spdsock_param_value 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate #define ss0dbg(a) printf a 1350Sstevel@tonic-gate /* NOTE: != 0 instead of > 0 so lint doesn't complain. */ 136*3448Sdh155122 #define ss1dbg(spds, a) if (spds->spds_debug != 0) printf a 137*3448Sdh155122 #define ss2dbg(spds, a) if (spds->spds_debug > 1) printf a 138*3448Sdh155122 #define ss3dbg(spds, a) if (spds->spds_debug > 2) printf a 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate static int spdsock_close(queue_t *); 1410Sstevel@tonic-gate static int spdsock_open(queue_t *, dev_t *, int, int, cred_t *); 1420Sstevel@tonic-gate static void spdsock_wput(queue_t *, mblk_t *); 1430Sstevel@tonic-gate static void spdsock_wsrv(queue_t *); 1440Sstevel@tonic-gate static void spdsock_rsrv(queue_t *); 145*3448Sdh155122 static void *spdsock_stack_init(netstackid_t stackid, netstack_t *ns); 146*3448Sdh155122 static void spdsock_stack_fini(netstackid_t stackid, void *arg); 1470Sstevel@tonic-gate static void spdsock_loadcheck(void *); 148*3448Sdh155122 static void spdsock_merge_algs(spd_stack_t *); 149*3448Sdh155122 static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *); 1503055Sdanmcd static mblk_t *spdsock_dump_next_record(spdsock_t *); 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate static struct module_info info = { 1530Sstevel@tonic-gate 5138, "spdsock", 1, INFPSZ, 512, 128 1540Sstevel@tonic-gate }; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate static struct qinit rinit = { 1570Sstevel@tonic-gate NULL, (pfi_t)spdsock_rsrv, spdsock_open, spdsock_close, 1580Sstevel@tonic-gate NULL, &info 1590Sstevel@tonic-gate }; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate static struct qinit winit = { 1620Sstevel@tonic-gate (pfi_t)spdsock_wput, (pfi_t)spdsock_wsrv, NULL, NULL, NULL, &info 1630Sstevel@tonic-gate }; 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate struct streamtab spdsockinfo = { 1660Sstevel@tonic-gate &rinit, &winit 1670Sstevel@tonic-gate }; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate /* mapping from alg type to protocol number, as per RFC 2407 */ 1700Sstevel@tonic-gate static const uint_t algproto[] = { 1710Sstevel@tonic-gate PROTO_IPSEC_AH, 1720Sstevel@tonic-gate PROTO_IPSEC_ESP, 1730Sstevel@tonic-gate }; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate #define NALGPROTOS (sizeof (algproto) / sizeof (algproto[0])) 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate /* mapping from kernel exec mode to spdsock exec mode */ 1780Sstevel@tonic-gate static const uint_t execmodes[] = { 1790Sstevel@tonic-gate SPD_ALG_EXEC_MODE_SYNC, 1800Sstevel@tonic-gate SPD_ALG_EXEC_MODE_ASYNC 1810Sstevel@tonic-gate }; 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate #define NEXECMODES (sizeof (execmodes) / sizeof (execmodes[0])) 1840Sstevel@tonic-gate 1853055Sdanmcd #define ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1) 1863055Sdanmcd #define ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2) 1873055Sdanmcd 1880Sstevel@tonic-gate /* ARGSUSED */ 1890Sstevel@tonic-gate static int 1900Sstevel@tonic-gate spdsock_param_get(q, mp, cp, cr) 1910Sstevel@tonic-gate queue_t *q; 1920Sstevel@tonic-gate mblk_t *mp; 1930Sstevel@tonic-gate caddr_t cp; 1940Sstevel@tonic-gate cred_t *cr; 1950Sstevel@tonic-gate { 1960Sstevel@tonic-gate spdsockparam_t *spdsockpa = (spdsockparam_t *)cp; 1970Sstevel@tonic-gate uint_t value; 198*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 199*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 200*3448Sdh155122 201*3448Sdh155122 mutex_enter(&spds->spds_param_lock); 2020Sstevel@tonic-gate value = spdsockpa->spdsock_param_value; 203*3448Sdh155122 mutex_exit(&spds->spds_param_lock); 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate (void) mi_mpprintf(mp, "%u", value); 2060Sstevel@tonic-gate return (0); 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate /* This routine sets an NDD variable in a spdsockparam_t structure. */ 2100Sstevel@tonic-gate /* ARGSUSED */ 2110Sstevel@tonic-gate static int 2120Sstevel@tonic-gate spdsock_param_set(q, mp, value, cp, cr) 2130Sstevel@tonic-gate queue_t *q; 2140Sstevel@tonic-gate mblk_t *mp; 2150Sstevel@tonic-gate char *value; 2160Sstevel@tonic-gate caddr_t cp; 2170Sstevel@tonic-gate cred_t *cr; 2180Sstevel@tonic-gate { 2190Sstevel@tonic-gate ulong_t new_value; 2200Sstevel@tonic-gate spdsockparam_t *spdsockpa = (spdsockparam_t *)cp; 221*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 222*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* Convert the value from a string into a long integer. */ 2250Sstevel@tonic-gate if (ddi_strtoul(value, NULL, 10, &new_value) != 0) 2260Sstevel@tonic-gate return (EINVAL); 2270Sstevel@tonic-gate 228*3448Sdh155122 mutex_enter(&spds->spds_param_lock); 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * Fail the request if the new value does not lie within the 2310Sstevel@tonic-gate * required bounds. 2320Sstevel@tonic-gate */ 2330Sstevel@tonic-gate if (new_value < spdsockpa->spdsock_param_min || 2340Sstevel@tonic-gate new_value > spdsockpa->spdsock_param_max) { 235*3448Sdh155122 mutex_exit(&spds->spds_param_lock); 2360Sstevel@tonic-gate return (EINVAL); 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate /* Set the new value */ 2400Sstevel@tonic-gate spdsockpa->spdsock_param_value = new_value; 241*3448Sdh155122 mutex_exit(&spds->spds_param_lock); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate return (0); 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate 246*3448Sdh155122 /* 247*3448Sdh155122 * Initialize at module load time 248*3448Sdh155122 */ 2490Sstevel@tonic-gate boolean_t 2500Sstevel@tonic-gate spdsock_ddi_init(void) 2510Sstevel@tonic-gate { 2520Sstevel@tonic-gate spdsock_max_optsize = optcom_max_optsize( 2530Sstevel@tonic-gate spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt); 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1, 2560Sstevel@tonic-gate NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER); 2570Sstevel@tonic-gate 258*3448Sdh155122 /* 259*3448Sdh155122 * We want to be informed each time a stack is created or 260*3448Sdh155122 * destroyed in the kernel, so we can maintain the 261*3448Sdh155122 * set of spd_stack_t's. 262*3448Sdh155122 */ 263*3448Sdh155122 netstack_register(NS_SPDSOCK, spdsock_stack_init, NULL, 264*3448Sdh155122 spdsock_stack_fini); 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate return (B_TRUE); 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate 269*3448Sdh155122 /* 270*3448Sdh155122 * Walk through the param array specified registering each element with the 271*3448Sdh155122 * named dispatch handler. 272*3448Sdh155122 */ 273*3448Sdh155122 static boolean_t 274*3448Sdh155122 spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt) 275*3448Sdh155122 { 276*3448Sdh155122 for (; cnt-- > 0; ssp++) { 277*3448Sdh155122 if (ssp->spdsock_param_name != NULL && 278*3448Sdh155122 ssp->spdsock_param_name[0]) { 279*3448Sdh155122 if (!nd_load(ndp, 280*3448Sdh155122 ssp->spdsock_param_name, 281*3448Sdh155122 spdsock_param_get, spdsock_param_set, 282*3448Sdh155122 (caddr_t)ssp)) { 283*3448Sdh155122 nd_free(ndp); 284*3448Sdh155122 return (B_FALSE); 285*3448Sdh155122 } 286*3448Sdh155122 } 287*3448Sdh155122 } 288*3448Sdh155122 return (B_TRUE); 289*3448Sdh155122 } 290*3448Sdh155122 291*3448Sdh155122 /* 292*3448Sdh155122 * Initialize for each stack instance 293*3448Sdh155122 */ 294*3448Sdh155122 /* ARGSUSED */ 295*3448Sdh155122 static void * 296*3448Sdh155122 spdsock_stack_init(netstackid_t stackid, netstack_t *ns) 297*3448Sdh155122 { 298*3448Sdh155122 spd_stack_t *spds; 299*3448Sdh155122 spdsockparam_t *ssp; 300*3448Sdh155122 301*3448Sdh155122 spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP); 302*3448Sdh155122 spds->spds_netstack = ns; 303*3448Sdh155122 304*3448Sdh155122 ASSERT(spds->spds_g_nd == NULL); 305*3448Sdh155122 306*3448Sdh155122 ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 307*3448Sdh155122 spds->spds_params = ssp; 308*3448Sdh155122 bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr)); 309*3448Sdh155122 310*3448Sdh155122 (void) spdsock_param_register(&spds->spds_g_nd, ssp, 311*3448Sdh155122 A_CNT(lcl_param_arr)); 312*3448Sdh155122 313*3448Sdh155122 mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL); 314*3448Sdh155122 mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL); 315*3448Sdh155122 316*3448Sdh155122 return (spds); 317*3448Sdh155122 } 318*3448Sdh155122 3190Sstevel@tonic-gate void 3200Sstevel@tonic-gate spdsock_ddi_destroy(void) 3210Sstevel@tonic-gate { 3220Sstevel@tonic-gate vmem_destroy(spdsock_vmem); 323*3448Sdh155122 324*3448Sdh155122 netstack_unregister(NS_SPDSOCK); 325*3448Sdh155122 } 326*3448Sdh155122 327*3448Sdh155122 /* ARGSUSED */ 328*3448Sdh155122 static void 329*3448Sdh155122 spdsock_stack_fini(netstackid_t stackid, void *arg) 330*3448Sdh155122 { 331*3448Sdh155122 spd_stack_t *spds = (spd_stack_t *)arg; 332*3448Sdh155122 333*3448Sdh155122 mutex_destroy(&spds->spds_param_lock); 334*3448Sdh155122 mutex_destroy(&spds->spds_alg_lock); 335*3448Sdh155122 nd_free(&spds->spds_g_nd); 336*3448Sdh155122 kmem_free(spds->spds_params, sizeof (lcl_param_arr)); 337*3448Sdh155122 spds->spds_params = NULL; 338*3448Sdh155122 339*3448Sdh155122 kmem_free(spds, sizeof (*spds)); 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate /* 3430Sstevel@tonic-gate * NOTE: large quantities of this should be shared with keysock. 3440Sstevel@tonic-gate * Would be nice to combine some of this into a common module, but 3450Sstevel@tonic-gate * not possible given time pressures. 3460Sstevel@tonic-gate */ 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate /* 3490Sstevel@tonic-gate * High-level reality checking of extensions. 3500Sstevel@tonic-gate */ 3510Sstevel@tonic-gate /* ARGSUSED */ /* XXX */ 3520Sstevel@tonic-gate static boolean_t 3530Sstevel@tonic-gate ext_check(spd_ext_t *ext) 3540Sstevel@tonic-gate { 3553055Sdanmcd spd_if_t *tunname = (spd_if_t *)ext; 3563055Sdanmcd int i; 3573055Sdanmcd char *idstr; 3583055Sdanmcd 3593055Sdanmcd if (ext->spd_ext_type == SPD_EXT_TUN_NAME) { 3603055Sdanmcd /* (NOTE: Modified from SADB_EXT_IDENTITY..) */ 3613055Sdanmcd 3623055Sdanmcd /* 3633055Sdanmcd * Make sure the strings in these identities are 3643055Sdanmcd * null-terminated. Let's "proactively" null-terminate the 3653055Sdanmcd * string at the last byte if it's not terminated sooner. 3663055Sdanmcd */ 3673055Sdanmcd i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t); 3683055Sdanmcd idstr = (char *)(tunname + 1); 3693055Sdanmcd while (*idstr != '\0' && i > 0) { 3703055Sdanmcd i--; 3713055Sdanmcd idstr++; 3723055Sdanmcd } 3733055Sdanmcd if (i == 0) { 3743055Sdanmcd /* 3753055Sdanmcd * I.e., if the bozo user didn't NULL-terminate the 3763055Sdanmcd * string... 3773055Sdanmcd */ 3783055Sdanmcd idstr--; 3793055Sdanmcd *idstr = '\0'; 3803055Sdanmcd } 3813055Sdanmcd } 3820Sstevel@tonic-gate return (B_TRUE); /* For now... */ 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate /* Return values for spdsock_get_ext(). */ 3880Sstevel@tonic-gate #define KGE_OK 0 3890Sstevel@tonic-gate #define KGE_DUP 1 3900Sstevel@tonic-gate #define KGE_UNK 2 3910Sstevel@tonic-gate #define KGE_LEN 3 3920Sstevel@tonic-gate #define KGE_CHK 4 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate /* 3950Sstevel@tonic-gate * Parse basic extension headers and return in the passed-in pointer vector. 3960Sstevel@tonic-gate * Return values include: 3970Sstevel@tonic-gate * 3980Sstevel@tonic-gate * KGE_OK Everything's nice and parsed out. 3990Sstevel@tonic-gate * If there are no extensions, place NULL in extv[0]. 4000Sstevel@tonic-gate * KGE_DUP There is a duplicate extension. 4010Sstevel@tonic-gate * First instance in appropriate bin. First duplicate in 4020Sstevel@tonic-gate * extv[0]. 4030Sstevel@tonic-gate * KGE_UNK Unknown extension type encountered. extv[0] contains 4040Sstevel@tonic-gate * unknown header. 4050Sstevel@tonic-gate * KGE_LEN Extension length error. 4060Sstevel@tonic-gate * KGE_CHK High-level reality check failed on specific extension. 4070Sstevel@tonic-gate * 4080Sstevel@tonic-gate * My apologies for some of the pointer arithmetic in here. I'm thinking 4090Sstevel@tonic-gate * like an assembly programmer, yet trying to make the compiler happy. 4100Sstevel@tonic-gate */ 4110Sstevel@tonic-gate static int 4120Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize) 4130Sstevel@tonic-gate { 4140Sstevel@tonic-gate bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1)); 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate /* Use extv[0] as the "current working pointer". */ 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate extv[0] = (spd_ext_t *)(basehdr + 1); 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) { 4210Sstevel@tonic-gate /* Check for unknown headers. */ 4220Sstevel@tonic-gate if (extv[0]->spd_ext_type == 0 || 4230Sstevel@tonic-gate extv[0]->spd_ext_type > SPD_EXT_MAX) 4240Sstevel@tonic-gate return (KGE_UNK); 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate /* 4270Sstevel@tonic-gate * Check length. Use uint64_t because extlen is in units 4280Sstevel@tonic-gate * of 64-bit words. If length goes beyond the msgsize, 4290Sstevel@tonic-gate * return an error. (Zero length also qualifies here.) 4300Sstevel@tonic-gate */ 4310Sstevel@tonic-gate if (extv[0]->spd_ext_len == 0 || 4320Sstevel@tonic-gate (void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) > 4330Sstevel@tonic-gate (void *)((uint8_t *)basehdr + msgsize)) 4340Sstevel@tonic-gate return (KGE_LEN); 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate /* Check for redundant headers. */ 4370Sstevel@tonic-gate if (extv[extv[0]->spd_ext_type] != NULL) 4380Sstevel@tonic-gate return (KGE_DUP); 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate /* 4410Sstevel@tonic-gate * Reality check the extension if possible at the spdsock 4420Sstevel@tonic-gate * level. 4430Sstevel@tonic-gate */ 4440Sstevel@tonic-gate if (!ext_check(extv[0])) 4450Sstevel@tonic-gate return (KGE_CHK); 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate /* If I make it here, assign the appropriate bin. */ 4480Sstevel@tonic-gate extv[extv[0]->spd_ext_type] = extv[0]; 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate /* Advance pointer (See above for uint64_t ptr reasoning.) */ 4510Sstevel@tonic-gate extv[0] = (spd_ext_t *) 4520Sstevel@tonic-gate ((uint64_t *)extv[0] + extv[0]->spd_ext_len); 4530Sstevel@tonic-gate } 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate /* Everything's cool. */ 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate /* 4580Sstevel@tonic-gate * If extv[0] == NULL, then there are no extension headers in this 4590Sstevel@tonic-gate * message. Ensure that this is the case. 4600Sstevel@tonic-gate */ 4610Sstevel@tonic-gate if (extv[0] == (spd_ext_t *)(basehdr + 1)) 4620Sstevel@tonic-gate extv[0] = NULL; 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate return (KGE_OK); 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate static const int bad_ext_diag[] = { 4680Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_LCLPORT, 4690Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_REMPORT, 4700Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_PROTO, 4710Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_LCLADDR, 4720Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_REMADDR, 4730Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_ACTION, 4740Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_RULE, 4750Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_RULESET, 4760Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE 4770Sstevel@tonic-gate }; 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate static const int dup_ext_diag[] = { 4800Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_LCLPORT, 4810Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_REMPORT, 4820Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_PROTO, 4830Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_LCLADDR, 4840Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_REMADDR, 4850Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_ACTION, 4860Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_RULE, 4870Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_RULESET, 4880Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE 4890Sstevel@tonic-gate }; 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate /* 4920Sstevel@tonic-gate * Transmit a PF_POLICY error message to the instance either pointed to 4930Sstevel@tonic-gate * by ks, the instance with serial number serial, or more, depending. 4940Sstevel@tonic-gate * 4950Sstevel@tonic-gate * The faulty message (or a reasonable facsimile thereof) is in mp. 4960Sstevel@tonic-gate * This function will free mp or recycle it for delivery, thereby causing 4970Sstevel@tonic-gate * the stream head to free it. 4980Sstevel@tonic-gate */ 4990Sstevel@tonic-gate static void 5000Sstevel@tonic-gate spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic) 5010Sstevel@tonic-gate { 5020Sstevel@tonic-gate spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate ASSERT(mp->b_datap->db_type == M_DATA); 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate if (spmsg->spd_msg_type < SPD_MIN || 5070Sstevel@tonic-gate spmsg->spd_msg_type > SPD_MAX) 5080Sstevel@tonic-gate spmsg->spd_msg_type = SPD_RESERVED; 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* 5110Sstevel@tonic-gate * Strip out extension headers. 5120Sstevel@tonic-gate */ 5130Sstevel@tonic-gate ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim); 5140Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (*spmsg); 5150Sstevel@tonic-gate spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t)); 5160Sstevel@tonic-gate spmsg->spd_msg_errno = (uint8_t)error; 5170Sstevel@tonic-gate spmsg->spd_msg_diagnostic = (uint16_t)diagnostic; 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate qreply(q, mp); 5200Sstevel@tonic-gate } 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate static void 5230Sstevel@tonic-gate spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic) 5240Sstevel@tonic-gate { 5250Sstevel@tonic-gate spdsock_error(q, mp, EINVAL, diagnostic); 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate static void 5290Sstevel@tonic-gate spd_echo(queue_t *q, mblk_t *mp) 5300Sstevel@tonic-gate { 5310Sstevel@tonic-gate qreply(q, mp); 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate 5343055Sdanmcd /* 5353055Sdanmcd * Do NOT consume a reference to itp. 5363055Sdanmcd */ 537*3448Sdh155122 /*ARGSUSED*/ 5380Sstevel@tonic-gate static void 539*3448Sdh155122 spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns) 5403055Sdanmcd { 5413055Sdanmcd boolean_t active = (boolean_t)cookie; 5423055Sdanmcd ipsec_policy_head_t *iph; 5433055Sdanmcd 5443055Sdanmcd iph = active ? itp->itp_policy : itp->itp_inactive; 5453055Sdanmcd IPPH_REFHOLD(iph); 5463055Sdanmcd mutex_enter(&itp->itp_lock); 547*3448Sdh155122 spdsock_flush_one(iph, ns); 5483055Sdanmcd if (active) 5493055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 5503055Sdanmcd else 5513055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 5523055Sdanmcd mutex_exit(&itp->itp_lock); 5533055Sdanmcd } 5543055Sdanmcd 5553055Sdanmcd /* 5563055Sdanmcd * Clear out one polhead. 5573055Sdanmcd */ 5583055Sdanmcd static void 559*3448Sdh155122 spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns) 5600Sstevel@tonic-gate { 5610Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_WRITER); 562*3448Sdh155122 ipsec_polhead_flush(iph, ns); 5630Sstevel@tonic-gate rw_exit(&iph->iph_lock); 564*3448Sdh155122 IPPH_REFRELE(iph, ns); 5653055Sdanmcd } 5663055Sdanmcd 5673055Sdanmcd static void 5683055Sdanmcd spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp) 5693055Sdanmcd { 5703055Sdanmcd boolean_t active; 571*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 572*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 573*3448Sdh155122 netstack_t *ns = spds->spds_netstack; 5743055Sdanmcd 5753055Sdanmcd if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) { 576*3448Sdh155122 spdsock_flush_one(iph, spds->spds_netstack); 5773055Sdanmcd } else { 5783055Sdanmcd active = (iph == ALL_ACTIVE_POLHEADS); 5793055Sdanmcd 5803055Sdanmcd /* First flush the global policy. */ 581*3448Sdh155122 spdsock_flush_one(active ? ipsec_system_policy(ns) : 582*3448Sdh155122 ipsec_inactive_policy(ns), ns); 5833055Sdanmcd 5843055Sdanmcd /* Then flush every tunnel's appropriate one. */ 585*3448Sdh155122 itp_walk(spdsock_flush_node, (void *)active, 586*3448Sdh155122 spds->spds_netstack); 5873055Sdanmcd } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate spd_echo(q, mp); 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate static boolean_t 5930Sstevel@tonic-gate spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag) 5940Sstevel@tonic-gate { 5950Sstevel@tonic-gate bzero(sel, sizeof (*sel)); 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate if (extv[SPD_EXT_PROTO] != NULL) { 5980Sstevel@tonic-gate struct spd_proto *pr = 5990Sstevel@tonic-gate (struct spd_proto *)extv[SPD_EXT_PROTO]; 6000Sstevel@tonic-gate sel->ipsl_proto = pr->spd_proto_number; 6010Sstevel@tonic-gate sel->ipsl_valid |= IPSL_PROTOCOL; 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate if (extv[SPD_EXT_LCLPORT] != NULL) { 6040Sstevel@tonic-gate struct spd_portrange *pr = 6050Sstevel@tonic-gate (struct spd_portrange *)extv[SPD_EXT_LCLPORT]; 6060Sstevel@tonic-gate sel->ipsl_lport = pr->spd_ports_minport; 6070Sstevel@tonic-gate sel->ipsl_valid |= IPSL_LOCAL_PORT; 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate if (extv[SPD_EXT_REMPORT] != NULL) { 6100Sstevel@tonic-gate struct spd_portrange *pr = 6110Sstevel@tonic-gate (struct spd_portrange *)extv[SPD_EXT_REMPORT]; 6120Sstevel@tonic-gate sel->ipsl_rport = pr->spd_ports_minport; 6130Sstevel@tonic-gate sel->ipsl_valid |= IPSL_REMOTE_PORT; 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) { 6170Sstevel@tonic-gate struct spd_typecode *tc= 6180Sstevel@tonic-gate (struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE]; 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate sel->ipsl_valid |= IPSL_ICMP_TYPE; 6210Sstevel@tonic-gate sel->ipsl_icmp_type = tc->spd_typecode_type; 6220Sstevel@tonic-gate if (tc->spd_typecode_type_end < tc->spd_typecode_type) 6230Sstevel@tonic-gate sel->ipsl_icmp_type_end = tc->spd_typecode_type; 6240Sstevel@tonic-gate else 6250Sstevel@tonic-gate sel->ipsl_icmp_type_end = tc->spd_typecode_type_end; 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate if (tc->spd_typecode_code != 255) { 6280Sstevel@tonic-gate sel->ipsl_valid |= IPSL_ICMP_CODE; 6290Sstevel@tonic-gate sel->ipsl_icmp_code = tc->spd_typecode_code; 6300Sstevel@tonic-gate if (tc->spd_typecode_code_end < tc->spd_typecode_code) 6310Sstevel@tonic-gate sel->ipsl_icmp_code_end = tc->spd_typecode_code; 6320Sstevel@tonic-gate else 6330Sstevel@tonic-gate sel->ipsl_icmp_code_end = 6340Sstevel@tonic-gate tc->spd_typecode_code_end; 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate #define ADDR2SEL(sel, extv, field, pfield, extn, bit) \ 6380Sstevel@tonic-gate if ((extv)[(extn)] != NULL) { \ 6390Sstevel@tonic-gate uint_t addrlen; \ 6400Sstevel@tonic-gate struct spd_address *ap = \ 6410Sstevel@tonic-gate (struct spd_address *)((extv)[(extn)]); \ 6420Sstevel@tonic-gate addrlen = (ap->spd_address_af == AF_INET6) ? \ 6430Sstevel@tonic-gate IPV6_ADDR_LEN : IP_ADDR_LEN; \ 6440Sstevel@tonic-gate if (SPD_64TO8(ap->spd_address_len) < \ 6450Sstevel@tonic-gate (addrlen + sizeof (*ap))) { \ 6460Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN; \ 6470Sstevel@tonic-gate return (B_FALSE); \ 6480Sstevel@tonic-gate } \ 6490Sstevel@tonic-gate bcopy((ap+1), &((sel)->field), addrlen); \ 6500Sstevel@tonic-gate (sel)->pfield = ap->spd_address_prefixlen; \ 6510Sstevel@tonic-gate (sel)->ipsl_valid |= (bit); \ 6520Sstevel@tonic-gate (sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ? \ 6530Sstevel@tonic-gate IPSL_IPV6 : IPSL_IPV4; \ 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen, 6570Sstevel@tonic-gate SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR); 6580Sstevel@tonic-gate ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen, 6590Sstevel@tonic-gate SPD_EXT_REMADDR, IPSL_REMOTE_ADDR); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) == 6620Sstevel@tonic-gate (IPSL_IPV6|IPSL_IPV4)) { 6630Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_MIXED_AF; 6640Sstevel@tonic-gate return (B_FALSE); 6650Sstevel@tonic-gate } 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate #undef ADDR2SEL 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate return (B_TRUE); 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate static boolean_t 6730Sstevel@tonic-gate spd_convert_type(uint32_t type, ipsec_act_t *act) 6740Sstevel@tonic-gate { 6750Sstevel@tonic-gate switch (type) { 6760Sstevel@tonic-gate case SPD_ACTTYPE_DROP: 6770Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_DISCARD; 6780Sstevel@tonic-gate return (B_TRUE); 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate case SPD_ACTTYPE_PASS: 6810Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_CLEAR; 6820Sstevel@tonic-gate return (B_TRUE); 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate case SPD_ACTTYPE_IPSEC: 6850Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_APPLY; 6860Sstevel@tonic-gate return (B_TRUE); 6870Sstevel@tonic-gate } 6880Sstevel@tonic-gate return (B_FALSE); 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate static boolean_t 6920Sstevel@tonic-gate spd_convert_flags(uint32_t flags, ipsec_act_t *act) 6930Sstevel@tonic-gate { 6940Sstevel@tonic-gate /* 6950Sstevel@tonic-gate * Note use of !! for boolean canonicalization. 6960Sstevel@tonic-gate */ 6970Sstevel@tonic-gate act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH); 6980Sstevel@tonic-gate act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP); 6990Sstevel@tonic-gate act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA); 7000Sstevel@tonic-gate act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE); 7010Sstevel@tonic-gate act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE); 7020Sstevel@tonic-gate return (B_TRUE); 7030Sstevel@tonic-gate } 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate static void 7060Sstevel@tonic-gate spdsock_reset_act(ipsec_act_t *act) 7070Sstevel@tonic-gate { 7080Sstevel@tonic-gate bzero(act, sizeof (*act)); 7090Sstevel@tonic-gate act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS; 7100Sstevel@tonic-gate act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS; 7110Sstevel@tonic-gate act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS; 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate /* 7150Sstevel@tonic-gate * Sanity check action against reality, and shrink-wrap key sizes.. 7160Sstevel@tonic-gate */ 7170Sstevel@tonic-gate static boolean_t 718*3448Sdh155122 spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag, 719*3448Sdh155122 spd_stack_t *spds) 7200Sstevel@tonic-gate { 7213055Sdanmcd if (tunnel_polhead && act->ipa_apply.ipp_use_unique) { 7223055Sdanmcd *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7233055Sdanmcd return (B_FALSE); 7243055Sdanmcd } 7250Sstevel@tonic-gate if ((act->ipa_type != IPSEC_ACT_APPLY) && 7260Sstevel@tonic-gate (act->ipa_apply.ipp_use_ah || 7270Sstevel@tonic-gate act->ipa_apply.ipp_use_esp || 7280Sstevel@tonic-gate act->ipa_apply.ipp_use_espa || 7290Sstevel@tonic-gate act->ipa_apply.ipp_use_se || 7300Sstevel@tonic-gate act->ipa_apply.ipp_use_unique)) { 7310Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7320Sstevel@tonic-gate return (B_FALSE); 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate if ((act->ipa_type == IPSEC_ACT_APPLY) && 7350Sstevel@tonic-gate !act->ipa_apply.ipp_use_ah && 7360Sstevel@tonic-gate !act->ipa_apply.ipp_use_esp) { 7370Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7380Sstevel@tonic-gate return (B_FALSE); 7390Sstevel@tonic-gate } 740*3448Sdh155122 return (ipsec_check_action(act, diag, spds->spds_netstack)); 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate /* 7440Sstevel@tonic-gate * We may be short a few error checks here.. 7450Sstevel@tonic-gate */ 7460Sstevel@tonic-gate static boolean_t 7470Sstevel@tonic-gate spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp, 748*3448Sdh155122 int *diag, spd_stack_t *spds) 7490Sstevel@tonic-gate { 7500Sstevel@tonic-gate struct spd_ext_actions *sactp = 7510Sstevel@tonic-gate (struct spd_ext_actions *)extv[SPD_EXT_ACTION]; 7520Sstevel@tonic-gate ipsec_act_t act, *actp, *endactp; 7530Sstevel@tonic-gate struct spd_attribute *attrp, *endattrp; 7540Sstevel@tonic-gate uint64_t *endp; 7550Sstevel@tonic-gate int nact; 7563055Sdanmcd boolean_t tunnel_polhead; 7573055Sdanmcd 7583055Sdanmcd tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL && 7593055Sdanmcd (((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags & 7603055Sdanmcd SPD_RULE_FLAG_TUNNEL)); 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate *actpp = NULL; 7630Sstevel@tonic-gate *nactp = 0; 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate if (sactp == NULL) { 7660Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_NO_ACTION_EXT; 7670Sstevel@tonic-gate return (B_FALSE); 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate /* 7710Sstevel@tonic-gate * Parse the "action" extension and convert into an action chain. 7720Sstevel@tonic-gate */ 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate nact = sactp->spd_actions_count; 7750Sstevel@tonic-gate 7760Sstevel@tonic-gate endp = (uint64_t *)sactp; 7770Sstevel@tonic-gate endp += sactp->spd_actions_len; 7780Sstevel@tonic-gate endattrp = (struct spd_attribute *)endp; 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP); 7810Sstevel@tonic-gate if (actp == NULL) { 7820Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_NO_MEM; 7830Sstevel@tonic-gate return (B_FALSE); 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate *actpp = actp; 7860Sstevel@tonic-gate *nactp = nact; 7870Sstevel@tonic-gate endactp = actp + nact; 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate spdsock_reset_act(&act); 7900Sstevel@tonic-gate attrp = (struct spd_attribute *)(&sactp[1]); 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate for (; attrp < endattrp; attrp++) { 7930Sstevel@tonic-gate switch (attrp->spd_attr_tag) { 7940Sstevel@tonic-gate case SPD_ATTR_NOP: 7950Sstevel@tonic-gate break; 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate case SPD_ATTR_EMPTY: 7980Sstevel@tonic-gate spdsock_reset_act(&act); 7990Sstevel@tonic-gate break; 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate case SPD_ATTR_END: 8020Sstevel@tonic-gate attrp = endattrp; 8030Sstevel@tonic-gate /* FALLTHRU */ 8040Sstevel@tonic-gate case SPD_ATTR_NEXT: 8050Sstevel@tonic-gate if (actp >= endactp) { 8060Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT; 8070Sstevel@tonic-gate goto fail; 8080Sstevel@tonic-gate } 809*3448Sdh155122 if (!spdsock_check_action(&act, tunnel_polhead, 810*3448Sdh155122 diag, spds)) 8110Sstevel@tonic-gate goto fail; 8120Sstevel@tonic-gate *actp++ = act; 8133055Sdanmcd spdsock_reset_act(&act); 8140Sstevel@tonic-gate break; 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate case SPD_ATTR_TYPE: 8170Sstevel@tonic-gate if (!spd_convert_type(attrp->spd_attr_value, &act)) { 8180Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE; 8190Sstevel@tonic-gate goto fail; 8200Sstevel@tonic-gate } 8210Sstevel@tonic-gate break; 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate case SPD_ATTR_FLAGS: 8243055Sdanmcd if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) { 8253055Sdanmcd /* 8263055Sdanmcd * Set "sa unique" for transport-mode 8273055Sdanmcd * tunnels whether we want to or not. 8283055Sdanmcd */ 8293055Sdanmcd attrp->spd_attr_value |= SPD_APPLY_UNIQUE; 8303055Sdanmcd } 8310Sstevel@tonic-gate if (!spd_convert_flags(attrp->spd_attr_value, &act)) { 8320Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS; 8330Sstevel@tonic-gate goto fail; 8340Sstevel@tonic-gate } 8350Sstevel@tonic-gate break; 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate case SPD_ATTR_AH_AUTH: 8383055Sdanmcd if (attrp->spd_attr_value == 0) { 8393055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG; 8403055Sdanmcd goto fail; 8413055Sdanmcd } 8420Sstevel@tonic-gate act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value; 8430Sstevel@tonic-gate break; 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate case SPD_ATTR_ESP_ENCR: 8463055Sdanmcd if (attrp->spd_attr_value == 0) { 8473055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG; 8483055Sdanmcd goto fail; 8493055Sdanmcd } 8500Sstevel@tonic-gate act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value; 8510Sstevel@tonic-gate break; 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate case SPD_ATTR_ESP_AUTH: 8543055Sdanmcd if (attrp->spd_attr_value == 0) { 8553055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG; 8563055Sdanmcd goto fail; 8573055Sdanmcd } 8580Sstevel@tonic-gate act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value; 8590Sstevel@tonic-gate break; 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate case SPD_ATTR_ENCR_MINBITS: 8620Sstevel@tonic-gate act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value; 8630Sstevel@tonic-gate break; 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate case SPD_ATTR_ENCR_MAXBITS: 8660Sstevel@tonic-gate act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value; 8670Sstevel@tonic-gate break; 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate case SPD_ATTR_AH_MINBITS: 8700Sstevel@tonic-gate act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value; 8710Sstevel@tonic-gate break; 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate case SPD_ATTR_AH_MAXBITS: 8740Sstevel@tonic-gate act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value; 8750Sstevel@tonic-gate break; 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate case SPD_ATTR_ESPA_MINBITS: 8780Sstevel@tonic-gate act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value; 8790Sstevel@tonic-gate break; 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate case SPD_ATTR_ESPA_MAXBITS: 8820Sstevel@tonic-gate act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value; 8830Sstevel@tonic-gate break; 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate case SPD_ATTR_LIFE_SOFT_TIME: 8860Sstevel@tonic-gate case SPD_ATTR_LIFE_HARD_TIME: 8870Sstevel@tonic-gate case SPD_ATTR_LIFE_SOFT_BYTES: 8880Sstevel@tonic-gate case SPD_ATTR_LIFE_HARD_BYTES: 8890Sstevel@tonic-gate break; 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate case SPD_ATTR_KM_PROTO: 8920Sstevel@tonic-gate act.ipa_apply.ipp_km_proto = attrp->spd_attr_value; 8930Sstevel@tonic-gate break; 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate case SPD_ATTR_KM_COOKIE: 8960Sstevel@tonic-gate act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value; 8970Sstevel@tonic-gate break; 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate case SPD_ATTR_REPLAY_DEPTH: 9000Sstevel@tonic-gate act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value; 9010Sstevel@tonic-gate break; 9020Sstevel@tonic-gate } 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate if (actp != endactp) { 9050Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT; 9060Sstevel@tonic-gate goto fail; 9070Sstevel@tonic-gate } 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate return (B_TRUE); 9100Sstevel@tonic-gate fail: 9110Sstevel@tonic-gate ipsec_actvec_free(*actpp, nact); 9120Sstevel@tonic-gate *actpp = NULL; 9130Sstevel@tonic-gate return (B_FALSE); 9140Sstevel@tonic-gate } 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate typedef struct 9170Sstevel@tonic-gate { 9180Sstevel@tonic-gate ipsec_policy_t *pol; 9190Sstevel@tonic-gate int dir; 9200Sstevel@tonic-gate } tmprule_t; 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate static int 9230Sstevel@tonic-gate mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule, 9240Sstevel@tonic-gate ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af, 925*3448Sdh155122 tmprule_t **rp, uint64_t *index, spd_stack_t *spds) 9260Sstevel@tonic-gate { 9270Sstevel@tonic-gate ipsec_policy_t *pol; 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4); 9300Sstevel@tonic-gate sel->ipsl_valid |= af; 9310Sstevel@tonic-gate 9323055Sdanmcd pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority, 933*3448Sdh155122 index, spds->spds_netstack); 9340Sstevel@tonic-gate if (pol == NULL) 9350Sstevel@tonic-gate return (ENOMEM); 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate (*rp)->pol = pol; 9380Sstevel@tonic-gate (*rp)->dir = dir; 9390Sstevel@tonic-gate (*rp)++; 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate if (!ipsec_check_policy(iph, pol, dir)) 9420Sstevel@tonic-gate return (EEXIST); 9430Sstevel@tonic-gate 9440Sstevel@tonic-gate rule->spd_rule_index = pol->ipsp_index; 9450Sstevel@tonic-gate return (0); 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate static int 9490Sstevel@tonic-gate mkrulepair(ipsec_policy_head_t *iph, struct spd_rule *rule, 9500Sstevel@tonic-gate ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t afs, 951*3448Sdh155122 tmprule_t **rp, uint64_t *index, spd_stack_t *spds) 9520Sstevel@tonic-gate { 9530Sstevel@tonic-gate int error; 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate if (afs & IPSL_IPV4) { 9563055Sdanmcd error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV4, rp, 957*3448Sdh155122 index, spds); 9580Sstevel@tonic-gate if (error != 0) 9590Sstevel@tonic-gate return (error); 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate if (afs & IPSL_IPV6) { 9623055Sdanmcd error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV6, rp, 963*3448Sdh155122 index, spds); 9640Sstevel@tonic-gate if (error != 0) 9650Sstevel@tonic-gate return (error); 9660Sstevel@tonic-gate } 9670Sstevel@tonic-gate return (0); 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate static void 9723055Sdanmcd spdsock_addrule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 9733055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 9740Sstevel@tonic-gate { 9750Sstevel@tonic-gate ipsec_selkey_t sel; 9760Sstevel@tonic-gate ipsec_act_t *actp; 9770Sstevel@tonic-gate uint_t nact; 9783055Sdanmcd int diag = 0, error, afs; 9790Sstevel@tonic-gate struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE]; 9800Sstevel@tonic-gate tmprule_t rules[4], *rulep = &rules[0]; 9813055Sdanmcd boolean_t tunnel_mode, empty_itp, active; 9823055Sdanmcd uint64_t *index = (itp == NULL) ? NULL : &itp->itp_next_policy_index; 983*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 984*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate if (rule == NULL) { 9870Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT); 9880Sstevel@tonic-gate return; 9890Sstevel@tonic-gate } 9900Sstevel@tonic-gate 9913055Sdanmcd tunnel_mode = (rule->spd_rule_flags & SPD_RULE_FLAG_TUNNEL); 9923055Sdanmcd 9933055Sdanmcd if (itp != NULL) { 9943055Sdanmcd mutex_enter(&itp->itp_lock); 9953055Sdanmcd ASSERT(itp->itp_policy == iph || itp->itp_inactive == iph); 9963055Sdanmcd active = (itp->itp_policy == iph); 9973055Sdanmcd if (ITP_P_ISACTIVE(itp, iph)) { 9983055Sdanmcd /* Check for mix-and-match of tunnel/transport. */ 9993055Sdanmcd if ((tunnel_mode && !ITP_P_ISTUNNEL(itp, iph)) || 10003055Sdanmcd (!tunnel_mode && ITP_P_ISTUNNEL(itp, iph))) { 10013055Sdanmcd mutex_exit(&itp->itp_lock); 10023055Sdanmcd spdsock_error(q, mp, EBUSY, 0); 10033055Sdanmcd return; 10043055Sdanmcd } 10053055Sdanmcd empty_itp = B_FALSE; 10063055Sdanmcd } else { 10073055Sdanmcd empty_itp = B_TRUE; 10083055Sdanmcd itp->itp_flags = active ? ITPF_P_ACTIVE : ITPF_I_ACTIVE; 10093055Sdanmcd if (tunnel_mode) 10103055Sdanmcd itp->itp_flags |= active ? ITPF_P_TUNNEL : 10113055Sdanmcd ITPF_I_TUNNEL; 10123055Sdanmcd } 10133055Sdanmcd } else { 10143055Sdanmcd empty_itp = B_FALSE; 10153055Sdanmcd } 10163055Sdanmcd 10170Sstevel@tonic-gate if (rule->spd_rule_index != 0) { 10183055Sdanmcd diag = SPD_DIAGNOSTIC_INVALID_RULE_INDEX; 10193055Sdanmcd error = EINVAL; 10203055Sdanmcd goto fail2; 10210Sstevel@tonic-gate } 10220Sstevel@tonic-gate 10230Sstevel@tonic-gate if (!spdsock_ext_to_sel(extv, &sel, &diag)) { 10243055Sdanmcd error = EINVAL; 10253055Sdanmcd goto fail2; 10263055Sdanmcd } 10273055Sdanmcd 10283055Sdanmcd if (itp != NULL) { 10293055Sdanmcd if (tunnel_mode) { 10303055Sdanmcd if (sel.ipsl_valid & 10313055Sdanmcd (IPSL_REMOTE_PORT | IPSL_LOCAL_PORT)) { 10323055Sdanmcd itp->itp_flags |= active ? 10333055Sdanmcd ITPF_P_PER_PORT_SECURITY : 10343055Sdanmcd ITPF_I_PER_PORT_SECURITY; 10353055Sdanmcd } 10363055Sdanmcd } else { 10373055Sdanmcd /* 10383055Sdanmcd * For now, we don't allow transport-mode on a tunnel 10393055Sdanmcd * with ANY specific selectors. Bail if we have such 10403055Sdanmcd * a request. 10413055Sdanmcd */ 10423055Sdanmcd if (sel.ipsl_valid & IPSL_WILDCARD) { 10433055Sdanmcd diag = SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS; 10443055Sdanmcd error = EINVAL; 10453055Sdanmcd goto fail2; 10463055Sdanmcd } 10473055Sdanmcd } 10480Sstevel@tonic-gate } 10490Sstevel@tonic-gate 1050*3448Sdh155122 if (!spdsock_ext_to_actvec(extv, &actp, &nact, &diag, spds)) { 10513055Sdanmcd error = EINVAL; 10523055Sdanmcd goto fail2; 10530Sstevel@tonic-gate } 10540Sstevel@tonic-gate /* 10550Sstevel@tonic-gate * If no addresses were specified, add both. 10560Sstevel@tonic-gate */ 10570Sstevel@tonic-gate afs = sel.ipsl_valid & (IPSL_IPV6|IPSL_IPV4); 10580Sstevel@tonic-gate if (afs == 0) 10590Sstevel@tonic-gate afs = (IPSL_IPV6|IPSL_IPV4); 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_WRITER); 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate if (rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) { 10640Sstevel@tonic-gate error = mkrulepair(iph, rule, &sel, actp, nact, 1065*3448Sdh155122 IPSEC_TYPE_OUTBOUND, afs, &rulep, index, spds); 10660Sstevel@tonic-gate if (error != 0) 10670Sstevel@tonic-gate goto fail; 10680Sstevel@tonic-gate } 10690Sstevel@tonic-gate 10700Sstevel@tonic-gate if (rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) { 10710Sstevel@tonic-gate error = mkrulepair(iph, rule, &sel, actp, nact, 1072*3448Sdh155122 IPSEC_TYPE_INBOUND, afs, &rulep, index, spds); 10730Sstevel@tonic-gate if (error != 0) 10740Sstevel@tonic-gate goto fail; 10750Sstevel@tonic-gate } 10760Sstevel@tonic-gate 1077*3448Sdh155122 while ((--rulep) >= &rules[0]) { 1078*3448Sdh155122 ipsec_enter_policy(iph, rulep->pol, rulep->dir, 1079*3448Sdh155122 spds->spds_netstack); 1080*3448Sdh155122 } 10810Sstevel@tonic-gate rw_exit(&iph->iph_lock); 10823055Sdanmcd if (itp != NULL) 10833055Sdanmcd mutex_exit(&itp->itp_lock); 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate ipsec_actvec_free(actp, nact); 10860Sstevel@tonic-gate spd_echo(q, mp); 10870Sstevel@tonic-gate return; 10880Sstevel@tonic-gate 10890Sstevel@tonic-gate fail: 10900Sstevel@tonic-gate rw_exit(&iph->iph_lock); 10910Sstevel@tonic-gate while ((--rulep) >= &rules[0]) { 1092*3448Sdh155122 IPPOL_REFRELE(rulep->pol, spds->spds_netstack); 10930Sstevel@tonic-gate } 10940Sstevel@tonic-gate ipsec_actvec_free(actp, nact); 10953055Sdanmcd fail2: 10963055Sdanmcd if (itp != NULL) { 10973055Sdanmcd if (empty_itp) 10983055Sdanmcd itp->itp_flags = 0; 10993055Sdanmcd mutex_exit(&itp->itp_lock); 11003055Sdanmcd } 11013055Sdanmcd spdsock_error(q, mp, error, diag); 11020Sstevel@tonic-gate } 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate void 11053055Sdanmcd spdsock_deleterule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 11063055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 11070Sstevel@tonic-gate { 11080Sstevel@tonic-gate ipsec_selkey_t sel; 11090Sstevel@tonic-gate struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE]; 11103055Sdanmcd int err, diag = 0; 1111*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 1112*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate if (rule == NULL) { 11150Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT); 11160Sstevel@tonic-gate return; 11170Sstevel@tonic-gate } 11180Sstevel@tonic-gate 11193055Sdanmcd /* 11203055Sdanmcd * Must enter itp_lock first to avoid deadlock. See tun.c's 11213055Sdanmcd * set_sec_simple() for the other case of itp_lock and iph_lock. 11223055Sdanmcd */ 11233055Sdanmcd if (itp != NULL) 11243055Sdanmcd mutex_enter(&itp->itp_lock); 11253055Sdanmcd 11260Sstevel@tonic-gate if (rule->spd_rule_index != 0) { 1127*3448Sdh155122 if (ipsec_policy_delete_index(iph, rule->spd_rule_index, 1128*3448Sdh155122 spds->spds_netstack) != 0) { 11293055Sdanmcd err = ESRCH; 11303055Sdanmcd goto fail; 11310Sstevel@tonic-gate } 11320Sstevel@tonic-gate } else { 11330Sstevel@tonic-gate if (!spdsock_ext_to_sel(extv, &sel, &diag)) { 11343055Sdanmcd err = EINVAL; /* diag already set... */ 11353055Sdanmcd goto fail; 11363055Sdanmcd } 11373055Sdanmcd 11383055Sdanmcd if ((rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) && 1139*3448Sdh155122 !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_INBOUND, 1140*3448Sdh155122 spds->spds_netstack)) { 11413055Sdanmcd err = ESRCH; 11423055Sdanmcd goto fail; 11433055Sdanmcd } 11443055Sdanmcd 11453055Sdanmcd if ((rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) && 1146*3448Sdh155122 !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_OUTBOUND, 1147*3448Sdh155122 spds->spds_netstack)) { 11483055Sdanmcd err = ESRCH; 11493055Sdanmcd goto fail; 11500Sstevel@tonic-gate } 11513055Sdanmcd } 11523055Sdanmcd 11533055Sdanmcd if (itp != NULL) { 11543055Sdanmcd ASSERT(iph == itp->itp_policy || iph == itp->itp_inactive); 11553055Sdanmcd rw_enter(&iph->iph_lock, RW_READER); 11563055Sdanmcd if (avl_numnodes(&iph->iph_rulebyid) == 0) { 11573055Sdanmcd if (iph == itp->itp_policy) 11583055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 11593055Sdanmcd else 11603055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 11610Sstevel@tonic-gate } 11623055Sdanmcd /* Can exit locks in any order. */ 11633055Sdanmcd rw_exit(&iph->iph_lock); 11643055Sdanmcd mutex_exit(&itp->itp_lock); 11650Sstevel@tonic-gate } 11660Sstevel@tonic-gate spd_echo(q, mp); 11670Sstevel@tonic-gate return; 11680Sstevel@tonic-gate fail: 11693055Sdanmcd if (itp != NULL) 11703055Sdanmcd mutex_exit(&itp->itp_lock); 11713055Sdanmcd spdsock_error(q, mp, err, diag); 11723055Sdanmcd } 11733055Sdanmcd 11743055Sdanmcd /* Do NOT consume a reference to itp. */ 11753055Sdanmcd /* ARGSUSED */ 11763055Sdanmcd static void 1177*3448Sdh155122 spdsock_flip_node(ipsec_tun_pol_t *itp, void *ignoreme, netstack_t *ns) 11783055Sdanmcd { 11793055Sdanmcd mutex_enter(&itp->itp_lock); 11803055Sdanmcd ITPF_SWAP(itp->itp_flags); 1181*3448Sdh155122 ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns); 11823055Sdanmcd mutex_exit(&itp->itp_lock); 11830Sstevel@tonic-gate } 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate void 11863055Sdanmcd spdsock_flip(queue_t *q, mblk_t *mp, spd_if_t *tunname) 11870Sstevel@tonic-gate { 11883055Sdanmcd char *tname; 11893055Sdanmcd ipsec_tun_pol_t *itp; 1190*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 1191*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 11923055Sdanmcd 11933055Sdanmcd if (tunname != NULL) { 11943055Sdanmcd tname = (char *)tunname->spd_if_name; 11953055Sdanmcd if (*tname == '\0') { 1196*3448Sdh155122 /* can't fail */ 1197*3448Sdh155122 ipsec_swap_global_policy(spds->spds_netstack); 1198*3448Sdh155122 itp_walk(spdsock_flip_node, NULL, spds->spds_netstack); 11993055Sdanmcd } else { 1200*3448Sdh155122 itp = get_tunnel_policy(tname, spds->spds_netstack); 12013055Sdanmcd if (itp == NULL) { 12023055Sdanmcd /* Better idea for "tunnel not found"? */ 12033055Sdanmcd spdsock_error(q, mp, ESRCH, 0); 12043055Sdanmcd return; 12053055Sdanmcd } 1206*3448Sdh155122 spdsock_flip_node(itp, NULL, NULL); 1207*3448Sdh155122 ITP_REFRELE(itp, spds->spds_netstack); 12083055Sdanmcd } 12093055Sdanmcd } else { 1210*3448Sdh155122 ipsec_swap_global_policy(spds->spds_netstack); /* can't fail */ 12113055Sdanmcd } 12120Sstevel@tonic-gate spd_echo(q, mp); 12130Sstevel@tonic-gate } 12140Sstevel@tonic-gate 12150Sstevel@tonic-gate /* 12160Sstevel@tonic-gate * Unimplemented feature 12170Sstevel@tonic-gate */ 12180Sstevel@tonic-gate /* ARGSUSED */ 12190Sstevel@tonic-gate static void 12203055Sdanmcd spdsock_lookup(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 12213055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 12220Sstevel@tonic-gate { 12230Sstevel@tonic-gate spdsock_error(q, mp, EINVAL, 0); 12240Sstevel@tonic-gate } 12250Sstevel@tonic-gate 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate static mblk_t * 12280Sstevel@tonic-gate spdsock_dump_ruleset(mblk_t *req, ipsec_policy_head_t *iph, 12290Sstevel@tonic-gate uint32_t count, uint16_t error) 12300Sstevel@tonic-gate { 12310Sstevel@tonic-gate size_t len = sizeof (spd_ruleset_ext_t) + sizeof (spd_msg_t); 12320Sstevel@tonic-gate spd_msg_t *msg; 12330Sstevel@tonic-gate spd_ruleset_ext_t *ruleset; 12340Sstevel@tonic-gate mblk_t *m = allocb(len, BPRI_HI); 12350Sstevel@tonic-gate 12360Sstevel@tonic-gate ASSERT(RW_READ_HELD(&iph->iph_lock)); 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate if (m == NULL) { 12390Sstevel@tonic-gate return (NULL); 12400Sstevel@tonic-gate } 12410Sstevel@tonic-gate msg = (spd_msg_t *)m->b_rptr; 12420Sstevel@tonic-gate ruleset = (spd_ruleset_ext_t *)(&msg[1]); 12430Sstevel@tonic-gate 12440Sstevel@tonic-gate m->b_wptr = (uint8_t *)&ruleset[1]; 12450Sstevel@tonic-gate 12460Sstevel@tonic-gate *msg = *(spd_msg_t *)(req->b_rptr); 12470Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(len); 12480Sstevel@tonic-gate msg->spd_msg_errno = error; 12490Sstevel@tonic-gate 12500Sstevel@tonic-gate ruleset->spd_ruleset_len = SPD_8TO64(sizeof (*ruleset)); 12510Sstevel@tonic-gate ruleset->spd_ruleset_type = SPD_EXT_RULESET; 12520Sstevel@tonic-gate ruleset->spd_ruleset_count = count; 12530Sstevel@tonic-gate ruleset->spd_ruleset_version = iph->iph_gen; 12540Sstevel@tonic-gate return (m); 12550Sstevel@tonic-gate } 12560Sstevel@tonic-gate 12570Sstevel@tonic-gate static mblk_t * 12580Sstevel@tonic-gate spdsock_dump_finish(spdsock_t *ss, int error) 12590Sstevel@tonic-gate { 12600Sstevel@tonic-gate mblk_t *m; 12610Sstevel@tonic-gate ipsec_policy_head_t *iph = ss->spdsock_dump_head; 12620Sstevel@tonic-gate mblk_t *req = ss->spdsock_dump_req; 12633055Sdanmcd ipsec_tun_pol_t *itp, dummy; 1264*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 1265*3448Sdh155122 netstack_t *ns = spds->spds_netstack; 1266*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 12673055Sdanmcd 12683055Sdanmcd ss->spdsock_dump_remaining_polheads--; 12693055Sdanmcd if (error == 0 && ss->spdsock_dump_remaining_polheads != 0) { 12703055Sdanmcd /* Attempt a respin with a new policy head. */ 1271*3448Sdh155122 rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER); 12723055Sdanmcd /* NOTE: No need for ITP_REF*() macros here. */ 1273*3448Sdh155122 if (ipss->ipsec_tunnel_policy_gen > ss->spdsock_dump_tun_gen) { 12743055Sdanmcd /* Bail with EAGAIN. */ 12753055Sdanmcd error = EAGAIN; 12763055Sdanmcd } else if (ss->spdsock_dump_name[0] == '\0') { 12773055Sdanmcd /* Just finished global, find first node. */ 1278*3448Sdh155122 itp = (ipsec_tun_pol_t *)avl_first( 1279*3448Sdh155122 &ipss->ipsec_tunnel_policies); 12803055Sdanmcd } else { 12813055Sdanmcd /* 12823055Sdanmcd * We just finished current-named polhead, find 12833055Sdanmcd * the next one. 12843055Sdanmcd */ 12853055Sdanmcd (void) strncpy(dummy.itp_name, ss->spdsock_dump_name, 12863055Sdanmcd LIFNAMSIZ); 1287*3448Sdh155122 itp = (ipsec_tun_pol_t *)avl_find( 1288*3448Sdh155122 &ipss->ipsec_tunnel_policies, &dummy, NULL); 12893055Sdanmcd ASSERT(itp != NULL); 1290*3448Sdh155122 itp = (ipsec_tun_pol_t *)AVL_NEXT( 1291*3448Sdh155122 &ipss->ipsec_tunnel_policies, itp); 12923055Sdanmcd /* remaining_polheads should maintain this assertion. */ 12933055Sdanmcd ASSERT(itp != NULL); 12943055Sdanmcd } 12953055Sdanmcd if (error == 0) { 12963055Sdanmcd (void) strncpy(ss->spdsock_dump_name, itp->itp_name, 12973055Sdanmcd LIFNAMSIZ); 12983055Sdanmcd /* Reset other spdsock_dump thingies. */ 1299*3448Sdh155122 IPPH_REFRELE(ss->spdsock_dump_head, ns); 13003055Sdanmcd if (ss->spdsock_dump_active) { 13013055Sdanmcd ss->spdsock_dump_tunnel = 13023055Sdanmcd itp->itp_flags & ITPF_P_TUNNEL; 13033055Sdanmcd iph = itp->itp_policy; 13043055Sdanmcd } else { 13053055Sdanmcd ss->spdsock_dump_tunnel = 13063055Sdanmcd itp->itp_flags & ITPF_I_TUNNEL; 13073055Sdanmcd iph = itp->itp_inactive; 13083055Sdanmcd } 13093055Sdanmcd IPPH_REFHOLD(iph); 13103055Sdanmcd rw_enter(&iph->iph_lock, RW_READER); 13113055Sdanmcd ss->spdsock_dump_head = iph; 13123055Sdanmcd ss->spdsock_dump_gen = iph->iph_gen; 13133055Sdanmcd ss->spdsock_dump_cur_type = 0; 13143055Sdanmcd ss->spdsock_dump_cur_af = IPSEC_AF_V4; 13153055Sdanmcd ss->spdsock_dump_cur_rule = NULL; 13163055Sdanmcd ss->spdsock_dump_count = 0; 13173055Sdanmcd ss->spdsock_dump_cur_chain = 0; 13183055Sdanmcd rw_exit(&iph->iph_lock); 1319*3448Sdh155122 rw_exit(&ipss->ipsec_tunnel_policy_lock); 13203055Sdanmcd /* And start again. */ 13213055Sdanmcd return (spdsock_dump_next_record(ss)); 13223055Sdanmcd } 1323*3448Sdh155122 rw_exit(&ipss->ipsec_tunnel_policy_lock); 13243055Sdanmcd } 13250Sstevel@tonic-gate 13260Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 13270Sstevel@tonic-gate m = spdsock_dump_ruleset(req, iph, ss->spdsock_dump_count, error); 13280Sstevel@tonic-gate rw_exit(&iph->iph_lock); 1329*3448Sdh155122 IPPH_REFRELE(iph, ns); 13300Sstevel@tonic-gate ss->spdsock_dump_req = NULL; 13310Sstevel@tonic-gate freemsg(req); 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate return (m); 13340Sstevel@tonic-gate } 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate /* 13370Sstevel@tonic-gate * Rule encoding functions. 13380Sstevel@tonic-gate * We do a two-pass encode. 13390Sstevel@tonic-gate * If base != NULL, fill in encoded rule part starting at base+offset. 13400Sstevel@tonic-gate * Always return "offset" plus length of to-be-encoded data. 13410Sstevel@tonic-gate */ 13420Sstevel@tonic-gate static uint_t 13430Sstevel@tonic-gate spdsock_encode_typecode(uint8_t *base, uint_t offset, uint8_t type, 13440Sstevel@tonic-gate uint8_t type_end, uint8_t code, uint8_t code_end) 13450Sstevel@tonic-gate { 13460Sstevel@tonic-gate struct spd_typecode *tcp; 13470Sstevel@tonic-gate 13480Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate if (base != NULL) { 13510Sstevel@tonic-gate tcp = (struct spd_typecode *)(base + offset); 13520Sstevel@tonic-gate tcp->spd_typecode_len = SPD_8TO64(sizeof (*tcp)); 13530Sstevel@tonic-gate tcp->spd_typecode_exttype = SPD_EXT_ICMP_TYPECODE; 13540Sstevel@tonic-gate tcp->spd_typecode_code = code; 13550Sstevel@tonic-gate tcp->spd_typecode_type = type; 13560Sstevel@tonic-gate tcp->spd_typecode_type_end = type_end; 13570Sstevel@tonic-gate tcp->spd_typecode_code_end = code_end; 13580Sstevel@tonic-gate } 13590Sstevel@tonic-gate offset += sizeof (*tcp); 13600Sstevel@tonic-gate 13610Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 13620Sstevel@tonic-gate 13630Sstevel@tonic-gate return (offset); 13640Sstevel@tonic-gate } 13650Sstevel@tonic-gate 13660Sstevel@tonic-gate static uint_t 13670Sstevel@tonic-gate spdsock_encode_proto(uint8_t *base, uint_t offset, uint8_t proto) 13680Sstevel@tonic-gate { 13690Sstevel@tonic-gate struct spd_proto *spp; 13700Sstevel@tonic-gate 13710Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 13720Sstevel@tonic-gate 13730Sstevel@tonic-gate if (base != NULL) { 13740Sstevel@tonic-gate spp = (struct spd_proto *)(base + offset); 13750Sstevel@tonic-gate spp->spd_proto_len = SPD_8TO64(sizeof (*spp)); 13760Sstevel@tonic-gate spp->spd_proto_exttype = SPD_EXT_PROTO; 13770Sstevel@tonic-gate spp->spd_proto_number = proto; 13780Sstevel@tonic-gate spp->spd_proto_reserved1 = 0; 13790Sstevel@tonic-gate spp->spd_proto_reserved2 = 0; 13800Sstevel@tonic-gate } 13810Sstevel@tonic-gate offset += sizeof (*spp); 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 13840Sstevel@tonic-gate 13850Sstevel@tonic-gate return (offset); 13860Sstevel@tonic-gate } 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate static uint_t 13890Sstevel@tonic-gate spdsock_encode_port(uint8_t *base, uint_t offset, uint16_t ext, uint16_t port) 13900Sstevel@tonic-gate { 13910Sstevel@tonic-gate struct spd_portrange *spp; 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 13940Sstevel@tonic-gate 13950Sstevel@tonic-gate if (base != NULL) { 13960Sstevel@tonic-gate spp = (struct spd_portrange *)(base + offset); 13970Sstevel@tonic-gate spp->spd_ports_len = SPD_8TO64(sizeof (*spp)); 13980Sstevel@tonic-gate spp->spd_ports_exttype = ext; 13990Sstevel@tonic-gate spp->spd_ports_minport = port; 14000Sstevel@tonic-gate spp->spd_ports_maxport = port; 14010Sstevel@tonic-gate } 14020Sstevel@tonic-gate offset += sizeof (*spp); 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate return (offset); 14070Sstevel@tonic-gate } 14080Sstevel@tonic-gate 14090Sstevel@tonic-gate static uint_t 14100Sstevel@tonic-gate spdsock_encode_addr(uint8_t *base, uint_t offset, uint16_t ext, 14110Sstevel@tonic-gate const ipsec_selkey_t *sel, const ipsec_addr_t *addr, uint_t pfxlen) 14120Sstevel@tonic-gate { 14130Sstevel@tonic-gate struct spd_address *sae; 14140Sstevel@tonic-gate ipsec_addr_t *spdaddr; 14150Sstevel@tonic-gate uint_t start = offset; 14160Sstevel@tonic-gate uint_t addrlen; 14170Sstevel@tonic-gate uint_t af; 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate if (sel->ipsl_valid & IPSL_IPV4) { 14200Sstevel@tonic-gate af = AF_INET; 14210Sstevel@tonic-gate addrlen = IP_ADDR_LEN; 14220Sstevel@tonic-gate } else { 14230Sstevel@tonic-gate af = AF_INET6; 14240Sstevel@tonic-gate addrlen = IPV6_ADDR_LEN; 14250Sstevel@tonic-gate } 14260Sstevel@tonic-gate 14270Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate if (base != NULL) { 14300Sstevel@tonic-gate sae = (struct spd_address *)(base + offset); 14310Sstevel@tonic-gate sae->spd_address_exttype = ext; 14320Sstevel@tonic-gate sae->spd_address_af = af; 14330Sstevel@tonic-gate sae->spd_address_prefixlen = pfxlen; 14340Sstevel@tonic-gate sae->spd_address_reserved2 = 0; 14350Sstevel@tonic-gate 14360Sstevel@tonic-gate spdaddr = (ipsec_addr_t *)(&sae[1]); 14370Sstevel@tonic-gate bcopy(addr, spdaddr, addrlen); 14380Sstevel@tonic-gate } 14390Sstevel@tonic-gate offset += sizeof (*sae); 14400Sstevel@tonic-gate addrlen = roundup(addrlen, sizeof (uint64_t)); 14410Sstevel@tonic-gate offset += addrlen; 14420Sstevel@tonic-gate 14430Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate if (base != NULL) 14460Sstevel@tonic-gate sae->spd_address_len = SPD_8TO64(offset - start); 14470Sstevel@tonic-gate return (offset); 14480Sstevel@tonic-gate } 14490Sstevel@tonic-gate 14500Sstevel@tonic-gate static uint_t 14510Sstevel@tonic-gate spdsock_encode_sel(uint8_t *base, uint_t offset, const ipsec_sel_t *sel) 14520Sstevel@tonic-gate { 14530Sstevel@tonic-gate const ipsec_selkey_t *selkey = &sel->ipsl_key; 14540Sstevel@tonic-gate 14550Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_PROTOCOL) 14560Sstevel@tonic-gate offset = spdsock_encode_proto(base, offset, selkey->ipsl_proto); 14570Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_LOCAL_PORT) 14580Sstevel@tonic-gate offset = spdsock_encode_port(base, offset, SPD_EXT_LCLPORT, 14590Sstevel@tonic-gate selkey->ipsl_lport); 14600Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_REMOTE_PORT) 14610Sstevel@tonic-gate offset = spdsock_encode_port(base, offset, SPD_EXT_REMPORT, 14620Sstevel@tonic-gate selkey->ipsl_rport); 14630Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_REMOTE_ADDR) 14640Sstevel@tonic-gate offset = spdsock_encode_addr(base, offset, SPD_EXT_REMADDR, 14650Sstevel@tonic-gate selkey, &selkey->ipsl_remote, selkey->ipsl_remote_pfxlen); 14660Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_LOCAL_ADDR) 14670Sstevel@tonic-gate offset = spdsock_encode_addr(base, offset, SPD_EXT_LCLADDR, 14680Sstevel@tonic-gate selkey, &selkey->ipsl_local, selkey->ipsl_local_pfxlen); 14690Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_ICMP_TYPE) { 14700Sstevel@tonic-gate offset = spdsock_encode_typecode(base, offset, 14710Sstevel@tonic-gate selkey->ipsl_icmp_type, selkey->ipsl_icmp_type_end, 14720Sstevel@tonic-gate (selkey->ipsl_valid & IPSL_ICMP_CODE) ? 14730Sstevel@tonic-gate selkey->ipsl_icmp_code : 255, 14740Sstevel@tonic-gate (selkey->ipsl_valid & IPSL_ICMP_CODE) ? 14750Sstevel@tonic-gate selkey->ipsl_icmp_code_end : 255); 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate return (offset); 14780Sstevel@tonic-gate } 14790Sstevel@tonic-gate 14800Sstevel@tonic-gate static uint_t 14810Sstevel@tonic-gate spdsock_encode_actattr(uint8_t *base, uint_t offset, uint32_t tag, 14820Sstevel@tonic-gate uint32_t value) 14830Sstevel@tonic-gate { 14840Sstevel@tonic-gate struct spd_attribute *attr; 14850Sstevel@tonic-gate 14860Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14870Sstevel@tonic-gate 14880Sstevel@tonic-gate if (base != NULL) { 14890Sstevel@tonic-gate attr = (struct spd_attribute *)(base + offset); 14900Sstevel@tonic-gate attr->spd_attr_tag = tag; 14910Sstevel@tonic-gate attr->spd_attr_value = value; 14920Sstevel@tonic-gate } 14930Sstevel@tonic-gate offset += sizeof (struct spd_attribute); 14940Sstevel@tonic-gate 14950Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate return (offset); 14980Sstevel@tonic-gate } 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate 15010Sstevel@tonic-gate #define EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v)) 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate static uint_t 15040Sstevel@tonic-gate spdsock_encode_action(uint8_t *base, uint_t offset, const ipsec_action_t *ap) 15050Sstevel@tonic-gate { 15060Sstevel@tonic-gate const struct ipsec_act *act = &(ap->ipa_act); 15070Sstevel@tonic-gate uint_t flags; 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate EMIT(SPD_ATTR_EMPTY, 0); 15100Sstevel@tonic-gate switch (act->ipa_type) { 15110Sstevel@tonic-gate case IPSEC_ACT_DISCARD: 15120Sstevel@tonic-gate case IPSEC_ACT_REJECT: 15130Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_DROP); 15140Sstevel@tonic-gate break; 15150Sstevel@tonic-gate case IPSEC_ACT_BYPASS: 15160Sstevel@tonic-gate case IPSEC_ACT_CLEAR: 15170Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_PASS); 15180Sstevel@tonic-gate break; 15190Sstevel@tonic-gate 15200Sstevel@tonic-gate case IPSEC_ACT_APPLY: 15210Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_IPSEC); 15220Sstevel@tonic-gate flags = 0; 15230Sstevel@tonic-gate if (act->ipa_apply.ipp_use_ah) 15240Sstevel@tonic-gate flags |= SPD_APPLY_AH; 15250Sstevel@tonic-gate if (act->ipa_apply.ipp_use_esp) 15260Sstevel@tonic-gate flags |= SPD_APPLY_ESP; 15270Sstevel@tonic-gate if (act->ipa_apply.ipp_use_espa) 15280Sstevel@tonic-gate flags |= SPD_APPLY_ESPA; 15290Sstevel@tonic-gate if (act->ipa_apply.ipp_use_se) 15300Sstevel@tonic-gate flags |= SPD_APPLY_SE; 15310Sstevel@tonic-gate if (act->ipa_apply.ipp_use_unique) 15320Sstevel@tonic-gate flags |= SPD_APPLY_UNIQUE; 15330Sstevel@tonic-gate EMIT(SPD_ATTR_FLAGS, flags); 15340Sstevel@tonic-gate if (flags & SPD_APPLY_AH) { 15350Sstevel@tonic-gate EMIT(SPD_ATTR_AH_AUTH, act->ipa_apply.ipp_auth_alg); 15360Sstevel@tonic-gate EMIT(SPD_ATTR_AH_MINBITS, 15370Sstevel@tonic-gate act->ipa_apply.ipp_ah_minbits); 15380Sstevel@tonic-gate EMIT(SPD_ATTR_AH_MAXBITS, 15390Sstevel@tonic-gate act->ipa_apply.ipp_ah_maxbits); 15400Sstevel@tonic-gate } 15410Sstevel@tonic-gate if (flags & SPD_APPLY_ESP) { 15420Sstevel@tonic-gate EMIT(SPD_ATTR_ESP_ENCR, act->ipa_apply.ipp_encr_alg); 15430Sstevel@tonic-gate EMIT(SPD_ATTR_ENCR_MINBITS, 15440Sstevel@tonic-gate act->ipa_apply.ipp_espe_minbits); 15450Sstevel@tonic-gate EMIT(SPD_ATTR_ENCR_MAXBITS, 15460Sstevel@tonic-gate act->ipa_apply.ipp_espe_maxbits); 15470Sstevel@tonic-gate if (flags & SPD_APPLY_ESPA) { 15480Sstevel@tonic-gate EMIT(SPD_ATTR_ESP_AUTH, 15490Sstevel@tonic-gate act->ipa_apply.ipp_esp_auth_alg); 15500Sstevel@tonic-gate EMIT(SPD_ATTR_ESPA_MINBITS, 15510Sstevel@tonic-gate act->ipa_apply.ipp_espa_minbits); 15520Sstevel@tonic-gate EMIT(SPD_ATTR_ESPA_MAXBITS, 15530Sstevel@tonic-gate act->ipa_apply.ipp_espa_maxbits); 15540Sstevel@tonic-gate } 15550Sstevel@tonic-gate } 15560Sstevel@tonic-gate if (act->ipa_apply.ipp_km_proto != 0) 15570Sstevel@tonic-gate EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_proto); 15580Sstevel@tonic-gate if (act->ipa_apply.ipp_km_cookie != 0) 15590Sstevel@tonic-gate EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_cookie); 15600Sstevel@tonic-gate if (act->ipa_apply.ipp_replay_depth != 0) 15610Sstevel@tonic-gate EMIT(SPD_ATTR_REPLAY_DEPTH, 15620Sstevel@tonic-gate act->ipa_apply.ipp_replay_depth); 15630Sstevel@tonic-gate /* Add more here */ 15640Sstevel@tonic-gate break; 15650Sstevel@tonic-gate } 15660Sstevel@tonic-gate 15670Sstevel@tonic-gate return (offset); 15680Sstevel@tonic-gate } 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate static uint_t 15710Sstevel@tonic-gate spdsock_encode_action_list(uint8_t *base, uint_t offset, 15720Sstevel@tonic-gate const ipsec_action_t *ap) 15730Sstevel@tonic-gate { 15740Sstevel@tonic-gate struct spd_ext_actions *act; 15750Sstevel@tonic-gate uint_t nact = 0; 15760Sstevel@tonic-gate uint_t start = offset; 15770Sstevel@tonic-gate 15780Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15790Sstevel@tonic-gate 15800Sstevel@tonic-gate if (base != NULL) { 15810Sstevel@tonic-gate act = (struct spd_ext_actions *)(base + offset); 15820Sstevel@tonic-gate act->spd_actions_len = 0; 15830Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 15840Sstevel@tonic-gate act->spd_actions_count = 0; 15850Sstevel@tonic-gate act->spd_actions_reserved = 0; 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate 15880Sstevel@tonic-gate offset += sizeof (*act); 15890Sstevel@tonic-gate 15900Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate while (ap != NULL) { 15930Sstevel@tonic-gate offset = spdsock_encode_action(base, offset, ap); 15940Sstevel@tonic-gate ap = ap->ipa_next; 15950Sstevel@tonic-gate nact++; 15960Sstevel@tonic-gate if (ap != NULL) { 15970Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); 15980Sstevel@tonic-gate } 15990Sstevel@tonic-gate } 16000Sstevel@tonic-gate EMIT(SPD_ATTR_END, 0); 16010Sstevel@tonic-gate 16020Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 16030Sstevel@tonic-gate 16040Sstevel@tonic-gate if (base != NULL) { 16050Sstevel@tonic-gate act->spd_actions_count = nact; 16060Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(offset - start); 16070Sstevel@tonic-gate } 16080Sstevel@tonic-gate 16090Sstevel@tonic-gate return (offset); 16100Sstevel@tonic-gate } 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate #undef EMIT 16130Sstevel@tonic-gate 16140Sstevel@tonic-gate /* ARGSUSED */ 16150Sstevel@tonic-gate static uint_t 16160Sstevel@tonic-gate spdsock_rule_flags(uint_t dir, uint_t af) 16170Sstevel@tonic-gate { 16180Sstevel@tonic-gate uint_t flags = 0; 16190Sstevel@tonic-gate 16200Sstevel@tonic-gate if (dir == IPSEC_TYPE_INBOUND) 16210Sstevel@tonic-gate flags |= SPD_RULE_FLAG_INBOUND; 16220Sstevel@tonic-gate if (dir == IPSEC_TYPE_OUTBOUND) 16230Sstevel@tonic-gate flags |= SPD_RULE_FLAG_OUTBOUND; 16240Sstevel@tonic-gate 16250Sstevel@tonic-gate return (flags); 16260Sstevel@tonic-gate } 16270Sstevel@tonic-gate 16280Sstevel@tonic-gate 16290Sstevel@tonic-gate static uint_t 16303055Sdanmcd spdsock_encode_rule_head(uint8_t *base, uint_t offset, spd_msg_t *req, 16313055Sdanmcd const ipsec_policy_t *rule, uint_t dir, uint_t af, char *name, 16323055Sdanmcd boolean_t tunnel) 16330Sstevel@tonic-gate { 16340Sstevel@tonic-gate struct spd_msg *spmsg; 16350Sstevel@tonic-gate struct spd_rule *spr; 16363055Sdanmcd spd_if_t *sid; 16370Sstevel@tonic-gate 16380Sstevel@tonic-gate uint_t start = offset; 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate if (base != NULL) { 16430Sstevel@tonic-gate spmsg = (struct spd_msg *)(base + offset); 16440Sstevel@tonic-gate bzero(spmsg, sizeof (*spmsg)); 16450Sstevel@tonic-gate spmsg->spd_msg_version = PF_POLICY_V1; 16460Sstevel@tonic-gate spmsg->spd_msg_type = SPD_DUMP; 16470Sstevel@tonic-gate spmsg->spd_msg_seq = req->spd_msg_seq; 16480Sstevel@tonic-gate spmsg->spd_msg_pid = req->spd_msg_pid; 16490Sstevel@tonic-gate } 16500Sstevel@tonic-gate offset += sizeof (struct spd_msg); 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 16530Sstevel@tonic-gate 16540Sstevel@tonic-gate if (base != NULL) { 16550Sstevel@tonic-gate spr = (struct spd_rule *)(base + offset); 16560Sstevel@tonic-gate spr->spd_rule_type = SPD_EXT_RULE; 16570Sstevel@tonic-gate spr->spd_rule_priority = rule->ipsp_prio; 16580Sstevel@tonic-gate spr->spd_rule_flags = spdsock_rule_flags(dir, af); 16593055Sdanmcd if (tunnel) 16603055Sdanmcd spr->spd_rule_flags |= SPD_RULE_FLAG_TUNNEL; 16610Sstevel@tonic-gate spr->spd_rule_unused = 0; 16620Sstevel@tonic-gate spr->spd_rule_len = SPD_8TO64(sizeof (*spr)); 16630Sstevel@tonic-gate spr->spd_rule_index = rule->ipsp_index; 16640Sstevel@tonic-gate } 16650Sstevel@tonic-gate offset += sizeof (struct spd_rule); 16663055Sdanmcd 16673055Sdanmcd /* 16683055Sdanmcd * If we have an interface name (i.e. if this policy head came from 16693055Sdanmcd * a tunnel), add the SPD_EXT_TUN_NAME extension. 16703055Sdanmcd */ 16713055Sdanmcd if (name[0] != '\0') { 16723055Sdanmcd 16733055Sdanmcd ASSERT(ALIGNED64(offset)); 16743055Sdanmcd 16753055Sdanmcd if (base != NULL) { 16763055Sdanmcd sid = (spd_if_t *)(base + offset); 16773055Sdanmcd sid->spd_if_exttype = SPD_EXT_TUN_NAME; 16783055Sdanmcd sid->spd_if_len = SPD_8TO64(sizeof (spd_if_t) + 16793055Sdanmcd roundup((strlen(name) - 4), 8)); 16803055Sdanmcd (void) strlcpy((char *)sid->spd_if_name, name, 16813055Sdanmcd LIFNAMSIZ); 16823055Sdanmcd } 16833055Sdanmcd 16843055Sdanmcd offset += sizeof (spd_if_t) + roundup((strlen(name) - 4), 8); 16853055Sdanmcd } 16863055Sdanmcd 16870Sstevel@tonic-gate offset = spdsock_encode_sel(base, offset, rule->ipsp_sel); 16880Sstevel@tonic-gate offset = spdsock_encode_action_list(base, offset, rule->ipsp_act); 16890Sstevel@tonic-gate 16900Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate if (base != NULL) { 16930Sstevel@tonic-gate spmsg->spd_msg_len = SPD_8TO64(offset - start); 16940Sstevel@tonic-gate } 16950Sstevel@tonic-gate return (offset); 16960Sstevel@tonic-gate } 16970Sstevel@tonic-gate 16980Sstevel@tonic-gate /* ARGSUSED */ 16990Sstevel@tonic-gate static mblk_t * 17000Sstevel@tonic-gate spdsock_encode_rule(mblk_t *req, const ipsec_policy_t *rule, 17013055Sdanmcd uint_t dir, uint_t af, char *name, boolean_t tunnel) 17020Sstevel@tonic-gate { 17030Sstevel@tonic-gate mblk_t *m; 17040Sstevel@tonic-gate uint_t len; 17050Sstevel@tonic-gate spd_msg_t *mreq = (spd_msg_t *)req->b_rptr; 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate /* 17080Sstevel@tonic-gate * Figure out how much space we'll need. 17090Sstevel@tonic-gate */ 17103055Sdanmcd len = spdsock_encode_rule_head(NULL, 0, mreq, rule, dir, af, name, 17113055Sdanmcd tunnel); 17120Sstevel@tonic-gate 17130Sstevel@tonic-gate /* 17140Sstevel@tonic-gate * Allocate mblk. 17150Sstevel@tonic-gate */ 17160Sstevel@tonic-gate m = allocb(len, BPRI_HI); 17170Sstevel@tonic-gate if (m == NULL) 17180Sstevel@tonic-gate return (NULL); 17190Sstevel@tonic-gate 17200Sstevel@tonic-gate /* 17210Sstevel@tonic-gate * Fill it in.. 17220Sstevel@tonic-gate */ 17230Sstevel@tonic-gate m->b_wptr = m->b_rptr + len; 17240Sstevel@tonic-gate bzero(m->b_rptr, len); 17253055Sdanmcd (void) spdsock_encode_rule_head(m->b_rptr, 0, mreq, rule, dir, af, 17263055Sdanmcd name, tunnel); 17270Sstevel@tonic-gate return (m); 17280Sstevel@tonic-gate } 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate static ipsec_policy_t * 173181Ssommerfe spdsock_dump_next_in_chain(spdsock_t *ss, ipsec_policy_head_t *iph, 173281Ssommerfe ipsec_policy_t *cur) 173381Ssommerfe { 173481Ssommerfe ASSERT(RW_READ_HELD(&iph->iph_lock)); 173581Ssommerfe 173681Ssommerfe ss->spdsock_dump_count++; 173781Ssommerfe ss->spdsock_dump_cur_rule = cur->ipsp_hash.hash_next; 173881Ssommerfe return (cur); 173981Ssommerfe } 174081Ssommerfe 174181Ssommerfe static ipsec_policy_t * 17420Sstevel@tonic-gate spdsock_dump_next_rule(spdsock_t *ss, ipsec_policy_head_t *iph) 17430Sstevel@tonic-gate { 17440Sstevel@tonic-gate ipsec_policy_t *cur; 174581Ssommerfe ipsec_policy_root_t *ipr; 174681Ssommerfe int chain, nchains, type, af; 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate ASSERT(RW_READ_HELD(&iph->iph_lock)); 17490Sstevel@tonic-gate 17500Sstevel@tonic-gate cur = ss->spdsock_dump_cur_rule; 17510Sstevel@tonic-gate 175281Ssommerfe if (cur != NULL) 175381Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 175481Ssommerfe 175581Ssommerfe type = ss->spdsock_dump_cur_type; 175681Ssommerfe 175781Ssommerfe next: 175881Ssommerfe chain = ss->spdsock_dump_cur_chain; 175981Ssommerfe ipr = &iph->iph_root[type]; 176081Ssommerfe nchains = ipr->ipr_nchains; 176181Ssommerfe 176281Ssommerfe while (chain < nchains) { 176381Ssommerfe cur = ipr->ipr_hash[chain].hash_head; 176481Ssommerfe chain++; 176581Ssommerfe if (cur != NULL) { 176681Ssommerfe ss->spdsock_dump_cur_chain = chain; 176781Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 176881Ssommerfe } 17690Sstevel@tonic-gate } 177081Ssommerfe ss->spdsock_dump_cur_chain = nchains; 177181Ssommerfe 177281Ssommerfe af = ss->spdsock_dump_cur_af; 177381Ssommerfe while (af < IPSEC_NAF) { 177481Ssommerfe cur = ipr->ipr_nonhash[af]; 177581Ssommerfe af++; 177681Ssommerfe if (cur != NULL) { 177781Ssommerfe ss->spdsock_dump_cur_af = af; 177881Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 177981Ssommerfe } 178081Ssommerfe } 178181Ssommerfe 178281Ssommerfe type++; 178381Ssommerfe if (type >= IPSEC_NTYPES) 178481Ssommerfe return (NULL); 178581Ssommerfe 178681Ssommerfe ss->spdsock_dump_cur_chain = 0; 178781Ssommerfe ss->spdsock_dump_cur_type = type; 178881Ssommerfe ss->spdsock_dump_cur_af = IPSEC_AF_V4; 178981Ssommerfe goto next; 179081Ssommerfe 17910Sstevel@tonic-gate } 17920Sstevel@tonic-gate 17930Sstevel@tonic-gate static mblk_t * 17940Sstevel@tonic-gate spdsock_dump_next_record(spdsock_t *ss) 17950Sstevel@tonic-gate { 17960Sstevel@tonic-gate ipsec_policy_head_t *iph; 17970Sstevel@tonic-gate ipsec_policy_t *rule; 17980Sstevel@tonic-gate mblk_t *m; 17990Sstevel@tonic-gate mblk_t *req = ss->spdsock_dump_req; 18000Sstevel@tonic-gate 18010Sstevel@tonic-gate iph = ss->spdsock_dump_head; 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate ASSERT(iph != NULL); 18040Sstevel@tonic-gate 18050Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 18060Sstevel@tonic-gate 18070Sstevel@tonic-gate if (iph->iph_gen != ss->spdsock_dump_gen) { 18080Sstevel@tonic-gate rw_exit(&iph->iph_lock); 18090Sstevel@tonic-gate return (spdsock_dump_finish(ss, EAGAIN)); 18100Sstevel@tonic-gate } 18110Sstevel@tonic-gate 18120Sstevel@tonic-gate rule = spdsock_dump_next_rule(ss, iph); 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate if (!rule) { 18150Sstevel@tonic-gate rw_exit(&iph->iph_lock); 18160Sstevel@tonic-gate return (spdsock_dump_finish(ss, 0)); 18170Sstevel@tonic-gate } 18180Sstevel@tonic-gate 18190Sstevel@tonic-gate m = spdsock_encode_rule(req, rule, ss->spdsock_dump_cur_type, 18203055Sdanmcd ss->spdsock_dump_cur_af, ss->spdsock_dump_name, 18213055Sdanmcd ss->spdsock_dump_tunnel); 18220Sstevel@tonic-gate rw_exit(&iph->iph_lock); 18230Sstevel@tonic-gate 18240Sstevel@tonic-gate if (m == NULL) 18250Sstevel@tonic-gate return (spdsock_dump_finish(ss, ENOMEM)); 18260Sstevel@tonic-gate return (m); 18270Sstevel@tonic-gate } 18280Sstevel@tonic-gate 18290Sstevel@tonic-gate /* 18300Sstevel@tonic-gate * Dump records until we run into flow-control back-pressure. 18310Sstevel@tonic-gate */ 18320Sstevel@tonic-gate static void 18330Sstevel@tonic-gate spdsock_dump_some(queue_t *q, spdsock_t *ss) 18340Sstevel@tonic-gate { 18350Sstevel@tonic-gate mblk_t *m, *dataind; 18360Sstevel@tonic-gate 18370Sstevel@tonic-gate while ((ss->spdsock_dump_req != NULL) && canputnext(q)) { 18380Sstevel@tonic-gate m = spdsock_dump_next_record(ss); 18390Sstevel@tonic-gate if (m == NULL) 18400Sstevel@tonic-gate return; 18410Sstevel@tonic-gate dataind = allocb(sizeof (struct T_data_req), BPRI_HI); 18420Sstevel@tonic-gate if (dataind == NULL) { 18430Sstevel@tonic-gate freemsg(m); 18440Sstevel@tonic-gate return; 18450Sstevel@tonic-gate } 18460Sstevel@tonic-gate dataind->b_cont = m; 18470Sstevel@tonic-gate dataind->b_wptr += sizeof (struct T_data_req); 18480Sstevel@tonic-gate ((struct T_data_ind *)dataind->b_rptr)->PRIM_type = T_DATA_IND; 18490Sstevel@tonic-gate ((struct T_data_ind *)dataind->b_rptr)->MORE_flag = 0; 18500Sstevel@tonic-gate dataind->b_datap->db_type = M_PROTO; 18510Sstevel@tonic-gate putnext(q, dataind); 18520Sstevel@tonic-gate } 18530Sstevel@tonic-gate } 18540Sstevel@tonic-gate 18550Sstevel@tonic-gate /* 18560Sstevel@tonic-gate * Start dumping. 18570Sstevel@tonic-gate * Format a start-of-dump record, and set up the stream and kick the rsrv 18580Sstevel@tonic-gate * procedure to continue the job.. 18590Sstevel@tonic-gate */ 18600Sstevel@tonic-gate /* ARGSUSED */ 18610Sstevel@tonic-gate static void 18623055Sdanmcd spdsock_dump(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp) 18630Sstevel@tonic-gate { 18640Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 1865*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 1866*3448Sdh155122 netstack_t *ns = spds->spds_netstack; 1867*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 18680Sstevel@tonic-gate mblk_t *mr; 18690Sstevel@tonic-gate 18703055Sdanmcd /* spdsock_parse() already NULL-terminated spdsock_dump_name. */ 18713055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) { 1872*3448Sdh155122 rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER); 18733055Sdanmcd ss->spdsock_dump_remaining_polheads = 1 + 1874*3448Sdh155122 avl_numnodes(&ipss->ipsec_tunnel_policies); 1875*3448Sdh155122 ss->spdsock_dump_tun_gen = ipss->ipsec_tunnel_policy_gen; 1876*3448Sdh155122 rw_exit(&ipss->ipsec_tunnel_policy_lock); 18773055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS) { 1878*3448Sdh155122 iph = ipsec_system_policy(ns); 18793055Sdanmcd ss->spdsock_dump_active = B_TRUE; 18803055Sdanmcd } else { 1881*3448Sdh155122 iph = ipsec_inactive_policy(spds->spds_netstack); 18823055Sdanmcd ss->spdsock_dump_active = B_FALSE; 18833055Sdanmcd } 18843055Sdanmcd ASSERT(ss->spdsock_dump_name[0] == '\0'); 18853055Sdanmcd } else { 18863055Sdanmcd ss->spdsock_dump_remaining_polheads = 1; 18873055Sdanmcd } 18883055Sdanmcd 18890Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 18900Sstevel@tonic-gate 18910Sstevel@tonic-gate mr = spdsock_dump_ruleset(mp, iph, 0, 0); 18920Sstevel@tonic-gate 18930Sstevel@tonic-gate if (!mr) { 18940Sstevel@tonic-gate rw_exit(&iph->iph_lock); 18950Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 18960Sstevel@tonic-gate return; 18970Sstevel@tonic-gate } 18980Sstevel@tonic-gate 18990Sstevel@tonic-gate ss->spdsock_dump_req = mp; 19000Sstevel@tonic-gate ss->spdsock_dump_head = iph; 19010Sstevel@tonic-gate ss->spdsock_dump_gen = iph->iph_gen; 19020Sstevel@tonic-gate ss->spdsock_dump_cur_type = 0; 19030Sstevel@tonic-gate ss->spdsock_dump_cur_af = IPSEC_AF_V4; 1904660Spr131582 ss->spdsock_dump_cur_rule = NULL; 19050Sstevel@tonic-gate ss->spdsock_dump_count = 0; 190681Ssommerfe ss->spdsock_dump_cur_chain = 0; 19070Sstevel@tonic-gate rw_exit(&iph->iph_lock); 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate qreply(q, mr); 19100Sstevel@tonic-gate qenable(OTHERQ(q)); 19110Sstevel@tonic-gate } 19120Sstevel@tonic-gate 19133055Sdanmcd /* Do NOT consume a reference to ITP. */ 19140Sstevel@tonic-gate void 1915*3448Sdh155122 spdsock_clone_node(ipsec_tun_pol_t *itp, void *ep, netstack_t *ns) 19163055Sdanmcd { 19173055Sdanmcd int *errptr = (int *)ep; 19183055Sdanmcd 19193055Sdanmcd if (*errptr != 0) 19203055Sdanmcd return; /* We've failed already for some reason. */ 19213055Sdanmcd mutex_enter(&itp->itp_lock); 19223055Sdanmcd ITPF_CLONE(itp->itp_flags); 1923*3448Sdh155122 *errptr = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns); 19243055Sdanmcd mutex_exit(&itp->itp_lock); 19253055Sdanmcd } 19263055Sdanmcd 19273055Sdanmcd void 19283055Sdanmcd spdsock_clone(queue_t *q, mblk_t *mp, spd_if_t *tunname) 19290Sstevel@tonic-gate { 19303055Sdanmcd int error; 19313055Sdanmcd char *tname; 19323055Sdanmcd ipsec_tun_pol_t *itp; 1933*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 1934*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 19353055Sdanmcd 19363055Sdanmcd if (tunname != NULL) { 19373055Sdanmcd tname = (char *)tunname->spd_if_name; 19383055Sdanmcd if (*tname == '\0') { 1939*3448Sdh155122 error = ipsec_clone_system_policy(spds->spds_netstack); 19403055Sdanmcd if (error == 0) 1941*3448Sdh155122 itp_walk(spdsock_clone_node, &error, 1942*3448Sdh155122 spds->spds_netstack); 19433055Sdanmcd } else { 1944*3448Sdh155122 itp = get_tunnel_policy(tname, spds->spds_netstack); 19453055Sdanmcd if (itp == NULL) { 19463055Sdanmcd spdsock_error(q, mp, ENOENT, 0); 19473055Sdanmcd return; 19483055Sdanmcd } 1949*3448Sdh155122 spdsock_clone_node(itp, &error, NULL); 1950*3448Sdh155122 ITP_REFRELE(itp, spds->spds_netstack); 19513055Sdanmcd } 19523055Sdanmcd } else { 1953*3448Sdh155122 error = ipsec_clone_system_policy(spds->spds_netstack); 19543055Sdanmcd } 19553055Sdanmcd 19560Sstevel@tonic-gate if (error != 0) 19570Sstevel@tonic-gate spdsock_error(q, mp, error, 0); 19580Sstevel@tonic-gate else 19590Sstevel@tonic-gate spd_echo(q, mp); 19600Sstevel@tonic-gate } 19610Sstevel@tonic-gate 19620Sstevel@tonic-gate /* 19630Sstevel@tonic-gate * Process a SPD_ALGLIST request. The caller expects separate alg entries 19640Sstevel@tonic-gate * for AH authentication, ESP authentication, and ESP encryption. 19650Sstevel@tonic-gate * The same distinction is then used when setting the min and max key 19660Sstevel@tonic-gate * sizes when defining policies. 19670Sstevel@tonic-gate */ 19680Sstevel@tonic-gate 19690Sstevel@tonic-gate #define SPDSOCK_AH_AUTH 0 19700Sstevel@tonic-gate #define SPDSOCK_ESP_AUTH 1 19710Sstevel@tonic-gate #define SPDSOCK_ESP_ENCR 2 19720Sstevel@tonic-gate #define SPDSOCK_NTYPES 3 19730Sstevel@tonic-gate 19740Sstevel@tonic-gate static const uint_t algattr[SPDSOCK_NTYPES] = { 19750Sstevel@tonic-gate SPD_ATTR_AH_AUTH, 19760Sstevel@tonic-gate SPD_ATTR_ESP_AUTH, 19770Sstevel@tonic-gate SPD_ATTR_ESP_ENCR 19780Sstevel@tonic-gate }; 19790Sstevel@tonic-gate static const uint_t minbitsattr[SPDSOCK_NTYPES] = { 19800Sstevel@tonic-gate SPD_ATTR_AH_MINBITS, 19810Sstevel@tonic-gate SPD_ATTR_ESPA_MINBITS, 19820Sstevel@tonic-gate SPD_ATTR_ENCR_MINBITS 19830Sstevel@tonic-gate }; 19840Sstevel@tonic-gate static const uint_t maxbitsattr[SPDSOCK_NTYPES] = { 19850Sstevel@tonic-gate SPD_ATTR_AH_MAXBITS, 19860Sstevel@tonic-gate SPD_ATTR_ESPA_MAXBITS, 19870Sstevel@tonic-gate SPD_ATTR_ENCR_MAXBITS 19880Sstevel@tonic-gate }; 19890Sstevel@tonic-gate static const uint_t defbitsattr[SPDSOCK_NTYPES] = { 19900Sstevel@tonic-gate SPD_ATTR_AH_DEFBITS, 19910Sstevel@tonic-gate SPD_ATTR_ESPA_DEFBITS, 19920Sstevel@tonic-gate SPD_ATTR_ENCR_DEFBITS 19930Sstevel@tonic-gate }; 19940Sstevel@tonic-gate static const uint_t incrbitsattr[SPDSOCK_NTYPES] = { 19950Sstevel@tonic-gate SPD_ATTR_AH_INCRBITS, 19960Sstevel@tonic-gate SPD_ATTR_ESPA_INCRBITS, 19970Sstevel@tonic-gate SPD_ATTR_ENCR_INCRBITS 19980Sstevel@tonic-gate }; 19990Sstevel@tonic-gate 20000Sstevel@tonic-gate #define ATTRPERALG 6 /* fixed attributes per algs */ 20010Sstevel@tonic-gate 20020Sstevel@tonic-gate void 20030Sstevel@tonic-gate spdsock_alglist(queue_t *q, mblk_t *mp) 20040Sstevel@tonic-gate { 20050Sstevel@tonic-gate uint_t algtype; 20060Sstevel@tonic-gate uint_t algidx; 20070Sstevel@tonic-gate uint_t algcount; 20080Sstevel@tonic-gate uint_t size; 20090Sstevel@tonic-gate mblk_t *m; 20100Sstevel@tonic-gate uint8_t *cur; 20110Sstevel@tonic-gate spd_msg_t *msg; 20120Sstevel@tonic-gate struct spd_ext_actions *act; 20130Sstevel@tonic-gate struct spd_attribute *attr; 2014*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2015*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2016*3448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 2017*3448Sdh155122 2018*3448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 20190Sstevel@tonic-gate /* 20200Sstevel@tonic-gate * The SPD client expects to receive separate entries for 20210Sstevel@tonic-gate * AH authentication and ESP authentication supported algorithms. 20220Sstevel@tonic-gate * 20230Sstevel@tonic-gate * Don't return the "any" algorithms, if defined, as no 20240Sstevel@tonic-gate * kernel policies can be set for these algorithms. 20250Sstevel@tonic-gate */ 2026*3448Sdh155122 algcount = 2 * ipss->ipsec_nalgs[IPSEC_ALG_AUTH] + 2027*3448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 2028*3448Sdh155122 2029*3448Sdh155122 if (ipss->ipsec_alglists[IPSEC_ALG_AUTH][SADB_AALG_NONE] != NULL) 20300Sstevel@tonic-gate algcount--; 2031*3448Sdh155122 if (ipss->ipsec_alglists[IPSEC_ALG_ENCR][SADB_EALG_NONE] != NULL) 20320Sstevel@tonic-gate algcount--; 20330Sstevel@tonic-gate 20340Sstevel@tonic-gate /* 20350Sstevel@tonic-gate * For each algorithm, we encode: 20360Sstevel@tonic-gate * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT} 20370Sstevel@tonic-gate */ 20380Sstevel@tonic-gate 20390Sstevel@tonic-gate size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions) + 20400Sstevel@tonic-gate ATTRPERALG * sizeof (struct spd_attribute) * algcount; 20410Sstevel@tonic-gate 20420Sstevel@tonic-gate ASSERT(ALIGNED64(size)); 20430Sstevel@tonic-gate 20440Sstevel@tonic-gate m = allocb(size, BPRI_HI); 20450Sstevel@tonic-gate if (m == NULL) { 2046*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 20470Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 20480Sstevel@tonic-gate return; 20490Sstevel@tonic-gate } 20500Sstevel@tonic-gate 20510Sstevel@tonic-gate m->b_wptr = m->b_rptr + size; 20520Sstevel@tonic-gate cur = m->b_rptr; 20530Sstevel@tonic-gate 20540Sstevel@tonic-gate msg = (spd_msg_t *)cur; 20550Sstevel@tonic-gate bcopy(mp->b_rptr, cur, sizeof (*msg)); 20560Sstevel@tonic-gate 20570Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(size); 20580Sstevel@tonic-gate msg->spd_msg_errno = 0; 20590Sstevel@tonic-gate msg->spd_msg_diagnostic = 0; 20600Sstevel@tonic-gate 20610Sstevel@tonic-gate cur += sizeof (*msg); 20620Sstevel@tonic-gate 20630Sstevel@tonic-gate act = (struct spd_ext_actions *)cur; 20640Sstevel@tonic-gate cur += sizeof (*act); 20650Sstevel@tonic-gate 20660Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t)); 20670Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 20680Sstevel@tonic-gate act->spd_actions_count = algcount; 20690Sstevel@tonic-gate act->spd_actions_reserved = 0; 20700Sstevel@tonic-gate 20710Sstevel@tonic-gate attr = (struct spd_attribute *)cur; 20720Sstevel@tonic-gate 20730Sstevel@tonic-gate #define EMIT(tag, value) { \ 20740Sstevel@tonic-gate attr->spd_attr_tag = (tag); \ 20750Sstevel@tonic-gate attr->spd_attr_value = (value); \ 20760Sstevel@tonic-gate attr++; \ 20770Sstevel@tonic-gate } 20780Sstevel@tonic-gate 20790Sstevel@tonic-gate /* 20800Sstevel@tonic-gate * If you change the number of EMIT's here, change 20810Sstevel@tonic-gate * ATTRPERALG above to match 20820Sstevel@tonic-gate */ 20830Sstevel@tonic-gate #define EMITALGATTRS(_type) { \ 20840Sstevel@tonic-gate EMIT(algattr[_type], algid); /* 1 */ \ 20850Sstevel@tonic-gate EMIT(minbitsattr[_type], minbits); /* 2 */ \ 20860Sstevel@tonic-gate EMIT(maxbitsattr[_type], maxbits); /* 3 */ \ 20870Sstevel@tonic-gate EMIT(defbitsattr[_type], defbits); /* 4 */ \ 20880Sstevel@tonic-gate EMIT(incrbitsattr[_type], incr); /* 5 */ \ 20890Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); /* 6 */ \ 20900Sstevel@tonic-gate } 20910Sstevel@tonic-gate 20920Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 2093*3448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 2094*3448Sdh155122 algidx++) { 2095*3448Sdh155122 int algid = ipss->ipsec_sortlist[algtype][algidx]; 2096*3448Sdh155122 ipsec_alginfo_t *alg = 2097*3448Sdh155122 ipss->ipsec_alglists[algtype][algid]; 20980Sstevel@tonic-gate uint_t minbits = alg->alg_minbits; 20990Sstevel@tonic-gate uint_t maxbits = alg->alg_maxbits; 21000Sstevel@tonic-gate uint_t defbits = alg->alg_default_bits; 21010Sstevel@tonic-gate uint_t incr = alg->alg_increment; 21020Sstevel@tonic-gate 21030Sstevel@tonic-gate if (algtype == IPSEC_ALG_AUTH) { 21040Sstevel@tonic-gate if (algid == SADB_AALG_NONE) 21050Sstevel@tonic-gate continue; 21060Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_AH_AUTH); 21070Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_ESP_AUTH); 21080Sstevel@tonic-gate } else { 21090Sstevel@tonic-gate if (algid == SADB_EALG_NONE) 21100Sstevel@tonic-gate continue; 21110Sstevel@tonic-gate ASSERT(algtype == IPSEC_ALG_ENCR); 21120Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_ESP_ENCR); 21130Sstevel@tonic-gate } 21140Sstevel@tonic-gate } 21150Sstevel@tonic-gate } 21160Sstevel@tonic-gate 2117*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 21180Sstevel@tonic-gate 21190Sstevel@tonic-gate #undef EMITALGATTRS 21200Sstevel@tonic-gate #undef EMIT 21210Sstevel@tonic-gate #undef ATTRPERALG 21220Sstevel@tonic-gate 21230Sstevel@tonic-gate attr--; 21240Sstevel@tonic-gate attr->spd_attr_tag = SPD_ATTR_END; 21250Sstevel@tonic-gate 21260Sstevel@tonic-gate freemsg(mp); 21270Sstevel@tonic-gate qreply(q, m); 21280Sstevel@tonic-gate } 21290Sstevel@tonic-gate 21300Sstevel@tonic-gate /* 21310Sstevel@tonic-gate * Process a SPD_DUMPALGS request. 21320Sstevel@tonic-gate */ 21330Sstevel@tonic-gate 21340Sstevel@tonic-gate #define ATTRPERALG 7 /* fixed attributes per algs */ 21350Sstevel@tonic-gate 21360Sstevel@tonic-gate void 21370Sstevel@tonic-gate spdsock_dumpalgs(queue_t *q, mblk_t *mp) 21380Sstevel@tonic-gate { 21390Sstevel@tonic-gate uint_t algtype; 21400Sstevel@tonic-gate uint_t algidx; 21410Sstevel@tonic-gate uint_t size; 21420Sstevel@tonic-gate mblk_t *m; 21430Sstevel@tonic-gate uint8_t *cur; 21440Sstevel@tonic-gate spd_msg_t *msg; 21450Sstevel@tonic-gate struct spd_ext_actions *act; 21460Sstevel@tonic-gate struct spd_attribute *attr; 21470Sstevel@tonic-gate ipsec_alginfo_t *alg; 21480Sstevel@tonic-gate uint_t algid; 21490Sstevel@tonic-gate uint_t i; 21500Sstevel@tonic-gate uint_t alg_size; 2151*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2152*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2153*3448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 2154*3448Sdh155122 2155*3448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 21560Sstevel@tonic-gate 21570Sstevel@tonic-gate /* 21580Sstevel@tonic-gate * For each algorithm, we encode: 21590Sstevel@tonic-gate * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT} 21600Sstevel@tonic-gate * 21610Sstevel@tonic-gate * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE* 21620Sstevel@tonic-gate * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_MECHNAME / {END, NEXT} 21630Sstevel@tonic-gate */ 21640Sstevel@tonic-gate 21650Sstevel@tonic-gate /* 21660Sstevel@tonic-gate * Compute the size of the SPD message. 21670Sstevel@tonic-gate */ 21680Sstevel@tonic-gate size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions); 21690Sstevel@tonic-gate 21700Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 2171*3448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 2172*3448Sdh155122 algidx++) { 2173*3448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 2174*3448Sdh155122 alg = ipss->ipsec_alglists[algtype][algid]; 21750Sstevel@tonic-gate alg_size = sizeof (struct spd_attribute) * 21760Sstevel@tonic-gate (ATTRPERALG + alg->alg_nkey_sizes + 21770Sstevel@tonic-gate alg->alg_nblock_sizes) + CRYPTO_MAX_MECH_NAME; 21780Sstevel@tonic-gate size += alg_size; 21790Sstevel@tonic-gate } 21800Sstevel@tonic-gate } 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate ASSERT(ALIGNED64(size)); 21830Sstevel@tonic-gate 21840Sstevel@tonic-gate m = allocb(size, BPRI_HI); 21850Sstevel@tonic-gate if (m == NULL) { 2186*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 21870Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 21880Sstevel@tonic-gate return; 21890Sstevel@tonic-gate } 21900Sstevel@tonic-gate 21910Sstevel@tonic-gate m->b_wptr = m->b_rptr + size; 21920Sstevel@tonic-gate cur = m->b_rptr; 21930Sstevel@tonic-gate 21940Sstevel@tonic-gate msg = (spd_msg_t *)cur; 21950Sstevel@tonic-gate bcopy(mp->b_rptr, cur, sizeof (*msg)); 21960Sstevel@tonic-gate 21970Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(size); 21980Sstevel@tonic-gate msg->spd_msg_errno = 0; 21990Sstevel@tonic-gate msg->spd_msg_diagnostic = 0; 22000Sstevel@tonic-gate 22010Sstevel@tonic-gate cur += sizeof (*msg); 22020Sstevel@tonic-gate 22030Sstevel@tonic-gate act = (struct spd_ext_actions *)cur; 22040Sstevel@tonic-gate cur += sizeof (*act); 22050Sstevel@tonic-gate 22060Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t)); 22070Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 2208*3448Sdh155122 act->spd_actions_count = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] + 2209*3448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 22100Sstevel@tonic-gate act->spd_actions_reserved = 0; 22110Sstevel@tonic-gate 22120Sstevel@tonic-gate attr = (struct spd_attribute *)cur; 22130Sstevel@tonic-gate 22140Sstevel@tonic-gate #define EMIT(tag, value) { \ 22150Sstevel@tonic-gate attr->spd_attr_tag = (tag); \ 22160Sstevel@tonic-gate attr->spd_attr_value = (value); \ 22170Sstevel@tonic-gate attr++; \ 22180Sstevel@tonic-gate } 22190Sstevel@tonic-gate 22200Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 2221*3448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 2222*3448Sdh155122 algidx++) { 2223*3448Sdh155122 2224*3448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 2225*3448Sdh155122 alg = ipss->ipsec_alglists[algtype][algid]; 22260Sstevel@tonic-gate 22270Sstevel@tonic-gate /* 22280Sstevel@tonic-gate * If you change the number of EMIT's here, change 22290Sstevel@tonic-gate * ATTRPERALG above to match 22300Sstevel@tonic-gate */ 22310Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_ID, algid); 22320Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_PROTO, algproto[algtype]); 22330Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_INCRBITS, alg->alg_increment); 22340Sstevel@tonic-gate 22350Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_NKEYSIZES, alg->alg_nkey_sizes); 22360Sstevel@tonic-gate for (i = 0; i < alg->alg_nkey_sizes; i++) 22370Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_KEYSIZE, 22380Sstevel@tonic-gate alg->alg_key_sizes[i]); 22390Sstevel@tonic-gate 22400Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_NBLOCKSIZES, alg->alg_nblock_sizes); 22410Sstevel@tonic-gate for (i = 0; i < alg->alg_nblock_sizes; i++) 22420Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_BLOCKSIZE, 22430Sstevel@tonic-gate alg->alg_block_sizes[i]); 22440Sstevel@tonic-gate 22450Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_MECHNAME, CRYPTO_MAX_MECH_NAME); 22460Sstevel@tonic-gate bcopy(alg->alg_mech_name, attr, CRYPTO_MAX_MECH_NAME); 22470Sstevel@tonic-gate attr = (struct spd_attribute *)((char *)attr + 22480Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME); 22490Sstevel@tonic-gate 22500Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); 22510Sstevel@tonic-gate } 22520Sstevel@tonic-gate } 22530Sstevel@tonic-gate 2254*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 22550Sstevel@tonic-gate 22560Sstevel@tonic-gate #undef EMITALGATTRS 22570Sstevel@tonic-gate #undef EMIT 22580Sstevel@tonic-gate #undef ATTRPERALG 22590Sstevel@tonic-gate 22600Sstevel@tonic-gate attr--; 22610Sstevel@tonic-gate attr->spd_attr_tag = SPD_ATTR_END; 22620Sstevel@tonic-gate 22630Sstevel@tonic-gate freemsg(mp); 22640Sstevel@tonic-gate qreply(q, m); 22650Sstevel@tonic-gate } 22660Sstevel@tonic-gate 22670Sstevel@tonic-gate /* 22680Sstevel@tonic-gate * Do the actual work of processing an SPD_UPDATEALGS request. Can 22690Sstevel@tonic-gate * be invoked either once IPsec is loaded on a cached request, or 22700Sstevel@tonic-gate * when a request is received while IPsec is loaded. 22710Sstevel@tonic-gate */ 22720Sstevel@tonic-gate static void 2273*3448Sdh155122 spdsock_do_updatealg(spd_ext_t *extv[], int *diag, spd_stack_t *spds) 22740Sstevel@tonic-gate { 22750Sstevel@tonic-gate struct spd_ext_actions *actp; 22760Sstevel@tonic-gate struct spd_attribute *attr, *endattr; 22770Sstevel@tonic-gate uint64_t *start, *end; 22780Sstevel@tonic-gate ipsec_alginfo_t *alg = NULL; 22790Sstevel@tonic-gate ipsec_algtype_t alg_type = 0; 22800Sstevel@tonic-gate boolean_t skip_alg = B_TRUE, doing_proto = B_FALSE; 22810Sstevel@tonic-gate uint_t i, cur_key, cur_block, algid; 22820Sstevel@tonic-gate 22830Sstevel@tonic-gate *diag = -1; 2284*3448Sdh155122 ASSERT(MUTEX_HELD(&spds->spds_alg_lock)); 22850Sstevel@tonic-gate 22860Sstevel@tonic-gate /* parse the message, building the list of algorithms */ 22870Sstevel@tonic-gate 22880Sstevel@tonic-gate actp = (struct spd_ext_actions *)extv[SPD_EXT_ACTION]; 22890Sstevel@tonic-gate if (actp == NULL) { 22900Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_NO_ACTION_EXT; 22910Sstevel@tonic-gate return; 22920Sstevel@tonic-gate } 22930Sstevel@tonic-gate 22940Sstevel@tonic-gate start = (uint64_t *)actp; 22950Sstevel@tonic-gate end = (start + actp->spd_actions_len); 22960Sstevel@tonic-gate endattr = (struct spd_attribute *)end; 22970Sstevel@tonic-gate attr = (struct spd_attribute *)&actp[1]; 22980Sstevel@tonic-gate 2299*3448Sdh155122 bzero(spds->spds_algs, IPSEC_NALGTYPES * IPSEC_MAX_ALGS * 23000Sstevel@tonic-gate sizeof (ipsec_alginfo_t *)); 23010Sstevel@tonic-gate 23020Sstevel@tonic-gate alg = kmem_zalloc(sizeof (*alg), KM_SLEEP); 23030Sstevel@tonic-gate 23040Sstevel@tonic-gate #define ALG_KEY_SIZES(a) (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t)) 23050Sstevel@tonic-gate #define ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t)) 23060Sstevel@tonic-gate 23070Sstevel@tonic-gate while (attr < endattr) { 23080Sstevel@tonic-gate switch (attr->spd_attr_tag) { 23090Sstevel@tonic-gate case SPD_ATTR_NOP: 23100Sstevel@tonic-gate case SPD_ATTR_EMPTY: 23110Sstevel@tonic-gate break; 23120Sstevel@tonic-gate case SPD_ATTR_END: 23130Sstevel@tonic-gate attr = endattr; 23140Sstevel@tonic-gate /* FALLTHRU */ 23150Sstevel@tonic-gate case SPD_ATTR_NEXT: 23160Sstevel@tonic-gate if (doing_proto) { 23170Sstevel@tonic-gate doing_proto = B_FALSE; 23180Sstevel@tonic-gate break; 23190Sstevel@tonic-gate } 23200Sstevel@tonic-gate if (skip_alg) { 23210Sstevel@tonic-gate ipsec_alg_free(alg); 23220Sstevel@tonic-gate } else { 23230Sstevel@tonic-gate ipsec_alg_free( 2324*3448Sdh155122 spds->spds_algs[alg_type][alg->alg_id]); 2325*3448Sdh155122 spds->spds_algs[alg_type][alg->alg_id] = 2326*3448Sdh155122 alg; 23270Sstevel@tonic-gate } 23280Sstevel@tonic-gate alg = kmem_zalloc(sizeof (*alg), KM_SLEEP); 23290Sstevel@tonic-gate break; 23300Sstevel@tonic-gate 23310Sstevel@tonic-gate case SPD_ATTR_ALG_ID: 23320Sstevel@tonic-gate if (attr->spd_attr_value >= IPSEC_MAX_ALGS) { 2333*3448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 23340Sstevel@tonic-gate "invalid alg id %d\n", 23350Sstevel@tonic-gate attr->spd_attr_value)); 23360Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_ID_RANGE; 23370Sstevel@tonic-gate goto bail; 23380Sstevel@tonic-gate } 23390Sstevel@tonic-gate alg->alg_id = attr->spd_attr_value; 23400Sstevel@tonic-gate break; 23410Sstevel@tonic-gate 23420Sstevel@tonic-gate case SPD_ATTR_ALG_PROTO: 23430Sstevel@tonic-gate /* find the alg type */ 23440Sstevel@tonic-gate for (i = 0; i < NALGPROTOS; i++) 23450Sstevel@tonic-gate if (algproto[i] == attr->spd_attr_value) 23460Sstevel@tonic-gate break; 23470Sstevel@tonic-gate skip_alg = (i == NALGPROTOS); 23480Sstevel@tonic-gate if (!skip_alg) 23490Sstevel@tonic-gate alg_type = i; 23500Sstevel@tonic-gate break; 23510Sstevel@tonic-gate 23520Sstevel@tonic-gate case SPD_ATTR_ALG_INCRBITS: 23530Sstevel@tonic-gate alg->alg_increment = attr->spd_attr_value; 23540Sstevel@tonic-gate break; 23550Sstevel@tonic-gate 23560Sstevel@tonic-gate case SPD_ATTR_ALG_NKEYSIZES: 23570Sstevel@tonic-gate if (alg->alg_key_sizes != NULL) { 23580Sstevel@tonic-gate kmem_free(alg->alg_key_sizes, 23590Sstevel@tonic-gate ALG_KEY_SIZES(alg)); 23600Sstevel@tonic-gate } 23610Sstevel@tonic-gate alg->alg_nkey_sizes = attr->spd_attr_value; 23620Sstevel@tonic-gate /* 23630Sstevel@tonic-gate * Allocate room for the trailing zero key size 23640Sstevel@tonic-gate * value as well. 23650Sstevel@tonic-gate */ 23660Sstevel@tonic-gate alg->alg_key_sizes = kmem_zalloc(ALG_KEY_SIZES(alg), 23670Sstevel@tonic-gate KM_SLEEP); 23680Sstevel@tonic-gate cur_key = 0; 23690Sstevel@tonic-gate break; 23700Sstevel@tonic-gate 23710Sstevel@tonic-gate case SPD_ATTR_ALG_KEYSIZE: 23720Sstevel@tonic-gate if (alg->alg_key_sizes == NULL || 23730Sstevel@tonic-gate cur_key >= alg->alg_nkey_sizes) { 2374*3448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 23750Sstevel@tonic-gate "too many key sizes\n")); 23760Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES; 23770Sstevel@tonic-gate goto bail; 23780Sstevel@tonic-gate } 23790Sstevel@tonic-gate alg->alg_key_sizes[cur_key++] = attr->spd_attr_value; 23800Sstevel@tonic-gate break; 23810Sstevel@tonic-gate 23820Sstevel@tonic-gate case SPD_ATTR_ALG_NBLOCKSIZES: 23830Sstevel@tonic-gate if (alg->alg_block_sizes != NULL) { 23840Sstevel@tonic-gate kmem_free(alg->alg_block_sizes, 23850Sstevel@tonic-gate ALG_BLOCK_SIZES(alg)); 23860Sstevel@tonic-gate } 23870Sstevel@tonic-gate alg->alg_nblock_sizes = attr->spd_attr_value; 23880Sstevel@tonic-gate /* 23890Sstevel@tonic-gate * Allocate room for the trailing zero block size 23900Sstevel@tonic-gate * value as well. 23910Sstevel@tonic-gate */ 23920Sstevel@tonic-gate alg->alg_block_sizes = kmem_zalloc(ALG_BLOCK_SIZES(alg), 23930Sstevel@tonic-gate KM_SLEEP); 23940Sstevel@tonic-gate cur_block = 0; 23950Sstevel@tonic-gate break; 23960Sstevel@tonic-gate 23970Sstevel@tonic-gate case SPD_ATTR_ALG_BLOCKSIZE: 23980Sstevel@tonic-gate if (alg->alg_block_sizes == NULL || 23990Sstevel@tonic-gate cur_block >= alg->alg_nblock_sizes) { 2400*3448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 24010Sstevel@tonic-gate "too many block sizes\n")); 24020Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES; 24030Sstevel@tonic-gate goto bail; 24040Sstevel@tonic-gate } 24050Sstevel@tonic-gate alg->alg_block_sizes[cur_block++] = 24060Sstevel@tonic-gate attr->spd_attr_value; 24070Sstevel@tonic-gate break; 24080Sstevel@tonic-gate 24090Sstevel@tonic-gate case SPD_ATTR_ALG_MECHNAME: { 24100Sstevel@tonic-gate char *mech_name; 24110Sstevel@tonic-gate 24120Sstevel@tonic-gate if (attr->spd_attr_value > CRYPTO_MAX_MECH_NAME) { 2413*3448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 24140Sstevel@tonic-gate "mech name too long\n")); 24150Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN; 24160Sstevel@tonic-gate goto bail; 24170Sstevel@tonic-gate } 24180Sstevel@tonic-gate mech_name = (char *)(attr + 1); 24190Sstevel@tonic-gate bcopy(mech_name, alg->alg_mech_name, 24200Sstevel@tonic-gate attr->spd_attr_value); 24210Sstevel@tonic-gate alg->alg_mech_name[CRYPTO_MAX_MECH_NAME-1] = '\0'; 24220Sstevel@tonic-gate attr = (struct spd_attribute *)((char *)attr + 24230Sstevel@tonic-gate attr->spd_attr_value); 24240Sstevel@tonic-gate break; 24250Sstevel@tonic-gate } 24260Sstevel@tonic-gate 24270Sstevel@tonic-gate case SPD_ATTR_PROTO_ID: 24280Sstevel@tonic-gate doing_proto = B_TRUE; 24290Sstevel@tonic-gate for (i = 0; i < NALGPROTOS; i++) { 24300Sstevel@tonic-gate if (algproto[i] == attr->spd_attr_value) { 24310Sstevel@tonic-gate alg_type = i; 24320Sstevel@tonic-gate break; 24330Sstevel@tonic-gate } 24340Sstevel@tonic-gate } 24350Sstevel@tonic-gate break; 24360Sstevel@tonic-gate 24370Sstevel@tonic-gate case SPD_ATTR_PROTO_EXEC_MODE: 24380Sstevel@tonic-gate if (!doing_proto) 24390Sstevel@tonic-gate break; 24400Sstevel@tonic-gate for (i = 0; i < NEXECMODES; i++) { 24410Sstevel@tonic-gate if (execmodes[i] == attr->spd_attr_value) { 2442*3448Sdh155122 spds->spds_algs_exec_mode[alg_type] = i; 24430Sstevel@tonic-gate break; 24440Sstevel@tonic-gate } 24450Sstevel@tonic-gate } 24460Sstevel@tonic-gate break; 24470Sstevel@tonic-gate } 24480Sstevel@tonic-gate attr++; 24490Sstevel@tonic-gate } 24500Sstevel@tonic-gate 24510Sstevel@tonic-gate #undef ALG_KEY_SIZES 24520Sstevel@tonic-gate #undef ALG_BLOCK_SIZES 24530Sstevel@tonic-gate 24540Sstevel@tonic-gate /* update the algorithm tables */ 2455*3448Sdh155122 spdsock_merge_algs(spds); 24560Sstevel@tonic-gate bail: 24570Sstevel@tonic-gate /* cleanup */ 24580Sstevel@tonic-gate ipsec_alg_free(alg); 24590Sstevel@tonic-gate for (alg_type = 0; alg_type < IPSEC_NALGTYPES; alg_type++) 2460*3448Sdh155122 for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) 2461*3448Sdh155122 if (spds->spds_algs[alg_type][algid] != NULL) 2462*3448Sdh155122 ipsec_alg_free(spds->spds_algs[alg_type][algid]); 24630Sstevel@tonic-gate } 24640Sstevel@tonic-gate 24650Sstevel@tonic-gate /* 24660Sstevel@tonic-gate * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue 24670Sstevel@tonic-gate * the request until IPsec loads. If IPsec is loaded, act on it 24680Sstevel@tonic-gate * immediately. 24690Sstevel@tonic-gate */ 24700Sstevel@tonic-gate 24710Sstevel@tonic-gate static void 24720Sstevel@tonic-gate spdsock_updatealg(queue_t *q, mblk_t *mp, spd_ext_t *extv[]) 24730Sstevel@tonic-gate { 2474*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2475*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2476*3448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 2477*3448Sdh155122 2478*3448Sdh155122 if (!ipsec_loaded(ipss)) { 24790Sstevel@tonic-gate /* 24800Sstevel@tonic-gate * IPsec is not loaded, save request and return nicely, 24810Sstevel@tonic-gate * the message will be processed once IPsec loads. 24820Sstevel@tonic-gate */ 24830Sstevel@tonic-gate mblk_t *new_mp; 24840Sstevel@tonic-gate 24850Sstevel@tonic-gate /* last update message wins */ 24860Sstevel@tonic-gate if ((new_mp = copymsg(mp)) == NULL) { 24870Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 24880Sstevel@tonic-gate return; 24890Sstevel@tonic-gate } 2490*3448Sdh155122 mutex_enter(&spds->spds_alg_lock); 2491*3448Sdh155122 bcopy(extv, spds->spds_extv_algs, 24920Sstevel@tonic-gate sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1)); 2493*3448Sdh155122 if (spds->spds_mp_algs != NULL) 2494*3448Sdh155122 freemsg(spds->spds_mp_algs); 2495*3448Sdh155122 spds->spds_mp_algs = mp; 2496*3448Sdh155122 spds->spds_algs_pending = B_TRUE; 2497*3448Sdh155122 mutex_exit(&spds->spds_alg_lock); 24980Sstevel@tonic-gate 24990Sstevel@tonic-gate spd_echo(q, new_mp); 25000Sstevel@tonic-gate } else { 25010Sstevel@tonic-gate /* 25020Sstevel@tonic-gate * IPsec is loaded, act on the message immediately. 25030Sstevel@tonic-gate */ 25040Sstevel@tonic-gate int diag; 25050Sstevel@tonic-gate 2506*3448Sdh155122 mutex_enter(&spds->spds_alg_lock); 2507*3448Sdh155122 spdsock_do_updatealg(extv, &diag, spds); 2508*3448Sdh155122 mutex_exit(&spds->spds_alg_lock); 25090Sstevel@tonic-gate if (diag == -1) 25100Sstevel@tonic-gate spd_echo(q, mp); 25110Sstevel@tonic-gate else 25120Sstevel@tonic-gate spdsock_diag(q, mp, diag); 25130Sstevel@tonic-gate } 25140Sstevel@tonic-gate } 25150Sstevel@tonic-gate 25163055Sdanmcd /* 25173055Sdanmcd * With a reference-held ill, dig down and find an instance of "tun", and 25183055Sdanmcd * assign its tunnel policy pointer, while reference-holding it. Also, 25193055Sdanmcd * release ill's refrence when finished. 25203055Sdanmcd * 25213055Sdanmcd * We'll be messing with q_next, so be VERY careful. 25223055Sdanmcd */ 25233055Sdanmcd static void 25243055Sdanmcd find_tun_and_set_itp(ill_t *ill, ipsec_tun_pol_t *itp) 25253055Sdanmcd { 25263055Sdanmcd queue_t *q; 25273055Sdanmcd tun_t *tun; 25283055Sdanmcd 25293055Sdanmcd /* Don't bother if this ill is going away. */ 25303055Sdanmcd if (ill->ill_flags & ILL_CONDEMNED) { 25313055Sdanmcd ill_refrele(ill); 25323055Sdanmcd return; 25333055Sdanmcd } 25343055Sdanmcd 25353055Sdanmcd 25363055Sdanmcd q = ill->ill_wq; 25373055Sdanmcd claimstr(q); /* Lighter-weight than freezestr(). */ 25383055Sdanmcd 25393055Sdanmcd do { 25403055Sdanmcd /* Use strcmp() because "tun" is bounded. */ 25413055Sdanmcd if (strcmp(q->q_qinfo->qi_minfo->mi_idname, "tun") == 0) { 25423055Sdanmcd /* Aha! Got it. */ 25433055Sdanmcd tun = (tun_t *)q->q_ptr; 25443055Sdanmcd if (tun != NULL) { 25453055Sdanmcd mutex_enter(&tun->tun_lock); 25463055Sdanmcd if (tun->tun_itp != itp) { 25473055Sdanmcd ASSERT(tun->tun_itp == NULL); 25483055Sdanmcd ITP_REFHOLD(itp); 25493055Sdanmcd tun->tun_itp = itp; 25503055Sdanmcd } 25513055Sdanmcd mutex_exit(&tun->tun_lock); 25523055Sdanmcd goto release_and_return; 25533055Sdanmcd } 25543055Sdanmcd /* 25553055Sdanmcd * Else assume this is some other module named "tun" 25563055Sdanmcd * and move on, hoping we find one that actually has 25573055Sdanmcd * something in q_ptr. 25583055Sdanmcd */ 25593055Sdanmcd } 25603055Sdanmcd q = q->q_next; 25613055Sdanmcd } while (q != NULL); 25623055Sdanmcd 25633055Sdanmcd release_and_return: 25643055Sdanmcd releasestr(ill->ill_wq); 25653055Sdanmcd ill_refrele(ill); 25663055Sdanmcd } 25673055Sdanmcd 25683055Sdanmcd /* 25693055Sdanmcd * Sort through the mess of polhead options to retrieve an appropriate one. 25703055Sdanmcd * Returns NULL if we send an spdsock error. Returns a valid pointer if we 25713055Sdanmcd * found a valid polhead. Returns ALL_ACTIVE_POLHEADS (aka. -1) or 25723055Sdanmcd * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to 25733055Sdanmcd * act on ALL policy heads. 25743055Sdanmcd */ 25753055Sdanmcd static ipsec_policy_head_t * 25763055Sdanmcd get_appropriate_polhead(queue_t *q, mblk_t *mp, spd_if_t *tunname, int spdid, 25773055Sdanmcd int msgtype, ipsec_tun_pol_t **itpp) 25783055Sdanmcd { 25793055Sdanmcd ipsec_tun_pol_t *itp; 25803055Sdanmcd ipsec_policy_head_t *iph; 25813055Sdanmcd int errno; 25823055Sdanmcd char *tname; 25833055Sdanmcd boolean_t active; 25843055Sdanmcd spdsock_t *ss = (spdsock_t *)q->q_ptr; 2585*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2586*3448Sdh155122 netstack_t *ns = spds->spds_netstack; 25873055Sdanmcd uint64_t gen; /* Placeholder */ 25883055Sdanmcd ill_t *v4, *v6; 25893055Sdanmcd 25903055Sdanmcd active = (spdid == SPD_ACTIVE); 25913055Sdanmcd *itpp = NULL; 25923055Sdanmcd if (!active && spdid != SPD_STANDBY) { 25933055Sdanmcd spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_SPDID); 25943055Sdanmcd return (NULL); 25953055Sdanmcd } 25963055Sdanmcd 25973055Sdanmcd if (tunname != NULL) { 25983055Sdanmcd /* Acting on a tunnel's SPD. */ 25993055Sdanmcd tname = (char *)tunname->spd_if_name; 26003055Sdanmcd if (*tname == '\0') { 26013055Sdanmcd /* Handle all-polhead cases here. */ 26023055Sdanmcd if (msgtype != SPD_FLUSH && msgtype != SPD_DUMP) { 26033055Sdanmcd spdsock_diag(q, mp, 26043055Sdanmcd SPD_DIAGNOSTIC_NOT_GLOBAL_OP); 26053055Sdanmcd return (NULL); 26063055Sdanmcd } 26073055Sdanmcd return (active ? ALL_ACTIVE_POLHEADS : 26083055Sdanmcd ALL_INACTIVE_POLHEADS); 26093055Sdanmcd } 26103055Sdanmcd 2611*3448Sdh155122 itp = get_tunnel_policy(tname, spds->spds_netstack); 26123055Sdanmcd if (itp == NULL) { 26133055Sdanmcd if (msgtype != SPD_ADDRULE) { 26143055Sdanmcd /* "Tunnel not found" */ 26153055Sdanmcd spdsock_error(q, mp, ENOENT, 0); 26163055Sdanmcd return (NULL); 26173055Sdanmcd } 26183055Sdanmcd 26193055Sdanmcd errno = 0; 2620*3448Sdh155122 itp = create_tunnel_policy(tname, &errno, &gen, 2621*3448Sdh155122 spds->spds_netstack); 26223055Sdanmcd if (itp == NULL) { 26233055Sdanmcd /* 26243055Sdanmcd * Something very bad happened, most likely 26253055Sdanmcd * ENOMEM. Return an indicator. 26263055Sdanmcd */ 26273055Sdanmcd spdsock_error(q, mp, errno, 0); 26283055Sdanmcd return (NULL); 26293055Sdanmcd } 26303055Sdanmcd } 26313055Sdanmcd /* 26323055Sdanmcd * Troll the plumbed tunnels and see if we have a 26333055Sdanmcd * match. We need to do this always in case we add 26343055Sdanmcd * policy AFTER plumbing a tunnel. 26353055Sdanmcd */ 26363055Sdanmcd v4 = ill_lookup_on_name(tname, B_FALSE, B_FALSE, NULL, 2637*3448Sdh155122 NULL, NULL, &errno, NULL, ns->netstack_ip); 26383055Sdanmcd if (v4 != NULL) 26393055Sdanmcd find_tun_and_set_itp(v4, itp); 26403055Sdanmcd v6 = ill_lookup_on_name(tname, B_FALSE, B_TRUE, NULL, 2641*3448Sdh155122 NULL, NULL, &errno, NULL, ns->netstack_ip); 26423055Sdanmcd if (v6 != NULL) 26433055Sdanmcd find_tun_and_set_itp(v6, itp); 26443055Sdanmcd ASSERT(itp != NULL); 26453055Sdanmcd *itpp = itp; 26463055Sdanmcd /* For spdsock dump state, set the polhead's name. */ 26473055Sdanmcd if (msgtype == SPD_DUMP) { 26483055Sdanmcd (void) strncpy(ss->spdsock_dump_name, tname, LIFNAMSIZ); 26493055Sdanmcd ss->spdsock_dump_tunnel = itp->itp_flags & 26503055Sdanmcd (active ? ITPF_P_TUNNEL : ITPF_I_TUNNEL); 26513055Sdanmcd } 26523055Sdanmcd } else { 26533055Sdanmcd itp = NULL; 26543055Sdanmcd /* For spdsock dump state, indicate it's global policy. */ 26553055Sdanmcd if (msgtype == SPD_DUMP) 26563055Sdanmcd ss->spdsock_dump_name[0] = '\0'; 26573055Sdanmcd } 26583055Sdanmcd 26593055Sdanmcd if (active) 2660*3448Sdh155122 iph = (itp == NULL) ? ipsec_system_policy(ns) : itp->itp_policy; 26613055Sdanmcd else 2662*3448Sdh155122 iph = (itp == NULL) ? ipsec_inactive_policy(ns) : 26633055Sdanmcd itp->itp_inactive; 26643055Sdanmcd 26653055Sdanmcd ASSERT(iph != NULL); 26663055Sdanmcd if (itp != NULL) { 26673055Sdanmcd IPPH_REFHOLD(iph); 26683055Sdanmcd } 26693055Sdanmcd 26703055Sdanmcd return (iph); 26713055Sdanmcd } 26723055Sdanmcd 26730Sstevel@tonic-gate static void 26740Sstevel@tonic-gate spdsock_parse(queue_t *q, mblk_t *mp) 26750Sstevel@tonic-gate { 26760Sstevel@tonic-gate spd_msg_t *spmsg; 26770Sstevel@tonic-gate spd_ext_t *extv[SPD_EXT_MAX + 1]; 26780Sstevel@tonic-gate uint_t msgsize; 26790Sstevel@tonic-gate ipsec_policy_head_t *iph; 26803055Sdanmcd ipsec_tun_pol_t *itp; 26813055Sdanmcd spd_if_t *tunname; 2682*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2683*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2684*3448Sdh155122 netstack_t *ns = spds->spds_netstack; 2685*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 26860Sstevel@tonic-gate 26870Sstevel@tonic-gate /* Make sure nothing's below me. */ 26880Sstevel@tonic-gate ASSERT(WR(q)->q_next == NULL); 26890Sstevel@tonic-gate 26900Sstevel@tonic-gate spmsg = (spd_msg_t *)mp->b_rptr; 26910Sstevel@tonic-gate 26920Sstevel@tonic-gate msgsize = SPD_64TO8(spmsg->spd_msg_len); 26930Sstevel@tonic-gate 26940Sstevel@tonic-gate if (msgdsize(mp) != msgsize) { 26950Sstevel@tonic-gate /* 26960Sstevel@tonic-gate * Message len incorrect w.r.t. actual size. Send an error 26970Sstevel@tonic-gate * (EMSGSIZE). It may be necessary to massage things a 26980Sstevel@tonic-gate * bit. For example, if the spd_msg_type is hosed, 26990Sstevel@tonic-gate * I need to set it to SPD_RESERVED to get delivery to 27000Sstevel@tonic-gate * do the right thing. Then again, maybe just letting 27010Sstevel@tonic-gate * the error delivery do the right thing. 27020Sstevel@tonic-gate */ 2703*3448Sdh155122 ss2dbg(spds, 2704*3448Sdh155122 ("mblk (%lu) and base (%d) message sizes don't jibe.\n", 27050Sstevel@tonic-gate msgdsize(mp), msgsize)); 27060Sstevel@tonic-gate spdsock_error(q, mp, EMSGSIZE, SPD_DIAGNOSTIC_NONE); 27070Sstevel@tonic-gate return; 27080Sstevel@tonic-gate } 27090Sstevel@tonic-gate 27100Sstevel@tonic-gate if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) { 27110Sstevel@tonic-gate /* Get all message into one mblk. */ 27120Sstevel@tonic-gate if (pullupmsg(mp, -1) == 0) { 27130Sstevel@tonic-gate /* 27140Sstevel@tonic-gate * Something screwy happened. 27150Sstevel@tonic-gate */ 2716*3448Sdh155122 ss3dbg(spds, ("spdsock_parse: pullupmsg() failed.\n")); 27170Sstevel@tonic-gate return; 27180Sstevel@tonic-gate } else { 27190Sstevel@tonic-gate spmsg = (spd_msg_t *)mp->b_rptr; 27200Sstevel@tonic-gate } 27210Sstevel@tonic-gate } 27220Sstevel@tonic-gate 27230Sstevel@tonic-gate switch (spdsock_get_ext(extv, spmsg, msgsize)) { 27240Sstevel@tonic-gate case KGE_DUP: 27250Sstevel@tonic-gate /* Handle duplicate extension. */ 2726*3448Sdh155122 ss1dbg(spds, ("Got duplicate extension of type %d.\n", 27270Sstevel@tonic-gate extv[0]->spd_ext_type)); 27280Sstevel@tonic-gate spdsock_diag(q, mp, dup_ext_diag[extv[0]->spd_ext_type]); 27290Sstevel@tonic-gate return; 27300Sstevel@tonic-gate case KGE_UNK: 27310Sstevel@tonic-gate /* Handle unknown extension. */ 2732*3448Sdh155122 ss1dbg(spds, ("Got unknown extension of type %d.\n", 27330Sstevel@tonic-gate extv[0]->spd_ext_type)); 27340Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_UNKNOWN_EXT); 27350Sstevel@tonic-gate return; 27360Sstevel@tonic-gate case KGE_LEN: 27370Sstevel@tonic-gate /* Length error. */ 2738*3448Sdh155122 ss1dbg(spds, ("Length %d on extension type %d overrun or 0.\n", 27390Sstevel@tonic-gate extv[0]->spd_ext_len, extv[0]->spd_ext_type)); 27400Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_EXTLEN); 27410Sstevel@tonic-gate return; 27420Sstevel@tonic-gate case KGE_CHK: 27430Sstevel@tonic-gate /* Reality check failed. */ 2744*3448Sdh155122 ss1dbg(spds, ("Reality check failed on extension type %d.\n", 27450Sstevel@tonic-gate extv[0]->spd_ext_type)); 27460Sstevel@tonic-gate spdsock_diag(q, mp, bad_ext_diag[extv[0]->spd_ext_type]); 27470Sstevel@tonic-gate return; 27480Sstevel@tonic-gate default: 27490Sstevel@tonic-gate /* Default case is no errors. */ 27500Sstevel@tonic-gate break; 27510Sstevel@tonic-gate } 27520Sstevel@tonic-gate 27530Sstevel@tonic-gate /* 27540Sstevel@tonic-gate * Special-case SPD_UPDATEALGS so as not to load IPsec. 27550Sstevel@tonic-gate */ 2756*3448Sdh155122 if (!ipsec_loaded(ipss) && spmsg->spd_msg_type != SPD_UPDATEALGS) { 27570Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 27580Sstevel@tonic-gate 27590Sstevel@tonic-gate ASSERT(ss != NULL); 2760*3448Sdh155122 ipsec_loader_loadnow(ipss); 27610Sstevel@tonic-gate ss->spdsock_timeout_arg = mp; 27620Sstevel@tonic-gate ss->spdsock_timeout = qtimeout(q, spdsock_loadcheck, 27630Sstevel@tonic-gate q, LOADCHECK_INTERVAL); 27640Sstevel@tonic-gate return; 27650Sstevel@tonic-gate } 27660Sstevel@tonic-gate 27673055Sdanmcd /* First check for messages that need no polheads at all. */ 27680Sstevel@tonic-gate switch (spmsg->spd_msg_type) { 27690Sstevel@tonic-gate case SPD_UPDATEALGS: 27700Sstevel@tonic-gate spdsock_updatealg(q, mp, extv); 27710Sstevel@tonic-gate return; 27720Sstevel@tonic-gate case SPD_ALGLIST: 27730Sstevel@tonic-gate spdsock_alglist(q, mp); 27740Sstevel@tonic-gate return; 27750Sstevel@tonic-gate case SPD_DUMPALGS: 27760Sstevel@tonic-gate spdsock_dumpalgs(q, mp); 27770Sstevel@tonic-gate return; 27783055Sdanmcd } 27793055Sdanmcd 27803055Sdanmcd /* 27813055Sdanmcd * Then check for ones that need both primary/secondary polheads, 27823055Sdanmcd * finding the appropriate tunnel policy if need be. 27833055Sdanmcd */ 27843055Sdanmcd tunname = (spd_if_t *)extv[SPD_EXT_TUN_NAME]; 27853055Sdanmcd switch (spmsg->spd_msg_type) { 27863055Sdanmcd case SPD_FLIP: 27873055Sdanmcd spdsock_flip(q, mp, tunname); 27883055Sdanmcd return; 27893055Sdanmcd case SPD_CLONE: 27903055Sdanmcd spdsock_clone(q, mp, tunname); 27913055Sdanmcd return; 27923055Sdanmcd } 27933055Sdanmcd 27943055Sdanmcd /* 27953055Sdanmcd * Finally, find ones that operate on exactly one polhead, or 27963055Sdanmcd * "all polheads" of a given type (active/inactive). 27973055Sdanmcd */ 27983055Sdanmcd iph = get_appropriate_polhead(q, mp, tunname, spmsg->spd_msg_spdid, 27993055Sdanmcd spmsg->spd_msg_type, &itp); 28003055Sdanmcd if (iph == NULL) 28013055Sdanmcd return; 28023055Sdanmcd 28033055Sdanmcd /* All-polheads-ready operations. */ 28043055Sdanmcd switch (spmsg->spd_msg_type) { 28053055Sdanmcd case SPD_FLUSH: 28063055Sdanmcd if (itp != NULL) { 28073055Sdanmcd mutex_enter(&itp->itp_lock); 28083055Sdanmcd if (spmsg->spd_msg_spdid == SPD_ACTIVE) 28093055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 28103055Sdanmcd else 28113055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 28123055Sdanmcd mutex_exit(&itp->itp_lock); 2813*3448Sdh155122 ITP_REFRELE(itp, ns); 28143055Sdanmcd } 28153055Sdanmcd spdsock_flush(q, iph, mp); 28163055Sdanmcd return; 28173055Sdanmcd case SPD_DUMP: 28183055Sdanmcd if (itp != NULL) 2819*3448Sdh155122 ITP_REFRELE(itp, ns); 28203055Sdanmcd spdsock_dump(q, iph, mp); 28213055Sdanmcd return; 28223055Sdanmcd } 28233055Sdanmcd 28243055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) { 28253055Sdanmcd spdsock_diag(q, mp, SPD_DIAGNOSTIC_NOT_GLOBAL_OP); 28263055Sdanmcd return; 28273055Sdanmcd } 28283055Sdanmcd 28293055Sdanmcd /* Single-polhead-only operations. */ 28303055Sdanmcd switch (spmsg->spd_msg_type) { 28313055Sdanmcd case SPD_ADDRULE: 28323055Sdanmcd spdsock_addrule(q, iph, mp, extv, itp); 28333055Sdanmcd break; 28343055Sdanmcd case SPD_DELETERULE: 28353055Sdanmcd spdsock_deleterule(q, iph, mp, extv, itp); 28363055Sdanmcd break; 28373055Sdanmcd case SPD_LOOKUP: 28383055Sdanmcd spdsock_lookup(q, iph, mp, extv, itp); 28393055Sdanmcd break; 28400Sstevel@tonic-gate default: 28410Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_MSG_TYPE); 28423055Sdanmcd break; 28430Sstevel@tonic-gate } 28443055Sdanmcd 2845*3448Sdh155122 IPPH_REFRELE(iph, spds->spds_netstack); 28463055Sdanmcd if (itp != NULL) 2847*3448Sdh155122 ITP_REFRELE(itp, ns); 28480Sstevel@tonic-gate } 28490Sstevel@tonic-gate 28500Sstevel@tonic-gate /* 28510Sstevel@tonic-gate * If an algorithm mapping was received before IPsec was loaded, process it. 28520Sstevel@tonic-gate * Called from the IPsec loader. 28530Sstevel@tonic-gate */ 28540Sstevel@tonic-gate void 2855*3448Sdh155122 spdsock_update_pending_algs(netstack_t *ns) 28560Sstevel@tonic-gate { 2857*3448Sdh155122 spd_stack_t *spds = ns->netstack_spdsock; 2858*3448Sdh155122 2859*3448Sdh155122 mutex_enter(&spds->spds_alg_lock); 2860*3448Sdh155122 if (spds->spds_algs_pending) { 28610Sstevel@tonic-gate int diag; 2862*3448Sdh155122 2863*3448Sdh155122 spdsock_do_updatealg(spds->spds_extv_algs, &diag, 2864*3448Sdh155122 spds); 2865*3448Sdh155122 spds->spds_algs_pending = B_FALSE; 28660Sstevel@tonic-gate } 2867*3448Sdh155122 mutex_exit(&spds->spds_alg_lock); 28680Sstevel@tonic-gate } 28690Sstevel@tonic-gate 28700Sstevel@tonic-gate static void 28710Sstevel@tonic-gate spdsock_loadcheck(void *arg) 28720Sstevel@tonic-gate { 28730Sstevel@tonic-gate queue_t *q = (queue_t *)arg; 28740Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 28750Sstevel@tonic-gate mblk_t *mp; 2876*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2877*3448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 28780Sstevel@tonic-gate 28790Sstevel@tonic-gate ASSERT(ss != NULL); 28800Sstevel@tonic-gate 28810Sstevel@tonic-gate ss->spdsock_timeout = 0; 28820Sstevel@tonic-gate mp = ss->spdsock_timeout_arg; 28830Sstevel@tonic-gate ASSERT(mp != NULL); 28840Sstevel@tonic-gate ss->spdsock_timeout_arg = NULL; 2885*3448Sdh155122 if (ipsec_failed(ipss)) 28860Sstevel@tonic-gate spdsock_error(q, mp, EPROTONOSUPPORT, 0); 28870Sstevel@tonic-gate else 28880Sstevel@tonic-gate spdsock_parse(q, mp); 28890Sstevel@tonic-gate } 28900Sstevel@tonic-gate 28910Sstevel@tonic-gate /* 28920Sstevel@tonic-gate * Copy relevant state bits. 28930Sstevel@tonic-gate */ 28940Sstevel@tonic-gate static void 28950Sstevel@tonic-gate spdsock_copy_info(struct T_info_ack *tap, spdsock_t *ss) 28960Sstevel@tonic-gate { 28970Sstevel@tonic-gate *tap = spdsock_g_t_info_ack; 28980Sstevel@tonic-gate tap->CURRENT_state = ss->spdsock_state; 28990Sstevel@tonic-gate tap->OPT_size = spdsock_max_optsize; 29000Sstevel@tonic-gate } 29010Sstevel@tonic-gate 29020Sstevel@tonic-gate /* 29030Sstevel@tonic-gate * This routine responds to T_CAPABILITY_REQ messages. It is called by 29040Sstevel@tonic-gate * spdsock_wput. Much of the T_CAPABILITY_ACK information is copied from 29050Sstevel@tonic-gate * spdsock_g_t_info_ack. The current state of the stream is copied from 29060Sstevel@tonic-gate * spdsock_state. 29070Sstevel@tonic-gate */ 29080Sstevel@tonic-gate static void 29090Sstevel@tonic-gate spdsock_capability_req(queue_t *q, mblk_t *mp) 29100Sstevel@tonic-gate { 29110Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 29120Sstevel@tonic-gate t_uscalar_t cap_bits1; 29130Sstevel@tonic-gate struct T_capability_ack *tcap; 29140Sstevel@tonic-gate 29150Sstevel@tonic-gate cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 29160Sstevel@tonic-gate 29170Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 29180Sstevel@tonic-gate mp->b_datap->db_type, T_CAPABILITY_ACK); 29190Sstevel@tonic-gate if (mp == NULL) 29200Sstevel@tonic-gate return; 29210Sstevel@tonic-gate 29220Sstevel@tonic-gate tcap = (struct T_capability_ack *)mp->b_rptr; 29230Sstevel@tonic-gate tcap->CAP_bits1 = 0; 29240Sstevel@tonic-gate 29250Sstevel@tonic-gate if (cap_bits1 & TC1_INFO) { 29260Sstevel@tonic-gate spdsock_copy_info(&tcap->INFO_ack, ss); 29270Sstevel@tonic-gate tcap->CAP_bits1 |= TC1_INFO; 29280Sstevel@tonic-gate } 29290Sstevel@tonic-gate 29300Sstevel@tonic-gate qreply(q, mp); 29310Sstevel@tonic-gate } 29320Sstevel@tonic-gate 29330Sstevel@tonic-gate /* 29340Sstevel@tonic-gate * This routine responds to T_INFO_REQ messages. It is called by 29350Sstevel@tonic-gate * spdsock_wput_other. 29360Sstevel@tonic-gate * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack. 29370Sstevel@tonic-gate * The current state of the stream is copied from spdsock_state. 29380Sstevel@tonic-gate */ 29390Sstevel@tonic-gate static void 29400Sstevel@tonic-gate spdsock_info_req(q, mp) 29410Sstevel@tonic-gate queue_t *q; 29420Sstevel@tonic-gate mblk_t *mp; 29430Sstevel@tonic-gate { 29440Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 29450Sstevel@tonic-gate T_INFO_ACK); 29460Sstevel@tonic-gate if (mp == NULL) 29470Sstevel@tonic-gate return; 29480Sstevel@tonic-gate spdsock_copy_info((struct T_info_ack *)mp->b_rptr, 29490Sstevel@tonic-gate (spdsock_t *)q->q_ptr); 29500Sstevel@tonic-gate qreply(q, mp); 29510Sstevel@tonic-gate } 29520Sstevel@tonic-gate 29530Sstevel@tonic-gate /* 29540Sstevel@tonic-gate * spdsock_err_ack. This routine creates a 29550Sstevel@tonic-gate * T_ERROR_ACK message and passes it 29560Sstevel@tonic-gate * upstream. 29570Sstevel@tonic-gate */ 29580Sstevel@tonic-gate static void 29590Sstevel@tonic-gate spdsock_err_ack(q, mp, t_error, sys_error) 29600Sstevel@tonic-gate queue_t *q; 29610Sstevel@tonic-gate mblk_t *mp; 29620Sstevel@tonic-gate int t_error; 29630Sstevel@tonic-gate int sys_error; 29640Sstevel@tonic-gate { 29650Sstevel@tonic-gate if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 29660Sstevel@tonic-gate qreply(q, mp); 29670Sstevel@tonic-gate } 29680Sstevel@tonic-gate 29690Sstevel@tonic-gate /* 29700Sstevel@tonic-gate * This routine retrieves the current status of socket options. 29710Sstevel@tonic-gate * It returns the size of the option retrieved. 29720Sstevel@tonic-gate */ 29730Sstevel@tonic-gate /* ARGSUSED */ 29740Sstevel@tonic-gate int 29750Sstevel@tonic-gate spdsock_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 29760Sstevel@tonic-gate { 29770Sstevel@tonic-gate int *i1 = (int *)ptr; 29780Sstevel@tonic-gate 29790Sstevel@tonic-gate switch (level) { 29800Sstevel@tonic-gate case SOL_SOCKET: 29810Sstevel@tonic-gate switch (name) { 29820Sstevel@tonic-gate case SO_TYPE: 29830Sstevel@tonic-gate *i1 = SOCK_RAW; 29840Sstevel@tonic-gate break; 29850Sstevel@tonic-gate /* 29860Sstevel@tonic-gate * The following two items can be manipulated, 29870Sstevel@tonic-gate * but changing them should do nothing. 29880Sstevel@tonic-gate */ 29890Sstevel@tonic-gate case SO_SNDBUF: 29900Sstevel@tonic-gate *i1 = (int)q->q_hiwat; 29910Sstevel@tonic-gate break; 29920Sstevel@tonic-gate case SO_RCVBUF: 29930Sstevel@tonic-gate *i1 = (int)(RD(q)->q_hiwat); 29940Sstevel@tonic-gate break; 29950Sstevel@tonic-gate } 29960Sstevel@tonic-gate break; 29970Sstevel@tonic-gate default: 29980Sstevel@tonic-gate return (0); 29990Sstevel@tonic-gate } 30000Sstevel@tonic-gate return (sizeof (int)); 30010Sstevel@tonic-gate } 30020Sstevel@tonic-gate 30030Sstevel@tonic-gate /* 30040Sstevel@tonic-gate * This routine sets socket options. 30050Sstevel@tonic-gate */ 30060Sstevel@tonic-gate /* ARGSUSED */ 30070Sstevel@tonic-gate int 30080Sstevel@tonic-gate spdsock_opt_set(queue_t *q, uint_t mgmt_flags, int level, int name, 30090Sstevel@tonic-gate uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 30100Sstevel@tonic-gate void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 30110Sstevel@tonic-gate { 30120Sstevel@tonic-gate int *i1 = (int *)invalp; 3013*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 3014*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 30150Sstevel@tonic-gate 30160Sstevel@tonic-gate switch (level) { 30170Sstevel@tonic-gate case SOL_SOCKET: 30180Sstevel@tonic-gate switch (name) { 30190Sstevel@tonic-gate case SO_SNDBUF: 3020*3448Sdh155122 if (*i1 > spds->spds_max_buf) 30210Sstevel@tonic-gate return (ENOBUFS); 30220Sstevel@tonic-gate q->q_hiwat = *i1; 30230Sstevel@tonic-gate break; 30240Sstevel@tonic-gate case SO_RCVBUF: 3025*3448Sdh155122 if (*i1 > spds->spds_max_buf) 30260Sstevel@tonic-gate return (ENOBUFS); 30270Sstevel@tonic-gate RD(q)->q_hiwat = *i1; 30280Sstevel@tonic-gate (void) mi_set_sth_hiwat(RD(q), *i1); 30290Sstevel@tonic-gate break; 30300Sstevel@tonic-gate } 30310Sstevel@tonic-gate break; 30320Sstevel@tonic-gate } 30330Sstevel@tonic-gate return (0); 30340Sstevel@tonic-gate } 30350Sstevel@tonic-gate 30360Sstevel@tonic-gate 30370Sstevel@tonic-gate /* 30380Sstevel@tonic-gate * Handle STREAMS messages. 30390Sstevel@tonic-gate */ 30400Sstevel@tonic-gate static void 30410Sstevel@tonic-gate spdsock_wput_other(queue_t *q, mblk_t *mp) 30420Sstevel@tonic-gate { 30430Sstevel@tonic-gate struct iocblk *iocp; 30440Sstevel@tonic-gate int error; 3045*3448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 3046*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 3047*3448Sdh155122 cred_t *cr; 30480Sstevel@tonic-gate 30490Sstevel@tonic-gate switch (mp->b_datap->db_type) { 30500Sstevel@tonic-gate case M_PROTO: 30510Sstevel@tonic-gate case M_PCPROTO: 30520Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) { 3053*3448Sdh155122 ss3dbg(spds, ( 30540Sstevel@tonic-gate "spdsock_wput_other: Not big enough M_PROTO\n")); 30550Sstevel@tonic-gate freemsg(mp); 30560Sstevel@tonic-gate return; 30570Sstevel@tonic-gate } 3058*3448Sdh155122 cr = zone_get_kcred(netstackid_to_zoneid( 3059*3448Sdh155122 spds->spds_netstack->netstack_stackid)); 3060*3448Sdh155122 ASSERT(cr != NULL); 3061*3448Sdh155122 30620Sstevel@tonic-gate switch (((union T_primitives *)mp->b_rptr)->type) { 30630Sstevel@tonic-gate case T_CAPABILITY_REQ: 30640Sstevel@tonic-gate spdsock_capability_req(q, mp); 3065*3448Sdh155122 break; 30660Sstevel@tonic-gate case T_INFO_REQ: 30670Sstevel@tonic-gate spdsock_info_req(q, mp); 3068*3448Sdh155122 break; 30690Sstevel@tonic-gate case T_SVR4_OPTMGMT_REQ: 3070*3448Sdh155122 (void) svr4_optcom_req(q, mp, DB_CREDDEF(mp, cr), 30710Sstevel@tonic-gate &spdsock_opt_obj); 3072*3448Sdh155122 break; 30730Sstevel@tonic-gate case T_OPTMGMT_REQ: 3074*3448Sdh155122 (void) tpi_optcom_req(q, mp, DB_CREDDEF(mp, cr), 30750Sstevel@tonic-gate &spdsock_opt_obj); 3076*3448Sdh155122 break; 30770Sstevel@tonic-gate case T_DATA_REQ: 30780Sstevel@tonic-gate case T_EXDATA_REQ: 30790Sstevel@tonic-gate case T_ORDREL_REQ: 30800Sstevel@tonic-gate /* Illegal for spdsock. */ 30810Sstevel@tonic-gate freemsg(mp); 30820Sstevel@tonic-gate (void) putnextctl1(RD(q), M_ERROR, EPROTO); 3083*3448Sdh155122 break; 30840Sstevel@tonic-gate default: 30850Sstevel@tonic-gate /* Not supported by spdsock. */ 30860Sstevel@tonic-gate spdsock_err_ack(q, mp, TNOTSUPPORT, 0); 3087*3448Sdh155122 break; 30880Sstevel@tonic-gate } 3089*3448Sdh155122 crfree(cr); 3090*3448Sdh155122 return; 30910Sstevel@tonic-gate case M_IOCTL: 30920Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 30930Sstevel@tonic-gate error = EINVAL; 30940Sstevel@tonic-gate 30950Sstevel@tonic-gate switch (iocp->ioc_cmd) { 30960Sstevel@tonic-gate case ND_SET: 30970Sstevel@tonic-gate case ND_GET: 3098*3448Sdh155122 if (nd_getset(q, spds->spds_g_nd, mp)) { 30990Sstevel@tonic-gate qreply(q, mp); 31000Sstevel@tonic-gate return; 31010Sstevel@tonic-gate } else 31020Sstevel@tonic-gate error = ENOENT; 31030Sstevel@tonic-gate /* FALLTHRU */ 31040Sstevel@tonic-gate default: 31050Sstevel@tonic-gate miocnak(q, mp, 0, error); 31060Sstevel@tonic-gate return; 31070Sstevel@tonic-gate } 31080Sstevel@tonic-gate case M_FLUSH: 31090Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) { 31100Sstevel@tonic-gate flushq(q, FLUSHALL); 31110Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW; 31120Sstevel@tonic-gate } 31130Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) { 31140Sstevel@tonic-gate qreply(q, mp); 31150Sstevel@tonic-gate return; 31160Sstevel@tonic-gate } 31170Sstevel@tonic-gate /* Else FALLTHRU */ 31180Sstevel@tonic-gate } 31190Sstevel@tonic-gate 31200Sstevel@tonic-gate /* If fell through, just black-hole the message. */ 31210Sstevel@tonic-gate freemsg(mp); 31220Sstevel@tonic-gate } 31230Sstevel@tonic-gate 31240Sstevel@tonic-gate static void 31250Sstevel@tonic-gate spdsock_wput(queue_t *q, mblk_t *mp) 31260Sstevel@tonic-gate { 31270Sstevel@tonic-gate uint8_t *rptr = mp->b_rptr; 31280Sstevel@tonic-gate mblk_t *mp1; 31290Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 3130*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 31310Sstevel@tonic-gate 31320Sstevel@tonic-gate /* 31330Sstevel@tonic-gate * If we're dumping, defer processing other messages until the 31340Sstevel@tonic-gate * dump completes. 31350Sstevel@tonic-gate */ 31360Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) { 31370Sstevel@tonic-gate if (!putq(q, mp)) 31380Sstevel@tonic-gate freemsg(mp); 31390Sstevel@tonic-gate return; 31400Sstevel@tonic-gate } 31410Sstevel@tonic-gate 31420Sstevel@tonic-gate switch (mp->b_datap->db_type) { 31430Sstevel@tonic-gate case M_DATA: 31440Sstevel@tonic-gate /* 31450Sstevel@tonic-gate * Silently discard. 31460Sstevel@tonic-gate */ 3147*3448Sdh155122 ss2dbg(spds, ("raw M_DATA in spdsock.\n")); 31480Sstevel@tonic-gate freemsg(mp); 31490Sstevel@tonic-gate return; 31500Sstevel@tonic-gate case M_PROTO: 31510Sstevel@tonic-gate case M_PCPROTO: 31520Sstevel@tonic-gate if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) { 31530Sstevel@tonic-gate if (((union T_primitives *)rptr)->type == T_DATA_REQ) { 31540Sstevel@tonic-gate if ((mp1 = mp->b_cont) == NULL) { 31550Sstevel@tonic-gate /* No data after T_DATA_REQ. */ 3156*3448Sdh155122 ss2dbg(spds, 3157*3448Sdh155122 ("No data after DATA_REQ.\n")); 31580Sstevel@tonic-gate freemsg(mp); 31590Sstevel@tonic-gate return; 31600Sstevel@tonic-gate } 31610Sstevel@tonic-gate freeb(mp); 31620Sstevel@tonic-gate mp = mp1; 3163*3448Sdh155122 ss2dbg(spds, ("T_DATA_REQ\n")); 31640Sstevel@tonic-gate break; /* Out of switch. */ 31650Sstevel@tonic-gate } 31660Sstevel@tonic-gate } 31670Sstevel@tonic-gate /* FALLTHRU */ 31680Sstevel@tonic-gate default: 3169*3448Sdh155122 ss3dbg(spds, ("In default wput case (%d %d).\n", 31700Sstevel@tonic-gate mp->b_datap->db_type, ((union T_primitives *)rptr)->type)); 31710Sstevel@tonic-gate spdsock_wput_other(q, mp); 31720Sstevel@tonic-gate return; 31730Sstevel@tonic-gate } 31740Sstevel@tonic-gate 31750Sstevel@tonic-gate /* I now have a PF_POLICY message in an M_DATA block. */ 31760Sstevel@tonic-gate spdsock_parse(q, mp); 31770Sstevel@tonic-gate } 31780Sstevel@tonic-gate 31790Sstevel@tonic-gate /* 31800Sstevel@tonic-gate * Device open procedure, called when new queue pair created. 31810Sstevel@tonic-gate * We are passed the read-side queue. 31820Sstevel@tonic-gate */ 31830Sstevel@tonic-gate /* ARGSUSED */ 31840Sstevel@tonic-gate static int 31850Sstevel@tonic-gate spdsock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 31860Sstevel@tonic-gate { 31870Sstevel@tonic-gate spdsock_t *ss; 31880Sstevel@tonic-gate queue_t *oq = OTHERQ(q); 31890Sstevel@tonic-gate minor_t ssminor; 3190*3448Sdh155122 netstack_t *ns; 3191*3448Sdh155122 spd_stack_t *spds; 3192*3448Sdh155122 3193*3448Sdh155122 if (secpolicy_ip_config(credp, B_FALSE) != 0) 31940Sstevel@tonic-gate return (EPERM); 31950Sstevel@tonic-gate 31960Sstevel@tonic-gate if (q->q_ptr != NULL) 31970Sstevel@tonic-gate return (0); /* Re-open of an already open instance. */ 31980Sstevel@tonic-gate 31990Sstevel@tonic-gate if (sflag & MODOPEN) 32000Sstevel@tonic-gate return (EINVAL); 32010Sstevel@tonic-gate 3202*3448Sdh155122 ns = netstack_find_by_cred(credp); 3203*3448Sdh155122 ASSERT(ns != NULL); 3204*3448Sdh155122 spds = ns->netstack_spdsock; 3205*3448Sdh155122 ASSERT(spds != NULL); 3206*3448Sdh155122 3207*3448Sdh155122 ss2dbg(spds, ("Made it into PF_POLICY socket open.\n")); 32080Sstevel@tonic-gate 32090Sstevel@tonic-gate ssminor = (minor_t)(uintptr_t)vmem_alloc(spdsock_vmem, 1, VM_NOSLEEP); 3210*3448Sdh155122 if (ssminor == 0) { 3211*3448Sdh155122 netstack_rele(spds->spds_netstack); 32120Sstevel@tonic-gate return (ENOMEM); 3213*3448Sdh155122 } 32140Sstevel@tonic-gate ss = kmem_zalloc(sizeof (spdsock_t), KM_NOSLEEP); 32150Sstevel@tonic-gate if (ss == NULL) { 32160Sstevel@tonic-gate vmem_free(spdsock_vmem, (void *)(uintptr_t)ssminor, 1); 3217*3448Sdh155122 netstack_rele(spds->spds_netstack); 32180Sstevel@tonic-gate return (ENOMEM); 32190Sstevel@tonic-gate } 32200Sstevel@tonic-gate 32210Sstevel@tonic-gate ss->spdsock_minor = ssminor; 32220Sstevel@tonic-gate ss->spdsock_state = TS_UNBND; 32230Sstevel@tonic-gate ss->spdsock_dump_req = NULL; 32240Sstevel@tonic-gate 3225*3448Sdh155122 ss->spdsock_spds = spds; 3226*3448Sdh155122 32270Sstevel@tonic-gate q->q_ptr = ss; 32280Sstevel@tonic-gate oq->q_ptr = ss; 32290Sstevel@tonic-gate 3230*3448Sdh155122 q->q_hiwat = spds->spds_recv_hiwat; 3231*3448Sdh155122 3232*3448Sdh155122 oq->q_hiwat = spds->spds_xmit_hiwat; 3233*3448Sdh155122 oq->q_lowat = spds->spds_xmit_lowat; 32340Sstevel@tonic-gate 32350Sstevel@tonic-gate qprocson(q); 3236*3448Sdh155122 (void) mi_set_sth_hiwat(q, spds->spds_recv_hiwat); 32370Sstevel@tonic-gate 32380Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), ss->spdsock_minor); 32390Sstevel@tonic-gate return (0); 32400Sstevel@tonic-gate } 32410Sstevel@tonic-gate 32420Sstevel@tonic-gate /* 32430Sstevel@tonic-gate * Read-side service procedure, invoked when we get back-enabled 32440Sstevel@tonic-gate * when buffer space becomes available. 32450Sstevel@tonic-gate * 32460Sstevel@tonic-gate * Dump another chunk if we were dumping before; when we finish, kick 32470Sstevel@tonic-gate * the write-side queue in case it's waiting for read queue space. 32480Sstevel@tonic-gate */ 32490Sstevel@tonic-gate void 32500Sstevel@tonic-gate spdsock_rsrv(queue_t *q) 32510Sstevel@tonic-gate { 32520Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 32530Sstevel@tonic-gate 32540Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) 32550Sstevel@tonic-gate spdsock_dump_some(q, ss); 32560Sstevel@tonic-gate 32570Sstevel@tonic-gate if (ss->spdsock_dump_req == NULL) 32580Sstevel@tonic-gate qenable(OTHERQ(q)); 32590Sstevel@tonic-gate } 32600Sstevel@tonic-gate 32610Sstevel@tonic-gate /* 32620Sstevel@tonic-gate * Write-side service procedure, invoked when we defer processing 32630Sstevel@tonic-gate * if another message is received while a dump is in progress. 32640Sstevel@tonic-gate */ 32650Sstevel@tonic-gate void 32660Sstevel@tonic-gate spdsock_wsrv(queue_t *q) 32670Sstevel@tonic-gate { 32680Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 32690Sstevel@tonic-gate mblk_t *mp; 3270*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 3271*3448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 32720Sstevel@tonic-gate 32730Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) { 32740Sstevel@tonic-gate qenable(OTHERQ(q)); 32750Sstevel@tonic-gate return; 32760Sstevel@tonic-gate } 32770Sstevel@tonic-gate 32780Sstevel@tonic-gate while ((mp = getq(q)) != NULL) { 3279*3448Sdh155122 if (ipsec_loaded(ipss)) { 32800Sstevel@tonic-gate spdsock_wput(q, mp); 32810Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) 32820Sstevel@tonic-gate return; 3283*3448Sdh155122 } else if (!ipsec_failed(ipss)) { 32840Sstevel@tonic-gate (void) putq(q, mp); 32850Sstevel@tonic-gate } else { 32860Sstevel@tonic-gate spdsock_error(q, mp, EPFNOSUPPORT, 0); 32870Sstevel@tonic-gate } 32880Sstevel@tonic-gate } 32890Sstevel@tonic-gate } 32900Sstevel@tonic-gate 32910Sstevel@tonic-gate static int 32920Sstevel@tonic-gate spdsock_close(queue_t *q) 32930Sstevel@tonic-gate { 32940Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 3295*3448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 32960Sstevel@tonic-gate 32970Sstevel@tonic-gate qprocsoff(q); 32980Sstevel@tonic-gate 32990Sstevel@tonic-gate /* Safe assumption. */ 33000Sstevel@tonic-gate ASSERT(ss != NULL); 33010Sstevel@tonic-gate 33020Sstevel@tonic-gate if (ss->spdsock_timeout != 0) 33030Sstevel@tonic-gate (void) quntimeout(q, ss->spdsock_timeout); 33040Sstevel@tonic-gate 3305*3448Sdh155122 ss3dbg(spds, ("Driver close, PF_POLICY socket is going away.\n")); 33060Sstevel@tonic-gate 33070Sstevel@tonic-gate vmem_free(spdsock_vmem, (void *)(uintptr_t)ss->spdsock_minor, 1); 3308*3448Sdh155122 netstack_rele(ss->spdsock_spds->spds_netstack); 33090Sstevel@tonic-gate 33100Sstevel@tonic-gate kmem_free(ss, sizeof (spdsock_t)); 33110Sstevel@tonic-gate return (0); 33120Sstevel@tonic-gate } 33130Sstevel@tonic-gate 33140Sstevel@tonic-gate /* 33150Sstevel@tonic-gate * Merge the IPsec algorithms tables with the received algorithm information. 33160Sstevel@tonic-gate */ 33170Sstevel@tonic-gate void 3318*3448Sdh155122 spdsock_merge_algs(spd_stack_t *spds) 33190Sstevel@tonic-gate { 33200Sstevel@tonic-gate ipsec_alginfo_t *alg, *oalg; 33210Sstevel@tonic-gate ipsec_algtype_t algtype; 33220Sstevel@tonic-gate uint_t algidx, algid, nalgs; 33230Sstevel@tonic-gate crypto_mech_name_t *mechs; 33240Sstevel@tonic-gate uint_t mech_count, mech_idx; 3325*3448Sdh155122 netstack_t *ns = spds->spds_netstack; 3326*3448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 3327*3448Sdh155122 3328*3448Sdh155122 ASSERT(MUTEX_HELD(&spds->spds_alg_lock)); 33290Sstevel@tonic-gate 33300Sstevel@tonic-gate /* 33310Sstevel@tonic-gate * Get the list of supported mechanisms from the crypto framework. 33320Sstevel@tonic-gate * If a mechanism is supported by KCF, resolve its mechanism 33330Sstevel@tonic-gate * id and mark it as being valid. This operation must be done 33340Sstevel@tonic-gate * without holding alg_lock, since it can cause a provider 33350Sstevel@tonic-gate * module to be loaded and the provider notification callback to 33360Sstevel@tonic-gate * be invoked. 33370Sstevel@tonic-gate */ 33380Sstevel@tonic-gate mechs = crypto_get_mech_list(&mech_count, KM_SLEEP); 33390Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 33400Sstevel@tonic-gate for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) { 33410Sstevel@tonic-gate int algflags = 0; 33420Sstevel@tonic-gate crypto_mech_type_t mt = CRYPTO_MECHANISM_INVALID; 33430Sstevel@tonic-gate 3344*3448Sdh155122 alg = spds->spds_algs[algtype][algid]; 3345*3448Sdh155122 if (alg == NULL) 33460Sstevel@tonic-gate continue; 33470Sstevel@tonic-gate 33480Sstevel@tonic-gate /* 33490Sstevel@tonic-gate * The NULL encryption algorithm is a special 33500Sstevel@tonic-gate * case because there are no mechanisms, yet 33510Sstevel@tonic-gate * the algorithm is still valid. 33520Sstevel@tonic-gate */ 33530Sstevel@tonic-gate if (alg->alg_id == SADB_EALG_NULL) { 33540Sstevel@tonic-gate alg->alg_mech_type = CRYPTO_MECHANISM_INVALID; 33550Sstevel@tonic-gate alg->alg_flags = ALG_FLAG_VALID; 33560Sstevel@tonic-gate continue; 33570Sstevel@tonic-gate } 33580Sstevel@tonic-gate 33590Sstevel@tonic-gate for (mech_idx = 0; mech_idx < mech_count; mech_idx++) { 33600Sstevel@tonic-gate if (strncmp(alg->alg_mech_name, mechs[mech_idx], 33610Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME) == 0) { 33620Sstevel@tonic-gate mt = crypto_mech2id(alg->alg_mech_name); 33630Sstevel@tonic-gate ASSERT(mt != CRYPTO_MECHANISM_INVALID); 33640Sstevel@tonic-gate algflags = ALG_FLAG_VALID; 33650Sstevel@tonic-gate break; 33660Sstevel@tonic-gate } 33670Sstevel@tonic-gate } 33680Sstevel@tonic-gate alg->alg_mech_type = mt; 33690Sstevel@tonic-gate alg->alg_flags = algflags; 33700Sstevel@tonic-gate } 33710Sstevel@tonic-gate } 33720Sstevel@tonic-gate 3373*3448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 33740Sstevel@tonic-gate 33750Sstevel@tonic-gate /* 33760Sstevel@tonic-gate * For each algorithm currently defined, check if it is 33770Sstevel@tonic-gate * present in the new tables created from the SPD_UPDATEALGS 33780Sstevel@tonic-gate * message received from user-space. 33790Sstevel@tonic-gate * Delete the algorithm entries that are currently defined 33800Sstevel@tonic-gate * but not part of the new tables. 33810Sstevel@tonic-gate */ 33820Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 3383*3448Sdh155122 nalgs = ipss->ipsec_nalgs[algtype]; 33840Sstevel@tonic-gate for (algidx = 0; algidx < nalgs; algidx++) { 3385*3448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 3386*3448Sdh155122 if (spds->spds_algs[algtype][algid] == NULL) 3387*3448Sdh155122 ipsec_alg_unreg(algtype, algid, ns); 33880Sstevel@tonic-gate } 33890Sstevel@tonic-gate } 33900Sstevel@tonic-gate 33910Sstevel@tonic-gate /* 33920Sstevel@tonic-gate * For each algorithm we just received, check if it is 33930Sstevel@tonic-gate * present in the currently defined tables. If it is, swap 33940Sstevel@tonic-gate * the entry with the one we just allocated. 33950Sstevel@tonic-gate * If the new algorithm is not in the current tables, 33960Sstevel@tonic-gate * add it. 33970Sstevel@tonic-gate */ 33980Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 33990Sstevel@tonic-gate for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) { 3400*3448Sdh155122 alg = spds->spds_algs[algtype][algid]; 3401*3448Sdh155122 if (alg == NULL) 34020Sstevel@tonic-gate continue; 34030Sstevel@tonic-gate 3404*3448Sdh155122 if ((oalg = ipss->ipsec_alglists[algtype][algid]) == 3405*3448Sdh155122 NULL) { 34060Sstevel@tonic-gate /* 34070Sstevel@tonic-gate * New algorithm, add it to the algorithm 34080Sstevel@tonic-gate * table. 34090Sstevel@tonic-gate */ 3410*3448Sdh155122 ipsec_alg_reg(algtype, alg, ns); 34110Sstevel@tonic-gate } else { 34120Sstevel@tonic-gate /* 34130Sstevel@tonic-gate * Algorithm is already in the table. Swap 34140Sstevel@tonic-gate * the existing entry with the new one. 34150Sstevel@tonic-gate */ 3416*3448Sdh155122 ipsec_alg_fix_min_max(alg, algtype, ns); 3417*3448Sdh155122 ipss->ipsec_alglists[algtype][algid] = alg; 34180Sstevel@tonic-gate ipsec_alg_free(oalg); 34190Sstevel@tonic-gate } 3420*3448Sdh155122 spds->spds_algs[algtype][algid] = NULL; 34210Sstevel@tonic-gate } 34220Sstevel@tonic-gate } 34230Sstevel@tonic-gate 3424*3448Sdh155122 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 3425*3448Sdh155122 ipss->ipsec_algs_exec_mode[algtype] = 3426*3448Sdh155122 spds->spds_algs_exec_mode[algtype]; 3427*3448Sdh155122 } 3428*3448Sdh155122 3429*3448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 34300Sstevel@tonic-gate 34310Sstevel@tonic-gate crypto_free_mech_list(mechs, mech_count); 34320Sstevel@tonic-gate 3433*3448Sdh155122 ipsecah_algs_changed(ns); 3434*3448Sdh155122 ipsecesp_algs_changed(ns); 34350Sstevel@tonic-gate } 3436