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 /* 228778SErik.Nordmark@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <sys/param.h> 270Sstevel@tonic-gate #include <sys/types.h> 280Sstevel@tonic-gate #include <sys/stream.h> 290Sstevel@tonic-gate #include <sys/strsubr.h> 300Sstevel@tonic-gate #include <sys/strsun.h> 310Sstevel@tonic-gate #include <sys/stropts.h> 323448Sdh155122 #include <sys/zone.h> 330Sstevel@tonic-gate #include <sys/vnode.h> 340Sstevel@tonic-gate #include <sys/sysmacros.h> 350Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 360Sstevel@tonic-gate #include <sys/tihdr.h> 370Sstevel@tonic-gate #include <sys/ddi.h> 380Sstevel@tonic-gate #include <sys/sunddi.h> 390Sstevel@tonic-gate #include <sys/mkdev.h> 400Sstevel@tonic-gate #include <sys/debug.h> 410Sstevel@tonic-gate #include <sys/kmem.h> 420Sstevel@tonic-gate #include <sys/cmn_err.h> 430Sstevel@tonic-gate #include <sys/suntpi.h> 440Sstevel@tonic-gate #include <sys/policy.h> 4510616SSebastien.Roy@Sun.COM #include <sys/dls.h> 460Sstevel@tonic-gate 470Sstevel@tonic-gate #include <sys/socket.h> 480Sstevel@tonic-gate #include <netinet/in.h> 490Sstevel@tonic-gate #include <net/pfkeyv2.h> 500Sstevel@tonic-gate #include <net/pfpolicy.h> 510Sstevel@tonic-gate 520Sstevel@tonic-gate #include <inet/common.h> 530Sstevel@tonic-gate #include <netinet/ip6.h> 540Sstevel@tonic-gate #include <inet/ip.h> 550Sstevel@tonic-gate #include <inet/ip6.h> 560Sstevel@tonic-gate #include <inet/mi.h> 578348SEric.Yu@Sun.COM #include <inet/proto_set.h> 580Sstevel@tonic-gate #include <inet/nd.h> 593055Sdanmcd #include <inet/ip_if.h> 600Sstevel@tonic-gate #include <inet/optcom.h> 610Sstevel@tonic-gate #include <inet/ipsec_info.h> 620Sstevel@tonic-gate #include <inet/ipsec_impl.h> 630Sstevel@tonic-gate #include <inet/spdsock.h> 640Sstevel@tonic-gate #include <inet/sadb.h> 6510616SSebastien.Roy@Sun.COM #include <inet/iptun.h> 6610616SSebastien.Roy@Sun.COM #include <inet/iptun/iptun_impl.h> 670Sstevel@tonic-gate 680Sstevel@tonic-gate #include <sys/isa_defs.h> 690Sstevel@tonic-gate 704307Spwernau #include <c2/audit.h> 714307Spwernau 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * This is a transport provider for the PF_POLICY IPsec policy 740Sstevel@tonic-gate * management socket, which provides a management interface into the 750Sstevel@tonic-gate * SPD, allowing policy rules to be added, deleted, and queried. 760Sstevel@tonic-gate * 770Sstevel@tonic-gate * This effectively replaces the old private SIOC*IPSECONFIG ioctls 780Sstevel@tonic-gate * with an extensible interface which will hopefully be public some 790Sstevel@tonic-gate * day. 800Sstevel@tonic-gate * 810Sstevel@tonic-gate * See <net/pfpolicy.h> for more details on the protocol. 820Sstevel@tonic-gate * 830Sstevel@tonic-gate * We link against drv/ip and call directly into it to manipulate the 840Sstevel@tonic-gate * SPD; see ipsec_impl.h for the policy data structures and spd.c for 850Sstevel@tonic-gate * the code which maintains them. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * The MT model of this is QPAIR with the addition of some explicit 880Sstevel@tonic-gate * locking to protect system-wide policy data structures. 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate 910Sstevel@tonic-gate static vmem_t *spdsock_vmem; /* for minor numbers. */ 920Sstevel@tonic-gate 930Sstevel@tonic-gate #define ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t)) 940Sstevel@tonic-gate 950Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages (from rts.c...) */ 960Sstevel@tonic-gate static struct T_info_ack spdsock_g_t_info_ack = { 970Sstevel@tonic-gate T_INFO_ACK, 980Sstevel@tonic-gate T_INFINITE, /* TSDU_size. Maximum size messages. */ 990Sstevel@tonic-gate T_INVALID, /* ETSDU_size. No expedited data. */ 1000Sstevel@tonic-gate T_INVALID, /* CDATA_size. No connect data. */ 1010Sstevel@tonic-gate T_INVALID, /* DDATA_size. No disconnect data. */ 1020Sstevel@tonic-gate 0, /* ADDR_size. */ 1030Sstevel@tonic-gate 0, /* OPT_size. No user-settable options */ 1040Sstevel@tonic-gate 64 * 1024, /* TIDU_size. spdsock allows maximum size messages. */ 1050Sstevel@tonic-gate T_COTS, /* SERV_type. spdsock supports connection oriented. */ 1060Sstevel@tonic-gate TS_UNBND, /* CURRENT_state. This is set from spdsock_state. */ 1070Sstevel@tonic-gate (XPG4_1) /* Provider flags */ 1080Sstevel@tonic-gate }; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */ 1113448Sdh155122 typedef struct spdsockparam_s { 1120Sstevel@tonic-gate uint_t spdsock_param_min; 1130Sstevel@tonic-gate uint_t spdsock_param_max; 1140Sstevel@tonic-gate uint_t spdsock_param_value; 1150Sstevel@tonic-gate char *spdsock_param_name; 1160Sstevel@tonic-gate } spdsockparam_t; 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* 1190Sstevel@tonic-gate * Table of NDD variables supported by spdsock. These are loaded into 1200Sstevel@tonic-gate * spdsock_g_nd in spdsock_init_nd. 1210Sstevel@tonic-gate * All of these are alterable, within the min/max values given, at run time. 1220Sstevel@tonic-gate */ 1233448Sdh155122 static spdsockparam_t lcl_param_arr[] = { 1240Sstevel@tonic-gate /* min max value name */ 1250Sstevel@tonic-gate { 4096, 65536, 8192, "spdsock_xmit_hiwat"}, 1260Sstevel@tonic-gate { 0, 65536, 1024, "spdsock_xmit_lowat"}, 1270Sstevel@tonic-gate { 4096, 65536, 8192, "spdsock_recv_hiwat"}, 1280Sstevel@tonic-gate { 65536, 1024*1024*1024, 256*1024, "spdsock_max_buf"}, 1290Sstevel@tonic-gate { 0, 3, 0, "spdsock_debug"}, 1300Sstevel@tonic-gate }; 1313448Sdh155122 #define spds_xmit_hiwat spds_params[0].spdsock_param_value 1323448Sdh155122 #define spds_xmit_lowat spds_params[1].spdsock_param_value 1333448Sdh155122 #define spds_recv_hiwat spds_params[2].spdsock_param_value 1343448Sdh155122 #define spds_max_buf spds_params[3].spdsock_param_value 1353448Sdh155122 #define spds_debug spds_params[4].spdsock_param_value 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate #define ss0dbg(a) printf a 1380Sstevel@tonic-gate /* NOTE: != 0 instead of > 0 so lint doesn't complain. */ 1393448Sdh155122 #define ss1dbg(spds, a) if (spds->spds_debug != 0) printf a 1403448Sdh155122 #define ss2dbg(spds, a) if (spds->spds_debug > 1) printf a 1413448Sdh155122 #define ss3dbg(spds, a) if (spds->spds_debug > 2) printf a 1420Sstevel@tonic-gate 1436430Sdanmcd #define RESET_SPDSOCK_DUMP_POLHEAD(ss, iph) { \ 1446430Sdanmcd ASSERT(RW_READ_HELD(&(iph)->iph_lock)); \ 1456430Sdanmcd (ss)->spdsock_dump_head = (iph); \ 1466430Sdanmcd (ss)->spdsock_dump_gen = (iph)->iph_gen; \ 1476430Sdanmcd (ss)->spdsock_dump_cur_type = 0; \ 1486430Sdanmcd (ss)->spdsock_dump_cur_af = IPSEC_AF_V4; \ 1496430Sdanmcd (ss)->spdsock_dump_cur_rule = NULL; \ 1506430Sdanmcd (ss)->spdsock_dump_count = 0; \ 1516430Sdanmcd (ss)->spdsock_dump_cur_chain = 0; \ 1526430Sdanmcd } 1536430Sdanmcd 1540Sstevel@tonic-gate static int spdsock_close(queue_t *); 1550Sstevel@tonic-gate static int spdsock_open(queue_t *, dev_t *, int, int, cred_t *); 1560Sstevel@tonic-gate static void spdsock_wput(queue_t *, mblk_t *); 1570Sstevel@tonic-gate static void spdsock_wsrv(queue_t *); 1580Sstevel@tonic-gate static void spdsock_rsrv(queue_t *); 1593448Sdh155122 static void *spdsock_stack_init(netstackid_t stackid, netstack_t *ns); 1603448Sdh155122 static void spdsock_stack_fini(netstackid_t stackid, void *arg); 1610Sstevel@tonic-gate static void spdsock_loadcheck(void *); 1623448Sdh155122 static void spdsock_merge_algs(spd_stack_t *); 1633448Sdh155122 static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *); 1643055Sdanmcd static mblk_t *spdsock_dump_next_record(spdsock_t *); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate static struct module_info info = { 1670Sstevel@tonic-gate 5138, "spdsock", 1, INFPSZ, 512, 128 1680Sstevel@tonic-gate }; 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate static struct qinit rinit = { 1710Sstevel@tonic-gate NULL, (pfi_t)spdsock_rsrv, spdsock_open, spdsock_close, 1720Sstevel@tonic-gate NULL, &info 1730Sstevel@tonic-gate }; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate static struct qinit winit = { 1760Sstevel@tonic-gate (pfi_t)spdsock_wput, (pfi_t)spdsock_wsrv, NULL, NULL, NULL, &info 1770Sstevel@tonic-gate }; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate struct streamtab spdsockinfo = { 1800Sstevel@tonic-gate &rinit, &winit 1810Sstevel@tonic-gate }; 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate /* mapping from alg type to protocol number, as per RFC 2407 */ 1840Sstevel@tonic-gate static const uint_t algproto[] = { 1850Sstevel@tonic-gate PROTO_IPSEC_AH, 1860Sstevel@tonic-gate PROTO_IPSEC_ESP, 1870Sstevel@tonic-gate }; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate #define NALGPROTOS (sizeof (algproto) / sizeof (algproto[0])) 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate /* mapping from kernel exec mode to spdsock exec mode */ 1920Sstevel@tonic-gate static const uint_t execmodes[] = { 1930Sstevel@tonic-gate SPD_ALG_EXEC_MODE_SYNC, 1940Sstevel@tonic-gate SPD_ALG_EXEC_MODE_ASYNC 1950Sstevel@tonic-gate }; 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate #define NEXECMODES (sizeof (execmodes) / sizeof (execmodes[0])) 1980Sstevel@tonic-gate 1993055Sdanmcd #define ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1) 2003055Sdanmcd #define ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2) 2013055Sdanmcd 2024307Spwernau #define ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL) 2034307Spwernau 2040Sstevel@tonic-gate /* ARGSUSED */ 2050Sstevel@tonic-gate static int 2060Sstevel@tonic-gate spdsock_param_get(q, mp, cp, cr) 2070Sstevel@tonic-gate queue_t *q; 2080Sstevel@tonic-gate mblk_t *mp; 2090Sstevel@tonic-gate caddr_t cp; 2100Sstevel@tonic-gate cred_t *cr; 2110Sstevel@tonic-gate { 2120Sstevel@tonic-gate spdsockparam_t *spdsockpa = (spdsockparam_t *)cp; 2130Sstevel@tonic-gate uint_t value; 2143448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2153448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2163448Sdh155122 2173448Sdh155122 mutex_enter(&spds->spds_param_lock); 2180Sstevel@tonic-gate value = spdsockpa->spdsock_param_value; 2193448Sdh155122 mutex_exit(&spds->spds_param_lock); 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate (void) mi_mpprintf(mp, "%u", value); 2220Sstevel@tonic-gate return (0); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* This routine sets an NDD variable in a spdsockparam_t structure. */ 2260Sstevel@tonic-gate /* ARGSUSED */ 2270Sstevel@tonic-gate static int 2280Sstevel@tonic-gate spdsock_param_set(q, mp, value, cp, cr) 2290Sstevel@tonic-gate queue_t *q; 2300Sstevel@tonic-gate mblk_t *mp; 2310Sstevel@tonic-gate char *value; 2320Sstevel@tonic-gate caddr_t cp; 2330Sstevel@tonic-gate cred_t *cr; 2340Sstevel@tonic-gate { 2350Sstevel@tonic-gate ulong_t new_value; 2360Sstevel@tonic-gate spdsockparam_t *spdsockpa = (spdsockparam_t *)cp; 2373448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2383448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate /* Convert the value from a string into a long integer. */ 2410Sstevel@tonic-gate if (ddi_strtoul(value, NULL, 10, &new_value) != 0) 2420Sstevel@tonic-gate return (EINVAL); 2430Sstevel@tonic-gate 2443448Sdh155122 mutex_enter(&spds->spds_param_lock); 2450Sstevel@tonic-gate /* 2460Sstevel@tonic-gate * Fail the request if the new value does not lie within the 2470Sstevel@tonic-gate * required bounds. 2480Sstevel@tonic-gate */ 2490Sstevel@tonic-gate if (new_value < spdsockpa->spdsock_param_min || 2500Sstevel@tonic-gate new_value > spdsockpa->spdsock_param_max) { 2513448Sdh155122 mutex_exit(&spds->spds_param_lock); 2520Sstevel@tonic-gate return (EINVAL); 2530Sstevel@tonic-gate } 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate /* Set the new value */ 2560Sstevel@tonic-gate spdsockpa->spdsock_param_value = new_value; 2573448Sdh155122 mutex_exit(&spds->spds_param_lock); 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate return (0); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2623448Sdh155122 /* 2633448Sdh155122 * Initialize at module load time 2643448Sdh155122 */ 2650Sstevel@tonic-gate boolean_t 2660Sstevel@tonic-gate spdsock_ddi_init(void) 2670Sstevel@tonic-gate { 2680Sstevel@tonic-gate spdsock_max_optsize = optcom_max_optsize( 2690Sstevel@tonic-gate spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt); 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1, 2720Sstevel@tonic-gate NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER); 2730Sstevel@tonic-gate 2743448Sdh155122 /* 2753448Sdh155122 * We want to be informed each time a stack is created or 2763448Sdh155122 * destroyed in the kernel, so we can maintain the 2773448Sdh155122 * set of spd_stack_t's. 2783448Sdh155122 */ 2793448Sdh155122 netstack_register(NS_SPDSOCK, spdsock_stack_init, NULL, 2803448Sdh155122 spdsock_stack_fini); 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate return (B_TRUE); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2853448Sdh155122 /* 2863448Sdh155122 * Walk through the param array specified registering each element with the 2873448Sdh155122 * named dispatch handler. 2883448Sdh155122 */ 2893448Sdh155122 static boolean_t 2903448Sdh155122 spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt) 2913448Sdh155122 { 2923448Sdh155122 for (; cnt-- > 0; ssp++) { 2933448Sdh155122 if (ssp->spdsock_param_name != NULL && 2943448Sdh155122 ssp->spdsock_param_name[0]) { 2953448Sdh155122 if (!nd_load(ndp, 2963448Sdh155122 ssp->spdsock_param_name, 2973448Sdh155122 spdsock_param_get, spdsock_param_set, 2983448Sdh155122 (caddr_t)ssp)) { 2993448Sdh155122 nd_free(ndp); 3003448Sdh155122 return (B_FALSE); 3013448Sdh155122 } 3023448Sdh155122 } 3033448Sdh155122 } 3043448Sdh155122 return (B_TRUE); 3053448Sdh155122 } 3063448Sdh155122 3073448Sdh155122 /* 3083448Sdh155122 * Initialize for each stack instance 3093448Sdh155122 */ 3103448Sdh155122 /* ARGSUSED */ 3113448Sdh155122 static void * 3123448Sdh155122 spdsock_stack_init(netstackid_t stackid, netstack_t *ns) 3133448Sdh155122 { 3143448Sdh155122 spd_stack_t *spds; 3153448Sdh155122 spdsockparam_t *ssp; 3163448Sdh155122 3173448Sdh155122 spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP); 3183448Sdh155122 spds->spds_netstack = ns; 3193448Sdh155122 3203448Sdh155122 ASSERT(spds->spds_g_nd == NULL); 3213448Sdh155122 3223448Sdh155122 ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 3233448Sdh155122 spds->spds_params = ssp; 3243448Sdh155122 bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr)); 3253448Sdh155122 3263448Sdh155122 (void) spdsock_param_register(&spds->spds_g_nd, ssp, 3273448Sdh155122 A_CNT(lcl_param_arr)); 3283448Sdh155122 3293448Sdh155122 mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL); 3303448Sdh155122 mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL); 3313448Sdh155122 3323448Sdh155122 return (spds); 3333448Sdh155122 } 3343448Sdh155122 3350Sstevel@tonic-gate void 3360Sstevel@tonic-gate spdsock_ddi_destroy(void) 3370Sstevel@tonic-gate { 3380Sstevel@tonic-gate vmem_destroy(spdsock_vmem); 3393448Sdh155122 3403448Sdh155122 netstack_unregister(NS_SPDSOCK); 3413448Sdh155122 } 3423448Sdh155122 3433448Sdh155122 /* ARGSUSED */ 3443448Sdh155122 static void 3453448Sdh155122 spdsock_stack_fini(netstackid_t stackid, void *arg) 3463448Sdh155122 { 3473448Sdh155122 spd_stack_t *spds = (spd_stack_t *)arg; 3483448Sdh155122 3496430Sdanmcd freemsg(spds->spds_mp_algs); 3503448Sdh155122 mutex_destroy(&spds->spds_param_lock); 3513448Sdh155122 mutex_destroy(&spds->spds_alg_lock); 3523448Sdh155122 nd_free(&spds->spds_g_nd); 3533448Sdh155122 kmem_free(spds->spds_params, sizeof (lcl_param_arr)); 3543448Sdh155122 spds->spds_params = NULL; 3553448Sdh155122 3563448Sdh155122 kmem_free(spds, sizeof (*spds)); 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate /* 3600Sstevel@tonic-gate * NOTE: large quantities of this should be shared with keysock. 3610Sstevel@tonic-gate * Would be nice to combine some of this into a common module, but 3620Sstevel@tonic-gate * not possible given time pressures. 3630Sstevel@tonic-gate */ 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate /* 3660Sstevel@tonic-gate * High-level reality checking of extensions. 3670Sstevel@tonic-gate */ 3680Sstevel@tonic-gate /* ARGSUSED */ /* XXX */ 3690Sstevel@tonic-gate static boolean_t 3700Sstevel@tonic-gate ext_check(spd_ext_t *ext) 3710Sstevel@tonic-gate { 3723055Sdanmcd spd_if_t *tunname = (spd_if_t *)ext; 3733055Sdanmcd int i; 3743055Sdanmcd char *idstr; 3753055Sdanmcd 3763055Sdanmcd if (ext->spd_ext_type == SPD_EXT_TUN_NAME) { 3773055Sdanmcd /* (NOTE: Modified from SADB_EXT_IDENTITY..) */ 3783055Sdanmcd 3793055Sdanmcd /* 3803055Sdanmcd * Make sure the strings in these identities are 3813055Sdanmcd * null-terminated. Let's "proactively" null-terminate the 3823055Sdanmcd * string at the last byte if it's not terminated sooner. 3833055Sdanmcd */ 3843055Sdanmcd i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t); 3853055Sdanmcd idstr = (char *)(tunname + 1); 3863055Sdanmcd while (*idstr != '\0' && i > 0) { 3873055Sdanmcd i--; 3883055Sdanmcd idstr++; 3893055Sdanmcd } 3903055Sdanmcd if (i == 0) { 3913055Sdanmcd /* 3923055Sdanmcd * I.e., if the bozo user didn't NULL-terminate the 3933055Sdanmcd * string... 3943055Sdanmcd */ 3953055Sdanmcd idstr--; 3963055Sdanmcd *idstr = '\0'; 3973055Sdanmcd } 3983055Sdanmcd } 3990Sstevel@tonic-gate return (B_TRUE); /* For now... */ 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* Return values for spdsock_get_ext(). */ 4050Sstevel@tonic-gate #define KGE_OK 0 4060Sstevel@tonic-gate #define KGE_DUP 1 4070Sstevel@tonic-gate #define KGE_UNK 2 4080Sstevel@tonic-gate #define KGE_LEN 3 4090Sstevel@tonic-gate #define KGE_CHK 4 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate /* 4120Sstevel@tonic-gate * Parse basic extension headers and return in the passed-in pointer vector. 4130Sstevel@tonic-gate * Return values include: 4140Sstevel@tonic-gate * 4150Sstevel@tonic-gate * KGE_OK Everything's nice and parsed out. 4160Sstevel@tonic-gate * If there are no extensions, place NULL in extv[0]. 4170Sstevel@tonic-gate * KGE_DUP There is a duplicate extension. 4180Sstevel@tonic-gate * First instance in appropriate bin. First duplicate in 4190Sstevel@tonic-gate * extv[0]. 4200Sstevel@tonic-gate * KGE_UNK Unknown extension type encountered. extv[0] contains 4210Sstevel@tonic-gate * unknown header. 4220Sstevel@tonic-gate * KGE_LEN Extension length error. 4230Sstevel@tonic-gate * KGE_CHK High-level reality check failed on specific extension. 4240Sstevel@tonic-gate * 4250Sstevel@tonic-gate * My apologies for some of the pointer arithmetic in here. I'm thinking 4260Sstevel@tonic-gate * like an assembly programmer, yet trying to make the compiler happy. 4270Sstevel@tonic-gate */ 4280Sstevel@tonic-gate static int 4290Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize) 4300Sstevel@tonic-gate { 4310Sstevel@tonic-gate bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1)); 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* Use extv[0] as the "current working pointer". */ 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate extv[0] = (spd_ext_t *)(basehdr + 1); 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) { 4380Sstevel@tonic-gate /* Check for unknown headers. */ 4390Sstevel@tonic-gate if (extv[0]->spd_ext_type == 0 || 4400Sstevel@tonic-gate extv[0]->spd_ext_type > SPD_EXT_MAX) 4410Sstevel@tonic-gate return (KGE_UNK); 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* 4440Sstevel@tonic-gate * Check length. Use uint64_t because extlen is in units 4450Sstevel@tonic-gate * of 64-bit words. If length goes beyond the msgsize, 4460Sstevel@tonic-gate * return an error. (Zero length also qualifies here.) 4470Sstevel@tonic-gate */ 4480Sstevel@tonic-gate if (extv[0]->spd_ext_len == 0 || 4490Sstevel@tonic-gate (void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) > 4500Sstevel@tonic-gate (void *)((uint8_t *)basehdr + msgsize)) 4510Sstevel@tonic-gate return (KGE_LEN); 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate /* Check for redundant headers. */ 4540Sstevel@tonic-gate if (extv[extv[0]->spd_ext_type] != NULL) 4550Sstevel@tonic-gate return (KGE_DUP); 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate /* 4580Sstevel@tonic-gate * Reality check the extension if possible at the spdsock 4590Sstevel@tonic-gate * level. 4600Sstevel@tonic-gate */ 4610Sstevel@tonic-gate if (!ext_check(extv[0])) 4620Sstevel@tonic-gate return (KGE_CHK); 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate /* If I make it here, assign the appropriate bin. */ 4650Sstevel@tonic-gate extv[extv[0]->spd_ext_type] = extv[0]; 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate /* Advance pointer (See above for uint64_t ptr reasoning.) */ 4680Sstevel@tonic-gate extv[0] = (spd_ext_t *) 4690Sstevel@tonic-gate ((uint64_t *)extv[0] + extv[0]->spd_ext_len); 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate /* Everything's cool. */ 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate /* 4750Sstevel@tonic-gate * If extv[0] == NULL, then there are no extension headers in this 4760Sstevel@tonic-gate * message. Ensure that this is the case. 4770Sstevel@tonic-gate */ 4780Sstevel@tonic-gate if (extv[0] == (spd_ext_t *)(basehdr + 1)) 4790Sstevel@tonic-gate extv[0] = NULL; 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate return (KGE_OK); 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate static const int bad_ext_diag[] = { 4850Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_LCLPORT, 4860Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_REMPORT, 4870Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_PROTO, 4880Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_LCLADDR, 4890Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_REMADDR, 4900Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_ACTION, 4910Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_RULE, 4920Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_RULESET, 4930Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE 4940Sstevel@tonic-gate }; 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate static const int dup_ext_diag[] = { 4970Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_LCLPORT, 4980Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_REMPORT, 4990Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_PROTO, 5000Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_LCLADDR, 5010Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_REMADDR, 5020Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_ACTION, 5030Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_RULE, 5040Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_RULESET, 5050Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE 5060Sstevel@tonic-gate }; 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate /* 5090Sstevel@tonic-gate * Transmit a PF_POLICY error message to the instance either pointed to 5100Sstevel@tonic-gate * by ks, the instance with serial number serial, or more, depending. 5110Sstevel@tonic-gate * 5120Sstevel@tonic-gate * The faulty message (or a reasonable facsimile thereof) is in mp. 5130Sstevel@tonic-gate * This function will free mp or recycle it for delivery, thereby causing 5140Sstevel@tonic-gate * the stream head to free it. 5150Sstevel@tonic-gate */ 5160Sstevel@tonic-gate static void 5170Sstevel@tonic-gate spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic) 5180Sstevel@tonic-gate { 5190Sstevel@tonic-gate spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate ASSERT(mp->b_datap->db_type == M_DATA); 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate if (spmsg->spd_msg_type < SPD_MIN || 5240Sstevel@tonic-gate spmsg->spd_msg_type > SPD_MAX) 5250Sstevel@tonic-gate spmsg->spd_msg_type = SPD_RESERVED; 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate /* 5280Sstevel@tonic-gate * Strip out extension headers. 5290Sstevel@tonic-gate */ 5300Sstevel@tonic-gate ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim); 5310Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (*spmsg); 5320Sstevel@tonic-gate spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t)); 5330Sstevel@tonic-gate spmsg->spd_msg_errno = (uint8_t)error; 5340Sstevel@tonic-gate spmsg->spd_msg_diagnostic = (uint16_t)diagnostic; 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate qreply(q, mp); 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate static void 5400Sstevel@tonic-gate spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic) 5410Sstevel@tonic-gate { 5420Sstevel@tonic-gate spdsock_error(q, mp, EINVAL, diagnostic); 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate static void 5460Sstevel@tonic-gate spd_echo(queue_t *q, mblk_t *mp) 5470Sstevel@tonic-gate { 5480Sstevel@tonic-gate qreply(q, mp); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate 5513055Sdanmcd /* 5523055Sdanmcd * Do NOT consume a reference to itp. 5533055Sdanmcd */ 5543448Sdh155122 /*ARGSUSED*/ 5550Sstevel@tonic-gate static void 5563448Sdh155122 spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns) 5573055Sdanmcd { 5583055Sdanmcd boolean_t active = (boolean_t)cookie; 5593055Sdanmcd ipsec_policy_head_t *iph; 5603055Sdanmcd 5613055Sdanmcd iph = active ? itp->itp_policy : itp->itp_inactive; 5623055Sdanmcd IPPH_REFHOLD(iph); 5633055Sdanmcd mutex_enter(&itp->itp_lock); 5643448Sdh155122 spdsock_flush_one(iph, ns); 5653055Sdanmcd if (active) 5663055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 5673055Sdanmcd else 5683055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 5693055Sdanmcd mutex_exit(&itp->itp_lock); 5703055Sdanmcd } 5713055Sdanmcd 5723055Sdanmcd /* 5733055Sdanmcd * Clear out one polhead. 5743055Sdanmcd */ 5753055Sdanmcd static void 5763448Sdh155122 spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns) 5770Sstevel@tonic-gate { 5780Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_WRITER); 5793448Sdh155122 ipsec_polhead_flush(iph, ns); 5800Sstevel@tonic-gate rw_exit(&iph->iph_lock); 5813448Sdh155122 IPPH_REFRELE(iph, ns); 5823055Sdanmcd } 5833055Sdanmcd 5843055Sdanmcd static void 5854307Spwernau spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, ipsec_tun_pol_t *itp, 5864307Spwernau mblk_t *mp) 5873055Sdanmcd { 5883055Sdanmcd boolean_t active; 5893448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 5906430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 5913055Sdanmcd 5923055Sdanmcd if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) { 5936430Sdanmcd spdsock_flush_one(iph, ns); 5944307Spwernau if (audit_active) { 5954307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 5968778SErik.Nordmark@Sun.COM cred_t *cr; 5978778SErik.Nordmark@Sun.COM pid_t cpid; 5988778SErik.Nordmark@Sun.COM 5998778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 6004307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 6018778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLUSH, cr, ns, 6028778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, 0, cpid); 6034307Spwernau } 6043055Sdanmcd } else { 6053055Sdanmcd active = (iph == ALL_ACTIVE_POLHEADS); 6063055Sdanmcd 6073055Sdanmcd /* First flush the global policy. */ 6083448Sdh155122 spdsock_flush_one(active ? ipsec_system_policy(ns) : 6093448Sdh155122 ipsec_inactive_policy(ns), ns); 6104307Spwernau if (audit_active) { 6118778SErik.Nordmark@Sun.COM cred_t *cr; 6128778SErik.Nordmark@Sun.COM pid_t cpid; 6138778SErik.Nordmark@Sun.COM 6148778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 6158778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLUSH, cr, ns, NULL, 6168778SErik.Nordmark@Sun.COM active, 0, cpid); 6174307Spwernau } 6183055Sdanmcd /* Then flush every tunnel's appropriate one. */ 6196430Sdanmcd itp_walk(spdsock_flush_node, (void *)active, ns); 6208778SErik.Nordmark@Sun.COM if (audit_active) { 6218778SErik.Nordmark@Sun.COM cred_t *cr; 6228778SErik.Nordmark@Sun.COM pid_t cpid; 6238778SErik.Nordmark@Sun.COM 6248778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 6258778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLUSH, cr, ns, 6268778SErik.Nordmark@Sun.COM "all tunnels", active, 0, cpid); 6278778SErik.Nordmark@Sun.COM } 6283055Sdanmcd } 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate spd_echo(q, mp); 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate static boolean_t 6340Sstevel@tonic-gate spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag) 6350Sstevel@tonic-gate { 6360Sstevel@tonic-gate bzero(sel, sizeof (*sel)); 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate if (extv[SPD_EXT_PROTO] != NULL) { 6390Sstevel@tonic-gate struct spd_proto *pr = 6400Sstevel@tonic-gate (struct spd_proto *)extv[SPD_EXT_PROTO]; 6410Sstevel@tonic-gate sel->ipsl_proto = pr->spd_proto_number; 6420Sstevel@tonic-gate sel->ipsl_valid |= IPSL_PROTOCOL; 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate if (extv[SPD_EXT_LCLPORT] != NULL) { 6450Sstevel@tonic-gate struct spd_portrange *pr = 6460Sstevel@tonic-gate (struct spd_portrange *)extv[SPD_EXT_LCLPORT]; 6470Sstevel@tonic-gate sel->ipsl_lport = pr->spd_ports_minport; 6480Sstevel@tonic-gate sel->ipsl_valid |= IPSL_LOCAL_PORT; 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate if (extv[SPD_EXT_REMPORT] != NULL) { 6510Sstevel@tonic-gate struct spd_portrange *pr = 6520Sstevel@tonic-gate (struct spd_portrange *)extv[SPD_EXT_REMPORT]; 6530Sstevel@tonic-gate sel->ipsl_rport = pr->spd_ports_minport; 6540Sstevel@tonic-gate sel->ipsl_valid |= IPSL_REMOTE_PORT; 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) { 6580Sstevel@tonic-gate struct spd_typecode *tc= 6590Sstevel@tonic-gate (struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE]; 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate sel->ipsl_valid |= IPSL_ICMP_TYPE; 6620Sstevel@tonic-gate sel->ipsl_icmp_type = tc->spd_typecode_type; 6630Sstevel@tonic-gate if (tc->spd_typecode_type_end < tc->spd_typecode_type) 6640Sstevel@tonic-gate sel->ipsl_icmp_type_end = tc->spd_typecode_type; 6650Sstevel@tonic-gate else 6660Sstevel@tonic-gate sel->ipsl_icmp_type_end = tc->spd_typecode_type_end; 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate if (tc->spd_typecode_code != 255) { 6690Sstevel@tonic-gate sel->ipsl_valid |= IPSL_ICMP_CODE; 6700Sstevel@tonic-gate sel->ipsl_icmp_code = tc->spd_typecode_code; 6710Sstevel@tonic-gate if (tc->spd_typecode_code_end < tc->spd_typecode_code) 6720Sstevel@tonic-gate sel->ipsl_icmp_code_end = tc->spd_typecode_code; 6730Sstevel@tonic-gate else 6740Sstevel@tonic-gate sel->ipsl_icmp_code_end = 6750Sstevel@tonic-gate tc->spd_typecode_code_end; 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate #define ADDR2SEL(sel, extv, field, pfield, extn, bit) \ 6790Sstevel@tonic-gate if ((extv)[(extn)] != NULL) { \ 6800Sstevel@tonic-gate uint_t addrlen; \ 6810Sstevel@tonic-gate struct spd_address *ap = \ 6820Sstevel@tonic-gate (struct spd_address *)((extv)[(extn)]); \ 6830Sstevel@tonic-gate addrlen = (ap->spd_address_af == AF_INET6) ? \ 6840Sstevel@tonic-gate IPV6_ADDR_LEN : IP_ADDR_LEN; \ 6850Sstevel@tonic-gate if (SPD_64TO8(ap->spd_address_len) < \ 6860Sstevel@tonic-gate (addrlen + sizeof (*ap))) { \ 6870Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN; \ 6880Sstevel@tonic-gate return (B_FALSE); \ 6890Sstevel@tonic-gate } \ 6900Sstevel@tonic-gate bcopy((ap+1), &((sel)->field), addrlen); \ 6910Sstevel@tonic-gate (sel)->pfield = ap->spd_address_prefixlen; \ 6920Sstevel@tonic-gate (sel)->ipsl_valid |= (bit); \ 6930Sstevel@tonic-gate (sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ? \ 6940Sstevel@tonic-gate IPSL_IPV6 : IPSL_IPV4; \ 6950Sstevel@tonic-gate } 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen, 6980Sstevel@tonic-gate SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR); 6990Sstevel@tonic-gate ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen, 7000Sstevel@tonic-gate SPD_EXT_REMADDR, IPSL_REMOTE_ADDR); 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) == 7030Sstevel@tonic-gate (IPSL_IPV6|IPSL_IPV4)) { 7040Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_MIXED_AF; 7050Sstevel@tonic-gate return (B_FALSE); 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate #undef ADDR2SEL 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate return (B_TRUE); 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate static boolean_t 7140Sstevel@tonic-gate spd_convert_type(uint32_t type, ipsec_act_t *act) 7150Sstevel@tonic-gate { 7160Sstevel@tonic-gate switch (type) { 7170Sstevel@tonic-gate case SPD_ACTTYPE_DROP: 7180Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_DISCARD; 7190Sstevel@tonic-gate return (B_TRUE); 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate case SPD_ACTTYPE_PASS: 7220Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_CLEAR; 7230Sstevel@tonic-gate return (B_TRUE); 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate case SPD_ACTTYPE_IPSEC: 7260Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_APPLY; 7270Sstevel@tonic-gate return (B_TRUE); 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate return (B_FALSE); 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate static boolean_t 7330Sstevel@tonic-gate spd_convert_flags(uint32_t flags, ipsec_act_t *act) 7340Sstevel@tonic-gate { 7350Sstevel@tonic-gate /* 7360Sstevel@tonic-gate * Note use of !! for boolean canonicalization. 7370Sstevel@tonic-gate */ 7380Sstevel@tonic-gate act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH); 7390Sstevel@tonic-gate act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP); 7400Sstevel@tonic-gate act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA); 7410Sstevel@tonic-gate act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE); 7420Sstevel@tonic-gate act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE); 7430Sstevel@tonic-gate return (B_TRUE); 7440Sstevel@tonic-gate } 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate static void 7470Sstevel@tonic-gate spdsock_reset_act(ipsec_act_t *act) 7480Sstevel@tonic-gate { 7490Sstevel@tonic-gate bzero(act, sizeof (*act)); 7500Sstevel@tonic-gate act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS; 7510Sstevel@tonic-gate act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS; 7520Sstevel@tonic-gate act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS; 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate /* 7560Sstevel@tonic-gate * Sanity check action against reality, and shrink-wrap key sizes.. 7570Sstevel@tonic-gate */ 7580Sstevel@tonic-gate static boolean_t 7593448Sdh155122 spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag, 7603448Sdh155122 spd_stack_t *spds) 7610Sstevel@tonic-gate { 7623055Sdanmcd if (tunnel_polhead && act->ipa_apply.ipp_use_unique) { 7633055Sdanmcd *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7643055Sdanmcd return (B_FALSE); 7653055Sdanmcd } 7660Sstevel@tonic-gate if ((act->ipa_type != IPSEC_ACT_APPLY) && 7670Sstevel@tonic-gate (act->ipa_apply.ipp_use_ah || 7685240Snordmark act->ipa_apply.ipp_use_esp || 7695240Snordmark act->ipa_apply.ipp_use_espa || 7705240Snordmark act->ipa_apply.ipp_use_se || 7715240Snordmark act->ipa_apply.ipp_use_unique)) { 7720Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7730Sstevel@tonic-gate return (B_FALSE); 7740Sstevel@tonic-gate } 7750Sstevel@tonic-gate if ((act->ipa_type == IPSEC_ACT_APPLY) && 7760Sstevel@tonic-gate !act->ipa_apply.ipp_use_ah && 7770Sstevel@tonic-gate !act->ipa_apply.ipp_use_esp) { 7780Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7790Sstevel@tonic-gate return (B_FALSE); 7800Sstevel@tonic-gate } 7813448Sdh155122 return (ipsec_check_action(act, diag, spds->spds_netstack)); 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate /* 7850Sstevel@tonic-gate * We may be short a few error checks here.. 7860Sstevel@tonic-gate */ 7870Sstevel@tonic-gate static boolean_t 7880Sstevel@tonic-gate spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp, 7893448Sdh155122 int *diag, spd_stack_t *spds) 7900Sstevel@tonic-gate { 7910Sstevel@tonic-gate struct spd_ext_actions *sactp = 7920Sstevel@tonic-gate (struct spd_ext_actions *)extv[SPD_EXT_ACTION]; 7930Sstevel@tonic-gate ipsec_act_t act, *actp, *endactp; 7940Sstevel@tonic-gate struct spd_attribute *attrp, *endattrp; 7950Sstevel@tonic-gate uint64_t *endp; 7960Sstevel@tonic-gate int nact; 7973055Sdanmcd boolean_t tunnel_polhead; 7983055Sdanmcd 7993055Sdanmcd tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL && 8003055Sdanmcd (((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags & 8015240Snordmark SPD_RULE_FLAG_TUNNEL)); 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate *actpp = NULL; 8040Sstevel@tonic-gate *nactp = 0; 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate if (sactp == NULL) { 8070Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_NO_ACTION_EXT; 8080Sstevel@tonic-gate return (B_FALSE); 8090Sstevel@tonic-gate } 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate /* 8120Sstevel@tonic-gate * Parse the "action" extension and convert into an action chain. 8130Sstevel@tonic-gate */ 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate nact = sactp->spd_actions_count; 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate endp = (uint64_t *)sactp; 8180Sstevel@tonic-gate endp += sactp->spd_actions_len; 8190Sstevel@tonic-gate endattrp = (struct spd_attribute *)endp; 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP); 8220Sstevel@tonic-gate if (actp == NULL) { 8230Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_NO_MEM; 8240Sstevel@tonic-gate return (B_FALSE); 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate *actpp = actp; 8270Sstevel@tonic-gate *nactp = nact; 8280Sstevel@tonic-gate endactp = actp + nact; 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate spdsock_reset_act(&act); 8310Sstevel@tonic-gate attrp = (struct spd_attribute *)(&sactp[1]); 8320Sstevel@tonic-gate 8330Sstevel@tonic-gate for (; attrp < endattrp; attrp++) { 8340Sstevel@tonic-gate switch (attrp->spd_attr_tag) { 8350Sstevel@tonic-gate case SPD_ATTR_NOP: 8360Sstevel@tonic-gate break; 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate case SPD_ATTR_EMPTY: 8390Sstevel@tonic-gate spdsock_reset_act(&act); 8400Sstevel@tonic-gate break; 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate case SPD_ATTR_END: 8430Sstevel@tonic-gate attrp = endattrp; 8440Sstevel@tonic-gate /* FALLTHRU */ 8450Sstevel@tonic-gate case SPD_ATTR_NEXT: 8460Sstevel@tonic-gate if (actp >= endactp) { 8470Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT; 8480Sstevel@tonic-gate goto fail; 8490Sstevel@tonic-gate } 8503448Sdh155122 if (!spdsock_check_action(&act, tunnel_polhead, 8513448Sdh155122 diag, spds)) 8520Sstevel@tonic-gate goto fail; 8530Sstevel@tonic-gate *actp++ = act; 8543055Sdanmcd spdsock_reset_act(&act); 8550Sstevel@tonic-gate break; 8560Sstevel@tonic-gate 8570Sstevel@tonic-gate case SPD_ATTR_TYPE: 8580Sstevel@tonic-gate if (!spd_convert_type(attrp->spd_attr_value, &act)) { 8590Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE; 8600Sstevel@tonic-gate goto fail; 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate break; 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate case SPD_ATTR_FLAGS: 8653055Sdanmcd if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) { 8663055Sdanmcd /* 8673055Sdanmcd * Set "sa unique" for transport-mode 8683055Sdanmcd * tunnels whether we want to or not. 8693055Sdanmcd */ 8703055Sdanmcd attrp->spd_attr_value |= SPD_APPLY_UNIQUE; 8713055Sdanmcd } 8720Sstevel@tonic-gate if (!spd_convert_flags(attrp->spd_attr_value, &act)) { 8730Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS; 8740Sstevel@tonic-gate goto fail; 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate break; 8770Sstevel@tonic-gate 8780Sstevel@tonic-gate case SPD_ATTR_AH_AUTH: 8793055Sdanmcd if (attrp->spd_attr_value == 0) { 8803055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG; 8813055Sdanmcd goto fail; 8823055Sdanmcd } 8830Sstevel@tonic-gate act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value; 8840Sstevel@tonic-gate break; 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate case SPD_ATTR_ESP_ENCR: 8873055Sdanmcd if (attrp->spd_attr_value == 0) { 8883055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG; 8893055Sdanmcd goto fail; 8903055Sdanmcd } 8910Sstevel@tonic-gate act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value; 8920Sstevel@tonic-gate break; 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate case SPD_ATTR_ESP_AUTH: 8953055Sdanmcd if (attrp->spd_attr_value == 0) { 8963055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG; 8973055Sdanmcd goto fail; 8983055Sdanmcd } 8990Sstevel@tonic-gate act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value; 9000Sstevel@tonic-gate break; 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate case SPD_ATTR_ENCR_MINBITS: 9030Sstevel@tonic-gate act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value; 9040Sstevel@tonic-gate break; 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate case SPD_ATTR_ENCR_MAXBITS: 9070Sstevel@tonic-gate act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value; 9080Sstevel@tonic-gate break; 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate case SPD_ATTR_AH_MINBITS: 9110Sstevel@tonic-gate act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value; 9120Sstevel@tonic-gate break; 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate case SPD_ATTR_AH_MAXBITS: 9150Sstevel@tonic-gate act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value; 9160Sstevel@tonic-gate break; 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate case SPD_ATTR_ESPA_MINBITS: 9190Sstevel@tonic-gate act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value; 9200Sstevel@tonic-gate break; 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate case SPD_ATTR_ESPA_MAXBITS: 9230Sstevel@tonic-gate act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value; 9240Sstevel@tonic-gate break; 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate case SPD_ATTR_LIFE_SOFT_TIME: 9270Sstevel@tonic-gate case SPD_ATTR_LIFE_HARD_TIME: 9280Sstevel@tonic-gate case SPD_ATTR_LIFE_SOFT_BYTES: 9290Sstevel@tonic-gate case SPD_ATTR_LIFE_HARD_BYTES: 9300Sstevel@tonic-gate break; 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate case SPD_ATTR_KM_PROTO: 9330Sstevel@tonic-gate act.ipa_apply.ipp_km_proto = attrp->spd_attr_value; 9340Sstevel@tonic-gate break; 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate case SPD_ATTR_KM_COOKIE: 9370Sstevel@tonic-gate act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value; 9380Sstevel@tonic-gate break; 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate case SPD_ATTR_REPLAY_DEPTH: 9410Sstevel@tonic-gate act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value; 9420Sstevel@tonic-gate break; 9430Sstevel@tonic-gate } 9440Sstevel@tonic-gate } 9450Sstevel@tonic-gate if (actp != endactp) { 9460Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT; 9470Sstevel@tonic-gate goto fail; 9480Sstevel@tonic-gate } 9490Sstevel@tonic-gate 9500Sstevel@tonic-gate return (B_TRUE); 9510Sstevel@tonic-gate fail: 9520Sstevel@tonic-gate ipsec_actvec_free(*actpp, nact); 9530Sstevel@tonic-gate *actpp = NULL; 9540Sstevel@tonic-gate return (B_FALSE); 9550Sstevel@tonic-gate } 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate typedef struct 9580Sstevel@tonic-gate { 9590Sstevel@tonic-gate ipsec_policy_t *pol; 9600Sstevel@tonic-gate int dir; 9610Sstevel@tonic-gate } tmprule_t; 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate static int 9640Sstevel@tonic-gate mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule, 9650Sstevel@tonic-gate ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af, 9663448Sdh155122 tmprule_t **rp, uint64_t *index, spd_stack_t *spds) 9670Sstevel@tonic-gate { 9680Sstevel@tonic-gate ipsec_policy_t *pol; 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4); 9710Sstevel@tonic-gate sel->ipsl_valid |= af; 9720Sstevel@tonic-gate 9733055Sdanmcd pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority, 9743448Sdh155122 index, spds->spds_netstack); 9750Sstevel@tonic-gate if (pol == NULL) 9760Sstevel@tonic-gate return (ENOMEM); 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate (*rp)->pol = pol; 9790Sstevel@tonic-gate (*rp)->dir = dir; 9800Sstevel@tonic-gate (*rp)++; 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate if (!ipsec_check_policy(iph, pol, dir)) 9830Sstevel@tonic-gate return (EEXIST); 9840Sstevel@tonic-gate 9850Sstevel@tonic-gate rule->spd_rule_index = pol->ipsp_index; 9860Sstevel@tonic-gate return (0); 9870Sstevel@tonic-gate } 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate static int 9900Sstevel@tonic-gate mkrulepair(ipsec_policy_head_t *iph, struct spd_rule *rule, 9910Sstevel@tonic-gate ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t afs, 9923448Sdh155122 tmprule_t **rp, uint64_t *index, spd_stack_t *spds) 9930Sstevel@tonic-gate { 9940Sstevel@tonic-gate int error; 9950Sstevel@tonic-gate 9960Sstevel@tonic-gate if (afs & IPSL_IPV4) { 9973055Sdanmcd error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV4, rp, 9983448Sdh155122 index, spds); 9990Sstevel@tonic-gate if (error != 0) 10000Sstevel@tonic-gate return (error); 10010Sstevel@tonic-gate } 10020Sstevel@tonic-gate if (afs & IPSL_IPV6) { 10033055Sdanmcd error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV6, rp, 10043448Sdh155122 index, spds); 10050Sstevel@tonic-gate if (error != 0) 10060Sstevel@tonic-gate return (error); 10070Sstevel@tonic-gate } 10080Sstevel@tonic-gate return (0); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate static void 10133055Sdanmcd spdsock_addrule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 10143055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 10150Sstevel@tonic-gate { 10160Sstevel@tonic-gate ipsec_selkey_t sel; 10170Sstevel@tonic-gate ipsec_act_t *actp; 10180Sstevel@tonic-gate uint_t nact; 10193055Sdanmcd int diag = 0, error, afs; 10200Sstevel@tonic-gate struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE]; 10210Sstevel@tonic-gate tmprule_t rules[4], *rulep = &rules[0]; 10223055Sdanmcd boolean_t tunnel_mode, empty_itp, active; 10233055Sdanmcd uint64_t *index = (itp == NULL) ? NULL : &itp->itp_next_policy_index; 10243448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 10253448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate if (rule == NULL) { 10280Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT); 10294307Spwernau if (audit_active) { 10304307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 10318778SErik.Nordmark@Sun.COM cred_t *cr; 10328778SErik.Nordmark@Sun.COM pid_t cpid; 10338778SErik.Nordmark@Sun.COM 10348778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 10354307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 10368778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_ADDRULE, cr, 10374307Spwernau spds->spds_netstack, ITP_NAME(itp), active, 10388778SErik.Nordmark@Sun.COM SPD_DIAGNOSTIC_NO_RULE_EXT, cpid); 10394307Spwernau } 10400Sstevel@tonic-gate return; 10410Sstevel@tonic-gate } 10420Sstevel@tonic-gate 10433055Sdanmcd tunnel_mode = (rule->spd_rule_flags & SPD_RULE_FLAG_TUNNEL); 10443055Sdanmcd 10453055Sdanmcd if (itp != NULL) { 10463055Sdanmcd mutex_enter(&itp->itp_lock); 10473055Sdanmcd ASSERT(itp->itp_policy == iph || itp->itp_inactive == iph); 10483055Sdanmcd active = (itp->itp_policy == iph); 10493055Sdanmcd if (ITP_P_ISACTIVE(itp, iph)) { 10503055Sdanmcd /* Check for mix-and-match of tunnel/transport. */ 10513055Sdanmcd if ((tunnel_mode && !ITP_P_ISTUNNEL(itp, iph)) || 10523055Sdanmcd (!tunnel_mode && ITP_P_ISTUNNEL(itp, iph))) { 10533055Sdanmcd mutex_exit(&itp->itp_lock); 10543055Sdanmcd spdsock_error(q, mp, EBUSY, 0); 10553055Sdanmcd return; 10563055Sdanmcd } 10573055Sdanmcd empty_itp = B_FALSE; 10583055Sdanmcd } else { 10593055Sdanmcd empty_itp = B_TRUE; 10603055Sdanmcd itp->itp_flags = active ? ITPF_P_ACTIVE : ITPF_I_ACTIVE; 10613055Sdanmcd if (tunnel_mode) 10623055Sdanmcd itp->itp_flags |= active ? ITPF_P_TUNNEL : 10633055Sdanmcd ITPF_I_TUNNEL; 10643055Sdanmcd } 10653055Sdanmcd } else { 10663055Sdanmcd empty_itp = B_FALSE; 10673055Sdanmcd } 10683055Sdanmcd 10690Sstevel@tonic-gate if (rule->spd_rule_index != 0) { 10703055Sdanmcd diag = SPD_DIAGNOSTIC_INVALID_RULE_INDEX; 10713055Sdanmcd error = EINVAL; 10723055Sdanmcd goto fail2; 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate if (!spdsock_ext_to_sel(extv, &sel, &diag)) { 10763055Sdanmcd error = EINVAL; 10773055Sdanmcd goto fail2; 10783055Sdanmcd } 10793055Sdanmcd 10803055Sdanmcd if (itp != NULL) { 10813055Sdanmcd if (tunnel_mode) { 10823055Sdanmcd if (sel.ipsl_valid & 10833055Sdanmcd (IPSL_REMOTE_PORT | IPSL_LOCAL_PORT)) { 10843055Sdanmcd itp->itp_flags |= active ? 10853055Sdanmcd ITPF_P_PER_PORT_SECURITY : 10863055Sdanmcd ITPF_I_PER_PORT_SECURITY; 10873055Sdanmcd } 10883055Sdanmcd } else { 10893055Sdanmcd /* 10903055Sdanmcd * For now, we don't allow transport-mode on a tunnel 10913055Sdanmcd * with ANY specific selectors. Bail if we have such 10923055Sdanmcd * a request. 10933055Sdanmcd */ 10943055Sdanmcd if (sel.ipsl_valid & IPSL_WILDCARD) { 10953055Sdanmcd diag = SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS; 10963055Sdanmcd error = EINVAL; 10973055Sdanmcd goto fail2; 10983055Sdanmcd } 10993055Sdanmcd } 11000Sstevel@tonic-gate } 11010Sstevel@tonic-gate 11023448Sdh155122 if (!spdsock_ext_to_actvec(extv, &actp, &nact, &diag, spds)) { 11033055Sdanmcd error = EINVAL; 11043055Sdanmcd goto fail2; 11050Sstevel@tonic-gate } 11060Sstevel@tonic-gate /* 11070Sstevel@tonic-gate * If no addresses were specified, add both. 11080Sstevel@tonic-gate */ 11090Sstevel@tonic-gate afs = sel.ipsl_valid & (IPSL_IPV6|IPSL_IPV4); 11100Sstevel@tonic-gate if (afs == 0) 11110Sstevel@tonic-gate afs = (IPSL_IPV6|IPSL_IPV4); 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_WRITER); 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate if (rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) { 11160Sstevel@tonic-gate error = mkrulepair(iph, rule, &sel, actp, nact, 11173448Sdh155122 IPSEC_TYPE_OUTBOUND, afs, &rulep, index, spds); 11180Sstevel@tonic-gate if (error != 0) 11190Sstevel@tonic-gate goto fail; 11200Sstevel@tonic-gate } 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate if (rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) { 11230Sstevel@tonic-gate error = mkrulepair(iph, rule, &sel, actp, nact, 11243448Sdh155122 IPSEC_TYPE_INBOUND, afs, &rulep, index, spds); 11250Sstevel@tonic-gate if (error != 0) 11260Sstevel@tonic-gate goto fail; 11270Sstevel@tonic-gate } 11280Sstevel@tonic-gate 11293448Sdh155122 while ((--rulep) >= &rules[0]) { 11303448Sdh155122 ipsec_enter_policy(iph, rulep->pol, rulep->dir, 11313448Sdh155122 spds->spds_netstack); 11323448Sdh155122 } 11330Sstevel@tonic-gate rw_exit(&iph->iph_lock); 11343055Sdanmcd if (itp != NULL) 11353055Sdanmcd mutex_exit(&itp->itp_lock); 11360Sstevel@tonic-gate 11370Sstevel@tonic-gate ipsec_actvec_free(actp, nact); 11380Sstevel@tonic-gate spd_echo(q, mp); 11394307Spwernau if (audit_active) { 11404307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 11418778SErik.Nordmark@Sun.COM cred_t *cr; 11428778SErik.Nordmark@Sun.COM pid_t cpid; 11438778SErik.Nordmark@Sun.COM 11448778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 11454307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 11468778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack, 11478778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, 0, cpid); 11484307Spwernau } 11490Sstevel@tonic-gate return; 11500Sstevel@tonic-gate 11510Sstevel@tonic-gate fail: 11520Sstevel@tonic-gate rw_exit(&iph->iph_lock); 11530Sstevel@tonic-gate while ((--rulep) >= &rules[0]) { 11543448Sdh155122 IPPOL_REFRELE(rulep->pol, spds->spds_netstack); 11550Sstevel@tonic-gate } 11560Sstevel@tonic-gate ipsec_actvec_free(actp, nact); 11573055Sdanmcd fail2: 11583055Sdanmcd if (itp != NULL) { 11593055Sdanmcd if (empty_itp) 11603055Sdanmcd itp->itp_flags = 0; 11613055Sdanmcd mutex_exit(&itp->itp_lock); 11623055Sdanmcd } 11633055Sdanmcd spdsock_error(q, mp, error, diag); 11644307Spwernau if (audit_active) { 11654307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 11668778SErik.Nordmark@Sun.COM cred_t *cr; 11678778SErik.Nordmark@Sun.COM pid_t cpid; 11688778SErik.Nordmark@Sun.COM 11698778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 11704307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 11718778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack, 11728778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, error, cpid); 11734307Spwernau } 11740Sstevel@tonic-gate } 11750Sstevel@tonic-gate 11760Sstevel@tonic-gate void 11773055Sdanmcd spdsock_deleterule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 11783055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 11790Sstevel@tonic-gate { 11800Sstevel@tonic-gate ipsec_selkey_t sel; 11810Sstevel@tonic-gate struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE]; 11823055Sdanmcd int err, diag = 0; 11833448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 11846430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate if (rule == NULL) { 11870Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT); 11884307Spwernau if (audit_active) { 11894307Spwernau boolean_t active; 11904307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 11918778SErik.Nordmark@Sun.COM cred_t *cr; 11928778SErik.Nordmark@Sun.COM pid_t cpid; 11938778SErik.Nordmark@Sun.COM 11948778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 11954307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 11968778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_DELETERULE, cr, ns, 11976430Sdanmcd ITP_NAME(itp), active, SPD_DIAGNOSTIC_NO_RULE_EXT, 11988778SErik.Nordmark@Sun.COM cpid); 11994307Spwernau } 12000Sstevel@tonic-gate return; 12010Sstevel@tonic-gate } 12020Sstevel@tonic-gate 12033055Sdanmcd /* 12043055Sdanmcd * Must enter itp_lock first to avoid deadlock. See tun.c's 12053055Sdanmcd * set_sec_simple() for the other case of itp_lock and iph_lock. 12063055Sdanmcd */ 12073055Sdanmcd if (itp != NULL) 12083055Sdanmcd mutex_enter(&itp->itp_lock); 12093055Sdanmcd 12100Sstevel@tonic-gate if (rule->spd_rule_index != 0) { 12116430Sdanmcd if (ipsec_policy_delete_index(iph, rule->spd_rule_index, ns) != 12126430Sdanmcd 0) { 12133055Sdanmcd err = ESRCH; 12143055Sdanmcd goto fail; 12150Sstevel@tonic-gate } 12160Sstevel@tonic-gate } else { 12170Sstevel@tonic-gate if (!spdsock_ext_to_sel(extv, &sel, &diag)) { 12183055Sdanmcd err = EINVAL; /* diag already set... */ 12193055Sdanmcd goto fail; 12203055Sdanmcd } 12213055Sdanmcd 12223055Sdanmcd if ((rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) && 12236430Sdanmcd !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_INBOUND, ns)) { 12243055Sdanmcd err = ESRCH; 12253055Sdanmcd goto fail; 12263055Sdanmcd } 12273055Sdanmcd 12283055Sdanmcd if ((rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) && 12296430Sdanmcd !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_OUTBOUND, ns)) { 12303055Sdanmcd err = ESRCH; 12313055Sdanmcd goto fail; 12320Sstevel@tonic-gate } 12333055Sdanmcd } 12343055Sdanmcd 12353055Sdanmcd if (itp != NULL) { 12363055Sdanmcd ASSERT(iph == itp->itp_policy || iph == itp->itp_inactive); 12373055Sdanmcd rw_enter(&iph->iph_lock, RW_READER); 12383055Sdanmcd if (avl_numnodes(&iph->iph_rulebyid) == 0) { 12393055Sdanmcd if (iph == itp->itp_policy) 12403055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 12413055Sdanmcd else 12423055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 12430Sstevel@tonic-gate } 12443055Sdanmcd /* Can exit locks in any order. */ 12453055Sdanmcd rw_exit(&iph->iph_lock); 12463055Sdanmcd mutex_exit(&itp->itp_lock); 12470Sstevel@tonic-gate } 12480Sstevel@tonic-gate spd_echo(q, mp); 12494307Spwernau if (audit_active) { 12504307Spwernau boolean_t active; 12514307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 12528778SErik.Nordmark@Sun.COM cred_t *cr; 12538778SErik.Nordmark@Sun.COM pid_t cpid; 12548778SErik.Nordmark@Sun.COM 12558778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 12564307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 12578778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp), 12588778SErik.Nordmark@Sun.COM active, 0, cpid); 12594307Spwernau } 12600Sstevel@tonic-gate return; 12610Sstevel@tonic-gate fail: 12623055Sdanmcd if (itp != NULL) 12633055Sdanmcd mutex_exit(&itp->itp_lock); 12643055Sdanmcd spdsock_error(q, mp, err, diag); 12654307Spwernau if (audit_active) { 12664307Spwernau boolean_t active; 12674307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 12688778SErik.Nordmark@Sun.COM cred_t *cr; 12698778SErik.Nordmark@Sun.COM pid_t cpid; 12708778SErik.Nordmark@Sun.COM 12718778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 12724307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 12738778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp), 12748778SErik.Nordmark@Sun.COM active, err, cpid); 12754307Spwernau } 12763055Sdanmcd } 12773055Sdanmcd 12783055Sdanmcd /* Do NOT consume a reference to itp. */ 12793055Sdanmcd /* ARGSUSED */ 12803055Sdanmcd static void 12813448Sdh155122 spdsock_flip_node(ipsec_tun_pol_t *itp, void *ignoreme, netstack_t *ns) 12823055Sdanmcd { 12833055Sdanmcd mutex_enter(&itp->itp_lock); 12843055Sdanmcd ITPF_SWAP(itp->itp_flags); 12853448Sdh155122 ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns); 12863055Sdanmcd mutex_exit(&itp->itp_lock); 12870Sstevel@tonic-gate } 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate void 12903055Sdanmcd spdsock_flip(queue_t *q, mblk_t *mp, spd_if_t *tunname) 12910Sstevel@tonic-gate { 12923055Sdanmcd char *tname; 12933055Sdanmcd ipsec_tun_pol_t *itp; 12943448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 12956430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 12963055Sdanmcd 12973055Sdanmcd if (tunname != NULL) { 12983055Sdanmcd tname = (char *)tunname->spd_if_name; 12993055Sdanmcd if (*tname == '\0') { 13003448Sdh155122 /* can't fail */ 13016430Sdanmcd ipsec_swap_global_policy(ns); 13024307Spwernau if (audit_active) { 13034307Spwernau boolean_t active; 13044307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 13058778SErik.Nordmark@Sun.COM cred_t *cr; 13068778SErik.Nordmark@Sun.COM pid_t cpid; 13078778SErik.Nordmark@Sun.COM 13088778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 13094307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 13108778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLIP, cr, ns, 13118778SErik.Nordmark@Sun.COM NULL, active, 0, cpid); 13124307Spwernau } 13136430Sdanmcd itp_walk(spdsock_flip_node, NULL, ns); 13144307Spwernau if (audit_active) { 13154307Spwernau boolean_t active; 13164307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 13178778SErik.Nordmark@Sun.COM cred_t *cr; 13188778SErik.Nordmark@Sun.COM pid_t cpid; 13198778SErik.Nordmark@Sun.COM 13208778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 13214307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 13228778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLIP, cr, ns, 13238778SErik.Nordmark@Sun.COM "all tunnels", active, 0, cpid); 13244307Spwernau } 13253055Sdanmcd } else { 13266430Sdanmcd itp = get_tunnel_policy(tname, ns); 13273055Sdanmcd if (itp == NULL) { 13283055Sdanmcd /* Better idea for "tunnel not found"? */ 13293055Sdanmcd spdsock_error(q, mp, ESRCH, 0); 13304307Spwernau if (audit_active) { 13314307Spwernau boolean_t active; 13324307Spwernau spd_msg_t *spmsg = 13334307Spwernau (spd_msg_t *)mp->b_rptr; 13348778SErik.Nordmark@Sun.COM cred_t *cr; 13358778SErik.Nordmark@Sun.COM pid_t cpid; 13368778SErik.Nordmark@Sun.COM 13378778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 13384307Spwernau active = (spmsg->spd_msg_spdid == 13394307Spwernau SPD_ACTIVE); 13408778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLIP, cr, ns, 13418778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, 13428778SErik.Nordmark@Sun.COM ESRCH, cpid); 13434307Spwernau } 13443055Sdanmcd return; 13453055Sdanmcd } 13463448Sdh155122 spdsock_flip_node(itp, NULL, NULL); 13474307Spwernau if (audit_active) { 13484307Spwernau boolean_t active; 13494307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 13508778SErik.Nordmark@Sun.COM cred_t *cr; 13518778SErik.Nordmark@Sun.COM pid_t cpid; 13528778SErik.Nordmark@Sun.COM 13538778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 13544307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 13558778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLIP, cr, ns, 13568778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, 0, cpid); 13574307Spwernau } 13586430Sdanmcd ITP_REFRELE(itp, ns); 13593055Sdanmcd } 13603055Sdanmcd } else { 13616430Sdanmcd ipsec_swap_global_policy(ns); /* can't fail */ 13624307Spwernau if (audit_active) { 13634307Spwernau boolean_t active; 13644307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 13658778SErik.Nordmark@Sun.COM cred_t *cr; 13668778SErik.Nordmark@Sun.COM pid_t cpid; 13678778SErik.Nordmark@Sun.COM 13688778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 13694307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 13708778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLIP, cr, 13718778SErik.Nordmark@Sun.COM ns, NULL, active, 0, cpid); 13724307Spwernau } 13733055Sdanmcd } 13740Sstevel@tonic-gate spd_echo(q, mp); 13750Sstevel@tonic-gate } 13760Sstevel@tonic-gate 13770Sstevel@tonic-gate /* 13780Sstevel@tonic-gate * Unimplemented feature 13790Sstevel@tonic-gate */ 13800Sstevel@tonic-gate /* ARGSUSED */ 13810Sstevel@tonic-gate static void 13823055Sdanmcd spdsock_lookup(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 13833055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 13840Sstevel@tonic-gate { 13850Sstevel@tonic-gate spdsock_error(q, mp, EINVAL, 0); 13860Sstevel@tonic-gate } 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate static mblk_t * 13900Sstevel@tonic-gate spdsock_dump_ruleset(mblk_t *req, ipsec_policy_head_t *iph, 13910Sstevel@tonic-gate uint32_t count, uint16_t error) 13920Sstevel@tonic-gate { 13930Sstevel@tonic-gate size_t len = sizeof (spd_ruleset_ext_t) + sizeof (spd_msg_t); 13940Sstevel@tonic-gate spd_msg_t *msg; 13950Sstevel@tonic-gate spd_ruleset_ext_t *ruleset; 13960Sstevel@tonic-gate mblk_t *m = allocb(len, BPRI_HI); 13970Sstevel@tonic-gate 13980Sstevel@tonic-gate ASSERT(RW_READ_HELD(&iph->iph_lock)); 13990Sstevel@tonic-gate 14000Sstevel@tonic-gate if (m == NULL) { 14010Sstevel@tonic-gate return (NULL); 14020Sstevel@tonic-gate } 14030Sstevel@tonic-gate msg = (spd_msg_t *)m->b_rptr; 14040Sstevel@tonic-gate ruleset = (spd_ruleset_ext_t *)(&msg[1]); 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate m->b_wptr = (uint8_t *)&ruleset[1]; 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate *msg = *(spd_msg_t *)(req->b_rptr); 14090Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(len); 14100Sstevel@tonic-gate msg->spd_msg_errno = error; 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate ruleset->spd_ruleset_len = SPD_8TO64(sizeof (*ruleset)); 14130Sstevel@tonic-gate ruleset->spd_ruleset_type = SPD_EXT_RULESET; 14140Sstevel@tonic-gate ruleset->spd_ruleset_count = count; 14150Sstevel@tonic-gate ruleset->spd_ruleset_version = iph->iph_gen; 14160Sstevel@tonic-gate return (m); 14170Sstevel@tonic-gate } 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate static mblk_t * 14200Sstevel@tonic-gate spdsock_dump_finish(spdsock_t *ss, int error) 14210Sstevel@tonic-gate { 14220Sstevel@tonic-gate mblk_t *m; 14230Sstevel@tonic-gate ipsec_policy_head_t *iph = ss->spdsock_dump_head; 14240Sstevel@tonic-gate mblk_t *req = ss->spdsock_dump_req; 142510592Sdanmcd@sun.com netstack_t *ns = ss->spdsock_spds->spds_netstack; 14260Sstevel@tonic-gate 14270Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 14280Sstevel@tonic-gate m = spdsock_dump_ruleset(req, iph, ss->spdsock_dump_count, error); 14290Sstevel@tonic-gate rw_exit(&iph->iph_lock); 143010592Sdanmcd@sun.com IPPH_REFRELE(iph, ns); 143110592Sdanmcd@sun.com if (ss->spdsock_itp != NULL) { 143210592Sdanmcd@sun.com ITP_REFRELE(ss->spdsock_itp, ns); 143310592Sdanmcd@sun.com ss->spdsock_itp = NULL; 143410592Sdanmcd@sun.com } 14350Sstevel@tonic-gate ss->spdsock_dump_req = NULL; 14360Sstevel@tonic-gate freemsg(req); 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate return (m); 14390Sstevel@tonic-gate } 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate /* 14420Sstevel@tonic-gate * Rule encoding functions. 14430Sstevel@tonic-gate * We do a two-pass encode. 14440Sstevel@tonic-gate * If base != NULL, fill in encoded rule part starting at base+offset. 14450Sstevel@tonic-gate * Always return "offset" plus length of to-be-encoded data. 14460Sstevel@tonic-gate */ 14470Sstevel@tonic-gate static uint_t 14480Sstevel@tonic-gate spdsock_encode_typecode(uint8_t *base, uint_t offset, uint8_t type, 14490Sstevel@tonic-gate uint8_t type_end, uint8_t code, uint8_t code_end) 14500Sstevel@tonic-gate { 14510Sstevel@tonic-gate struct spd_typecode *tcp; 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14540Sstevel@tonic-gate 14550Sstevel@tonic-gate if (base != NULL) { 14560Sstevel@tonic-gate tcp = (struct spd_typecode *)(base + offset); 14570Sstevel@tonic-gate tcp->spd_typecode_len = SPD_8TO64(sizeof (*tcp)); 14580Sstevel@tonic-gate tcp->spd_typecode_exttype = SPD_EXT_ICMP_TYPECODE; 14590Sstevel@tonic-gate tcp->spd_typecode_code = code; 14600Sstevel@tonic-gate tcp->spd_typecode_type = type; 14610Sstevel@tonic-gate tcp->spd_typecode_type_end = type_end; 14620Sstevel@tonic-gate tcp->spd_typecode_code_end = code_end; 14630Sstevel@tonic-gate } 14640Sstevel@tonic-gate offset += sizeof (*tcp); 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14670Sstevel@tonic-gate 14680Sstevel@tonic-gate return (offset); 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate static uint_t 14720Sstevel@tonic-gate spdsock_encode_proto(uint8_t *base, uint_t offset, uint8_t proto) 14730Sstevel@tonic-gate { 14740Sstevel@tonic-gate struct spd_proto *spp; 14750Sstevel@tonic-gate 14760Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate if (base != NULL) { 14790Sstevel@tonic-gate spp = (struct spd_proto *)(base + offset); 14800Sstevel@tonic-gate spp->spd_proto_len = SPD_8TO64(sizeof (*spp)); 14810Sstevel@tonic-gate spp->spd_proto_exttype = SPD_EXT_PROTO; 14820Sstevel@tonic-gate spp->spd_proto_number = proto; 14830Sstevel@tonic-gate spp->spd_proto_reserved1 = 0; 14840Sstevel@tonic-gate spp->spd_proto_reserved2 = 0; 14850Sstevel@tonic-gate } 14860Sstevel@tonic-gate offset += sizeof (*spp); 14870Sstevel@tonic-gate 14880Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate return (offset); 14910Sstevel@tonic-gate } 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate static uint_t 14940Sstevel@tonic-gate spdsock_encode_port(uint8_t *base, uint_t offset, uint16_t ext, uint16_t port) 14950Sstevel@tonic-gate { 14960Sstevel@tonic-gate struct spd_portrange *spp; 14970Sstevel@tonic-gate 14980Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate if (base != NULL) { 15010Sstevel@tonic-gate spp = (struct spd_portrange *)(base + offset); 15020Sstevel@tonic-gate spp->spd_ports_len = SPD_8TO64(sizeof (*spp)); 15030Sstevel@tonic-gate spp->spd_ports_exttype = ext; 15040Sstevel@tonic-gate spp->spd_ports_minport = port; 15050Sstevel@tonic-gate spp->spd_ports_maxport = port; 15060Sstevel@tonic-gate } 15070Sstevel@tonic-gate offset += sizeof (*spp); 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15100Sstevel@tonic-gate 15110Sstevel@tonic-gate return (offset); 15120Sstevel@tonic-gate } 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate static uint_t 15150Sstevel@tonic-gate spdsock_encode_addr(uint8_t *base, uint_t offset, uint16_t ext, 15160Sstevel@tonic-gate const ipsec_selkey_t *sel, const ipsec_addr_t *addr, uint_t pfxlen) 15170Sstevel@tonic-gate { 15180Sstevel@tonic-gate struct spd_address *sae; 15190Sstevel@tonic-gate ipsec_addr_t *spdaddr; 15200Sstevel@tonic-gate uint_t start = offset; 15210Sstevel@tonic-gate uint_t addrlen; 15220Sstevel@tonic-gate uint_t af; 15230Sstevel@tonic-gate 15240Sstevel@tonic-gate if (sel->ipsl_valid & IPSL_IPV4) { 15250Sstevel@tonic-gate af = AF_INET; 15260Sstevel@tonic-gate addrlen = IP_ADDR_LEN; 15270Sstevel@tonic-gate } else { 15280Sstevel@tonic-gate af = AF_INET6; 15290Sstevel@tonic-gate addrlen = IPV6_ADDR_LEN; 15300Sstevel@tonic-gate } 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate if (base != NULL) { 15350Sstevel@tonic-gate sae = (struct spd_address *)(base + offset); 15360Sstevel@tonic-gate sae->spd_address_exttype = ext; 15370Sstevel@tonic-gate sae->spd_address_af = af; 15380Sstevel@tonic-gate sae->spd_address_prefixlen = pfxlen; 15390Sstevel@tonic-gate sae->spd_address_reserved2 = 0; 15400Sstevel@tonic-gate 15410Sstevel@tonic-gate spdaddr = (ipsec_addr_t *)(&sae[1]); 15420Sstevel@tonic-gate bcopy(addr, spdaddr, addrlen); 15430Sstevel@tonic-gate } 15440Sstevel@tonic-gate offset += sizeof (*sae); 15450Sstevel@tonic-gate addrlen = roundup(addrlen, sizeof (uint64_t)); 15460Sstevel@tonic-gate offset += addrlen; 15470Sstevel@tonic-gate 15480Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate if (base != NULL) 15510Sstevel@tonic-gate sae->spd_address_len = SPD_8TO64(offset - start); 15520Sstevel@tonic-gate return (offset); 15530Sstevel@tonic-gate } 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate static uint_t 15560Sstevel@tonic-gate spdsock_encode_sel(uint8_t *base, uint_t offset, const ipsec_sel_t *sel) 15570Sstevel@tonic-gate { 15580Sstevel@tonic-gate const ipsec_selkey_t *selkey = &sel->ipsl_key; 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_PROTOCOL) 15610Sstevel@tonic-gate offset = spdsock_encode_proto(base, offset, selkey->ipsl_proto); 15620Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_LOCAL_PORT) 15630Sstevel@tonic-gate offset = spdsock_encode_port(base, offset, SPD_EXT_LCLPORT, 15640Sstevel@tonic-gate selkey->ipsl_lport); 15650Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_REMOTE_PORT) 15660Sstevel@tonic-gate offset = spdsock_encode_port(base, offset, SPD_EXT_REMPORT, 15670Sstevel@tonic-gate selkey->ipsl_rport); 15680Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_REMOTE_ADDR) 15690Sstevel@tonic-gate offset = spdsock_encode_addr(base, offset, SPD_EXT_REMADDR, 15700Sstevel@tonic-gate selkey, &selkey->ipsl_remote, selkey->ipsl_remote_pfxlen); 15710Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_LOCAL_ADDR) 15720Sstevel@tonic-gate offset = spdsock_encode_addr(base, offset, SPD_EXT_LCLADDR, 15730Sstevel@tonic-gate selkey, &selkey->ipsl_local, selkey->ipsl_local_pfxlen); 15740Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_ICMP_TYPE) { 15750Sstevel@tonic-gate offset = spdsock_encode_typecode(base, offset, 15760Sstevel@tonic-gate selkey->ipsl_icmp_type, selkey->ipsl_icmp_type_end, 15770Sstevel@tonic-gate (selkey->ipsl_valid & IPSL_ICMP_CODE) ? 15785240Snordmark selkey->ipsl_icmp_code : 255, 15790Sstevel@tonic-gate (selkey->ipsl_valid & IPSL_ICMP_CODE) ? 15805240Snordmark selkey->ipsl_icmp_code_end : 255); 15810Sstevel@tonic-gate } 15820Sstevel@tonic-gate return (offset); 15830Sstevel@tonic-gate } 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate static uint_t 15860Sstevel@tonic-gate spdsock_encode_actattr(uint8_t *base, uint_t offset, uint32_t tag, 15870Sstevel@tonic-gate uint32_t value) 15880Sstevel@tonic-gate { 15890Sstevel@tonic-gate struct spd_attribute *attr; 15900Sstevel@tonic-gate 15910Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15920Sstevel@tonic-gate 15930Sstevel@tonic-gate if (base != NULL) { 15940Sstevel@tonic-gate attr = (struct spd_attribute *)(base + offset); 15950Sstevel@tonic-gate attr->spd_attr_tag = tag; 15960Sstevel@tonic-gate attr->spd_attr_value = value; 15970Sstevel@tonic-gate } 15980Sstevel@tonic-gate offset += sizeof (struct spd_attribute); 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 16010Sstevel@tonic-gate 16020Sstevel@tonic-gate return (offset); 16030Sstevel@tonic-gate } 16040Sstevel@tonic-gate 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate #define EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v)) 16070Sstevel@tonic-gate 16080Sstevel@tonic-gate static uint_t 16090Sstevel@tonic-gate spdsock_encode_action(uint8_t *base, uint_t offset, const ipsec_action_t *ap) 16100Sstevel@tonic-gate { 16110Sstevel@tonic-gate const struct ipsec_act *act = &(ap->ipa_act); 16120Sstevel@tonic-gate uint_t flags; 16130Sstevel@tonic-gate 16140Sstevel@tonic-gate EMIT(SPD_ATTR_EMPTY, 0); 16150Sstevel@tonic-gate switch (act->ipa_type) { 16160Sstevel@tonic-gate case IPSEC_ACT_DISCARD: 16170Sstevel@tonic-gate case IPSEC_ACT_REJECT: 16180Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_DROP); 16190Sstevel@tonic-gate break; 16200Sstevel@tonic-gate case IPSEC_ACT_BYPASS: 16210Sstevel@tonic-gate case IPSEC_ACT_CLEAR: 16220Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_PASS); 16230Sstevel@tonic-gate break; 16240Sstevel@tonic-gate 16250Sstevel@tonic-gate case IPSEC_ACT_APPLY: 16260Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_IPSEC); 16270Sstevel@tonic-gate flags = 0; 16280Sstevel@tonic-gate if (act->ipa_apply.ipp_use_ah) 16290Sstevel@tonic-gate flags |= SPD_APPLY_AH; 16300Sstevel@tonic-gate if (act->ipa_apply.ipp_use_esp) 16310Sstevel@tonic-gate flags |= SPD_APPLY_ESP; 16320Sstevel@tonic-gate if (act->ipa_apply.ipp_use_espa) 16330Sstevel@tonic-gate flags |= SPD_APPLY_ESPA; 16340Sstevel@tonic-gate if (act->ipa_apply.ipp_use_se) 16350Sstevel@tonic-gate flags |= SPD_APPLY_SE; 16360Sstevel@tonic-gate if (act->ipa_apply.ipp_use_unique) 16370Sstevel@tonic-gate flags |= SPD_APPLY_UNIQUE; 16380Sstevel@tonic-gate EMIT(SPD_ATTR_FLAGS, flags); 16390Sstevel@tonic-gate if (flags & SPD_APPLY_AH) { 16400Sstevel@tonic-gate EMIT(SPD_ATTR_AH_AUTH, act->ipa_apply.ipp_auth_alg); 16410Sstevel@tonic-gate EMIT(SPD_ATTR_AH_MINBITS, 16420Sstevel@tonic-gate act->ipa_apply.ipp_ah_minbits); 16430Sstevel@tonic-gate EMIT(SPD_ATTR_AH_MAXBITS, 16440Sstevel@tonic-gate act->ipa_apply.ipp_ah_maxbits); 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate if (flags & SPD_APPLY_ESP) { 16470Sstevel@tonic-gate EMIT(SPD_ATTR_ESP_ENCR, act->ipa_apply.ipp_encr_alg); 16480Sstevel@tonic-gate EMIT(SPD_ATTR_ENCR_MINBITS, 16490Sstevel@tonic-gate act->ipa_apply.ipp_espe_minbits); 16500Sstevel@tonic-gate EMIT(SPD_ATTR_ENCR_MAXBITS, 16510Sstevel@tonic-gate act->ipa_apply.ipp_espe_maxbits); 16520Sstevel@tonic-gate if (flags & SPD_APPLY_ESPA) { 16530Sstevel@tonic-gate EMIT(SPD_ATTR_ESP_AUTH, 16540Sstevel@tonic-gate act->ipa_apply.ipp_esp_auth_alg); 16550Sstevel@tonic-gate EMIT(SPD_ATTR_ESPA_MINBITS, 16560Sstevel@tonic-gate act->ipa_apply.ipp_espa_minbits); 16570Sstevel@tonic-gate EMIT(SPD_ATTR_ESPA_MAXBITS, 16580Sstevel@tonic-gate act->ipa_apply.ipp_espa_maxbits); 16590Sstevel@tonic-gate } 16600Sstevel@tonic-gate } 16610Sstevel@tonic-gate if (act->ipa_apply.ipp_km_proto != 0) 16620Sstevel@tonic-gate EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_proto); 16630Sstevel@tonic-gate if (act->ipa_apply.ipp_km_cookie != 0) 16640Sstevel@tonic-gate EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_cookie); 16650Sstevel@tonic-gate if (act->ipa_apply.ipp_replay_depth != 0) 16660Sstevel@tonic-gate EMIT(SPD_ATTR_REPLAY_DEPTH, 16670Sstevel@tonic-gate act->ipa_apply.ipp_replay_depth); 16680Sstevel@tonic-gate /* Add more here */ 16690Sstevel@tonic-gate break; 16700Sstevel@tonic-gate } 16710Sstevel@tonic-gate 16720Sstevel@tonic-gate return (offset); 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate static uint_t 16760Sstevel@tonic-gate spdsock_encode_action_list(uint8_t *base, uint_t offset, 16770Sstevel@tonic-gate const ipsec_action_t *ap) 16780Sstevel@tonic-gate { 16790Sstevel@tonic-gate struct spd_ext_actions *act; 16800Sstevel@tonic-gate uint_t nact = 0; 16810Sstevel@tonic-gate uint_t start = offset; 16820Sstevel@tonic-gate 16830Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 16840Sstevel@tonic-gate 16850Sstevel@tonic-gate if (base != NULL) { 16860Sstevel@tonic-gate act = (struct spd_ext_actions *)(base + offset); 16870Sstevel@tonic-gate act->spd_actions_len = 0; 16880Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 16890Sstevel@tonic-gate act->spd_actions_count = 0; 16900Sstevel@tonic-gate act->spd_actions_reserved = 0; 16910Sstevel@tonic-gate } 16920Sstevel@tonic-gate 16930Sstevel@tonic-gate offset += sizeof (*act); 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 16960Sstevel@tonic-gate 16970Sstevel@tonic-gate while (ap != NULL) { 16980Sstevel@tonic-gate offset = spdsock_encode_action(base, offset, ap); 16990Sstevel@tonic-gate ap = ap->ipa_next; 17000Sstevel@tonic-gate nact++; 17010Sstevel@tonic-gate if (ap != NULL) { 17020Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); 17030Sstevel@tonic-gate } 17040Sstevel@tonic-gate } 17050Sstevel@tonic-gate EMIT(SPD_ATTR_END, 0); 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17080Sstevel@tonic-gate 17090Sstevel@tonic-gate if (base != NULL) { 17100Sstevel@tonic-gate act->spd_actions_count = nact; 17110Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(offset - start); 17120Sstevel@tonic-gate } 17130Sstevel@tonic-gate 17140Sstevel@tonic-gate return (offset); 17150Sstevel@tonic-gate } 17160Sstevel@tonic-gate 17170Sstevel@tonic-gate #undef EMIT 17180Sstevel@tonic-gate 17190Sstevel@tonic-gate /* ARGSUSED */ 17200Sstevel@tonic-gate static uint_t 17210Sstevel@tonic-gate spdsock_rule_flags(uint_t dir, uint_t af) 17220Sstevel@tonic-gate { 17230Sstevel@tonic-gate uint_t flags = 0; 17240Sstevel@tonic-gate 17250Sstevel@tonic-gate if (dir == IPSEC_TYPE_INBOUND) 17260Sstevel@tonic-gate flags |= SPD_RULE_FLAG_INBOUND; 17270Sstevel@tonic-gate if (dir == IPSEC_TYPE_OUTBOUND) 17280Sstevel@tonic-gate flags |= SPD_RULE_FLAG_OUTBOUND; 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate return (flags); 17310Sstevel@tonic-gate } 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate 17340Sstevel@tonic-gate static uint_t 17353055Sdanmcd spdsock_encode_rule_head(uint8_t *base, uint_t offset, spd_msg_t *req, 17363055Sdanmcd const ipsec_policy_t *rule, uint_t dir, uint_t af, char *name, 17373055Sdanmcd boolean_t tunnel) 17380Sstevel@tonic-gate { 17390Sstevel@tonic-gate struct spd_msg *spmsg; 17400Sstevel@tonic-gate struct spd_rule *spr; 17413055Sdanmcd spd_if_t *sid; 17420Sstevel@tonic-gate 17430Sstevel@tonic-gate uint_t start = offset; 17440Sstevel@tonic-gate 17450Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17460Sstevel@tonic-gate 17470Sstevel@tonic-gate if (base != NULL) { 17480Sstevel@tonic-gate spmsg = (struct spd_msg *)(base + offset); 17490Sstevel@tonic-gate bzero(spmsg, sizeof (*spmsg)); 17500Sstevel@tonic-gate spmsg->spd_msg_version = PF_POLICY_V1; 17510Sstevel@tonic-gate spmsg->spd_msg_type = SPD_DUMP; 17520Sstevel@tonic-gate spmsg->spd_msg_seq = req->spd_msg_seq; 17530Sstevel@tonic-gate spmsg->spd_msg_pid = req->spd_msg_pid; 17540Sstevel@tonic-gate } 17550Sstevel@tonic-gate offset += sizeof (struct spd_msg); 17560Sstevel@tonic-gate 17570Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate if (base != NULL) { 17600Sstevel@tonic-gate spr = (struct spd_rule *)(base + offset); 17610Sstevel@tonic-gate spr->spd_rule_type = SPD_EXT_RULE; 17620Sstevel@tonic-gate spr->spd_rule_priority = rule->ipsp_prio; 17630Sstevel@tonic-gate spr->spd_rule_flags = spdsock_rule_flags(dir, af); 17643055Sdanmcd if (tunnel) 17653055Sdanmcd spr->spd_rule_flags |= SPD_RULE_FLAG_TUNNEL; 17660Sstevel@tonic-gate spr->spd_rule_unused = 0; 17670Sstevel@tonic-gate spr->spd_rule_len = SPD_8TO64(sizeof (*spr)); 17680Sstevel@tonic-gate spr->spd_rule_index = rule->ipsp_index; 17690Sstevel@tonic-gate } 17700Sstevel@tonic-gate offset += sizeof (struct spd_rule); 17713055Sdanmcd 17723055Sdanmcd /* 17733055Sdanmcd * If we have an interface name (i.e. if this policy head came from 17743055Sdanmcd * a tunnel), add the SPD_EXT_TUN_NAME extension. 17753055Sdanmcd */ 17766466Sdanmcd if (name != NULL) { 17773055Sdanmcd 17783055Sdanmcd ASSERT(ALIGNED64(offset)); 17793055Sdanmcd 17803055Sdanmcd if (base != NULL) { 17813055Sdanmcd sid = (spd_if_t *)(base + offset); 17823055Sdanmcd sid->spd_if_exttype = SPD_EXT_TUN_NAME; 17833055Sdanmcd sid->spd_if_len = SPD_8TO64(sizeof (spd_if_t) + 17843055Sdanmcd roundup((strlen(name) - 4), 8)); 17853055Sdanmcd (void) strlcpy((char *)sid->spd_if_name, name, 17863055Sdanmcd LIFNAMSIZ); 17873055Sdanmcd } 17883055Sdanmcd 17893055Sdanmcd offset += sizeof (spd_if_t) + roundup((strlen(name) - 4), 8); 17903055Sdanmcd } 17913055Sdanmcd 17920Sstevel@tonic-gate offset = spdsock_encode_sel(base, offset, rule->ipsp_sel); 17930Sstevel@tonic-gate offset = spdsock_encode_action_list(base, offset, rule->ipsp_act); 17940Sstevel@tonic-gate 17950Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17960Sstevel@tonic-gate 17970Sstevel@tonic-gate if (base != NULL) { 17980Sstevel@tonic-gate spmsg->spd_msg_len = SPD_8TO64(offset - start); 17990Sstevel@tonic-gate } 18000Sstevel@tonic-gate return (offset); 18010Sstevel@tonic-gate } 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate /* ARGSUSED */ 18040Sstevel@tonic-gate static mblk_t * 18050Sstevel@tonic-gate spdsock_encode_rule(mblk_t *req, const ipsec_policy_t *rule, 18063055Sdanmcd uint_t dir, uint_t af, char *name, boolean_t tunnel) 18070Sstevel@tonic-gate { 18080Sstevel@tonic-gate mblk_t *m; 18090Sstevel@tonic-gate uint_t len; 18100Sstevel@tonic-gate spd_msg_t *mreq = (spd_msg_t *)req->b_rptr; 18110Sstevel@tonic-gate 18120Sstevel@tonic-gate /* 18130Sstevel@tonic-gate * Figure out how much space we'll need. 18140Sstevel@tonic-gate */ 18153055Sdanmcd len = spdsock_encode_rule_head(NULL, 0, mreq, rule, dir, af, name, 18163055Sdanmcd tunnel); 18170Sstevel@tonic-gate 18180Sstevel@tonic-gate /* 18190Sstevel@tonic-gate * Allocate mblk. 18200Sstevel@tonic-gate */ 18210Sstevel@tonic-gate m = allocb(len, BPRI_HI); 18220Sstevel@tonic-gate if (m == NULL) 18230Sstevel@tonic-gate return (NULL); 18240Sstevel@tonic-gate 18250Sstevel@tonic-gate /* 18260Sstevel@tonic-gate * Fill it in.. 18270Sstevel@tonic-gate */ 18280Sstevel@tonic-gate m->b_wptr = m->b_rptr + len; 18290Sstevel@tonic-gate bzero(m->b_rptr, len); 18303055Sdanmcd (void) spdsock_encode_rule_head(m->b_rptr, 0, mreq, rule, dir, af, 18313055Sdanmcd name, tunnel); 18320Sstevel@tonic-gate return (m); 18330Sstevel@tonic-gate } 18340Sstevel@tonic-gate 18350Sstevel@tonic-gate static ipsec_policy_t * 183681Ssommerfe spdsock_dump_next_in_chain(spdsock_t *ss, ipsec_policy_head_t *iph, 183781Ssommerfe ipsec_policy_t *cur) 183881Ssommerfe { 183981Ssommerfe ASSERT(RW_READ_HELD(&iph->iph_lock)); 184081Ssommerfe 184181Ssommerfe ss->spdsock_dump_count++; 184281Ssommerfe ss->spdsock_dump_cur_rule = cur->ipsp_hash.hash_next; 184381Ssommerfe return (cur); 184481Ssommerfe } 184581Ssommerfe 184681Ssommerfe static ipsec_policy_t * 18470Sstevel@tonic-gate spdsock_dump_next_rule(spdsock_t *ss, ipsec_policy_head_t *iph) 18480Sstevel@tonic-gate { 18490Sstevel@tonic-gate ipsec_policy_t *cur; 185081Ssommerfe ipsec_policy_root_t *ipr; 185181Ssommerfe int chain, nchains, type, af; 18520Sstevel@tonic-gate 18530Sstevel@tonic-gate ASSERT(RW_READ_HELD(&iph->iph_lock)); 18540Sstevel@tonic-gate 18550Sstevel@tonic-gate cur = ss->spdsock_dump_cur_rule; 18560Sstevel@tonic-gate 185781Ssommerfe if (cur != NULL) 185881Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 185981Ssommerfe 186081Ssommerfe type = ss->spdsock_dump_cur_type; 186181Ssommerfe 186281Ssommerfe next: 186381Ssommerfe chain = ss->spdsock_dump_cur_chain; 186481Ssommerfe ipr = &iph->iph_root[type]; 186581Ssommerfe nchains = ipr->ipr_nchains; 186681Ssommerfe 186781Ssommerfe while (chain < nchains) { 186881Ssommerfe cur = ipr->ipr_hash[chain].hash_head; 186981Ssommerfe chain++; 187081Ssommerfe if (cur != NULL) { 187181Ssommerfe ss->spdsock_dump_cur_chain = chain; 187281Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 187381Ssommerfe } 18740Sstevel@tonic-gate } 187581Ssommerfe ss->spdsock_dump_cur_chain = nchains; 187681Ssommerfe 187781Ssommerfe af = ss->spdsock_dump_cur_af; 187881Ssommerfe while (af < IPSEC_NAF) { 187981Ssommerfe cur = ipr->ipr_nonhash[af]; 188081Ssommerfe af++; 188181Ssommerfe if (cur != NULL) { 188281Ssommerfe ss->spdsock_dump_cur_af = af; 188381Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 188481Ssommerfe } 188581Ssommerfe } 188681Ssommerfe 188781Ssommerfe type++; 188881Ssommerfe if (type >= IPSEC_NTYPES) 188981Ssommerfe return (NULL); 189081Ssommerfe 189181Ssommerfe ss->spdsock_dump_cur_chain = 0; 189281Ssommerfe ss->spdsock_dump_cur_type = type; 189381Ssommerfe ss->spdsock_dump_cur_af = IPSEC_AF_V4; 189481Ssommerfe goto next; 189581Ssommerfe 18960Sstevel@tonic-gate } 18970Sstevel@tonic-gate 18986430Sdanmcd /* 18996430Sdanmcd * If we're done with one policy head, but have more to go, we iterate through 19006430Sdanmcd * another IPsec tunnel policy head (itp). Return NULL if it is an error 19016430Sdanmcd * worthy of returning EAGAIN via PF_POLICY. 19026430Sdanmcd */ 19036430Sdanmcd static ipsec_tun_pol_t * 19046430Sdanmcd spdsock_dump_iterate_next_tunnel(spdsock_t *ss, ipsec_stack_t *ipss) 19056430Sdanmcd { 19066430Sdanmcd ipsec_tun_pol_t *itp; 19076430Sdanmcd 19086430Sdanmcd ASSERT(RW_READ_HELD(&ipss->ipsec_tunnel_policy_lock)); 19096430Sdanmcd if (ipss->ipsec_tunnel_policy_gen > ss->spdsock_dump_tun_gen) { 19106430Sdanmcd /* Oops, state of the tunnel polheads changed. */ 19116430Sdanmcd itp = NULL; 19126430Sdanmcd } else if (ss->spdsock_itp == NULL) { 19136430Sdanmcd /* Just finished global, find first node. */ 19146430Sdanmcd itp = avl_first(&ipss->ipsec_tunnel_policies); 19156430Sdanmcd } else { 19166430Sdanmcd /* We just finished current polhead, find the next one. */ 19176430Sdanmcd itp = AVL_NEXT(&ipss->ipsec_tunnel_policies, ss->spdsock_itp); 19186430Sdanmcd } 19196430Sdanmcd if (itp != NULL) { 19206430Sdanmcd ITP_REFHOLD(itp); 19216430Sdanmcd } 19226430Sdanmcd if (ss->spdsock_itp != NULL) { 19236430Sdanmcd ITP_REFRELE(ss->spdsock_itp, ipss->ipsec_netstack); 19246430Sdanmcd } 19256430Sdanmcd ss->spdsock_itp = itp; 19266430Sdanmcd return (itp); 19276430Sdanmcd } 19286430Sdanmcd 19290Sstevel@tonic-gate static mblk_t * 19300Sstevel@tonic-gate spdsock_dump_next_record(spdsock_t *ss) 19310Sstevel@tonic-gate { 19320Sstevel@tonic-gate ipsec_policy_head_t *iph; 19330Sstevel@tonic-gate ipsec_policy_t *rule; 19340Sstevel@tonic-gate mblk_t *m; 19356430Sdanmcd ipsec_tun_pol_t *itp; 19366430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 19376430Sdanmcd ipsec_stack_t *ipss = ns->netstack_ipsec; 19380Sstevel@tonic-gate 19390Sstevel@tonic-gate iph = ss->spdsock_dump_head; 19400Sstevel@tonic-gate 19410Sstevel@tonic-gate ASSERT(iph != NULL); 19420Sstevel@tonic-gate 19430Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 19440Sstevel@tonic-gate 19450Sstevel@tonic-gate if (iph->iph_gen != ss->spdsock_dump_gen) { 19460Sstevel@tonic-gate rw_exit(&iph->iph_lock); 19470Sstevel@tonic-gate return (spdsock_dump_finish(ss, EAGAIN)); 19480Sstevel@tonic-gate } 19490Sstevel@tonic-gate 19506430Sdanmcd while ((rule = spdsock_dump_next_rule(ss, iph)) == NULL) { 19510Sstevel@tonic-gate rw_exit(&iph->iph_lock); 19526430Sdanmcd if (--(ss->spdsock_dump_remaining_polheads) == 0) 19536430Sdanmcd return (spdsock_dump_finish(ss, 0)); 19546430Sdanmcd 19556430Sdanmcd 19566430Sdanmcd /* 19576430Sdanmcd * If we reach here, we have more policy heads (tunnel 19586430Sdanmcd * entries) to dump. Let's reset to a new policy head 19596430Sdanmcd * and get some more rules. 19606430Sdanmcd * 19616430Sdanmcd * An empty policy head will have spdsock_dump_next_rule() 19626430Sdanmcd * return NULL, and we loop (while dropping the number of 19636430Sdanmcd * remaining polheads). If we loop to 0, we finish. We 19646430Sdanmcd * keep looping until we hit 0 or until we have a rule to 19656430Sdanmcd * encode. 19666430Sdanmcd * 19676430Sdanmcd * NOTE: No need for ITP_REF*() macros here as we're only 19686430Sdanmcd * going after and refholding the policy head itself. 19696430Sdanmcd */ 19706430Sdanmcd rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER); 19716430Sdanmcd itp = spdsock_dump_iterate_next_tunnel(ss, ipss); 19726430Sdanmcd if (itp == NULL) { 19736430Sdanmcd rw_exit(&ipss->ipsec_tunnel_policy_lock); 19746430Sdanmcd return (spdsock_dump_finish(ss, EAGAIN)); 19756430Sdanmcd } 19766430Sdanmcd 19776430Sdanmcd /* Reset other spdsock_dump thingies. */ 19786430Sdanmcd IPPH_REFRELE(ss->spdsock_dump_head, ns); 19796430Sdanmcd if (ss->spdsock_dump_active) { 19806430Sdanmcd ss->spdsock_dump_tunnel = 19816430Sdanmcd itp->itp_flags & ITPF_P_TUNNEL; 19826430Sdanmcd iph = itp->itp_policy; 19836430Sdanmcd } else { 19846430Sdanmcd ss->spdsock_dump_tunnel = 19856430Sdanmcd itp->itp_flags & ITPF_I_TUNNEL; 19866430Sdanmcd iph = itp->itp_inactive; 19876430Sdanmcd } 19886430Sdanmcd IPPH_REFHOLD(iph); 19896430Sdanmcd rw_exit(&ipss->ipsec_tunnel_policy_lock); 19906430Sdanmcd 19916430Sdanmcd rw_enter(&iph->iph_lock, RW_READER); 19926430Sdanmcd RESET_SPDSOCK_DUMP_POLHEAD(ss, iph); 19930Sstevel@tonic-gate } 19940Sstevel@tonic-gate 19956430Sdanmcd m = spdsock_encode_rule(ss->spdsock_dump_req, rule, 19966430Sdanmcd ss->spdsock_dump_cur_type, ss->spdsock_dump_cur_af, 19976466Sdanmcd (ss->spdsock_itp == NULL) ? NULL : ss->spdsock_itp->itp_name, 19986466Sdanmcd ss->spdsock_dump_tunnel); 19990Sstevel@tonic-gate rw_exit(&iph->iph_lock); 20000Sstevel@tonic-gate 20010Sstevel@tonic-gate if (m == NULL) 20020Sstevel@tonic-gate return (spdsock_dump_finish(ss, ENOMEM)); 20030Sstevel@tonic-gate return (m); 20040Sstevel@tonic-gate } 20050Sstevel@tonic-gate 20060Sstevel@tonic-gate /* 20070Sstevel@tonic-gate * Dump records until we run into flow-control back-pressure. 20080Sstevel@tonic-gate */ 20090Sstevel@tonic-gate static void 20100Sstevel@tonic-gate spdsock_dump_some(queue_t *q, spdsock_t *ss) 20110Sstevel@tonic-gate { 20120Sstevel@tonic-gate mblk_t *m, *dataind; 20130Sstevel@tonic-gate 20140Sstevel@tonic-gate while ((ss->spdsock_dump_req != NULL) && canputnext(q)) { 20150Sstevel@tonic-gate m = spdsock_dump_next_record(ss); 20160Sstevel@tonic-gate if (m == NULL) 20170Sstevel@tonic-gate return; 20180Sstevel@tonic-gate dataind = allocb(sizeof (struct T_data_req), BPRI_HI); 20190Sstevel@tonic-gate if (dataind == NULL) { 20200Sstevel@tonic-gate freemsg(m); 20210Sstevel@tonic-gate return; 20220Sstevel@tonic-gate } 20230Sstevel@tonic-gate dataind->b_cont = m; 20240Sstevel@tonic-gate dataind->b_wptr += sizeof (struct T_data_req); 20250Sstevel@tonic-gate ((struct T_data_ind *)dataind->b_rptr)->PRIM_type = T_DATA_IND; 20260Sstevel@tonic-gate ((struct T_data_ind *)dataind->b_rptr)->MORE_flag = 0; 20270Sstevel@tonic-gate dataind->b_datap->db_type = M_PROTO; 20280Sstevel@tonic-gate putnext(q, dataind); 20290Sstevel@tonic-gate } 20300Sstevel@tonic-gate } 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate /* 20330Sstevel@tonic-gate * Start dumping. 20340Sstevel@tonic-gate * Format a start-of-dump record, and set up the stream and kick the rsrv 20350Sstevel@tonic-gate * procedure to continue the job.. 20360Sstevel@tonic-gate */ 20370Sstevel@tonic-gate /* ARGSUSED */ 20380Sstevel@tonic-gate static void 20393055Sdanmcd spdsock_dump(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp) 20400Sstevel@tonic-gate { 20410Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 20426430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 20433448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 20440Sstevel@tonic-gate mblk_t *mr; 20450Sstevel@tonic-gate 20466430Sdanmcd /* spdsock_open() already set spdsock_itp to NULL. */ 20473055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) { 20483448Sdh155122 rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER); 20493055Sdanmcd ss->spdsock_dump_remaining_polheads = 1 + 20503448Sdh155122 avl_numnodes(&ipss->ipsec_tunnel_policies); 20513448Sdh155122 ss->spdsock_dump_tun_gen = ipss->ipsec_tunnel_policy_gen; 20523448Sdh155122 rw_exit(&ipss->ipsec_tunnel_policy_lock); 20533055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS) { 20543448Sdh155122 iph = ipsec_system_policy(ns); 20553055Sdanmcd ss->spdsock_dump_active = B_TRUE; 20563055Sdanmcd } else { 20576430Sdanmcd iph = ipsec_inactive_policy(ns); 20583055Sdanmcd ss->spdsock_dump_active = B_FALSE; 20593055Sdanmcd } 20606430Sdanmcd ASSERT(ss->spdsock_itp == NULL); 20613055Sdanmcd } else { 20623055Sdanmcd ss->spdsock_dump_remaining_polheads = 1; 20633055Sdanmcd } 20643055Sdanmcd 20650Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 20660Sstevel@tonic-gate 20670Sstevel@tonic-gate mr = spdsock_dump_ruleset(mp, iph, 0, 0); 20680Sstevel@tonic-gate 20690Sstevel@tonic-gate if (!mr) { 20700Sstevel@tonic-gate rw_exit(&iph->iph_lock); 20710Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 20720Sstevel@tonic-gate return; 20730Sstevel@tonic-gate } 20740Sstevel@tonic-gate 20750Sstevel@tonic-gate ss->spdsock_dump_req = mp; 20766430Sdanmcd RESET_SPDSOCK_DUMP_POLHEAD(ss, iph); 20776430Sdanmcd 20780Sstevel@tonic-gate rw_exit(&iph->iph_lock); 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate qreply(q, mr); 20810Sstevel@tonic-gate qenable(OTHERQ(q)); 20820Sstevel@tonic-gate } 20830Sstevel@tonic-gate 20843055Sdanmcd /* Do NOT consume a reference to ITP. */ 20850Sstevel@tonic-gate void 20863448Sdh155122 spdsock_clone_node(ipsec_tun_pol_t *itp, void *ep, netstack_t *ns) 20873055Sdanmcd { 20883055Sdanmcd int *errptr = (int *)ep; 20893055Sdanmcd 20903055Sdanmcd if (*errptr != 0) 20913055Sdanmcd return; /* We've failed already for some reason. */ 20923055Sdanmcd mutex_enter(&itp->itp_lock); 20933055Sdanmcd ITPF_CLONE(itp->itp_flags); 20943448Sdh155122 *errptr = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns); 20953055Sdanmcd mutex_exit(&itp->itp_lock); 20963055Sdanmcd } 20973055Sdanmcd 20983055Sdanmcd void 20993055Sdanmcd spdsock_clone(queue_t *q, mblk_t *mp, spd_if_t *tunname) 21000Sstevel@tonic-gate { 21013055Sdanmcd int error; 21023055Sdanmcd char *tname; 21033055Sdanmcd ipsec_tun_pol_t *itp; 21043448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 21056430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 21063055Sdanmcd 21073055Sdanmcd if (tunname != NULL) { 21083055Sdanmcd tname = (char *)tunname->spd_if_name; 21093055Sdanmcd if (*tname == '\0') { 21106430Sdanmcd error = ipsec_clone_system_policy(ns); 21114307Spwernau if (audit_active) { 21124307Spwernau boolean_t active; 21134307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 21148778SErik.Nordmark@Sun.COM cred_t *cr; 21158778SErik.Nordmark@Sun.COM pid_t cpid; 21168778SErik.Nordmark@Sun.COM 21178778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 21184307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 21198778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_CLONE, cr, ns, 21208778SErik.Nordmark@Sun.COM NULL, active, error, cpid); 21214307Spwernau } 21224307Spwernau if (error == 0) { 21236430Sdanmcd itp_walk(spdsock_clone_node, &error, ns); 21244307Spwernau if (audit_active) { 21254307Spwernau boolean_t active; 21264307Spwernau spd_msg_t *spmsg = 21274307Spwernau (spd_msg_t *)mp->b_rptr; 21288778SErik.Nordmark@Sun.COM cred_t *cr; 21298778SErik.Nordmark@Sun.COM pid_t cpid; 21308778SErik.Nordmark@Sun.COM 21318778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 21324307Spwernau active = (spmsg->spd_msg_spdid == 21334307Spwernau SPD_ACTIVE); 21348778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_CLONE, cr, 21356430Sdanmcd ns, "all tunnels", active, 0, 21368778SErik.Nordmark@Sun.COM cpid); 21374307Spwernau } 21384307Spwernau } 21393055Sdanmcd } else { 21406430Sdanmcd itp = get_tunnel_policy(tname, ns); 21413055Sdanmcd if (itp == NULL) { 21423055Sdanmcd spdsock_error(q, mp, ENOENT, 0); 21434307Spwernau if (audit_active) { 21444307Spwernau boolean_t active; 21454307Spwernau spd_msg_t *spmsg = 21464307Spwernau (spd_msg_t *)mp->b_rptr; 21478778SErik.Nordmark@Sun.COM cred_t *cr; 21488778SErik.Nordmark@Sun.COM pid_t cpid; 21498778SErik.Nordmark@Sun.COM 21508778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 21514307Spwernau active = (spmsg->spd_msg_spdid == 21524307Spwernau SPD_ACTIVE); 21538778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_CLONE, cr, 215410764Sdanmcd@sun.com ns, NULL, active, ENOENT, cpid); 21554307Spwernau } 21563055Sdanmcd return; 21573055Sdanmcd } 21583448Sdh155122 spdsock_clone_node(itp, &error, NULL); 21594307Spwernau if (audit_active) { 21604307Spwernau boolean_t active; 21614307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 21628778SErik.Nordmark@Sun.COM cred_t *cr; 21638778SErik.Nordmark@Sun.COM pid_t cpid; 21648778SErik.Nordmark@Sun.COM 21658778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 21664307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 21678778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_CLONE, cr, ns, 21688778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, error, cpid); 21694307Spwernau } 217010764Sdanmcd@sun.com ITP_REFRELE(itp, ns); 21713055Sdanmcd } 21723055Sdanmcd } else { 21736430Sdanmcd error = ipsec_clone_system_policy(ns); 21744307Spwernau if (audit_active) { 21754307Spwernau boolean_t active; 21764307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 21778778SErik.Nordmark@Sun.COM cred_t *cr; 21788778SErik.Nordmark@Sun.COM pid_t cpid; 21798778SErik.Nordmark@Sun.COM 21808778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 21814307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 21828778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_CLONE, cr, ns, NULL, 21838778SErik.Nordmark@Sun.COM active, error, cpid); 21844307Spwernau } 21853055Sdanmcd } 21863055Sdanmcd 21870Sstevel@tonic-gate if (error != 0) 21880Sstevel@tonic-gate spdsock_error(q, mp, error, 0); 21890Sstevel@tonic-gate else 21900Sstevel@tonic-gate spd_echo(q, mp); 21910Sstevel@tonic-gate } 21920Sstevel@tonic-gate 21930Sstevel@tonic-gate /* 21940Sstevel@tonic-gate * Process a SPD_ALGLIST request. The caller expects separate alg entries 21950Sstevel@tonic-gate * for AH authentication, ESP authentication, and ESP encryption. 21960Sstevel@tonic-gate * The same distinction is then used when setting the min and max key 21970Sstevel@tonic-gate * sizes when defining policies. 21980Sstevel@tonic-gate */ 21990Sstevel@tonic-gate 22000Sstevel@tonic-gate #define SPDSOCK_AH_AUTH 0 22010Sstevel@tonic-gate #define SPDSOCK_ESP_AUTH 1 22020Sstevel@tonic-gate #define SPDSOCK_ESP_ENCR 2 22030Sstevel@tonic-gate #define SPDSOCK_NTYPES 3 22040Sstevel@tonic-gate 22050Sstevel@tonic-gate static const uint_t algattr[SPDSOCK_NTYPES] = { 22060Sstevel@tonic-gate SPD_ATTR_AH_AUTH, 22070Sstevel@tonic-gate SPD_ATTR_ESP_AUTH, 22080Sstevel@tonic-gate SPD_ATTR_ESP_ENCR 22090Sstevel@tonic-gate }; 22100Sstevel@tonic-gate static const uint_t minbitsattr[SPDSOCK_NTYPES] = { 22110Sstevel@tonic-gate SPD_ATTR_AH_MINBITS, 22120Sstevel@tonic-gate SPD_ATTR_ESPA_MINBITS, 22130Sstevel@tonic-gate SPD_ATTR_ENCR_MINBITS 22140Sstevel@tonic-gate }; 22150Sstevel@tonic-gate static const uint_t maxbitsattr[SPDSOCK_NTYPES] = { 22160Sstevel@tonic-gate SPD_ATTR_AH_MAXBITS, 22170Sstevel@tonic-gate SPD_ATTR_ESPA_MAXBITS, 22180Sstevel@tonic-gate SPD_ATTR_ENCR_MAXBITS 22190Sstevel@tonic-gate }; 22200Sstevel@tonic-gate static const uint_t defbitsattr[SPDSOCK_NTYPES] = { 22210Sstevel@tonic-gate SPD_ATTR_AH_DEFBITS, 22220Sstevel@tonic-gate SPD_ATTR_ESPA_DEFBITS, 22230Sstevel@tonic-gate SPD_ATTR_ENCR_DEFBITS 22240Sstevel@tonic-gate }; 22250Sstevel@tonic-gate static const uint_t incrbitsattr[SPDSOCK_NTYPES] = { 22260Sstevel@tonic-gate SPD_ATTR_AH_INCRBITS, 22270Sstevel@tonic-gate SPD_ATTR_ESPA_INCRBITS, 22280Sstevel@tonic-gate SPD_ATTR_ENCR_INCRBITS 22290Sstevel@tonic-gate }; 22300Sstevel@tonic-gate 22310Sstevel@tonic-gate #define ATTRPERALG 6 /* fixed attributes per algs */ 22320Sstevel@tonic-gate 22330Sstevel@tonic-gate void 22340Sstevel@tonic-gate spdsock_alglist(queue_t *q, mblk_t *mp) 22350Sstevel@tonic-gate { 22360Sstevel@tonic-gate uint_t algtype; 22370Sstevel@tonic-gate uint_t algidx; 22380Sstevel@tonic-gate uint_t algcount; 22390Sstevel@tonic-gate uint_t size; 22400Sstevel@tonic-gate mblk_t *m; 22410Sstevel@tonic-gate uint8_t *cur; 22420Sstevel@tonic-gate spd_msg_t *msg; 22430Sstevel@tonic-gate struct spd_ext_actions *act; 22440Sstevel@tonic-gate struct spd_attribute *attr; 22453448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 22466430Sdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 22473448Sdh155122 22483448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 22490Sstevel@tonic-gate /* 22500Sstevel@tonic-gate * The SPD client expects to receive separate entries for 22510Sstevel@tonic-gate * AH authentication and ESP authentication supported algorithms. 22520Sstevel@tonic-gate * 22530Sstevel@tonic-gate * Don't return the "any" algorithms, if defined, as no 22540Sstevel@tonic-gate * kernel policies can be set for these algorithms. 22550Sstevel@tonic-gate */ 22563448Sdh155122 algcount = 2 * ipss->ipsec_nalgs[IPSEC_ALG_AUTH] + 22573448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 22583448Sdh155122 22593448Sdh155122 if (ipss->ipsec_alglists[IPSEC_ALG_AUTH][SADB_AALG_NONE] != NULL) 22600Sstevel@tonic-gate algcount--; 22613448Sdh155122 if (ipss->ipsec_alglists[IPSEC_ALG_ENCR][SADB_EALG_NONE] != NULL) 22620Sstevel@tonic-gate algcount--; 22630Sstevel@tonic-gate 22640Sstevel@tonic-gate /* 22650Sstevel@tonic-gate * For each algorithm, we encode: 22660Sstevel@tonic-gate * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT} 22670Sstevel@tonic-gate */ 22680Sstevel@tonic-gate 22690Sstevel@tonic-gate size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions) + 22700Sstevel@tonic-gate ATTRPERALG * sizeof (struct spd_attribute) * algcount; 22710Sstevel@tonic-gate 22720Sstevel@tonic-gate ASSERT(ALIGNED64(size)); 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate m = allocb(size, BPRI_HI); 22750Sstevel@tonic-gate if (m == NULL) { 22763448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 22770Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 22780Sstevel@tonic-gate return; 22790Sstevel@tonic-gate } 22800Sstevel@tonic-gate 22810Sstevel@tonic-gate m->b_wptr = m->b_rptr + size; 22820Sstevel@tonic-gate cur = m->b_rptr; 22830Sstevel@tonic-gate 22840Sstevel@tonic-gate msg = (spd_msg_t *)cur; 22850Sstevel@tonic-gate bcopy(mp->b_rptr, cur, sizeof (*msg)); 22860Sstevel@tonic-gate 22870Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(size); 22880Sstevel@tonic-gate msg->spd_msg_errno = 0; 22890Sstevel@tonic-gate msg->spd_msg_diagnostic = 0; 22900Sstevel@tonic-gate 22910Sstevel@tonic-gate cur += sizeof (*msg); 22920Sstevel@tonic-gate 22930Sstevel@tonic-gate act = (struct spd_ext_actions *)cur; 22940Sstevel@tonic-gate cur += sizeof (*act); 22950Sstevel@tonic-gate 22960Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t)); 22970Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 22980Sstevel@tonic-gate act->spd_actions_count = algcount; 22990Sstevel@tonic-gate act->spd_actions_reserved = 0; 23000Sstevel@tonic-gate 23010Sstevel@tonic-gate attr = (struct spd_attribute *)cur; 23020Sstevel@tonic-gate 23030Sstevel@tonic-gate #define EMIT(tag, value) { \ 23040Sstevel@tonic-gate attr->spd_attr_tag = (tag); \ 23050Sstevel@tonic-gate attr->spd_attr_value = (value); \ 23060Sstevel@tonic-gate attr++; \ 23070Sstevel@tonic-gate } 23080Sstevel@tonic-gate 23090Sstevel@tonic-gate /* 23100Sstevel@tonic-gate * If you change the number of EMIT's here, change 23110Sstevel@tonic-gate * ATTRPERALG above to match 23120Sstevel@tonic-gate */ 23130Sstevel@tonic-gate #define EMITALGATTRS(_type) { \ 23140Sstevel@tonic-gate EMIT(algattr[_type], algid); /* 1 */ \ 23150Sstevel@tonic-gate EMIT(minbitsattr[_type], minbits); /* 2 */ \ 23160Sstevel@tonic-gate EMIT(maxbitsattr[_type], maxbits); /* 3 */ \ 23170Sstevel@tonic-gate EMIT(defbitsattr[_type], defbits); /* 4 */ \ 23180Sstevel@tonic-gate EMIT(incrbitsattr[_type], incr); /* 5 */ \ 23190Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); /* 6 */ \ 23200Sstevel@tonic-gate } 23210Sstevel@tonic-gate 23220Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 23233448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 23243448Sdh155122 algidx++) { 23253448Sdh155122 int algid = ipss->ipsec_sortlist[algtype][algidx]; 23263448Sdh155122 ipsec_alginfo_t *alg = 23273448Sdh155122 ipss->ipsec_alglists[algtype][algid]; 23280Sstevel@tonic-gate uint_t minbits = alg->alg_minbits; 23290Sstevel@tonic-gate uint_t maxbits = alg->alg_maxbits; 23300Sstevel@tonic-gate uint_t defbits = alg->alg_default_bits; 23310Sstevel@tonic-gate uint_t incr = alg->alg_increment; 23320Sstevel@tonic-gate 23330Sstevel@tonic-gate if (algtype == IPSEC_ALG_AUTH) { 23340Sstevel@tonic-gate if (algid == SADB_AALG_NONE) 23350Sstevel@tonic-gate continue; 23360Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_AH_AUTH); 23370Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_ESP_AUTH); 23380Sstevel@tonic-gate } else { 23390Sstevel@tonic-gate if (algid == SADB_EALG_NONE) 23400Sstevel@tonic-gate continue; 23410Sstevel@tonic-gate ASSERT(algtype == IPSEC_ALG_ENCR); 23420Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_ESP_ENCR); 23430Sstevel@tonic-gate } 23440Sstevel@tonic-gate } 23450Sstevel@tonic-gate } 23460Sstevel@tonic-gate 23473448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 23480Sstevel@tonic-gate 23490Sstevel@tonic-gate #undef EMITALGATTRS 23500Sstevel@tonic-gate #undef EMIT 23510Sstevel@tonic-gate #undef ATTRPERALG 23520Sstevel@tonic-gate 23530Sstevel@tonic-gate attr--; 23540Sstevel@tonic-gate attr->spd_attr_tag = SPD_ATTR_END; 23550Sstevel@tonic-gate 23560Sstevel@tonic-gate freemsg(mp); 23570Sstevel@tonic-gate qreply(q, m); 23580Sstevel@tonic-gate } 23590Sstevel@tonic-gate 23600Sstevel@tonic-gate /* 23610Sstevel@tonic-gate * Process a SPD_DUMPALGS request. 23620Sstevel@tonic-gate */ 23630Sstevel@tonic-gate 2364*10824SMark.Fenwick@Sun.COM #define ATTRPERALG 9 /* fixed attributes per algs */ 23650Sstevel@tonic-gate 23660Sstevel@tonic-gate void 23670Sstevel@tonic-gate spdsock_dumpalgs(queue_t *q, mblk_t *mp) 23680Sstevel@tonic-gate { 23690Sstevel@tonic-gate uint_t algtype; 23700Sstevel@tonic-gate uint_t algidx; 23710Sstevel@tonic-gate uint_t size; 23720Sstevel@tonic-gate mblk_t *m; 23730Sstevel@tonic-gate uint8_t *cur; 23740Sstevel@tonic-gate spd_msg_t *msg; 23750Sstevel@tonic-gate struct spd_ext_actions *act; 23760Sstevel@tonic-gate struct spd_attribute *attr; 23770Sstevel@tonic-gate ipsec_alginfo_t *alg; 23780Sstevel@tonic-gate uint_t algid; 23790Sstevel@tonic-gate uint_t i; 23800Sstevel@tonic-gate uint_t alg_size; 23813448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 23826430Sdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 23833448Sdh155122 23843448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 23850Sstevel@tonic-gate 23860Sstevel@tonic-gate /* 23870Sstevel@tonic-gate * For each algorithm, we encode: 23880Sstevel@tonic-gate * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT} 23890Sstevel@tonic-gate * 23900Sstevel@tonic-gate * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE* 2391*10824SMark.Fenwick@Sun.COM * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_NPARAMS / ALG_PARAMS* / 2392*10824SMark.Fenwick@Sun.COM * ALG_MECHNAME / ALG_FLAGS / {END, NEXT} 23930Sstevel@tonic-gate */ 23940Sstevel@tonic-gate 23950Sstevel@tonic-gate /* 23960Sstevel@tonic-gate * Compute the size of the SPD message. 23970Sstevel@tonic-gate */ 23980Sstevel@tonic-gate size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions); 23990Sstevel@tonic-gate 24000Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 24013448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 24023448Sdh155122 algidx++) { 24033448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 24043448Sdh155122 alg = ipss->ipsec_alglists[algtype][algid]; 24050Sstevel@tonic-gate alg_size = sizeof (struct spd_attribute) * 24060Sstevel@tonic-gate (ATTRPERALG + alg->alg_nkey_sizes + 2407*10824SMark.Fenwick@Sun.COM alg->alg_nblock_sizes + alg->alg_nparams) + 2408*10824SMark.Fenwick@Sun.COM CRYPTO_MAX_MECH_NAME; 24090Sstevel@tonic-gate size += alg_size; 24100Sstevel@tonic-gate } 24110Sstevel@tonic-gate } 24120Sstevel@tonic-gate 24130Sstevel@tonic-gate ASSERT(ALIGNED64(size)); 24140Sstevel@tonic-gate 24150Sstevel@tonic-gate m = allocb(size, BPRI_HI); 24160Sstevel@tonic-gate if (m == NULL) { 24173448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 24180Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 24190Sstevel@tonic-gate return; 24200Sstevel@tonic-gate } 24210Sstevel@tonic-gate 24220Sstevel@tonic-gate m->b_wptr = m->b_rptr + size; 24230Sstevel@tonic-gate cur = m->b_rptr; 24240Sstevel@tonic-gate 24250Sstevel@tonic-gate msg = (spd_msg_t *)cur; 24260Sstevel@tonic-gate bcopy(mp->b_rptr, cur, sizeof (*msg)); 24270Sstevel@tonic-gate 24280Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(size); 24290Sstevel@tonic-gate msg->spd_msg_errno = 0; 243010019SMark.Fenwick@Sun.COM msg->spd_msg_type = SPD_ALGLIST; 243110019SMark.Fenwick@Sun.COM 24320Sstevel@tonic-gate msg->spd_msg_diagnostic = 0; 24330Sstevel@tonic-gate 24340Sstevel@tonic-gate cur += sizeof (*msg); 24350Sstevel@tonic-gate 24360Sstevel@tonic-gate act = (struct spd_ext_actions *)cur; 24370Sstevel@tonic-gate cur += sizeof (*act); 24380Sstevel@tonic-gate 24390Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t)); 24400Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 24413448Sdh155122 act->spd_actions_count = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] + 24423448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 24430Sstevel@tonic-gate act->spd_actions_reserved = 0; 24440Sstevel@tonic-gate 244510019SMark.Fenwick@Sun.COM /* 244610019SMark.Fenwick@Sun.COM * If there aren't any algorithms registered, return an empty message. 244710019SMark.Fenwick@Sun.COM * spdsock_get_ext() knows how to deal with this. 244810019SMark.Fenwick@Sun.COM */ 244910019SMark.Fenwick@Sun.COM if (act->spd_actions_count == 0) { 245010019SMark.Fenwick@Sun.COM act->spd_actions_len = 0; 245110019SMark.Fenwick@Sun.COM mutex_exit(&ipss->ipsec_alg_lock); 245210019SMark.Fenwick@Sun.COM goto error; 245310019SMark.Fenwick@Sun.COM } 245410019SMark.Fenwick@Sun.COM 24550Sstevel@tonic-gate attr = (struct spd_attribute *)cur; 24560Sstevel@tonic-gate 24570Sstevel@tonic-gate #define EMIT(tag, value) { \ 24580Sstevel@tonic-gate attr->spd_attr_tag = (tag); \ 24590Sstevel@tonic-gate attr->spd_attr_value = (value); \ 24600Sstevel@tonic-gate attr++; \ 24610Sstevel@tonic-gate } 24620Sstevel@tonic-gate 24630Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 24643448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 24653448Sdh155122 algidx++) { 24663448Sdh155122 24673448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 24683448Sdh155122 alg = ipss->ipsec_alglists[algtype][algid]; 24690Sstevel@tonic-gate 24700Sstevel@tonic-gate /* 24710Sstevel@tonic-gate * If you change the number of EMIT's here, change 24720Sstevel@tonic-gate * ATTRPERALG above to match 24730Sstevel@tonic-gate */ 24740Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_ID, algid); 24750Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_PROTO, algproto[algtype]); 24760Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_INCRBITS, alg->alg_increment); 24770Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_NKEYSIZES, alg->alg_nkey_sizes); 24780Sstevel@tonic-gate for (i = 0; i < alg->alg_nkey_sizes; i++) 24790Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_KEYSIZE, 24800Sstevel@tonic-gate alg->alg_key_sizes[i]); 24810Sstevel@tonic-gate 24820Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_NBLOCKSIZES, alg->alg_nblock_sizes); 24830Sstevel@tonic-gate for (i = 0; i < alg->alg_nblock_sizes; i++) 24840Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_BLOCKSIZE, 24850Sstevel@tonic-gate alg->alg_block_sizes[i]); 24860Sstevel@tonic-gate 2487*10824SMark.Fenwick@Sun.COM EMIT(SPD_ATTR_ALG_NPARAMS, alg->alg_nparams); 2488*10824SMark.Fenwick@Sun.COM for (i = 0; i < alg->alg_nparams; i++) 2489*10824SMark.Fenwick@Sun.COM EMIT(SPD_ATTR_ALG_PARAMS, 2490*10824SMark.Fenwick@Sun.COM alg->alg_params[i]); 2491*10824SMark.Fenwick@Sun.COM 2492*10824SMark.Fenwick@Sun.COM EMIT(SPD_ATTR_ALG_FLAGS, alg->alg_flags); 2493*10824SMark.Fenwick@Sun.COM 24940Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_MECHNAME, CRYPTO_MAX_MECH_NAME); 24950Sstevel@tonic-gate bcopy(alg->alg_mech_name, attr, CRYPTO_MAX_MECH_NAME); 24960Sstevel@tonic-gate attr = (struct spd_attribute *)((char *)attr + 24970Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME); 24980Sstevel@tonic-gate 24990Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); 25000Sstevel@tonic-gate } 25010Sstevel@tonic-gate } 25020Sstevel@tonic-gate 25033448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 25040Sstevel@tonic-gate 25050Sstevel@tonic-gate #undef EMITALGATTRS 25060Sstevel@tonic-gate #undef EMIT 25070Sstevel@tonic-gate #undef ATTRPERALG 25080Sstevel@tonic-gate 25090Sstevel@tonic-gate attr--; 25100Sstevel@tonic-gate attr->spd_attr_tag = SPD_ATTR_END; 25110Sstevel@tonic-gate 251210019SMark.Fenwick@Sun.COM error: 25130Sstevel@tonic-gate freemsg(mp); 25140Sstevel@tonic-gate qreply(q, m); 25150Sstevel@tonic-gate } 25160Sstevel@tonic-gate 25170Sstevel@tonic-gate /* 25180Sstevel@tonic-gate * Do the actual work of processing an SPD_UPDATEALGS request. Can 25190Sstevel@tonic-gate * be invoked either once IPsec is loaded on a cached request, or 25200Sstevel@tonic-gate * when a request is received while IPsec is loaded. 25210Sstevel@tonic-gate */ 25220Sstevel@tonic-gate static void 25233448Sdh155122 spdsock_do_updatealg(spd_ext_t *extv[], int *diag, spd_stack_t *spds) 25240Sstevel@tonic-gate { 25250Sstevel@tonic-gate struct spd_ext_actions *actp; 25260Sstevel@tonic-gate struct spd_attribute *attr, *endattr; 25270Sstevel@tonic-gate uint64_t *start, *end; 25280Sstevel@tonic-gate ipsec_alginfo_t *alg = NULL; 25290Sstevel@tonic-gate ipsec_algtype_t alg_type = 0; 25300Sstevel@tonic-gate boolean_t skip_alg = B_TRUE, doing_proto = B_FALSE; 25310Sstevel@tonic-gate uint_t i, cur_key, cur_block, algid; 25320Sstevel@tonic-gate 25330Sstevel@tonic-gate *diag = -1; 25343448Sdh155122 ASSERT(MUTEX_HELD(&spds->spds_alg_lock)); 25350Sstevel@tonic-gate 25360Sstevel@tonic-gate /* parse the message, building the list of algorithms */ 25370Sstevel@tonic-gate 25380Sstevel@tonic-gate actp = (struct spd_ext_actions *)extv[SPD_EXT_ACTION]; 25390Sstevel@tonic-gate if (actp == NULL) { 25400Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_NO_ACTION_EXT; 25410Sstevel@tonic-gate return; 25420Sstevel@tonic-gate } 25430Sstevel@tonic-gate 25440Sstevel@tonic-gate start = (uint64_t *)actp; 25450Sstevel@tonic-gate end = (start + actp->spd_actions_len); 25460Sstevel@tonic-gate endattr = (struct spd_attribute *)end; 25470Sstevel@tonic-gate attr = (struct spd_attribute *)&actp[1]; 25480Sstevel@tonic-gate 25493448Sdh155122 bzero(spds->spds_algs, IPSEC_NALGTYPES * IPSEC_MAX_ALGS * 25500Sstevel@tonic-gate sizeof (ipsec_alginfo_t *)); 25510Sstevel@tonic-gate 25520Sstevel@tonic-gate alg = kmem_zalloc(sizeof (*alg), KM_SLEEP); 25530Sstevel@tonic-gate 25540Sstevel@tonic-gate #define ALG_KEY_SIZES(a) (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t)) 25550Sstevel@tonic-gate #define ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t)) 25560Sstevel@tonic-gate 25570Sstevel@tonic-gate while (attr < endattr) { 25580Sstevel@tonic-gate switch (attr->spd_attr_tag) { 25590Sstevel@tonic-gate case SPD_ATTR_NOP: 25600Sstevel@tonic-gate case SPD_ATTR_EMPTY: 25610Sstevel@tonic-gate break; 25620Sstevel@tonic-gate case SPD_ATTR_END: 25630Sstevel@tonic-gate attr = endattr; 25640Sstevel@tonic-gate /* FALLTHRU */ 25650Sstevel@tonic-gate case SPD_ATTR_NEXT: 25660Sstevel@tonic-gate if (doing_proto) { 25670Sstevel@tonic-gate doing_proto = B_FALSE; 25680Sstevel@tonic-gate break; 25690Sstevel@tonic-gate } 25700Sstevel@tonic-gate if (skip_alg) { 25710Sstevel@tonic-gate ipsec_alg_free(alg); 25720Sstevel@tonic-gate } else { 25730Sstevel@tonic-gate ipsec_alg_free( 25743448Sdh155122 spds->spds_algs[alg_type][alg->alg_id]); 25753448Sdh155122 spds->spds_algs[alg_type][alg->alg_id] = 25763448Sdh155122 alg; 25770Sstevel@tonic-gate } 25780Sstevel@tonic-gate alg = kmem_zalloc(sizeof (*alg), KM_SLEEP); 25790Sstevel@tonic-gate break; 25800Sstevel@tonic-gate 25810Sstevel@tonic-gate case SPD_ATTR_ALG_ID: 25820Sstevel@tonic-gate if (attr->spd_attr_value >= IPSEC_MAX_ALGS) { 25833448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 25840Sstevel@tonic-gate "invalid alg id %d\n", 25850Sstevel@tonic-gate attr->spd_attr_value)); 25860Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_ID_RANGE; 25870Sstevel@tonic-gate goto bail; 25880Sstevel@tonic-gate } 25890Sstevel@tonic-gate alg->alg_id = attr->spd_attr_value; 25900Sstevel@tonic-gate break; 25910Sstevel@tonic-gate 25920Sstevel@tonic-gate case SPD_ATTR_ALG_PROTO: 25930Sstevel@tonic-gate /* find the alg type */ 25940Sstevel@tonic-gate for (i = 0; i < NALGPROTOS; i++) 25950Sstevel@tonic-gate if (algproto[i] == attr->spd_attr_value) 25960Sstevel@tonic-gate break; 25970Sstevel@tonic-gate skip_alg = (i == NALGPROTOS); 25980Sstevel@tonic-gate if (!skip_alg) 25990Sstevel@tonic-gate alg_type = i; 26000Sstevel@tonic-gate break; 26010Sstevel@tonic-gate 26020Sstevel@tonic-gate case SPD_ATTR_ALG_INCRBITS: 26030Sstevel@tonic-gate alg->alg_increment = attr->spd_attr_value; 26040Sstevel@tonic-gate break; 26050Sstevel@tonic-gate 26060Sstevel@tonic-gate case SPD_ATTR_ALG_NKEYSIZES: 26070Sstevel@tonic-gate if (alg->alg_key_sizes != NULL) { 26080Sstevel@tonic-gate kmem_free(alg->alg_key_sizes, 26090Sstevel@tonic-gate ALG_KEY_SIZES(alg)); 26100Sstevel@tonic-gate } 26110Sstevel@tonic-gate alg->alg_nkey_sizes = attr->spd_attr_value; 26120Sstevel@tonic-gate /* 26130Sstevel@tonic-gate * Allocate room for the trailing zero key size 26140Sstevel@tonic-gate * value as well. 26150Sstevel@tonic-gate */ 26160Sstevel@tonic-gate alg->alg_key_sizes = kmem_zalloc(ALG_KEY_SIZES(alg), 26170Sstevel@tonic-gate KM_SLEEP); 26180Sstevel@tonic-gate cur_key = 0; 26190Sstevel@tonic-gate break; 26200Sstevel@tonic-gate 26210Sstevel@tonic-gate case SPD_ATTR_ALG_KEYSIZE: 26220Sstevel@tonic-gate if (alg->alg_key_sizes == NULL || 26230Sstevel@tonic-gate cur_key >= alg->alg_nkey_sizes) { 26243448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 26255240Snordmark "too many key sizes\n")); 26260Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES; 26270Sstevel@tonic-gate goto bail; 26280Sstevel@tonic-gate } 26290Sstevel@tonic-gate alg->alg_key_sizes[cur_key++] = attr->spd_attr_value; 26300Sstevel@tonic-gate break; 26310Sstevel@tonic-gate 2632*10824SMark.Fenwick@Sun.COM case SPD_ATTR_ALG_FLAGS: 2633*10824SMark.Fenwick@Sun.COM /* 2634*10824SMark.Fenwick@Sun.COM * Flags (bit mask). The alg_flags element of 2635*10824SMark.Fenwick@Sun.COM * ipsecalg_flags_t is only 8 bits wide. The 2636*10824SMark.Fenwick@Sun.COM * user can set the VALID bit, but we will ignore it 2637*10824SMark.Fenwick@Sun.COM * and make the decision is the algorithm is valid. 2638*10824SMark.Fenwick@Sun.COM */ 2639*10824SMark.Fenwick@Sun.COM alg->alg_flags |= (uint8_t)attr->spd_attr_value; 2640*10824SMark.Fenwick@Sun.COM break; 2641*10824SMark.Fenwick@Sun.COM 26420Sstevel@tonic-gate case SPD_ATTR_ALG_NBLOCKSIZES: 26430Sstevel@tonic-gate if (alg->alg_block_sizes != NULL) { 26440Sstevel@tonic-gate kmem_free(alg->alg_block_sizes, 26450Sstevel@tonic-gate ALG_BLOCK_SIZES(alg)); 26460Sstevel@tonic-gate } 26470Sstevel@tonic-gate alg->alg_nblock_sizes = attr->spd_attr_value; 26480Sstevel@tonic-gate /* 26490Sstevel@tonic-gate * Allocate room for the trailing zero block size 26500Sstevel@tonic-gate * value as well. 26510Sstevel@tonic-gate */ 26520Sstevel@tonic-gate alg->alg_block_sizes = kmem_zalloc(ALG_BLOCK_SIZES(alg), 26530Sstevel@tonic-gate KM_SLEEP); 26540Sstevel@tonic-gate cur_block = 0; 26550Sstevel@tonic-gate break; 26560Sstevel@tonic-gate 26570Sstevel@tonic-gate case SPD_ATTR_ALG_BLOCKSIZE: 26580Sstevel@tonic-gate if (alg->alg_block_sizes == NULL || 26590Sstevel@tonic-gate cur_block >= alg->alg_nblock_sizes) { 26603448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 26615240Snordmark "too many block sizes\n")); 26620Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES; 26630Sstevel@tonic-gate goto bail; 26640Sstevel@tonic-gate } 26650Sstevel@tonic-gate alg->alg_block_sizes[cur_block++] = 26660Sstevel@tonic-gate attr->spd_attr_value; 26670Sstevel@tonic-gate break; 26680Sstevel@tonic-gate 2669*10824SMark.Fenwick@Sun.COM case SPD_ATTR_ALG_NPARAMS: 2670*10824SMark.Fenwick@Sun.COM if (alg->alg_params != NULL) { 2671*10824SMark.Fenwick@Sun.COM kmem_free(alg->alg_params, 2672*10824SMark.Fenwick@Sun.COM ALG_BLOCK_SIZES(alg)); 2673*10824SMark.Fenwick@Sun.COM } 2674*10824SMark.Fenwick@Sun.COM alg->alg_nparams = attr->spd_attr_value; 2675*10824SMark.Fenwick@Sun.COM /* 2676*10824SMark.Fenwick@Sun.COM * Allocate room for the trailing zero block size 2677*10824SMark.Fenwick@Sun.COM * value as well. 2678*10824SMark.Fenwick@Sun.COM */ 2679*10824SMark.Fenwick@Sun.COM alg->alg_params = kmem_zalloc(ALG_BLOCK_SIZES(alg), 2680*10824SMark.Fenwick@Sun.COM KM_SLEEP); 2681*10824SMark.Fenwick@Sun.COM cur_block = 0; 2682*10824SMark.Fenwick@Sun.COM break; 2683*10824SMark.Fenwick@Sun.COM 2684*10824SMark.Fenwick@Sun.COM case SPD_ATTR_ALG_PARAMS: 2685*10824SMark.Fenwick@Sun.COM if (alg->alg_params == NULL || 2686*10824SMark.Fenwick@Sun.COM cur_block >= alg->alg_nparams) { 2687*10824SMark.Fenwick@Sun.COM ss1dbg(spds, ("spdsock_do_updatealg: " 2688*10824SMark.Fenwick@Sun.COM "too many params\n")); 2689*10824SMark.Fenwick@Sun.COM *diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES; 2690*10824SMark.Fenwick@Sun.COM goto bail; 2691*10824SMark.Fenwick@Sun.COM } 2692*10824SMark.Fenwick@Sun.COM /* 2693*10824SMark.Fenwick@Sun.COM * Array contains: iv_len, icv_len, salt_len 2694*10824SMark.Fenwick@Sun.COM * Any additional parameters are currently ignored. 2695*10824SMark.Fenwick@Sun.COM */ 2696*10824SMark.Fenwick@Sun.COM alg->alg_params[cur_block++] = 2697*10824SMark.Fenwick@Sun.COM attr->spd_attr_value; 2698*10824SMark.Fenwick@Sun.COM break; 2699*10824SMark.Fenwick@Sun.COM 27000Sstevel@tonic-gate case SPD_ATTR_ALG_MECHNAME: { 27010Sstevel@tonic-gate char *mech_name; 27020Sstevel@tonic-gate 27030Sstevel@tonic-gate if (attr->spd_attr_value > CRYPTO_MAX_MECH_NAME) { 27043448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 27055240Snordmark "mech name too long\n")); 27060Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN; 27070Sstevel@tonic-gate goto bail; 27080Sstevel@tonic-gate } 27090Sstevel@tonic-gate mech_name = (char *)(attr + 1); 27100Sstevel@tonic-gate bcopy(mech_name, alg->alg_mech_name, 27110Sstevel@tonic-gate attr->spd_attr_value); 27120Sstevel@tonic-gate alg->alg_mech_name[CRYPTO_MAX_MECH_NAME-1] = '\0'; 27130Sstevel@tonic-gate attr = (struct spd_attribute *)((char *)attr + 27140Sstevel@tonic-gate attr->spd_attr_value); 27150Sstevel@tonic-gate break; 27160Sstevel@tonic-gate } 27170Sstevel@tonic-gate 27180Sstevel@tonic-gate case SPD_ATTR_PROTO_ID: 27190Sstevel@tonic-gate doing_proto = B_TRUE; 27200Sstevel@tonic-gate for (i = 0; i < NALGPROTOS; i++) { 27210Sstevel@tonic-gate if (algproto[i] == attr->spd_attr_value) { 27220Sstevel@tonic-gate alg_type = i; 27230Sstevel@tonic-gate break; 27240Sstevel@tonic-gate } 27250Sstevel@tonic-gate } 27260Sstevel@tonic-gate break; 27270Sstevel@tonic-gate 27280Sstevel@tonic-gate case SPD_ATTR_PROTO_EXEC_MODE: 27290Sstevel@tonic-gate if (!doing_proto) 27300Sstevel@tonic-gate break; 27310Sstevel@tonic-gate for (i = 0; i < NEXECMODES; i++) { 27320Sstevel@tonic-gate if (execmodes[i] == attr->spd_attr_value) { 27333448Sdh155122 spds->spds_algs_exec_mode[alg_type] = i; 27340Sstevel@tonic-gate break; 27350Sstevel@tonic-gate } 27360Sstevel@tonic-gate } 27370Sstevel@tonic-gate break; 27380Sstevel@tonic-gate } 27390Sstevel@tonic-gate attr++; 27400Sstevel@tonic-gate } 27410Sstevel@tonic-gate 27420Sstevel@tonic-gate #undef ALG_KEY_SIZES 27430Sstevel@tonic-gate #undef ALG_BLOCK_SIZES 27440Sstevel@tonic-gate 27450Sstevel@tonic-gate /* update the algorithm tables */ 27463448Sdh155122 spdsock_merge_algs(spds); 27470Sstevel@tonic-gate bail: 27480Sstevel@tonic-gate /* cleanup */ 27490Sstevel@tonic-gate ipsec_alg_free(alg); 27500Sstevel@tonic-gate for (alg_type = 0; alg_type < IPSEC_NALGTYPES; alg_type++) 27515240Snordmark for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) 27523448Sdh155122 if (spds->spds_algs[alg_type][algid] != NULL) 27535240Snordmark ipsec_alg_free(spds->spds_algs[alg_type][algid]); 27540Sstevel@tonic-gate } 27550Sstevel@tonic-gate 27560Sstevel@tonic-gate /* 27570Sstevel@tonic-gate * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue 27580Sstevel@tonic-gate * the request until IPsec loads. If IPsec is loaded, act on it 27590Sstevel@tonic-gate * immediately. 27600Sstevel@tonic-gate */ 27610Sstevel@tonic-gate 27620Sstevel@tonic-gate static void 27630Sstevel@tonic-gate spdsock_updatealg(queue_t *q, mblk_t *mp, spd_ext_t *extv[]) 27640Sstevel@tonic-gate { 27653448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 27663448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 27673448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 27683448Sdh155122 27693448Sdh155122 if (!ipsec_loaded(ipss)) { 27700Sstevel@tonic-gate /* 27710Sstevel@tonic-gate * IPsec is not loaded, save request and return nicely, 27720Sstevel@tonic-gate * the message will be processed once IPsec loads. 27730Sstevel@tonic-gate */ 27740Sstevel@tonic-gate mblk_t *new_mp; 27750Sstevel@tonic-gate 27760Sstevel@tonic-gate /* last update message wins */ 27770Sstevel@tonic-gate if ((new_mp = copymsg(mp)) == NULL) { 27780Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 27790Sstevel@tonic-gate return; 27800Sstevel@tonic-gate } 27813448Sdh155122 mutex_enter(&spds->spds_alg_lock); 27823448Sdh155122 bcopy(extv, spds->spds_extv_algs, 27830Sstevel@tonic-gate sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1)); 27843448Sdh155122 if (spds->spds_mp_algs != NULL) 27853448Sdh155122 freemsg(spds->spds_mp_algs); 27863448Sdh155122 spds->spds_mp_algs = mp; 27873448Sdh155122 spds->spds_algs_pending = B_TRUE; 27883448Sdh155122 mutex_exit(&spds->spds_alg_lock); 27898778SErik.Nordmark@Sun.COM if (audit_active) { 27908778SErik.Nordmark@Sun.COM cred_t *cr; 27918778SErik.Nordmark@Sun.COM pid_t cpid; 27928778SErik.Nordmark@Sun.COM 27938778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 27948778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_UPDATEALGS, cr, 27954307Spwernau spds->spds_netstack, NULL, B_TRUE, EAGAIN, 27968778SErik.Nordmark@Sun.COM cpid); 27978778SErik.Nordmark@Sun.COM } 27980Sstevel@tonic-gate spd_echo(q, new_mp); 27990Sstevel@tonic-gate } else { 28000Sstevel@tonic-gate /* 28010Sstevel@tonic-gate * IPsec is loaded, act on the message immediately. 28020Sstevel@tonic-gate */ 28030Sstevel@tonic-gate int diag; 28040Sstevel@tonic-gate 28053448Sdh155122 mutex_enter(&spds->spds_alg_lock); 28063448Sdh155122 spdsock_do_updatealg(extv, &diag, spds); 28073448Sdh155122 mutex_exit(&spds->spds_alg_lock); 28084307Spwernau if (diag == -1) { 28090Sstevel@tonic-gate spd_echo(q, mp); 28108778SErik.Nordmark@Sun.COM if (audit_active) { 28118778SErik.Nordmark@Sun.COM cred_t *cr; 28128778SErik.Nordmark@Sun.COM pid_t cpid; 28138778SErik.Nordmark@Sun.COM 28148778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 28158778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_UPDATEALGS, cr, 28168778SErik.Nordmark@Sun.COM spds->spds_netstack, NULL, B_TRUE, 0, 28178778SErik.Nordmark@Sun.COM cpid); 28188778SErik.Nordmark@Sun.COM } 28194307Spwernau } else { 28200Sstevel@tonic-gate spdsock_diag(q, mp, diag); 28218778SErik.Nordmark@Sun.COM if (audit_active) { 28228778SErik.Nordmark@Sun.COM cred_t *cr; 28238778SErik.Nordmark@Sun.COM pid_t cpid; 28248778SErik.Nordmark@Sun.COM 28258778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 28268778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_UPDATEALGS, cr, 28278778SErik.Nordmark@Sun.COM spds->spds_netstack, NULL, B_TRUE, diag, 28288778SErik.Nordmark@Sun.COM cpid); 28298778SErik.Nordmark@Sun.COM } 28304307Spwernau } 28310Sstevel@tonic-gate } 28320Sstevel@tonic-gate } 28330Sstevel@tonic-gate 28343055Sdanmcd /* 28353055Sdanmcd * Sort through the mess of polhead options to retrieve an appropriate one. 28363055Sdanmcd * Returns NULL if we send an spdsock error. Returns a valid pointer if we 28373055Sdanmcd * found a valid polhead. Returns ALL_ACTIVE_POLHEADS (aka. -1) or 28383055Sdanmcd * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to 28393055Sdanmcd * act on ALL policy heads. 28403055Sdanmcd */ 28413055Sdanmcd static ipsec_policy_head_t * 28423055Sdanmcd get_appropriate_polhead(queue_t *q, mblk_t *mp, spd_if_t *tunname, int spdid, 28433055Sdanmcd int msgtype, ipsec_tun_pol_t **itpp) 28443055Sdanmcd { 28453055Sdanmcd ipsec_tun_pol_t *itp; 28463055Sdanmcd ipsec_policy_head_t *iph; 28473055Sdanmcd int errno; 28483055Sdanmcd char *tname; 28493055Sdanmcd boolean_t active; 28503055Sdanmcd spdsock_t *ss = (spdsock_t *)q->q_ptr; 28516430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 28523055Sdanmcd uint64_t gen; /* Placeholder */ 285310616SSebastien.Roy@Sun.COM datalink_id_t linkid; 28543055Sdanmcd 28553055Sdanmcd active = (spdid == SPD_ACTIVE); 28563055Sdanmcd *itpp = NULL; 28573055Sdanmcd if (!active && spdid != SPD_STANDBY) { 28583055Sdanmcd spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_SPDID); 28593055Sdanmcd return (NULL); 28603055Sdanmcd } 28613055Sdanmcd 28623055Sdanmcd if (tunname != NULL) { 28633055Sdanmcd /* Acting on a tunnel's SPD. */ 28643055Sdanmcd tname = (char *)tunname->spd_if_name; 28653055Sdanmcd if (*tname == '\0') { 28663055Sdanmcd /* Handle all-polhead cases here. */ 28673055Sdanmcd if (msgtype != SPD_FLUSH && msgtype != SPD_DUMP) { 28683055Sdanmcd spdsock_diag(q, mp, 28693055Sdanmcd SPD_DIAGNOSTIC_NOT_GLOBAL_OP); 28703055Sdanmcd return (NULL); 28713055Sdanmcd } 28723055Sdanmcd return (active ? ALL_ACTIVE_POLHEADS : 28733055Sdanmcd ALL_INACTIVE_POLHEADS); 28743055Sdanmcd } 28753055Sdanmcd 28766430Sdanmcd itp = get_tunnel_policy(tname, ns); 28773055Sdanmcd if (itp == NULL) { 28783055Sdanmcd if (msgtype != SPD_ADDRULE) { 28793055Sdanmcd /* "Tunnel not found" */ 28803055Sdanmcd spdsock_error(q, mp, ENOENT, 0); 28813055Sdanmcd return (NULL); 28823055Sdanmcd } 28833055Sdanmcd 28843055Sdanmcd errno = 0; 28856430Sdanmcd itp = create_tunnel_policy(tname, &errno, &gen, ns); 28863055Sdanmcd if (itp == NULL) { 28873055Sdanmcd /* 28883055Sdanmcd * Something very bad happened, most likely 28893055Sdanmcd * ENOMEM. Return an indicator. 28903055Sdanmcd */ 28913055Sdanmcd spdsock_error(q, mp, errno, 0); 28923055Sdanmcd return (NULL); 28933055Sdanmcd } 28943055Sdanmcd } 28953055Sdanmcd /* 289610616SSebastien.Roy@Sun.COM * Troll the plumbed tunnels and see if we have a match. We 289710616SSebastien.Roy@Sun.COM * need to do this always in case we add policy AFTER plumbing 289810616SSebastien.Roy@Sun.COM * a tunnel. 28993055Sdanmcd */ 290010616SSebastien.Roy@Sun.COM if (dls_mgmt_get_linkid(tname, &linkid) == 0) 290110616SSebastien.Roy@Sun.COM iptun_set_policy(linkid, itp); 290210616SSebastien.Roy@Sun.COM 29033055Sdanmcd *itpp = itp; 29043055Sdanmcd /* For spdsock dump state, set the polhead's name. */ 29053055Sdanmcd if (msgtype == SPD_DUMP) { 29066430Sdanmcd ITP_REFHOLD(itp); 29076430Sdanmcd ss->spdsock_itp = itp; 29083055Sdanmcd ss->spdsock_dump_tunnel = itp->itp_flags & 29093055Sdanmcd (active ? ITPF_P_TUNNEL : ITPF_I_TUNNEL); 29103055Sdanmcd } 29113055Sdanmcd } else { 29123055Sdanmcd itp = NULL; 29133055Sdanmcd /* For spdsock dump state, indicate it's global policy. */ 29143055Sdanmcd if (msgtype == SPD_DUMP) 29156430Sdanmcd ss->spdsock_itp = NULL; 29163055Sdanmcd } 29173055Sdanmcd 29183055Sdanmcd if (active) 29193448Sdh155122 iph = (itp == NULL) ? ipsec_system_policy(ns) : itp->itp_policy; 29203055Sdanmcd else 29213448Sdh155122 iph = (itp == NULL) ? ipsec_inactive_policy(ns) : 29223055Sdanmcd itp->itp_inactive; 29233055Sdanmcd 29243055Sdanmcd ASSERT(iph != NULL); 29253055Sdanmcd if (itp != NULL) { 29263055Sdanmcd IPPH_REFHOLD(iph); 29273055Sdanmcd } 29283055Sdanmcd 29293055Sdanmcd return (iph); 29303055Sdanmcd } 29313055Sdanmcd 29320Sstevel@tonic-gate static void 29330Sstevel@tonic-gate spdsock_parse(queue_t *q, mblk_t *mp) 29340Sstevel@tonic-gate { 29350Sstevel@tonic-gate spd_msg_t *spmsg; 29360Sstevel@tonic-gate spd_ext_t *extv[SPD_EXT_MAX + 1]; 29370Sstevel@tonic-gate uint_t msgsize; 29380Sstevel@tonic-gate ipsec_policy_head_t *iph; 29393055Sdanmcd ipsec_tun_pol_t *itp; 29403055Sdanmcd spd_if_t *tunname; 29413448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 29423448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 29433448Sdh155122 netstack_t *ns = spds->spds_netstack; 29443448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 29450Sstevel@tonic-gate 29460Sstevel@tonic-gate /* Make sure nothing's below me. */ 29470Sstevel@tonic-gate ASSERT(WR(q)->q_next == NULL); 29480Sstevel@tonic-gate 29490Sstevel@tonic-gate spmsg = (spd_msg_t *)mp->b_rptr; 29500Sstevel@tonic-gate 29510Sstevel@tonic-gate msgsize = SPD_64TO8(spmsg->spd_msg_len); 29520Sstevel@tonic-gate 29530Sstevel@tonic-gate if (msgdsize(mp) != msgsize) { 29540Sstevel@tonic-gate /* 29550Sstevel@tonic-gate * Message len incorrect w.r.t. actual size. Send an error 29560Sstevel@tonic-gate * (EMSGSIZE). It may be necessary to massage things a 29570Sstevel@tonic-gate * bit. For example, if the spd_msg_type is hosed, 29580Sstevel@tonic-gate * I need to set it to SPD_RESERVED to get delivery to 29590Sstevel@tonic-gate * do the right thing. Then again, maybe just letting 29600Sstevel@tonic-gate * the error delivery do the right thing. 29610Sstevel@tonic-gate */ 29623448Sdh155122 ss2dbg(spds, 29633448Sdh155122 ("mblk (%lu) and base (%d) message sizes don't jibe.\n", 29640Sstevel@tonic-gate msgdsize(mp), msgsize)); 29650Sstevel@tonic-gate spdsock_error(q, mp, EMSGSIZE, SPD_DIAGNOSTIC_NONE); 29660Sstevel@tonic-gate return; 29670Sstevel@tonic-gate } 29680Sstevel@tonic-gate 29690Sstevel@tonic-gate if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) { 29700Sstevel@tonic-gate /* Get all message into one mblk. */ 29710Sstevel@tonic-gate if (pullupmsg(mp, -1) == 0) { 29720Sstevel@tonic-gate /* 29730Sstevel@tonic-gate * Something screwy happened. 29740Sstevel@tonic-gate */ 29753448Sdh155122 ss3dbg(spds, ("spdsock_parse: pullupmsg() failed.\n")); 29760Sstevel@tonic-gate return; 29770Sstevel@tonic-gate } else { 29780Sstevel@tonic-gate spmsg = (spd_msg_t *)mp->b_rptr; 29790Sstevel@tonic-gate } 29800Sstevel@tonic-gate } 29810Sstevel@tonic-gate 29820Sstevel@tonic-gate switch (spdsock_get_ext(extv, spmsg, msgsize)) { 29830Sstevel@tonic-gate case KGE_DUP: 29840Sstevel@tonic-gate /* Handle duplicate extension. */ 29853448Sdh155122 ss1dbg(spds, ("Got duplicate extension of type %d.\n", 29860Sstevel@tonic-gate extv[0]->spd_ext_type)); 29870Sstevel@tonic-gate spdsock_diag(q, mp, dup_ext_diag[extv[0]->spd_ext_type]); 29880Sstevel@tonic-gate return; 29890Sstevel@tonic-gate case KGE_UNK: 29900Sstevel@tonic-gate /* Handle unknown extension. */ 29913448Sdh155122 ss1dbg(spds, ("Got unknown extension of type %d.\n", 29920Sstevel@tonic-gate extv[0]->spd_ext_type)); 29930Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_UNKNOWN_EXT); 29940Sstevel@tonic-gate return; 29950Sstevel@tonic-gate case KGE_LEN: 29960Sstevel@tonic-gate /* Length error. */ 29973448Sdh155122 ss1dbg(spds, ("Length %d on extension type %d overrun or 0.\n", 29980Sstevel@tonic-gate extv[0]->spd_ext_len, extv[0]->spd_ext_type)); 29990Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_EXTLEN); 30000Sstevel@tonic-gate return; 30010Sstevel@tonic-gate case KGE_CHK: 30020Sstevel@tonic-gate /* Reality check failed. */ 30033448Sdh155122 ss1dbg(spds, ("Reality check failed on extension type %d.\n", 30040Sstevel@tonic-gate extv[0]->spd_ext_type)); 30050Sstevel@tonic-gate spdsock_diag(q, mp, bad_ext_diag[extv[0]->spd_ext_type]); 30060Sstevel@tonic-gate return; 30070Sstevel@tonic-gate default: 30080Sstevel@tonic-gate /* Default case is no errors. */ 30090Sstevel@tonic-gate break; 30100Sstevel@tonic-gate } 30110Sstevel@tonic-gate 30120Sstevel@tonic-gate /* 30130Sstevel@tonic-gate * Special-case SPD_UPDATEALGS so as not to load IPsec. 30140Sstevel@tonic-gate */ 30153448Sdh155122 if (!ipsec_loaded(ipss) && spmsg->spd_msg_type != SPD_UPDATEALGS) { 30160Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 30170Sstevel@tonic-gate 30180Sstevel@tonic-gate ASSERT(ss != NULL); 30193448Sdh155122 ipsec_loader_loadnow(ipss); 30200Sstevel@tonic-gate ss->spdsock_timeout_arg = mp; 30210Sstevel@tonic-gate ss->spdsock_timeout = qtimeout(q, spdsock_loadcheck, 30220Sstevel@tonic-gate q, LOADCHECK_INTERVAL); 30230Sstevel@tonic-gate return; 30240Sstevel@tonic-gate } 30250Sstevel@tonic-gate 30263055Sdanmcd /* First check for messages that need no polheads at all. */ 30270Sstevel@tonic-gate switch (spmsg->spd_msg_type) { 30280Sstevel@tonic-gate case SPD_UPDATEALGS: 30290Sstevel@tonic-gate spdsock_updatealg(q, mp, extv); 30300Sstevel@tonic-gate return; 30310Sstevel@tonic-gate case SPD_ALGLIST: 30320Sstevel@tonic-gate spdsock_alglist(q, mp); 30330Sstevel@tonic-gate return; 30340Sstevel@tonic-gate case SPD_DUMPALGS: 30350Sstevel@tonic-gate spdsock_dumpalgs(q, mp); 30360Sstevel@tonic-gate return; 30373055Sdanmcd } 30383055Sdanmcd 30393055Sdanmcd /* 30403055Sdanmcd * Then check for ones that need both primary/secondary polheads, 30413055Sdanmcd * finding the appropriate tunnel policy if need be. 30423055Sdanmcd */ 30433055Sdanmcd tunname = (spd_if_t *)extv[SPD_EXT_TUN_NAME]; 30443055Sdanmcd switch (spmsg->spd_msg_type) { 30453055Sdanmcd case SPD_FLIP: 30463055Sdanmcd spdsock_flip(q, mp, tunname); 30473055Sdanmcd return; 30483055Sdanmcd case SPD_CLONE: 30493055Sdanmcd spdsock_clone(q, mp, tunname); 30503055Sdanmcd return; 30513055Sdanmcd } 30523055Sdanmcd 30533055Sdanmcd /* 30543055Sdanmcd * Finally, find ones that operate on exactly one polhead, or 30553055Sdanmcd * "all polheads" of a given type (active/inactive). 30563055Sdanmcd */ 30573055Sdanmcd iph = get_appropriate_polhead(q, mp, tunname, spmsg->spd_msg_spdid, 30583055Sdanmcd spmsg->spd_msg_type, &itp); 30593055Sdanmcd if (iph == NULL) 30603055Sdanmcd return; 30613055Sdanmcd 30623055Sdanmcd /* All-polheads-ready operations. */ 30633055Sdanmcd switch (spmsg->spd_msg_type) { 30643055Sdanmcd case SPD_FLUSH: 30653055Sdanmcd if (itp != NULL) { 30663055Sdanmcd mutex_enter(&itp->itp_lock); 30673055Sdanmcd if (spmsg->spd_msg_spdid == SPD_ACTIVE) 30683055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 30693055Sdanmcd else 30703055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 30713055Sdanmcd mutex_exit(&itp->itp_lock); 30723448Sdh155122 ITP_REFRELE(itp, ns); 30733055Sdanmcd } 30744307Spwernau spdsock_flush(q, iph, itp, mp); 30753055Sdanmcd return; 30763055Sdanmcd case SPD_DUMP: 30773055Sdanmcd if (itp != NULL) 30783448Sdh155122 ITP_REFRELE(itp, ns); 30793055Sdanmcd spdsock_dump(q, iph, mp); 30803055Sdanmcd return; 30813055Sdanmcd } 30823055Sdanmcd 30833055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) { 30843055Sdanmcd spdsock_diag(q, mp, SPD_DIAGNOSTIC_NOT_GLOBAL_OP); 30853055Sdanmcd return; 30863055Sdanmcd } 30873055Sdanmcd 30883055Sdanmcd /* Single-polhead-only operations. */ 30893055Sdanmcd switch (spmsg->spd_msg_type) { 30903055Sdanmcd case SPD_ADDRULE: 30913055Sdanmcd spdsock_addrule(q, iph, mp, extv, itp); 30923055Sdanmcd break; 30933055Sdanmcd case SPD_DELETERULE: 30943055Sdanmcd spdsock_deleterule(q, iph, mp, extv, itp); 30953055Sdanmcd break; 30963055Sdanmcd case SPD_LOOKUP: 30973055Sdanmcd spdsock_lookup(q, iph, mp, extv, itp); 30983055Sdanmcd break; 30990Sstevel@tonic-gate default: 31000Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_MSG_TYPE); 31013055Sdanmcd break; 31020Sstevel@tonic-gate } 31033055Sdanmcd 31046430Sdanmcd IPPH_REFRELE(iph, ns); 31053055Sdanmcd if (itp != NULL) 31063448Sdh155122 ITP_REFRELE(itp, ns); 31070Sstevel@tonic-gate } 31080Sstevel@tonic-gate 31090Sstevel@tonic-gate /* 31100Sstevel@tonic-gate * If an algorithm mapping was received before IPsec was loaded, process it. 31110Sstevel@tonic-gate * Called from the IPsec loader. 31120Sstevel@tonic-gate */ 31130Sstevel@tonic-gate void 31143448Sdh155122 spdsock_update_pending_algs(netstack_t *ns) 31150Sstevel@tonic-gate { 31163448Sdh155122 spd_stack_t *spds = ns->netstack_spdsock; 31173448Sdh155122 31183448Sdh155122 mutex_enter(&spds->spds_alg_lock); 31193448Sdh155122 if (spds->spds_algs_pending) { 31200Sstevel@tonic-gate int diag; 31213448Sdh155122 31223448Sdh155122 spdsock_do_updatealg(spds->spds_extv_algs, &diag, 31233448Sdh155122 spds); 31243448Sdh155122 spds->spds_algs_pending = B_FALSE; 31250Sstevel@tonic-gate } 31263448Sdh155122 mutex_exit(&spds->spds_alg_lock); 31270Sstevel@tonic-gate } 31280Sstevel@tonic-gate 31290Sstevel@tonic-gate static void 31300Sstevel@tonic-gate spdsock_loadcheck(void *arg) 31310Sstevel@tonic-gate { 31320Sstevel@tonic-gate queue_t *q = (queue_t *)arg; 31330Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 31340Sstevel@tonic-gate mblk_t *mp; 31356430Sdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 31360Sstevel@tonic-gate 31370Sstevel@tonic-gate ASSERT(ss != NULL); 31380Sstevel@tonic-gate 31390Sstevel@tonic-gate ss->spdsock_timeout = 0; 31400Sstevel@tonic-gate mp = ss->spdsock_timeout_arg; 31410Sstevel@tonic-gate ASSERT(mp != NULL); 31420Sstevel@tonic-gate ss->spdsock_timeout_arg = NULL; 31433448Sdh155122 if (ipsec_failed(ipss)) 31440Sstevel@tonic-gate spdsock_error(q, mp, EPROTONOSUPPORT, 0); 31450Sstevel@tonic-gate else 31460Sstevel@tonic-gate spdsock_parse(q, mp); 31470Sstevel@tonic-gate } 31480Sstevel@tonic-gate 31490Sstevel@tonic-gate /* 31500Sstevel@tonic-gate * Copy relevant state bits. 31510Sstevel@tonic-gate */ 31520Sstevel@tonic-gate static void 31530Sstevel@tonic-gate spdsock_copy_info(struct T_info_ack *tap, spdsock_t *ss) 31540Sstevel@tonic-gate { 31550Sstevel@tonic-gate *tap = spdsock_g_t_info_ack; 31560Sstevel@tonic-gate tap->CURRENT_state = ss->spdsock_state; 31570Sstevel@tonic-gate tap->OPT_size = spdsock_max_optsize; 31580Sstevel@tonic-gate } 31590Sstevel@tonic-gate 31600Sstevel@tonic-gate /* 31610Sstevel@tonic-gate * This routine responds to T_CAPABILITY_REQ messages. It is called by 31620Sstevel@tonic-gate * spdsock_wput. Much of the T_CAPABILITY_ACK information is copied from 31630Sstevel@tonic-gate * spdsock_g_t_info_ack. The current state of the stream is copied from 31640Sstevel@tonic-gate * spdsock_state. 31650Sstevel@tonic-gate */ 31660Sstevel@tonic-gate static void 31670Sstevel@tonic-gate spdsock_capability_req(queue_t *q, mblk_t *mp) 31680Sstevel@tonic-gate { 31690Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 31700Sstevel@tonic-gate t_uscalar_t cap_bits1; 31710Sstevel@tonic-gate struct T_capability_ack *tcap; 31720Sstevel@tonic-gate 31730Sstevel@tonic-gate cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 31740Sstevel@tonic-gate 31750Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 31765240Snordmark mp->b_datap->db_type, T_CAPABILITY_ACK); 31770Sstevel@tonic-gate if (mp == NULL) 31780Sstevel@tonic-gate return; 31790Sstevel@tonic-gate 31800Sstevel@tonic-gate tcap = (struct T_capability_ack *)mp->b_rptr; 31810Sstevel@tonic-gate tcap->CAP_bits1 = 0; 31820Sstevel@tonic-gate 31830Sstevel@tonic-gate if (cap_bits1 & TC1_INFO) { 31840Sstevel@tonic-gate spdsock_copy_info(&tcap->INFO_ack, ss); 31850Sstevel@tonic-gate tcap->CAP_bits1 |= TC1_INFO; 31860Sstevel@tonic-gate } 31870Sstevel@tonic-gate 31880Sstevel@tonic-gate qreply(q, mp); 31890Sstevel@tonic-gate } 31900Sstevel@tonic-gate 31910Sstevel@tonic-gate /* 31920Sstevel@tonic-gate * This routine responds to T_INFO_REQ messages. It is called by 31930Sstevel@tonic-gate * spdsock_wput_other. 31940Sstevel@tonic-gate * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack. 31950Sstevel@tonic-gate * The current state of the stream is copied from spdsock_state. 31960Sstevel@tonic-gate */ 31970Sstevel@tonic-gate static void 31980Sstevel@tonic-gate spdsock_info_req(q, mp) 31990Sstevel@tonic-gate queue_t *q; 32000Sstevel@tonic-gate mblk_t *mp; 32010Sstevel@tonic-gate { 32020Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 32030Sstevel@tonic-gate T_INFO_ACK); 32040Sstevel@tonic-gate if (mp == NULL) 32050Sstevel@tonic-gate return; 32060Sstevel@tonic-gate spdsock_copy_info((struct T_info_ack *)mp->b_rptr, 32070Sstevel@tonic-gate (spdsock_t *)q->q_ptr); 32080Sstevel@tonic-gate qreply(q, mp); 32090Sstevel@tonic-gate } 32100Sstevel@tonic-gate 32110Sstevel@tonic-gate /* 32120Sstevel@tonic-gate * spdsock_err_ack. This routine creates a 32130Sstevel@tonic-gate * T_ERROR_ACK message and passes it 32140Sstevel@tonic-gate * upstream. 32150Sstevel@tonic-gate */ 32160Sstevel@tonic-gate static void 32170Sstevel@tonic-gate spdsock_err_ack(q, mp, t_error, sys_error) 32180Sstevel@tonic-gate queue_t *q; 32190Sstevel@tonic-gate mblk_t *mp; 32200Sstevel@tonic-gate int t_error; 32210Sstevel@tonic-gate int sys_error; 32220Sstevel@tonic-gate { 32230Sstevel@tonic-gate if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 32240Sstevel@tonic-gate qreply(q, mp); 32250Sstevel@tonic-gate } 32260Sstevel@tonic-gate 32270Sstevel@tonic-gate /* 32280Sstevel@tonic-gate * This routine retrieves the current status of socket options. 32290Sstevel@tonic-gate * It returns the size of the option retrieved. 32300Sstevel@tonic-gate */ 32310Sstevel@tonic-gate /* ARGSUSED */ 32320Sstevel@tonic-gate int 32330Sstevel@tonic-gate spdsock_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 32340Sstevel@tonic-gate { 32350Sstevel@tonic-gate int *i1 = (int *)ptr; 32360Sstevel@tonic-gate 32370Sstevel@tonic-gate switch (level) { 32380Sstevel@tonic-gate case SOL_SOCKET: 32390Sstevel@tonic-gate switch (name) { 32400Sstevel@tonic-gate case SO_TYPE: 32410Sstevel@tonic-gate *i1 = SOCK_RAW; 32420Sstevel@tonic-gate break; 32430Sstevel@tonic-gate /* 32440Sstevel@tonic-gate * The following two items can be manipulated, 32450Sstevel@tonic-gate * but changing them should do nothing. 32460Sstevel@tonic-gate */ 32470Sstevel@tonic-gate case SO_SNDBUF: 32480Sstevel@tonic-gate *i1 = (int)q->q_hiwat; 32490Sstevel@tonic-gate break; 32500Sstevel@tonic-gate case SO_RCVBUF: 32510Sstevel@tonic-gate *i1 = (int)(RD(q)->q_hiwat); 32520Sstevel@tonic-gate break; 32530Sstevel@tonic-gate } 32540Sstevel@tonic-gate break; 32550Sstevel@tonic-gate default: 32560Sstevel@tonic-gate return (0); 32570Sstevel@tonic-gate } 32580Sstevel@tonic-gate return (sizeof (int)); 32590Sstevel@tonic-gate } 32600Sstevel@tonic-gate 32610Sstevel@tonic-gate /* 32620Sstevel@tonic-gate * This routine sets socket options. 32630Sstevel@tonic-gate */ 32640Sstevel@tonic-gate /* ARGSUSED */ 32650Sstevel@tonic-gate int 32660Sstevel@tonic-gate spdsock_opt_set(queue_t *q, uint_t mgmt_flags, int level, int name, 32670Sstevel@tonic-gate uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 32680Sstevel@tonic-gate void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 32690Sstevel@tonic-gate { 32700Sstevel@tonic-gate int *i1 = (int *)invalp; 32713448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 32723448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 32730Sstevel@tonic-gate 32740Sstevel@tonic-gate switch (level) { 32750Sstevel@tonic-gate case SOL_SOCKET: 32760Sstevel@tonic-gate switch (name) { 32770Sstevel@tonic-gate case SO_SNDBUF: 32783448Sdh155122 if (*i1 > spds->spds_max_buf) 32790Sstevel@tonic-gate return (ENOBUFS); 32800Sstevel@tonic-gate q->q_hiwat = *i1; 32810Sstevel@tonic-gate break; 32820Sstevel@tonic-gate case SO_RCVBUF: 32833448Sdh155122 if (*i1 > spds->spds_max_buf) 32840Sstevel@tonic-gate return (ENOBUFS); 32850Sstevel@tonic-gate RD(q)->q_hiwat = *i1; 32868348SEric.Yu@Sun.COM (void) proto_set_rx_hiwat(RD(q), NULL, *i1); 32870Sstevel@tonic-gate break; 32880Sstevel@tonic-gate } 32890Sstevel@tonic-gate break; 32900Sstevel@tonic-gate } 32910Sstevel@tonic-gate return (0); 32920Sstevel@tonic-gate } 32930Sstevel@tonic-gate 32940Sstevel@tonic-gate 32950Sstevel@tonic-gate /* 32960Sstevel@tonic-gate * Handle STREAMS messages. 32970Sstevel@tonic-gate */ 32980Sstevel@tonic-gate static void 32990Sstevel@tonic-gate spdsock_wput_other(queue_t *q, mblk_t *mp) 33000Sstevel@tonic-gate { 33010Sstevel@tonic-gate struct iocblk *iocp; 33020Sstevel@tonic-gate int error; 33033448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 33043448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 33053448Sdh155122 cred_t *cr; 33060Sstevel@tonic-gate 33070Sstevel@tonic-gate switch (mp->b_datap->db_type) { 33080Sstevel@tonic-gate case M_PROTO: 33090Sstevel@tonic-gate case M_PCPROTO: 33100Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) { 33113448Sdh155122 ss3dbg(spds, ( 33120Sstevel@tonic-gate "spdsock_wput_other: Not big enough M_PROTO\n")); 33130Sstevel@tonic-gate freemsg(mp); 33140Sstevel@tonic-gate return; 33150Sstevel@tonic-gate } 33160Sstevel@tonic-gate switch (((union T_primitives *)mp->b_rptr)->type) { 33170Sstevel@tonic-gate case T_CAPABILITY_REQ: 33180Sstevel@tonic-gate spdsock_capability_req(q, mp); 33193448Sdh155122 break; 33200Sstevel@tonic-gate case T_INFO_REQ: 33210Sstevel@tonic-gate spdsock_info_req(q, mp); 33223448Sdh155122 break; 33230Sstevel@tonic-gate case T_SVR4_OPTMGMT_REQ: 33240Sstevel@tonic-gate case T_OPTMGMT_REQ: 33258778SErik.Nordmark@Sun.COM /* 33268778SErik.Nordmark@Sun.COM * All Solaris components should pass a db_credp 33278778SErik.Nordmark@Sun.COM * for this TPI message, hence we ASSERT. 33288778SErik.Nordmark@Sun.COM * But in case there is some other M_PROTO that looks 33298778SErik.Nordmark@Sun.COM * like a TPI message sent by some other kernel 33308778SErik.Nordmark@Sun.COM * component, we check and return an error. 33318778SErik.Nordmark@Sun.COM */ 33328778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, NULL); 33338778SErik.Nordmark@Sun.COM ASSERT(cr != NULL); 33348778SErik.Nordmark@Sun.COM if (cr == NULL) { 33358778SErik.Nordmark@Sun.COM spdsock_err_ack(q, mp, TSYSERR, EINVAL); 33368778SErik.Nordmark@Sun.COM return; 33378778SErik.Nordmark@Sun.COM } 33388778SErik.Nordmark@Sun.COM if (((union T_primitives *)mp->b_rptr)->type == 33398778SErik.Nordmark@Sun.COM T_SVR4_OPTMGMT_REQ) { 33408778SErik.Nordmark@Sun.COM (void) svr4_optcom_req(q, mp, cr, 33418778SErik.Nordmark@Sun.COM &spdsock_opt_obj, B_FALSE); 33428778SErik.Nordmark@Sun.COM } else { 33438778SErik.Nordmark@Sun.COM (void) tpi_optcom_req(q, mp, cr, 33448778SErik.Nordmark@Sun.COM &spdsock_opt_obj, B_FALSE); 33458778SErik.Nordmark@Sun.COM } 33463448Sdh155122 break; 33470Sstevel@tonic-gate case T_DATA_REQ: 33480Sstevel@tonic-gate case T_EXDATA_REQ: 33490Sstevel@tonic-gate case T_ORDREL_REQ: 33500Sstevel@tonic-gate /* Illegal for spdsock. */ 33510Sstevel@tonic-gate freemsg(mp); 33520Sstevel@tonic-gate (void) putnextctl1(RD(q), M_ERROR, EPROTO); 33533448Sdh155122 break; 33540Sstevel@tonic-gate default: 33550Sstevel@tonic-gate /* Not supported by spdsock. */ 33560Sstevel@tonic-gate spdsock_err_ack(q, mp, TNOTSUPPORT, 0); 33573448Sdh155122 break; 33580Sstevel@tonic-gate } 33593448Sdh155122 return; 33600Sstevel@tonic-gate case M_IOCTL: 33610Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 33620Sstevel@tonic-gate error = EINVAL; 33630Sstevel@tonic-gate 33640Sstevel@tonic-gate switch (iocp->ioc_cmd) { 33650Sstevel@tonic-gate case ND_SET: 33660Sstevel@tonic-gate case ND_GET: 33673448Sdh155122 if (nd_getset(q, spds->spds_g_nd, mp)) { 33680Sstevel@tonic-gate qreply(q, mp); 33690Sstevel@tonic-gate return; 33700Sstevel@tonic-gate } else 33710Sstevel@tonic-gate error = ENOENT; 33720Sstevel@tonic-gate /* FALLTHRU */ 33730Sstevel@tonic-gate default: 33740Sstevel@tonic-gate miocnak(q, mp, 0, error); 33750Sstevel@tonic-gate return; 33760Sstevel@tonic-gate } 33770Sstevel@tonic-gate case M_FLUSH: 33780Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) { 33790Sstevel@tonic-gate flushq(q, FLUSHALL); 33800Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW; 33810Sstevel@tonic-gate } 33820Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) { 33830Sstevel@tonic-gate qreply(q, mp); 33840Sstevel@tonic-gate return; 33850Sstevel@tonic-gate } 33860Sstevel@tonic-gate /* Else FALLTHRU */ 33870Sstevel@tonic-gate } 33880Sstevel@tonic-gate 33890Sstevel@tonic-gate /* If fell through, just black-hole the message. */ 33900Sstevel@tonic-gate freemsg(mp); 33910Sstevel@tonic-gate } 33920Sstevel@tonic-gate 33930Sstevel@tonic-gate static void 33940Sstevel@tonic-gate spdsock_wput(queue_t *q, mblk_t *mp) 33950Sstevel@tonic-gate { 33960Sstevel@tonic-gate uint8_t *rptr = mp->b_rptr; 33970Sstevel@tonic-gate mblk_t *mp1; 33980Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 33993448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 34000Sstevel@tonic-gate 34010Sstevel@tonic-gate /* 34020Sstevel@tonic-gate * If we're dumping, defer processing other messages until the 34030Sstevel@tonic-gate * dump completes. 34040Sstevel@tonic-gate */ 34050Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) { 34060Sstevel@tonic-gate if (!putq(q, mp)) 34070Sstevel@tonic-gate freemsg(mp); 34080Sstevel@tonic-gate return; 34090Sstevel@tonic-gate } 34100Sstevel@tonic-gate 34110Sstevel@tonic-gate switch (mp->b_datap->db_type) { 34120Sstevel@tonic-gate case M_DATA: 34130Sstevel@tonic-gate /* 34140Sstevel@tonic-gate * Silently discard. 34150Sstevel@tonic-gate */ 34163448Sdh155122 ss2dbg(spds, ("raw M_DATA in spdsock.\n")); 34170Sstevel@tonic-gate freemsg(mp); 34180Sstevel@tonic-gate return; 34190Sstevel@tonic-gate case M_PROTO: 34200Sstevel@tonic-gate case M_PCPROTO: 34210Sstevel@tonic-gate if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) { 34220Sstevel@tonic-gate if (((union T_primitives *)rptr)->type == T_DATA_REQ) { 34230Sstevel@tonic-gate if ((mp1 = mp->b_cont) == NULL) { 34240Sstevel@tonic-gate /* No data after T_DATA_REQ. */ 34253448Sdh155122 ss2dbg(spds, 34263448Sdh155122 ("No data after DATA_REQ.\n")); 34270Sstevel@tonic-gate freemsg(mp); 34280Sstevel@tonic-gate return; 34290Sstevel@tonic-gate } 34300Sstevel@tonic-gate freeb(mp); 34310Sstevel@tonic-gate mp = mp1; 34323448Sdh155122 ss2dbg(spds, ("T_DATA_REQ\n")); 34330Sstevel@tonic-gate break; /* Out of switch. */ 34340Sstevel@tonic-gate } 34350Sstevel@tonic-gate } 34360Sstevel@tonic-gate /* FALLTHRU */ 34370Sstevel@tonic-gate default: 34383448Sdh155122 ss3dbg(spds, ("In default wput case (%d %d).\n", 34390Sstevel@tonic-gate mp->b_datap->db_type, ((union T_primitives *)rptr)->type)); 34400Sstevel@tonic-gate spdsock_wput_other(q, mp); 34410Sstevel@tonic-gate return; 34420Sstevel@tonic-gate } 34430Sstevel@tonic-gate 34440Sstevel@tonic-gate /* I now have a PF_POLICY message in an M_DATA block. */ 34450Sstevel@tonic-gate spdsock_parse(q, mp); 34460Sstevel@tonic-gate } 34470Sstevel@tonic-gate 34480Sstevel@tonic-gate /* 34490Sstevel@tonic-gate * Device open procedure, called when new queue pair created. 34500Sstevel@tonic-gate * We are passed the read-side queue. 34510Sstevel@tonic-gate */ 34520Sstevel@tonic-gate /* ARGSUSED */ 34530Sstevel@tonic-gate static int 34540Sstevel@tonic-gate spdsock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 34550Sstevel@tonic-gate { 34560Sstevel@tonic-gate spdsock_t *ss; 34570Sstevel@tonic-gate queue_t *oq = OTHERQ(q); 34580Sstevel@tonic-gate minor_t ssminor; 34593448Sdh155122 netstack_t *ns; 34603448Sdh155122 spd_stack_t *spds; 34613448Sdh155122 34623448Sdh155122 if (secpolicy_ip_config(credp, B_FALSE) != 0) 34630Sstevel@tonic-gate return (EPERM); 34640Sstevel@tonic-gate 34650Sstevel@tonic-gate if (q->q_ptr != NULL) 34660Sstevel@tonic-gate return (0); /* Re-open of an already open instance. */ 34670Sstevel@tonic-gate 34680Sstevel@tonic-gate if (sflag & MODOPEN) 34690Sstevel@tonic-gate return (EINVAL); 34700Sstevel@tonic-gate 34713448Sdh155122 ns = netstack_find_by_cred(credp); 34723448Sdh155122 ASSERT(ns != NULL); 34733448Sdh155122 spds = ns->netstack_spdsock; 34743448Sdh155122 ASSERT(spds != NULL); 34753448Sdh155122 34763448Sdh155122 ss2dbg(spds, ("Made it into PF_POLICY socket open.\n")); 34770Sstevel@tonic-gate 34780Sstevel@tonic-gate ssminor = (minor_t)(uintptr_t)vmem_alloc(spdsock_vmem, 1, VM_NOSLEEP); 34793448Sdh155122 if (ssminor == 0) { 34803448Sdh155122 netstack_rele(spds->spds_netstack); 34810Sstevel@tonic-gate return (ENOMEM); 34823448Sdh155122 } 34830Sstevel@tonic-gate ss = kmem_zalloc(sizeof (spdsock_t), KM_NOSLEEP); 34840Sstevel@tonic-gate if (ss == NULL) { 34850Sstevel@tonic-gate vmem_free(spdsock_vmem, (void *)(uintptr_t)ssminor, 1); 34863448Sdh155122 netstack_rele(spds->spds_netstack); 34870Sstevel@tonic-gate return (ENOMEM); 34880Sstevel@tonic-gate } 34890Sstevel@tonic-gate 34900Sstevel@tonic-gate ss->spdsock_minor = ssminor; 34910Sstevel@tonic-gate ss->spdsock_state = TS_UNBND; 34920Sstevel@tonic-gate ss->spdsock_dump_req = NULL; 34930Sstevel@tonic-gate 34943448Sdh155122 ss->spdsock_spds = spds; 34953448Sdh155122 34960Sstevel@tonic-gate q->q_ptr = ss; 34970Sstevel@tonic-gate oq->q_ptr = ss; 34980Sstevel@tonic-gate 34993448Sdh155122 q->q_hiwat = spds->spds_recv_hiwat; 35003448Sdh155122 35013448Sdh155122 oq->q_hiwat = spds->spds_xmit_hiwat; 35023448Sdh155122 oq->q_lowat = spds->spds_xmit_lowat; 35030Sstevel@tonic-gate 35040Sstevel@tonic-gate qprocson(q); 35058348SEric.Yu@Sun.COM (void) proto_set_rx_hiwat(q, NULL, spds->spds_recv_hiwat); 35060Sstevel@tonic-gate 35070Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), ss->spdsock_minor); 35080Sstevel@tonic-gate return (0); 35090Sstevel@tonic-gate } 35100Sstevel@tonic-gate 35110Sstevel@tonic-gate /* 35120Sstevel@tonic-gate * Read-side service procedure, invoked when we get back-enabled 35130Sstevel@tonic-gate * when buffer space becomes available. 35140Sstevel@tonic-gate * 35150Sstevel@tonic-gate * Dump another chunk if we were dumping before; when we finish, kick 35160Sstevel@tonic-gate * the write-side queue in case it's waiting for read queue space. 35170Sstevel@tonic-gate */ 35180Sstevel@tonic-gate void 35190Sstevel@tonic-gate spdsock_rsrv(queue_t *q) 35200Sstevel@tonic-gate { 35210Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 35220Sstevel@tonic-gate 35230Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) 35240Sstevel@tonic-gate spdsock_dump_some(q, ss); 35250Sstevel@tonic-gate 35260Sstevel@tonic-gate if (ss->spdsock_dump_req == NULL) 35270Sstevel@tonic-gate qenable(OTHERQ(q)); 35280Sstevel@tonic-gate } 35290Sstevel@tonic-gate 35300Sstevel@tonic-gate /* 35310Sstevel@tonic-gate * Write-side service procedure, invoked when we defer processing 35320Sstevel@tonic-gate * if another message is received while a dump is in progress. 35330Sstevel@tonic-gate */ 35340Sstevel@tonic-gate void 35350Sstevel@tonic-gate spdsock_wsrv(queue_t *q) 35360Sstevel@tonic-gate { 35370Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 35380Sstevel@tonic-gate mblk_t *mp; 35396430Sdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 35400Sstevel@tonic-gate 35410Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) { 35420Sstevel@tonic-gate qenable(OTHERQ(q)); 35430Sstevel@tonic-gate return; 35440Sstevel@tonic-gate } 35450Sstevel@tonic-gate 35460Sstevel@tonic-gate while ((mp = getq(q)) != NULL) { 35473448Sdh155122 if (ipsec_loaded(ipss)) { 35480Sstevel@tonic-gate spdsock_wput(q, mp); 35490Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) 35500Sstevel@tonic-gate return; 35513448Sdh155122 } else if (!ipsec_failed(ipss)) { 35520Sstevel@tonic-gate (void) putq(q, mp); 35530Sstevel@tonic-gate } else { 35540Sstevel@tonic-gate spdsock_error(q, mp, EPFNOSUPPORT, 0); 35550Sstevel@tonic-gate } 35560Sstevel@tonic-gate } 35570Sstevel@tonic-gate } 35580Sstevel@tonic-gate 35590Sstevel@tonic-gate static int 35600Sstevel@tonic-gate spdsock_close(queue_t *q) 35610Sstevel@tonic-gate { 35620Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 35633448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 35640Sstevel@tonic-gate 35650Sstevel@tonic-gate qprocsoff(q); 35660Sstevel@tonic-gate 35670Sstevel@tonic-gate /* Safe assumption. */ 35680Sstevel@tonic-gate ASSERT(ss != NULL); 35690Sstevel@tonic-gate 35700Sstevel@tonic-gate if (ss->spdsock_timeout != 0) 35710Sstevel@tonic-gate (void) quntimeout(q, ss->spdsock_timeout); 35720Sstevel@tonic-gate 35733448Sdh155122 ss3dbg(spds, ("Driver close, PF_POLICY socket is going away.\n")); 35740Sstevel@tonic-gate 35750Sstevel@tonic-gate vmem_free(spdsock_vmem, (void *)(uintptr_t)ss->spdsock_minor, 1); 35763448Sdh155122 netstack_rele(ss->spdsock_spds->spds_netstack); 35770Sstevel@tonic-gate 35780Sstevel@tonic-gate kmem_free(ss, sizeof (spdsock_t)); 35790Sstevel@tonic-gate return (0); 35800Sstevel@tonic-gate } 35810Sstevel@tonic-gate 35820Sstevel@tonic-gate /* 35830Sstevel@tonic-gate * Merge the IPsec algorithms tables with the received algorithm information. 35840Sstevel@tonic-gate */ 35850Sstevel@tonic-gate void 35863448Sdh155122 spdsock_merge_algs(spd_stack_t *spds) 35870Sstevel@tonic-gate { 35880Sstevel@tonic-gate ipsec_alginfo_t *alg, *oalg; 35890Sstevel@tonic-gate ipsec_algtype_t algtype; 35900Sstevel@tonic-gate uint_t algidx, algid, nalgs; 35910Sstevel@tonic-gate crypto_mech_name_t *mechs; 35920Sstevel@tonic-gate uint_t mech_count, mech_idx; 35933448Sdh155122 netstack_t *ns = spds->spds_netstack; 35943448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 35953448Sdh155122 35963448Sdh155122 ASSERT(MUTEX_HELD(&spds->spds_alg_lock)); 35970Sstevel@tonic-gate 35980Sstevel@tonic-gate /* 35990Sstevel@tonic-gate * Get the list of supported mechanisms from the crypto framework. 36000Sstevel@tonic-gate * If a mechanism is supported by KCF, resolve its mechanism 36010Sstevel@tonic-gate * id and mark it as being valid. This operation must be done 36020Sstevel@tonic-gate * without holding alg_lock, since it can cause a provider 36030Sstevel@tonic-gate * module to be loaded and the provider notification callback to 36040Sstevel@tonic-gate * be invoked. 36050Sstevel@tonic-gate */ 36060Sstevel@tonic-gate mechs = crypto_get_mech_list(&mech_count, KM_SLEEP); 36070Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 36080Sstevel@tonic-gate for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) { 36090Sstevel@tonic-gate int algflags = 0; 36100Sstevel@tonic-gate crypto_mech_type_t mt = CRYPTO_MECHANISM_INVALID; 36110Sstevel@tonic-gate 36123448Sdh155122 alg = spds->spds_algs[algtype][algid]; 36133448Sdh155122 if (alg == NULL) 36140Sstevel@tonic-gate continue; 36150Sstevel@tonic-gate 36160Sstevel@tonic-gate /* 36170Sstevel@tonic-gate * The NULL encryption algorithm is a special 36180Sstevel@tonic-gate * case because there are no mechanisms, yet 36190Sstevel@tonic-gate * the algorithm is still valid. 36200Sstevel@tonic-gate */ 36210Sstevel@tonic-gate if (alg->alg_id == SADB_EALG_NULL) { 36220Sstevel@tonic-gate alg->alg_mech_type = CRYPTO_MECHANISM_INVALID; 3623*10824SMark.Fenwick@Sun.COM alg->alg_flags |= ALG_FLAG_VALID; 36240Sstevel@tonic-gate continue; 36250Sstevel@tonic-gate } 36260Sstevel@tonic-gate 36270Sstevel@tonic-gate for (mech_idx = 0; mech_idx < mech_count; mech_idx++) { 36280Sstevel@tonic-gate if (strncmp(alg->alg_mech_name, mechs[mech_idx], 36290Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME) == 0) { 36300Sstevel@tonic-gate mt = crypto_mech2id(alg->alg_mech_name); 36310Sstevel@tonic-gate ASSERT(mt != CRYPTO_MECHANISM_INVALID); 36320Sstevel@tonic-gate algflags = ALG_FLAG_VALID; 36330Sstevel@tonic-gate break; 36340Sstevel@tonic-gate } 36350Sstevel@tonic-gate } 36360Sstevel@tonic-gate alg->alg_mech_type = mt; 3637*10824SMark.Fenwick@Sun.COM alg->alg_flags |= algflags; 36380Sstevel@tonic-gate } 36390Sstevel@tonic-gate } 36400Sstevel@tonic-gate 36413448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 36420Sstevel@tonic-gate 36430Sstevel@tonic-gate /* 36440Sstevel@tonic-gate * For each algorithm currently defined, check if it is 36450Sstevel@tonic-gate * present in the new tables created from the SPD_UPDATEALGS 36460Sstevel@tonic-gate * message received from user-space. 36470Sstevel@tonic-gate * Delete the algorithm entries that are currently defined 36480Sstevel@tonic-gate * but not part of the new tables. 36490Sstevel@tonic-gate */ 36500Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 36513448Sdh155122 nalgs = ipss->ipsec_nalgs[algtype]; 36520Sstevel@tonic-gate for (algidx = 0; algidx < nalgs; algidx++) { 36533448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 36543448Sdh155122 if (spds->spds_algs[algtype][algid] == NULL) 36553448Sdh155122 ipsec_alg_unreg(algtype, algid, ns); 36560Sstevel@tonic-gate } 36570Sstevel@tonic-gate } 36580Sstevel@tonic-gate 36590Sstevel@tonic-gate /* 36600Sstevel@tonic-gate * For each algorithm we just received, check if it is 36610Sstevel@tonic-gate * present in the currently defined tables. If it is, swap 36620Sstevel@tonic-gate * the entry with the one we just allocated. 36630Sstevel@tonic-gate * If the new algorithm is not in the current tables, 36640Sstevel@tonic-gate * add it. 36650Sstevel@tonic-gate */ 36660Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 36670Sstevel@tonic-gate for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) { 36683448Sdh155122 alg = spds->spds_algs[algtype][algid]; 36693448Sdh155122 if (alg == NULL) 36700Sstevel@tonic-gate continue; 36710Sstevel@tonic-gate 36723448Sdh155122 if ((oalg = ipss->ipsec_alglists[algtype][algid]) == 36733448Sdh155122 NULL) { 36740Sstevel@tonic-gate /* 36750Sstevel@tonic-gate * New algorithm, add it to the algorithm 36760Sstevel@tonic-gate * table. 36770Sstevel@tonic-gate */ 36783448Sdh155122 ipsec_alg_reg(algtype, alg, ns); 36790Sstevel@tonic-gate } else { 36800Sstevel@tonic-gate /* 36810Sstevel@tonic-gate * Algorithm is already in the table. Swap 36820Sstevel@tonic-gate * the existing entry with the new one. 36830Sstevel@tonic-gate */ 36843448Sdh155122 ipsec_alg_fix_min_max(alg, algtype, ns); 36853448Sdh155122 ipss->ipsec_alglists[algtype][algid] = alg; 36860Sstevel@tonic-gate ipsec_alg_free(oalg); 36870Sstevel@tonic-gate } 36883448Sdh155122 spds->spds_algs[algtype][algid] = NULL; 36890Sstevel@tonic-gate } 36900Sstevel@tonic-gate } 36910Sstevel@tonic-gate 36923448Sdh155122 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 36933448Sdh155122 ipss->ipsec_algs_exec_mode[algtype] = 36943448Sdh155122 spds->spds_algs_exec_mode[algtype]; 36953448Sdh155122 } 36963448Sdh155122 36973448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 36980Sstevel@tonic-gate 36990Sstevel@tonic-gate crypto_free_mech_list(mechs, mech_count); 37000Sstevel@tonic-gate 37013448Sdh155122 ipsecah_algs_changed(ns); 37023448Sdh155122 ipsecesp_algs_changed(ns); 37030Sstevel@tonic-gate } 3704