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 /* 223448Sdh155122 * 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> 343448Sdh155122 #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 69*4307Spwernau #include <c2/audit.h> 70*4307Spwernau 710Sstevel@tonic-gate /* 720Sstevel@tonic-gate * This is a transport provider for the PF_POLICY IPsec policy 730Sstevel@tonic-gate * management socket, which provides a management interface into the 740Sstevel@tonic-gate * SPD, allowing policy rules to be added, deleted, and queried. 750Sstevel@tonic-gate * 760Sstevel@tonic-gate * This effectively replaces the old private SIOC*IPSECONFIG ioctls 770Sstevel@tonic-gate * with an extensible interface which will hopefully be public some 780Sstevel@tonic-gate * day. 790Sstevel@tonic-gate * 800Sstevel@tonic-gate * See <net/pfpolicy.h> for more details on the protocol. 810Sstevel@tonic-gate * 820Sstevel@tonic-gate * We link against drv/ip and call directly into it to manipulate the 830Sstevel@tonic-gate * SPD; see ipsec_impl.h for the policy data structures and spd.c for 840Sstevel@tonic-gate * the code which maintains them. 850Sstevel@tonic-gate * 860Sstevel@tonic-gate * The MT model of this is QPAIR with the addition of some explicit 870Sstevel@tonic-gate * locking to protect system-wide policy data structures. 880Sstevel@tonic-gate */ 890Sstevel@tonic-gate 900Sstevel@tonic-gate static vmem_t *spdsock_vmem; /* for minor numbers. */ 910Sstevel@tonic-gate 920Sstevel@tonic-gate #define ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t)) 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages (from rts.c...) */ 950Sstevel@tonic-gate static struct T_info_ack spdsock_g_t_info_ack = { 960Sstevel@tonic-gate T_INFO_ACK, 970Sstevel@tonic-gate T_INFINITE, /* TSDU_size. Maximum size messages. */ 980Sstevel@tonic-gate T_INVALID, /* ETSDU_size. No expedited data. */ 990Sstevel@tonic-gate T_INVALID, /* CDATA_size. No connect data. */ 1000Sstevel@tonic-gate T_INVALID, /* DDATA_size. No disconnect data. */ 1010Sstevel@tonic-gate 0, /* ADDR_size. */ 1020Sstevel@tonic-gate 0, /* OPT_size. No user-settable options */ 1030Sstevel@tonic-gate 64 * 1024, /* TIDU_size. spdsock allows maximum size messages. */ 1040Sstevel@tonic-gate T_COTS, /* SERV_type. spdsock supports connection oriented. */ 1050Sstevel@tonic-gate TS_UNBND, /* CURRENT_state. This is set from spdsock_state. */ 1060Sstevel@tonic-gate (XPG4_1) /* Provider flags */ 1070Sstevel@tonic-gate }; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */ 1103448Sdh155122 typedef struct spdsockparam_s { 1110Sstevel@tonic-gate uint_t spdsock_param_min; 1120Sstevel@tonic-gate uint_t spdsock_param_max; 1130Sstevel@tonic-gate uint_t spdsock_param_value; 1140Sstevel@tonic-gate char *spdsock_param_name; 1150Sstevel@tonic-gate } spdsockparam_t; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * Table of NDD variables supported by spdsock. These are loaded into 1190Sstevel@tonic-gate * spdsock_g_nd in spdsock_init_nd. 1200Sstevel@tonic-gate * All of these are alterable, within the min/max values given, at run time. 1210Sstevel@tonic-gate */ 1223448Sdh155122 static spdsockparam_t lcl_param_arr[] = { 1230Sstevel@tonic-gate /* min max value name */ 1240Sstevel@tonic-gate { 4096, 65536, 8192, "spdsock_xmit_hiwat"}, 1250Sstevel@tonic-gate { 0, 65536, 1024, "spdsock_xmit_lowat"}, 1260Sstevel@tonic-gate { 4096, 65536, 8192, "spdsock_recv_hiwat"}, 1270Sstevel@tonic-gate { 65536, 1024*1024*1024, 256*1024, "spdsock_max_buf"}, 1280Sstevel@tonic-gate { 0, 3, 0, "spdsock_debug"}, 1290Sstevel@tonic-gate }; 1303448Sdh155122 #define spds_xmit_hiwat spds_params[0].spdsock_param_value 1313448Sdh155122 #define spds_xmit_lowat spds_params[1].spdsock_param_value 1323448Sdh155122 #define spds_recv_hiwat spds_params[2].spdsock_param_value 1333448Sdh155122 #define spds_max_buf spds_params[3].spdsock_param_value 1343448Sdh155122 #define spds_debug spds_params[4].spdsock_param_value 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate #define ss0dbg(a) printf a 1370Sstevel@tonic-gate /* NOTE: != 0 instead of > 0 so lint doesn't complain. */ 1383448Sdh155122 #define ss1dbg(spds, a) if (spds->spds_debug != 0) printf a 1393448Sdh155122 #define ss2dbg(spds, a) if (spds->spds_debug > 1) printf a 1403448Sdh155122 #define ss3dbg(spds, a) if (spds->spds_debug > 2) printf a 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate static int spdsock_close(queue_t *); 1430Sstevel@tonic-gate static int spdsock_open(queue_t *, dev_t *, int, int, cred_t *); 1440Sstevel@tonic-gate static void spdsock_wput(queue_t *, mblk_t *); 1450Sstevel@tonic-gate static void spdsock_wsrv(queue_t *); 1460Sstevel@tonic-gate static void spdsock_rsrv(queue_t *); 1473448Sdh155122 static void *spdsock_stack_init(netstackid_t stackid, netstack_t *ns); 1483448Sdh155122 static void spdsock_stack_fini(netstackid_t stackid, void *arg); 1490Sstevel@tonic-gate static void spdsock_loadcheck(void *); 1503448Sdh155122 static void spdsock_merge_algs(spd_stack_t *); 1513448Sdh155122 static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *); 1523055Sdanmcd static mblk_t *spdsock_dump_next_record(spdsock_t *); 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate static struct module_info info = { 1550Sstevel@tonic-gate 5138, "spdsock", 1, INFPSZ, 512, 128 1560Sstevel@tonic-gate }; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate static struct qinit rinit = { 1590Sstevel@tonic-gate NULL, (pfi_t)spdsock_rsrv, spdsock_open, spdsock_close, 1600Sstevel@tonic-gate NULL, &info 1610Sstevel@tonic-gate }; 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate static struct qinit winit = { 1640Sstevel@tonic-gate (pfi_t)spdsock_wput, (pfi_t)spdsock_wsrv, NULL, NULL, NULL, &info 1650Sstevel@tonic-gate }; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate struct streamtab spdsockinfo = { 1680Sstevel@tonic-gate &rinit, &winit 1690Sstevel@tonic-gate }; 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate /* mapping from alg type to protocol number, as per RFC 2407 */ 1720Sstevel@tonic-gate static const uint_t algproto[] = { 1730Sstevel@tonic-gate PROTO_IPSEC_AH, 1740Sstevel@tonic-gate PROTO_IPSEC_ESP, 1750Sstevel@tonic-gate }; 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate #define NALGPROTOS (sizeof (algproto) / sizeof (algproto[0])) 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate /* mapping from kernel exec mode to spdsock exec mode */ 1800Sstevel@tonic-gate static const uint_t execmodes[] = { 1810Sstevel@tonic-gate SPD_ALG_EXEC_MODE_SYNC, 1820Sstevel@tonic-gate SPD_ALG_EXEC_MODE_ASYNC 1830Sstevel@tonic-gate }; 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate #define NEXECMODES (sizeof (execmodes) / sizeof (execmodes[0])) 1860Sstevel@tonic-gate 1873055Sdanmcd #define ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1) 1883055Sdanmcd #define ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2) 1893055Sdanmcd 190*4307Spwernau #define ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL) 191*4307Spwernau 1920Sstevel@tonic-gate /* ARGSUSED */ 1930Sstevel@tonic-gate static int 1940Sstevel@tonic-gate spdsock_param_get(q, mp, cp, cr) 1950Sstevel@tonic-gate queue_t *q; 1960Sstevel@tonic-gate mblk_t *mp; 1970Sstevel@tonic-gate caddr_t cp; 1980Sstevel@tonic-gate cred_t *cr; 1990Sstevel@tonic-gate { 2000Sstevel@tonic-gate spdsockparam_t *spdsockpa = (spdsockparam_t *)cp; 2010Sstevel@tonic-gate uint_t value; 2023448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2033448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2043448Sdh155122 2053448Sdh155122 mutex_enter(&spds->spds_param_lock); 2060Sstevel@tonic-gate value = spdsockpa->spdsock_param_value; 2073448Sdh155122 mutex_exit(&spds->spds_param_lock); 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate (void) mi_mpprintf(mp, "%u", value); 2100Sstevel@tonic-gate return (0); 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate /* This routine sets an NDD variable in a spdsockparam_t structure. */ 2140Sstevel@tonic-gate /* ARGSUSED */ 2150Sstevel@tonic-gate static int 2160Sstevel@tonic-gate spdsock_param_set(q, mp, value, cp, cr) 2170Sstevel@tonic-gate queue_t *q; 2180Sstevel@tonic-gate mblk_t *mp; 2190Sstevel@tonic-gate char *value; 2200Sstevel@tonic-gate caddr_t cp; 2210Sstevel@tonic-gate cred_t *cr; 2220Sstevel@tonic-gate { 2230Sstevel@tonic-gate ulong_t new_value; 2240Sstevel@tonic-gate spdsockparam_t *spdsockpa = (spdsockparam_t *)cp; 2253448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2263448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* Convert the value from a string into a long integer. */ 2290Sstevel@tonic-gate if (ddi_strtoul(value, NULL, 10, &new_value) != 0) 2300Sstevel@tonic-gate return (EINVAL); 2310Sstevel@tonic-gate 2323448Sdh155122 mutex_enter(&spds->spds_param_lock); 2330Sstevel@tonic-gate /* 2340Sstevel@tonic-gate * Fail the request if the new value does not lie within the 2350Sstevel@tonic-gate * required bounds. 2360Sstevel@tonic-gate */ 2370Sstevel@tonic-gate if (new_value < spdsockpa->spdsock_param_min || 2380Sstevel@tonic-gate new_value > spdsockpa->spdsock_param_max) { 2393448Sdh155122 mutex_exit(&spds->spds_param_lock); 2400Sstevel@tonic-gate return (EINVAL); 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate /* Set the new value */ 2440Sstevel@tonic-gate spdsockpa->spdsock_param_value = new_value; 2453448Sdh155122 mutex_exit(&spds->spds_param_lock); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate return (0); 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2503448Sdh155122 /* 2513448Sdh155122 * Initialize at module load time 2523448Sdh155122 */ 2530Sstevel@tonic-gate boolean_t 2540Sstevel@tonic-gate spdsock_ddi_init(void) 2550Sstevel@tonic-gate { 2560Sstevel@tonic-gate spdsock_max_optsize = optcom_max_optsize( 2570Sstevel@tonic-gate spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt); 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1, 2600Sstevel@tonic-gate NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER); 2610Sstevel@tonic-gate 2623448Sdh155122 /* 2633448Sdh155122 * We want to be informed each time a stack is created or 2643448Sdh155122 * destroyed in the kernel, so we can maintain the 2653448Sdh155122 * set of spd_stack_t's. 2663448Sdh155122 */ 2673448Sdh155122 netstack_register(NS_SPDSOCK, spdsock_stack_init, NULL, 2683448Sdh155122 spdsock_stack_fini); 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate return (B_TRUE); 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate 2733448Sdh155122 /* 2743448Sdh155122 * Walk through the param array specified registering each element with the 2753448Sdh155122 * named dispatch handler. 2763448Sdh155122 */ 2773448Sdh155122 static boolean_t 2783448Sdh155122 spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt) 2793448Sdh155122 { 2803448Sdh155122 for (; cnt-- > 0; ssp++) { 2813448Sdh155122 if (ssp->spdsock_param_name != NULL && 2823448Sdh155122 ssp->spdsock_param_name[0]) { 2833448Sdh155122 if (!nd_load(ndp, 2843448Sdh155122 ssp->spdsock_param_name, 2853448Sdh155122 spdsock_param_get, spdsock_param_set, 2863448Sdh155122 (caddr_t)ssp)) { 2873448Sdh155122 nd_free(ndp); 2883448Sdh155122 return (B_FALSE); 2893448Sdh155122 } 2903448Sdh155122 } 2913448Sdh155122 } 2923448Sdh155122 return (B_TRUE); 2933448Sdh155122 } 2943448Sdh155122 2953448Sdh155122 /* 2963448Sdh155122 * Initialize for each stack instance 2973448Sdh155122 */ 2983448Sdh155122 /* ARGSUSED */ 2993448Sdh155122 static void * 3003448Sdh155122 spdsock_stack_init(netstackid_t stackid, netstack_t *ns) 3013448Sdh155122 { 3023448Sdh155122 spd_stack_t *spds; 3033448Sdh155122 spdsockparam_t *ssp; 3043448Sdh155122 3053448Sdh155122 spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP); 3063448Sdh155122 spds->spds_netstack = ns; 3073448Sdh155122 3083448Sdh155122 ASSERT(spds->spds_g_nd == NULL); 3093448Sdh155122 3103448Sdh155122 ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 3113448Sdh155122 spds->spds_params = ssp; 3123448Sdh155122 bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr)); 3133448Sdh155122 3143448Sdh155122 (void) spdsock_param_register(&spds->spds_g_nd, ssp, 3153448Sdh155122 A_CNT(lcl_param_arr)); 3163448Sdh155122 3173448Sdh155122 mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL); 3183448Sdh155122 mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL); 3193448Sdh155122 3203448Sdh155122 return (spds); 3213448Sdh155122 } 3223448Sdh155122 3230Sstevel@tonic-gate void 3240Sstevel@tonic-gate spdsock_ddi_destroy(void) 3250Sstevel@tonic-gate { 3260Sstevel@tonic-gate vmem_destroy(spdsock_vmem); 3273448Sdh155122 3283448Sdh155122 netstack_unregister(NS_SPDSOCK); 3293448Sdh155122 } 3303448Sdh155122 3313448Sdh155122 /* ARGSUSED */ 3323448Sdh155122 static void 3333448Sdh155122 spdsock_stack_fini(netstackid_t stackid, void *arg) 3343448Sdh155122 { 3353448Sdh155122 spd_stack_t *spds = (spd_stack_t *)arg; 3363448Sdh155122 3373448Sdh155122 mutex_destroy(&spds->spds_param_lock); 3383448Sdh155122 mutex_destroy(&spds->spds_alg_lock); 3393448Sdh155122 nd_free(&spds->spds_g_nd); 3403448Sdh155122 kmem_free(spds->spds_params, sizeof (lcl_param_arr)); 3413448Sdh155122 spds->spds_params = NULL; 3423448Sdh155122 3433448Sdh155122 kmem_free(spds, sizeof (*spds)); 3440Sstevel@tonic-gate } 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate /* 3470Sstevel@tonic-gate * NOTE: large quantities of this should be shared with keysock. 3480Sstevel@tonic-gate * Would be nice to combine some of this into a common module, but 3490Sstevel@tonic-gate * not possible given time pressures. 3500Sstevel@tonic-gate */ 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate /* 3530Sstevel@tonic-gate * High-level reality checking of extensions. 3540Sstevel@tonic-gate */ 3550Sstevel@tonic-gate /* ARGSUSED */ /* XXX */ 3560Sstevel@tonic-gate static boolean_t 3570Sstevel@tonic-gate ext_check(spd_ext_t *ext) 3580Sstevel@tonic-gate { 3593055Sdanmcd spd_if_t *tunname = (spd_if_t *)ext; 3603055Sdanmcd int i; 3613055Sdanmcd char *idstr; 3623055Sdanmcd 3633055Sdanmcd if (ext->spd_ext_type == SPD_EXT_TUN_NAME) { 3643055Sdanmcd /* (NOTE: Modified from SADB_EXT_IDENTITY..) */ 3653055Sdanmcd 3663055Sdanmcd /* 3673055Sdanmcd * Make sure the strings in these identities are 3683055Sdanmcd * null-terminated. Let's "proactively" null-terminate the 3693055Sdanmcd * string at the last byte if it's not terminated sooner. 3703055Sdanmcd */ 3713055Sdanmcd i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t); 3723055Sdanmcd idstr = (char *)(tunname + 1); 3733055Sdanmcd while (*idstr != '\0' && i > 0) { 3743055Sdanmcd i--; 3753055Sdanmcd idstr++; 3763055Sdanmcd } 3773055Sdanmcd if (i == 0) { 3783055Sdanmcd /* 3793055Sdanmcd * I.e., if the bozo user didn't NULL-terminate the 3803055Sdanmcd * string... 3813055Sdanmcd */ 3823055Sdanmcd idstr--; 3833055Sdanmcd *idstr = '\0'; 3843055Sdanmcd } 3853055Sdanmcd } 3860Sstevel@tonic-gate return (B_TRUE); /* For now... */ 3870Sstevel@tonic-gate } 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate /* Return values for spdsock_get_ext(). */ 3920Sstevel@tonic-gate #define KGE_OK 0 3930Sstevel@tonic-gate #define KGE_DUP 1 3940Sstevel@tonic-gate #define KGE_UNK 2 3950Sstevel@tonic-gate #define KGE_LEN 3 3960Sstevel@tonic-gate #define KGE_CHK 4 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate /* 3990Sstevel@tonic-gate * Parse basic extension headers and return in the passed-in pointer vector. 4000Sstevel@tonic-gate * Return values include: 4010Sstevel@tonic-gate * 4020Sstevel@tonic-gate * KGE_OK Everything's nice and parsed out. 4030Sstevel@tonic-gate * If there are no extensions, place NULL in extv[0]. 4040Sstevel@tonic-gate * KGE_DUP There is a duplicate extension. 4050Sstevel@tonic-gate * First instance in appropriate bin. First duplicate in 4060Sstevel@tonic-gate * extv[0]. 4070Sstevel@tonic-gate * KGE_UNK Unknown extension type encountered. extv[0] contains 4080Sstevel@tonic-gate * unknown header. 4090Sstevel@tonic-gate * KGE_LEN Extension length error. 4100Sstevel@tonic-gate * KGE_CHK High-level reality check failed on specific extension. 4110Sstevel@tonic-gate * 4120Sstevel@tonic-gate * My apologies for some of the pointer arithmetic in here. I'm thinking 4130Sstevel@tonic-gate * like an assembly programmer, yet trying to make the compiler happy. 4140Sstevel@tonic-gate */ 4150Sstevel@tonic-gate static int 4160Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize) 4170Sstevel@tonic-gate { 4180Sstevel@tonic-gate bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1)); 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate /* Use extv[0] as the "current working pointer". */ 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate extv[0] = (spd_ext_t *)(basehdr + 1); 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) { 4250Sstevel@tonic-gate /* Check for unknown headers. */ 4260Sstevel@tonic-gate if (extv[0]->spd_ext_type == 0 || 4270Sstevel@tonic-gate extv[0]->spd_ext_type > SPD_EXT_MAX) 4280Sstevel@tonic-gate return (KGE_UNK); 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate /* 4310Sstevel@tonic-gate * Check length. Use uint64_t because extlen is in units 4320Sstevel@tonic-gate * of 64-bit words. If length goes beyond the msgsize, 4330Sstevel@tonic-gate * return an error. (Zero length also qualifies here.) 4340Sstevel@tonic-gate */ 4350Sstevel@tonic-gate if (extv[0]->spd_ext_len == 0 || 4360Sstevel@tonic-gate (void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) > 4370Sstevel@tonic-gate (void *)((uint8_t *)basehdr + msgsize)) 4380Sstevel@tonic-gate return (KGE_LEN); 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate /* Check for redundant headers. */ 4410Sstevel@tonic-gate if (extv[extv[0]->spd_ext_type] != NULL) 4420Sstevel@tonic-gate return (KGE_DUP); 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate /* 4450Sstevel@tonic-gate * Reality check the extension if possible at the spdsock 4460Sstevel@tonic-gate * level. 4470Sstevel@tonic-gate */ 4480Sstevel@tonic-gate if (!ext_check(extv[0])) 4490Sstevel@tonic-gate return (KGE_CHK); 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /* If I make it here, assign the appropriate bin. */ 4520Sstevel@tonic-gate extv[extv[0]->spd_ext_type] = extv[0]; 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate /* Advance pointer (See above for uint64_t ptr reasoning.) */ 4550Sstevel@tonic-gate extv[0] = (spd_ext_t *) 4560Sstevel@tonic-gate ((uint64_t *)extv[0] + extv[0]->spd_ext_len); 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate /* Everything's cool. */ 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate /* 4620Sstevel@tonic-gate * If extv[0] == NULL, then there are no extension headers in this 4630Sstevel@tonic-gate * message. Ensure that this is the case. 4640Sstevel@tonic-gate */ 4650Sstevel@tonic-gate if (extv[0] == (spd_ext_t *)(basehdr + 1)) 4660Sstevel@tonic-gate extv[0] = NULL; 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate return (KGE_OK); 4690Sstevel@tonic-gate } 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate static const int bad_ext_diag[] = { 4720Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_LCLPORT, 4730Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_REMPORT, 4740Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_PROTO, 4750Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_LCLADDR, 4760Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_REMADDR, 4770Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_ACTION, 4780Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_RULE, 4790Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_RULESET, 4800Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE 4810Sstevel@tonic-gate }; 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate static const int dup_ext_diag[] = { 4840Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_LCLPORT, 4850Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_REMPORT, 4860Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_PROTO, 4870Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_LCLADDR, 4880Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_REMADDR, 4890Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_ACTION, 4900Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_RULE, 4910Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_RULESET, 4920Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE 4930Sstevel@tonic-gate }; 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate /* 4960Sstevel@tonic-gate * Transmit a PF_POLICY error message to the instance either pointed to 4970Sstevel@tonic-gate * by ks, the instance with serial number serial, or more, depending. 4980Sstevel@tonic-gate * 4990Sstevel@tonic-gate * The faulty message (or a reasonable facsimile thereof) is in mp. 5000Sstevel@tonic-gate * This function will free mp or recycle it for delivery, thereby causing 5010Sstevel@tonic-gate * the stream head to free it. 5020Sstevel@tonic-gate */ 5030Sstevel@tonic-gate static void 5040Sstevel@tonic-gate spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic) 5050Sstevel@tonic-gate { 5060Sstevel@tonic-gate spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate ASSERT(mp->b_datap->db_type == M_DATA); 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate if (spmsg->spd_msg_type < SPD_MIN || 5110Sstevel@tonic-gate spmsg->spd_msg_type > SPD_MAX) 5120Sstevel@tonic-gate spmsg->spd_msg_type = SPD_RESERVED; 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate /* 5150Sstevel@tonic-gate * Strip out extension headers. 5160Sstevel@tonic-gate */ 5170Sstevel@tonic-gate ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim); 5180Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (*spmsg); 5190Sstevel@tonic-gate spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t)); 5200Sstevel@tonic-gate spmsg->spd_msg_errno = (uint8_t)error; 5210Sstevel@tonic-gate spmsg->spd_msg_diagnostic = (uint16_t)diagnostic; 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate qreply(q, mp); 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate static void 5270Sstevel@tonic-gate spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic) 5280Sstevel@tonic-gate { 5290Sstevel@tonic-gate spdsock_error(q, mp, EINVAL, diagnostic); 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate static void 5330Sstevel@tonic-gate spd_echo(queue_t *q, mblk_t *mp) 5340Sstevel@tonic-gate { 5350Sstevel@tonic-gate qreply(q, mp); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5383055Sdanmcd /* 5393055Sdanmcd * Do NOT consume a reference to itp. 5403055Sdanmcd */ 5413448Sdh155122 /*ARGSUSED*/ 5420Sstevel@tonic-gate static void 5433448Sdh155122 spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns) 5443055Sdanmcd { 5453055Sdanmcd boolean_t active = (boolean_t)cookie; 5463055Sdanmcd ipsec_policy_head_t *iph; 5473055Sdanmcd 5483055Sdanmcd iph = active ? itp->itp_policy : itp->itp_inactive; 5493055Sdanmcd IPPH_REFHOLD(iph); 5503055Sdanmcd mutex_enter(&itp->itp_lock); 5513448Sdh155122 spdsock_flush_one(iph, ns); 5523055Sdanmcd if (active) 5533055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 5543055Sdanmcd else 5553055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 5563055Sdanmcd mutex_exit(&itp->itp_lock); 5573055Sdanmcd } 5583055Sdanmcd 5593055Sdanmcd /* 5603055Sdanmcd * Clear out one polhead. 5613055Sdanmcd */ 5623055Sdanmcd static void 5633448Sdh155122 spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns) 5640Sstevel@tonic-gate { 5650Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_WRITER); 5663448Sdh155122 ipsec_polhead_flush(iph, ns); 5670Sstevel@tonic-gate rw_exit(&iph->iph_lock); 5683448Sdh155122 IPPH_REFRELE(iph, ns); 5693055Sdanmcd } 5703055Sdanmcd 5713055Sdanmcd static void 572*4307Spwernau spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, ipsec_tun_pol_t *itp, 573*4307Spwernau mblk_t *mp) 5743055Sdanmcd { 5753055Sdanmcd boolean_t active; 5763448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 5773448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 5783448Sdh155122 netstack_t *ns = spds->spds_netstack; 5793055Sdanmcd 5803055Sdanmcd if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) { 5813448Sdh155122 spdsock_flush_one(iph, spds->spds_netstack); 582*4307Spwernau if (audit_active) { 583*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 584*4307Spwernau 585*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 586*4307Spwernau audit_pf_policy(SPD_FLUSH, DB_CRED(mp), ns, 587*4307Spwernau ITP_NAME(itp), active, 0, DB_CPID(mp)); 588*4307Spwernau } 5893055Sdanmcd } else { 5903055Sdanmcd active = (iph == ALL_ACTIVE_POLHEADS); 5913055Sdanmcd 5923055Sdanmcd /* First flush the global policy. */ 5933448Sdh155122 spdsock_flush_one(active ? ipsec_system_policy(ns) : 5943448Sdh155122 ipsec_inactive_policy(ns), ns); 595*4307Spwernau if (audit_active) { 596*4307Spwernau audit_pf_policy(SPD_FLUSH, DB_CRED(mp), ns, NULL, 597*4307Spwernau active, 0, DB_CPID(mp)); 598*4307Spwernau } 5993055Sdanmcd /* Then flush every tunnel's appropriate one. */ 6003448Sdh155122 itp_walk(spdsock_flush_node, (void *)active, 6013448Sdh155122 spds->spds_netstack); 602*4307Spwernau if (audit_active) 603*4307Spwernau audit_pf_policy(SPD_FLUSH, DB_CRED(mp), ns, 604*4307Spwernau "all tunnels", active, 0, DB_CPID(mp)); 6053055Sdanmcd } 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate spd_echo(q, mp); 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate static boolean_t 6110Sstevel@tonic-gate spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag) 6120Sstevel@tonic-gate { 6130Sstevel@tonic-gate bzero(sel, sizeof (*sel)); 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate if (extv[SPD_EXT_PROTO] != NULL) { 6160Sstevel@tonic-gate struct spd_proto *pr = 6170Sstevel@tonic-gate (struct spd_proto *)extv[SPD_EXT_PROTO]; 6180Sstevel@tonic-gate sel->ipsl_proto = pr->spd_proto_number; 6190Sstevel@tonic-gate sel->ipsl_valid |= IPSL_PROTOCOL; 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate if (extv[SPD_EXT_LCLPORT] != NULL) { 6220Sstevel@tonic-gate struct spd_portrange *pr = 6230Sstevel@tonic-gate (struct spd_portrange *)extv[SPD_EXT_LCLPORT]; 6240Sstevel@tonic-gate sel->ipsl_lport = pr->spd_ports_minport; 6250Sstevel@tonic-gate sel->ipsl_valid |= IPSL_LOCAL_PORT; 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate if (extv[SPD_EXT_REMPORT] != NULL) { 6280Sstevel@tonic-gate struct spd_portrange *pr = 6290Sstevel@tonic-gate (struct spd_portrange *)extv[SPD_EXT_REMPORT]; 6300Sstevel@tonic-gate sel->ipsl_rport = pr->spd_ports_minport; 6310Sstevel@tonic-gate sel->ipsl_valid |= IPSL_REMOTE_PORT; 6320Sstevel@tonic-gate } 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) { 6350Sstevel@tonic-gate struct spd_typecode *tc= 6360Sstevel@tonic-gate (struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE]; 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate sel->ipsl_valid |= IPSL_ICMP_TYPE; 6390Sstevel@tonic-gate sel->ipsl_icmp_type = tc->spd_typecode_type; 6400Sstevel@tonic-gate if (tc->spd_typecode_type_end < tc->spd_typecode_type) 6410Sstevel@tonic-gate sel->ipsl_icmp_type_end = tc->spd_typecode_type; 6420Sstevel@tonic-gate else 6430Sstevel@tonic-gate sel->ipsl_icmp_type_end = tc->spd_typecode_type_end; 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate if (tc->spd_typecode_code != 255) { 6460Sstevel@tonic-gate sel->ipsl_valid |= IPSL_ICMP_CODE; 6470Sstevel@tonic-gate sel->ipsl_icmp_code = tc->spd_typecode_code; 6480Sstevel@tonic-gate if (tc->spd_typecode_code_end < tc->spd_typecode_code) 6490Sstevel@tonic-gate sel->ipsl_icmp_code_end = tc->spd_typecode_code; 6500Sstevel@tonic-gate else 6510Sstevel@tonic-gate sel->ipsl_icmp_code_end = 6520Sstevel@tonic-gate tc->spd_typecode_code_end; 6530Sstevel@tonic-gate } 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate #define ADDR2SEL(sel, extv, field, pfield, extn, bit) \ 6560Sstevel@tonic-gate if ((extv)[(extn)] != NULL) { \ 6570Sstevel@tonic-gate uint_t addrlen; \ 6580Sstevel@tonic-gate struct spd_address *ap = \ 6590Sstevel@tonic-gate (struct spd_address *)((extv)[(extn)]); \ 6600Sstevel@tonic-gate addrlen = (ap->spd_address_af == AF_INET6) ? \ 6610Sstevel@tonic-gate IPV6_ADDR_LEN : IP_ADDR_LEN; \ 6620Sstevel@tonic-gate if (SPD_64TO8(ap->spd_address_len) < \ 6630Sstevel@tonic-gate (addrlen + sizeof (*ap))) { \ 6640Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN; \ 6650Sstevel@tonic-gate return (B_FALSE); \ 6660Sstevel@tonic-gate } \ 6670Sstevel@tonic-gate bcopy((ap+1), &((sel)->field), addrlen); \ 6680Sstevel@tonic-gate (sel)->pfield = ap->spd_address_prefixlen; \ 6690Sstevel@tonic-gate (sel)->ipsl_valid |= (bit); \ 6700Sstevel@tonic-gate (sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ? \ 6710Sstevel@tonic-gate IPSL_IPV6 : IPSL_IPV4; \ 6720Sstevel@tonic-gate } 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen, 6750Sstevel@tonic-gate SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR); 6760Sstevel@tonic-gate ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen, 6770Sstevel@tonic-gate SPD_EXT_REMADDR, IPSL_REMOTE_ADDR); 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) == 6800Sstevel@tonic-gate (IPSL_IPV6|IPSL_IPV4)) { 6810Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_MIXED_AF; 6820Sstevel@tonic-gate return (B_FALSE); 6830Sstevel@tonic-gate } 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate #undef ADDR2SEL 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate return (B_TRUE); 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate static boolean_t 6910Sstevel@tonic-gate spd_convert_type(uint32_t type, ipsec_act_t *act) 6920Sstevel@tonic-gate { 6930Sstevel@tonic-gate switch (type) { 6940Sstevel@tonic-gate case SPD_ACTTYPE_DROP: 6950Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_DISCARD; 6960Sstevel@tonic-gate return (B_TRUE); 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate case SPD_ACTTYPE_PASS: 6990Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_CLEAR; 7000Sstevel@tonic-gate return (B_TRUE); 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate case SPD_ACTTYPE_IPSEC: 7030Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_APPLY; 7040Sstevel@tonic-gate return (B_TRUE); 7050Sstevel@tonic-gate } 7060Sstevel@tonic-gate return (B_FALSE); 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate 7090Sstevel@tonic-gate static boolean_t 7100Sstevel@tonic-gate spd_convert_flags(uint32_t flags, ipsec_act_t *act) 7110Sstevel@tonic-gate { 7120Sstevel@tonic-gate /* 7130Sstevel@tonic-gate * Note use of !! for boolean canonicalization. 7140Sstevel@tonic-gate */ 7150Sstevel@tonic-gate act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH); 7160Sstevel@tonic-gate act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP); 7170Sstevel@tonic-gate act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA); 7180Sstevel@tonic-gate act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE); 7190Sstevel@tonic-gate act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE); 7200Sstevel@tonic-gate return (B_TRUE); 7210Sstevel@tonic-gate } 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate static void 7240Sstevel@tonic-gate spdsock_reset_act(ipsec_act_t *act) 7250Sstevel@tonic-gate { 7260Sstevel@tonic-gate bzero(act, sizeof (*act)); 7270Sstevel@tonic-gate act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS; 7280Sstevel@tonic-gate act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS; 7290Sstevel@tonic-gate act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS; 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate /* 7330Sstevel@tonic-gate * Sanity check action against reality, and shrink-wrap key sizes.. 7340Sstevel@tonic-gate */ 7350Sstevel@tonic-gate static boolean_t 7363448Sdh155122 spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag, 7373448Sdh155122 spd_stack_t *spds) 7380Sstevel@tonic-gate { 7393055Sdanmcd if (tunnel_polhead && act->ipa_apply.ipp_use_unique) { 7403055Sdanmcd *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7413055Sdanmcd return (B_FALSE); 7423055Sdanmcd } 7430Sstevel@tonic-gate if ((act->ipa_type != IPSEC_ACT_APPLY) && 7440Sstevel@tonic-gate (act->ipa_apply.ipp_use_ah || 7450Sstevel@tonic-gate act->ipa_apply.ipp_use_esp || 7460Sstevel@tonic-gate act->ipa_apply.ipp_use_espa || 7470Sstevel@tonic-gate act->ipa_apply.ipp_use_se || 7480Sstevel@tonic-gate act->ipa_apply.ipp_use_unique)) { 7490Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7500Sstevel@tonic-gate return (B_FALSE); 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate if ((act->ipa_type == IPSEC_ACT_APPLY) && 7530Sstevel@tonic-gate !act->ipa_apply.ipp_use_ah && 7540Sstevel@tonic-gate !act->ipa_apply.ipp_use_esp) { 7550Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7560Sstevel@tonic-gate return (B_FALSE); 7570Sstevel@tonic-gate } 7583448Sdh155122 return (ipsec_check_action(act, diag, spds->spds_netstack)); 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate /* 7620Sstevel@tonic-gate * We may be short a few error checks here.. 7630Sstevel@tonic-gate */ 7640Sstevel@tonic-gate static boolean_t 7650Sstevel@tonic-gate spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp, 7663448Sdh155122 int *diag, spd_stack_t *spds) 7670Sstevel@tonic-gate { 7680Sstevel@tonic-gate struct spd_ext_actions *sactp = 7690Sstevel@tonic-gate (struct spd_ext_actions *)extv[SPD_EXT_ACTION]; 7700Sstevel@tonic-gate ipsec_act_t act, *actp, *endactp; 7710Sstevel@tonic-gate struct spd_attribute *attrp, *endattrp; 7720Sstevel@tonic-gate uint64_t *endp; 7730Sstevel@tonic-gate int nact; 7743055Sdanmcd boolean_t tunnel_polhead; 7753055Sdanmcd 7763055Sdanmcd tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL && 7773055Sdanmcd (((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags & 7783055Sdanmcd SPD_RULE_FLAG_TUNNEL)); 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate *actpp = NULL; 7810Sstevel@tonic-gate *nactp = 0; 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate if (sactp == NULL) { 7840Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_NO_ACTION_EXT; 7850Sstevel@tonic-gate return (B_FALSE); 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate 7880Sstevel@tonic-gate /* 7890Sstevel@tonic-gate * Parse the "action" extension and convert into an action chain. 7900Sstevel@tonic-gate */ 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate nact = sactp->spd_actions_count; 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate endp = (uint64_t *)sactp; 7950Sstevel@tonic-gate endp += sactp->spd_actions_len; 7960Sstevel@tonic-gate endattrp = (struct spd_attribute *)endp; 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP); 7990Sstevel@tonic-gate if (actp == NULL) { 8000Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_NO_MEM; 8010Sstevel@tonic-gate return (B_FALSE); 8020Sstevel@tonic-gate } 8030Sstevel@tonic-gate *actpp = actp; 8040Sstevel@tonic-gate *nactp = nact; 8050Sstevel@tonic-gate endactp = actp + nact; 8060Sstevel@tonic-gate 8070Sstevel@tonic-gate spdsock_reset_act(&act); 8080Sstevel@tonic-gate attrp = (struct spd_attribute *)(&sactp[1]); 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate for (; attrp < endattrp; attrp++) { 8110Sstevel@tonic-gate switch (attrp->spd_attr_tag) { 8120Sstevel@tonic-gate case SPD_ATTR_NOP: 8130Sstevel@tonic-gate break; 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate case SPD_ATTR_EMPTY: 8160Sstevel@tonic-gate spdsock_reset_act(&act); 8170Sstevel@tonic-gate break; 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate case SPD_ATTR_END: 8200Sstevel@tonic-gate attrp = endattrp; 8210Sstevel@tonic-gate /* FALLTHRU */ 8220Sstevel@tonic-gate case SPD_ATTR_NEXT: 8230Sstevel@tonic-gate if (actp >= endactp) { 8240Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT; 8250Sstevel@tonic-gate goto fail; 8260Sstevel@tonic-gate } 8273448Sdh155122 if (!spdsock_check_action(&act, tunnel_polhead, 8283448Sdh155122 diag, spds)) 8290Sstevel@tonic-gate goto fail; 8300Sstevel@tonic-gate *actp++ = act; 8313055Sdanmcd spdsock_reset_act(&act); 8320Sstevel@tonic-gate break; 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate case SPD_ATTR_TYPE: 8350Sstevel@tonic-gate if (!spd_convert_type(attrp->spd_attr_value, &act)) { 8360Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE; 8370Sstevel@tonic-gate goto fail; 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate break; 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate case SPD_ATTR_FLAGS: 8423055Sdanmcd if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) { 8433055Sdanmcd /* 8443055Sdanmcd * Set "sa unique" for transport-mode 8453055Sdanmcd * tunnels whether we want to or not. 8463055Sdanmcd */ 8473055Sdanmcd attrp->spd_attr_value |= SPD_APPLY_UNIQUE; 8483055Sdanmcd } 8490Sstevel@tonic-gate if (!spd_convert_flags(attrp->spd_attr_value, &act)) { 8500Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS; 8510Sstevel@tonic-gate goto fail; 8520Sstevel@tonic-gate } 8530Sstevel@tonic-gate break; 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate case SPD_ATTR_AH_AUTH: 8563055Sdanmcd if (attrp->spd_attr_value == 0) { 8573055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG; 8583055Sdanmcd goto fail; 8593055Sdanmcd } 8600Sstevel@tonic-gate act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value; 8610Sstevel@tonic-gate break; 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate case SPD_ATTR_ESP_ENCR: 8643055Sdanmcd if (attrp->spd_attr_value == 0) { 8653055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG; 8663055Sdanmcd goto fail; 8673055Sdanmcd } 8680Sstevel@tonic-gate act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value; 8690Sstevel@tonic-gate break; 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate case SPD_ATTR_ESP_AUTH: 8723055Sdanmcd if (attrp->spd_attr_value == 0) { 8733055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG; 8743055Sdanmcd goto fail; 8753055Sdanmcd } 8760Sstevel@tonic-gate act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value; 8770Sstevel@tonic-gate break; 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate case SPD_ATTR_ENCR_MINBITS: 8800Sstevel@tonic-gate act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value; 8810Sstevel@tonic-gate break; 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate case SPD_ATTR_ENCR_MAXBITS: 8840Sstevel@tonic-gate act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value; 8850Sstevel@tonic-gate break; 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate case SPD_ATTR_AH_MINBITS: 8880Sstevel@tonic-gate act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value; 8890Sstevel@tonic-gate break; 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate case SPD_ATTR_AH_MAXBITS: 8920Sstevel@tonic-gate act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value; 8930Sstevel@tonic-gate break; 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate case SPD_ATTR_ESPA_MINBITS: 8960Sstevel@tonic-gate act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value; 8970Sstevel@tonic-gate break; 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate case SPD_ATTR_ESPA_MAXBITS: 9000Sstevel@tonic-gate act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value; 9010Sstevel@tonic-gate break; 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate case SPD_ATTR_LIFE_SOFT_TIME: 9040Sstevel@tonic-gate case SPD_ATTR_LIFE_HARD_TIME: 9050Sstevel@tonic-gate case SPD_ATTR_LIFE_SOFT_BYTES: 9060Sstevel@tonic-gate case SPD_ATTR_LIFE_HARD_BYTES: 9070Sstevel@tonic-gate break; 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate case SPD_ATTR_KM_PROTO: 9100Sstevel@tonic-gate act.ipa_apply.ipp_km_proto = attrp->spd_attr_value; 9110Sstevel@tonic-gate break; 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate case SPD_ATTR_KM_COOKIE: 9140Sstevel@tonic-gate act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value; 9150Sstevel@tonic-gate break; 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate case SPD_ATTR_REPLAY_DEPTH: 9180Sstevel@tonic-gate act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value; 9190Sstevel@tonic-gate break; 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate } 9220Sstevel@tonic-gate if (actp != endactp) { 9230Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT; 9240Sstevel@tonic-gate goto fail; 9250Sstevel@tonic-gate } 9260Sstevel@tonic-gate 9270Sstevel@tonic-gate return (B_TRUE); 9280Sstevel@tonic-gate fail: 9290Sstevel@tonic-gate ipsec_actvec_free(*actpp, nact); 9300Sstevel@tonic-gate *actpp = NULL; 9310Sstevel@tonic-gate return (B_FALSE); 9320Sstevel@tonic-gate } 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate typedef struct 9350Sstevel@tonic-gate { 9360Sstevel@tonic-gate ipsec_policy_t *pol; 9370Sstevel@tonic-gate int dir; 9380Sstevel@tonic-gate } tmprule_t; 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate static int 9410Sstevel@tonic-gate mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule, 9420Sstevel@tonic-gate ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af, 9433448Sdh155122 tmprule_t **rp, uint64_t *index, spd_stack_t *spds) 9440Sstevel@tonic-gate { 9450Sstevel@tonic-gate ipsec_policy_t *pol; 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4); 9480Sstevel@tonic-gate sel->ipsl_valid |= af; 9490Sstevel@tonic-gate 9503055Sdanmcd pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority, 9513448Sdh155122 index, spds->spds_netstack); 9520Sstevel@tonic-gate if (pol == NULL) 9530Sstevel@tonic-gate return (ENOMEM); 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate (*rp)->pol = pol; 9560Sstevel@tonic-gate (*rp)->dir = dir; 9570Sstevel@tonic-gate (*rp)++; 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate if (!ipsec_check_policy(iph, pol, dir)) 9600Sstevel@tonic-gate return (EEXIST); 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate rule->spd_rule_index = pol->ipsp_index; 9630Sstevel@tonic-gate return (0); 9640Sstevel@tonic-gate } 9650Sstevel@tonic-gate 9660Sstevel@tonic-gate static int 9670Sstevel@tonic-gate mkrulepair(ipsec_policy_head_t *iph, struct spd_rule *rule, 9680Sstevel@tonic-gate ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t afs, 9693448Sdh155122 tmprule_t **rp, uint64_t *index, spd_stack_t *spds) 9700Sstevel@tonic-gate { 9710Sstevel@tonic-gate int error; 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate if (afs & IPSL_IPV4) { 9743055Sdanmcd error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV4, rp, 9753448Sdh155122 index, spds); 9760Sstevel@tonic-gate if (error != 0) 9770Sstevel@tonic-gate return (error); 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate if (afs & IPSL_IPV6) { 9803055Sdanmcd error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV6, rp, 9813448Sdh155122 index, spds); 9820Sstevel@tonic-gate if (error != 0) 9830Sstevel@tonic-gate return (error); 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate return (0); 9860Sstevel@tonic-gate } 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate static void 9903055Sdanmcd spdsock_addrule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 9913055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 9920Sstevel@tonic-gate { 9930Sstevel@tonic-gate ipsec_selkey_t sel; 9940Sstevel@tonic-gate ipsec_act_t *actp; 9950Sstevel@tonic-gate uint_t nact; 9963055Sdanmcd int diag = 0, error, afs; 9970Sstevel@tonic-gate struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE]; 9980Sstevel@tonic-gate tmprule_t rules[4], *rulep = &rules[0]; 9993055Sdanmcd boolean_t tunnel_mode, empty_itp, active; 10003055Sdanmcd uint64_t *index = (itp == NULL) ? NULL : &itp->itp_next_policy_index; 10013448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 10023448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate if (rule == NULL) { 10050Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT); 1006*4307Spwernau if (audit_active) { 1007*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1008*4307Spwernau 1009*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1010*4307Spwernau audit_pf_policy(SPD_ADDRULE, DB_CRED(mp), 1011*4307Spwernau spds->spds_netstack, ITP_NAME(itp), active, 1012*4307Spwernau SPD_DIAGNOSTIC_NO_RULE_EXT, DB_CPID(mp)); 1013*4307Spwernau } 10140Sstevel@tonic-gate return; 10150Sstevel@tonic-gate } 10160Sstevel@tonic-gate 10173055Sdanmcd tunnel_mode = (rule->spd_rule_flags & SPD_RULE_FLAG_TUNNEL); 10183055Sdanmcd 10193055Sdanmcd if (itp != NULL) { 10203055Sdanmcd mutex_enter(&itp->itp_lock); 10213055Sdanmcd ASSERT(itp->itp_policy == iph || itp->itp_inactive == iph); 10223055Sdanmcd active = (itp->itp_policy == iph); 10233055Sdanmcd if (ITP_P_ISACTIVE(itp, iph)) { 10243055Sdanmcd /* Check for mix-and-match of tunnel/transport. */ 10253055Sdanmcd if ((tunnel_mode && !ITP_P_ISTUNNEL(itp, iph)) || 10263055Sdanmcd (!tunnel_mode && ITP_P_ISTUNNEL(itp, iph))) { 10273055Sdanmcd mutex_exit(&itp->itp_lock); 10283055Sdanmcd spdsock_error(q, mp, EBUSY, 0); 10293055Sdanmcd return; 10303055Sdanmcd } 10313055Sdanmcd empty_itp = B_FALSE; 10323055Sdanmcd } else { 10333055Sdanmcd empty_itp = B_TRUE; 10343055Sdanmcd itp->itp_flags = active ? ITPF_P_ACTIVE : ITPF_I_ACTIVE; 10353055Sdanmcd if (tunnel_mode) 10363055Sdanmcd itp->itp_flags |= active ? ITPF_P_TUNNEL : 10373055Sdanmcd ITPF_I_TUNNEL; 10383055Sdanmcd } 10393055Sdanmcd } else { 10403055Sdanmcd empty_itp = B_FALSE; 10413055Sdanmcd } 10423055Sdanmcd 10430Sstevel@tonic-gate if (rule->spd_rule_index != 0) { 10443055Sdanmcd diag = SPD_DIAGNOSTIC_INVALID_RULE_INDEX; 10453055Sdanmcd error = EINVAL; 10463055Sdanmcd goto fail2; 10470Sstevel@tonic-gate } 10480Sstevel@tonic-gate 10490Sstevel@tonic-gate if (!spdsock_ext_to_sel(extv, &sel, &diag)) { 10503055Sdanmcd error = EINVAL; 10513055Sdanmcd goto fail2; 10523055Sdanmcd } 10533055Sdanmcd 10543055Sdanmcd if (itp != NULL) { 10553055Sdanmcd if (tunnel_mode) { 10563055Sdanmcd if (sel.ipsl_valid & 10573055Sdanmcd (IPSL_REMOTE_PORT | IPSL_LOCAL_PORT)) { 10583055Sdanmcd itp->itp_flags |= active ? 10593055Sdanmcd ITPF_P_PER_PORT_SECURITY : 10603055Sdanmcd ITPF_I_PER_PORT_SECURITY; 10613055Sdanmcd } 10623055Sdanmcd } else { 10633055Sdanmcd /* 10643055Sdanmcd * For now, we don't allow transport-mode on a tunnel 10653055Sdanmcd * with ANY specific selectors. Bail if we have such 10663055Sdanmcd * a request. 10673055Sdanmcd */ 10683055Sdanmcd if (sel.ipsl_valid & IPSL_WILDCARD) { 10693055Sdanmcd diag = SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS; 10703055Sdanmcd error = EINVAL; 10713055Sdanmcd goto fail2; 10723055Sdanmcd } 10733055Sdanmcd } 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate 10763448Sdh155122 if (!spdsock_ext_to_actvec(extv, &actp, &nact, &diag, spds)) { 10773055Sdanmcd error = EINVAL; 10783055Sdanmcd goto fail2; 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate /* 10810Sstevel@tonic-gate * If no addresses were specified, add both. 10820Sstevel@tonic-gate */ 10830Sstevel@tonic-gate afs = sel.ipsl_valid & (IPSL_IPV6|IPSL_IPV4); 10840Sstevel@tonic-gate if (afs == 0) 10850Sstevel@tonic-gate afs = (IPSL_IPV6|IPSL_IPV4); 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_WRITER); 10880Sstevel@tonic-gate 10890Sstevel@tonic-gate if (rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) { 10900Sstevel@tonic-gate error = mkrulepair(iph, rule, &sel, actp, nact, 10913448Sdh155122 IPSEC_TYPE_OUTBOUND, afs, &rulep, index, spds); 10920Sstevel@tonic-gate if (error != 0) 10930Sstevel@tonic-gate goto fail; 10940Sstevel@tonic-gate } 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate if (rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) { 10970Sstevel@tonic-gate error = mkrulepair(iph, rule, &sel, actp, nact, 10983448Sdh155122 IPSEC_TYPE_INBOUND, afs, &rulep, index, spds); 10990Sstevel@tonic-gate if (error != 0) 11000Sstevel@tonic-gate goto fail; 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate 11033448Sdh155122 while ((--rulep) >= &rules[0]) { 11043448Sdh155122 ipsec_enter_policy(iph, rulep->pol, rulep->dir, 11053448Sdh155122 spds->spds_netstack); 11063448Sdh155122 } 11070Sstevel@tonic-gate rw_exit(&iph->iph_lock); 11083055Sdanmcd if (itp != NULL) 11093055Sdanmcd mutex_exit(&itp->itp_lock); 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate ipsec_actvec_free(actp, nact); 11120Sstevel@tonic-gate spd_echo(q, mp); 1113*4307Spwernau if (audit_active) { 1114*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1115*4307Spwernau 1116*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1117*4307Spwernau audit_pf_policy(SPD_ADDRULE, DB_CRED(mp), spds->spds_netstack, 1118*4307Spwernau ITP_NAME(itp), active, 0, DB_CPID(mp)); 1119*4307Spwernau } 11200Sstevel@tonic-gate return; 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate fail: 11230Sstevel@tonic-gate rw_exit(&iph->iph_lock); 11240Sstevel@tonic-gate while ((--rulep) >= &rules[0]) { 11253448Sdh155122 IPPOL_REFRELE(rulep->pol, spds->spds_netstack); 11260Sstevel@tonic-gate } 11270Sstevel@tonic-gate ipsec_actvec_free(actp, nact); 11283055Sdanmcd fail2: 11293055Sdanmcd if (itp != NULL) { 11303055Sdanmcd if (empty_itp) 11313055Sdanmcd itp->itp_flags = 0; 11323055Sdanmcd mutex_exit(&itp->itp_lock); 11333055Sdanmcd } 11343055Sdanmcd spdsock_error(q, mp, error, diag); 1135*4307Spwernau if (audit_active) { 1136*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1137*4307Spwernau 1138*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1139*4307Spwernau audit_pf_policy(SPD_ADDRULE, DB_CRED(mp), spds->spds_netstack, 1140*4307Spwernau ITP_NAME(itp), active, error, DB_CPID(mp)); 1141*4307Spwernau } 11420Sstevel@tonic-gate } 11430Sstevel@tonic-gate 11440Sstevel@tonic-gate void 11453055Sdanmcd spdsock_deleterule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 11463055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 11470Sstevel@tonic-gate { 11480Sstevel@tonic-gate ipsec_selkey_t sel; 11490Sstevel@tonic-gate struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE]; 11503055Sdanmcd int err, diag = 0; 11513448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 11523448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 11530Sstevel@tonic-gate 11540Sstevel@tonic-gate if (rule == NULL) { 11550Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT); 1156*4307Spwernau if (audit_active) { 1157*4307Spwernau boolean_t active; 1158*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1159*4307Spwernau 1160*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1161*4307Spwernau audit_pf_policy(SPD_DELETERULE, DB_CRED(mp), 1162*4307Spwernau spds->spds_netstack, ITP_NAME(itp), active, 1163*4307Spwernau SPD_DIAGNOSTIC_NO_RULE_EXT, DB_CPID(mp)); 1164*4307Spwernau } 11650Sstevel@tonic-gate return; 11660Sstevel@tonic-gate } 11670Sstevel@tonic-gate 11683055Sdanmcd /* 11693055Sdanmcd * Must enter itp_lock first to avoid deadlock. See tun.c's 11703055Sdanmcd * set_sec_simple() for the other case of itp_lock and iph_lock. 11713055Sdanmcd */ 11723055Sdanmcd if (itp != NULL) 11733055Sdanmcd mutex_enter(&itp->itp_lock); 11743055Sdanmcd 11750Sstevel@tonic-gate if (rule->spd_rule_index != 0) { 11763448Sdh155122 if (ipsec_policy_delete_index(iph, rule->spd_rule_index, 11773448Sdh155122 spds->spds_netstack) != 0) { 11783055Sdanmcd err = ESRCH; 11793055Sdanmcd goto fail; 11800Sstevel@tonic-gate } 11810Sstevel@tonic-gate } else { 11820Sstevel@tonic-gate if (!spdsock_ext_to_sel(extv, &sel, &diag)) { 11833055Sdanmcd err = EINVAL; /* diag already set... */ 11843055Sdanmcd goto fail; 11853055Sdanmcd } 11863055Sdanmcd 11873055Sdanmcd if ((rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) && 11883448Sdh155122 !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_INBOUND, 11893448Sdh155122 spds->spds_netstack)) { 11903055Sdanmcd err = ESRCH; 11913055Sdanmcd goto fail; 11923055Sdanmcd } 11933055Sdanmcd 11943055Sdanmcd if ((rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) && 11953448Sdh155122 !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_OUTBOUND, 11963448Sdh155122 spds->spds_netstack)) { 11973055Sdanmcd err = ESRCH; 11983055Sdanmcd goto fail; 11990Sstevel@tonic-gate } 12003055Sdanmcd } 12013055Sdanmcd 12023055Sdanmcd if (itp != NULL) { 12033055Sdanmcd ASSERT(iph == itp->itp_policy || iph == itp->itp_inactive); 12043055Sdanmcd rw_enter(&iph->iph_lock, RW_READER); 12053055Sdanmcd if (avl_numnodes(&iph->iph_rulebyid) == 0) { 12063055Sdanmcd if (iph == itp->itp_policy) 12073055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 12083055Sdanmcd else 12093055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 12100Sstevel@tonic-gate } 12113055Sdanmcd /* Can exit locks in any order. */ 12123055Sdanmcd rw_exit(&iph->iph_lock); 12133055Sdanmcd mutex_exit(&itp->itp_lock); 12140Sstevel@tonic-gate } 12150Sstevel@tonic-gate spd_echo(q, mp); 1216*4307Spwernau if (audit_active) { 1217*4307Spwernau boolean_t active; 1218*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1219*4307Spwernau 1220*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1221*4307Spwernau audit_pf_policy(SPD_DELETERULE, DB_CRED(mp), 1222*4307Spwernau spds->spds_netstack, ITP_NAME(itp), active, 0, DB_CPID(mp)); 1223*4307Spwernau } 12240Sstevel@tonic-gate return; 12250Sstevel@tonic-gate fail: 12263055Sdanmcd if (itp != NULL) 12273055Sdanmcd mutex_exit(&itp->itp_lock); 12283055Sdanmcd spdsock_error(q, mp, err, diag); 1229*4307Spwernau if (audit_active) { 1230*4307Spwernau boolean_t active; 1231*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1232*4307Spwernau 1233*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1234*4307Spwernau audit_pf_policy(SPD_DELETERULE, DB_CRED(mp), 1235*4307Spwernau spds->spds_netstack, ITP_NAME(itp), active, err, 1236*4307Spwernau DB_CPID(mp)); 1237*4307Spwernau } 12383055Sdanmcd } 12393055Sdanmcd 12403055Sdanmcd /* Do NOT consume a reference to itp. */ 12413055Sdanmcd /* ARGSUSED */ 12423055Sdanmcd static void 12433448Sdh155122 spdsock_flip_node(ipsec_tun_pol_t *itp, void *ignoreme, netstack_t *ns) 12443055Sdanmcd { 12453055Sdanmcd mutex_enter(&itp->itp_lock); 12463055Sdanmcd ITPF_SWAP(itp->itp_flags); 12473448Sdh155122 ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns); 12483055Sdanmcd mutex_exit(&itp->itp_lock); 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate 12510Sstevel@tonic-gate void 12523055Sdanmcd spdsock_flip(queue_t *q, mblk_t *mp, spd_if_t *tunname) 12530Sstevel@tonic-gate { 12543055Sdanmcd char *tname; 12553055Sdanmcd ipsec_tun_pol_t *itp; 12563448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 12573448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 12583055Sdanmcd 12593055Sdanmcd if (tunname != NULL) { 12603055Sdanmcd tname = (char *)tunname->spd_if_name; 12613055Sdanmcd if (*tname == '\0') { 12623448Sdh155122 /* can't fail */ 12633448Sdh155122 ipsec_swap_global_policy(spds->spds_netstack); 1264*4307Spwernau if (audit_active) { 1265*4307Spwernau boolean_t active; 1266*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1267*4307Spwernau 1268*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1269*4307Spwernau audit_pf_policy(SPD_FLIP, DB_CRED(mp), 1270*4307Spwernau spds->spds_netstack, NULL, active, 0, 1271*4307Spwernau DB_CPID(mp)); 1272*4307Spwernau } 12733448Sdh155122 itp_walk(spdsock_flip_node, NULL, spds->spds_netstack); 1274*4307Spwernau if (audit_active) { 1275*4307Spwernau boolean_t active; 1276*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1277*4307Spwernau 1278*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1279*4307Spwernau audit_pf_policy(SPD_FLIP, DB_CRED(mp), 1280*4307Spwernau spds->spds_netstack, "all tunnels", active, 1281*4307Spwernau 0, DB_CPID(mp)); 1282*4307Spwernau } 12833055Sdanmcd } else { 12843448Sdh155122 itp = get_tunnel_policy(tname, spds->spds_netstack); 12853055Sdanmcd if (itp == NULL) { 12863055Sdanmcd /* Better idea for "tunnel not found"? */ 12873055Sdanmcd spdsock_error(q, mp, ESRCH, 0); 1288*4307Spwernau if (audit_active) { 1289*4307Spwernau boolean_t active; 1290*4307Spwernau spd_msg_t *spmsg = 1291*4307Spwernau (spd_msg_t *)mp->b_rptr; 1292*4307Spwernau 1293*4307Spwernau active = (spmsg->spd_msg_spdid == 1294*4307Spwernau SPD_ACTIVE); 1295*4307Spwernau audit_pf_policy(SPD_FLIP, DB_CRED(mp), 1296*4307Spwernau spds->spds_netstack, ITP_NAME(itp), 1297*4307Spwernau active, ESRCH, DB_CPID(mp)); 1298*4307Spwernau } 12993055Sdanmcd return; 13003055Sdanmcd } 13013448Sdh155122 spdsock_flip_node(itp, NULL, NULL); 1302*4307Spwernau if (audit_active) { 1303*4307Spwernau boolean_t active; 1304*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1305*4307Spwernau 1306*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1307*4307Spwernau audit_pf_policy(SPD_FLIP, DB_CRED(mp), 1308*4307Spwernau spds->spds_netstack, ITP_NAME(itp), active, 1309*4307Spwernau 0, DB_CPID(mp)); 1310*4307Spwernau } 13113448Sdh155122 ITP_REFRELE(itp, spds->spds_netstack); 13123055Sdanmcd } 13133055Sdanmcd } else { 13143448Sdh155122 ipsec_swap_global_policy(spds->spds_netstack); /* can't fail */ 1315*4307Spwernau if (audit_active) { 1316*4307Spwernau boolean_t active; 1317*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1318*4307Spwernau 1319*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1320*4307Spwernau audit_pf_policy(SPD_FLIP, DB_CRED(mp), 1321*4307Spwernau spds->spds_netstack, NULL, active, 0, DB_CPID(mp)); 1322*4307Spwernau } 13233055Sdanmcd } 13240Sstevel@tonic-gate spd_echo(q, mp); 13250Sstevel@tonic-gate } 13260Sstevel@tonic-gate 13270Sstevel@tonic-gate /* 13280Sstevel@tonic-gate * Unimplemented feature 13290Sstevel@tonic-gate */ 13300Sstevel@tonic-gate /* ARGSUSED */ 13310Sstevel@tonic-gate static void 13323055Sdanmcd spdsock_lookup(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 13333055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 13340Sstevel@tonic-gate { 13350Sstevel@tonic-gate spdsock_error(q, mp, EINVAL, 0); 13360Sstevel@tonic-gate } 13370Sstevel@tonic-gate 13380Sstevel@tonic-gate 13390Sstevel@tonic-gate static mblk_t * 13400Sstevel@tonic-gate spdsock_dump_ruleset(mblk_t *req, ipsec_policy_head_t *iph, 13410Sstevel@tonic-gate uint32_t count, uint16_t error) 13420Sstevel@tonic-gate { 13430Sstevel@tonic-gate size_t len = sizeof (spd_ruleset_ext_t) + sizeof (spd_msg_t); 13440Sstevel@tonic-gate spd_msg_t *msg; 13450Sstevel@tonic-gate spd_ruleset_ext_t *ruleset; 13460Sstevel@tonic-gate mblk_t *m = allocb(len, BPRI_HI); 13470Sstevel@tonic-gate 13480Sstevel@tonic-gate ASSERT(RW_READ_HELD(&iph->iph_lock)); 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate if (m == NULL) { 13510Sstevel@tonic-gate return (NULL); 13520Sstevel@tonic-gate } 13530Sstevel@tonic-gate msg = (spd_msg_t *)m->b_rptr; 13540Sstevel@tonic-gate ruleset = (spd_ruleset_ext_t *)(&msg[1]); 13550Sstevel@tonic-gate 13560Sstevel@tonic-gate m->b_wptr = (uint8_t *)&ruleset[1]; 13570Sstevel@tonic-gate 13580Sstevel@tonic-gate *msg = *(spd_msg_t *)(req->b_rptr); 13590Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(len); 13600Sstevel@tonic-gate msg->spd_msg_errno = error; 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate ruleset->spd_ruleset_len = SPD_8TO64(sizeof (*ruleset)); 13630Sstevel@tonic-gate ruleset->spd_ruleset_type = SPD_EXT_RULESET; 13640Sstevel@tonic-gate ruleset->spd_ruleset_count = count; 13650Sstevel@tonic-gate ruleset->spd_ruleset_version = iph->iph_gen; 13660Sstevel@tonic-gate return (m); 13670Sstevel@tonic-gate } 13680Sstevel@tonic-gate 13690Sstevel@tonic-gate static mblk_t * 13700Sstevel@tonic-gate spdsock_dump_finish(spdsock_t *ss, int error) 13710Sstevel@tonic-gate { 13720Sstevel@tonic-gate mblk_t *m; 13730Sstevel@tonic-gate ipsec_policy_head_t *iph = ss->spdsock_dump_head; 13740Sstevel@tonic-gate mblk_t *req = ss->spdsock_dump_req; 13753055Sdanmcd ipsec_tun_pol_t *itp, dummy; 13763448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 13773448Sdh155122 netstack_t *ns = spds->spds_netstack; 13783448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 13793055Sdanmcd 13803055Sdanmcd ss->spdsock_dump_remaining_polheads--; 13813055Sdanmcd if (error == 0 && ss->spdsock_dump_remaining_polheads != 0) { 13823055Sdanmcd /* Attempt a respin with a new policy head. */ 13833448Sdh155122 rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER); 13843055Sdanmcd /* NOTE: No need for ITP_REF*() macros here. */ 13853448Sdh155122 if (ipss->ipsec_tunnel_policy_gen > ss->spdsock_dump_tun_gen) { 13863055Sdanmcd /* Bail with EAGAIN. */ 13873055Sdanmcd error = EAGAIN; 13883055Sdanmcd } else if (ss->spdsock_dump_name[0] == '\0') { 13893055Sdanmcd /* Just finished global, find first node. */ 13903448Sdh155122 itp = (ipsec_tun_pol_t *)avl_first( 13913448Sdh155122 &ipss->ipsec_tunnel_policies); 13923055Sdanmcd } else { 13933055Sdanmcd /* 13943055Sdanmcd * We just finished current-named polhead, find 13953055Sdanmcd * the next one. 13963055Sdanmcd */ 13973055Sdanmcd (void) strncpy(dummy.itp_name, ss->spdsock_dump_name, 13983055Sdanmcd LIFNAMSIZ); 13993448Sdh155122 itp = (ipsec_tun_pol_t *)avl_find( 14003448Sdh155122 &ipss->ipsec_tunnel_policies, &dummy, NULL); 14013055Sdanmcd ASSERT(itp != NULL); 14023448Sdh155122 itp = (ipsec_tun_pol_t *)AVL_NEXT( 14033448Sdh155122 &ipss->ipsec_tunnel_policies, itp); 14043055Sdanmcd /* remaining_polheads should maintain this assertion. */ 14053055Sdanmcd ASSERT(itp != NULL); 14063055Sdanmcd } 14073055Sdanmcd if (error == 0) { 14083055Sdanmcd (void) strncpy(ss->spdsock_dump_name, itp->itp_name, 14093055Sdanmcd LIFNAMSIZ); 14103055Sdanmcd /* Reset other spdsock_dump thingies. */ 14113448Sdh155122 IPPH_REFRELE(ss->spdsock_dump_head, ns); 14123055Sdanmcd if (ss->spdsock_dump_active) { 14133055Sdanmcd ss->spdsock_dump_tunnel = 14143055Sdanmcd itp->itp_flags & ITPF_P_TUNNEL; 14153055Sdanmcd iph = itp->itp_policy; 14163055Sdanmcd } else { 14173055Sdanmcd ss->spdsock_dump_tunnel = 14183055Sdanmcd itp->itp_flags & ITPF_I_TUNNEL; 14193055Sdanmcd iph = itp->itp_inactive; 14203055Sdanmcd } 14213055Sdanmcd IPPH_REFHOLD(iph); 14223055Sdanmcd rw_enter(&iph->iph_lock, RW_READER); 14233055Sdanmcd ss->spdsock_dump_head = iph; 14243055Sdanmcd ss->spdsock_dump_gen = iph->iph_gen; 14253055Sdanmcd ss->spdsock_dump_cur_type = 0; 14263055Sdanmcd ss->spdsock_dump_cur_af = IPSEC_AF_V4; 14273055Sdanmcd ss->spdsock_dump_cur_rule = NULL; 14283055Sdanmcd ss->spdsock_dump_count = 0; 14293055Sdanmcd ss->spdsock_dump_cur_chain = 0; 14303055Sdanmcd rw_exit(&iph->iph_lock); 14313448Sdh155122 rw_exit(&ipss->ipsec_tunnel_policy_lock); 14323055Sdanmcd /* And start again. */ 14333055Sdanmcd return (spdsock_dump_next_record(ss)); 14343055Sdanmcd } 14353448Sdh155122 rw_exit(&ipss->ipsec_tunnel_policy_lock); 14363055Sdanmcd } 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 14390Sstevel@tonic-gate m = spdsock_dump_ruleset(req, iph, ss->spdsock_dump_count, error); 14400Sstevel@tonic-gate rw_exit(&iph->iph_lock); 14413448Sdh155122 IPPH_REFRELE(iph, ns); 14420Sstevel@tonic-gate ss->spdsock_dump_req = NULL; 14430Sstevel@tonic-gate freemsg(req); 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate return (m); 14460Sstevel@tonic-gate } 14470Sstevel@tonic-gate 14480Sstevel@tonic-gate /* 14490Sstevel@tonic-gate * Rule encoding functions. 14500Sstevel@tonic-gate * We do a two-pass encode. 14510Sstevel@tonic-gate * If base != NULL, fill in encoded rule part starting at base+offset. 14520Sstevel@tonic-gate * Always return "offset" plus length of to-be-encoded data. 14530Sstevel@tonic-gate */ 14540Sstevel@tonic-gate static uint_t 14550Sstevel@tonic-gate spdsock_encode_typecode(uint8_t *base, uint_t offset, uint8_t type, 14560Sstevel@tonic-gate uint8_t type_end, uint8_t code, uint8_t code_end) 14570Sstevel@tonic-gate { 14580Sstevel@tonic-gate struct spd_typecode *tcp; 14590Sstevel@tonic-gate 14600Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14610Sstevel@tonic-gate 14620Sstevel@tonic-gate if (base != NULL) { 14630Sstevel@tonic-gate tcp = (struct spd_typecode *)(base + offset); 14640Sstevel@tonic-gate tcp->spd_typecode_len = SPD_8TO64(sizeof (*tcp)); 14650Sstevel@tonic-gate tcp->spd_typecode_exttype = SPD_EXT_ICMP_TYPECODE; 14660Sstevel@tonic-gate tcp->spd_typecode_code = code; 14670Sstevel@tonic-gate tcp->spd_typecode_type = type; 14680Sstevel@tonic-gate tcp->spd_typecode_type_end = type_end; 14690Sstevel@tonic-gate tcp->spd_typecode_code_end = code_end; 14700Sstevel@tonic-gate } 14710Sstevel@tonic-gate offset += sizeof (*tcp); 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14740Sstevel@tonic-gate 14750Sstevel@tonic-gate return (offset); 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate static uint_t 14790Sstevel@tonic-gate spdsock_encode_proto(uint8_t *base, uint_t offset, uint8_t proto) 14800Sstevel@tonic-gate { 14810Sstevel@tonic-gate struct spd_proto *spp; 14820Sstevel@tonic-gate 14830Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14840Sstevel@tonic-gate 14850Sstevel@tonic-gate if (base != NULL) { 14860Sstevel@tonic-gate spp = (struct spd_proto *)(base + offset); 14870Sstevel@tonic-gate spp->spd_proto_len = SPD_8TO64(sizeof (*spp)); 14880Sstevel@tonic-gate spp->spd_proto_exttype = SPD_EXT_PROTO; 14890Sstevel@tonic-gate spp->spd_proto_number = proto; 14900Sstevel@tonic-gate spp->spd_proto_reserved1 = 0; 14910Sstevel@tonic-gate spp->spd_proto_reserved2 = 0; 14920Sstevel@tonic-gate } 14930Sstevel@tonic-gate offset += sizeof (*spp); 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 static uint_t 15010Sstevel@tonic-gate spdsock_encode_port(uint8_t *base, uint_t offset, uint16_t ext, uint16_t port) 15020Sstevel@tonic-gate { 15030Sstevel@tonic-gate struct spd_portrange *spp; 15040Sstevel@tonic-gate 15050Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15060Sstevel@tonic-gate 15070Sstevel@tonic-gate if (base != NULL) { 15080Sstevel@tonic-gate spp = (struct spd_portrange *)(base + offset); 15090Sstevel@tonic-gate spp->spd_ports_len = SPD_8TO64(sizeof (*spp)); 15100Sstevel@tonic-gate spp->spd_ports_exttype = ext; 15110Sstevel@tonic-gate spp->spd_ports_minport = port; 15120Sstevel@tonic-gate spp->spd_ports_maxport = port; 15130Sstevel@tonic-gate } 15140Sstevel@tonic-gate offset += sizeof (*spp); 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate return (offset); 15190Sstevel@tonic-gate } 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate static uint_t 15220Sstevel@tonic-gate spdsock_encode_addr(uint8_t *base, uint_t offset, uint16_t ext, 15230Sstevel@tonic-gate const ipsec_selkey_t *sel, const ipsec_addr_t *addr, uint_t pfxlen) 15240Sstevel@tonic-gate { 15250Sstevel@tonic-gate struct spd_address *sae; 15260Sstevel@tonic-gate ipsec_addr_t *spdaddr; 15270Sstevel@tonic-gate uint_t start = offset; 15280Sstevel@tonic-gate uint_t addrlen; 15290Sstevel@tonic-gate uint_t af; 15300Sstevel@tonic-gate 15310Sstevel@tonic-gate if (sel->ipsl_valid & IPSL_IPV4) { 15320Sstevel@tonic-gate af = AF_INET; 15330Sstevel@tonic-gate addrlen = IP_ADDR_LEN; 15340Sstevel@tonic-gate } else { 15350Sstevel@tonic-gate af = AF_INET6; 15360Sstevel@tonic-gate addrlen = IPV6_ADDR_LEN; 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15400Sstevel@tonic-gate 15410Sstevel@tonic-gate if (base != NULL) { 15420Sstevel@tonic-gate sae = (struct spd_address *)(base + offset); 15430Sstevel@tonic-gate sae->spd_address_exttype = ext; 15440Sstevel@tonic-gate sae->spd_address_af = af; 15450Sstevel@tonic-gate sae->spd_address_prefixlen = pfxlen; 15460Sstevel@tonic-gate sae->spd_address_reserved2 = 0; 15470Sstevel@tonic-gate 15480Sstevel@tonic-gate spdaddr = (ipsec_addr_t *)(&sae[1]); 15490Sstevel@tonic-gate bcopy(addr, spdaddr, addrlen); 15500Sstevel@tonic-gate } 15510Sstevel@tonic-gate offset += sizeof (*sae); 15520Sstevel@tonic-gate addrlen = roundup(addrlen, sizeof (uint64_t)); 15530Sstevel@tonic-gate offset += addrlen; 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate if (base != NULL) 15580Sstevel@tonic-gate sae->spd_address_len = SPD_8TO64(offset - start); 15590Sstevel@tonic-gate return (offset); 15600Sstevel@tonic-gate } 15610Sstevel@tonic-gate 15620Sstevel@tonic-gate static uint_t 15630Sstevel@tonic-gate spdsock_encode_sel(uint8_t *base, uint_t offset, const ipsec_sel_t *sel) 15640Sstevel@tonic-gate { 15650Sstevel@tonic-gate const ipsec_selkey_t *selkey = &sel->ipsl_key; 15660Sstevel@tonic-gate 15670Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_PROTOCOL) 15680Sstevel@tonic-gate offset = spdsock_encode_proto(base, offset, selkey->ipsl_proto); 15690Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_LOCAL_PORT) 15700Sstevel@tonic-gate offset = spdsock_encode_port(base, offset, SPD_EXT_LCLPORT, 15710Sstevel@tonic-gate selkey->ipsl_lport); 15720Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_REMOTE_PORT) 15730Sstevel@tonic-gate offset = spdsock_encode_port(base, offset, SPD_EXT_REMPORT, 15740Sstevel@tonic-gate selkey->ipsl_rport); 15750Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_REMOTE_ADDR) 15760Sstevel@tonic-gate offset = spdsock_encode_addr(base, offset, SPD_EXT_REMADDR, 15770Sstevel@tonic-gate selkey, &selkey->ipsl_remote, selkey->ipsl_remote_pfxlen); 15780Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_LOCAL_ADDR) 15790Sstevel@tonic-gate offset = spdsock_encode_addr(base, offset, SPD_EXT_LCLADDR, 15800Sstevel@tonic-gate selkey, &selkey->ipsl_local, selkey->ipsl_local_pfxlen); 15810Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_ICMP_TYPE) { 15820Sstevel@tonic-gate offset = spdsock_encode_typecode(base, offset, 15830Sstevel@tonic-gate selkey->ipsl_icmp_type, selkey->ipsl_icmp_type_end, 15840Sstevel@tonic-gate (selkey->ipsl_valid & IPSL_ICMP_CODE) ? 15850Sstevel@tonic-gate selkey->ipsl_icmp_code : 255, 15860Sstevel@tonic-gate (selkey->ipsl_valid & IPSL_ICMP_CODE) ? 15870Sstevel@tonic-gate selkey->ipsl_icmp_code_end : 255); 15880Sstevel@tonic-gate } 15890Sstevel@tonic-gate return (offset); 15900Sstevel@tonic-gate } 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate static uint_t 15930Sstevel@tonic-gate spdsock_encode_actattr(uint8_t *base, uint_t offset, uint32_t tag, 15940Sstevel@tonic-gate uint32_t value) 15950Sstevel@tonic-gate { 15960Sstevel@tonic-gate struct spd_attribute *attr; 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate if (base != NULL) { 16010Sstevel@tonic-gate attr = (struct spd_attribute *)(base + offset); 16020Sstevel@tonic-gate attr->spd_attr_tag = tag; 16030Sstevel@tonic-gate attr->spd_attr_value = value; 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate offset += sizeof (struct spd_attribute); 16060Sstevel@tonic-gate 16070Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 16080Sstevel@tonic-gate 16090Sstevel@tonic-gate return (offset); 16100Sstevel@tonic-gate } 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate 16130Sstevel@tonic-gate #define EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v)) 16140Sstevel@tonic-gate 16150Sstevel@tonic-gate static uint_t 16160Sstevel@tonic-gate spdsock_encode_action(uint8_t *base, uint_t offset, const ipsec_action_t *ap) 16170Sstevel@tonic-gate { 16180Sstevel@tonic-gate const struct ipsec_act *act = &(ap->ipa_act); 16190Sstevel@tonic-gate uint_t flags; 16200Sstevel@tonic-gate 16210Sstevel@tonic-gate EMIT(SPD_ATTR_EMPTY, 0); 16220Sstevel@tonic-gate switch (act->ipa_type) { 16230Sstevel@tonic-gate case IPSEC_ACT_DISCARD: 16240Sstevel@tonic-gate case IPSEC_ACT_REJECT: 16250Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_DROP); 16260Sstevel@tonic-gate break; 16270Sstevel@tonic-gate case IPSEC_ACT_BYPASS: 16280Sstevel@tonic-gate case IPSEC_ACT_CLEAR: 16290Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_PASS); 16300Sstevel@tonic-gate break; 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate case IPSEC_ACT_APPLY: 16330Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_IPSEC); 16340Sstevel@tonic-gate flags = 0; 16350Sstevel@tonic-gate if (act->ipa_apply.ipp_use_ah) 16360Sstevel@tonic-gate flags |= SPD_APPLY_AH; 16370Sstevel@tonic-gate if (act->ipa_apply.ipp_use_esp) 16380Sstevel@tonic-gate flags |= SPD_APPLY_ESP; 16390Sstevel@tonic-gate if (act->ipa_apply.ipp_use_espa) 16400Sstevel@tonic-gate flags |= SPD_APPLY_ESPA; 16410Sstevel@tonic-gate if (act->ipa_apply.ipp_use_se) 16420Sstevel@tonic-gate flags |= SPD_APPLY_SE; 16430Sstevel@tonic-gate if (act->ipa_apply.ipp_use_unique) 16440Sstevel@tonic-gate flags |= SPD_APPLY_UNIQUE; 16450Sstevel@tonic-gate EMIT(SPD_ATTR_FLAGS, flags); 16460Sstevel@tonic-gate if (flags & SPD_APPLY_AH) { 16470Sstevel@tonic-gate EMIT(SPD_ATTR_AH_AUTH, act->ipa_apply.ipp_auth_alg); 16480Sstevel@tonic-gate EMIT(SPD_ATTR_AH_MINBITS, 16490Sstevel@tonic-gate act->ipa_apply.ipp_ah_minbits); 16500Sstevel@tonic-gate EMIT(SPD_ATTR_AH_MAXBITS, 16510Sstevel@tonic-gate act->ipa_apply.ipp_ah_maxbits); 16520Sstevel@tonic-gate } 16530Sstevel@tonic-gate if (flags & SPD_APPLY_ESP) { 16540Sstevel@tonic-gate EMIT(SPD_ATTR_ESP_ENCR, act->ipa_apply.ipp_encr_alg); 16550Sstevel@tonic-gate EMIT(SPD_ATTR_ENCR_MINBITS, 16560Sstevel@tonic-gate act->ipa_apply.ipp_espe_minbits); 16570Sstevel@tonic-gate EMIT(SPD_ATTR_ENCR_MAXBITS, 16580Sstevel@tonic-gate act->ipa_apply.ipp_espe_maxbits); 16590Sstevel@tonic-gate if (flags & SPD_APPLY_ESPA) { 16600Sstevel@tonic-gate EMIT(SPD_ATTR_ESP_AUTH, 16610Sstevel@tonic-gate act->ipa_apply.ipp_esp_auth_alg); 16620Sstevel@tonic-gate EMIT(SPD_ATTR_ESPA_MINBITS, 16630Sstevel@tonic-gate act->ipa_apply.ipp_espa_minbits); 16640Sstevel@tonic-gate EMIT(SPD_ATTR_ESPA_MAXBITS, 16650Sstevel@tonic-gate act->ipa_apply.ipp_espa_maxbits); 16660Sstevel@tonic-gate } 16670Sstevel@tonic-gate } 16680Sstevel@tonic-gate if (act->ipa_apply.ipp_km_proto != 0) 16690Sstevel@tonic-gate EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_proto); 16700Sstevel@tonic-gate if (act->ipa_apply.ipp_km_cookie != 0) 16710Sstevel@tonic-gate EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_cookie); 16720Sstevel@tonic-gate if (act->ipa_apply.ipp_replay_depth != 0) 16730Sstevel@tonic-gate EMIT(SPD_ATTR_REPLAY_DEPTH, 16740Sstevel@tonic-gate act->ipa_apply.ipp_replay_depth); 16750Sstevel@tonic-gate /* Add more here */ 16760Sstevel@tonic-gate break; 16770Sstevel@tonic-gate } 16780Sstevel@tonic-gate 16790Sstevel@tonic-gate return (offset); 16800Sstevel@tonic-gate } 16810Sstevel@tonic-gate 16820Sstevel@tonic-gate static uint_t 16830Sstevel@tonic-gate spdsock_encode_action_list(uint8_t *base, uint_t offset, 16840Sstevel@tonic-gate const ipsec_action_t *ap) 16850Sstevel@tonic-gate { 16860Sstevel@tonic-gate struct spd_ext_actions *act; 16870Sstevel@tonic-gate uint_t nact = 0; 16880Sstevel@tonic-gate uint_t start = offset; 16890Sstevel@tonic-gate 16900Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate if (base != NULL) { 16930Sstevel@tonic-gate act = (struct spd_ext_actions *)(base + offset); 16940Sstevel@tonic-gate act->spd_actions_len = 0; 16950Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 16960Sstevel@tonic-gate act->spd_actions_count = 0; 16970Sstevel@tonic-gate act->spd_actions_reserved = 0; 16980Sstevel@tonic-gate } 16990Sstevel@tonic-gate 17000Sstevel@tonic-gate offset += sizeof (*act); 17010Sstevel@tonic-gate 17020Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17030Sstevel@tonic-gate 17040Sstevel@tonic-gate while (ap != NULL) { 17050Sstevel@tonic-gate offset = spdsock_encode_action(base, offset, ap); 17060Sstevel@tonic-gate ap = ap->ipa_next; 17070Sstevel@tonic-gate nact++; 17080Sstevel@tonic-gate if (ap != NULL) { 17090Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate } 17120Sstevel@tonic-gate EMIT(SPD_ATTR_END, 0); 17130Sstevel@tonic-gate 17140Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17150Sstevel@tonic-gate 17160Sstevel@tonic-gate if (base != NULL) { 17170Sstevel@tonic-gate act->spd_actions_count = nact; 17180Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(offset - start); 17190Sstevel@tonic-gate } 17200Sstevel@tonic-gate 17210Sstevel@tonic-gate return (offset); 17220Sstevel@tonic-gate } 17230Sstevel@tonic-gate 17240Sstevel@tonic-gate #undef EMIT 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate /* ARGSUSED */ 17270Sstevel@tonic-gate static uint_t 17280Sstevel@tonic-gate spdsock_rule_flags(uint_t dir, uint_t af) 17290Sstevel@tonic-gate { 17300Sstevel@tonic-gate uint_t flags = 0; 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate if (dir == IPSEC_TYPE_INBOUND) 17330Sstevel@tonic-gate flags |= SPD_RULE_FLAG_INBOUND; 17340Sstevel@tonic-gate if (dir == IPSEC_TYPE_OUTBOUND) 17350Sstevel@tonic-gate flags |= SPD_RULE_FLAG_OUTBOUND; 17360Sstevel@tonic-gate 17370Sstevel@tonic-gate return (flags); 17380Sstevel@tonic-gate } 17390Sstevel@tonic-gate 17400Sstevel@tonic-gate 17410Sstevel@tonic-gate static uint_t 17423055Sdanmcd spdsock_encode_rule_head(uint8_t *base, uint_t offset, spd_msg_t *req, 17433055Sdanmcd const ipsec_policy_t *rule, uint_t dir, uint_t af, char *name, 17443055Sdanmcd boolean_t tunnel) 17450Sstevel@tonic-gate { 17460Sstevel@tonic-gate struct spd_msg *spmsg; 17470Sstevel@tonic-gate struct spd_rule *spr; 17483055Sdanmcd spd_if_t *sid; 17490Sstevel@tonic-gate 17500Sstevel@tonic-gate uint_t start = offset; 17510Sstevel@tonic-gate 17520Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17530Sstevel@tonic-gate 17540Sstevel@tonic-gate if (base != NULL) { 17550Sstevel@tonic-gate spmsg = (struct spd_msg *)(base + offset); 17560Sstevel@tonic-gate bzero(spmsg, sizeof (*spmsg)); 17570Sstevel@tonic-gate spmsg->spd_msg_version = PF_POLICY_V1; 17580Sstevel@tonic-gate spmsg->spd_msg_type = SPD_DUMP; 17590Sstevel@tonic-gate spmsg->spd_msg_seq = req->spd_msg_seq; 17600Sstevel@tonic-gate spmsg->spd_msg_pid = req->spd_msg_pid; 17610Sstevel@tonic-gate } 17620Sstevel@tonic-gate offset += sizeof (struct spd_msg); 17630Sstevel@tonic-gate 17640Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17650Sstevel@tonic-gate 17660Sstevel@tonic-gate if (base != NULL) { 17670Sstevel@tonic-gate spr = (struct spd_rule *)(base + offset); 17680Sstevel@tonic-gate spr->spd_rule_type = SPD_EXT_RULE; 17690Sstevel@tonic-gate spr->spd_rule_priority = rule->ipsp_prio; 17700Sstevel@tonic-gate spr->spd_rule_flags = spdsock_rule_flags(dir, af); 17713055Sdanmcd if (tunnel) 17723055Sdanmcd spr->spd_rule_flags |= SPD_RULE_FLAG_TUNNEL; 17730Sstevel@tonic-gate spr->spd_rule_unused = 0; 17740Sstevel@tonic-gate spr->spd_rule_len = SPD_8TO64(sizeof (*spr)); 17750Sstevel@tonic-gate spr->spd_rule_index = rule->ipsp_index; 17760Sstevel@tonic-gate } 17770Sstevel@tonic-gate offset += sizeof (struct spd_rule); 17783055Sdanmcd 17793055Sdanmcd /* 17803055Sdanmcd * If we have an interface name (i.e. if this policy head came from 17813055Sdanmcd * a tunnel), add the SPD_EXT_TUN_NAME extension. 17823055Sdanmcd */ 17833055Sdanmcd if (name[0] != '\0') { 17843055Sdanmcd 17853055Sdanmcd ASSERT(ALIGNED64(offset)); 17863055Sdanmcd 17873055Sdanmcd if (base != NULL) { 17883055Sdanmcd sid = (spd_if_t *)(base + offset); 17893055Sdanmcd sid->spd_if_exttype = SPD_EXT_TUN_NAME; 17903055Sdanmcd sid->spd_if_len = SPD_8TO64(sizeof (spd_if_t) + 17913055Sdanmcd roundup((strlen(name) - 4), 8)); 17923055Sdanmcd (void) strlcpy((char *)sid->spd_if_name, name, 17933055Sdanmcd LIFNAMSIZ); 17943055Sdanmcd } 17953055Sdanmcd 17963055Sdanmcd offset += sizeof (spd_if_t) + roundup((strlen(name) - 4), 8); 17973055Sdanmcd } 17983055Sdanmcd 17990Sstevel@tonic-gate offset = spdsock_encode_sel(base, offset, rule->ipsp_sel); 18000Sstevel@tonic-gate offset = spdsock_encode_action_list(base, offset, rule->ipsp_act); 18010Sstevel@tonic-gate 18020Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 18030Sstevel@tonic-gate 18040Sstevel@tonic-gate if (base != NULL) { 18050Sstevel@tonic-gate spmsg->spd_msg_len = SPD_8TO64(offset - start); 18060Sstevel@tonic-gate } 18070Sstevel@tonic-gate return (offset); 18080Sstevel@tonic-gate } 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate /* ARGSUSED */ 18110Sstevel@tonic-gate static mblk_t * 18120Sstevel@tonic-gate spdsock_encode_rule(mblk_t *req, const ipsec_policy_t *rule, 18133055Sdanmcd uint_t dir, uint_t af, char *name, boolean_t tunnel) 18140Sstevel@tonic-gate { 18150Sstevel@tonic-gate mblk_t *m; 18160Sstevel@tonic-gate uint_t len; 18170Sstevel@tonic-gate spd_msg_t *mreq = (spd_msg_t *)req->b_rptr; 18180Sstevel@tonic-gate 18190Sstevel@tonic-gate /* 18200Sstevel@tonic-gate * Figure out how much space we'll need. 18210Sstevel@tonic-gate */ 18223055Sdanmcd len = spdsock_encode_rule_head(NULL, 0, mreq, rule, dir, af, name, 18233055Sdanmcd tunnel); 18240Sstevel@tonic-gate 18250Sstevel@tonic-gate /* 18260Sstevel@tonic-gate * Allocate mblk. 18270Sstevel@tonic-gate */ 18280Sstevel@tonic-gate m = allocb(len, BPRI_HI); 18290Sstevel@tonic-gate if (m == NULL) 18300Sstevel@tonic-gate return (NULL); 18310Sstevel@tonic-gate 18320Sstevel@tonic-gate /* 18330Sstevel@tonic-gate * Fill it in.. 18340Sstevel@tonic-gate */ 18350Sstevel@tonic-gate m->b_wptr = m->b_rptr + len; 18360Sstevel@tonic-gate bzero(m->b_rptr, len); 18373055Sdanmcd (void) spdsock_encode_rule_head(m->b_rptr, 0, mreq, rule, dir, af, 18383055Sdanmcd name, tunnel); 18390Sstevel@tonic-gate return (m); 18400Sstevel@tonic-gate } 18410Sstevel@tonic-gate 18420Sstevel@tonic-gate static ipsec_policy_t * 184381Ssommerfe spdsock_dump_next_in_chain(spdsock_t *ss, ipsec_policy_head_t *iph, 184481Ssommerfe ipsec_policy_t *cur) 184581Ssommerfe { 184681Ssommerfe ASSERT(RW_READ_HELD(&iph->iph_lock)); 184781Ssommerfe 184881Ssommerfe ss->spdsock_dump_count++; 184981Ssommerfe ss->spdsock_dump_cur_rule = cur->ipsp_hash.hash_next; 185081Ssommerfe return (cur); 185181Ssommerfe } 185281Ssommerfe 185381Ssommerfe static ipsec_policy_t * 18540Sstevel@tonic-gate spdsock_dump_next_rule(spdsock_t *ss, ipsec_policy_head_t *iph) 18550Sstevel@tonic-gate { 18560Sstevel@tonic-gate ipsec_policy_t *cur; 185781Ssommerfe ipsec_policy_root_t *ipr; 185881Ssommerfe int chain, nchains, type, af; 18590Sstevel@tonic-gate 18600Sstevel@tonic-gate ASSERT(RW_READ_HELD(&iph->iph_lock)); 18610Sstevel@tonic-gate 18620Sstevel@tonic-gate cur = ss->spdsock_dump_cur_rule; 18630Sstevel@tonic-gate 186481Ssommerfe if (cur != NULL) 186581Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 186681Ssommerfe 186781Ssommerfe type = ss->spdsock_dump_cur_type; 186881Ssommerfe 186981Ssommerfe next: 187081Ssommerfe chain = ss->spdsock_dump_cur_chain; 187181Ssommerfe ipr = &iph->iph_root[type]; 187281Ssommerfe nchains = ipr->ipr_nchains; 187381Ssommerfe 187481Ssommerfe while (chain < nchains) { 187581Ssommerfe cur = ipr->ipr_hash[chain].hash_head; 187681Ssommerfe chain++; 187781Ssommerfe if (cur != NULL) { 187881Ssommerfe ss->spdsock_dump_cur_chain = chain; 187981Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 188081Ssommerfe } 18810Sstevel@tonic-gate } 188281Ssommerfe ss->spdsock_dump_cur_chain = nchains; 188381Ssommerfe 188481Ssommerfe af = ss->spdsock_dump_cur_af; 188581Ssommerfe while (af < IPSEC_NAF) { 188681Ssommerfe cur = ipr->ipr_nonhash[af]; 188781Ssommerfe af++; 188881Ssommerfe if (cur != NULL) { 188981Ssommerfe ss->spdsock_dump_cur_af = af; 189081Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 189181Ssommerfe } 189281Ssommerfe } 189381Ssommerfe 189481Ssommerfe type++; 189581Ssommerfe if (type >= IPSEC_NTYPES) 189681Ssommerfe return (NULL); 189781Ssommerfe 189881Ssommerfe ss->spdsock_dump_cur_chain = 0; 189981Ssommerfe ss->spdsock_dump_cur_type = type; 190081Ssommerfe ss->spdsock_dump_cur_af = IPSEC_AF_V4; 190181Ssommerfe goto next; 190281Ssommerfe 19030Sstevel@tonic-gate } 19040Sstevel@tonic-gate 19050Sstevel@tonic-gate static mblk_t * 19060Sstevel@tonic-gate spdsock_dump_next_record(spdsock_t *ss) 19070Sstevel@tonic-gate { 19080Sstevel@tonic-gate ipsec_policy_head_t *iph; 19090Sstevel@tonic-gate ipsec_policy_t *rule; 19100Sstevel@tonic-gate mblk_t *m; 19110Sstevel@tonic-gate mblk_t *req = ss->spdsock_dump_req; 19120Sstevel@tonic-gate 19130Sstevel@tonic-gate iph = ss->spdsock_dump_head; 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate ASSERT(iph != NULL); 19160Sstevel@tonic-gate 19170Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 19180Sstevel@tonic-gate 19190Sstevel@tonic-gate if (iph->iph_gen != ss->spdsock_dump_gen) { 19200Sstevel@tonic-gate rw_exit(&iph->iph_lock); 19210Sstevel@tonic-gate return (spdsock_dump_finish(ss, EAGAIN)); 19220Sstevel@tonic-gate } 19230Sstevel@tonic-gate 19240Sstevel@tonic-gate rule = spdsock_dump_next_rule(ss, iph); 19250Sstevel@tonic-gate 19260Sstevel@tonic-gate if (!rule) { 19270Sstevel@tonic-gate rw_exit(&iph->iph_lock); 19280Sstevel@tonic-gate return (spdsock_dump_finish(ss, 0)); 19290Sstevel@tonic-gate } 19300Sstevel@tonic-gate 19310Sstevel@tonic-gate m = spdsock_encode_rule(req, rule, ss->spdsock_dump_cur_type, 19323055Sdanmcd ss->spdsock_dump_cur_af, ss->spdsock_dump_name, 19333055Sdanmcd ss->spdsock_dump_tunnel); 19340Sstevel@tonic-gate rw_exit(&iph->iph_lock); 19350Sstevel@tonic-gate 19360Sstevel@tonic-gate if (m == NULL) 19370Sstevel@tonic-gate return (spdsock_dump_finish(ss, ENOMEM)); 19380Sstevel@tonic-gate return (m); 19390Sstevel@tonic-gate } 19400Sstevel@tonic-gate 19410Sstevel@tonic-gate /* 19420Sstevel@tonic-gate * Dump records until we run into flow-control back-pressure. 19430Sstevel@tonic-gate */ 19440Sstevel@tonic-gate static void 19450Sstevel@tonic-gate spdsock_dump_some(queue_t *q, spdsock_t *ss) 19460Sstevel@tonic-gate { 19470Sstevel@tonic-gate mblk_t *m, *dataind; 19480Sstevel@tonic-gate 19490Sstevel@tonic-gate while ((ss->spdsock_dump_req != NULL) && canputnext(q)) { 19500Sstevel@tonic-gate m = spdsock_dump_next_record(ss); 19510Sstevel@tonic-gate if (m == NULL) 19520Sstevel@tonic-gate return; 19530Sstevel@tonic-gate dataind = allocb(sizeof (struct T_data_req), BPRI_HI); 19540Sstevel@tonic-gate if (dataind == NULL) { 19550Sstevel@tonic-gate freemsg(m); 19560Sstevel@tonic-gate return; 19570Sstevel@tonic-gate } 19580Sstevel@tonic-gate dataind->b_cont = m; 19590Sstevel@tonic-gate dataind->b_wptr += sizeof (struct T_data_req); 19600Sstevel@tonic-gate ((struct T_data_ind *)dataind->b_rptr)->PRIM_type = T_DATA_IND; 19610Sstevel@tonic-gate ((struct T_data_ind *)dataind->b_rptr)->MORE_flag = 0; 19620Sstevel@tonic-gate dataind->b_datap->db_type = M_PROTO; 19630Sstevel@tonic-gate putnext(q, dataind); 19640Sstevel@tonic-gate } 19650Sstevel@tonic-gate } 19660Sstevel@tonic-gate 19670Sstevel@tonic-gate /* 19680Sstevel@tonic-gate * Start dumping. 19690Sstevel@tonic-gate * Format a start-of-dump record, and set up the stream and kick the rsrv 19700Sstevel@tonic-gate * procedure to continue the job.. 19710Sstevel@tonic-gate */ 19720Sstevel@tonic-gate /* ARGSUSED */ 19730Sstevel@tonic-gate static void 19743055Sdanmcd spdsock_dump(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp) 19750Sstevel@tonic-gate { 19760Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 19773448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 19783448Sdh155122 netstack_t *ns = spds->spds_netstack; 19793448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 19800Sstevel@tonic-gate mblk_t *mr; 19810Sstevel@tonic-gate 19823055Sdanmcd /* spdsock_parse() already NULL-terminated spdsock_dump_name. */ 19833055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) { 19843448Sdh155122 rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER); 19853055Sdanmcd ss->spdsock_dump_remaining_polheads = 1 + 19863448Sdh155122 avl_numnodes(&ipss->ipsec_tunnel_policies); 19873448Sdh155122 ss->spdsock_dump_tun_gen = ipss->ipsec_tunnel_policy_gen; 19883448Sdh155122 rw_exit(&ipss->ipsec_tunnel_policy_lock); 19893055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS) { 19903448Sdh155122 iph = ipsec_system_policy(ns); 19913055Sdanmcd ss->spdsock_dump_active = B_TRUE; 19923055Sdanmcd } else { 19933448Sdh155122 iph = ipsec_inactive_policy(spds->spds_netstack); 19943055Sdanmcd ss->spdsock_dump_active = B_FALSE; 19953055Sdanmcd } 19963055Sdanmcd ASSERT(ss->spdsock_dump_name[0] == '\0'); 19973055Sdanmcd } else { 19983055Sdanmcd ss->spdsock_dump_remaining_polheads = 1; 19993055Sdanmcd } 20003055Sdanmcd 20010Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 20020Sstevel@tonic-gate 20030Sstevel@tonic-gate mr = spdsock_dump_ruleset(mp, iph, 0, 0); 20040Sstevel@tonic-gate 20050Sstevel@tonic-gate if (!mr) { 20060Sstevel@tonic-gate rw_exit(&iph->iph_lock); 20070Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 20080Sstevel@tonic-gate return; 20090Sstevel@tonic-gate } 20100Sstevel@tonic-gate 20110Sstevel@tonic-gate ss->spdsock_dump_req = mp; 20120Sstevel@tonic-gate ss->spdsock_dump_head = iph; 20130Sstevel@tonic-gate ss->spdsock_dump_gen = iph->iph_gen; 20140Sstevel@tonic-gate ss->spdsock_dump_cur_type = 0; 20150Sstevel@tonic-gate ss->spdsock_dump_cur_af = IPSEC_AF_V4; 2016660Spr131582 ss->spdsock_dump_cur_rule = NULL; 20170Sstevel@tonic-gate ss->spdsock_dump_count = 0; 201881Ssommerfe ss->spdsock_dump_cur_chain = 0; 20190Sstevel@tonic-gate rw_exit(&iph->iph_lock); 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate qreply(q, mr); 20220Sstevel@tonic-gate qenable(OTHERQ(q)); 20230Sstevel@tonic-gate } 20240Sstevel@tonic-gate 20253055Sdanmcd /* Do NOT consume a reference to ITP. */ 20260Sstevel@tonic-gate void 20273448Sdh155122 spdsock_clone_node(ipsec_tun_pol_t *itp, void *ep, netstack_t *ns) 20283055Sdanmcd { 20293055Sdanmcd int *errptr = (int *)ep; 20303055Sdanmcd 20313055Sdanmcd if (*errptr != 0) 20323055Sdanmcd return; /* We've failed already for some reason. */ 20333055Sdanmcd mutex_enter(&itp->itp_lock); 20343055Sdanmcd ITPF_CLONE(itp->itp_flags); 20353448Sdh155122 *errptr = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns); 20363055Sdanmcd mutex_exit(&itp->itp_lock); 20373055Sdanmcd } 20383055Sdanmcd 20393055Sdanmcd void 20403055Sdanmcd spdsock_clone(queue_t *q, mblk_t *mp, spd_if_t *tunname) 20410Sstevel@tonic-gate { 20423055Sdanmcd int error; 20433055Sdanmcd char *tname; 20443055Sdanmcd ipsec_tun_pol_t *itp; 20453448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 20463448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 20473055Sdanmcd 20483055Sdanmcd if (tunname != NULL) { 20493055Sdanmcd tname = (char *)tunname->spd_if_name; 20503055Sdanmcd if (*tname == '\0') { 20513448Sdh155122 error = ipsec_clone_system_policy(spds->spds_netstack); 2052*4307Spwernau if (audit_active) { 2053*4307Spwernau boolean_t active; 2054*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 2055*4307Spwernau 2056*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 2057*4307Spwernau audit_pf_policy(SPD_CLONE, DB_CRED(mp), 2058*4307Spwernau spds->spds_netstack, NULL, active, error, 2059*4307Spwernau DB_CPID(mp)); 2060*4307Spwernau } 2061*4307Spwernau if (error == 0) { 20623448Sdh155122 itp_walk(spdsock_clone_node, &error, 20633448Sdh155122 spds->spds_netstack); 2064*4307Spwernau if (audit_active) { 2065*4307Spwernau boolean_t active; 2066*4307Spwernau spd_msg_t *spmsg = 2067*4307Spwernau (spd_msg_t *)mp->b_rptr; 2068*4307Spwernau 2069*4307Spwernau active = (spmsg->spd_msg_spdid == 2070*4307Spwernau SPD_ACTIVE); 2071*4307Spwernau audit_pf_policy(SPD_CLONE, DB_CRED(mp), 2072*4307Spwernau spds->spds_netstack, 2073*4307Spwernau "all tunnels", active, 0, 2074*4307Spwernau DB_CPID(mp)); 2075*4307Spwernau } 2076*4307Spwernau } 20773055Sdanmcd } else { 20783448Sdh155122 itp = get_tunnel_policy(tname, spds->spds_netstack); 20793055Sdanmcd if (itp == NULL) { 20803055Sdanmcd spdsock_error(q, mp, ENOENT, 0); 2081*4307Spwernau if (audit_active) { 2082*4307Spwernau boolean_t active; 2083*4307Spwernau spd_msg_t *spmsg = 2084*4307Spwernau (spd_msg_t *)mp->b_rptr; 2085*4307Spwernau 2086*4307Spwernau active = (spmsg->spd_msg_spdid == 2087*4307Spwernau SPD_ACTIVE); 2088*4307Spwernau audit_pf_policy(SPD_CLONE, DB_CRED(mp), 2089*4307Spwernau spds->spds_netstack, ITP_NAME(itp), 2090*4307Spwernau active, ENOENT, DB_CPID(mp)); 2091*4307Spwernau } 20923055Sdanmcd return; 20933055Sdanmcd } 20943448Sdh155122 spdsock_clone_node(itp, &error, NULL); 20953448Sdh155122 ITP_REFRELE(itp, spds->spds_netstack); 2096*4307Spwernau if (audit_active) { 2097*4307Spwernau boolean_t active; 2098*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 2099*4307Spwernau 2100*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 2101*4307Spwernau audit_pf_policy(SPD_CLONE, DB_CRED(mp), 2102*4307Spwernau spds->spds_netstack, ITP_NAME(itp), active, 2103*4307Spwernau error, DB_CPID(mp)); 2104*4307Spwernau } 21053055Sdanmcd } 21063055Sdanmcd } else { 21073448Sdh155122 error = ipsec_clone_system_policy(spds->spds_netstack); 2108*4307Spwernau if (audit_active) { 2109*4307Spwernau boolean_t active; 2110*4307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 2111*4307Spwernau 2112*4307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 2113*4307Spwernau audit_pf_policy(SPD_CLONE, DB_CRED(mp), 2114*4307Spwernau spds->spds_netstack, NULL, active, error, 2115*4307Spwernau DB_CPID(mp)); 2116*4307Spwernau } 21173055Sdanmcd } 21183055Sdanmcd 21190Sstevel@tonic-gate if (error != 0) 21200Sstevel@tonic-gate spdsock_error(q, mp, error, 0); 21210Sstevel@tonic-gate else 21220Sstevel@tonic-gate spd_echo(q, mp); 21230Sstevel@tonic-gate } 21240Sstevel@tonic-gate 21250Sstevel@tonic-gate /* 21260Sstevel@tonic-gate * Process a SPD_ALGLIST request. The caller expects separate alg entries 21270Sstevel@tonic-gate * for AH authentication, ESP authentication, and ESP encryption. 21280Sstevel@tonic-gate * The same distinction is then used when setting the min and max key 21290Sstevel@tonic-gate * sizes when defining policies. 21300Sstevel@tonic-gate */ 21310Sstevel@tonic-gate 21320Sstevel@tonic-gate #define SPDSOCK_AH_AUTH 0 21330Sstevel@tonic-gate #define SPDSOCK_ESP_AUTH 1 21340Sstevel@tonic-gate #define SPDSOCK_ESP_ENCR 2 21350Sstevel@tonic-gate #define SPDSOCK_NTYPES 3 21360Sstevel@tonic-gate 21370Sstevel@tonic-gate static const uint_t algattr[SPDSOCK_NTYPES] = { 21380Sstevel@tonic-gate SPD_ATTR_AH_AUTH, 21390Sstevel@tonic-gate SPD_ATTR_ESP_AUTH, 21400Sstevel@tonic-gate SPD_ATTR_ESP_ENCR 21410Sstevel@tonic-gate }; 21420Sstevel@tonic-gate static const uint_t minbitsattr[SPDSOCK_NTYPES] = { 21430Sstevel@tonic-gate SPD_ATTR_AH_MINBITS, 21440Sstevel@tonic-gate SPD_ATTR_ESPA_MINBITS, 21450Sstevel@tonic-gate SPD_ATTR_ENCR_MINBITS 21460Sstevel@tonic-gate }; 21470Sstevel@tonic-gate static const uint_t maxbitsattr[SPDSOCK_NTYPES] = { 21480Sstevel@tonic-gate SPD_ATTR_AH_MAXBITS, 21490Sstevel@tonic-gate SPD_ATTR_ESPA_MAXBITS, 21500Sstevel@tonic-gate SPD_ATTR_ENCR_MAXBITS 21510Sstevel@tonic-gate }; 21520Sstevel@tonic-gate static const uint_t defbitsattr[SPDSOCK_NTYPES] = { 21530Sstevel@tonic-gate SPD_ATTR_AH_DEFBITS, 21540Sstevel@tonic-gate SPD_ATTR_ESPA_DEFBITS, 21550Sstevel@tonic-gate SPD_ATTR_ENCR_DEFBITS 21560Sstevel@tonic-gate }; 21570Sstevel@tonic-gate static const uint_t incrbitsattr[SPDSOCK_NTYPES] = { 21580Sstevel@tonic-gate SPD_ATTR_AH_INCRBITS, 21590Sstevel@tonic-gate SPD_ATTR_ESPA_INCRBITS, 21600Sstevel@tonic-gate SPD_ATTR_ENCR_INCRBITS 21610Sstevel@tonic-gate }; 21620Sstevel@tonic-gate 21630Sstevel@tonic-gate #define ATTRPERALG 6 /* fixed attributes per algs */ 21640Sstevel@tonic-gate 21650Sstevel@tonic-gate void 21660Sstevel@tonic-gate spdsock_alglist(queue_t *q, mblk_t *mp) 21670Sstevel@tonic-gate { 21680Sstevel@tonic-gate uint_t algtype; 21690Sstevel@tonic-gate uint_t algidx; 21700Sstevel@tonic-gate uint_t algcount; 21710Sstevel@tonic-gate uint_t size; 21720Sstevel@tonic-gate mblk_t *m; 21730Sstevel@tonic-gate uint8_t *cur; 21740Sstevel@tonic-gate spd_msg_t *msg; 21750Sstevel@tonic-gate struct spd_ext_actions *act; 21760Sstevel@tonic-gate struct spd_attribute *attr; 21773448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 21783448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 21793448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 21803448Sdh155122 21813448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 21820Sstevel@tonic-gate /* 21830Sstevel@tonic-gate * The SPD client expects to receive separate entries for 21840Sstevel@tonic-gate * AH authentication and ESP authentication supported algorithms. 21850Sstevel@tonic-gate * 21860Sstevel@tonic-gate * Don't return the "any" algorithms, if defined, as no 21870Sstevel@tonic-gate * kernel policies can be set for these algorithms. 21880Sstevel@tonic-gate */ 21893448Sdh155122 algcount = 2 * ipss->ipsec_nalgs[IPSEC_ALG_AUTH] + 21903448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 21913448Sdh155122 21923448Sdh155122 if (ipss->ipsec_alglists[IPSEC_ALG_AUTH][SADB_AALG_NONE] != NULL) 21930Sstevel@tonic-gate algcount--; 21943448Sdh155122 if (ipss->ipsec_alglists[IPSEC_ALG_ENCR][SADB_EALG_NONE] != NULL) 21950Sstevel@tonic-gate algcount--; 21960Sstevel@tonic-gate 21970Sstevel@tonic-gate /* 21980Sstevel@tonic-gate * For each algorithm, we encode: 21990Sstevel@tonic-gate * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT} 22000Sstevel@tonic-gate */ 22010Sstevel@tonic-gate 22020Sstevel@tonic-gate size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions) + 22030Sstevel@tonic-gate ATTRPERALG * sizeof (struct spd_attribute) * algcount; 22040Sstevel@tonic-gate 22050Sstevel@tonic-gate ASSERT(ALIGNED64(size)); 22060Sstevel@tonic-gate 22070Sstevel@tonic-gate m = allocb(size, BPRI_HI); 22080Sstevel@tonic-gate if (m == NULL) { 22093448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 22100Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 22110Sstevel@tonic-gate return; 22120Sstevel@tonic-gate } 22130Sstevel@tonic-gate 22140Sstevel@tonic-gate m->b_wptr = m->b_rptr + size; 22150Sstevel@tonic-gate cur = m->b_rptr; 22160Sstevel@tonic-gate 22170Sstevel@tonic-gate msg = (spd_msg_t *)cur; 22180Sstevel@tonic-gate bcopy(mp->b_rptr, cur, sizeof (*msg)); 22190Sstevel@tonic-gate 22200Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(size); 22210Sstevel@tonic-gate msg->spd_msg_errno = 0; 22220Sstevel@tonic-gate msg->spd_msg_diagnostic = 0; 22230Sstevel@tonic-gate 22240Sstevel@tonic-gate cur += sizeof (*msg); 22250Sstevel@tonic-gate 22260Sstevel@tonic-gate act = (struct spd_ext_actions *)cur; 22270Sstevel@tonic-gate cur += sizeof (*act); 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t)); 22300Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 22310Sstevel@tonic-gate act->spd_actions_count = algcount; 22320Sstevel@tonic-gate act->spd_actions_reserved = 0; 22330Sstevel@tonic-gate 22340Sstevel@tonic-gate attr = (struct spd_attribute *)cur; 22350Sstevel@tonic-gate 22360Sstevel@tonic-gate #define EMIT(tag, value) { \ 22370Sstevel@tonic-gate attr->spd_attr_tag = (tag); \ 22380Sstevel@tonic-gate attr->spd_attr_value = (value); \ 22390Sstevel@tonic-gate attr++; \ 22400Sstevel@tonic-gate } 22410Sstevel@tonic-gate 22420Sstevel@tonic-gate /* 22430Sstevel@tonic-gate * If you change the number of EMIT's here, change 22440Sstevel@tonic-gate * ATTRPERALG above to match 22450Sstevel@tonic-gate */ 22460Sstevel@tonic-gate #define EMITALGATTRS(_type) { \ 22470Sstevel@tonic-gate EMIT(algattr[_type], algid); /* 1 */ \ 22480Sstevel@tonic-gate EMIT(minbitsattr[_type], minbits); /* 2 */ \ 22490Sstevel@tonic-gate EMIT(maxbitsattr[_type], maxbits); /* 3 */ \ 22500Sstevel@tonic-gate EMIT(defbitsattr[_type], defbits); /* 4 */ \ 22510Sstevel@tonic-gate EMIT(incrbitsattr[_type], incr); /* 5 */ \ 22520Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); /* 6 */ \ 22530Sstevel@tonic-gate } 22540Sstevel@tonic-gate 22550Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 22563448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 22573448Sdh155122 algidx++) { 22583448Sdh155122 int algid = ipss->ipsec_sortlist[algtype][algidx]; 22593448Sdh155122 ipsec_alginfo_t *alg = 22603448Sdh155122 ipss->ipsec_alglists[algtype][algid]; 22610Sstevel@tonic-gate uint_t minbits = alg->alg_minbits; 22620Sstevel@tonic-gate uint_t maxbits = alg->alg_maxbits; 22630Sstevel@tonic-gate uint_t defbits = alg->alg_default_bits; 22640Sstevel@tonic-gate uint_t incr = alg->alg_increment; 22650Sstevel@tonic-gate 22660Sstevel@tonic-gate if (algtype == IPSEC_ALG_AUTH) { 22670Sstevel@tonic-gate if (algid == SADB_AALG_NONE) 22680Sstevel@tonic-gate continue; 22690Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_AH_AUTH); 22700Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_ESP_AUTH); 22710Sstevel@tonic-gate } else { 22720Sstevel@tonic-gate if (algid == SADB_EALG_NONE) 22730Sstevel@tonic-gate continue; 22740Sstevel@tonic-gate ASSERT(algtype == IPSEC_ALG_ENCR); 22750Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_ESP_ENCR); 22760Sstevel@tonic-gate } 22770Sstevel@tonic-gate } 22780Sstevel@tonic-gate } 22790Sstevel@tonic-gate 22803448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 22810Sstevel@tonic-gate 22820Sstevel@tonic-gate #undef EMITALGATTRS 22830Sstevel@tonic-gate #undef EMIT 22840Sstevel@tonic-gate #undef ATTRPERALG 22850Sstevel@tonic-gate 22860Sstevel@tonic-gate attr--; 22870Sstevel@tonic-gate attr->spd_attr_tag = SPD_ATTR_END; 22880Sstevel@tonic-gate 22890Sstevel@tonic-gate freemsg(mp); 22900Sstevel@tonic-gate qreply(q, m); 22910Sstevel@tonic-gate } 22920Sstevel@tonic-gate 22930Sstevel@tonic-gate /* 22940Sstevel@tonic-gate * Process a SPD_DUMPALGS request. 22950Sstevel@tonic-gate */ 22960Sstevel@tonic-gate 22970Sstevel@tonic-gate #define ATTRPERALG 7 /* fixed attributes per algs */ 22980Sstevel@tonic-gate 22990Sstevel@tonic-gate void 23000Sstevel@tonic-gate spdsock_dumpalgs(queue_t *q, mblk_t *mp) 23010Sstevel@tonic-gate { 23020Sstevel@tonic-gate uint_t algtype; 23030Sstevel@tonic-gate uint_t algidx; 23040Sstevel@tonic-gate uint_t size; 23050Sstevel@tonic-gate mblk_t *m; 23060Sstevel@tonic-gate uint8_t *cur; 23070Sstevel@tonic-gate spd_msg_t *msg; 23080Sstevel@tonic-gate struct spd_ext_actions *act; 23090Sstevel@tonic-gate struct spd_attribute *attr; 23100Sstevel@tonic-gate ipsec_alginfo_t *alg; 23110Sstevel@tonic-gate uint_t algid; 23120Sstevel@tonic-gate uint_t i; 23130Sstevel@tonic-gate uint_t alg_size; 23143448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 23153448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 23163448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 23173448Sdh155122 23183448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 23190Sstevel@tonic-gate 23200Sstevel@tonic-gate /* 23210Sstevel@tonic-gate * For each algorithm, we encode: 23220Sstevel@tonic-gate * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT} 23230Sstevel@tonic-gate * 23240Sstevel@tonic-gate * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE* 23250Sstevel@tonic-gate * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_MECHNAME / {END, NEXT} 23260Sstevel@tonic-gate */ 23270Sstevel@tonic-gate 23280Sstevel@tonic-gate /* 23290Sstevel@tonic-gate * Compute the size of the SPD message. 23300Sstevel@tonic-gate */ 23310Sstevel@tonic-gate size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions); 23320Sstevel@tonic-gate 23330Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 23343448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 23353448Sdh155122 algidx++) { 23363448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 23373448Sdh155122 alg = ipss->ipsec_alglists[algtype][algid]; 23380Sstevel@tonic-gate alg_size = sizeof (struct spd_attribute) * 23390Sstevel@tonic-gate (ATTRPERALG + alg->alg_nkey_sizes + 23400Sstevel@tonic-gate alg->alg_nblock_sizes) + CRYPTO_MAX_MECH_NAME; 23410Sstevel@tonic-gate size += alg_size; 23420Sstevel@tonic-gate } 23430Sstevel@tonic-gate } 23440Sstevel@tonic-gate 23450Sstevel@tonic-gate ASSERT(ALIGNED64(size)); 23460Sstevel@tonic-gate 23470Sstevel@tonic-gate m = allocb(size, BPRI_HI); 23480Sstevel@tonic-gate if (m == NULL) { 23493448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 23500Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 23510Sstevel@tonic-gate return; 23520Sstevel@tonic-gate } 23530Sstevel@tonic-gate 23540Sstevel@tonic-gate m->b_wptr = m->b_rptr + size; 23550Sstevel@tonic-gate cur = m->b_rptr; 23560Sstevel@tonic-gate 23570Sstevel@tonic-gate msg = (spd_msg_t *)cur; 23580Sstevel@tonic-gate bcopy(mp->b_rptr, cur, sizeof (*msg)); 23590Sstevel@tonic-gate 23600Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(size); 23610Sstevel@tonic-gate msg->spd_msg_errno = 0; 23620Sstevel@tonic-gate msg->spd_msg_diagnostic = 0; 23630Sstevel@tonic-gate 23640Sstevel@tonic-gate cur += sizeof (*msg); 23650Sstevel@tonic-gate 23660Sstevel@tonic-gate act = (struct spd_ext_actions *)cur; 23670Sstevel@tonic-gate cur += sizeof (*act); 23680Sstevel@tonic-gate 23690Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t)); 23700Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 23713448Sdh155122 act->spd_actions_count = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] + 23723448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 23730Sstevel@tonic-gate act->spd_actions_reserved = 0; 23740Sstevel@tonic-gate 23750Sstevel@tonic-gate attr = (struct spd_attribute *)cur; 23760Sstevel@tonic-gate 23770Sstevel@tonic-gate #define EMIT(tag, value) { \ 23780Sstevel@tonic-gate attr->spd_attr_tag = (tag); \ 23790Sstevel@tonic-gate attr->spd_attr_value = (value); \ 23800Sstevel@tonic-gate attr++; \ 23810Sstevel@tonic-gate } 23820Sstevel@tonic-gate 23830Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 23843448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 23853448Sdh155122 algidx++) { 23863448Sdh155122 23873448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 23883448Sdh155122 alg = ipss->ipsec_alglists[algtype][algid]; 23890Sstevel@tonic-gate 23900Sstevel@tonic-gate /* 23910Sstevel@tonic-gate * If you change the number of EMIT's here, change 23920Sstevel@tonic-gate * ATTRPERALG above to match 23930Sstevel@tonic-gate */ 23940Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_ID, algid); 23950Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_PROTO, algproto[algtype]); 23960Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_INCRBITS, alg->alg_increment); 23970Sstevel@tonic-gate 23980Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_NKEYSIZES, alg->alg_nkey_sizes); 23990Sstevel@tonic-gate for (i = 0; i < alg->alg_nkey_sizes; i++) 24000Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_KEYSIZE, 24010Sstevel@tonic-gate alg->alg_key_sizes[i]); 24020Sstevel@tonic-gate 24030Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_NBLOCKSIZES, alg->alg_nblock_sizes); 24040Sstevel@tonic-gate for (i = 0; i < alg->alg_nblock_sizes; i++) 24050Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_BLOCKSIZE, 24060Sstevel@tonic-gate alg->alg_block_sizes[i]); 24070Sstevel@tonic-gate 24080Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_MECHNAME, CRYPTO_MAX_MECH_NAME); 24090Sstevel@tonic-gate bcopy(alg->alg_mech_name, attr, CRYPTO_MAX_MECH_NAME); 24100Sstevel@tonic-gate attr = (struct spd_attribute *)((char *)attr + 24110Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME); 24120Sstevel@tonic-gate 24130Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); 24140Sstevel@tonic-gate } 24150Sstevel@tonic-gate } 24160Sstevel@tonic-gate 24173448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 24180Sstevel@tonic-gate 24190Sstevel@tonic-gate #undef EMITALGATTRS 24200Sstevel@tonic-gate #undef EMIT 24210Sstevel@tonic-gate #undef ATTRPERALG 24220Sstevel@tonic-gate 24230Sstevel@tonic-gate attr--; 24240Sstevel@tonic-gate attr->spd_attr_tag = SPD_ATTR_END; 24250Sstevel@tonic-gate 24260Sstevel@tonic-gate freemsg(mp); 24270Sstevel@tonic-gate qreply(q, m); 24280Sstevel@tonic-gate } 24290Sstevel@tonic-gate 24300Sstevel@tonic-gate /* 24310Sstevel@tonic-gate * Do the actual work of processing an SPD_UPDATEALGS request. Can 24320Sstevel@tonic-gate * be invoked either once IPsec is loaded on a cached request, or 24330Sstevel@tonic-gate * when a request is received while IPsec is loaded. 24340Sstevel@tonic-gate */ 24350Sstevel@tonic-gate static void 24363448Sdh155122 spdsock_do_updatealg(spd_ext_t *extv[], int *diag, spd_stack_t *spds) 24370Sstevel@tonic-gate { 24380Sstevel@tonic-gate struct spd_ext_actions *actp; 24390Sstevel@tonic-gate struct spd_attribute *attr, *endattr; 24400Sstevel@tonic-gate uint64_t *start, *end; 24410Sstevel@tonic-gate ipsec_alginfo_t *alg = NULL; 24420Sstevel@tonic-gate ipsec_algtype_t alg_type = 0; 24430Sstevel@tonic-gate boolean_t skip_alg = B_TRUE, doing_proto = B_FALSE; 24440Sstevel@tonic-gate uint_t i, cur_key, cur_block, algid; 24450Sstevel@tonic-gate 24460Sstevel@tonic-gate *diag = -1; 24473448Sdh155122 ASSERT(MUTEX_HELD(&spds->spds_alg_lock)); 24480Sstevel@tonic-gate 24490Sstevel@tonic-gate /* parse the message, building the list of algorithms */ 24500Sstevel@tonic-gate 24510Sstevel@tonic-gate actp = (struct spd_ext_actions *)extv[SPD_EXT_ACTION]; 24520Sstevel@tonic-gate if (actp == NULL) { 24530Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_NO_ACTION_EXT; 24540Sstevel@tonic-gate return; 24550Sstevel@tonic-gate } 24560Sstevel@tonic-gate 24570Sstevel@tonic-gate start = (uint64_t *)actp; 24580Sstevel@tonic-gate end = (start + actp->spd_actions_len); 24590Sstevel@tonic-gate endattr = (struct spd_attribute *)end; 24600Sstevel@tonic-gate attr = (struct spd_attribute *)&actp[1]; 24610Sstevel@tonic-gate 24623448Sdh155122 bzero(spds->spds_algs, IPSEC_NALGTYPES * IPSEC_MAX_ALGS * 24630Sstevel@tonic-gate sizeof (ipsec_alginfo_t *)); 24640Sstevel@tonic-gate 24650Sstevel@tonic-gate alg = kmem_zalloc(sizeof (*alg), KM_SLEEP); 24660Sstevel@tonic-gate 24670Sstevel@tonic-gate #define ALG_KEY_SIZES(a) (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t)) 24680Sstevel@tonic-gate #define ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t)) 24690Sstevel@tonic-gate 24700Sstevel@tonic-gate while (attr < endattr) { 24710Sstevel@tonic-gate switch (attr->spd_attr_tag) { 24720Sstevel@tonic-gate case SPD_ATTR_NOP: 24730Sstevel@tonic-gate case SPD_ATTR_EMPTY: 24740Sstevel@tonic-gate break; 24750Sstevel@tonic-gate case SPD_ATTR_END: 24760Sstevel@tonic-gate attr = endattr; 24770Sstevel@tonic-gate /* FALLTHRU */ 24780Sstevel@tonic-gate case SPD_ATTR_NEXT: 24790Sstevel@tonic-gate if (doing_proto) { 24800Sstevel@tonic-gate doing_proto = B_FALSE; 24810Sstevel@tonic-gate break; 24820Sstevel@tonic-gate } 24830Sstevel@tonic-gate if (skip_alg) { 24840Sstevel@tonic-gate ipsec_alg_free(alg); 24850Sstevel@tonic-gate } else { 24860Sstevel@tonic-gate ipsec_alg_free( 24873448Sdh155122 spds->spds_algs[alg_type][alg->alg_id]); 24883448Sdh155122 spds->spds_algs[alg_type][alg->alg_id] = 24893448Sdh155122 alg; 24900Sstevel@tonic-gate } 24910Sstevel@tonic-gate alg = kmem_zalloc(sizeof (*alg), KM_SLEEP); 24920Sstevel@tonic-gate break; 24930Sstevel@tonic-gate 24940Sstevel@tonic-gate case SPD_ATTR_ALG_ID: 24950Sstevel@tonic-gate if (attr->spd_attr_value >= IPSEC_MAX_ALGS) { 24963448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 24970Sstevel@tonic-gate "invalid alg id %d\n", 24980Sstevel@tonic-gate attr->spd_attr_value)); 24990Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_ID_RANGE; 25000Sstevel@tonic-gate goto bail; 25010Sstevel@tonic-gate } 25020Sstevel@tonic-gate alg->alg_id = attr->spd_attr_value; 25030Sstevel@tonic-gate break; 25040Sstevel@tonic-gate 25050Sstevel@tonic-gate case SPD_ATTR_ALG_PROTO: 25060Sstevel@tonic-gate /* find the alg type */ 25070Sstevel@tonic-gate for (i = 0; i < NALGPROTOS; i++) 25080Sstevel@tonic-gate if (algproto[i] == attr->spd_attr_value) 25090Sstevel@tonic-gate break; 25100Sstevel@tonic-gate skip_alg = (i == NALGPROTOS); 25110Sstevel@tonic-gate if (!skip_alg) 25120Sstevel@tonic-gate alg_type = i; 25130Sstevel@tonic-gate break; 25140Sstevel@tonic-gate 25150Sstevel@tonic-gate case SPD_ATTR_ALG_INCRBITS: 25160Sstevel@tonic-gate alg->alg_increment = attr->spd_attr_value; 25170Sstevel@tonic-gate break; 25180Sstevel@tonic-gate 25190Sstevel@tonic-gate case SPD_ATTR_ALG_NKEYSIZES: 25200Sstevel@tonic-gate if (alg->alg_key_sizes != NULL) { 25210Sstevel@tonic-gate kmem_free(alg->alg_key_sizes, 25220Sstevel@tonic-gate ALG_KEY_SIZES(alg)); 25230Sstevel@tonic-gate } 25240Sstevel@tonic-gate alg->alg_nkey_sizes = attr->spd_attr_value; 25250Sstevel@tonic-gate /* 25260Sstevel@tonic-gate * Allocate room for the trailing zero key size 25270Sstevel@tonic-gate * value as well. 25280Sstevel@tonic-gate */ 25290Sstevel@tonic-gate alg->alg_key_sizes = kmem_zalloc(ALG_KEY_SIZES(alg), 25300Sstevel@tonic-gate KM_SLEEP); 25310Sstevel@tonic-gate cur_key = 0; 25320Sstevel@tonic-gate break; 25330Sstevel@tonic-gate 25340Sstevel@tonic-gate case SPD_ATTR_ALG_KEYSIZE: 25350Sstevel@tonic-gate if (alg->alg_key_sizes == NULL || 25360Sstevel@tonic-gate cur_key >= alg->alg_nkey_sizes) { 25373448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 25380Sstevel@tonic-gate "too many key sizes\n")); 25390Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES; 25400Sstevel@tonic-gate goto bail; 25410Sstevel@tonic-gate } 25420Sstevel@tonic-gate alg->alg_key_sizes[cur_key++] = attr->spd_attr_value; 25430Sstevel@tonic-gate break; 25440Sstevel@tonic-gate 25450Sstevel@tonic-gate case SPD_ATTR_ALG_NBLOCKSIZES: 25460Sstevel@tonic-gate if (alg->alg_block_sizes != NULL) { 25470Sstevel@tonic-gate kmem_free(alg->alg_block_sizes, 25480Sstevel@tonic-gate ALG_BLOCK_SIZES(alg)); 25490Sstevel@tonic-gate } 25500Sstevel@tonic-gate alg->alg_nblock_sizes = attr->spd_attr_value; 25510Sstevel@tonic-gate /* 25520Sstevel@tonic-gate * Allocate room for the trailing zero block size 25530Sstevel@tonic-gate * value as well. 25540Sstevel@tonic-gate */ 25550Sstevel@tonic-gate alg->alg_block_sizes = kmem_zalloc(ALG_BLOCK_SIZES(alg), 25560Sstevel@tonic-gate KM_SLEEP); 25570Sstevel@tonic-gate cur_block = 0; 25580Sstevel@tonic-gate break; 25590Sstevel@tonic-gate 25600Sstevel@tonic-gate case SPD_ATTR_ALG_BLOCKSIZE: 25610Sstevel@tonic-gate if (alg->alg_block_sizes == NULL || 25620Sstevel@tonic-gate cur_block >= alg->alg_nblock_sizes) { 25633448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 25640Sstevel@tonic-gate "too many block sizes\n")); 25650Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES; 25660Sstevel@tonic-gate goto bail; 25670Sstevel@tonic-gate } 25680Sstevel@tonic-gate alg->alg_block_sizes[cur_block++] = 25690Sstevel@tonic-gate attr->spd_attr_value; 25700Sstevel@tonic-gate break; 25710Sstevel@tonic-gate 25720Sstevel@tonic-gate case SPD_ATTR_ALG_MECHNAME: { 25730Sstevel@tonic-gate char *mech_name; 25740Sstevel@tonic-gate 25750Sstevel@tonic-gate if (attr->spd_attr_value > CRYPTO_MAX_MECH_NAME) { 25763448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 25770Sstevel@tonic-gate "mech name too long\n")); 25780Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN; 25790Sstevel@tonic-gate goto bail; 25800Sstevel@tonic-gate } 25810Sstevel@tonic-gate mech_name = (char *)(attr + 1); 25820Sstevel@tonic-gate bcopy(mech_name, alg->alg_mech_name, 25830Sstevel@tonic-gate attr->spd_attr_value); 25840Sstevel@tonic-gate alg->alg_mech_name[CRYPTO_MAX_MECH_NAME-1] = '\0'; 25850Sstevel@tonic-gate attr = (struct spd_attribute *)((char *)attr + 25860Sstevel@tonic-gate attr->spd_attr_value); 25870Sstevel@tonic-gate break; 25880Sstevel@tonic-gate } 25890Sstevel@tonic-gate 25900Sstevel@tonic-gate case SPD_ATTR_PROTO_ID: 25910Sstevel@tonic-gate doing_proto = B_TRUE; 25920Sstevel@tonic-gate for (i = 0; i < NALGPROTOS; i++) { 25930Sstevel@tonic-gate if (algproto[i] == attr->spd_attr_value) { 25940Sstevel@tonic-gate alg_type = i; 25950Sstevel@tonic-gate break; 25960Sstevel@tonic-gate } 25970Sstevel@tonic-gate } 25980Sstevel@tonic-gate break; 25990Sstevel@tonic-gate 26000Sstevel@tonic-gate case SPD_ATTR_PROTO_EXEC_MODE: 26010Sstevel@tonic-gate if (!doing_proto) 26020Sstevel@tonic-gate break; 26030Sstevel@tonic-gate for (i = 0; i < NEXECMODES; i++) { 26040Sstevel@tonic-gate if (execmodes[i] == attr->spd_attr_value) { 26053448Sdh155122 spds->spds_algs_exec_mode[alg_type] = i; 26060Sstevel@tonic-gate break; 26070Sstevel@tonic-gate } 26080Sstevel@tonic-gate } 26090Sstevel@tonic-gate break; 26100Sstevel@tonic-gate } 26110Sstevel@tonic-gate attr++; 26120Sstevel@tonic-gate } 26130Sstevel@tonic-gate 26140Sstevel@tonic-gate #undef ALG_KEY_SIZES 26150Sstevel@tonic-gate #undef ALG_BLOCK_SIZES 26160Sstevel@tonic-gate 26170Sstevel@tonic-gate /* update the algorithm tables */ 26183448Sdh155122 spdsock_merge_algs(spds); 26190Sstevel@tonic-gate bail: 26200Sstevel@tonic-gate /* cleanup */ 26210Sstevel@tonic-gate ipsec_alg_free(alg); 26220Sstevel@tonic-gate for (alg_type = 0; alg_type < IPSEC_NALGTYPES; alg_type++) 26233448Sdh155122 for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) 26243448Sdh155122 if (spds->spds_algs[alg_type][algid] != NULL) 26253448Sdh155122 ipsec_alg_free(spds->spds_algs[alg_type][algid]); 26260Sstevel@tonic-gate } 26270Sstevel@tonic-gate 26280Sstevel@tonic-gate /* 26290Sstevel@tonic-gate * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue 26300Sstevel@tonic-gate * the request until IPsec loads. If IPsec is loaded, act on it 26310Sstevel@tonic-gate * immediately. 26320Sstevel@tonic-gate */ 26330Sstevel@tonic-gate 26340Sstevel@tonic-gate static void 26350Sstevel@tonic-gate spdsock_updatealg(queue_t *q, mblk_t *mp, spd_ext_t *extv[]) 26360Sstevel@tonic-gate { 26373448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 26383448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 26393448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 26403448Sdh155122 26413448Sdh155122 if (!ipsec_loaded(ipss)) { 26420Sstevel@tonic-gate /* 26430Sstevel@tonic-gate * IPsec is not loaded, save request and return nicely, 26440Sstevel@tonic-gate * the message will be processed once IPsec loads. 26450Sstevel@tonic-gate */ 26460Sstevel@tonic-gate mblk_t *new_mp; 26470Sstevel@tonic-gate 26480Sstevel@tonic-gate /* last update message wins */ 26490Sstevel@tonic-gate if ((new_mp = copymsg(mp)) == NULL) { 26500Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 26510Sstevel@tonic-gate return; 26520Sstevel@tonic-gate } 26533448Sdh155122 mutex_enter(&spds->spds_alg_lock); 26543448Sdh155122 bcopy(extv, spds->spds_extv_algs, 26550Sstevel@tonic-gate sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1)); 26563448Sdh155122 if (spds->spds_mp_algs != NULL) 26573448Sdh155122 freemsg(spds->spds_mp_algs); 26583448Sdh155122 spds->spds_mp_algs = mp; 26593448Sdh155122 spds->spds_algs_pending = B_TRUE; 26603448Sdh155122 mutex_exit(&spds->spds_alg_lock); 2661*4307Spwernau if (audit_active) 2662*4307Spwernau audit_pf_policy(SPD_UPDATEALGS, DB_CRED(mp), 2663*4307Spwernau spds->spds_netstack, NULL, B_TRUE, EAGAIN, 2664*4307Spwernau DB_CPID(mp)); 26650Sstevel@tonic-gate spd_echo(q, new_mp); 26660Sstevel@tonic-gate } else { 26670Sstevel@tonic-gate /* 26680Sstevel@tonic-gate * IPsec is loaded, act on the message immediately. 26690Sstevel@tonic-gate */ 26700Sstevel@tonic-gate int diag; 26710Sstevel@tonic-gate 26723448Sdh155122 mutex_enter(&spds->spds_alg_lock); 26733448Sdh155122 spdsock_do_updatealg(extv, &diag, spds); 26743448Sdh155122 mutex_exit(&spds->spds_alg_lock); 2675*4307Spwernau if (diag == -1) { 26760Sstevel@tonic-gate spd_echo(q, mp); 2677*4307Spwernau if (audit_active) 2678*4307Spwernau audit_pf_policy(SPD_UPDATEALGS, DB_CRED(mp), 2679*4307Spwernau spds->spds_netstack, NULL, B_TRUE, 0, 2680*4307Spwernau DB_CPID(mp)); 2681*4307Spwernau } else { 26820Sstevel@tonic-gate spdsock_diag(q, mp, diag); 2683*4307Spwernau if (audit_active) 2684*4307Spwernau audit_pf_policy(SPD_UPDATEALGS, DB_CRED(mp), 2685*4307Spwernau spds->spds_netstack, NULL, B_TRUE, diag, 2686*4307Spwernau DB_CPID(mp)); 2687*4307Spwernau } 26880Sstevel@tonic-gate } 26890Sstevel@tonic-gate } 26900Sstevel@tonic-gate 26913055Sdanmcd /* 26923055Sdanmcd * With a reference-held ill, dig down and find an instance of "tun", and 26933055Sdanmcd * assign its tunnel policy pointer, while reference-holding it. Also, 26943055Sdanmcd * release ill's refrence when finished. 26953055Sdanmcd * 26963055Sdanmcd * We'll be messing with q_next, so be VERY careful. 26973055Sdanmcd */ 26983055Sdanmcd static void 26993055Sdanmcd find_tun_and_set_itp(ill_t *ill, ipsec_tun_pol_t *itp) 27003055Sdanmcd { 27013055Sdanmcd queue_t *q; 27023055Sdanmcd tun_t *tun; 27033055Sdanmcd 27043055Sdanmcd /* Don't bother if this ill is going away. */ 27053055Sdanmcd if (ill->ill_flags & ILL_CONDEMNED) { 27063055Sdanmcd ill_refrele(ill); 27073055Sdanmcd return; 27083055Sdanmcd } 27093055Sdanmcd 27103055Sdanmcd 27113055Sdanmcd q = ill->ill_wq; 27123055Sdanmcd claimstr(q); /* Lighter-weight than freezestr(). */ 27133055Sdanmcd 27143055Sdanmcd do { 27153055Sdanmcd /* Use strcmp() because "tun" is bounded. */ 27163055Sdanmcd if (strcmp(q->q_qinfo->qi_minfo->mi_idname, "tun") == 0) { 27173055Sdanmcd /* Aha! Got it. */ 27183055Sdanmcd tun = (tun_t *)q->q_ptr; 27193055Sdanmcd if (tun != NULL) { 27203055Sdanmcd mutex_enter(&tun->tun_lock); 27213055Sdanmcd if (tun->tun_itp != itp) { 27223055Sdanmcd ASSERT(tun->tun_itp == NULL); 27233055Sdanmcd ITP_REFHOLD(itp); 27243055Sdanmcd tun->tun_itp = itp; 27253055Sdanmcd } 27263055Sdanmcd mutex_exit(&tun->tun_lock); 27273055Sdanmcd goto release_and_return; 27283055Sdanmcd } 27293055Sdanmcd /* 27303055Sdanmcd * Else assume this is some other module named "tun" 27313055Sdanmcd * and move on, hoping we find one that actually has 27323055Sdanmcd * something in q_ptr. 27333055Sdanmcd */ 27343055Sdanmcd } 27353055Sdanmcd q = q->q_next; 27363055Sdanmcd } while (q != NULL); 27373055Sdanmcd 27383055Sdanmcd release_and_return: 27393055Sdanmcd releasestr(ill->ill_wq); 27403055Sdanmcd ill_refrele(ill); 27413055Sdanmcd } 27423055Sdanmcd 27433055Sdanmcd /* 27443055Sdanmcd * Sort through the mess of polhead options to retrieve an appropriate one. 27453055Sdanmcd * Returns NULL if we send an spdsock error. Returns a valid pointer if we 27463055Sdanmcd * found a valid polhead. Returns ALL_ACTIVE_POLHEADS (aka. -1) or 27473055Sdanmcd * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to 27483055Sdanmcd * act on ALL policy heads. 27493055Sdanmcd */ 27503055Sdanmcd static ipsec_policy_head_t * 27513055Sdanmcd get_appropriate_polhead(queue_t *q, mblk_t *mp, spd_if_t *tunname, int spdid, 27523055Sdanmcd int msgtype, ipsec_tun_pol_t **itpp) 27533055Sdanmcd { 27543055Sdanmcd ipsec_tun_pol_t *itp; 27553055Sdanmcd ipsec_policy_head_t *iph; 27563055Sdanmcd int errno; 27573055Sdanmcd char *tname; 27583055Sdanmcd boolean_t active; 27593055Sdanmcd spdsock_t *ss = (spdsock_t *)q->q_ptr; 27603448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 27613448Sdh155122 netstack_t *ns = spds->spds_netstack; 27623055Sdanmcd uint64_t gen; /* Placeholder */ 27633055Sdanmcd ill_t *v4, *v6; 27643055Sdanmcd 27653055Sdanmcd active = (spdid == SPD_ACTIVE); 27663055Sdanmcd *itpp = NULL; 27673055Sdanmcd if (!active && spdid != SPD_STANDBY) { 27683055Sdanmcd spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_SPDID); 27693055Sdanmcd return (NULL); 27703055Sdanmcd } 27713055Sdanmcd 27723055Sdanmcd if (tunname != NULL) { 27733055Sdanmcd /* Acting on a tunnel's SPD. */ 27743055Sdanmcd tname = (char *)tunname->spd_if_name; 27753055Sdanmcd if (*tname == '\0') { 27763055Sdanmcd /* Handle all-polhead cases here. */ 27773055Sdanmcd if (msgtype != SPD_FLUSH && msgtype != SPD_DUMP) { 27783055Sdanmcd spdsock_diag(q, mp, 27793055Sdanmcd SPD_DIAGNOSTIC_NOT_GLOBAL_OP); 27803055Sdanmcd return (NULL); 27813055Sdanmcd } 27823055Sdanmcd return (active ? ALL_ACTIVE_POLHEADS : 27833055Sdanmcd ALL_INACTIVE_POLHEADS); 27843055Sdanmcd } 27853055Sdanmcd 27863448Sdh155122 itp = get_tunnel_policy(tname, spds->spds_netstack); 27873055Sdanmcd if (itp == NULL) { 27883055Sdanmcd if (msgtype != SPD_ADDRULE) { 27893055Sdanmcd /* "Tunnel not found" */ 27903055Sdanmcd spdsock_error(q, mp, ENOENT, 0); 27913055Sdanmcd return (NULL); 27923055Sdanmcd } 27933055Sdanmcd 27943055Sdanmcd errno = 0; 27953448Sdh155122 itp = create_tunnel_policy(tname, &errno, &gen, 27963448Sdh155122 spds->spds_netstack); 27973055Sdanmcd if (itp == NULL) { 27983055Sdanmcd /* 27993055Sdanmcd * Something very bad happened, most likely 28003055Sdanmcd * ENOMEM. Return an indicator. 28013055Sdanmcd */ 28023055Sdanmcd spdsock_error(q, mp, errno, 0); 28033055Sdanmcd return (NULL); 28043055Sdanmcd } 28053055Sdanmcd } 28063055Sdanmcd /* 28073055Sdanmcd * Troll the plumbed tunnels and see if we have a 28083055Sdanmcd * match. We need to do this always in case we add 28093055Sdanmcd * policy AFTER plumbing a tunnel. 28103055Sdanmcd */ 28113055Sdanmcd v4 = ill_lookup_on_name(tname, B_FALSE, B_FALSE, NULL, 28123448Sdh155122 NULL, NULL, &errno, NULL, ns->netstack_ip); 28133055Sdanmcd if (v4 != NULL) 28143055Sdanmcd find_tun_and_set_itp(v4, itp); 28153055Sdanmcd v6 = ill_lookup_on_name(tname, B_FALSE, B_TRUE, NULL, 28163448Sdh155122 NULL, NULL, &errno, NULL, ns->netstack_ip); 28173055Sdanmcd if (v6 != NULL) 28183055Sdanmcd find_tun_and_set_itp(v6, itp); 28193055Sdanmcd ASSERT(itp != NULL); 28203055Sdanmcd *itpp = itp; 28213055Sdanmcd /* For spdsock dump state, set the polhead's name. */ 28223055Sdanmcd if (msgtype == SPD_DUMP) { 28233055Sdanmcd (void) strncpy(ss->spdsock_dump_name, tname, LIFNAMSIZ); 28243055Sdanmcd ss->spdsock_dump_tunnel = itp->itp_flags & 28253055Sdanmcd (active ? ITPF_P_TUNNEL : ITPF_I_TUNNEL); 28263055Sdanmcd } 28273055Sdanmcd } else { 28283055Sdanmcd itp = NULL; 28293055Sdanmcd /* For spdsock dump state, indicate it's global policy. */ 28303055Sdanmcd if (msgtype == SPD_DUMP) 28313055Sdanmcd ss->spdsock_dump_name[0] = '\0'; 28323055Sdanmcd } 28333055Sdanmcd 28343055Sdanmcd if (active) 28353448Sdh155122 iph = (itp == NULL) ? ipsec_system_policy(ns) : itp->itp_policy; 28363055Sdanmcd else 28373448Sdh155122 iph = (itp == NULL) ? ipsec_inactive_policy(ns) : 28383055Sdanmcd itp->itp_inactive; 28393055Sdanmcd 28403055Sdanmcd ASSERT(iph != NULL); 28413055Sdanmcd if (itp != NULL) { 28423055Sdanmcd IPPH_REFHOLD(iph); 28433055Sdanmcd } 28443055Sdanmcd 28453055Sdanmcd return (iph); 28463055Sdanmcd } 28473055Sdanmcd 28480Sstevel@tonic-gate static void 28490Sstevel@tonic-gate spdsock_parse(queue_t *q, mblk_t *mp) 28500Sstevel@tonic-gate { 28510Sstevel@tonic-gate spd_msg_t *spmsg; 28520Sstevel@tonic-gate spd_ext_t *extv[SPD_EXT_MAX + 1]; 28530Sstevel@tonic-gate uint_t msgsize; 28540Sstevel@tonic-gate ipsec_policy_head_t *iph; 28553055Sdanmcd ipsec_tun_pol_t *itp; 28563055Sdanmcd spd_if_t *tunname; 28573448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 28583448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 28593448Sdh155122 netstack_t *ns = spds->spds_netstack; 28603448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 28610Sstevel@tonic-gate 28620Sstevel@tonic-gate /* Make sure nothing's below me. */ 28630Sstevel@tonic-gate ASSERT(WR(q)->q_next == NULL); 28640Sstevel@tonic-gate 28650Sstevel@tonic-gate spmsg = (spd_msg_t *)mp->b_rptr; 28660Sstevel@tonic-gate 28670Sstevel@tonic-gate msgsize = SPD_64TO8(spmsg->spd_msg_len); 28680Sstevel@tonic-gate 28690Sstevel@tonic-gate if (msgdsize(mp) != msgsize) { 28700Sstevel@tonic-gate /* 28710Sstevel@tonic-gate * Message len incorrect w.r.t. actual size. Send an error 28720Sstevel@tonic-gate * (EMSGSIZE). It may be necessary to massage things a 28730Sstevel@tonic-gate * bit. For example, if the spd_msg_type is hosed, 28740Sstevel@tonic-gate * I need to set it to SPD_RESERVED to get delivery to 28750Sstevel@tonic-gate * do the right thing. Then again, maybe just letting 28760Sstevel@tonic-gate * the error delivery do the right thing. 28770Sstevel@tonic-gate */ 28783448Sdh155122 ss2dbg(spds, 28793448Sdh155122 ("mblk (%lu) and base (%d) message sizes don't jibe.\n", 28800Sstevel@tonic-gate msgdsize(mp), msgsize)); 28810Sstevel@tonic-gate spdsock_error(q, mp, EMSGSIZE, SPD_DIAGNOSTIC_NONE); 28820Sstevel@tonic-gate return; 28830Sstevel@tonic-gate } 28840Sstevel@tonic-gate 28850Sstevel@tonic-gate if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) { 28860Sstevel@tonic-gate /* Get all message into one mblk. */ 28870Sstevel@tonic-gate if (pullupmsg(mp, -1) == 0) { 28880Sstevel@tonic-gate /* 28890Sstevel@tonic-gate * Something screwy happened. 28900Sstevel@tonic-gate */ 28913448Sdh155122 ss3dbg(spds, ("spdsock_parse: pullupmsg() failed.\n")); 28920Sstevel@tonic-gate return; 28930Sstevel@tonic-gate } else { 28940Sstevel@tonic-gate spmsg = (spd_msg_t *)mp->b_rptr; 28950Sstevel@tonic-gate } 28960Sstevel@tonic-gate } 28970Sstevel@tonic-gate 28980Sstevel@tonic-gate switch (spdsock_get_ext(extv, spmsg, msgsize)) { 28990Sstevel@tonic-gate case KGE_DUP: 29000Sstevel@tonic-gate /* Handle duplicate extension. */ 29013448Sdh155122 ss1dbg(spds, ("Got duplicate extension of type %d.\n", 29020Sstevel@tonic-gate extv[0]->spd_ext_type)); 29030Sstevel@tonic-gate spdsock_diag(q, mp, dup_ext_diag[extv[0]->spd_ext_type]); 29040Sstevel@tonic-gate return; 29050Sstevel@tonic-gate case KGE_UNK: 29060Sstevel@tonic-gate /* Handle unknown extension. */ 29073448Sdh155122 ss1dbg(spds, ("Got unknown extension of type %d.\n", 29080Sstevel@tonic-gate extv[0]->spd_ext_type)); 29090Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_UNKNOWN_EXT); 29100Sstevel@tonic-gate return; 29110Sstevel@tonic-gate case KGE_LEN: 29120Sstevel@tonic-gate /* Length error. */ 29133448Sdh155122 ss1dbg(spds, ("Length %d on extension type %d overrun or 0.\n", 29140Sstevel@tonic-gate extv[0]->spd_ext_len, extv[0]->spd_ext_type)); 29150Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_EXTLEN); 29160Sstevel@tonic-gate return; 29170Sstevel@tonic-gate case KGE_CHK: 29180Sstevel@tonic-gate /* Reality check failed. */ 29193448Sdh155122 ss1dbg(spds, ("Reality check failed on extension type %d.\n", 29200Sstevel@tonic-gate extv[0]->spd_ext_type)); 29210Sstevel@tonic-gate spdsock_diag(q, mp, bad_ext_diag[extv[0]->spd_ext_type]); 29220Sstevel@tonic-gate return; 29230Sstevel@tonic-gate default: 29240Sstevel@tonic-gate /* Default case is no errors. */ 29250Sstevel@tonic-gate break; 29260Sstevel@tonic-gate } 29270Sstevel@tonic-gate 29280Sstevel@tonic-gate /* 29290Sstevel@tonic-gate * Special-case SPD_UPDATEALGS so as not to load IPsec. 29300Sstevel@tonic-gate */ 29313448Sdh155122 if (!ipsec_loaded(ipss) && spmsg->spd_msg_type != SPD_UPDATEALGS) { 29320Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 29330Sstevel@tonic-gate 29340Sstevel@tonic-gate ASSERT(ss != NULL); 29353448Sdh155122 ipsec_loader_loadnow(ipss); 29360Sstevel@tonic-gate ss->spdsock_timeout_arg = mp; 29370Sstevel@tonic-gate ss->spdsock_timeout = qtimeout(q, spdsock_loadcheck, 29380Sstevel@tonic-gate q, LOADCHECK_INTERVAL); 29390Sstevel@tonic-gate return; 29400Sstevel@tonic-gate } 29410Sstevel@tonic-gate 29423055Sdanmcd /* First check for messages that need no polheads at all. */ 29430Sstevel@tonic-gate switch (spmsg->spd_msg_type) { 29440Sstevel@tonic-gate case SPD_UPDATEALGS: 29450Sstevel@tonic-gate spdsock_updatealg(q, mp, extv); 29460Sstevel@tonic-gate return; 29470Sstevel@tonic-gate case SPD_ALGLIST: 29480Sstevel@tonic-gate spdsock_alglist(q, mp); 29490Sstevel@tonic-gate return; 29500Sstevel@tonic-gate case SPD_DUMPALGS: 29510Sstevel@tonic-gate spdsock_dumpalgs(q, mp); 29520Sstevel@tonic-gate return; 29533055Sdanmcd } 29543055Sdanmcd 29553055Sdanmcd /* 29563055Sdanmcd * Then check for ones that need both primary/secondary polheads, 29573055Sdanmcd * finding the appropriate tunnel policy if need be. 29583055Sdanmcd */ 29593055Sdanmcd tunname = (spd_if_t *)extv[SPD_EXT_TUN_NAME]; 29603055Sdanmcd switch (spmsg->spd_msg_type) { 29613055Sdanmcd case SPD_FLIP: 29623055Sdanmcd spdsock_flip(q, mp, tunname); 29633055Sdanmcd return; 29643055Sdanmcd case SPD_CLONE: 29653055Sdanmcd spdsock_clone(q, mp, tunname); 29663055Sdanmcd return; 29673055Sdanmcd } 29683055Sdanmcd 29693055Sdanmcd /* 29703055Sdanmcd * Finally, find ones that operate on exactly one polhead, or 29713055Sdanmcd * "all polheads" of a given type (active/inactive). 29723055Sdanmcd */ 29733055Sdanmcd iph = get_appropriate_polhead(q, mp, tunname, spmsg->spd_msg_spdid, 29743055Sdanmcd spmsg->spd_msg_type, &itp); 29753055Sdanmcd if (iph == NULL) 29763055Sdanmcd return; 29773055Sdanmcd 29783055Sdanmcd /* All-polheads-ready operations. */ 29793055Sdanmcd switch (spmsg->spd_msg_type) { 29803055Sdanmcd case SPD_FLUSH: 29813055Sdanmcd if (itp != NULL) { 29823055Sdanmcd mutex_enter(&itp->itp_lock); 29833055Sdanmcd if (spmsg->spd_msg_spdid == SPD_ACTIVE) 29843055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 29853055Sdanmcd else 29863055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 29873055Sdanmcd mutex_exit(&itp->itp_lock); 29883448Sdh155122 ITP_REFRELE(itp, ns); 29893055Sdanmcd } 2990*4307Spwernau spdsock_flush(q, iph, itp, mp); 29913055Sdanmcd return; 29923055Sdanmcd case SPD_DUMP: 29933055Sdanmcd if (itp != NULL) 29943448Sdh155122 ITP_REFRELE(itp, ns); 29953055Sdanmcd spdsock_dump(q, iph, mp); 29963055Sdanmcd return; 29973055Sdanmcd } 29983055Sdanmcd 29993055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) { 30003055Sdanmcd spdsock_diag(q, mp, SPD_DIAGNOSTIC_NOT_GLOBAL_OP); 30013055Sdanmcd return; 30023055Sdanmcd } 30033055Sdanmcd 30043055Sdanmcd /* Single-polhead-only operations. */ 30053055Sdanmcd switch (spmsg->spd_msg_type) { 30063055Sdanmcd case SPD_ADDRULE: 30073055Sdanmcd spdsock_addrule(q, iph, mp, extv, itp); 30083055Sdanmcd break; 30093055Sdanmcd case SPD_DELETERULE: 30103055Sdanmcd spdsock_deleterule(q, iph, mp, extv, itp); 30113055Sdanmcd break; 30123055Sdanmcd case SPD_LOOKUP: 30133055Sdanmcd spdsock_lookup(q, iph, mp, extv, itp); 30143055Sdanmcd break; 30150Sstevel@tonic-gate default: 30160Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_MSG_TYPE); 30173055Sdanmcd break; 30180Sstevel@tonic-gate } 30193055Sdanmcd 30203448Sdh155122 IPPH_REFRELE(iph, spds->spds_netstack); 30213055Sdanmcd if (itp != NULL) 30223448Sdh155122 ITP_REFRELE(itp, ns); 30230Sstevel@tonic-gate } 30240Sstevel@tonic-gate 30250Sstevel@tonic-gate /* 30260Sstevel@tonic-gate * If an algorithm mapping was received before IPsec was loaded, process it. 30270Sstevel@tonic-gate * Called from the IPsec loader. 30280Sstevel@tonic-gate */ 30290Sstevel@tonic-gate void 30303448Sdh155122 spdsock_update_pending_algs(netstack_t *ns) 30310Sstevel@tonic-gate { 30323448Sdh155122 spd_stack_t *spds = ns->netstack_spdsock; 30333448Sdh155122 30343448Sdh155122 mutex_enter(&spds->spds_alg_lock); 30353448Sdh155122 if (spds->spds_algs_pending) { 30360Sstevel@tonic-gate int diag; 30373448Sdh155122 30383448Sdh155122 spdsock_do_updatealg(spds->spds_extv_algs, &diag, 30393448Sdh155122 spds); 30403448Sdh155122 spds->spds_algs_pending = B_FALSE; 30410Sstevel@tonic-gate } 30423448Sdh155122 mutex_exit(&spds->spds_alg_lock); 30430Sstevel@tonic-gate } 30440Sstevel@tonic-gate 30450Sstevel@tonic-gate static void 30460Sstevel@tonic-gate spdsock_loadcheck(void *arg) 30470Sstevel@tonic-gate { 30480Sstevel@tonic-gate queue_t *q = (queue_t *)arg; 30490Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 30500Sstevel@tonic-gate mblk_t *mp; 30513448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 30523448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 30530Sstevel@tonic-gate 30540Sstevel@tonic-gate ASSERT(ss != NULL); 30550Sstevel@tonic-gate 30560Sstevel@tonic-gate ss->spdsock_timeout = 0; 30570Sstevel@tonic-gate mp = ss->spdsock_timeout_arg; 30580Sstevel@tonic-gate ASSERT(mp != NULL); 30590Sstevel@tonic-gate ss->spdsock_timeout_arg = NULL; 30603448Sdh155122 if (ipsec_failed(ipss)) 30610Sstevel@tonic-gate spdsock_error(q, mp, EPROTONOSUPPORT, 0); 30620Sstevel@tonic-gate else 30630Sstevel@tonic-gate spdsock_parse(q, mp); 30640Sstevel@tonic-gate } 30650Sstevel@tonic-gate 30660Sstevel@tonic-gate /* 30670Sstevel@tonic-gate * Copy relevant state bits. 30680Sstevel@tonic-gate */ 30690Sstevel@tonic-gate static void 30700Sstevel@tonic-gate spdsock_copy_info(struct T_info_ack *tap, spdsock_t *ss) 30710Sstevel@tonic-gate { 30720Sstevel@tonic-gate *tap = spdsock_g_t_info_ack; 30730Sstevel@tonic-gate tap->CURRENT_state = ss->spdsock_state; 30740Sstevel@tonic-gate tap->OPT_size = spdsock_max_optsize; 30750Sstevel@tonic-gate } 30760Sstevel@tonic-gate 30770Sstevel@tonic-gate /* 30780Sstevel@tonic-gate * This routine responds to T_CAPABILITY_REQ messages. It is called by 30790Sstevel@tonic-gate * spdsock_wput. Much of the T_CAPABILITY_ACK information is copied from 30800Sstevel@tonic-gate * spdsock_g_t_info_ack. The current state of the stream is copied from 30810Sstevel@tonic-gate * spdsock_state. 30820Sstevel@tonic-gate */ 30830Sstevel@tonic-gate static void 30840Sstevel@tonic-gate spdsock_capability_req(queue_t *q, mblk_t *mp) 30850Sstevel@tonic-gate { 30860Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 30870Sstevel@tonic-gate t_uscalar_t cap_bits1; 30880Sstevel@tonic-gate struct T_capability_ack *tcap; 30890Sstevel@tonic-gate 30900Sstevel@tonic-gate cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 30910Sstevel@tonic-gate 30920Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 30930Sstevel@tonic-gate mp->b_datap->db_type, T_CAPABILITY_ACK); 30940Sstevel@tonic-gate if (mp == NULL) 30950Sstevel@tonic-gate return; 30960Sstevel@tonic-gate 30970Sstevel@tonic-gate tcap = (struct T_capability_ack *)mp->b_rptr; 30980Sstevel@tonic-gate tcap->CAP_bits1 = 0; 30990Sstevel@tonic-gate 31000Sstevel@tonic-gate if (cap_bits1 & TC1_INFO) { 31010Sstevel@tonic-gate spdsock_copy_info(&tcap->INFO_ack, ss); 31020Sstevel@tonic-gate tcap->CAP_bits1 |= TC1_INFO; 31030Sstevel@tonic-gate } 31040Sstevel@tonic-gate 31050Sstevel@tonic-gate qreply(q, mp); 31060Sstevel@tonic-gate } 31070Sstevel@tonic-gate 31080Sstevel@tonic-gate /* 31090Sstevel@tonic-gate * This routine responds to T_INFO_REQ messages. It is called by 31100Sstevel@tonic-gate * spdsock_wput_other. 31110Sstevel@tonic-gate * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack. 31120Sstevel@tonic-gate * The current state of the stream is copied from spdsock_state. 31130Sstevel@tonic-gate */ 31140Sstevel@tonic-gate static void 31150Sstevel@tonic-gate spdsock_info_req(q, mp) 31160Sstevel@tonic-gate queue_t *q; 31170Sstevel@tonic-gate mblk_t *mp; 31180Sstevel@tonic-gate { 31190Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 31200Sstevel@tonic-gate T_INFO_ACK); 31210Sstevel@tonic-gate if (mp == NULL) 31220Sstevel@tonic-gate return; 31230Sstevel@tonic-gate spdsock_copy_info((struct T_info_ack *)mp->b_rptr, 31240Sstevel@tonic-gate (spdsock_t *)q->q_ptr); 31250Sstevel@tonic-gate qreply(q, mp); 31260Sstevel@tonic-gate } 31270Sstevel@tonic-gate 31280Sstevel@tonic-gate /* 31290Sstevel@tonic-gate * spdsock_err_ack. This routine creates a 31300Sstevel@tonic-gate * T_ERROR_ACK message and passes it 31310Sstevel@tonic-gate * upstream. 31320Sstevel@tonic-gate */ 31330Sstevel@tonic-gate static void 31340Sstevel@tonic-gate spdsock_err_ack(q, mp, t_error, sys_error) 31350Sstevel@tonic-gate queue_t *q; 31360Sstevel@tonic-gate mblk_t *mp; 31370Sstevel@tonic-gate int t_error; 31380Sstevel@tonic-gate int sys_error; 31390Sstevel@tonic-gate { 31400Sstevel@tonic-gate if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 31410Sstevel@tonic-gate qreply(q, mp); 31420Sstevel@tonic-gate } 31430Sstevel@tonic-gate 31440Sstevel@tonic-gate /* 31450Sstevel@tonic-gate * This routine retrieves the current status of socket options. 31460Sstevel@tonic-gate * It returns the size of the option retrieved. 31470Sstevel@tonic-gate */ 31480Sstevel@tonic-gate /* ARGSUSED */ 31490Sstevel@tonic-gate int 31500Sstevel@tonic-gate spdsock_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 31510Sstevel@tonic-gate { 31520Sstevel@tonic-gate int *i1 = (int *)ptr; 31530Sstevel@tonic-gate 31540Sstevel@tonic-gate switch (level) { 31550Sstevel@tonic-gate case SOL_SOCKET: 31560Sstevel@tonic-gate switch (name) { 31570Sstevel@tonic-gate case SO_TYPE: 31580Sstevel@tonic-gate *i1 = SOCK_RAW; 31590Sstevel@tonic-gate break; 31600Sstevel@tonic-gate /* 31610Sstevel@tonic-gate * The following two items can be manipulated, 31620Sstevel@tonic-gate * but changing them should do nothing. 31630Sstevel@tonic-gate */ 31640Sstevel@tonic-gate case SO_SNDBUF: 31650Sstevel@tonic-gate *i1 = (int)q->q_hiwat; 31660Sstevel@tonic-gate break; 31670Sstevel@tonic-gate case SO_RCVBUF: 31680Sstevel@tonic-gate *i1 = (int)(RD(q)->q_hiwat); 31690Sstevel@tonic-gate break; 31700Sstevel@tonic-gate } 31710Sstevel@tonic-gate break; 31720Sstevel@tonic-gate default: 31730Sstevel@tonic-gate return (0); 31740Sstevel@tonic-gate } 31750Sstevel@tonic-gate return (sizeof (int)); 31760Sstevel@tonic-gate } 31770Sstevel@tonic-gate 31780Sstevel@tonic-gate /* 31790Sstevel@tonic-gate * This routine sets socket options. 31800Sstevel@tonic-gate */ 31810Sstevel@tonic-gate /* ARGSUSED */ 31820Sstevel@tonic-gate int 31830Sstevel@tonic-gate spdsock_opt_set(queue_t *q, uint_t mgmt_flags, int level, int name, 31840Sstevel@tonic-gate uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 31850Sstevel@tonic-gate void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 31860Sstevel@tonic-gate { 31870Sstevel@tonic-gate int *i1 = (int *)invalp; 31883448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 31893448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 31900Sstevel@tonic-gate 31910Sstevel@tonic-gate switch (level) { 31920Sstevel@tonic-gate case SOL_SOCKET: 31930Sstevel@tonic-gate switch (name) { 31940Sstevel@tonic-gate case SO_SNDBUF: 31953448Sdh155122 if (*i1 > spds->spds_max_buf) 31960Sstevel@tonic-gate return (ENOBUFS); 31970Sstevel@tonic-gate q->q_hiwat = *i1; 31980Sstevel@tonic-gate break; 31990Sstevel@tonic-gate case SO_RCVBUF: 32003448Sdh155122 if (*i1 > spds->spds_max_buf) 32010Sstevel@tonic-gate return (ENOBUFS); 32020Sstevel@tonic-gate RD(q)->q_hiwat = *i1; 32030Sstevel@tonic-gate (void) mi_set_sth_hiwat(RD(q), *i1); 32040Sstevel@tonic-gate break; 32050Sstevel@tonic-gate } 32060Sstevel@tonic-gate break; 32070Sstevel@tonic-gate } 32080Sstevel@tonic-gate return (0); 32090Sstevel@tonic-gate } 32100Sstevel@tonic-gate 32110Sstevel@tonic-gate 32120Sstevel@tonic-gate /* 32130Sstevel@tonic-gate * Handle STREAMS messages. 32140Sstevel@tonic-gate */ 32150Sstevel@tonic-gate static void 32160Sstevel@tonic-gate spdsock_wput_other(queue_t *q, mblk_t *mp) 32170Sstevel@tonic-gate { 32180Sstevel@tonic-gate struct iocblk *iocp; 32190Sstevel@tonic-gate int error; 32203448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 32213448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 32223448Sdh155122 cred_t *cr; 32230Sstevel@tonic-gate 32240Sstevel@tonic-gate switch (mp->b_datap->db_type) { 32250Sstevel@tonic-gate case M_PROTO: 32260Sstevel@tonic-gate case M_PCPROTO: 32270Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) { 32283448Sdh155122 ss3dbg(spds, ( 32290Sstevel@tonic-gate "spdsock_wput_other: Not big enough M_PROTO\n")); 32300Sstevel@tonic-gate freemsg(mp); 32310Sstevel@tonic-gate return; 32320Sstevel@tonic-gate } 32333448Sdh155122 cr = zone_get_kcred(netstackid_to_zoneid( 32343448Sdh155122 spds->spds_netstack->netstack_stackid)); 32353448Sdh155122 ASSERT(cr != NULL); 32363448Sdh155122 32370Sstevel@tonic-gate switch (((union T_primitives *)mp->b_rptr)->type) { 32380Sstevel@tonic-gate case T_CAPABILITY_REQ: 32390Sstevel@tonic-gate spdsock_capability_req(q, mp); 32403448Sdh155122 break; 32410Sstevel@tonic-gate case T_INFO_REQ: 32420Sstevel@tonic-gate spdsock_info_req(q, mp); 32433448Sdh155122 break; 32440Sstevel@tonic-gate case T_SVR4_OPTMGMT_REQ: 32453448Sdh155122 (void) svr4_optcom_req(q, mp, DB_CREDDEF(mp, cr), 32460Sstevel@tonic-gate &spdsock_opt_obj); 32473448Sdh155122 break; 32480Sstevel@tonic-gate case T_OPTMGMT_REQ: 32493448Sdh155122 (void) tpi_optcom_req(q, mp, DB_CREDDEF(mp, cr), 32500Sstevel@tonic-gate &spdsock_opt_obj); 32513448Sdh155122 break; 32520Sstevel@tonic-gate case T_DATA_REQ: 32530Sstevel@tonic-gate case T_EXDATA_REQ: 32540Sstevel@tonic-gate case T_ORDREL_REQ: 32550Sstevel@tonic-gate /* Illegal for spdsock. */ 32560Sstevel@tonic-gate freemsg(mp); 32570Sstevel@tonic-gate (void) putnextctl1(RD(q), M_ERROR, EPROTO); 32583448Sdh155122 break; 32590Sstevel@tonic-gate default: 32600Sstevel@tonic-gate /* Not supported by spdsock. */ 32610Sstevel@tonic-gate spdsock_err_ack(q, mp, TNOTSUPPORT, 0); 32623448Sdh155122 break; 32630Sstevel@tonic-gate } 32643448Sdh155122 crfree(cr); 32653448Sdh155122 return; 32660Sstevel@tonic-gate case M_IOCTL: 32670Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 32680Sstevel@tonic-gate error = EINVAL; 32690Sstevel@tonic-gate 32700Sstevel@tonic-gate switch (iocp->ioc_cmd) { 32710Sstevel@tonic-gate case ND_SET: 32720Sstevel@tonic-gate case ND_GET: 32733448Sdh155122 if (nd_getset(q, spds->spds_g_nd, mp)) { 32740Sstevel@tonic-gate qreply(q, mp); 32750Sstevel@tonic-gate return; 32760Sstevel@tonic-gate } else 32770Sstevel@tonic-gate error = ENOENT; 32780Sstevel@tonic-gate /* FALLTHRU */ 32790Sstevel@tonic-gate default: 32800Sstevel@tonic-gate miocnak(q, mp, 0, error); 32810Sstevel@tonic-gate return; 32820Sstevel@tonic-gate } 32830Sstevel@tonic-gate case M_FLUSH: 32840Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) { 32850Sstevel@tonic-gate flushq(q, FLUSHALL); 32860Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW; 32870Sstevel@tonic-gate } 32880Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) { 32890Sstevel@tonic-gate qreply(q, mp); 32900Sstevel@tonic-gate return; 32910Sstevel@tonic-gate } 32920Sstevel@tonic-gate /* Else FALLTHRU */ 32930Sstevel@tonic-gate } 32940Sstevel@tonic-gate 32950Sstevel@tonic-gate /* If fell through, just black-hole the message. */ 32960Sstevel@tonic-gate freemsg(mp); 32970Sstevel@tonic-gate } 32980Sstevel@tonic-gate 32990Sstevel@tonic-gate static void 33000Sstevel@tonic-gate spdsock_wput(queue_t *q, mblk_t *mp) 33010Sstevel@tonic-gate { 33020Sstevel@tonic-gate uint8_t *rptr = mp->b_rptr; 33030Sstevel@tonic-gate mblk_t *mp1; 33040Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 33053448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 33060Sstevel@tonic-gate 33070Sstevel@tonic-gate /* 33080Sstevel@tonic-gate * If we're dumping, defer processing other messages until the 33090Sstevel@tonic-gate * dump completes. 33100Sstevel@tonic-gate */ 33110Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) { 33120Sstevel@tonic-gate if (!putq(q, mp)) 33130Sstevel@tonic-gate freemsg(mp); 33140Sstevel@tonic-gate return; 33150Sstevel@tonic-gate } 33160Sstevel@tonic-gate 33170Sstevel@tonic-gate switch (mp->b_datap->db_type) { 33180Sstevel@tonic-gate case M_DATA: 33190Sstevel@tonic-gate /* 33200Sstevel@tonic-gate * Silently discard. 33210Sstevel@tonic-gate */ 33223448Sdh155122 ss2dbg(spds, ("raw M_DATA in spdsock.\n")); 33230Sstevel@tonic-gate freemsg(mp); 33240Sstevel@tonic-gate return; 33250Sstevel@tonic-gate case M_PROTO: 33260Sstevel@tonic-gate case M_PCPROTO: 33270Sstevel@tonic-gate if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) { 33280Sstevel@tonic-gate if (((union T_primitives *)rptr)->type == T_DATA_REQ) { 33290Sstevel@tonic-gate if ((mp1 = mp->b_cont) == NULL) { 33300Sstevel@tonic-gate /* No data after T_DATA_REQ. */ 33313448Sdh155122 ss2dbg(spds, 33323448Sdh155122 ("No data after DATA_REQ.\n")); 33330Sstevel@tonic-gate freemsg(mp); 33340Sstevel@tonic-gate return; 33350Sstevel@tonic-gate } 33360Sstevel@tonic-gate freeb(mp); 33370Sstevel@tonic-gate mp = mp1; 33383448Sdh155122 ss2dbg(spds, ("T_DATA_REQ\n")); 33390Sstevel@tonic-gate break; /* Out of switch. */ 33400Sstevel@tonic-gate } 33410Sstevel@tonic-gate } 33420Sstevel@tonic-gate /* FALLTHRU */ 33430Sstevel@tonic-gate default: 33443448Sdh155122 ss3dbg(spds, ("In default wput case (%d %d).\n", 33450Sstevel@tonic-gate mp->b_datap->db_type, ((union T_primitives *)rptr)->type)); 33460Sstevel@tonic-gate spdsock_wput_other(q, mp); 33470Sstevel@tonic-gate return; 33480Sstevel@tonic-gate } 33490Sstevel@tonic-gate 33500Sstevel@tonic-gate /* I now have a PF_POLICY message in an M_DATA block. */ 33510Sstevel@tonic-gate spdsock_parse(q, mp); 33520Sstevel@tonic-gate } 33530Sstevel@tonic-gate 33540Sstevel@tonic-gate /* 33550Sstevel@tonic-gate * Device open procedure, called when new queue pair created. 33560Sstevel@tonic-gate * We are passed the read-side queue. 33570Sstevel@tonic-gate */ 33580Sstevel@tonic-gate /* ARGSUSED */ 33590Sstevel@tonic-gate static int 33600Sstevel@tonic-gate spdsock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 33610Sstevel@tonic-gate { 33620Sstevel@tonic-gate spdsock_t *ss; 33630Sstevel@tonic-gate queue_t *oq = OTHERQ(q); 33640Sstevel@tonic-gate minor_t ssminor; 33653448Sdh155122 netstack_t *ns; 33663448Sdh155122 spd_stack_t *spds; 33673448Sdh155122 33683448Sdh155122 if (secpolicy_ip_config(credp, B_FALSE) != 0) 33690Sstevel@tonic-gate return (EPERM); 33700Sstevel@tonic-gate 33710Sstevel@tonic-gate if (q->q_ptr != NULL) 33720Sstevel@tonic-gate return (0); /* Re-open of an already open instance. */ 33730Sstevel@tonic-gate 33740Sstevel@tonic-gate if (sflag & MODOPEN) 33750Sstevel@tonic-gate return (EINVAL); 33760Sstevel@tonic-gate 33773448Sdh155122 ns = netstack_find_by_cred(credp); 33783448Sdh155122 ASSERT(ns != NULL); 33793448Sdh155122 spds = ns->netstack_spdsock; 33803448Sdh155122 ASSERT(spds != NULL); 33813448Sdh155122 33823448Sdh155122 ss2dbg(spds, ("Made it into PF_POLICY socket open.\n")); 33830Sstevel@tonic-gate 33840Sstevel@tonic-gate ssminor = (minor_t)(uintptr_t)vmem_alloc(spdsock_vmem, 1, VM_NOSLEEP); 33853448Sdh155122 if (ssminor == 0) { 33863448Sdh155122 netstack_rele(spds->spds_netstack); 33870Sstevel@tonic-gate return (ENOMEM); 33883448Sdh155122 } 33890Sstevel@tonic-gate ss = kmem_zalloc(sizeof (spdsock_t), KM_NOSLEEP); 33900Sstevel@tonic-gate if (ss == NULL) { 33910Sstevel@tonic-gate vmem_free(spdsock_vmem, (void *)(uintptr_t)ssminor, 1); 33923448Sdh155122 netstack_rele(spds->spds_netstack); 33930Sstevel@tonic-gate return (ENOMEM); 33940Sstevel@tonic-gate } 33950Sstevel@tonic-gate 33960Sstevel@tonic-gate ss->spdsock_minor = ssminor; 33970Sstevel@tonic-gate ss->spdsock_state = TS_UNBND; 33980Sstevel@tonic-gate ss->spdsock_dump_req = NULL; 33990Sstevel@tonic-gate 34003448Sdh155122 ss->spdsock_spds = spds; 34013448Sdh155122 34020Sstevel@tonic-gate q->q_ptr = ss; 34030Sstevel@tonic-gate oq->q_ptr = ss; 34040Sstevel@tonic-gate 34053448Sdh155122 q->q_hiwat = spds->spds_recv_hiwat; 34063448Sdh155122 34073448Sdh155122 oq->q_hiwat = spds->spds_xmit_hiwat; 34083448Sdh155122 oq->q_lowat = spds->spds_xmit_lowat; 34090Sstevel@tonic-gate 34100Sstevel@tonic-gate qprocson(q); 34113448Sdh155122 (void) mi_set_sth_hiwat(q, spds->spds_recv_hiwat); 34120Sstevel@tonic-gate 34130Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), ss->spdsock_minor); 34140Sstevel@tonic-gate return (0); 34150Sstevel@tonic-gate } 34160Sstevel@tonic-gate 34170Sstevel@tonic-gate /* 34180Sstevel@tonic-gate * Read-side service procedure, invoked when we get back-enabled 34190Sstevel@tonic-gate * when buffer space becomes available. 34200Sstevel@tonic-gate * 34210Sstevel@tonic-gate * Dump another chunk if we were dumping before; when we finish, kick 34220Sstevel@tonic-gate * the write-side queue in case it's waiting for read queue space. 34230Sstevel@tonic-gate */ 34240Sstevel@tonic-gate void 34250Sstevel@tonic-gate spdsock_rsrv(queue_t *q) 34260Sstevel@tonic-gate { 34270Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 34280Sstevel@tonic-gate 34290Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) 34300Sstevel@tonic-gate spdsock_dump_some(q, ss); 34310Sstevel@tonic-gate 34320Sstevel@tonic-gate if (ss->spdsock_dump_req == NULL) 34330Sstevel@tonic-gate qenable(OTHERQ(q)); 34340Sstevel@tonic-gate } 34350Sstevel@tonic-gate 34360Sstevel@tonic-gate /* 34370Sstevel@tonic-gate * Write-side service procedure, invoked when we defer processing 34380Sstevel@tonic-gate * if another message is received while a dump is in progress. 34390Sstevel@tonic-gate */ 34400Sstevel@tonic-gate void 34410Sstevel@tonic-gate spdsock_wsrv(queue_t *q) 34420Sstevel@tonic-gate { 34430Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 34440Sstevel@tonic-gate mblk_t *mp; 34453448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 34463448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 34470Sstevel@tonic-gate 34480Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) { 34490Sstevel@tonic-gate qenable(OTHERQ(q)); 34500Sstevel@tonic-gate return; 34510Sstevel@tonic-gate } 34520Sstevel@tonic-gate 34530Sstevel@tonic-gate while ((mp = getq(q)) != NULL) { 34543448Sdh155122 if (ipsec_loaded(ipss)) { 34550Sstevel@tonic-gate spdsock_wput(q, mp); 34560Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) 34570Sstevel@tonic-gate return; 34583448Sdh155122 } else if (!ipsec_failed(ipss)) { 34590Sstevel@tonic-gate (void) putq(q, mp); 34600Sstevel@tonic-gate } else { 34610Sstevel@tonic-gate spdsock_error(q, mp, EPFNOSUPPORT, 0); 34620Sstevel@tonic-gate } 34630Sstevel@tonic-gate } 34640Sstevel@tonic-gate } 34650Sstevel@tonic-gate 34660Sstevel@tonic-gate static int 34670Sstevel@tonic-gate spdsock_close(queue_t *q) 34680Sstevel@tonic-gate { 34690Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 34703448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 34710Sstevel@tonic-gate 34720Sstevel@tonic-gate qprocsoff(q); 34730Sstevel@tonic-gate 34740Sstevel@tonic-gate /* Safe assumption. */ 34750Sstevel@tonic-gate ASSERT(ss != NULL); 34760Sstevel@tonic-gate 34770Sstevel@tonic-gate if (ss->spdsock_timeout != 0) 34780Sstevel@tonic-gate (void) quntimeout(q, ss->spdsock_timeout); 34790Sstevel@tonic-gate 34803448Sdh155122 ss3dbg(spds, ("Driver close, PF_POLICY socket is going away.\n")); 34810Sstevel@tonic-gate 34820Sstevel@tonic-gate vmem_free(spdsock_vmem, (void *)(uintptr_t)ss->spdsock_minor, 1); 34833448Sdh155122 netstack_rele(ss->spdsock_spds->spds_netstack); 34840Sstevel@tonic-gate 34850Sstevel@tonic-gate kmem_free(ss, sizeof (spdsock_t)); 34860Sstevel@tonic-gate return (0); 34870Sstevel@tonic-gate } 34880Sstevel@tonic-gate 34890Sstevel@tonic-gate /* 34900Sstevel@tonic-gate * Merge the IPsec algorithms tables with the received algorithm information. 34910Sstevel@tonic-gate */ 34920Sstevel@tonic-gate void 34933448Sdh155122 spdsock_merge_algs(spd_stack_t *spds) 34940Sstevel@tonic-gate { 34950Sstevel@tonic-gate ipsec_alginfo_t *alg, *oalg; 34960Sstevel@tonic-gate ipsec_algtype_t algtype; 34970Sstevel@tonic-gate uint_t algidx, algid, nalgs; 34980Sstevel@tonic-gate crypto_mech_name_t *mechs; 34990Sstevel@tonic-gate uint_t mech_count, mech_idx; 35003448Sdh155122 netstack_t *ns = spds->spds_netstack; 35013448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 35023448Sdh155122 35033448Sdh155122 ASSERT(MUTEX_HELD(&spds->spds_alg_lock)); 35040Sstevel@tonic-gate 35050Sstevel@tonic-gate /* 35060Sstevel@tonic-gate * Get the list of supported mechanisms from the crypto framework. 35070Sstevel@tonic-gate * If a mechanism is supported by KCF, resolve its mechanism 35080Sstevel@tonic-gate * id and mark it as being valid. This operation must be done 35090Sstevel@tonic-gate * without holding alg_lock, since it can cause a provider 35100Sstevel@tonic-gate * module to be loaded and the provider notification callback to 35110Sstevel@tonic-gate * be invoked. 35120Sstevel@tonic-gate */ 35130Sstevel@tonic-gate mechs = crypto_get_mech_list(&mech_count, KM_SLEEP); 35140Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 35150Sstevel@tonic-gate for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) { 35160Sstevel@tonic-gate int algflags = 0; 35170Sstevel@tonic-gate crypto_mech_type_t mt = CRYPTO_MECHANISM_INVALID; 35180Sstevel@tonic-gate 35193448Sdh155122 alg = spds->spds_algs[algtype][algid]; 35203448Sdh155122 if (alg == NULL) 35210Sstevel@tonic-gate continue; 35220Sstevel@tonic-gate 35230Sstevel@tonic-gate /* 35240Sstevel@tonic-gate * The NULL encryption algorithm is a special 35250Sstevel@tonic-gate * case because there are no mechanisms, yet 35260Sstevel@tonic-gate * the algorithm is still valid. 35270Sstevel@tonic-gate */ 35280Sstevel@tonic-gate if (alg->alg_id == SADB_EALG_NULL) { 35290Sstevel@tonic-gate alg->alg_mech_type = CRYPTO_MECHANISM_INVALID; 35300Sstevel@tonic-gate alg->alg_flags = ALG_FLAG_VALID; 35310Sstevel@tonic-gate continue; 35320Sstevel@tonic-gate } 35330Sstevel@tonic-gate 35340Sstevel@tonic-gate for (mech_idx = 0; mech_idx < mech_count; mech_idx++) { 35350Sstevel@tonic-gate if (strncmp(alg->alg_mech_name, mechs[mech_idx], 35360Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME) == 0) { 35370Sstevel@tonic-gate mt = crypto_mech2id(alg->alg_mech_name); 35380Sstevel@tonic-gate ASSERT(mt != CRYPTO_MECHANISM_INVALID); 35390Sstevel@tonic-gate algflags = ALG_FLAG_VALID; 35400Sstevel@tonic-gate break; 35410Sstevel@tonic-gate } 35420Sstevel@tonic-gate } 35430Sstevel@tonic-gate alg->alg_mech_type = mt; 35440Sstevel@tonic-gate alg->alg_flags = algflags; 35450Sstevel@tonic-gate } 35460Sstevel@tonic-gate } 35470Sstevel@tonic-gate 35483448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 35490Sstevel@tonic-gate 35500Sstevel@tonic-gate /* 35510Sstevel@tonic-gate * For each algorithm currently defined, check if it is 35520Sstevel@tonic-gate * present in the new tables created from the SPD_UPDATEALGS 35530Sstevel@tonic-gate * message received from user-space. 35540Sstevel@tonic-gate * Delete the algorithm entries that are currently defined 35550Sstevel@tonic-gate * but not part of the new tables. 35560Sstevel@tonic-gate */ 35570Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 35583448Sdh155122 nalgs = ipss->ipsec_nalgs[algtype]; 35590Sstevel@tonic-gate for (algidx = 0; algidx < nalgs; algidx++) { 35603448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 35613448Sdh155122 if (spds->spds_algs[algtype][algid] == NULL) 35623448Sdh155122 ipsec_alg_unreg(algtype, algid, ns); 35630Sstevel@tonic-gate } 35640Sstevel@tonic-gate } 35650Sstevel@tonic-gate 35660Sstevel@tonic-gate /* 35670Sstevel@tonic-gate * For each algorithm we just received, check if it is 35680Sstevel@tonic-gate * present in the currently defined tables. If it is, swap 35690Sstevel@tonic-gate * the entry with the one we just allocated. 35700Sstevel@tonic-gate * If the new algorithm is not in the current tables, 35710Sstevel@tonic-gate * add it. 35720Sstevel@tonic-gate */ 35730Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 35740Sstevel@tonic-gate for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) { 35753448Sdh155122 alg = spds->spds_algs[algtype][algid]; 35763448Sdh155122 if (alg == NULL) 35770Sstevel@tonic-gate continue; 35780Sstevel@tonic-gate 35793448Sdh155122 if ((oalg = ipss->ipsec_alglists[algtype][algid]) == 35803448Sdh155122 NULL) { 35810Sstevel@tonic-gate /* 35820Sstevel@tonic-gate * New algorithm, add it to the algorithm 35830Sstevel@tonic-gate * table. 35840Sstevel@tonic-gate */ 35853448Sdh155122 ipsec_alg_reg(algtype, alg, ns); 35860Sstevel@tonic-gate } else { 35870Sstevel@tonic-gate /* 35880Sstevel@tonic-gate * Algorithm is already in the table. Swap 35890Sstevel@tonic-gate * the existing entry with the new one. 35900Sstevel@tonic-gate */ 35913448Sdh155122 ipsec_alg_fix_min_max(alg, algtype, ns); 35923448Sdh155122 ipss->ipsec_alglists[algtype][algid] = alg; 35930Sstevel@tonic-gate ipsec_alg_free(oalg); 35940Sstevel@tonic-gate } 35953448Sdh155122 spds->spds_algs[algtype][algid] = NULL; 35960Sstevel@tonic-gate } 35970Sstevel@tonic-gate } 35980Sstevel@tonic-gate 35993448Sdh155122 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 36003448Sdh155122 ipss->ipsec_algs_exec_mode[algtype] = 36013448Sdh155122 spds->spds_algs_exec_mode[algtype]; 36023448Sdh155122 } 36033448Sdh155122 36043448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 36050Sstevel@tonic-gate 36060Sstevel@tonic-gate crypto_free_mech_list(mechs, mech_count); 36070Sstevel@tonic-gate 36083448Sdh155122 ipsecah_algs_changed(ns); 36093448Sdh155122 ipsecesp_algs_changed(ns); 36100Sstevel@tonic-gate } 3611