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_impl.h> 620Sstevel@tonic-gate #include <inet/spdsock.h> 630Sstevel@tonic-gate #include <inet/sadb.h> 6410616SSebastien.Roy@Sun.COM #include <inet/iptun.h> 6510616SSebastien.Roy@Sun.COM #include <inet/iptun/iptun_impl.h> 660Sstevel@tonic-gate 670Sstevel@tonic-gate #include <sys/isa_defs.h> 680Sstevel@tonic-gate 694307Spwernau #include <c2/audit.h> 704307Spwernau 710Sstevel@tonic-gate /* 720Sstevel@tonic-gate * This is a transport provider for the PF_POLICY IPsec policy 730Sstevel@tonic-gate * management socket, which provides a management interface into the 740Sstevel@tonic-gate * SPD, allowing policy rules to be added, deleted, and queried. 750Sstevel@tonic-gate * 760Sstevel@tonic-gate * This effectively replaces the old private SIOC*IPSECONFIG ioctls 770Sstevel@tonic-gate * with an extensible interface which will hopefully be public some 780Sstevel@tonic-gate * day. 790Sstevel@tonic-gate * 800Sstevel@tonic-gate * See <net/pfpolicy.h> for more details on the protocol. 810Sstevel@tonic-gate * 820Sstevel@tonic-gate * We link against drv/ip and call directly into it to manipulate the 830Sstevel@tonic-gate * SPD; see ipsec_impl.h for the policy data structures and spd.c for 840Sstevel@tonic-gate * the code which maintains them. 850Sstevel@tonic-gate * 860Sstevel@tonic-gate * The MT model of this is QPAIR with the addition of some explicit 870Sstevel@tonic-gate * locking to protect system-wide policy data structures. 880Sstevel@tonic-gate */ 890Sstevel@tonic-gate 900Sstevel@tonic-gate static vmem_t *spdsock_vmem; /* for minor numbers. */ 910Sstevel@tonic-gate 920Sstevel@tonic-gate #define ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t)) 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages (from rts.c...) */ 950Sstevel@tonic-gate static struct T_info_ack spdsock_g_t_info_ack = { 960Sstevel@tonic-gate T_INFO_ACK, 970Sstevel@tonic-gate T_INFINITE, /* TSDU_size. Maximum size messages. */ 980Sstevel@tonic-gate T_INVALID, /* ETSDU_size. No expedited data. */ 990Sstevel@tonic-gate T_INVALID, /* CDATA_size. No connect data. */ 1000Sstevel@tonic-gate T_INVALID, /* DDATA_size. No disconnect data. */ 1010Sstevel@tonic-gate 0, /* ADDR_size. */ 1020Sstevel@tonic-gate 0, /* OPT_size. No user-settable options */ 1030Sstevel@tonic-gate 64 * 1024, /* TIDU_size. spdsock allows maximum size messages. */ 1040Sstevel@tonic-gate T_COTS, /* SERV_type. spdsock supports connection oriented. */ 1050Sstevel@tonic-gate TS_UNBND, /* CURRENT_state. This is set from spdsock_state. */ 1060Sstevel@tonic-gate (XPG4_1) /* Provider flags */ 1070Sstevel@tonic-gate }; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */ 1103448Sdh155122 typedef struct spdsockparam_s { 1110Sstevel@tonic-gate uint_t spdsock_param_min; 1120Sstevel@tonic-gate uint_t spdsock_param_max; 1130Sstevel@tonic-gate uint_t spdsock_param_value; 1140Sstevel@tonic-gate char *spdsock_param_name; 1150Sstevel@tonic-gate } spdsockparam_t; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * Table of NDD variables supported by spdsock. These are loaded into 1190Sstevel@tonic-gate * spdsock_g_nd in spdsock_init_nd. 1200Sstevel@tonic-gate * All of these are alterable, within the min/max values given, at run time. 1210Sstevel@tonic-gate */ 1223448Sdh155122 static spdsockparam_t lcl_param_arr[] = { 1230Sstevel@tonic-gate /* min max value name */ 1240Sstevel@tonic-gate { 4096, 65536, 8192, "spdsock_xmit_hiwat"}, 1250Sstevel@tonic-gate { 0, 65536, 1024, "spdsock_xmit_lowat"}, 1260Sstevel@tonic-gate { 4096, 65536, 8192, "spdsock_recv_hiwat"}, 1270Sstevel@tonic-gate { 65536, 1024*1024*1024, 256*1024, "spdsock_max_buf"}, 1280Sstevel@tonic-gate { 0, 3, 0, "spdsock_debug"}, 1290Sstevel@tonic-gate }; 1303448Sdh155122 #define spds_xmit_hiwat spds_params[0].spdsock_param_value 1313448Sdh155122 #define spds_xmit_lowat spds_params[1].spdsock_param_value 1323448Sdh155122 #define spds_recv_hiwat spds_params[2].spdsock_param_value 1333448Sdh155122 #define spds_max_buf spds_params[3].spdsock_param_value 1343448Sdh155122 #define spds_debug spds_params[4].spdsock_param_value 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate #define ss0dbg(a) printf a 1370Sstevel@tonic-gate /* NOTE: != 0 instead of > 0 so lint doesn't complain. */ 1383448Sdh155122 #define ss1dbg(spds, a) if (spds->spds_debug != 0) printf a 1393448Sdh155122 #define ss2dbg(spds, a) if (spds->spds_debug > 1) printf a 1403448Sdh155122 #define ss3dbg(spds, a) if (spds->spds_debug > 2) printf a 1410Sstevel@tonic-gate 1426430Sdanmcd #define RESET_SPDSOCK_DUMP_POLHEAD(ss, iph) { \ 1436430Sdanmcd ASSERT(RW_READ_HELD(&(iph)->iph_lock)); \ 1446430Sdanmcd (ss)->spdsock_dump_head = (iph); \ 1456430Sdanmcd (ss)->spdsock_dump_gen = (iph)->iph_gen; \ 1466430Sdanmcd (ss)->spdsock_dump_cur_type = 0; \ 1476430Sdanmcd (ss)->spdsock_dump_cur_af = IPSEC_AF_V4; \ 1486430Sdanmcd (ss)->spdsock_dump_cur_rule = NULL; \ 1496430Sdanmcd (ss)->spdsock_dump_count = 0; \ 1506430Sdanmcd (ss)->spdsock_dump_cur_chain = 0; \ 1516430Sdanmcd } 1526430Sdanmcd 1530Sstevel@tonic-gate static int spdsock_close(queue_t *); 1540Sstevel@tonic-gate static int spdsock_open(queue_t *, dev_t *, int, int, cred_t *); 1550Sstevel@tonic-gate static void spdsock_wput(queue_t *, mblk_t *); 1560Sstevel@tonic-gate static void spdsock_wsrv(queue_t *); 1570Sstevel@tonic-gate static void spdsock_rsrv(queue_t *); 1583448Sdh155122 static void *spdsock_stack_init(netstackid_t stackid, netstack_t *ns); 1593448Sdh155122 static void spdsock_stack_fini(netstackid_t stackid, void *arg); 1600Sstevel@tonic-gate static void spdsock_loadcheck(void *); 1613448Sdh155122 static void spdsock_merge_algs(spd_stack_t *); 1623448Sdh155122 static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *); 1633055Sdanmcd static mblk_t *spdsock_dump_next_record(spdsock_t *); 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate static struct module_info info = { 1660Sstevel@tonic-gate 5138, "spdsock", 1, INFPSZ, 512, 128 1670Sstevel@tonic-gate }; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate static struct qinit rinit = { 1700Sstevel@tonic-gate NULL, (pfi_t)spdsock_rsrv, spdsock_open, spdsock_close, 1710Sstevel@tonic-gate NULL, &info 1720Sstevel@tonic-gate }; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate static struct qinit winit = { 1750Sstevel@tonic-gate (pfi_t)spdsock_wput, (pfi_t)spdsock_wsrv, NULL, NULL, NULL, &info 1760Sstevel@tonic-gate }; 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate struct streamtab spdsockinfo = { 1790Sstevel@tonic-gate &rinit, &winit 1800Sstevel@tonic-gate }; 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* mapping from alg type to protocol number, as per RFC 2407 */ 1830Sstevel@tonic-gate static const uint_t algproto[] = { 1840Sstevel@tonic-gate PROTO_IPSEC_AH, 1850Sstevel@tonic-gate PROTO_IPSEC_ESP, 1860Sstevel@tonic-gate }; 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate #define NALGPROTOS (sizeof (algproto) / sizeof (algproto[0])) 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* mapping from kernel exec mode to spdsock exec mode */ 1910Sstevel@tonic-gate static const uint_t execmodes[] = { 1920Sstevel@tonic-gate SPD_ALG_EXEC_MODE_SYNC, 1930Sstevel@tonic-gate SPD_ALG_EXEC_MODE_ASYNC 1940Sstevel@tonic-gate }; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate #define NEXECMODES (sizeof (execmodes) / sizeof (execmodes[0])) 1970Sstevel@tonic-gate 1983055Sdanmcd #define ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1) 1993055Sdanmcd #define ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2) 2003055Sdanmcd 2014307Spwernau #define ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL) 2024307Spwernau 2030Sstevel@tonic-gate /* ARGSUSED */ 2040Sstevel@tonic-gate static int 2050Sstevel@tonic-gate spdsock_param_get(q, mp, cp, cr) 2060Sstevel@tonic-gate queue_t *q; 2070Sstevel@tonic-gate mblk_t *mp; 2080Sstevel@tonic-gate caddr_t cp; 2090Sstevel@tonic-gate cred_t *cr; 2100Sstevel@tonic-gate { 2110Sstevel@tonic-gate spdsockparam_t *spdsockpa = (spdsockparam_t *)cp; 2120Sstevel@tonic-gate uint_t value; 2133448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2143448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2153448Sdh155122 2163448Sdh155122 mutex_enter(&spds->spds_param_lock); 2170Sstevel@tonic-gate value = spdsockpa->spdsock_param_value; 2183448Sdh155122 mutex_exit(&spds->spds_param_lock); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate (void) mi_mpprintf(mp, "%u", value); 2210Sstevel@tonic-gate return (0); 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* This routine sets an NDD variable in a spdsockparam_t structure. */ 2250Sstevel@tonic-gate /* ARGSUSED */ 2260Sstevel@tonic-gate static int 2270Sstevel@tonic-gate spdsock_param_set(q, mp, value, cp, cr) 2280Sstevel@tonic-gate queue_t *q; 2290Sstevel@tonic-gate mblk_t *mp; 2300Sstevel@tonic-gate char *value; 2310Sstevel@tonic-gate caddr_t cp; 2320Sstevel@tonic-gate cred_t *cr; 2330Sstevel@tonic-gate { 2340Sstevel@tonic-gate ulong_t new_value; 2350Sstevel@tonic-gate spdsockparam_t *spdsockpa = (spdsockparam_t *)cp; 2363448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2373448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate /* Convert the value from a string into a long integer. */ 2400Sstevel@tonic-gate if (ddi_strtoul(value, NULL, 10, &new_value) != 0) 2410Sstevel@tonic-gate return (EINVAL); 2420Sstevel@tonic-gate 2433448Sdh155122 mutex_enter(&spds->spds_param_lock); 2440Sstevel@tonic-gate /* 2450Sstevel@tonic-gate * Fail the request if the new value does not lie within the 2460Sstevel@tonic-gate * required bounds. 2470Sstevel@tonic-gate */ 2480Sstevel@tonic-gate if (new_value < spdsockpa->spdsock_param_min || 2490Sstevel@tonic-gate new_value > spdsockpa->spdsock_param_max) { 2503448Sdh155122 mutex_exit(&spds->spds_param_lock); 2510Sstevel@tonic-gate return (EINVAL); 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate /* Set the new value */ 2550Sstevel@tonic-gate spdsockpa->spdsock_param_value = new_value; 2563448Sdh155122 mutex_exit(&spds->spds_param_lock); 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate return (0); 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 2613448Sdh155122 /* 2623448Sdh155122 * Initialize at module load time 2633448Sdh155122 */ 2640Sstevel@tonic-gate boolean_t 2650Sstevel@tonic-gate spdsock_ddi_init(void) 2660Sstevel@tonic-gate { 2670Sstevel@tonic-gate spdsock_max_optsize = optcom_max_optsize( 2680Sstevel@tonic-gate spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt); 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1, 2710Sstevel@tonic-gate NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER); 2720Sstevel@tonic-gate 2733448Sdh155122 /* 2743448Sdh155122 * We want to be informed each time a stack is created or 2753448Sdh155122 * destroyed in the kernel, so we can maintain the 2763448Sdh155122 * set of spd_stack_t's. 2773448Sdh155122 */ 2783448Sdh155122 netstack_register(NS_SPDSOCK, spdsock_stack_init, NULL, 2793448Sdh155122 spdsock_stack_fini); 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate return (B_TRUE); 2820Sstevel@tonic-gate } 2830Sstevel@tonic-gate 2843448Sdh155122 /* 2853448Sdh155122 * Walk through the param array specified registering each element with the 2863448Sdh155122 * named dispatch handler. 2873448Sdh155122 */ 2883448Sdh155122 static boolean_t 2893448Sdh155122 spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt) 2903448Sdh155122 { 2913448Sdh155122 for (; cnt-- > 0; ssp++) { 2923448Sdh155122 if (ssp->spdsock_param_name != NULL && 2933448Sdh155122 ssp->spdsock_param_name[0]) { 2943448Sdh155122 if (!nd_load(ndp, 2953448Sdh155122 ssp->spdsock_param_name, 2963448Sdh155122 spdsock_param_get, spdsock_param_set, 2973448Sdh155122 (caddr_t)ssp)) { 2983448Sdh155122 nd_free(ndp); 2993448Sdh155122 return (B_FALSE); 3003448Sdh155122 } 3013448Sdh155122 } 3023448Sdh155122 } 3033448Sdh155122 return (B_TRUE); 3043448Sdh155122 } 3053448Sdh155122 3063448Sdh155122 /* 3073448Sdh155122 * Initialize for each stack instance 3083448Sdh155122 */ 3093448Sdh155122 /* ARGSUSED */ 3103448Sdh155122 static void * 3113448Sdh155122 spdsock_stack_init(netstackid_t stackid, netstack_t *ns) 3123448Sdh155122 { 3133448Sdh155122 spd_stack_t *spds; 3143448Sdh155122 spdsockparam_t *ssp; 3153448Sdh155122 3163448Sdh155122 spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP); 3173448Sdh155122 spds->spds_netstack = ns; 3183448Sdh155122 3193448Sdh155122 ASSERT(spds->spds_g_nd == NULL); 3203448Sdh155122 3213448Sdh155122 ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 3223448Sdh155122 spds->spds_params = ssp; 3233448Sdh155122 bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr)); 3243448Sdh155122 3253448Sdh155122 (void) spdsock_param_register(&spds->spds_g_nd, ssp, 3263448Sdh155122 A_CNT(lcl_param_arr)); 3273448Sdh155122 3283448Sdh155122 mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL); 3293448Sdh155122 mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL); 3303448Sdh155122 3313448Sdh155122 return (spds); 3323448Sdh155122 } 3333448Sdh155122 3340Sstevel@tonic-gate void 3350Sstevel@tonic-gate spdsock_ddi_destroy(void) 3360Sstevel@tonic-gate { 3370Sstevel@tonic-gate vmem_destroy(spdsock_vmem); 3383448Sdh155122 3393448Sdh155122 netstack_unregister(NS_SPDSOCK); 3403448Sdh155122 } 3413448Sdh155122 3423448Sdh155122 /* ARGSUSED */ 3433448Sdh155122 static void 3443448Sdh155122 spdsock_stack_fini(netstackid_t stackid, void *arg) 3453448Sdh155122 { 3463448Sdh155122 spd_stack_t *spds = (spd_stack_t *)arg; 3473448Sdh155122 3486430Sdanmcd freemsg(spds->spds_mp_algs); 3493448Sdh155122 mutex_destroy(&spds->spds_param_lock); 3503448Sdh155122 mutex_destroy(&spds->spds_alg_lock); 3513448Sdh155122 nd_free(&spds->spds_g_nd); 3523448Sdh155122 kmem_free(spds->spds_params, sizeof (lcl_param_arr)); 3533448Sdh155122 spds->spds_params = NULL; 3543448Sdh155122 3553448Sdh155122 kmem_free(spds, sizeof (*spds)); 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* 3590Sstevel@tonic-gate * NOTE: large quantities of this should be shared with keysock. 3600Sstevel@tonic-gate * Would be nice to combine some of this into a common module, but 3610Sstevel@tonic-gate * not possible given time pressures. 3620Sstevel@tonic-gate */ 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate /* 3650Sstevel@tonic-gate * High-level reality checking of extensions. 3660Sstevel@tonic-gate */ 3670Sstevel@tonic-gate /* ARGSUSED */ /* XXX */ 3680Sstevel@tonic-gate static boolean_t 3690Sstevel@tonic-gate ext_check(spd_ext_t *ext) 3700Sstevel@tonic-gate { 3713055Sdanmcd spd_if_t *tunname = (spd_if_t *)ext; 3723055Sdanmcd int i; 3733055Sdanmcd char *idstr; 3743055Sdanmcd 3753055Sdanmcd if (ext->spd_ext_type == SPD_EXT_TUN_NAME) { 3763055Sdanmcd /* (NOTE: Modified from SADB_EXT_IDENTITY..) */ 3773055Sdanmcd 3783055Sdanmcd /* 3793055Sdanmcd * Make sure the strings in these identities are 3803055Sdanmcd * null-terminated. Let's "proactively" null-terminate the 3813055Sdanmcd * string at the last byte if it's not terminated sooner. 3823055Sdanmcd */ 3833055Sdanmcd i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t); 3843055Sdanmcd idstr = (char *)(tunname + 1); 3853055Sdanmcd while (*idstr != '\0' && i > 0) { 3863055Sdanmcd i--; 3873055Sdanmcd idstr++; 3883055Sdanmcd } 3893055Sdanmcd if (i == 0) { 3903055Sdanmcd /* 3913055Sdanmcd * I.e., if the bozo user didn't NULL-terminate the 3923055Sdanmcd * string... 3933055Sdanmcd */ 3943055Sdanmcd idstr--; 3953055Sdanmcd *idstr = '\0'; 3963055Sdanmcd } 3973055Sdanmcd } 3980Sstevel@tonic-gate return (B_TRUE); /* For now... */ 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate /* Return values for spdsock_get_ext(). */ 4040Sstevel@tonic-gate #define KGE_OK 0 4050Sstevel@tonic-gate #define KGE_DUP 1 4060Sstevel@tonic-gate #define KGE_UNK 2 4070Sstevel@tonic-gate #define KGE_LEN 3 4080Sstevel@tonic-gate #define KGE_CHK 4 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate /* 4110Sstevel@tonic-gate * Parse basic extension headers and return in the passed-in pointer vector. 4120Sstevel@tonic-gate * Return values include: 4130Sstevel@tonic-gate * 4140Sstevel@tonic-gate * KGE_OK Everything's nice and parsed out. 4150Sstevel@tonic-gate * If there are no extensions, place NULL in extv[0]. 4160Sstevel@tonic-gate * KGE_DUP There is a duplicate extension. 4170Sstevel@tonic-gate * First instance in appropriate bin. First duplicate in 4180Sstevel@tonic-gate * extv[0]. 4190Sstevel@tonic-gate * KGE_UNK Unknown extension type encountered. extv[0] contains 4200Sstevel@tonic-gate * unknown header. 4210Sstevel@tonic-gate * KGE_LEN Extension length error. 4220Sstevel@tonic-gate * KGE_CHK High-level reality check failed on specific extension. 4230Sstevel@tonic-gate * 4240Sstevel@tonic-gate * My apologies for some of the pointer arithmetic in here. I'm thinking 4250Sstevel@tonic-gate * like an assembly programmer, yet trying to make the compiler happy. 4260Sstevel@tonic-gate */ 4270Sstevel@tonic-gate static int 4280Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize) 4290Sstevel@tonic-gate { 4300Sstevel@tonic-gate bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1)); 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate /* Use extv[0] as the "current working pointer". */ 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate extv[0] = (spd_ext_t *)(basehdr + 1); 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) { 4370Sstevel@tonic-gate /* Check for unknown headers. */ 4380Sstevel@tonic-gate if (extv[0]->spd_ext_type == 0 || 4390Sstevel@tonic-gate extv[0]->spd_ext_type > SPD_EXT_MAX) 4400Sstevel@tonic-gate return (KGE_UNK); 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate /* 4430Sstevel@tonic-gate * Check length. Use uint64_t because extlen is in units 4440Sstevel@tonic-gate * of 64-bit words. If length goes beyond the msgsize, 4450Sstevel@tonic-gate * return an error. (Zero length also qualifies here.) 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate if (extv[0]->spd_ext_len == 0 || 4480Sstevel@tonic-gate (void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) > 4490Sstevel@tonic-gate (void *)((uint8_t *)basehdr + msgsize)) 4500Sstevel@tonic-gate return (KGE_LEN); 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate /* Check for redundant headers. */ 4530Sstevel@tonic-gate if (extv[extv[0]->spd_ext_type] != NULL) 4540Sstevel@tonic-gate return (KGE_DUP); 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate /* 4570Sstevel@tonic-gate * Reality check the extension if possible at the spdsock 4580Sstevel@tonic-gate * level. 4590Sstevel@tonic-gate */ 4600Sstevel@tonic-gate if (!ext_check(extv[0])) 4610Sstevel@tonic-gate return (KGE_CHK); 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate /* If I make it here, assign the appropriate bin. */ 4640Sstevel@tonic-gate extv[extv[0]->spd_ext_type] = extv[0]; 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* Advance pointer (See above for uint64_t ptr reasoning.) */ 4670Sstevel@tonic-gate extv[0] = (spd_ext_t *) 4680Sstevel@tonic-gate ((uint64_t *)extv[0] + extv[0]->spd_ext_len); 4690Sstevel@tonic-gate } 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate /* Everything's cool. */ 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate /* 4740Sstevel@tonic-gate * If extv[0] == NULL, then there are no extension headers in this 4750Sstevel@tonic-gate * message. Ensure that this is the case. 4760Sstevel@tonic-gate */ 4770Sstevel@tonic-gate if (extv[0] == (spd_ext_t *)(basehdr + 1)) 4780Sstevel@tonic-gate extv[0] = NULL; 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate return (KGE_OK); 4810Sstevel@tonic-gate } 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate static const int bad_ext_diag[] = { 4840Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_LCLPORT, 4850Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_REMPORT, 4860Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_PROTO, 4870Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_LCLADDR, 4880Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_REMADDR, 4890Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_ACTION, 4900Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_RULE, 4910Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_RULESET, 4920Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE 4930Sstevel@tonic-gate }; 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate static const int dup_ext_diag[] = { 4960Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_LCLPORT, 4970Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_REMPORT, 4980Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_PROTO, 4990Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_LCLADDR, 5000Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_REMADDR, 5010Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_ACTION, 5020Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_RULE, 5030Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_RULESET, 5040Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE 5050Sstevel@tonic-gate }; 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate /* 5080Sstevel@tonic-gate * Transmit a PF_POLICY error message to the instance either pointed to 5090Sstevel@tonic-gate * by ks, the instance with serial number serial, or more, depending. 5100Sstevel@tonic-gate * 5110Sstevel@tonic-gate * The faulty message (or a reasonable facsimile thereof) is in mp. 5120Sstevel@tonic-gate * This function will free mp or recycle it for delivery, thereby causing 5130Sstevel@tonic-gate * the stream head to free it. 5140Sstevel@tonic-gate */ 5150Sstevel@tonic-gate static void 5160Sstevel@tonic-gate spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic) 5170Sstevel@tonic-gate { 5180Sstevel@tonic-gate spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate ASSERT(mp->b_datap->db_type == M_DATA); 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate if (spmsg->spd_msg_type < SPD_MIN || 5230Sstevel@tonic-gate spmsg->spd_msg_type > SPD_MAX) 5240Sstevel@tonic-gate spmsg->spd_msg_type = SPD_RESERVED; 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate /* 5270Sstevel@tonic-gate * Strip out extension headers. 5280Sstevel@tonic-gate */ 5290Sstevel@tonic-gate ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim); 5300Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (*spmsg); 5310Sstevel@tonic-gate spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t)); 5320Sstevel@tonic-gate spmsg->spd_msg_errno = (uint8_t)error; 5330Sstevel@tonic-gate spmsg->spd_msg_diagnostic = (uint16_t)diagnostic; 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate qreply(q, mp); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate static void 5390Sstevel@tonic-gate spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic) 5400Sstevel@tonic-gate { 5410Sstevel@tonic-gate spdsock_error(q, mp, EINVAL, diagnostic); 5420Sstevel@tonic-gate } 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate static void 5450Sstevel@tonic-gate spd_echo(queue_t *q, mblk_t *mp) 5460Sstevel@tonic-gate { 5470Sstevel@tonic-gate qreply(q, mp); 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate 5503055Sdanmcd /* 5513055Sdanmcd * Do NOT consume a reference to itp. 5523055Sdanmcd */ 5533448Sdh155122 /*ARGSUSED*/ 5540Sstevel@tonic-gate static void 5553448Sdh155122 spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns) 5563055Sdanmcd { 5573055Sdanmcd boolean_t active = (boolean_t)cookie; 5583055Sdanmcd ipsec_policy_head_t *iph; 5593055Sdanmcd 5603055Sdanmcd iph = active ? itp->itp_policy : itp->itp_inactive; 5613055Sdanmcd IPPH_REFHOLD(iph); 5623055Sdanmcd mutex_enter(&itp->itp_lock); 5633448Sdh155122 spdsock_flush_one(iph, ns); 5643055Sdanmcd if (active) 5653055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 5663055Sdanmcd else 5673055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 5683055Sdanmcd mutex_exit(&itp->itp_lock); 5693055Sdanmcd } 5703055Sdanmcd 5713055Sdanmcd /* 5723055Sdanmcd * Clear out one polhead. 5733055Sdanmcd */ 5743055Sdanmcd static void 5753448Sdh155122 spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns) 5760Sstevel@tonic-gate { 5770Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_WRITER); 5783448Sdh155122 ipsec_polhead_flush(iph, ns); 5790Sstevel@tonic-gate rw_exit(&iph->iph_lock); 5803448Sdh155122 IPPH_REFRELE(iph, ns); 5813055Sdanmcd } 5823055Sdanmcd 5833055Sdanmcd static void 5844307Spwernau spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, ipsec_tun_pol_t *itp, 5854307Spwernau mblk_t *mp) 5863055Sdanmcd { 5873055Sdanmcd boolean_t active; 5883448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 5896430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 5903055Sdanmcd 5913055Sdanmcd if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) { 5926430Sdanmcd spdsock_flush_one(iph, ns); 5934307Spwernau if (audit_active) { 5944307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 5958778SErik.Nordmark@Sun.COM cred_t *cr; 5968778SErik.Nordmark@Sun.COM pid_t cpid; 5978778SErik.Nordmark@Sun.COM 5988778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 5994307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 6008778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLUSH, cr, ns, 6018778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, 0, cpid); 6024307Spwernau } 6033055Sdanmcd } else { 6043055Sdanmcd active = (iph == ALL_ACTIVE_POLHEADS); 6053055Sdanmcd 6063055Sdanmcd /* First flush the global policy. */ 6073448Sdh155122 spdsock_flush_one(active ? ipsec_system_policy(ns) : 6083448Sdh155122 ipsec_inactive_policy(ns), ns); 6094307Spwernau if (audit_active) { 6108778SErik.Nordmark@Sun.COM cred_t *cr; 6118778SErik.Nordmark@Sun.COM pid_t cpid; 6128778SErik.Nordmark@Sun.COM 6138778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 6148778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLUSH, cr, ns, NULL, 6158778SErik.Nordmark@Sun.COM active, 0, cpid); 6164307Spwernau } 6173055Sdanmcd /* Then flush every tunnel's appropriate one. */ 6186430Sdanmcd itp_walk(spdsock_flush_node, (void *)active, ns); 6198778SErik.Nordmark@Sun.COM if (audit_active) { 6208778SErik.Nordmark@Sun.COM cred_t *cr; 6218778SErik.Nordmark@Sun.COM pid_t cpid; 6228778SErik.Nordmark@Sun.COM 6238778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 6248778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLUSH, cr, ns, 6258778SErik.Nordmark@Sun.COM "all tunnels", active, 0, cpid); 6268778SErik.Nordmark@Sun.COM } 6273055Sdanmcd } 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate spd_echo(q, mp); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate static boolean_t 6330Sstevel@tonic-gate spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag) 6340Sstevel@tonic-gate { 6350Sstevel@tonic-gate bzero(sel, sizeof (*sel)); 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate if (extv[SPD_EXT_PROTO] != NULL) { 6380Sstevel@tonic-gate struct spd_proto *pr = 6390Sstevel@tonic-gate (struct spd_proto *)extv[SPD_EXT_PROTO]; 6400Sstevel@tonic-gate sel->ipsl_proto = pr->spd_proto_number; 6410Sstevel@tonic-gate sel->ipsl_valid |= IPSL_PROTOCOL; 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate if (extv[SPD_EXT_LCLPORT] != NULL) { 6440Sstevel@tonic-gate struct spd_portrange *pr = 6450Sstevel@tonic-gate (struct spd_portrange *)extv[SPD_EXT_LCLPORT]; 6460Sstevel@tonic-gate sel->ipsl_lport = pr->spd_ports_minport; 6470Sstevel@tonic-gate sel->ipsl_valid |= IPSL_LOCAL_PORT; 6480Sstevel@tonic-gate } 6490Sstevel@tonic-gate if (extv[SPD_EXT_REMPORT] != NULL) { 6500Sstevel@tonic-gate struct spd_portrange *pr = 6510Sstevel@tonic-gate (struct spd_portrange *)extv[SPD_EXT_REMPORT]; 6520Sstevel@tonic-gate sel->ipsl_rport = pr->spd_ports_minport; 6530Sstevel@tonic-gate sel->ipsl_valid |= IPSL_REMOTE_PORT; 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) { 6570Sstevel@tonic-gate struct spd_typecode *tc= 6580Sstevel@tonic-gate (struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE]; 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate sel->ipsl_valid |= IPSL_ICMP_TYPE; 6610Sstevel@tonic-gate sel->ipsl_icmp_type = tc->spd_typecode_type; 6620Sstevel@tonic-gate if (tc->spd_typecode_type_end < tc->spd_typecode_type) 6630Sstevel@tonic-gate sel->ipsl_icmp_type_end = tc->spd_typecode_type; 6640Sstevel@tonic-gate else 6650Sstevel@tonic-gate sel->ipsl_icmp_type_end = tc->spd_typecode_type_end; 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate if (tc->spd_typecode_code != 255) { 6680Sstevel@tonic-gate sel->ipsl_valid |= IPSL_ICMP_CODE; 6690Sstevel@tonic-gate sel->ipsl_icmp_code = tc->spd_typecode_code; 6700Sstevel@tonic-gate if (tc->spd_typecode_code_end < tc->spd_typecode_code) 6710Sstevel@tonic-gate sel->ipsl_icmp_code_end = tc->spd_typecode_code; 6720Sstevel@tonic-gate else 6730Sstevel@tonic-gate sel->ipsl_icmp_code_end = 6740Sstevel@tonic-gate tc->spd_typecode_code_end; 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate #define ADDR2SEL(sel, extv, field, pfield, extn, bit) \ 6780Sstevel@tonic-gate if ((extv)[(extn)] != NULL) { \ 6790Sstevel@tonic-gate uint_t addrlen; \ 6800Sstevel@tonic-gate struct spd_address *ap = \ 6810Sstevel@tonic-gate (struct spd_address *)((extv)[(extn)]); \ 6820Sstevel@tonic-gate addrlen = (ap->spd_address_af == AF_INET6) ? \ 6830Sstevel@tonic-gate IPV6_ADDR_LEN : IP_ADDR_LEN; \ 6840Sstevel@tonic-gate if (SPD_64TO8(ap->spd_address_len) < \ 6850Sstevel@tonic-gate (addrlen + sizeof (*ap))) { \ 6860Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN; \ 6870Sstevel@tonic-gate return (B_FALSE); \ 6880Sstevel@tonic-gate } \ 6890Sstevel@tonic-gate bcopy((ap+1), &((sel)->field), addrlen); \ 6900Sstevel@tonic-gate (sel)->pfield = ap->spd_address_prefixlen; \ 6910Sstevel@tonic-gate (sel)->ipsl_valid |= (bit); \ 6920Sstevel@tonic-gate (sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ? \ 6930Sstevel@tonic-gate IPSL_IPV6 : IPSL_IPV4; \ 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen, 6970Sstevel@tonic-gate SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR); 6980Sstevel@tonic-gate ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen, 6990Sstevel@tonic-gate SPD_EXT_REMADDR, IPSL_REMOTE_ADDR); 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) == 7020Sstevel@tonic-gate (IPSL_IPV6|IPSL_IPV4)) { 7030Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_MIXED_AF; 7040Sstevel@tonic-gate return (B_FALSE); 7050Sstevel@tonic-gate } 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate #undef ADDR2SEL 7080Sstevel@tonic-gate 7090Sstevel@tonic-gate return (B_TRUE); 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate 7120Sstevel@tonic-gate static boolean_t 7130Sstevel@tonic-gate spd_convert_type(uint32_t type, ipsec_act_t *act) 7140Sstevel@tonic-gate { 7150Sstevel@tonic-gate switch (type) { 7160Sstevel@tonic-gate case SPD_ACTTYPE_DROP: 7170Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_DISCARD; 7180Sstevel@tonic-gate return (B_TRUE); 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate case SPD_ACTTYPE_PASS: 7210Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_CLEAR; 7220Sstevel@tonic-gate return (B_TRUE); 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate case SPD_ACTTYPE_IPSEC: 7250Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_APPLY; 7260Sstevel@tonic-gate return (B_TRUE); 7270Sstevel@tonic-gate } 7280Sstevel@tonic-gate return (B_FALSE); 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate static boolean_t 7320Sstevel@tonic-gate spd_convert_flags(uint32_t flags, ipsec_act_t *act) 7330Sstevel@tonic-gate { 7340Sstevel@tonic-gate /* 7350Sstevel@tonic-gate * Note use of !! for boolean canonicalization. 7360Sstevel@tonic-gate */ 7370Sstevel@tonic-gate act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH); 7380Sstevel@tonic-gate act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP); 7390Sstevel@tonic-gate act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA); 7400Sstevel@tonic-gate act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE); 7410Sstevel@tonic-gate act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE); 7420Sstevel@tonic-gate return (B_TRUE); 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate static void 7460Sstevel@tonic-gate spdsock_reset_act(ipsec_act_t *act) 7470Sstevel@tonic-gate { 7480Sstevel@tonic-gate bzero(act, sizeof (*act)); 7490Sstevel@tonic-gate act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS; 7500Sstevel@tonic-gate act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS; 7510Sstevel@tonic-gate act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS; 7520Sstevel@tonic-gate } 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate /* 7550Sstevel@tonic-gate * Sanity check action against reality, and shrink-wrap key sizes.. 7560Sstevel@tonic-gate */ 7570Sstevel@tonic-gate static boolean_t 7583448Sdh155122 spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag, 7593448Sdh155122 spd_stack_t *spds) 7600Sstevel@tonic-gate { 7613055Sdanmcd if (tunnel_polhead && act->ipa_apply.ipp_use_unique) { 7623055Sdanmcd *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7633055Sdanmcd return (B_FALSE); 7643055Sdanmcd } 7650Sstevel@tonic-gate if ((act->ipa_type != IPSEC_ACT_APPLY) && 7660Sstevel@tonic-gate (act->ipa_apply.ipp_use_ah || 7675240Snordmark act->ipa_apply.ipp_use_esp || 7685240Snordmark act->ipa_apply.ipp_use_espa || 7695240Snordmark act->ipa_apply.ipp_use_se || 7705240Snordmark act->ipa_apply.ipp_use_unique)) { 7710Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7720Sstevel@tonic-gate return (B_FALSE); 7730Sstevel@tonic-gate } 7740Sstevel@tonic-gate if ((act->ipa_type == IPSEC_ACT_APPLY) && 7750Sstevel@tonic-gate !act->ipa_apply.ipp_use_ah && 7760Sstevel@tonic-gate !act->ipa_apply.ipp_use_esp) { 7770Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 7780Sstevel@tonic-gate return (B_FALSE); 7790Sstevel@tonic-gate } 7803448Sdh155122 return (ipsec_check_action(act, diag, spds->spds_netstack)); 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate /* 7840Sstevel@tonic-gate * We may be short a few error checks here.. 7850Sstevel@tonic-gate */ 7860Sstevel@tonic-gate static boolean_t 7870Sstevel@tonic-gate spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp, 7883448Sdh155122 int *diag, spd_stack_t *spds) 7890Sstevel@tonic-gate { 7900Sstevel@tonic-gate struct spd_ext_actions *sactp = 7910Sstevel@tonic-gate (struct spd_ext_actions *)extv[SPD_EXT_ACTION]; 7920Sstevel@tonic-gate ipsec_act_t act, *actp, *endactp; 7930Sstevel@tonic-gate struct spd_attribute *attrp, *endattrp; 7940Sstevel@tonic-gate uint64_t *endp; 7950Sstevel@tonic-gate int nact; 7963055Sdanmcd boolean_t tunnel_polhead; 7973055Sdanmcd 7983055Sdanmcd tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL && 7993055Sdanmcd (((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags & 8005240Snordmark SPD_RULE_FLAG_TUNNEL)); 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate *actpp = NULL; 8030Sstevel@tonic-gate *nactp = 0; 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate if (sactp == NULL) { 8060Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_NO_ACTION_EXT; 8070Sstevel@tonic-gate return (B_FALSE); 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate /* 8110Sstevel@tonic-gate * Parse the "action" extension and convert into an action chain. 8120Sstevel@tonic-gate */ 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate nact = sactp->spd_actions_count; 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate endp = (uint64_t *)sactp; 8170Sstevel@tonic-gate endp += sactp->spd_actions_len; 8180Sstevel@tonic-gate endattrp = (struct spd_attribute *)endp; 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP); 8210Sstevel@tonic-gate if (actp == NULL) { 8220Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_NO_MEM; 8230Sstevel@tonic-gate return (B_FALSE); 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate *actpp = actp; 8260Sstevel@tonic-gate *nactp = nact; 8270Sstevel@tonic-gate endactp = actp + nact; 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate spdsock_reset_act(&act); 8300Sstevel@tonic-gate attrp = (struct spd_attribute *)(&sactp[1]); 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate for (; attrp < endattrp; attrp++) { 8330Sstevel@tonic-gate switch (attrp->spd_attr_tag) { 8340Sstevel@tonic-gate case SPD_ATTR_NOP: 8350Sstevel@tonic-gate break; 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate case SPD_ATTR_EMPTY: 8380Sstevel@tonic-gate spdsock_reset_act(&act); 8390Sstevel@tonic-gate break; 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate case SPD_ATTR_END: 8420Sstevel@tonic-gate attrp = endattrp; 8430Sstevel@tonic-gate /* FALLTHRU */ 8440Sstevel@tonic-gate case SPD_ATTR_NEXT: 8450Sstevel@tonic-gate if (actp >= endactp) { 8460Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT; 8470Sstevel@tonic-gate goto fail; 8480Sstevel@tonic-gate } 8493448Sdh155122 if (!spdsock_check_action(&act, tunnel_polhead, 8503448Sdh155122 diag, spds)) 8510Sstevel@tonic-gate goto fail; 8520Sstevel@tonic-gate *actp++ = act; 8533055Sdanmcd spdsock_reset_act(&act); 8540Sstevel@tonic-gate break; 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate case SPD_ATTR_TYPE: 8570Sstevel@tonic-gate if (!spd_convert_type(attrp->spd_attr_value, &act)) { 8580Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE; 8590Sstevel@tonic-gate goto fail; 8600Sstevel@tonic-gate } 8610Sstevel@tonic-gate break; 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate case SPD_ATTR_FLAGS: 8643055Sdanmcd if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) { 8653055Sdanmcd /* 8663055Sdanmcd * Set "sa unique" for transport-mode 8673055Sdanmcd * tunnels whether we want to or not. 8683055Sdanmcd */ 8693055Sdanmcd attrp->spd_attr_value |= SPD_APPLY_UNIQUE; 8703055Sdanmcd } 8710Sstevel@tonic-gate if (!spd_convert_flags(attrp->spd_attr_value, &act)) { 8720Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS; 8730Sstevel@tonic-gate goto fail; 8740Sstevel@tonic-gate } 8750Sstevel@tonic-gate break; 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate case SPD_ATTR_AH_AUTH: 8783055Sdanmcd if (attrp->spd_attr_value == 0) { 8793055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG; 8803055Sdanmcd goto fail; 8813055Sdanmcd } 8820Sstevel@tonic-gate act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value; 8830Sstevel@tonic-gate break; 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate case SPD_ATTR_ESP_ENCR: 8863055Sdanmcd if (attrp->spd_attr_value == 0) { 8873055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG; 8883055Sdanmcd goto fail; 8893055Sdanmcd } 8900Sstevel@tonic-gate act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value; 8910Sstevel@tonic-gate break; 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate case SPD_ATTR_ESP_AUTH: 8943055Sdanmcd if (attrp->spd_attr_value == 0) { 8953055Sdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG; 8963055Sdanmcd goto fail; 8973055Sdanmcd } 8980Sstevel@tonic-gate act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value; 8990Sstevel@tonic-gate break; 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate case SPD_ATTR_ENCR_MINBITS: 9020Sstevel@tonic-gate act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value; 9030Sstevel@tonic-gate break; 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate case SPD_ATTR_ENCR_MAXBITS: 9060Sstevel@tonic-gate act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value; 9070Sstevel@tonic-gate break; 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate case SPD_ATTR_AH_MINBITS: 9100Sstevel@tonic-gate act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value; 9110Sstevel@tonic-gate break; 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate case SPD_ATTR_AH_MAXBITS: 9140Sstevel@tonic-gate act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value; 9150Sstevel@tonic-gate break; 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate case SPD_ATTR_ESPA_MINBITS: 9180Sstevel@tonic-gate act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value; 9190Sstevel@tonic-gate break; 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate case SPD_ATTR_ESPA_MAXBITS: 9220Sstevel@tonic-gate act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value; 9230Sstevel@tonic-gate break; 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate case SPD_ATTR_LIFE_SOFT_TIME: 9260Sstevel@tonic-gate case SPD_ATTR_LIFE_HARD_TIME: 9270Sstevel@tonic-gate case SPD_ATTR_LIFE_SOFT_BYTES: 9280Sstevel@tonic-gate case SPD_ATTR_LIFE_HARD_BYTES: 9290Sstevel@tonic-gate break; 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate case SPD_ATTR_KM_PROTO: 9320Sstevel@tonic-gate act.ipa_apply.ipp_km_proto = attrp->spd_attr_value; 9330Sstevel@tonic-gate break; 9340Sstevel@tonic-gate 9350Sstevel@tonic-gate case SPD_ATTR_KM_COOKIE: 9360Sstevel@tonic-gate act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value; 9370Sstevel@tonic-gate break; 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate case SPD_ATTR_REPLAY_DEPTH: 9400Sstevel@tonic-gate act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value; 9410Sstevel@tonic-gate break; 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate } 9440Sstevel@tonic-gate if (actp != endactp) { 9450Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT; 9460Sstevel@tonic-gate goto fail; 9470Sstevel@tonic-gate } 9480Sstevel@tonic-gate 9490Sstevel@tonic-gate return (B_TRUE); 9500Sstevel@tonic-gate fail: 9510Sstevel@tonic-gate ipsec_actvec_free(*actpp, nact); 9520Sstevel@tonic-gate *actpp = NULL; 9530Sstevel@tonic-gate return (B_FALSE); 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate typedef struct 9570Sstevel@tonic-gate { 9580Sstevel@tonic-gate ipsec_policy_t *pol; 9590Sstevel@tonic-gate int dir; 9600Sstevel@tonic-gate } tmprule_t; 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate static int 9630Sstevel@tonic-gate mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule, 9640Sstevel@tonic-gate ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af, 9653448Sdh155122 tmprule_t **rp, uint64_t *index, spd_stack_t *spds) 9660Sstevel@tonic-gate { 9670Sstevel@tonic-gate ipsec_policy_t *pol; 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4); 9700Sstevel@tonic-gate sel->ipsl_valid |= af; 9710Sstevel@tonic-gate 9723055Sdanmcd pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority, 9733448Sdh155122 index, spds->spds_netstack); 9740Sstevel@tonic-gate if (pol == NULL) 9750Sstevel@tonic-gate return (ENOMEM); 9760Sstevel@tonic-gate 9770Sstevel@tonic-gate (*rp)->pol = pol; 9780Sstevel@tonic-gate (*rp)->dir = dir; 9790Sstevel@tonic-gate (*rp)++; 9800Sstevel@tonic-gate 9810Sstevel@tonic-gate if (!ipsec_check_policy(iph, pol, dir)) 9820Sstevel@tonic-gate return (EEXIST); 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate rule->spd_rule_index = pol->ipsp_index; 9850Sstevel@tonic-gate return (0); 9860Sstevel@tonic-gate } 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate static int 9890Sstevel@tonic-gate mkrulepair(ipsec_policy_head_t *iph, struct spd_rule *rule, 9900Sstevel@tonic-gate ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t afs, 9913448Sdh155122 tmprule_t **rp, uint64_t *index, spd_stack_t *spds) 9920Sstevel@tonic-gate { 9930Sstevel@tonic-gate int error; 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate if (afs & IPSL_IPV4) { 9963055Sdanmcd error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV4, rp, 9973448Sdh155122 index, spds); 9980Sstevel@tonic-gate if (error != 0) 9990Sstevel@tonic-gate return (error); 10000Sstevel@tonic-gate } 10010Sstevel@tonic-gate if (afs & IPSL_IPV6) { 10023055Sdanmcd error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV6, rp, 10033448Sdh155122 index, spds); 10040Sstevel@tonic-gate if (error != 0) 10050Sstevel@tonic-gate return (error); 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate return (0); 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate static void 10123055Sdanmcd spdsock_addrule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 10133055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 10140Sstevel@tonic-gate { 10150Sstevel@tonic-gate ipsec_selkey_t sel; 10160Sstevel@tonic-gate ipsec_act_t *actp; 10170Sstevel@tonic-gate uint_t nact; 10183055Sdanmcd int diag = 0, error, afs; 10190Sstevel@tonic-gate struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE]; 10200Sstevel@tonic-gate tmprule_t rules[4], *rulep = &rules[0]; 10213055Sdanmcd boolean_t tunnel_mode, empty_itp, active; 10223055Sdanmcd uint64_t *index = (itp == NULL) ? NULL : &itp->itp_next_policy_index; 10233448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 10243448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 10250Sstevel@tonic-gate 10260Sstevel@tonic-gate if (rule == NULL) { 10270Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT); 10284307Spwernau if (audit_active) { 10294307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 10308778SErik.Nordmark@Sun.COM cred_t *cr; 10318778SErik.Nordmark@Sun.COM pid_t cpid; 10328778SErik.Nordmark@Sun.COM 10338778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 10344307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 10358778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_ADDRULE, cr, 10364307Spwernau spds->spds_netstack, ITP_NAME(itp), active, 10378778SErik.Nordmark@Sun.COM SPD_DIAGNOSTIC_NO_RULE_EXT, cpid); 10384307Spwernau } 10390Sstevel@tonic-gate return; 10400Sstevel@tonic-gate } 10410Sstevel@tonic-gate 10423055Sdanmcd tunnel_mode = (rule->spd_rule_flags & SPD_RULE_FLAG_TUNNEL); 10433055Sdanmcd 10443055Sdanmcd if (itp != NULL) { 10453055Sdanmcd mutex_enter(&itp->itp_lock); 10463055Sdanmcd ASSERT(itp->itp_policy == iph || itp->itp_inactive == iph); 10473055Sdanmcd active = (itp->itp_policy == iph); 10483055Sdanmcd if (ITP_P_ISACTIVE(itp, iph)) { 10493055Sdanmcd /* Check for mix-and-match of tunnel/transport. */ 10503055Sdanmcd if ((tunnel_mode && !ITP_P_ISTUNNEL(itp, iph)) || 10513055Sdanmcd (!tunnel_mode && ITP_P_ISTUNNEL(itp, iph))) { 10523055Sdanmcd mutex_exit(&itp->itp_lock); 10533055Sdanmcd spdsock_error(q, mp, EBUSY, 0); 10543055Sdanmcd return; 10553055Sdanmcd } 10563055Sdanmcd empty_itp = B_FALSE; 10573055Sdanmcd } else { 10583055Sdanmcd empty_itp = B_TRUE; 10593055Sdanmcd itp->itp_flags = active ? ITPF_P_ACTIVE : ITPF_I_ACTIVE; 10603055Sdanmcd if (tunnel_mode) 10613055Sdanmcd itp->itp_flags |= active ? ITPF_P_TUNNEL : 10623055Sdanmcd ITPF_I_TUNNEL; 10633055Sdanmcd } 10643055Sdanmcd } else { 10653055Sdanmcd empty_itp = B_FALSE; 10663055Sdanmcd } 10673055Sdanmcd 10680Sstevel@tonic-gate if (rule->spd_rule_index != 0) { 10693055Sdanmcd diag = SPD_DIAGNOSTIC_INVALID_RULE_INDEX; 10703055Sdanmcd error = EINVAL; 10713055Sdanmcd goto fail2; 10720Sstevel@tonic-gate } 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate if (!spdsock_ext_to_sel(extv, &sel, &diag)) { 10753055Sdanmcd error = EINVAL; 10763055Sdanmcd goto fail2; 10773055Sdanmcd } 10783055Sdanmcd 10793055Sdanmcd if (itp != NULL) { 10803055Sdanmcd if (tunnel_mode) { 10813055Sdanmcd if (sel.ipsl_valid & 10823055Sdanmcd (IPSL_REMOTE_PORT | IPSL_LOCAL_PORT)) { 10833055Sdanmcd itp->itp_flags |= active ? 10843055Sdanmcd ITPF_P_PER_PORT_SECURITY : 10853055Sdanmcd ITPF_I_PER_PORT_SECURITY; 10863055Sdanmcd } 10873055Sdanmcd } else { 10883055Sdanmcd /* 10893055Sdanmcd * For now, we don't allow transport-mode on a tunnel 10903055Sdanmcd * with ANY specific selectors. Bail if we have such 10913055Sdanmcd * a request. 10923055Sdanmcd */ 10933055Sdanmcd if (sel.ipsl_valid & IPSL_WILDCARD) { 10943055Sdanmcd diag = SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS; 10953055Sdanmcd error = EINVAL; 10963055Sdanmcd goto fail2; 10973055Sdanmcd } 10983055Sdanmcd } 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate 11013448Sdh155122 if (!spdsock_ext_to_actvec(extv, &actp, &nact, &diag, spds)) { 11023055Sdanmcd error = EINVAL; 11033055Sdanmcd goto fail2; 11040Sstevel@tonic-gate } 11050Sstevel@tonic-gate /* 11060Sstevel@tonic-gate * If no addresses were specified, add both. 11070Sstevel@tonic-gate */ 11080Sstevel@tonic-gate afs = sel.ipsl_valid & (IPSL_IPV6|IPSL_IPV4); 11090Sstevel@tonic-gate if (afs == 0) 11100Sstevel@tonic-gate afs = (IPSL_IPV6|IPSL_IPV4); 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_WRITER); 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate if (rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) { 11150Sstevel@tonic-gate error = mkrulepair(iph, rule, &sel, actp, nact, 11163448Sdh155122 IPSEC_TYPE_OUTBOUND, afs, &rulep, index, spds); 11170Sstevel@tonic-gate if (error != 0) 11180Sstevel@tonic-gate goto fail; 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate if (rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) { 11220Sstevel@tonic-gate error = mkrulepair(iph, rule, &sel, actp, nact, 11233448Sdh155122 IPSEC_TYPE_INBOUND, afs, &rulep, index, spds); 11240Sstevel@tonic-gate if (error != 0) 11250Sstevel@tonic-gate goto fail; 11260Sstevel@tonic-gate } 11270Sstevel@tonic-gate 11283448Sdh155122 while ((--rulep) >= &rules[0]) { 11293448Sdh155122 ipsec_enter_policy(iph, rulep->pol, rulep->dir, 11303448Sdh155122 spds->spds_netstack); 11313448Sdh155122 } 11320Sstevel@tonic-gate rw_exit(&iph->iph_lock); 11333055Sdanmcd if (itp != NULL) 11343055Sdanmcd mutex_exit(&itp->itp_lock); 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate ipsec_actvec_free(actp, nact); 11370Sstevel@tonic-gate spd_echo(q, mp); 11384307Spwernau if (audit_active) { 11394307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 11408778SErik.Nordmark@Sun.COM cred_t *cr; 11418778SErik.Nordmark@Sun.COM pid_t cpid; 11428778SErik.Nordmark@Sun.COM 11438778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 11444307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 11458778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack, 11468778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, 0, cpid); 11474307Spwernau } 11480Sstevel@tonic-gate return; 11490Sstevel@tonic-gate 11500Sstevel@tonic-gate fail: 11510Sstevel@tonic-gate rw_exit(&iph->iph_lock); 1152*11042SErik.Nordmark@Sun.COM while ((--rulep) >= &rules[0]) 1153*11042SErik.Nordmark@Sun.COM IPPOL_REFRELE(rulep->pol); 11540Sstevel@tonic-gate ipsec_actvec_free(actp, nact); 11553055Sdanmcd fail2: 11563055Sdanmcd if (itp != NULL) { 11573055Sdanmcd if (empty_itp) 11583055Sdanmcd itp->itp_flags = 0; 11593055Sdanmcd mutex_exit(&itp->itp_lock); 11603055Sdanmcd } 11613055Sdanmcd spdsock_error(q, mp, error, diag); 11624307Spwernau if (audit_active) { 11634307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 11648778SErik.Nordmark@Sun.COM cred_t *cr; 11658778SErik.Nordmark@Sun.COM pid_t cpid; 11668778SErik.Nordmark@Sun.COM 11678778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 11684307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 11698778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack, 11708778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, error, cpid); 11714307Spwernau } 11720Sstevel@tonic-gate } 11730Sstevel@tonic-gate 11740Sstevel@tonic-gate void 11753055Sdanmcd spdsock_deleterule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 11763055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 11770Sstevel@tonic-gate { 11780Sstevel@tonic-gate ipsec_selkey_t sel; 11790Sstevel@tonic-gate struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE]; 11803055Sdanmcd int err, diag = 0; 11813448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 11826430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 11830Sstevel@tonic-gate 11840Sstevel@tonic-gate if (rule == NULL) { 11850Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT); 11864307Spwernau if (audit_active) { 11874307Spwernau boolean_t active; 11884307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 11898778SErik.Nordmark@Sun.COM cred_t *cr; 11908778SErik.Nordmark@Sun.COM pid_t cpid; 11918778SErik.Nordmark@Sun.COM 11928778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 11934307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 11948778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_DELETERULE, cr, ns, 11956430Sdanmcd ITP_NAME(itp), active, SPD_DIAGNOSTIC_NO_RULE_EXT, 11968778SErik.Nordmark@Sun.COM cpid); 11974307Spwernau } 11980Sstevel@tonic-gate return; 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate 12013055Sdanmcd /* 12023055Sdanmcd * Must enter itp_lock first to avoid deadlock. See tun.c's 12033055Sdanmcd * set_sec_simple() for the other case of itp_lock and iph_lock. 12043055Sdanmcd */ 12053055Sdanmcd if (itp != NULL) 12063055Sdanmcd mutex_enter(&itp->itp_lock); 12073055Sdanmcd 12080Sstevel@tonic-gate if (rule->spd_rule_index != 0) { 12096430Sdanmcd if (ipsec_policy_delete_index(iph, rule->spd_rule_index, ns) != 12106430Sdanmcd 0) { 12113055Sdanmcd err = ESRCH; 12123055Sdanmcd goto fail; 12130Sstevel@tonic-gate } 12140Sstevel@tonic-gate } else { 12150Sstevel@tonic-gate if (!spdsock_ext_to_sel(extv, &sel, &diag)) { 12163055Sdanmcd err = EINVAL; /* diag already set... */ 12173055Sdanmcd goto fail; 12183055Sdanmcd } 12193055Sdanmcd 12203055Sdanmcd if ((rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) && 12216430Sdanmcd !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_INBOUND, ns)) { 12223055Sdanmcd err = ESRCH; 12233055Sdanmcd goto fail; 12243055Sdanmcd } 12253055Sdanmcd 12263055Sdanmcd if ((rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) && 12276430Sdanmcd !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_OUTBOUND, ns)) { 12283055Sdanmcd err = ESRCH; 12293055Sdanmcd goto fail; 12300Sstevel@tonic-gate } 12313055Sdanmcd } 12323055Sdanmcd 12333055Sdanmcd if (itp != NULL) { 12343055Sdanmcd ASSERT(iph == itp->itp_policy || iph == itp->itp_inactive); 12353055Sdanmcd rw_enter(&iph->iph_lock, RW_READER); 12363055Sdanmcd if (avl_numnodes(&iph->iph_rulebyid) == 0) { 12373055Sdanmcd if (iph == itp->itp_policy) 12383055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 12393055Sdanmcd else 12403055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 12410Sstevel@tonic-gate } 12423055Sdanmcd /* Can exit locks in any order. */ 12433055Sdanmcd rw_exit(&iph->iph_lock); 12443055Sdanmcd mutex_exit(&itp->itp_lock); 12450Sstevel@tonic-gate } 12460Sstevel@tonic-gate spd_echo(q, mp); 12474307Spwernau if (audit_active) { 12484307Spwernau boolean_t active; 12494307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 12508778SErik.Nordmark@Sun.COM cred_t *cr; 12518778SErik.Nordmark@Sun.COM pid_t cpid; 12528778SErik.Nordmark@Sun.COM 12538778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 12544307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 12558778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp), 12568778SErik.Nordmark@Sun.COM active, 0, cpid); 12574307Spwernau } 12580Sstevel@tonic-gate return; 12590Sstevel@tonic-gate fail: 12603055Sdanmcd if (itp != NULL) 12613055Sdanmcd mutex_exit(&itp->itp_lock); 12623055Sdanmcd spdsock_error(q, mp, err, diag); 12634307Spwernau if (audit_active) { 12644307Spwernau boolean_t active; 12654307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 12668778SErik.Nordmark@Sun.COM cred_t *cr; 12678778SErik.Nordmark@Sun.COM pid_t cpid; 12688778SErik.Nordmark@Sun.COM 12698778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 12704307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 12718778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp), 12728778SErik.Nordmark@Sun.COM active, err, cpid); 12734307Spwernau } 12743055Sdanmcd } 12753055Sdanmcd 12763055Sdanmcd /* Do NOT consume a reference to itp. */ 12773055Sdanmcd /* ARGSUSED */ 12783055Sdanmcd static void 12793448Sdh155122 spdsock_flip_node(ipsec_tun_pol_t *itp, void *ignoreme, netstack_t *ns) 12803055Sdanmcd { 12813055Sdanmcd mutex_enter(&itp->itp_lock); 12823055Sdanmcd ITPF_SWAP(itp->itp_flags); 12833448Sdh155122 ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns); 12843055Sdanmcd mutex_exit(&itp->itp_lock); 12850Sstevel@tonic-gate } 12860Sstevel@tonic-gate 12870Sstevel@tonic-gate void 12883055Sdanmcd spdsock_flip(queue_t *q, mblk_t *mp, spd_if_t *tunname) 12890Sstevel@tonic-gate { 12903055Sdanmcd char *tname; 12913055Sdanmcd ipsec_tun_pol_t *itp; 12923448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 12936430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 12943055Sdanmcd 12953055Sdanmcd if (tunname != NULL) { 12963055Sdanmcd tname = (char *)tunname->spd_if_name; 12973055Sdanmcd if (*tname == '\0') { 12983448Sdh155122 /* can't fail */ 12996430Sdanmcd ipsec_swap_global_policy(ns); 13004307Spwernau if (audit_active) { 13014307Spwernau boolean_t active; 13024307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 13038778SErik.Nordmark@Sun.COM cred_t *cr; 13048778SErik.Nordmark@Sun.COM pid_t cpid; 13058778SErik.Nordmark@Sun.COM 13068778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 13074307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 13088778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLIP, cr, ns, 13098778SErik.Nordmark@Sun.COM NULL, active, 0, cpid); 13104307Spwernau } 13116430Sdanmcd itp_walk(spdsock_flip_node, NULL, ns); 13124307Spwernau if (audit_active) { 13134307Spwernau boolean_t active; 13144307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 13158778SErik.Nordmark@Sun.COM cred_t *cr; 13168778SErik.Nordmark@Sun.COM pid_t cpid; 13178778SErik.Nordmark@Sun.COM 13188778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 13194307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 13208778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLIP, cr, ns, 13218778SErik.Nordmark@Sun.COM "all tunnels", active, 0, cpid); 13224307Spwernau } 13233055Sdanmcd } else { 13246430Sdanmcd itp = get_tunnel_policy(tname, ns); 13253055Sdanmcd if (itp == NULL) { 13263055Sdanmcd /* Better idea for "tunnel not found"? */ 13273055Sdanmcd spdsock_error(q, mp, ESRCH, 0); 13284307Spwernau if (audit_active) { 13294307Spwernau boolean_t active; 13304307Spwernau spd_msg_t *spmsg = 13314307Spwernau (spd_msg_t *)mp->b_rptr; 13328778SErik.Nordmark@Sun.COM cred_t *cr; 13338778SErik.Nordmark@Sun.COM pid_t cpid; 13348778SErik.Nordmark@Sun.COM 13358778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 13364307Spwernau active = (spmsg->spd_msg_spdid == 13374307Spwernau SPD_ACTIVE); 13388778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLIP, cr, ns, 13398778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, 13408778SErik.Nordmark@Sun.COM ESRCH, cpid); 13414307Spwernau } 13423055Sdanmcd return; 13433055Sdanmcd } 13443448Sdh155122 spdsock_flip_node(itp, NULL, NULL); 13454307Spwernau if (audit_active) { 13464307Spwernau boolean_t active; 13474307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 13488778SErik.Nordmark@Sun.COM cred_t *cr; 13498778SErik.Nordmark@Sun.COM pid_t cpid; 13508778SErik.Nordmark@Sun.COM 13518778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 13524307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 13538778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLIP, cr, ns, 13548778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, 0, cpid); 13554307Spwernau } 13566430Sdanmcd ITP_REFRELE(itp, ns); 13573055Sdanmcd } 13583055Sdanmcd } else { 13596430Sdanmcd ipsec_swap_global_policy(ns); /* can't fail */ 13604307Spwernau if (audit_active) { 13614307Spwernau boolean_t active; 13624307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 13638778SErik.Nordmark@Sun.COM cred_t *cr; 13648778SErik.Nordmark@Sun.COM pid_t cpid; 13658778SErik.Nordmark@Sun.COM 13668778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 13674307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 13688778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_FLIP, cr, 13698778SErik.Nordmark@Sun.COM ns, NULL, active, 0, cpid); 13704307Spwernau } 13713055Sdanmcd } 13720Sstevel@tonic-gate spd_echo(q, mp); 13730Sstevel@tonic-gate } 13740Sstevel@tonic-gate 13750Sstevel@tonic-gate /* 13760Sstevel@tonic-gate * Unimplemented feature 13770Sstevel@tonic-gate */ 13780Sstevel@tonic-gate /* ARGSUSED */ 13790Sstevel@tonic-gate static void 13803055Sdanmcd spdsock_lookup(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 13813055Sdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp) 13820Sstevel@tonic-gate { 13830Sstevel@tonic-gate spdsock_error(q, mp, EINVAL, 0); 13840Sstevel@tonic-gate } 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate static mblk_t * 13880Sstevel@tonic-gate spdsock_dump_ruleset(mblk_t *req, ipsec_policy_head_t *iph, 13890Sstevel@tonic-gate uint32_t count, uint16_t error) 13900Sstevel@tonic-gate { 13910Sstevel@tonic-gate size_t len = sizeof (spd_ruleset_ext_t) + sizeof (spd_msg_t); 13920Sstevel@tonic-gate spd_msg_t *msg; 13930Sstevel@tonic-gate spd_ruleset_ext_t *ruleset; 13940Sstevel@tonic-gate mblk_t *m = allocb(len, BPRI_HI); 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate ASSERT(RW_READ_HELD(&iph->iph_lock)); 13970Sstevel@tonic-gate 13980Sstevel@tonic-gate if (m == NULL) { 13990Sstevel@tonic-gate return (NULL); 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate msg = (spd_msg_t *)m->b_rptr; 14020Sstevel@tonic-gate ruleset = (spd_ruleset_ext_t *)(&msg[1]); 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate m->b_wptr = (uint8_t *)&ruleset[1]; 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate *msg = *(spd_msg_t *)(req->b_rptr); 14070Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(len); 14080Sstevel@tonic-gate msg->spd_msg_errno = error; 14090Sstevel@tonic-gate 14100Sstevel@tonic-gate ruleset->spd_ruleset_len = SPD_8TO64(sizeof (*ruleset)); 14110Sstevel@tonic-gate ruleset->spd_ruleset_type = SPD_EXT_RULESET; 14120Sstevel@tonic-gate ruleset->spd_ruleset_count = count; 14130Sstevel@tonic-gate ruleset->spd_ruleset_version = iph->iph_gen; 14140Sstevel@tonic-gate return (m); 14150Sstevel@tonic-gate } 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate static mblk_t * 14180Sstevel@tonic-gate spdsock_dump_finish(spdsock_t *ss, int error) 14190Sstevel@tonic-gate { 14200Sstevel@tonic-gate mblk_t *m; 14210Sstevel@tonic-gate ipsec_policy_head_t *iph = ss->spdsock_dump_head; 14220Sstevel@tonic-gate mblk_t *req = ss->spdsock_dump_req; 142310592Sdanmcd@sun.com netstack_t *ns = ss->spdsock_spds->spds_netstack; 14240Sstevel@tonic-gate 14250Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 14260Sstevel@tonic-gate m = spdsock_dump_ruleset(req, iph, ss->spdsock_dump_count, error); 14270Sstevel@tonic-gate rw_exit(&iph->iph_lock); 142810592Sdanmcd@sun.com IPPH_REFRELE(iph, ns); 142910592Sdanmcd@sun.com if (ss->spdsock_itp != NULL) { 143010592Sdanmcd@sun.com ITP_REFRELE(ss->spdsock_itp, ns); 143110592Sdanmcd@sun.com ss->spdsock_itp = NULL; 143210592Sdanmcd@sun.com } 14330Sstevel@tonic-gate ss->spdsock_dump_req = NULL; 14340Sstevel@tonic-gate freemsg(req); 14350Sstevel@tonic-gate 14360Sstevel@tonic-gate return (m); 14370Sstevel@tonic-gate } 14380Sstevel@tonic-gate 14390Sstevel@tonic-gate /* 14400Sstevel@tonic-gate * Rule encoding functions. 14410Sstevel@tonic-gate * We do a two-pass encode. 14420Sstevel@tonic-gate * If base != NULL, fill in encoded rule part starting at base+offset. 14430Sstevel@tonic-gate * Always return "offset" plus length of to-be-encoded data. 14440Sstevel@tonic-gate */ 14450Sstevel@tonic-gate static uint_t 14460Sstevel@tonic-gate spdsock_encode_typecode(uint8_t *base, uint_t offset, uint8_t type, 14470Sstevel@tonic-gate uint8_t type_end, uint8_t code, uint8_t code_end) 14480Sstevel@tonic-gate { 14490Sstevel@tonic-gate struct spd_typecode *tcp; 14500Sstevel@tonic-gate 14510Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate if (base != NULL) { 14540Sstevel@tonic-gate tcp = (struct spd_typecode *)(base + offset); 14550Sstevel@tonic-gate tcp->spd_typecode_len = SPD_8TO64(sizeof (*tcp)); 14560Sstevel@tonic-gate tcp->spd_typecode_exttype = SPD_EXT_ICMP_TYPECODE; 14570Sstevel@tonic-gate tcp->spd_typecode_code = code; 14580Sstevel@tonic-gate tcp->spd_typecode_type = type; 14590Sstevel@tonic-gate tcp->spd_typecode_type_end = type_end; 14600Sstevel@tonic-gate tcp->spd_typecode_code_end = code_end; 14610Sstevel@tonic-gate } 14620Sstevel@tonic-gate offset += sizeof (*tcp); 14630Sstevel@tonic-gate 14640Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate return (offset); 14670Sstevel@tonic-gate } 14680Sstevel@tonic-gate 14690Sstevel@tonic-gate static uint_t 14700Sstevel@tonic-gate spdsock_encode_proto(uint8_t *base, uint_t offset, uint8_t proto) 14710Sstevel@tonic-gate { 14720Sstevel@tonic-gate struct spd_proto *spp; 14730Sstevel@tonic-gate 14740Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14750Sstevel@tonic-gate 14760Sstevel@tonic-gate if (base != NULL) { 14770Sstevel@tonic-gate spp = (struct spd_proto *)(base + offset); 14780Sstevel@tonic-gate spp->spd_proto_len = SPD_8TO64(sizeof (*spp)); 14790Sstevel@tonic-gate spp->spd_proto_exttype = SPD_EXT_PROTO; 14800Sstevel@tonic-gate spp->spd_proto_number = proto; 14810Sstevel@tonic-gate spp->spd_proto_reserved1 = 0; 14820Sstevel@tonic-gate spp->spd_proto_reserved2 = 0; 14830Sstevel@tonic-gate } 14840Sstevel@tonic-gate offset += sizeof (*spp); 14850Sstevel@tonic-gate 14860Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14870Sstevel@tonic-gate 14880Sstevel@tonic-gate return (offset); 14890Sstevel@tonic-gate } 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate static uint_t 14920Sstevel@tonic-gate spdsock_encode_port(uint8_t *base, uint_t offset, uint16_t ext, uint16_t port) 14930Sstevel@tonic-gate { 14940Sstevel@tonic-gate struct spd_portrange *spp; 14950Sstevel@tonic-gate 14960Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 14970Sstevel@tonic-gate 14980Sstevel@tonic-gate if (base != NULL) { 14990Sstevel@tonic-gate spp = (struct spd_portrange *)(base + offset); 15000Sstevel@tonic-gate spp->spd_ports_len = SPD_8TO64(sizeof (*spp)); 15010Sstevel@tonic-gate spp->spd_ports_exttype = ext; 15020Sstevel@tonic-gate spp->spd_ports_minport = port; 15030Sstevel@tonic-gate spp->spd_ports_maxport = port; 15040Sstevel@tonic-gate } 15050Sstevel@tonic-gate offset += sizeof (*spp); 15060Sstevel@tonic-gate 15070Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate return (offset); 15100Sstevel@tonic-gate } 15110Sstevel@tonic-gate 15120Sstevel@tonic-gate static uint_t 15130Sstevel@tonic-gate spdsock_encode_addr(uint8_t *base, uint_t offset, uint16_t ext, 15140Sstevel@tonic-gate const ipsec_selkey_t *sel, const ipsec_addr_t *addr, uint_t pfxlen) 15150Sstevel@tonic-gate { 15160Sstevel@tonic-gate struct spd_address *sae; 15170Sstevel@tonic-gate ipsec_addr_t *spdaddr; 15180Sstevel@tonic-gate uint_t start = offset; 15190Sstevel@tonic-gate uint_t addrlen; 15200Sstevel@tonic-gate uint_t af; 15210Sstevel@tonic-gate 15220Sstevel@tonic-gate if (sel->ipsl_valid & IPSL_IPV4) { 15230Sstevel@tonic-gate af = AF_INET; 15240Sstevel@tonic-gate addrlen = IP_ADDR_LEN; 15250Sstevel@tonic-gate } else { 15260Sstevel@tonic-gate af = AF_INET6; 15270Sstevel@tonic-gate addrlen = IPV6_ADDR_LEN; 15280Sstevel@tonic-gate } 15290Sstevel@tonic-gate 15300Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate if (base != NULL) { 15330Sstevel@tonic-gate sae = (struct spd_address *)(base + offset); 15340Sstevel@tonic-gate sae->spd_address_exttype = ext; 15350Sstevel@tonic-gate sae->spd_address_af = af; 15360Sstevel@tonic-gate sae->spd_address_prefixlen = pfxlen; 15370Sstevel@tonic-gate sae->spd_address_reserved2 = 0; 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate spdaddr = (ipsec_addr_t *)(&sae[1]); 15400Sstevel@tonic-gate bcopy(addr, spdaddr, addrlen); 15410Sstevel@tonic-gate } 15420Sstevel@tonic-gate offset += sizeof (*sae); 15430Sstevel@tonic-gate addrlen = roundup(addrlen, sizeof (uint64_t)); 15440Sstevel@tonic-gate offset += addrlen; 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15470Sstevel@tonic-gate 15480Sstevel@tonic-gate if (base != NULL) 15490Sstevel@tonic-gate sae->spd_address_len = SPD_8TO64(offset - start); 15500Sstevel@tonic-gate return (offset); 15510Sstevel@tonic-gate } 15520Sstevel@tonic-gate 15530Sstevel@tonic-gate static uint_t 15540Sstevel@tonic-gate spdsock_encode_sel(uint8_t *base, uint_t offset, const ipsec_sel_t *sel) 15550Sstevel@tonic-gate { 15560Sstevel@tonic-gate const ipsec_selkey_t *selkey = &sel->ipsl_key; 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_PROTOCOL) 15590Sstevel@tonic-gate offset = spdsock_encode_proto(base, offset, selkey->ipsl_proto); 15600Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_LOCAL_PORT) 15610Sstevel@tonic-gate offset = spdsock_encode_port(base, offset, SPD_EXT_LCLPORT, 15620Sstevel@tonic-gate selkey->ipsl_lport); 15630Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_REMOTE_PORT) 15640Sstevel@tonic-gate offset = spdsock_encode_port(base, offset, SPD_EXT_REMPORT, 15650Sstevel@tonic-gate selkey->ipsl_rport); 15660Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_REMOTE_ADDR) 15670Sstevel@tonic-gate offset = spdsock_encode_addr(base, offset, SPD_EXT_REMADDR, 15680Sstevel@tonic-gate selkey, &selkey->ipsl_remote, selkey->ipsl_remote_pfxlen); 15690Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_LOCAL_ADDR) 15700Sstevel@tonic-gate offset = spdsock_encode_addr(base, offset, SPD_EXT_LCLADDR, 15710Sstevel@tonic-gate selkey, &selkey->ipsl_local, selkey->ipsl_local_pfxlen); 15720Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_ICMP_TYPE) { 15730Sstevel@tonic-gate offset = spdsock_encode_typecode(base, offset, 15740Sstevel@tonic-gate selkey->ipsl_icmp_type, selkey->ipsl_icmp_type_end, 15750Sstevel@tonic-gate (selkey->ipsl_valid & IPSL_ICMP_CODE) ? 15765240Snordmark selkey->ipsl_icmp_code : 255, 15770Sstevel@tonic-gate (selkey->ipsl_valid & IPSL_ICMP_CODE) ? 15785240Snordmark selkey->ipsl_icmp_code_end : 255); 15790Sstevel@tonic-gate } 15800Sstevel@tonic-gate return (offset); 15810Sstevel@tonic-gate } 15820Sstevel@tonic-gate 15830Sstevel@tonic-gate static uint_t 15840Sstevel@tonic-gate spdsock_encode_actattr(uint8_t *base, uint_t offset, uint32_t tag, 15850Sstevel@tonic-gate uint32_t value) 15860Sstevel@tonic-gate { 15870Sstevel@tonic-gate struct spd_attribute *attr; 15880Sstevel@tonic-gate 15890Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15900Sstevel@tonic-gate 15910Sstevel@tonic-gate if (base != NULL) { 15920Sstevel@tonic-gate attr = (struct spd_attribute *)(base + offset); 15930Sstevel@tonic-gate attr->spd_attr_tag = tag; 15940Sstevel@tonic-gate attr->spd_attr_value = value; 15950Sstevel@tonic-gate } 15960Sstevel@tonic-gate offset += sizeof (struct spd_attribute); 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate return (offset); 16010Sstevel@tonic-gate } 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate 16040Sstevel@tonic-gate #define EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v)) 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate static uint_t 16070Sstevel@tonic-gate spdsock_encode_action(uint8_t *base, uint_t offset, const ipsec_action_t *ap) 16080Sstevel@tonic-gate { 16090Sstevel@tonic-gate const struct ipsec_act *act = &(ap->ipa_act); 16100Sstevel@tonic-gate uint_t flags; 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate EMIT(SPD_ATTR_EMPTY, 0); 16130Sstevel@tonic-gate switch (act->ipa_type) { 16140Sstevel@tonic-gate case IPSEC_ACT_DISCARD: 16150Sstevel@tonic-gate case IPSEC_ACT_REJECT: 16160Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_DROP); 16170Sstevel@tonic-gate break; 16180Sstevel@tonic-gate case IPSEC_ACT_BYPASS: 16190Sstevel@tonic-gate case IPSEC_ACT_CLEAR: 16200Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_PASS); 16210Sstevel@tonic-gate break; 16220Sstevel@tonic-gate 16230Sstevel@tonic-gate case IPSEC_ACT_APPLY: 16240Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_IPSEC); 16250Sstevel@tonic-gate flags = 0; 16260Sstevel@tonic-gate if (act->ipa_apply.ipp_use_ah) 16270Sstevel@tonic-gate flags |= SPD_APPLY_AH; 16280Sstevel@tonic-gate if (act->ipa_apply.ipp_use_esp) 16290Sstevel@tonic-gate flags |= SPD_APPLY_ESP; 16300Sstevel@tonic-gate if (act->ipa_apply.ipp_use_espa) 16310Sstevel@tonic-gate flags |= SPD_APPLY_ESPA; 16320Sstevel@tonic-gate if (act->ipa_apply.ipp_use_se) 16330Sstevel@tonic-gate flags |= SPD_APPLY_SE; 16340Sstevel@tonic-gate if (act->ipa_apply.ipp_use_unique) 16350Sstevel@tonic-gate flags |= SPD_APPLY_UNIQUE; 16360Sstevel@tonic-gate EMIT(SPD_ATTR_FLAGS, flags); 16370Sstevel@tonic-gate if (flags & SPD_APPLY_AH) { 16380Sstevel@tonic-gate EMIT(SPD_ATTR_AH_AUTH, act->ipa_apply.ipp_auth_alg); 16390Sstevel@tonic-gate EMIT(SPD_ATTR_AH_MINBITS, 16400Sstevel@tonic-gate act->ipa_apply.ipp_ah_minbits); 16410Sstevel@tonic-gate EMIT(SPD_ATTR_AH_MAXBITS, 16420Sstevel@tonic-gate act->ipa_apply.ipp_ah_maxbits); 16430Sstevel@tonic-gate } 16440Sstevel@tonic-gate if (flags & SPD_APPLY_ESP) { 16450Sstevel@tonic-gate EMIT(SPD_ATTR_ESP_ENCR, act->ipa_apply.ipp_encr_alg); 16460Sstevel@tonic-gate EMIT(SPD_ATTR_ENCR_MINBITS, 16470Sstevel@tonic-gate act->ipa_apply.ipp_espe_minbits); 16480Sstevel@tonic-gate EMIT(SPD_ATTR_ENCR_MAXBITS, 16490Sstevel@tonic-gate act->ipa_apply.ipp_espe_maxbits); 16500Sstevel@tonic-gate if (flags & SPD_APPLY_ESPA) { 16510Sstevel@tonic-gate EMIT(SPD_ATTR_ESP_AUTH, 16520Sstevel@tonic-gate act->ipa_apply.ipp_esp_auth_alg); 16530Sstevel@tonic-gate EMIT(SPD_ATTR_ESPA_MINBITS, 16540Sstevel@tonic-gate act->ipa_apply.ipp_espa_minbits); 16550Sstevel@tonic-gate EMIT(SPD_ATTR_ESPA_MAXBITS, 16560Sstevel@tonic-gate act->ipa_apply.ipp_espa_maxbits); 16570Sstevel@tonic-gate } 16580Sstevel@tonic-gate } 16590Sstevel@tonic-gate if (act->ipa_apply.ipp_km_proto != 0) 16600Sstevel@tonic-gate EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_proto); 16610Sstevel@tonic-gate if (act->ipa_apply.ipp_km_cookie != 0) 16620Sstevel@tonic-gate EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_cookie); 16630Sstevel@tonic-gate if (act->ipa_apply.ipp_replay_depth != 0) 16640Sstevel@tonic-gate EMIT(SPD_ATTR_REPLAY_DEPTH, 16650Sstevel@tonic-gate act->ipa_apply.ipp_replay_depth); 16660Sstevel@tonic-gate /* Add more here */ 16670Sstevel@tonic-gate break; 16680Sstevel@tonic-gate } 16690Sstevel@tonic-gate 16700Sstevel@tonic-gate return (offset); 16710Sstevel@tonic-gate } 16720Sstevel@tonic-gate 16730Sstevel@tonic-gate static uint_t 16740Sstevel@tonic-gate spdsock_encode_action_list(uint8_t *base, uint_t offset, 16750Sstevel@tonic-gate const ipsec_action_t *ap) 16760Sstevel@tonic-gate { 16770Sstevel@tonic-gate struct spd_ext_actions *act; 16780Sstevel@tonic-gate uint_t nact = 0; 16790Sstevel@tonic-gate uint_t start = offset; 16800Sstevel@tonic-gate 16810Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 16820Sstevel@tonic-gate 16830Sstevel@tonic-gate if (base != NULL) { 16840Sstevel@tonic-gate act = (struct spd_ext_actions *)(base + offset); 16850Sstevel@tonic-gate act->spd_actions_len = 0; 16860Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 16870Sstevel@tonic-gate act->spd_actions_count = 0; 16880Sstevel@tonic-gate act->spd_actions_reserved = 0; 16890Sstevel@tonic-gate } 16900Sstevel@tonic-gate 16910Sstevel@tonic-gate offset += sizeof (*act); 16920Sstevel@tonic-gate 16930Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate while (ap != NULL) { 16960Sstevel@tonic-gate offset = spdsock_encode_action(base, offset, ap); 16970Sstevel@tonic-gate ap = ap->ipa_next; 16980Sstevel@tonic-gate nact++; 16990Sstevel@tonic-gate if (ap != NULL) { 17000Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); 17010Sstevel@tonic-gate } 17020Sstevel@tonic-gate } 17030Sstevel@tonic-gate EMIT(SPD_ATTR_END, 0); 17040Sstevel@tonic-gate 17050Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate if (base != NULL) { 17080Sstevel@tonic-gate act->spd_actions_count = nact; 17090Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(offset - start); 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate return (offset); 17130Sstevel@tonic-gate } 17140Sstevel@tonic-gate 17150Sstevel@tonic-gate #undef EMIT 17160Sstevel@tonic-gate 17170Sstevel@tonic-gate /* ARGSUSED */ 17180Sstevel@tonic-gate static uint_t 17190Sstevel@tonic-gate spdsock_rule_flags(uint_t dir, uint_t af) 17200Sstevel@tonic-gate { 17210Sstevel@tonic-gate uint_t flags = 0; 17220Sstevel@tonic-gate 17230Sstevel@tonic-gate if (dir == IPSEC_TYPE_INBOUND) 17240Sstevel@tonic-gate flags |= SPD_RULE_FLAG_INBOUND; 17250Sstevel@tonic-gate if (dir == IPSEC_TYPE_OUTBOUND) 17260Sstevel@tonic-gate flags |= SPD_RULE_FLAG_OUTBOUND; 17270Sstevel@tonic-gate 17280Sstevel@tonic-gate return (flags); 17290Sstevel@tonic-gate } 17300Sstevel@tonic-gate 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate static uint_t 17333055Sdanmcd spdsock_encode_rule_head(uint8_t *base, uint_t offset, spd_msg_t *req, 17343055Sdanmcd const ipsec_policy_t *rule, uint_t dir, uint_t af, char *name, 17353055Sdanmcd boolean_t tunnel) 17360Sstevel@tonic-gate { 17370Sstevel@tonic-gate struct spd_msg *spmsg; 17380Sstevel@tonic-gate struct spd_rule *spr; 17393055Sdanmcd spd_if_t *sid; 17400Sstevel@tonic-gate 17410Sstevel@tonic-gate uint_t start = offset; 17420Sstevel@tonic-gate 17430Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17440Sstevel@tonic-gate 17450Sstevel@tonic-gate if (base != NULL) { 17460Sstevel@tonic-gate spmsg = (struct spd_msg *)(base + offset); 17470Sstevel@tonic-gate bzero(spmsg, sizeof (*spmsg)); 17480Sstevel@tonic-gate spmsg->spd_msg_version = PF_POLICY_V1; 17490Sstevel@tonic-gate spmsg->spd_msg_type = SPD_DUMP; 17500Sstevel@tonic-gate spmsg->spd_msg_seq = req->spd_msg_seq; 17510Sstevel@tonic-gate spmsg->spd_msg_pid = req->spd_msg_pid; 17520Sstevel@tonic-gate } 17530Sstevel@tonic-gate offset += sizeof (struct spd_msg); 17540Sstevel@tonic-gate 17550Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17560Sstevel@tonic-gate 17570Sstevel@tonic-gate if (base != NULL) { 17580Sstevel@tonic-gate spr = (struct spd_rule *)(base + offset); 17590Sstevel@tonic-gate spr->spd_rule_type = SPD_EXT_RULE; 17600Sstevel@tonic-gate spr->spd_rule_priority = rule->ipsp_prio; 17610Sstevel@tonic-gate spr->spd_rule_flags = spdsock_rule_flags(dir, af); 17623055Sdanmcd if (tunnel) 17633055Sdanmcd spr->spd_rule_flags |= SPD_RULE_FLAG_TUNNEL; 17640Sstevel@tonic-gate spr->spd_rule_unused = 0; 17650Sstevel@tonic-gate spr->spd_rule_len = SPD_8TO64(sizeof (*spr)); 17660Sstevel@tonic-gate spr->spd_rule_index = rule->ipsp_index; 17670Sstevel@tonic-gate } 17680Sstevel@tonic-gate offset += sizeof (struct spd_rule); 17693055Sdanmcd 17703055Sdanmcd /* 17713055Sdanmcd * If we have an interface name (i.e. if this policy head came from 17723055Sdanmcd * a tunnel), add the SPD_EXT_TUN_NAME extension. 17733055Sdanmcd */ 17746466Sdanmcd if (name != NULL) { 17753055Sdanmcd 17763055Sdanmcd ASSERT(ALIGNED64(offset)); 17773055Sdanmcd 17783055Sdanmcd if (base != NULL) { 17793055Sdanmcd sid = (spd_if_t *)(base + offset); 17803055Sdanmcd sid->spd_if_exttype = SPD_EXT_TUN_NAME; 17813055Sdanmcd sid->spd_if_len = SPD_8TO64(sizeof (spd_if_t) + 17823055Sdanmcd roundup((strlen(name) - 4), 8)); 17833055Sdanmcd (void) strlcpy((char *)sid->spd_if_name, name, 17843055Sdanmcd LIFNAMSIZ); 17853055Sdanmcd } 17863055Sdanmcd 17873055Sdanmcd offset += sizeof (spd_if_t) + roundup((strlen(name) - 4), 8); 17883055Sdanmcd } 17893055Sdanmcd 17900Sstevel@tonic-gate offset = spdsock_encode_sel(base, offset, rule->ipsp_sel); 17910Sstevel@tonic-gate offset = spdsock_encode_action_list(base, offset, rule->ipsp_act); 17920Sstevel@tonic-gate 17930Sstevel@tonic-gate ASSERT(ALIGNED64(offset)); 17940Sstevel@tonic-gate 17950Sstevel@tonic-gate if (base != NULL) { 17960Sstevel@tonic-gate spmsg->spd_msg_len = SPD_8TO64(offset - start); 17970Sstevel@tonic-gate } 17980Sstevel@tonic-gate return (offset); 17990Sstevel@tonic-gate } 18000Sstevel@tonic-gate 18010Sstevel@tonic-gate /* ARGSUSED */ 18020Sstevel@tonic-gate static mblk_t * 18030Sstevel@tonic-gate spdsock_encode_rule(mblk_t *req, const ipsec_policy_t *rule, 18043055Sdanmcd uint_t dir, uint_t af, char *name, boolean_t tunnel) 18050Sstevel@tonic-gate { 18060Sstevel@tonic-gate mblk_t *m; 18070Sstevel@tonic-gate uint_t len; 18080Sstevel@tonic-gate spd_msg_t *mreq = (spd_msg_t *)req->b_rptr; 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate /* 18110Sstevel@tonic-gate * Figure out how much space we'll need. 18120Sstevel@tonic-gate */ 18133055Sdanmcd len = spdsock_encode_rule_head(NULL, 0, mreq, rule, dir, af, name, 18143055Sdanmcd tunnel); 18150Sstevel@tonic-gate 18160Sstevel@tonic-gate /* 18170Sstevel@tonic-gate * Allocate mblk. 18180Sstevel@tonic-gate */ 18190Sstevel@tonic-gate m = allocb(len, BPRI_HI); 18200Sstevel@tonic-gate if (m == NULL) 18210Sstevel@tonic-gate return (NULL); 18220Sstevel@tonic-gate 18230Sstevel@tonic-gate /* 18240Sstevel@tonic-gate * Fill it in.. 18250Sstevel@tonic-gate */ 18260Sstevel@tonic-gate m->b_wptr = m->b_rptr + len; 18270Sstevel@tonic-gate bzero(m->b_rptr, len); 18283055Sdanmcd (void) spdsock_encode_rule_head(m->b_rptr, 0, mreq, rule, dir, af, 18293055Sdanmcd name, tunnel); 18300Sstevel@tonic-gate return (m); 18310Sstevel@tonic-gate } 18320Sstevel@tonic-gate 18330Sstevel@tonic-gate static ipsec_policy_t * 183481Ssommerfe spdsock_dump_next_in_chain(spdsock_t *ss, ipsec_policy_head_t *iph, 183581Ssommerfe ipsec_policy_t *cur) 183681Ssommerfe { 183781Ssommerfe ASSERT(RW_READ_HELD(&iph->iph_lock)); 183881Ssommerfe 183981Ssommerfe ss->spdsock_dump_count++; 184081Ssommerfe ss->spdsock_dump_cur_rule = cur->ipsp_hash.hash_next; 184181Ssommerfe return (cur); 184281Ssommerfe } 184381Ssommerfe 184481Ssommerfe static ipsec_policy_t * 18450Sstevel@tonic-gate spdsock_dump_next_rule(spdsock_t *ss, ipsec_policy_head_t *iph) 18460Sstevel@tonic-gate { 18470Sstevel@tonic-gate ipsec_policy_t *cur; 184881Ssommerfe ipsec_policy_root_t *ipr; 184981Ssommerfe int chain, nchains, type, af; 18500Sstevel@tonic-gate 18510Sstevel@tonic-gate ASSERT(RW_READ_HELD(&iph->iph_lock)); 18520Sstevel@tonic-gate 18530Sstevel@tonic-gate cur = ss->spdsock_dump_cur_rule; 18540Sstevel@tonic-gate 185581Ssommerfe if (cur != NULL) 185681Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 185781Ssommerfe 185881Ssommerfe type = ss->spdsock_dump_cur_type; 185981Ssommerfe 186081Ssommerfe next: 186181Ssommerfe chain = ss->spdsock_dump_cur_chain; 186281Ssommerfe ipr = &iph->iph_root[type]; 186381Ssommerfe nchains = ipr->ipr_nchains; 186481Ssommerfe 186581Ssommerfe while (chain < nchains) { 186681Ssommerfe cur = ipr->ipr_hash[chain].hash_head; 186781Ssommerfe chain++; 186881Ssommerfe if (cur != NULL) { 186981Ssommerfe ss->spdsock_dump_cur_chain = chain; 187081Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 187181Ssommerfe } 18720Sstevel@tonic-gate } 187381Ssommerfe ss->spdsock_dump_cur_chain = nchains; 187481Ssommerfe 187581Ssommerfe af = ss->spdsock_dump_cur_af; 187681Ssommerfe while (af < IPSEC_NAF) { 187781Ssommerfe cur = ipr->ipr_nonhash[af]; 187881Ssommerfe af++; 187981Ssommerfe if (cur != NULL) { 188081Ssommerfe ss->spdsock_dump_cur_af = af; 188181Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur)); 188281Ssommerfe } 188381Ssommerfe } 188481Ssommerfe 188581Ssommerfe type++; 188681Ssommerfe if (type >= IPSEC_NTYPES) 188781Ssommerfe return (NULL); 188881Ssommerfe 188981Ssommerfe ss->spdsock_dump_cur_chain = 0; 189081Ssommerfe ss->spdsock_dump_cur_type = type; 189181Ssommerfe ss->spdsock_dump_cur_af = IPSEC_AF_V4; 189281Ssommerfe goto next; 189381Ssommerfe 18940Sstevel@tonic-gate } 18950Sstevel@tonic-gate 18966430Sdanmcd /* 18976430Sdanmcd * If we're done with one policy head, but have more to go, we iterate through 18986430Sdanmcd * another IPsec tunnel policy head (itp). Return NULL if it is an error 18996430Sdanmcd * worthy of returning EAGAIN via PF_POLICY. 19006430Sdanmcd */ 19016430Sdanmcd static ipsec_tun_pol_t * 19026430Sdanmcd spdsock_dump_iterate_next_tunnel(spdsock_t *ss, ipsec_stack_t *ipss) 19036430Sdanmcd { 19046430Sdanmcd ipsec_tun_pol_t *itp; 19056430Sdanmcd 19066430Sdanmcd ASSERT(RW_READ_HELD(&ipss->ipsec_tunnel_policy_lock)); 19076430Sdanmcd if (ipss->ipsec_tunnel_policy_gen > ss->spdsock_dump_tun_gen) { 19086430Sdanmcd /* Oops, state of the tunnel polheads changed. */ 19096430Sdanmcd itp = NULL; 19106430Sdanmcd } else if (ss->spdsock_itp == NULL) { 19116430Sdanmcd /* Just finished global, find first node. */ 19126430Sdanmcd itp = avl_first(&ipss->ipsec_tunnel_policies); 19136430Sdanmcd } else { 19146430Sdanmcd /* We just finished current polhead, find the next one. */ 19156430Sdanmcd itp = AVL_NEXT(&ipss->ipsec_tunnel_policies, ss->spdsock_itp); 19166430Sdanmcd } 19176430Sdanmcd if (itp != NULL) { 19186430Sdanmcd ITP_REFHOLD(itp); 19196430Sdanmcd } 19206430Sdanmcd if (ss->spdsock_itp != NULL) { 19216430Sdanmcd ITP_REFRELE(ss->spdsock_itp, ipss->ipsec_netstack); 19226430Sdanmcd } 19236430Sdanmcd ss->spdsock_itp = itp; 19246430Sdanmcd return (itp); 19256430Sdanmcd } 19266430Sdanmcd 19270Sstevel@tonic-gate static mblk_t * 19280Sstevel@tonic-gate spdsock_dump_next_record(spdsock_t *ss) 19290Sstevel@tonic-gate { 19300Sstevel@tonic-gate ipsec_policy_head_t *iph; 19310Sstevel@tonic-gate ipsec_policy_t *rule; 19320Sstevel@tonic-gate mblk_t *m; 19336430Sdanmcd ipsec_tun_pol_t *itp; 19346430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 19356430Sdanmcd ipsec_stack_t *ipss = ns->netstack_ipsec; 19360Sstevel@tonic-gate 19370Sstevel@tonic-gate iph = ss->spdsock_dump_head; 19380Sstevel@tonic-gate 19390Sstevel@tonic-gate ASSERT(iph != NULL); 19400Sstevel@tonic-gate 19410Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 19420Sstevel@tonic-gate 19430Sstevel@tonic-gate if (iph->iph_gen != ss->spdsock_dump_gen) { 19440Sstevel@tonic-gate rw_exit(&iph->iph_lock); 19450Sstevel@tonic-gate return (spdsock_dump_finish(ss, EAGAIN)); 19460Sstevel@tonic-gate } 19470Sstevel@tonic-gate 19486430Sdanmcd while ((rule = spdsock_dump_next_rule(ss, iph)) == NULL) { 19490Sstevel@tonic-gate rw_exit(&iph->iph_lock); 19506430Sdanmcd if (--(ss->spdsock_dump_remaining_polheads) == 0) 19516430Sdanmcd return (spdsock_dump_finish(ss, 0)); 19526430Sdanmcd 19536430Sdanmcd 19546430Sdanmcd /* 19556430Sdanmcd * If we reach here, we have more policy heads (tunnel 19566430Sdanmcd * entries) to dump. Let's reset to a new policy head 19576430Sdanmcd * and get some more rules. 19586430Sdanmcd * 19596430Sdanmcd * An empty policy head will have spdsock_dump_next_rule() 19606430Sdanmcd * return NULL, and we loop (while dropping the number of 19616430Sdanmcd * remaining polheads). If we loop to 0, we finish. We 19626430Sdanmcd * keep looping until we hit 0 or until we have a rule to 19636430Sdanmcd * encode. 19646430Sdanmcd * 19656430Sdanmcd * NOTE: No need for ITP_REF*() macros here as we're only 19666430Sdanmcd * going after and refholding the policy head itself. 19676430Sdanmcd */ 19686430Sdanmcd rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER); 19696430Sdanmcd itp = spdsock_dump_iterate_next_tunnel(ss, ipss); 19706430Sdanmcd if (itp == NULL) { 19716430Sdanmcd rw_exit(&ipss->ipsec_tunnel_policy_lock); 19726430Sdanmcd return (spdsock_dump_finish(ss, EAGAIN)); 19736430Sdanmcd } 19746430Sdanmcd 19756430Sdanmcd /* Reset other spdsock_dump thingies. */ 19766430Sdanmcd IPPH_REFRELE(ss->spdsock_dump_head, ns); 19776430Sdanmcd if (ss->spdsock_dump_active) { 19786430Sdanmcd ss->spdsock_dump_tunnel = 19796430Sdanmcd itp->itp_flags & ITPF_P_TUNNEL; 19806430Sdanmcd iph = itp->itp_policy; 19816430Sdanmcd } else { 19826430Sdanmcd ss->spdsock_dump_tunnel = 19836430Sdanmcd itp->itp_flags & ITPF_I_TUNNEL; 19846430Sdanmcd iph = itp->itp_inactive; 19856430Sdanmcd } 19866430Sdanmcd IPPH_REFHOLD(iph); 19876430Sdanmcd rw_exit(&ipss->ipsec_tunnel_policy_lock); 19886430Sdanmcd 19896430Sdanmcd rw_enter(&iph->iph_lock, RW_READER); 19906430Sdanmcd RESET_SPDSOCK_DUMP_POLHEAD(ss, iph); 19910Sstevel@tonic-gate } 19920Sstevel@tonic-gate 19936430Sdanmcd m = spdsock_encode_rule(ss->spdsock_dump_req, rule, 19946430Sdanmcd ss->spdsock_dump_cur_type, ss->spdsock_dump_cur_af, 19956466Sdanmcd (ss->spdsock_itp == NULL) ? NULL : ss->spdsock_itp->itp_name, 19966466Sdanmcd ss->spdsock_dump_tunnel); 19970Sstevel@tonic-gate rw_exit(&iph->iph_lock); 19980Sstevel@tonic-gate 19990Sstevel@tonic-gate if (m == NULL) 20000Sstevel@tonic-gate return (spdsock_dump_finish(ss, ENOMEM)); 20010Sstevel@tonic-gate return (m); 20020Sstevel@tonic-gate } 20030Sstevel@tonic-gate 20040Sstevel@tonic-gate /* 20050Sstevel@tonic-gate * Dump records until we run into flow-control back-pressure. 20060Sstevel@tonic-gate */ 20070Sstevel@tonic-gate static void 20080Sstevel@tonic-gate spdsock_dump_some(queue_t *q, spdsock_t *ss) 20090Sstevel@tonic-gate { 20100Sstevel@tonic-gate mblk_t *m, *dataind; 20110Sstevel@tonic-gate 20120Sstevel@tonic-gate while ((ss->spdsock_dump_req != NULL) && canputnext(q)) { 20130Sstevel@tonic-gate m = spdsock_dump_next_record(ss); 20140Sstevel@tonic-gate if (m == NULL) 20150Sstevel@tonic-gate return; 20160Sstevel@tonic-gate dataind = allocb(sizeof (struct T_data_req), BPRI_HI); 20170Sstevel@tonic-gate if (dataind == NULL) { 20180Sstevel@tonic-gate freemsg(m); 20190Sstevel@tonic-gate return; 20200Sstevel@tonic-gate } 20210Sstevel@tonic-gate dataind->b_cont = m; 20220Sstevel@tonic-gate dataind->b_wptr += sizeof (struct T_data_req); 20230Sstevel@tonic-gate ((struct T_data_ind *)dataind->b_rptr)->PRIM_type = T_DATA_IND; 20240Sstevel@tonic-gate ((struct T_data_ind *)dataind->b_rptr)->MORE_flag = 0; 20250Sstevel@tonic-gate dataind->b_datap->db_type = M_PROTO; 20260Sstevel@tonic-gate putnext(q, dataind); 20270Sstevel@tonic-gate } 20280Sstevel@tonic-gate } 20290Sstevel@tonic-gate 20300Sstevel@tonic-gate /* 20310Sstevel@tonic-gate * Start dumping. 20320Sstevel@tonic-gate * Format a start-of-dump record, and set up the stream and kick the rsrv 20330Sstevel@tonic-gate * procedure to continue the job.. 20340Sstevel@tonic-gate */ 20350Sstevel@tonic-gate /* ARGSUSED */ 20360Sstevel@tonic-gate static void 20373055Sdanmcd spdsock_dump(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp) 20380Sstevel@tonic-gate { 20390Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 20406430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 20413448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 20420Sstevel@tonic-gate mblk_t *mr; 20430Sstevel@tonic-gate 20446430Sdanmcd /* spdsock_open() already set spdsock_itp to NULL. */ 20453055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) { 20463448Sdh155122 rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER); 20473055Sdanmcd ss->spdsock_dump_remaining_polheads = 1 + 20483448Sdh155122 avl_numnodes(&ipss->ipsec_tunnel_policies); 20493448Sdh155122 ss->spdsock_dump_tun_gen = ipss->ipsec_tunnel_policy_gen; 20503448Sdh155122 rw_exit(&ipss->ipsec_tunnel_policy_lock); 20513055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS) { 20523448Sdh155122 iph = ipsec_system_policy(ns); 20533055Sdanmcd ss->spdsock_dump_active = B_TRUE; 20543055Sdanmcd } else { 20556430Sdanmcd iph = ipsec_inactive_policy(ns); 20563055Sdanmcd ss->spdsock_dump_active = B_FALSE; 20573055Sdanmcd } 20586430Sdanmcd ASSERT(ss->spdsock_itp == NULL); 20593055Sdanmcd } else { 20603055Sdanmcd ss->spdsock_dump_remaining_polheads = 1; 20613055Sdanmcd } 20623055Sdanmcd 20630Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER); 20640Sstevel@tonic-gate 20650Sstevel@tonic-gate mr = spdsock_dump_ruleset(mp, iph, 0, 0); 20660Sstevel@tonic-gate 20670Sstevel@tonic-gate if (!mr) { 20680Sstevel@tonic-gate rw_exit(&iph->iph_lock); 20690Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 20700Sstevel@tonic-gate return; 20710Sstevel@tonic-gate } 20720Sstevel@tonic-gate 20730Sstevel@tonic-gate ss->spdsock_dump_req = mp; 20746430Sdanmcd RESET_SPDSOCK_DUMP_POLHEAD(ss, iph); 20756430Sdanmcd 20760Sstevel@tonic-gate rw_exit(&iph->iph_lock); 20770Sstevel@tonic-gate 20780Sstevel@tonic-gate qreply(q, mr); 20790Sstevel@tonic-gate qenable(OTHERQ(q)); 20800Sstevel@tonic-gate } 20810Sstevel@tonic-gate 20823055Sdanmcd /* Do NOT consume a reference to ITP. */ 20830Sstevel@tonic-gate void 20843448Sdh155122 spdsock_clone_node(ipsec_tun_pol_t *itp, void *ep, netstack_t *ns) 20853055Sdanmcd { 20863055Sdanmcd int *errptr = (int *)ep; 20873055Sdanmcd 20883055Sdanmcd if (*errptr != 0) 20893055Sdanmcd return; /* We've failed already for some reason. */ 20903055Sdanmcd mutex_enter(&itp->itp_lock); 20913055Sdanmcd ITPF_CLONE(itp->itp_flags); 20923448Sdh155122 *errptr = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns); 20933055Sdanmcd mutex_exit(&itp->itp_lock); 20943055Sdanmcd } 20953055Sdanmcd 20963055Sdanmcd void 20973055Sdanmcd spdsock_clone(queue_t *q, mblk_t *mp, spd_if_t *tunname) 20980Sstevel@tonic-gate { 20993055Sdanmcd int error; 21003055Sdanmcd char *tname; 21013055Sdanmcd ipsec_tun_pol_t *itp; 21023448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 21036430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 21043055Sdanmcd 21053055Sdanmcd if (tunname != NULL) { 21063055Sdanmcd tname = (char *)tunname->spd_if_name; 21073055Sdanmcd if (*tname == '\0') { 21086430Sdanmcd error = ipsec_clone_system_policy(ns); 21094307Spwernau if (audit_active) { 21104307Spwernau boolean_t active; 21114307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 21128778SErik.Nordmark@Sun.COM cred_t *cr; 21138778SErik.Nordmark@Sun.COM pid_t cpid; 21148778SErik.Nordmark@Sun.COM 21158778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 21164307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 21178778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_CLONE, cr, ns, 21188778SErik.Nordmark@Sun.COM NULL, active, error, cpid); 21194307Spwernau } 21204307Spwernau if (error == 0) { 21216430Sdanmcd itp_walk(spdsock_clone_node, &error, ns); 21224307Spwernau if (audit_active) { 21234307Spwernau boolean_t active; 21244307Spwernau spd_msg_t *spmsg = 21254307Spwernau (spd_msg_t *)mp->b_rptr; 21268778SErik.Nordmark@Sun.COM cred_t *cr; 21278778SErik.Nordmark@Sun.COM pid_t cpid; 21288778SErik.Nordmark@Sun.COM 21298778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 21304307Spwernau active = (spmsg->spd_msg_spdid == 21314307Spwernau SPD_ACTIVE); 21328778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_CLONE, cr, 21336430Sdanmcd ns, "all tunnels", active, 0, 21348778SErik.Nordmark@Sun.COM cpid); 21354307Spwernau } 21364307Spwernau } 21373055Sdanmcd } else { 21386430Sdanmcd itp = get_tunnel_policy(tname, ns); 21393055Sdanmcd if (itp == NULL) { 21403055Sdanmcd spdsock_error(q, mp, ENOENT, 0); 21414307Spwernau if (audit_active) { 21424307Spwernau boolean_t active; 21434307Spwernau spd_msg_t *spmsg = 21444307Spwernau (spd_msg_t *)mp->b_rptr; 21458778SErik.Nordmark@Sun.COM cred_t *cr; 21468778SErik.Nordmark@Sun.COM pid_t cpid; 21478778SErik.Nordmark@Sun.COM 21488778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 21494307Spwernau active = (spmsg->spd_msg_spdid == 21504307Spwernau SPD_ACTIVE); 21518778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_CLONE, cr, 215210764Sdanmcd@sun.com ns, NULL, active, ENOENT, cpid); 21534307Spwernau } 21543055Sdanmcd return; 21553055Sdanmcd } 21563448Sdh155122 spdsock_clone_node(itp, &error, NULL); 21574307Spwernau if (audit_active) { 21584307Spwernau boolean_t active; 21594307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 21608778SErik.Nordmark@Sun.COM cred_t *cr; 21618778SErik.Nordmark@Sun.COM pid_t cpid; 21628778SErik.Nordmark@Sun.COM 21638778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 21644307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 21658778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_CLONE, cr, ns, 21668778SErik.Nordmark@Sun.COM ITP_NAME(itp), active, error, cpid); 21674307Spwernau } 216810764Sdanmcd@sun.com ITP_REFRELE(itp, ns); 21693055Sdanmcd } 21703055Sdanmcd } else { 21716430Sdanmcd error = ipsec_clone_system_policy(ns); 21724307Spwernau if (audit_active) { 21734307Spwernau boolean_t active; 21744307Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 21758778SErik.Nordmark@Sun.COM cred_t *cr; 21768778SErik.Nordmark@Sun.COM pid_t cpid; 21778778SErik.Nordmark@Sun.COM 21788778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 21794307Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 21808778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_CLONE, cr, ns, NULL, 21818778SErik.Nordmark@Sun.COM active, error, cpid); 21824307Spwernau } 21833055Sdanmcd } 21843055Sdanmcd 21850Sstevel@tonic-gate if (error != 0) 21860Sstevel@tonic-gate spdsock_error(q, mp, error, 0); 21870Sstevel@tonic-gate else 21880Sstevel@tonic-gate spd_echo(q, mp); 21890Sstevel@tonic-gate } 21900Sstevel@tonic-gate 21910Sstevel@tonic-gate /* 21920Sstevel@tonic-gate * Process a SPD_ALGLIST request. The caller expects separate alg entries 21930Sstevel@tonic-gate * for AH authentication, ESP authentication, and ESP encryption. 21940Sstevel@tonic-gate * The same distinction is then used when setting the min and max key 21950Sstevel@tonic-gate * sizes when defining policies. 21960Sstevel@tonic-gate */ 21970Sstevel@tonic-gate 21980Sstevel@tonic-gate #define SPDSOCK_AH_AUTH 0 21990Sstevel@tonic-gate #define SPDSOCK_ESP_AUTH 1 22000Sstevel@tonic-gate #define SPDSOCK_ESP_ENCR 2 22010Sstevel@tonic-gate #define SPDSOCK_NTYPES 3 22020Sstevel@tonic-gate 22030Sstevel@tonic-gate static const uint_t algattr[SPDSOCK_NTYPES] = { 22040Sstevel@tonic-gate SPD_ATTR_AH_AUTH, 22050Sstevel@tonic-gate SPD_ATTR_ESP_AUTH, 22060Sstevel@tonic-gate SPD_ATTR_ESP_ENCR 22070Sstevel@tonic-gate }; 22080Sstevel@tonic-gate static const uint_t minbitsattr[SPDSOCK_NTYPES] = { 22090Sstevel@tonic-gate SPD_ATTR_AH_MINBITS, 22100Sstevel@tonic-gate SPD_ATTR_ESPA_MINBITS, 22110Sstevel@tonic-gate SPD_ATTR_ENCR_MINBITS 22120Sstevel@tonic-gate }; 22130Sstevel@tonic-gate static const uint_t maxbitsattr[SPDSOCK_NTYPES] = { 22140Sstevel@tonic-gate SPD_ATTR_AH_MAXBITS, 22150Sstevel@tonic-gate SPD_ATTR_ESPA_MAXBITS, 22160Sstevel@tonic-gate SPD_ATTR_ENCR_MAXBITS 22170Sstevel@tonic-gate }; 22180Sstevel@tonic-gate static const uint_t defbitsattr[SPDSOCK_NTYPES] = { 22190Sstevel@tonic-gate SPD_ATTR_AH_DEFBITS, 22200Sstevel@tonic-gate SPD_ATTR_ESPA_DEFBITS, 22210Sstevel@tonic-gate SPD_ATTR_ENCR_DEFBITS 22220Sstevel@tonic-gate }; 22230Sstevel@tonic-gate static const uint_t incrbitsattr[SPDSOCK_NTYPES] = { 22240Sstevel@tonic-gate SPD_ATTR_AH_INCRBITS, 22250Sstevel@tonic-gate SPD_ATTR_ESPA_INCRBITS, 22260Sstevel@tonic-gate SPD_ATTR_ENCR_INCRBITS 22270Sstevel@tonic-gate }; 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate #define ATTRPERALG 6 /* fixed attributes per algs */ 22300Sstevel@tonic-gate 22310Sstevel@tonic-gate void 22320Sstevel@tonic-gate spdsock_alglist(queue_t *q, mblk_t *mp) 22330Sstevel@tonic-gate { 22340Sstevel@tonic-gate uint_t algtype; 22350Sstevel@tonic-gate uint_t algidx; 22360Sstevel@tonic-gate uint_t algcount; 22370Sstevel@tonic-gate uint_t size; 22380Sstevel@tonic-gate mblk_t *m; 22390Sstevel@tonic-gate uint8_t *cur; 22400Sstevel@tonic-gate spd_msg_t *msg; 22410Sstevel@tonic-gate struct spd_ext_actions *act; 22420Sstevel@tonic-gate struct spd_attribute *attr; 22433448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 22446430Sdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 22453448Sdh155122 22463448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 22470Sstevel@tonic-gate /* 22480Sstevel@tonic-gate * The SPD client expects to receive separate entries for 22490Sstevel@tonic-gate * AH authentication and ESP authentication supported algorithms. 22500Sstevel@tonic-gate * 22510Sstevel@tonic-gate * Don't return the "any" algorithms, if defined, as no 22520Sstevel@tonic-gate * kernel policies can be set for these algorithms. 22530Sstevel@tonic-gate */ 22543448Sdh155122 algcount = 2 * ipss->ipsec_nalgs[IPSEC_ALG_AUTH] + 22553448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 22563448Sdh155122 22573448Sdh155122 if (ipss->ipsec_alglists[IPSEC_ALG_AUTH][SADB_AALG_NONE] != NULL) 22580Sstevel@tonic-gate algcount--; 22593448Sdh155122 if (ipss->ipsec_alglists[IPSEC_ALG_ENCR][SADB_EALG_NONE] != NULL) 22600Sstevel@tonic-gate algcount--; 22610Sstevel@tonic-gate 22620Sstevel@tonic-gate /* 22630Sstevel@tonic-gate * For each algorithm, we encode: 22640Sstevel@tonic-gate * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT} 22650Sstevel@tonic-gate */ 22660Sstevel@tonic-gate 22670Sstevel@tonic-gate size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions) + 22680Sstevel@tonic-gate ATTRPERALG * sizeof (struct spd_attribute) * algcount; 22690Sstevel@tonic-gate 22700Sstevel@tonic-gate ASSERT(ALIGNED64(size)); 22710Sstevel@tonic-gate 22720Sstevel@tonic-gate m = allocb(size, BPRI_HI); 22730Sstevel@tonic-gate if (m == NULL) { 22743448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 22750Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 22760Sstevel@tonic-gate return; 22770Sstevel@tonic-gate } 22780Sstevel@tonic-gate 22790Sstevel@tonic-gate m->b_wptr = m->b_rptr + size; 22800Sstevel@tonic-gate cur = m->b_rptr; 22810Sstevel@tonic-gate 22820Sstevel@tonic-gate msg = (spd_msg_t *)cur; 22830Sstevel@tonic-gate bcopy(mp->b_rptr, cur, sizeof (*msg)); 22840Sstevel@tonic-gate 22850Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(size); 22860Sstevel@tonic-gate msg->spd_msg_errno = 0; 22870Sstevel@tonic-gate msg->spd_msg_diagnostic = 0; 22880Sstevel@tonic-gate 22890Sstevel@tonic-gate cur += sizeof (*msg); 22900Sstevel@tonic-gate 22910Sstevel@tonic-gate act = (struct spd_ext_actions *)cur; 22920Sstevel@tonic-gate cur += sizeof (*act); 22930Sstevel@tonic-gate 22940Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t)); 22950Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 22960Sstevel@tonic-gate act->spd_actions_count = algcount; 22970Sstevel@tonic-gate act->spd_actions_reserved = 0; 22980Sstevel@tonic-gate 22990Sstevel@tonic-gate attr = (struct spd_attribute *)cur; 23000Sstevel@tonic-gate 23010Sstevel@tonic-gate #define EMIT(tag, value) { \ 23020Sstevel@tonic-gate attr->spd_attr_tag = (tag); \ 23030Sstevel@tonic-gate attr->spd_attr_value = (value); \ 23040Sstevel@tonic-gate attr++; \ 23050Sstevel@tonic-gate } 23060Sstevel@tonic-gate 23070Sstevel@tonic-gate /* 23080Sstevel@tonic-gate * If you change the number of EMIT's here, change 23090Sstevel@tonic-gate * ATTRPERALG above to match 23100Sstevel@tonic-gate */ 23110Sstevel@tonic-gate #define EMITALGATTRS(_type) { \ 23120Sstevel@tonic-gate EMIT(algattr[_type], algid); /* 1 */ \ 23130Sstevel@tonic-gate EMIT(minbitsattr[_type], minbits); /* 2 */ \ 23140Sstevel@tonic-gate EMIT(maxbitsattr[_type], maxbits); /* 3 */ \ 23150Sstevel@tonic-gate EMIT(defbitsattr[_type], defbits); /* 4 */ \ 23160Sstevel@tonic-gate EMIT(incrbitsattr[_type], incr); /* 5 */ \ 23170Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); /* 6 */ \ 23180Sstevel@tonic-gate } 23190Sstevel@tonic-gate 23200Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 23213448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 23223448Sdh155122 algidx++) { 23233448Sdh155122 int algid = ipss->ipsec_sortlist[algtype][algidx]; 23243448Sdh155122 ipsec_alginfo_t *alg = 23253448Sdh155122 ipss->ipsec_alglists[algtype][algid]; 23260Sstevel@tonic-gate uint_t minbits = alg->alg_minbits; 23270Sstevel@tonic-gate uint_t maxbits = alg->alg_maxbits; 23280Sstevel@tonic-gate uint_t defbits = alg->alg_default_bits; 23290Sstevel@tonic-gate uint_t incr = alg->alg_increment; 23300Sstevel@tonic-gate 23310Sstevel@tonic-gate if (algtype == IPSEC_ALG_AUTH) { 23320Sstevel@tonic-gate if (algid == SADB_AALG_NONE) 23330Sstevel@tonic-gate continue; 23340Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_AH_AUTH); 23350Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_ESP_AUTH); 23360Sstevel@tonic-gate } else { 23370Sstevel@tonic-gate if (algid == SADB_EALG_NONE) 23380Sstevel@tonic-gate continue; 23390Sstevel@tonic-gate ASSERT(algtype == IPSEC_ALG_ENCR); 23400Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_ESP_ENCR); 23410Sstevel@tonic-gate } 23420Sstevel@tonic-gate } 23430Sstevel@tonic-gate } 23440Sstevel@tonic-gate 23453448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 23460Sstevel@tonic-gate 23470Sstevel@tonic-gate #undef EMITALGATTRS 23480Sstevel@tonic-gate #undef EMIT 23490Sstevel@tonic-gate #undef ATTRPERALG 23500Sstevel@tonic-gate 23510Sstevel@tonic-gate attr--; 23520Sstevel@tonic-gate attr->spd_attr_tag = SPD_ATTR_END; 23530Sstevel@tonic-gate 23540Sstevel@tonic-gate freemsg(mp); 23550Sstevel@tonic-gate qreply(q, m); 23560Sstevel@tonic-gate } 23570Sstevel@tonic-gate 23580Sstevel@tonic-gate /* 23590Sstevel@tonic-gate * Process a SPD_DUMPALGS request. 23600Sstevel@tonic-gate */ 23610Sstevel@tonic-gate 236210824SMark.Fenwick@Sun.COM #define ATTRPERALG 9 /* fixed attributes per algs */ 23630Sstevel@tonic-gate 23640Sstevel@tonic-gate void 23650Sstevel@tonic-gate spdsock_dumpalgs(queue_t *q, mblk_t *mp) 23660Sstevel@tonic-gate { 23670Sstevel@tonic-gate uint_t algtype; 23680Sstevel@tonic-gate uint_t algidx; 23690Sstevel@tonic-gate uint_t size; 23700Sstevel@tonic-gate mblk_t *m; 23710Sstevel@tonic-gate uint8_t *cur; 23720Sstevel@tonic-gate spd_msg_t *msg; 23730Sstevel@tonic-gate struct spd_ext_actions *act; 23740Sstevel@tonic-gate struct spd_attribute *attr; 23750Sstevel@tonic-gate ipsec_alginfo_t *alg; 23760Sstevel@tonic-gate uint_t algid; 23770Sstevel@tonic-gate uint_t i; 23780Sstevel@tonic-gate uint_t alg_size; 23793448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 23806430Sdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 23813448Sdh155122 23823448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 23830Sstevel@tonic-gate 23840Sstevel@tonic-gate /* 23850Sstevel@tonic-gate * For each algorithm, we encode: 23860Sstevel@tonic-gate * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT} 23870Sstevel@tonic-gate * 23880Sstevel@tonic-gate * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE* 238910824SMark.Fenwick@Sun.COM * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_NPARAMS / ALG_PARAMS* / 239010824SMark.Fenwick@Sun.COM * ALG_MECHNAME / ALG_FLAGS / {END, NEXT} 23910Sstevel@tonic-gate */ 23920Sstevel@tonic-gate 23930Sstevel@tonic-gate /* 23940Sstevel@tonic-gate * Compute the size of the SPD message. 23950Sstevel@tonic-gate */ 23960Sstevel@tonic-gate size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions); 23970Sstevel@tonic-gate 23980Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 23993448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 24003448Sdh155122 algidx++) { 24013448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 24023448Sdh155122 alg = ipss->ipsec_alglists[algtype][algid]; 24030Sstevel@tonic-gate alg_size = sizeof (struct spd_attribute) * 24040Sstevel@tonic-gate (ATTRPERALG + alg->alg_nkey_sizes + 240510824SMark.Fenwick@Sun.COM alg->alg_nblock_sizes + alg->alg_nparams) + 240610824SMark.Fenwick@Sun.COM CRYPTO_MAX_MECH_NAME; 24070Sstevel@tonic-gate size += alg_size; 24080Sstevel@tonic-gate } 24090Sstevel@tonic-gate } 24100Sstevel@tonic-gate 24110Sstevel@tonic-gate ASSERT(ALIGNED64(size)); 24120Sstevel@tonic-gate 24130Sstevel@tonic-gate m = allocb(size, BPRI_HI); 24140Sstevel@tonic-gate if (m == NULL) { 24153448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 24160Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 24170Sstevel@tonic-gate return; 24180Sstevel@tonic-gate } 24190Sstevel@tonic-gate 24200Sstevel@tonic-gate m->b_wptr = m->b_rptr + size; 24210Sstevel@tonic-gate cur = m->b_rptr; 24220Sstevel@tonic-gate 24230Sstevel@tonic-gate msg = (spd_msg_t *)cur; 24240Sstevel@tonic-gate bcopy(mp->b_rptr, cur, sizeof (*msg)); 24250Sstevel@tonic-gate 24260Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(size); 24270Sstevel@tonic-gate msg->spd_msg_errno = 0; 242810019SMark.Fenwick@Sun.COM msg->spd_msg_type = SPD_ALGLIST; 242910019SMark.Fenwick@Sun.COM 24300Sstevel@tonic-gate msg->spd_msg_diagnostic = 0; 24310Sstevel@tonic-gate 24320Sstevel@tonic-gate cur += sizeof (*msg); 24330Sstevel@tonic-gate 24340Sstevel@tonic-gate act = (struct spd_ext_actions *)cur; 24350Sstevel@tonic-gate cur += sizeof (*act); 24360Sstevel@tonic-gate 24370Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t)); 24380Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION; 24393448Sdh155122 act->spd_actions_count = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] + 24403448Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 24410Sstevel@tonic-gate act->spd_actions_reserved = 0; 24420Sstevel@tonic-gate 244310019SMark.Fenwick@Sun.COM /* 244410019SMark.Fenwick@Sun.COM * If there aren't any algorithms registered, return an empty message. 244510019SMark.Fenwick@Sun.COM * spdsock_get_ext() knows how to deal with this. 244610019SMark.Fenwick@Sun.COM */ 244710019SMark.Fenwick@Sun.COM if (act->spd_actions_count == 0) { 244810019SMark.Fenwick@Sun.COM act->spd_actions_len = 0; 244910019SMark.Fenwick@Sun.COM mutex_exit(&ipss->ipsec_alg_lock); 245010019SMark.Fenwick@Sun.COM goto error; 245110019SMark.Fenwick@Sun.COM } 245210019SMark.Fenwick@Sun.COM 24530Sstevel@tonic-gate attr = (struct spd_attribute *)cur; 24540Sstevel@tonic-gate 24550Sstevel@tonic-gate #define EMIT(tag, value) { \ 24560Sstevel@tonic-gate attr->spd_attr_tag = (tag); \ 24570Sstevel@tonic-gate attr->spd_attr_value = (value); \ 24580Sstevel@tonic-gate attr++; \ 24590Sstevel@tonic-gate } 24600Sstevel@tonic-gate 24610Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 24623448Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 24633448Sdh155122 algidx++) { 24643448Sdh155122 24653448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 24663448Sdh155122 alg = ipss->ipsec_alglists[algtype][algid]; 24670Sstevel@tonic-gate 24680Sstevel@tonic-gate /* 24690Sstevel@tonic-gate * If you change the number of EMIT's here, change 24700Sstevel@tonic-gate * ATTRPERALG above to match 24710Sstevel@tonic-gate */ 24720Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_ID, algid); 24730Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_PROTO, algproto[algtype]); 24740Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_INCRBITS, alg->alg_increment); 24750Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_NKEYSIZES, alg->alg_nkey_sizes); 24760Sstevel@tonic-gate for (i = 0; i < alg->alg_nkey_sizes; i++) 24770Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_KEYSIZE, 24780Sstevel@tonic-gate alg->alg_key_sizes[i]); 24790Sstevel@tonic-gate 24800Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_NBLOCKSIZES, alg->alg_nblock_sizes); 24810Sstevel@tonic-gate for (i = 0; i < alg->alg_nblock_sizes; i++) 24820Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_BLOCKSIZE, 24830Sstevel@tonic-gate alg->alg_block_sizes[i]); 24840Sstevel@tonic-gate 248510824SMark.Fenwick@Sun.COM EMIT(SPD_ATTR_ALG_NPARAMS, alg->alg_nparams); 248610824SMark.Fenwick@Sun.COM for (i = 0; i < alg->alg_nparams; i++) 248710824SMark.Fenwick@Sun.COM EMIT(SPD_ATTR_ALG_PARAMS, 248810824SMark.Fenwick@Sun.COM alg->alg_params[i]); 248910824SMark.Fenwick@Sun.COM 249010824SMark.Fenwick@Sun.COM EMIT(SPD_ATTR_ALG_FLAGS, alg->alg_flags); 249110824SMark.Fenwick@Sun.COM 24920Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_MECHNAME, CRYPTO_MAX_MECH_NAME); 24930Sstevel@tonic-gate bcopy(alg->alg_mech_name, attr, CRYPTO_MAX_MECH_NAME); 24940Sstevel@tonic-gate attr = (struct spd_attribute *)((char *)attr + 24950Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME); 24960Sstevel@tonic-gate 24970Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); 24980Sstevel@tonic-gate } 24990Sstevel@tonic-gate } 25000Sstevel@tonic-gate 25013448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 25020Sstevel@tonic-gate 25030Sstevel@tonic-gate #undef EMITALGATTRS 25040Sstevel@tonic-gate #undef EMIT 25050Sstevel@tonic-gate #undef ATTRPERALG 25060Sstevel@tonic-gate 25070Sstevel@tonic-gate attr--; 25080Sstevel@tonic-gate attr->spd_attr_tag = SPD_ATTR_END; 25090Sstevel@tonic-gate 251010019SMark.Fenwick@Sun.COM error: 25110Sstevel@tonic-gate freemsg(mp); 25120Sstevel@tonic-gate qreply(q, m); 25130Sstevel@tonic-gate } 25140Sstevel@tonic-gate 25150Sstevel@tonic-gate /* 25160Sstevel@tonic-gate * Do the actual work of processing an SPD_UPDATEALGS request. Can 25170Sstevel@tonic-gate * be invoked either once IPsec is loaded on a cached request, or 25180Sstevel@tonic-gate * when a request is received while IPsec is loaded. 25190Sstevel@tonic-gate */ 2520*11042SErik.Nordmark@Sun.COM static int 2521*11042SErik.Nordmark@Sun.COM spdsock_do_updatealg(spd_ext_t *extv[], spd_stack_t *spds) 25220Sstevel@tonic-gate { 25230Sstevel@tonic-gate struct spd_ext_actions *actp; 25240Sstevel@tonic-gate struct spd_attribute *attr, *endattr; 25250Sstevel@tonic-gate uint64_t *start, *end; 25260Sstevel@tonic-gate ipsec_alginfo_t *alg = NULL; 25270Sstevel@tonic-gate ipsec_algtype_t alg_type = 0; 25280Sstevel@tonic-gate boolean_t skip_alg = B_TRUE, doing_proto = B_FALSE; 25290Sstevel@tonic-gate uint_t i, cur_key, cur_block, algid; 2530*11042SErik.Nordmark@Sun.COM int diag = -1; 2531*11042SErik.Nordmark@Sun.COM 25323448Sdh155122 ASSERT(MUTEX_HELD(&spds->spds_alg_lock)); 25330Sstevel@tonic-gate 25340Sstevel@tonic-gate /* parse the message, building the list of algorithms */ 25350Sstevel@tonic-gate 25360Sstevel@tonic-gate actp = (struct spd_ext_actions *)extv[SPD_EXT_ACTION]; 2537*11042SErik.Nordmark@Sun.COM if (actp == NULL) 2538*11042SErik.Nordmark@Sun.COM return (SPD_DIAGNOSTIC_NO_ACTION_EXT); 25390Sstevel@tonic-gate 25400Sstevel@tonic-gate start = (uint64_t *)actp; 25410Sstevel@tonic-gate end = (start + actp->spd_actions_len); 25420Sstevel@tonic-gate endattr = (struct spd_attribute *)end; 25430Sstevel@tonic-gate attr = (struct spd_attribute *)&actp[1]; 25440Sstevel@tonic-gate 25453448Sdh155122 bzero(spds->spds_algs, IPSEC_NALGTYPES * IPSEC_MAX_ALGS * 25460Sstevel@tonic-gate sizeof (ipsec_alginfo_t *)); 25470Sstevel@tonic-gate 25480Sstevel@tonic-gate alg = kmem_zalloc(sizeof (*alg), KM_SLEEP); 25490Sstevel@tonic-gate 25500Sstevel@tonic-gate #define ALG_KEY_SIZES(a) (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t)) 25510Sstevel@tonic-gate #define ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t)) 25520Sstevel@tonic-gate 25530Sstevel@tonic-gate while (attr < endattr) { 25540Sstevel@tonic-gate switch (attr->spd_attr_tag) { 25550Sstevel@tonic-gate case SPD_ATTR_NOP: 25560Sstevel@tonic-gate case SPD_ATTR_EMPTY: 25570Sstevel@tonic-gate break; 25580Sstevel@tonic-gate case SPD_ATTR_END: 25590Sstevel@tonic-gate attr = endattr; 25600Sstevel@tonic-gate /* FALLTHRU */ 25610Sstevel@tonic-gate case SPD_ATTR_NEXT: 25620Sstevel@tonic-gate if (doing_proto) { 25630Sstevel@tonic-gate doing_proto = B_FALSE; 25640Sstevel@tonic-gate break; 25650Sstevel@tonic-gate } 25660Sstevel@tonic-gate if (skip_alg) { 25670Sstevel@tonic-gate ipsec_alg_free(alg); 25680Sstevel@tonic-gate } else { 25690Sstevel@tonic-gate ipsec_alg_free( 25703448Sdh155122 spds->spds_algs[alg_type][alg->alg_id]); 25713448Sdh155122 spds->spds_algs[alg_type][alg->alg_id] = 25723448Sdh155122 alg; 25730Sstevel@tonic-gate } 25740Sstevel@tonic-gate alg = kmem_zalloc(sizeof (*alg), KM_SLEEP); 25750Sstevel@tonic-gate break; 25760Sstevel@tonic-gate 25770Sstevel@tonic-gate case SPD_ATTR_ALG_ID: 25780Sstevel@tonic-gate if (attr->spd_attr_value >= IPSEC_MAX_ALGS) { 25793448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 25800Sstevel@tonic-gate "invalid alg id %d\n", 25810Sstevel@tonic-gate attr->spd_attr_value)); 2582*11042SErik.Nordmark@Sun.COM diag = SPD_DIAGNOSTIC_ALG_ID_RANGE; 25830Sstevel@tonic-gate goto bail; 25840Sstevel@tonic-gate } 25850Sstevel@tonic-gate alg->alg_id = attr->spd_attr_value; 25860Sstevel@tonic-gate break; 25870Sstevel@tonic-gate 25880Sstevel@tonic-gate case SPD_ATTR_ALG_PROTO: 25890Sstevel@tonic-gate /* find the alg type */ 25900Sstevel@tonic-gate for (i = 0; i < NALGPROTOS; i++) 25910Sstevel@tonic-gate if (algproto[i] == attr->spd_attr_value) 25920Sstevel@tonic-gate break; 25930Sstevel@tonic-gate skip_alg = (i == NALGPROTOS); 25940Sstevel@tonic-gate if (!skip_alg) 25950Sstevel@tonic-gate alg_type = i; 25960Sstevel@tonic-gate break; 25970Sstevel@tonic-gate 25980Sstevel@tonic-gate case SPD_ATTR_ALG_INCRBITS: 25990Sstevel@tonic-gate alg->alg_increment = attr->spd_attr_value; 26000Sstevel@tonic-gate break; 26010Sstevel@tonic-gate 26020Sstevel@tonic-gate case SPD_ATTR_ALG_NKEYSIZES: 26030Sstevel@tonic-gate if (alg->alg_key_sizes != NULL) { 26040Sstevel@tonic-gate kmem_free(alg->alg_key_sizes, 26050Sstevel@tonic-gate ALG_KEY_SIZES(alg)); 26060Sstevel@tonic-gate } 26070Sstevel@tonic-gate alg->alg_nkey_sizes = attr->spd_attr_value; 26080Sstevel@tonic-gate /* 26090Sstevel@tonic-gate * Allocate room for the trailing zero key size 26100Sstevel@tonic-gate * value as well. 26110Sstevel@tonic-gate */ 26120Sstevel@tonic-gate alg->alg_key_sizes = kmem_zalloc(ALG_KEY_SIZES(alg), 26130Sstevel@tonic-gate KM_SLEEP); 26140Sstevel@tonic-gate cur_key = 0; 26150Sstevel@tonic-gate break; 26160Sstevel@tonic-gate 26170Sstevel@tonic-gate case SPD_ATTR_ALG_KEYSIZE: 26180Sstevel@tonic-gate if (alg->alg_key_sizes == NULL || 26190Sstevel@tonic-gate cur_key >= alg->alg_nkey_sizes) { 26203448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 26215240Snordmark "too many key sizes\n")); 2622*11042SErik.Nordmark@Sun.COM diag = SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES; 26230Sstevel@tonic-gate goto bail; 26240Sstevel@tonic-gate } 26250Sstevel@tonic-gate alg->alg_key_sizes[cur_key++] = attr->spd_attr_value; 26260Sstevel@tonic-gate break; 26270Sstevel@tonic-gate 262810824SMark.Fenwick@Sun.COM case SPD_ATTR_ALG_FLAGS: 262910824SMark.Fenwick@Sun.COM /* 263010824SMark.Fenwick@Sun.COM * Flags (bit mask). The alg_flags element of 263110824SMark.Fenwick@Sun.COM * ipsecalg_flags_t is only 8 bits wide. The 263210824SMark.Fenwick@Sun.COM * user can set the VALID bit, but we will ignore it 263310824SMark.Fenwick@Sun.COM * and make the decision is the algorithm is valid. 263410824SMark.Fenwick@Sun.COM */ 263510824SMark.Fenwick@Sun.COM alg->alg_flags |= (uint8_t)attr->spd_attr_value; 263610824SMark.Fenwick@Sun.COM break; 263710824SMark.Fenwick@Sun.COM 26380Sstevel@tonic-gate case SPD_ATTR_ALG_NBLOCKSIZES: 26390Sstevel@tonic-gate if (alg->alg_block_sizes != NULL) { 26400Sstevel@tonic-gate kmem_free(alg->alg_block_sizes, 26410Sstevel@tonic-gate ALG_BLOCK_SIZES(alg)); 26420Sstevel@tonic-gate } 26430Sstevel@tonic-gate alg->alg_nblock_sizes = attr->spd_attr_value; 26440Sstevel@tonic-gate /* 26450Sstevel@tonic-gate * Allocate room for the trailing zero block size 26460Sstevel@tonic-gate * value as well. 26470Sstevel@tonic-gate */ 26480Sstevel@tonic-gate alg->alg_block_sizes = kmem_zalloc(ALG_BLOCK_SIZES(alg), 26490Sstevel@tonic-gate KM_SLEEP); 26500Sstevel@tonic-gate cur_block = 0; 26510Sstevel@tonic-gate break; 26520Sstevel@tonic-gate 26530Sstevel@tonic-gate case SPD_ATTR_ALG_BLOCKSIZE: 26540Sstevel@tonic-gate if (alg->alg_block_sizes == NULL || 26550Sstevel@tonic-gate cur_block >= alg->alg_nblock_sizes) { 26563448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 26575240Snordmark "too many block sizes\n")); 2658*11042SErik.Nordmark@Sun.COM diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES; 26590Sstevel@tonic-gate goto bail; 26600Sstevel@tonic-gate } 26610Sstevel@tonic-gate alg->alg_block_sizes[cur_block++] = 26620Sstevel@tonic-gate attr->spd_attr_value; 26630Sstevel@tonic-gate break; 26640Sstevel@tonic-gate 266510824SMark.Fenwick@Sun.COM case SPD_ATTR_ALG_NPARAMS: 266610824SMark.Fenwick@Sun.COM if (alg->alg_params != NULL) { 266710824SMark.Fenwick@Sun.COM kmem_free(alg->alg_params, 266810824SMark.Fenwick@Sun.COM ALG_BLOCK_SIZES(alg)); 266910824SMark.Fenwick@Sun.COM } 267010824SMark.Fenwick@Sun.COM alg->alg_nparams = attr->spd_attr_value; 267110824SMark.Fenwick@Sun.COM /* 267210824SMark.Fenwick@Sun.COM * Allocate room for the trailing zero block size 267310824SMark.Fenwick@Sun.COM * value as well. 267410824SMark.Fenwick@Sun.COM */ 267510824SMark.Fenwick@Sun.COM alg->alg_params = kmem_zalloc(ALG_BLOCK_SIZES(alg), 267610824SMark.Fenwick@Sun.COM KM_SLEEP); 267710824SMark.Fenwick@Sun.COM cur_block = 0; 267810824SMark.Fenwick@Sun.COM break; 267910824SMark.Fenwick@Sun.COM 268010824SMark.Fenwick@Sun.COM case SPD_ATTR_ALG_PARAMS: 268110824SMark.Fenwick@Sun.COM if (alg->alg_params == NULL || 268210824SMark.Fenwick@Sun.COM cur_block >= alg->alg_nparams) { 268310824SMark.Fenwick@Sun.COM ss1dbg(spds, ("spdsock_do_updatealg: " 268410824SMark.Fenwick@Sun.COM "too many params\n")); 2685*11042SErik.Nordmark@Sun.COM diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES; 268610824SMark.Fenwick@Sun.COM goto bail; 268710824SMark.Fenwick@Sun.COM } 268810824SMark.Fenwick@Sun.COM /* 268910824SMark.Fenwick@Sun.COM * Array contains: iv_len, icv_len, salt_len 269010824SMark.Fenwick@Sun.COM * Any additional parameters are currently ignored. 269110824SMark.Fenwick@Sun.COM */ 269210824SMark.Fenwick@Sun.COM alg->alg_params[cur_block++] = 269310824SMark.Fenwick@Sun.COM attr->spd_attr_value; 269410824SMark.Fenwick@Sun.COM break; 269510824SMark.Fenwick@Sun.COM 26960Sstevel@tonic-gate case SPD_ATTR_ALG_MECHNAME: { 26970Sstevel@tonic-gate char *mech_name; 26980Sstevel@tonic-gate 26990Sstevel@tonic-gate if (attr->spd_attr_value > CRYPTO_MAX_MECH_NAME) { 27003448Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: " 27015240Snordmark "mech name too long\n")); 2702*11042SErik.Nordmark@Sun.COM diag = SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN; 27030Sstevel@tonic-gate goto bail; 27040Sstevel@tonic-gate } 27050Sstevel@tonic-gate mech_name = (char *)(attr + 1); 27060Sstevel@tonic-gate bcopy(mech_name, alg->alg_mech_name, 27070Sstevel@tonic-gate attr->spd_attr_value); 27080Sstevel@tonic-gate alg->alg_mech_name[CRYPTO_MAX_MECH_NAME-1] = '\0'; 27090Sstevel@tonic-gate attr = (struct spd_attribute *)((char *)attr + 27100Sstevel@tonic-gate attr->spd_attr_value); 27110Sstevel@tonic-gate break; 27120Sstevel@tonic-gate } 27130Sstevel@tonic-gate 27140Sstevel@tonic-gate case SPD_ATTR_PROTO_ID: 27150Sstevel@tonic-gate doing_proto = B_TRUE; 27160Sstevel@tonic-gate for (i = 0; i < NALGPROTOS; i++) { 27170Sstevel@tonic-gate if (algproto[i] == attr->spd_attr_value) { 27180Sstevel@tonic-gate alg_type = i; 27190Sstevel@tonic-gate break; 27200Sstevel@tonic-gate } 27210Sstevel@tonic-gate } 27220Sstevel@tonic-gate break; 27230Sstevel@tonic-gate 27240Sstevel@tonic-gate case SPD_ATTR_PROTO_EXEC_MODE: 27250Sstevel@tonic-gate if (!doing_proto) 27260Sstevel@tonic-gate break; 27270Sstevel@tonic-gate for (i = 0; i < NEXECMODES; i++) { 27280Sstevel@tonic-gate if (execmodes[i] == attr->spd_attr_value) { 27293448Sdh155122 spds->spds_algs_exec_mode[alg_type] = i; 27300Sstevel@tonic-gate break; 27310Sstevel@tonic-gate } 27320Sstevel@tonic-gate } 27330Sstevel@tonic-gate break; 27340Sstevel@tonic-gate } 27350Sstevel@tonic-gate attr++; 27360Sstevel@tonic-gate } 27370Sstevel@tonic-gate 27380Sstevel@tonic-gate #undef ALG_KEY_SIZES 27390Sstevel@tonic-gate #undef ALG_BLOCK_SIZES 27400Sstevel@tonic-gate 27410Sstevel@tonic-gate /* update the algorithm tables */ 27423448Sdh155122 spdsock_merge_algs(spds); 27430Sstevel@tonic-gate bail: 27440Sstevel@tonic-gate /* cleanup */ 27450Sstevel@tonic-gate ipsec_alg_free(alg); 27460Sstevel@tonic-gate for (alg_type = 0; alg_type < IPSEC_NALGTYPES; alg_type++) 27475240Snordmark for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) 27483448Sdh155122 if (spds->spds_algs[alg_type][algid] != NULL) 27495240Snordmark ipsec_alg_free(spds->spds_algs[alg_type][algid]); 2750*11042SErik.Nordmark@Sun.COM return (diag); 27510Sstevel@tonic-gate } 27520Sstevel@tonic-gate 27530Sstevel@tonic-gate /* 27540Sstevel@tonic-gate * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue 27550Sstevel@tonic-gate * the request until IPsec loads. If IPsec is loaded, act on it 27560Sstevel@tonic-gate * immediately. 27570Sstevel@tonic-gate */ 27580Sstevel@tonic-gate 27590Sstevel@tonic-gate static void 27600Sstevel@tonic-gate spdsock_updatealg(queue_t *q, mblk_t *mp, spd_ext_t *extv[]) 27610Sstevel@tonic-gate { 27623448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 27633448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 27643448Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 27653448Sdh155122 27663448Sdh155122 if (!ipsec_loaded(ipss)) { 27670Sstevel@tonic-gate /* 27680Sstevel@tonic-gate * IPsec is not loaded, save request and return nicely, 27690Sstevel@tonic-gate * the message will be processed once IPsec loads. 27700Sstevel@tonic-gate */ 27710Sstevel@tonic-gate mblk_t *new_mp; 27720Sstevel@tonic-gate 27730Sstevel@tonic-gate /* last update message wins */ 27740Sstevel@tonic-gate if ((new_mp = copymsg(mp)) == NULL) { 27750Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0); 27760Sstevel@tonic-gate return; 27770Sstevel@tonic-gate } 27783448Sdh155122 mutex_enter(&spds->spds_alg_lock); 27793448Sdh155122 bcopy(extv, spds->spds_extv_algs, 27800Sstevel@tonic-gate sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1)); 27813448Sdh155122 if (spds->spds_mp_algs != NULL) 27823448Sdh155122 freemsg(spds->spds_mp_algs); 27833448Sdh155122 spds->spds_mp_algs = mp; 27843448Sdh155122 spds->spds_algs_pending = B_TRUE; 27853448Sdh155122 mutex_exit(&spds->spds_alg_lock); 27868778SErik.Nordmark@Sun.COM if (audit_active) { 27878778SErik.Nordmark@Sun.COM cred_t *cr; 27888778SErik.Nordmark@Sun.COM pid_t cpid; 27898778SErik.Nordmark@Sun.COM 27908778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 27918778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_UPDATEALGS, cr, 27924307Spwernau spds->spds_netstack, NULL, B_TRUE, EAGAIN, 27938778SErik.Nordmark@Sun.COM cpid); 27948778SErik.Nordmark@Sun.COM } 27950Sstevel@tonic-gate spd_echo(q, new_mp); 27960Sstevel@tonic-gate } else { 27970Sstevel@tonic-gate /* 27980Sstevel@tonic-gate * IPsec is loaded, act on the message immediately. 27990Sstevel@tonic-gate */ 28000Sstevel@tonic-gate int diag; 28010Sstevel@tonic-gate 28023448Sdh155122 mutex_enter(&spds->spds_alg_lock); 2803*11042SErik.Nordmark@Sun.COM diag = spdsock_do_updatealg(extv, spds); 28044307Spwernau if (diag == -1) { 2805*11042SErik.Nordmark@Sun.COM /* Keep the lock held while we walk the SA tables. */ 2806*11042SErik.Nordmark@Sun.COM sadb_alg_update(IPSEC_ALG_ALL, 0, 0, 2807*11042SErik.Nordmark@Sun.COM spds->spds_netstack); 2808*11042SErik.Nordmark@Sun.COM mutex_exit(&spds->spds_alg_lock); 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 { 2820*11042SErik.Nordmark@Sun.COM mutex_exit(&spds->spds_alg_lock); 28210Sstevel@tonic-gate spdsock_diag(q, mp, diag); 28228778SErik.Nordmark@Sun.COM if (audit_active) { 28238778SErik.Nordmark@Sun.COM cred_t *cr; 28248778SErik.Nordmark@Sun.COM pid_t cpid; 28258778SErik.Nordmark@Sun.COM 28268778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, &cpid); 28278778SErik.Nordmark@Sun.COM audit_pf_policy(SPD_UPDATEALGS, cr, 28288778SErik.Nordmark@Sun.COM spds->spds_netstack, NULL, B_TRUE, diag, 28298778SErik.Nordmark@Sun.COM cpid); 28308778SErik.Nordmark@Sun.COM } 28314307Spwernau } 28320Sstevel@tonic-gate } 28330Sstevel@tonic-gate } 28340Sstevel@tonic-gate 28353055Sdanmcd /* 28363055Sdanmcd * Sort through the mess of polhead options to retrieve an appropriate one. 28373055Sdanmcd * Returns NULL if we send an spdsock error. Returns a valid pointer if we 28383055Sdanmcd * found a valid polhead. Returns ALL_ACTIVE_POLHEADS (aka. -1) or 28393055Sdanmcd * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to 28403055Sdanmcd * act on ALL policy heads. 28413055Sdanmcd */ 28423055Sdanmcd static ipsec_policy_head_t * 28433055Sdanmcd get_appropriate_polhead(queue_t *q, mblk_t *mp, spd_if_t *tunname, int spdid, 28443055Sdanmcd int msgtype, ipsec_tun_pol_t **itpp) 28453055Sdanmcd { 28463055Sdanmcd ipsec_tun_pol_t *itp; 28473055Sdanmcd ipsec_policy_head_t *iph; 28483055Sdanmcd int errno; 28493055Sdanmcd char *tname; 28503055Sdanmcd boolean_t active; 28513055Sdanmcd spdsock_t *ss = (spdsock_t *)q->q_ptr; 28526430Sdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack; 28533055Sdanmcd uint64_t gen; /* Placeholder */ 285410616SSebastien.Roy@Sun.COM datalink_id_t linkid; 28553055Sdanmcd 28563055Sdanmcd active = (spdid == SPD_ACTIVE); 28573055Sdanmcd *itpp = NULL; 28583055Sdanmcd if (!active && spdid != SPD_STANDBY) { 28593055Sdanmcd spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_SPDID); 28603055Sdanmcd return (NULL); 28613055Sdanmcd } 28623055Sdanmcd 28633055Sdanmcd if (tunname != NULL) { 28643055Sdanmcd /* Acting on a tunnel's SPD. */ 28653055Sdanmcd tname = (char *)tunname->spd_if_name; 28663055Sdanmcd if (*tname == '\0') { 28673055Sdanmcd /* Handle all-polhead cases here. */ 28683055Sdanmcd if (msgtype != SPD_FLUSH && msgtype != SPD_DUMP) { 28693055Sdanmcd spdsock_diag(q, mp, 28703055Sdanmcd SPD_DIAGNOSTIC_NOT_GLOBAL_OP); 28713055Sdanmcd return (NULL); 28723055Sdanmcd } 28733055Sdanmcd return (active ? ALL_ACTIVE_POLHEADS : 28743055Sdanmcd ALL_INACTIVE_POLHEADS); 28753055Sdanmcd } 28763055Sdanmcd 28776430Sdanmcd itp = get_tunnel_policy(tname, ns); 28783055Sdanmcd if (itp == NULL) { 28793055Sdanmcd if (msgtype != SPD_ADDRULE) { 28803055Sdanmcd /* "Tunnel not found" */ 28813055Sdanmcd spdsock_error(q, mp, ENOENT, 0); 28823055Sdanmcd return (NULL); 28833055Sdanmcd } 28843055Sdanmcd 28853055Sdanmcd errno = 0; 28866430Sdanmcd itp = create_tunnel_policy(tname, &errno, &gen, ns); 28873055Sdanmcd if (itp == NULL) { 28883055Sdanmcd /* 28893055Sdanmcd * Something very bad happened, most likely 28903055Sdanmcd * ENOMEM. Return an indicator. 28913055Sdanmcd */ 28923055Sdanmcd spdsock_error(q, mp, errno, 0); 28933055Sdanmcd return (NULL); 28943055Sdanmcd } 28953055Sdanmcd } 28963055Sdanmcd /* 289710616SSebastien.Roy@Sun.COM * Troll the plumbed tunnels and see if we have a match. We 289810616SSebastien.Roy@Sun.COM * need to do this always in case we add policy AFTER plumbing 289910616SSebastien.Roy@Sun.COM * a tunnel. 29003055Sdanmcd */ 290110616SSebastien.Roy@Sun.COM if (dls_mgmt_get_linkid(tname, &linkid) == 0) 290210616SSebastien.Roy@Sun.COM iptun_set_policy(linkid, itp); 290310616SSebastien.Roy@Sun.COM 29043055Sdanmcd *itpp = itp; 29053055Sdanmcd /* For spdsock dump state, set the polhead's name. */ 29063055Sdanmcd if (msgtype == SPD_DUMP) { 29076430Sdanmcd ITP_REFHOLD(itp); 29086430Sdanmcd ss->spdsock_itp = itp; 29093055Sdanmcd ss->spdsock_dump_tunnel = itp->itp_flags & 29103055Sdanmcd (active ? ITPF_P_TUNNEL : ITPF_I_TUNNEL); 29113055Sdanmcd } 29123055Sdanmcd } else { 29133055Sdanmcd itp = NULL; 29143055Sdanmcd /* For spdsock dump state, indicate it's global policy. */ 29153055Sdanmcd if (msgtype == SPD_DUMP) 29166430Sdanmcd ss->spdsock_itp = NULL; 29173055Sdanmcd } 29183055Sdanmcd 29193055Sdanmcd if (active) 29203448Sdh155122 iph = (itp == NULL) ? ipsec_system_policy(ns) : itp->itp_policy; 29213055Sdanmcd else 29223448Sdh155122 iph = (itp == NULL) ? ipsec_inactive_policy(ns) : 29233055Sdanmcd itp->itp_inactive; 29243055Sdanmcd 29253055Sdanmcd ASSERT(iph != NULL); 29263055Sdanmcd if (itp != NULL) { 29273055Sdanmcd IPPH_REFHOLD(iph); 29283055Sdanmcd } 29293055Sdanmcd 29303055Sdanmcd return (iph); 29313055Sdanmcd } 29323055Sdanmcd 29330Sstevel@tonic-gate static void 29340Sstevel@tonic-gate spdsock_parse(queue_t *q, mblk_t *mp) 29350Sstevel@tonic-gate { 29360Sstevel@tonic-gate spd_msg_t *spmsg; 29370Sstevel@tonic-gate spd_ext_t *extv[SPD_EXT_MAX + 1]; 29380Sstevel@tonic-gate uint_t msgsize; 29390Sstevel@tonic-gate ipsec_policy_head_t *iph; 29403055Sdanmcd ipsec_tun_pol_t *itp; 29413055Sdanmcd spd_if_t *tunname; 29423448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 29433448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 29443448Sdh155122 netstack_t *ns = spds->spds_netstack; 29453448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 29460Sstevel@tonic-gate 29470Sstevel@tonic-gate /* Make sure nothing's below me. */ 29480Sstevel@tonic-gate ASSERT(WR(q)->q_next == NULL); 29490Sstevel@tonic-gate 29500Sstevel@tonic-gate spmsg = (spd_msg_t *)mp->b_rptr; 29510Sstevel@tonic-gate 29520Sstevel@tonic-gate msgsize = SPD_64TO8(spmsg->spd_msg_len); 29530Sstevel@tonic-gate 29540Sstevel@tonic-gate if (msgdsize(mp) != msgsize) { 29550Sstevel@tonic-gate /* 29560Sstevel@tonic-gate * Message len incorrect w.r.t. actual size. Send an error 29570Sstevel@tonic-gate * (EMSGSIZE). It may be necessary to massage things a 29580Sstevel@tonic-gate * bit. For example, if the spd_msg_type is hosed, 29590Sstevel@tonic-gate * I need to set it to SPD_RESERVED to get delivery to 29600Sstevel@tonic-gate * do the right thing. Then again, maybe just letting 29610Sstevel@tonic-gate * the error delivery do the right thing. 29620Sstevel@tonic-gate */ 29633448Sdh155122 ss2dbg(spds, 29643448Sdh155122 ("mblk (%lu) and base (%d) message sizes don't jibe.\n", 29650Sstevel@tonic-gate msgdsize(mp), msgsize)); 29660Sstevel@tonic-gate spdsock_error(q, mp, EMSGSIZE, SPD_DIAGNOSTIC_NONE); 29670Sstevel@tonic-gate return; 29680Sstevel@tonic-gate } 29690Sstevel@tonic-gate 29700Sstevel@tonic-gate if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) { 29710Sstevel@tonic-gate /* Get all message into one mblk. */ 29720Sstevel@tonic-gate if (pullupmsg(mp, -1) == 0) { 29730Sstevel@tonic-gate /* 29740Sstevel@tonic-gate * Something screwy happened. 29750Sstevel@tonic-gate */ 29763448Sdh155122 ss3dbg(spds, ("spdsock_parse: pullupmsg() failed.\n")); 29770Sstevel@tonic-gate return; 29780Sstevel@tonic-gate } else { 29790Sstevel@tonic-gate spmsg = (spd_msg_t *)mp->b_rptr; 29800Sstevel@tonic-gate } 29810Sstevel@tonic-gate } 29820Sstevel@tonic-gate 29830Sstevel@tonic-gate switch (spdsock_get_ext(extv, spmsg, msgsize)) { 29840Sstevel@tonic-gate case KGE_DUP: 29850Sstevel@tonic-gate /* Handle duplicate extension. */ 29863448Sdh155122 ss1dbg(spds, ("Got duplicate extension of type %d.\n", 29870Sstevel@tonic-gate extv[0]->spd_ext_type)); 29880Sstevel@tonic-gate spdsock_diag(q, mp, dup_ext_diag[extv[0]->spd_ext_type]); 29890Sstevel@tonic-gate return; 29900Sstevel@tonic-gate case KGE_UNK: 29910Sstevel@tonic-gate /* Handle unknown extension. */ 29923448Sdh155122 ss1dbg(spds, ("Got unknown extension of type %d.\n", 29930Sstevel@tonic-gate extv[0]->spd_ext_type)); 29940Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_UNKNOWN_EXT); 29950Sstevel@tonic-gate return; 29960Sstevel@tonic-gate case KGE_LEN: 29970Sstevel@tonic-gate /* Length error. */ 29983448Sdh155122 ss1dbg(spds, ("Length %d on extension type %d overrun or 0.\n", 29990Sstevel@tonic-gate extv[0]->spd_ext_len, extv[0]->spd_ext_type)); 30000Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_EXTLEN); 30010Sstevel@tonic-gate return; 30020Sstevel@tonic-gate case KGE_CHK: 30030Sstevel@tonic-gate /* Reality check failed. */ 30043448Sdh155122 ss1dbg(spds, ("Reality check failed on extension type %d.\n", 30050Sstevel@tonic-gate extv[0]->spd_ext_type)); 30060Sstevel@tonic-gate spdsock_diag(q, mp, bad_ext_diag[extv[0]->spd_ext_type]); 30070Sstevel@tonic-gate return; 30080Sstevel@tonic-gate default: 30090Sstevel@tonic-gate /* Default case is no errors. */ 30100Sstevel@tonic-gate break; 30110Sstevel@tonic-gate } 30120Sstevel@tonic-gate 30130Sstevel@tonic-gate /* 30140Sstevel@tonic-gate * Special-case SPD_UPDATEALGS so as not to load IPsec. 30150Sstevel@tonic-gate */ 30163448Sdh155122 if (!ipsec_loaded(ipss) && spmsg->spd_msg_type != SPD_UPDATEALGS) { 30170Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 30180Sstevel@tonic-gate 30190Sstevel@tonic-gate ASSERT(ss != NULL); 30203448Sdh155122 ipsec_loader_loadnow(ipss); 30210Sstevel@tonic-gate ss->spdsock_timeout_arg = mp; 30220Sstevel@tonic-gate ss->spdsock_timeout = qtimeout(q, spdsock_loadcheck, 30230Sstevel@tonic-gate q, LOADCHECK_INTERVAL); 30240Sstevel@tonic-gate return; 30250Sstevel@tonic-gate } 30260Sstevel@tonic-gate 30273055Sdanmcd /* First check for messages that need no polheads at all. */ 30280Sstevel@tonic-gate switch (spmsg->spd_msg_type) { 30290Sstevel@tonic-gate case SPD_UPDATEALGS: 30300Sstevel@tonic-gate spdsock_updatealg(q, mp, extv); 30310Sstevel@tonic-gate return; 30320Sstevel@tonic-gate case SPD_ALGLIST: 30330Sstevel@tonic-gate spdsock_alglist(q, mp); 30340Sstevel@tonic-gate return; 30350Sstevel@tonic-gate case SPD_DUMPALGS: 30360Sstevel@tonic-gate spdsock_dumpalgs(q, mp); 30370Sstevel@tonic-gate return; 30383055Sdanmcd } 30393055Sdanmcd 30403055Sdanmcd /* 30413055Sdanmcd * Then check for ones that need both primary/secondary polheads, 30423055Sdanmcd * finding the appropriate tunnel policy if need be. 30433055Sdanmcd */ 30443055Sdanmcd tunname = (spd_if_t *)extv[SPD_EXT_TUN_NAME]; 30453055Sdanmcd switch (spmsg->spd_msg_type) { 30463055Sdanmcd case SPD_FLIP: 30473055Sdanmcd spdsock_flip(q, mp, tunname); 30483055Sdanmcd return; 30493055Sdanmcd case SPD_CLONE: 30503055Sdanmcd spdsock_clone(q, mp, tunname); 30513055Sdanmcd return; 30523055Sdanmcd } 30533055Sdanmcd 30543055Sdanmcd /* 30553055Sdanmcd * Finally, find ones that operate on exactly one polhead, or 30563055Sdanmcd * "all polheads" of a given type (active/inactive). 30573055Sdanmcd */ 30583055Sdanmcd iph = get_appropriate_polhead(q, mp, tunname, spmsg->spd_msg_spdid, 30593055Sdanmcd spmsg->spd_msg_type, &itp); 30603055Sdanmcd if (iph == NULL) 30613055Sdanmcd return; 30623055Sdanmcd 30633055Sdanmcd /* All-polheads-ready operations. */ 30643055Sdanmcd switch (spmsg->spd_msg_type) { 30653055Sdanmcd case SPD_FLUSH: 30663055Sdanmcd if (itp != NULL) { 30673055Sdanmcd mutex_enter(&itp->itp_lock); 30683055Sdanmcd if (spmsg->spd_msg_spdid == SPD_ACTIVE) 30693055Sdanmcd itp->itp_flags &= ~ITPF_PFLAGS; 30703055Sdanmcd else 30713055Sdanmcd itp->itp_flags &= ~ITPF_IFLAGS; 30723055Sdanmcd mutex_exit(&itp->itp_lock); 30733448Sdh155122 ITP_REFRELE(itp, ns); 30743055Sdanmcd } 30754307Spwernau spdsock_flush(q, iph, itp, mp); 30763055Sdanmcd return; 30773055Sdanmcd case SPD_DUMP: 30783055Sdanmcd if (itp != NULL) 30793448Sdh155122 ITP_REFRELE(itp, ns); 30803055Sdanmcd spdsock_dump(q, iph, mp); 30813055Sdanmcd return; 30823055Sdanmcd } 30833055Sdanmcd 30843055Sdanmcd if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) { 30853055Sdanmcd spdsock_diag(q, mp, SPD_DIAGNOSTIC_NOT_GLOBAL_OP); 30863055Sdanmcd return; 30873055Sdanmcd } 30883055Sdanmcd 30893055Sdanmcd /* Single-polhead-only operations. */ 30903055Sdanmcd switch (spmsg->spd_msg_type) { 30913055Sdanmcd case SPD_ADDRULE: 30923055Sdanmcd spdsock_addrule(q, iph, mp, extv, itp); 30933055Sdanmcd break; 30943055Sdanmcd case SPD_DELETERULE: 30953055Sdanmcd spdsock_deleterule(q, iph, mp, extv, itp); 30963055Sdanmcd break; 30973055Sdanmcd case SPD_LOOKUP: 30983055Sdanmcd spdsock_lookup(q, iph, mp, extv, itp); 30993055Sdanmcd break; 31000Sstevel@tonic-gate default: 31010Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_MSG_TYPE); 31023055Sdanmcd break; 31030Sstevel@tonic-gate } 31043055Sdanmcd 31056430Sdanmcd IPPH_REFRELE(iph, ns); 31063055Sdanmcd if (itp != NULL) 31073448Sdh155122 ITP_REFRELE(itp, ns); 31080Sstevel@tonic-gate } 31090Sstevel@tonic-gate 31100Sstevel@tonic-gate /* 31110Sstevel@tonic-gate * If an algorithm mapping was received before IPsec was loaded, process it. 31120Sstevel@tonic-gate * Called from the IPsec loader. 31130Sstevel@tonic-gate */ 31140Sstevel@tonic-gate void 31153448Sdh155122 spdsock_update_pending_algs(netstack_t *ns) 31160Sstevel@tonic-gate { 31173448Sdh155122 spd_stack_t *spds = ns->netstack_spdsock; 31183448Sdh155122 31193448Sdh155122 mutex_enter(&spds->spds_alg_lock); 31203448Sdh155122 if (spds->spds_algs_pending) { 3121*11042SErik.Nordmark@Sun.COM (void) spdsock_do_updatealg(spds->spds_extv_algs, spds); 31223448Sdh155122 spds->spds_algs_pending = B_FALSE; 31230Sstevel@tonic-gate } 31243448Sdh155122 mutex_exit(&spds->spds_alg_lock); 31250Sstevel@tonic-gate } 31260Sstevel@tonic-gate 31270Sstevel@tonic-gate static void 31280Sstevel@tonic-gate spdsock_loadcheck(void *arg) 31290Sstevel@tonic-gate { 31300Sstevel@tonic-gate queue_t *q = (queue_t *)arg; 31310Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 31320Sstevel@tonic-gate mblk_t *mp; 31336430Sdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 31340Sstevel@tonic-gate 31350Sstevel@tonic-gate ASSERT(ss != NULL); 31360Sstevel@tonic-gate 31370Sstevel@tonic-gate ss->spdsock_timeout = 0; 31380Sstevel@tonic-gate mp = ss->spdsock_timeout_arg; 31390Sstevel@tonic-gate ASSERT(mp != NULL); 31400Sstevel@tonic-gate ss->spdsock_timeout_arg = NULL; 31413448Sdh155122 if (ipsec_failed(ipss)) 31420Sstevel@tonic-gate spdsock_error(q, mp, EPROTONOSUPPORT, 0); 31430Sstevel@tonic-gate else 31440Sstevel@tonic-gate spdsock_parse(q, mp); 31450Sstevel@tonic-gate } 31460Sstevel@tonic-gate 31470Sstevel@tonic-gate /* 31480Sstevel@tonic-gate * Copy relevant state bits. 31490Sstevel@tonic-gate */ 31500Sstevel@tonic-gate static void 31510Sstevel@tonic-gate spdsock_copy_info(struct T_info_ack *tap, spdsock_t *ss) 31520Sstevel@tonic-gate { 31530Sstevel@tonic-gate *tap = spdsock_g_t_info_ack; 31540Sstevel@tonic-gate tap->CURRENT_state = ss->spdsock_state; 31550Sstevel@tonic-gate tap->OPT_size = spdsock_max_optsize; 31560Sstevel@tonic-gate } 31570Sstevel@tonic-gate 31580Sstevel@tonic-gate /* 31590Sstevel@tonic-gate * This routine responds to T_CAPABILITY_REQ messages. It is called by 31600Sstevel@tonic-gate * spdsock_wput. Much of the T_CAPABILITY_ACK information is copied from 31610Sstevel@tonic-gate * spdsock_g_t_info_ack. The current state of the stream is copied from 31620Sstevel@tonic-gate * spdsock_state. 31630Sstevel@tonic-gate */ 31640Sstevel@tonic-gate static void 31650Sstevel@tonic-gate spdsock_capability_req(queue_t *q, mblk_t *mp) 31660Sstevel@tonic-gate { 31670Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 31680Sstevel@tonic-gate t_uscalar_t cap_bits1; 31690Sstevel@tonic-gate struct T_capability_ack *tcap; 31700Sstevel@tonic-gate 31710Sstevel@tonic-gate cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 31720Sstevel@tonic-gate 31730Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 31745240Snordmark mp->b_datap->db_type, T_CAPABILITY_ACK); 31750Sstevel@tonic-gate if (mp == NULL) 31760Sstevel@tonic-gate return; 31770Sstevel@tonic-gate 31780Sstevel@tonic-gate tcap = (struct T_capability_ack *)mp->b_rptr; 31790Sstevel@tonic-gate tcap->CAP_bits1 = 0; 31800Sstevel@tonic-gate 31810Sstevel@tonic-gate if (cap_bits1 & TC1_INFO) { 31820Sstevel@tonic-gate spdsock_copy_info(&tcap->INFO_ack, ss); 31830Sstevel@tonic-gate tcap->CAP_bits1 |= TC1_INFO; 31840Sstevel@tonic-gate } 31850Sstevel@tonic-gate 31860Sstevel@tonic-gate qreply(q, mp); 31870Sstevel@tonic-gate } 31880Sstevel@tonic-gate 31890Sstevel@tonic-gate /* 31900Sstevel@tonic-gate * This routine responds to T_INFO_REQ messages. It is called by 31910Sstevel@tonic-gate * spdsock_wput_other. 31920Sstevel@tonic-gate * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack. 31930Sstevel@tonic-gate * The current state of the stream is copied from spdsock_state. 31940Sstevel@tonic-gate */ 31950Sstevel@tonic-gate static void 31960Sstevel@tonic-gate spdsock_info_req(q, mp) 31970Sstevel@tonic-gate queue_t *q; 31980Sstevel@tonic-gate mblk_t *mp; 31990Sstevel@tonic-gate { 32000Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 32010Sstevel@tonic-gate T_INFO_ACK); 32020Sstevel@tonic-gate if (mp == NULL) 32030Sstevel@tonic-gate return; 32040Sstevel@tonic-gate spdsock_copy_info((struct T_info_ack *)mp->b_rptr, 32050Sstevel@tonic-gate (spdsock_t *)q->q_ptr); 32060Sstevel@tonic-gate qreply(q, mp); 32070Sstevel@tonic-gate } 32080Sstevel@tonic-gate 32090Sstevel@tonic-gate /* 32100Sstevel@tonic-gate * spdsock_err_ack. This routine creates a 32110Sstevel@tonic-gate * T_ERROR_ACK message and passes it 32120Sstevel@tonic-gate * upstream. 32130Sstevel@tonic-gate */ 32140Sstevel@tonic-gate static void 32150Sstevel@tonic-gate spdsock_err_ack(q, mp, t_error, sys_error) 32160Sstevel@tonic-gate queue_t *q; 32170Sstevel@tonic-gate mblk_t *mp; 32180Sstevel@tonic-gate int t_error; 32190Sstevel@tonic-gate int sys_error; 32200Sstevel@tonic-gate { 32210Sstevel@tonic-gate if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 32220Sstevel@tonic-gate qreply(q, mp); 32230Sstevel@tonic-gate } 32240Sstevel@tonic-gate 32250Sstevel@tonic-gate /* 32260Sstevel@tonic-gate * This routine retrieves the current status of socket options. 32270Sstevel@tonic-gate * It returns the size of the option retrieved. 32280Sstevel@tonic-gate */ 32290Sstevel@tonic-gate /* ARGSUSED */ 32300Sstevel@tonic-gate int 32310Sstevel@tonic-gate spdsock_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 32320Sstevel@tonic-gate { 32330Sstevel@tonic-gate int *i1 = (int *)ptr; 32340Sstevel@tonic-gate 32350Sstevel@tonic-gate switch (level) { 32360Sstevel@tonic-gate case SOL_SOCKET: 32370Sstevel@tonic-gate switch (name) { 32380Sstevel@tonic-gate case SO_TYPE: 32390Sstevel@tonic-gate *i1 = SOCK_RAW; 32400Sstevel@tonic-gate break; 32410Sstevel@tonic-gate /* 32420Sstevel@tonic-gate * The following two items can be manipulated, 32430Sstevel@tonic-gate * but changing them should do nothing. 32440Sstevel@tonic-gate */ 32450Sstevel@tonic-gate case SO_SNDBUF: 32460Sstevel@tonic-gate *i1 = (int)q->q_hiwat; 32470Sstevel@tonic-gate break; 32480Sstevel@tonic-gate case SO_RCVBUF: 32490Sstevel@tonic-gate *i1 = (int)(RD(q)->q_hiwat); 32500Sstevel@tonic-gate break; 32510Sstevel@tonic-gate } 32520Sstevel@tonic-gate break; 32530Sstevel@tonic-gate default: 32540Sstevel@tonic-gate return (0); 32550Sstevel@tonic-gate } 32560Sstevel@tonic-gate return (sizeof (int)); 32570Sstevel@tonic-gate } 32580Sstevel@tonic-gate 32590Sstevel@tonic-gate /* 32600Sstevel@tonic-gate * This routine sets socket options. 32610Sstevel@tonic-gate */ 32620Sstevel@tonic-gate /* ARGSUSED */ 32630Sstevel@tonic-gate int 32640Sstevel@tonic-gate spdsock_opt_set(queue_t *q, uint_t mgmt_flags, int level, int name, 32650Sstevel@tonic-gate uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 3266*11042SErik.Nordmark@Sun.COM void *thisdg_attrs, cred_t *cr) 32670Sstevel@tonic-gate { 32680Sstevel@tonic-gate int *i1 = (int *)invalp; 32693448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 32703448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 32710Sstevel@tonic-gate 32720Sstevel@tonic-gate switch (level) { 32730Sstevel@tonic-gate case SOL_SOCKET: 32740Sstevel@tonic-gate switch (name) { 32750Sstevel@tonic-gate case SO_SNDBUF: 32763448Sdh155122 if (*i1 > spds->spds_max_buf) 32770Sstevel@tonic-gate return (ENOBUFS); 32780Sstevel@tonic-gate q->q_hiwat = *i1; 32790Sstevel@tonic-gate break; 32800Sstevel@tonic-gate case SO_RCVBUF: 32813448Sdh155122 if (*i1 > spds->spds_max_buf) 32820Sstevel@tonic-gate return (ENOBUFS); 32830Sstevel@tonic-gate RD(q)->q_hiwat = *i1; 32848348SEric.Yu@Sun.COM (void) proto_set_rx_hiwat(RD(q), NULL, *i1); 32850Sstevel@tonic-gate break; 32860Sstevel@tonic-gate } 32870Sstevel@tonic-gate break; 32880Sstevel@tonic-gate } 32890Sstevel@tonic-gate return (0); 32900Sstevel@tonic-gate } 32910Sstevel@tonic-gate 32920Sstevel@tonic-gate 32930Sstevel@tonic-gate /* 32940Sstevel@tonic-gate * Handle STREAMS messages. 32950Sstevel@tonic-gate */ 32960Sstevel@tonic-gate static void 32970Sstevel@tonic-gate spdsock_wput_other(queue_t *q, mblk_t *mp) 32980Sstevel@tonic-gate { 32990Sstevel@tonic-gate struct iocblk *iocp; 33000Sstevel@tonic-gate int error; 33013448Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr; 33023448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 33033448Sdh155122 cred_t *cr; 33040Sstevel@tonic-gate 33050Sstevel@tonic-gate switch (mp->b_datap->db_type) { 33060Sstevel@tonic-gate case M_PROTO: 33070Sstevel@tonic-gate case M_PCPROTO: 33080Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) { 33093448Sdh155122 ss3dbg(spds, ( 33100Sstevel@tonic-gate "spdsock_wput_other: Not big enough M_PROTO\n")); 33110Sstevel@tonic-gate freemsg(mp); 33120Sstevel@tonic-gate return; 33130Sstevel@tonic-gate } 33140Sstevel@tonic-gate switch (((union T_primitives *)mp->b_rptr)->type) { 33150Sstevel@tonic-gate case T_CAPABILITY_REQ: 33160Sstevel@tonic-gate spdsock_capability_req(q, mp); 33173448Sdh155122 break; 33180Sstevel@tonic-gate case T_INFO_REQ: 33190Sstevel@tonic-gate spdsock_info_req(q, mp); 33203448Sdh155122 break; 33210Sstevel@tonic-gate case T_SVR4_OPTMGMT_REQ: 33220Sstevel@tonic-gate case T_OPTMGMT_REQ: 33238778SErik.Nordmark@Sun.COM /* 33248778SErik.Nordmark@Sun.COM * All Solaris components should pass a db_credp 33258778SErik.Nordmark@Sun.COM * for this TPI message, hence we ASSERT. 33268778SErik.Nordmark@Sun.COM * But in case there is some other M_PROTO that looks 33278778SErik.Nordmark@Sun.COM * like a TPI message sent by some other kernel 33288778SErik.Nordmark@Sun.COM * component, we check and return an error. 33298778SErik.Nordmark@Sun.COM */ 33308778SErik.Nordmark@Sun.COM cr = msg_getcred(mp, NULL); 33318778SErik.Nordmark@Sun.COM ASSERT(cr != NULL); 33328778SErik.Nordmark@Sun.COM if (cr == NULL) { 33338778SErik.Nordmark@Sun.COM spdsock_err_ack(q, mp, TSYSERR, EINVAL); 33348778SErik.Nordmark@Sun.COM return; 33358778SErik.Nordmark@Sun.COM } 33368778SErik.Nordmark@Sun.COM if (((union T_primitives *)mp->b_rptr)->type == 33378778SErik.Nordmark@Sun.COM T_SVR4_OPTMGMT_REQ) { 3338*11042SErik.Nordmark@Sun.COM svr4_optcom_req(q, mp, cr, &spdsock_opt_obj); 33398778SErik.Nordmark@Sun.COM } else { 3340*11042SErik.Nordmark@Sun.COM tpi_optcom_req(q, mp, cr, &spdsock_opt_obj); 33418778SErik.Nordmark@Sun.COM } 33423448Sdh155122 break; 33430Sstevel@tonic-gate case T_DATA_REQ: 33440Sstevel@tonic-gate case T_EXDATA_REQ: 33450Sstevel@tonic-gate case T_ORDREL_REQ: 33460Sstevel@tonic-gate /* Illegal for spdsock. */ 33470Sstevel@tonic-gate freemsg(mp); 33480Sstevel@tonic-gate (void) putnextctl1(RD(q), M_ERROR, EPROTO); 33493448Sdh155122 break; 33500Sstevel@tonic-gate default: 33510Sstevel@tonic-gate /* Not supported by spdsock. */ 33520Sstevel@tonic-gate spdsock_err_ack(q, mp, TNOTSUPPORT, 0); 33533448Sdh155122 break; 33540Sstevel@tonic-gate } 33553448Sdh155122 return; 33560Sstevel@tonic-gate case M_IOCTL: 33570Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 33580Sstevel@tonic-gate error = EINVAL; 33590Sstevel@tonic-gate 33600Sstevel@tonic-gate switch (iocp->ioc_cmd) { 33610Sstevel@tonic-gate case ND_SET: 33620Sstevel@tonic-gate case ND_GET: 33633448Sdh155122 if (nd_getset(q, spds->spds_g_nd, mp)) { 33640Sstevel@tonic-gate qreply(q, mp); 33650Sstevel@tonic-gate return; 33660Sstevel@tonic-gate } else 33670Sstevel@tonic-gate error = ENOENT; 33680Sstevel@tonic-gate /* FALLTHRU */ 33690Sstevel@tonic-gate default: 33700Sstevel@tonic-gate miocnak(q, mp, 0, error); 33710Sstevel@tonic-gate return; 33720Sstevel@tonic-gate } 33730Sstevel@tonic-gate case M_FLUSH: 33740Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) { 33750Sstevel@tonic-gate flushq(q, FLUSHALL); 33760Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW; 33770Sstevel@tonic-gate } 33780Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) { 33790Sstevel@tonic-gate qreply(q, mp); 33800Sstevel@tonic-gate return; 33810Sstevel@tonic-gate } 33820Sstevel@tonic-gate /* Else FALLTHRU */ 33830Sstevel@tonic-gate } 33840Sstevel@tonic-gate 33850Sstevel@tonic-gate /* If fell through, just black-hole the message. */ 33860Sstevel@tonic-gate freemsg(mp); 33870Sstevel@tonic-gate } 33880Sstevel@tonic-gate 33890Sstevel@tonic-gate static void 33900Sstevel@tonic-gate spdsock_wput(queue_t *q, mblk_t *mp) 33910Sstevel@tonic-gate { 33920Sstevel@tonic-gate uint8_t *rptr = mp->b_rptr; 33930Sstevel@tonic-gate mblk_t *mp1; 33940Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr; 33953448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 33960Sstevel@tonic-gate 33970Sstevel@tonic-gate /* 33980Sstevel@tonic-gate * If we're dumping, defer processing other messages until the 33990Sstevel@tonic-gate * dump completes. 34000Sstevel@tonic-gate */ 34010Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) { 34020Sstevel@tonic-gate if (!putq(q, mp)) 34030Sstevel@tonic-gate freemsg(mp); 34040Sstevel@tonic-gate return; 34050Sstevel@tonic-gate } 34060Sstevel@tonic-gate 34070Sstevel@tonic-gate switch (mp->b_datap->db_type) { 34080Sstevel@tonic-gate case M_DATA: 34090Sstevel@tonic-gate /* 34100Sstevel@tonic-gate * Silently discard. 34110Sstevel@tonic-gate */ 34123448Sdh155122 ss2dbg(spds, ("raw M_DATA in spdsock.\n")); 34130Sstevel@tonic-gate freemsg(mp); 34140Sstevel@tonic-gate return; 34150Sstevel@tonic-gate case M_PROTO: 34160Sstevel@tonic-gate case M_PCPROTO: 34170Sstevel@tonic-gate if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) { 34180Sstevel@tonic-gate if (((union T_primitives *)rptr)->type == T_DATA_REQ) { 34190Sstevel@tonic-gate if ((mp1 = mp->b_cont) == NULL) { 34200Sstevel@tonic-gate /* No data after T_DATA_REQ. */ 34213448Sdh155122 ss2dbg(spds, 34223448Sdh155122 ("No data after DATA_REQ.\n")); 34230Sstevel@tonic-gate freemsg(mp); 34240Sstevel@tonic-gate return; 34250Sstevel@tonic-gate } 34260Sstevel@tonic-gate freeb(mp); 34270Sstevel@tonic-gate mp = mp1; 34283448Sdh155122 ss2dbg(spds, ("T_DATA_REQ\n")); 34290Sstevel@tonic-gate break; /* Out of switch. */ 34300Sstevel@tonic-gate } 34310Sstevel@tonic-gate } 34320Sstevel@tonic-gate /* FALLTHRU */ 34330Sstevel@tonic-gate default: 34343448Sdh155122 ss3dbg(spds, ("In default wput case (%d %d).\n", 34350Sstevel@tonic-gate mp->b_datap->db_type, ((union T_primitives *)rptr)->type)); 34360Sstevel@tonic-gate spdsock_wput_other(q, mp); 34370Sstevel@tonic-gate return; 34380Sstevel@tonic-gate } 34390Sstevel@tonic-gate 34400Sstevel@tonic-gate /* I now have a PF_POLICY message in an M_DATA block. */ 34410Sstevel@tonic-gate spdsock_parse(q, mp); 34420Sstevel@tonic-gate } 34430Sstevel@tonic-gate 34440Sstevel@tonic-gate /* 34450Sstevel@tonic-gate * Device open procedure, called when new queue pair created. 34460Sstevel@tonic-gate * We are passed the read-side queue. 34470Sstevel@tonic-gate */ 34480Sstevel@tonic-gate /* ARGSUSED */ 34490Sstevel@tonic-gate static int 34500Sstevel@tonic-gate spdsock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 34510Sstevel@tonic-gate { 34520Sstevel@tonic-gate spdsock_t *ss; 34530Sstevel@tonic-gate queue_t *oq = OTHERQ(q); 34540Sstevel@tonic-gate minor_t ssminor; 34553448Sdh155122 netstack_t *ns; 34563448Sdh155122 spd_stack_t *spds; 34573448Sdh155122 34583448Sdh155122 if (secpolicy_ip_config(credp, B_FALSE) != 0) 34590Sstevel@tonic-gate return (EPERM); 34600Sstevel@tonic-gate 34610Sstevel@tonic-gate if (q->q_ptr != NULL) 34620Sstevel@tonic-gate return (0); /* Re-open of an already open instance. */ 34630Sstevel@tonic-gate 34640Sstevel@tonic-gate if (sflag & MODOPEN) 34650Sstevel@tonic-gate return (EINVAL); 34660Sstevel@tonic-gate 34673448Sdh155122 ns = netstack_find_by_cred(credp); 34683448Sdh155122 ASSERT(ns != NULL); 34693448Sdh155122 spds = ns->netstack_spdsock; 34703448Sdh155122 ASSERT(spds != NULL); 34713448Sdh155122 34723448Sdh155122 ss2dbg(spds, ("Made it into PF_POLICY socket open.\n")); 34730Sstevel@tonic-gate 34740Sstevel@tonic-gate ssminor = (minor_t)(uintptr_t)vmem_alloc(spdsock_vmem, 1, VM_NOSLEEP); 34753448Sdh155122 if (ssminor == 0) { 34763448Sdh155122 netstack_rele(spds->spds_netstack); 34770Sstevel@tonic-gate return (ENOMEM); 34783448Sdh155122 } 34790Sstevel@tonic-gate ss = kmem_zalloc(sizeof (spdsock_t), KM_NOSLEEP); 34800Sstevel@tonic-gate if (ss == NULL) { 34810Sstevel@tonic-gate vmem_free(spdsock_vmem, (void *)(uintptr_t)ssminor, 1); 34823448Sdh155122 netstack_rele(spds->spds_netstack); 34830Sstevel@tonic-gate return (ENOMEM); 34840Sstevel@tonic-gate } 34850Sstevel@tonic-gate 34860Sstevel@tonic-gate ss->spdsock_minor = ssminor; 34870Sstevel@tonic-gate ss->spdsock_state = TS_UNBND; 34880Sstevel@tonic-gate ss->spdsock_dump_req = NULL; 34890Sstevel@tonic-gate 34903448Sdh155122 ss->spdsock_spds = spds; 34913448Sdh155122 34920Sstevel@tonic-gate q->q_ptr = ss; 34930Sstevel@tonic-gate oq->q_ptr = ss; 34940Sstevel@tonic-gate 34953448Sdh155122 q->q_hiwat = spds->spds_recv_hiwat; 34963448Sdh155122 34973448Sdh155122 oq->q_hiwat = spds->spds_xmit_hiwat; 34983448Sdh155122 oq->q_lowat = spds->spds_xmit_lowat; 34990Sstevel@tonic-gate 35000Sstevel@tonic-gate qprocson(q); 35018348SEric.Yu@Sun.COM (void) proto_set_rx_hiwat(q, NULL, spds->spds_recv_hiwat); 35020Sstevel@tonic-gate 35030Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), ss->spdsock_minor); 35040Sstevel@tonic-gate return (0); 35050Sstevel@tonic-gate } 35060Sstevel@tonic-gate 35070Sstevel@tonic-gate /* 35080Sstevel@tonic-gate * Read-side service procedure, invoked when we get back-enabled 35090Sstevel@tonic-gate * when buffer space becomes available. 35100Sstevel@tonic-gate * 35110Sstevel@tonic-gate * Dump another chunk if we were dumping before; when we finish, kick 35120Sstevel@tonic-gate * the write-side queue in case it's waiting for read queue space. 35130Sstevel@tonic-gate */ 35140Sstevel@tonic-gate void 35150Sstevel@tonic-gate spdsock_rsrv(queue_t *q) 35160Sstevel@tonic-gate { 35170Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 35180Sstevel@tonic-gate 35190Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) 35200Sstevel@tonic-gate spdsock_dump_some(q, ss); 35210Sstevel@tonic-gate 35220Sstevel@tonic-gate if (ss->spdsock_dump_req == NULL) 35230Sstevel@tonic-gate qenable(OTHERQ(q)); 35240Sstevel@tonic-gate } 35250Sstevel@tonic-gate 35260Sstevel@tonic-gate /* 35270Sstevel@tonic-gate * Write-side service procedure, invoked when we defer processing 35280Sstevel@tonic-gate * if another message is received while a dump is in progress. 35290Sstevel@tonic-gate */ 35300Sstevel@tonic-gate void 35310Sstevel@tonic-gate spdsock_wsrv(queue_t *q) 35320Sstevel@tonic-gate { 35330Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 35340Sstevel@tonic-gate mblk_t *mp; 35356430Sdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 35360Sstevel@tonic-gate 35370Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) { 35380Sstevel@tonic-gate qenable(OTHERQ(q)); 35390Sstevel@tonic-gate return; 35400Sstevel@tonic-gate } 35410Sstevel@tonic-gate 35420Sstevel@tonic-gate while ((mp = getq(q)) != NULL) { 35433448Sdh155122 if (ipsec_loaded(ipss)) { 35440Sstevel@tonic-gate spdsock_wput(q, mp); 35450Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) 35460Sstevel@tonic-gate return; 35473448Sdh155122 } else if (!ipsec_failed(ipss)) { 35480Sstevel@tonic-gate (void) putq(q, mp); 35490Sstevel@tonic-gate } else { 35500Sstevel@tonic-gate spdsock_error(q, mp, EPFNOSUPPORT, 0); 35510Sstevel@tonic-gate } 35520Sstevel@tonic-gate } 35530Sstevel@tonic-gate } 35540Sstevel@tonic-gate 35550Sstevel@tonic-gate static int 35560Sstevel@tonic-gate spdsock_close(queue_t *q) 35570Sstevel@tonic-gate { 35580Sstevel@tonic-gate spdsock_t *ss = q->q_ptr; 35593448Sdh155122 spd_stack_t *spds = ss->spdsock_spds; 35600Sstevel@tonic-gate 35610Sstevel@tonic-gate qprocsoff(q); 35620Sstevel@tonic-gate 35630Sstevel@tonic-gate /* Safe assumption. */ 35640Sstevel@tonic-gate ASSERT(ss != NULL); 35650Sstevel@tonic-gate 35660Sstevel@tonic-gate if (ss->spdsock_timeout != 0) 35670Sstevel@tonic-gate (void) quntimeout(q, ss->spdsock_timeout); 35680Sstevel@tonic-gate 35693448Sdh155122 ss3dbg(spds, ("Driver close, PF_POLICY socket is going away.\n")); 35700Sstevel@tonic-gate 35710Sstevel@tonic-gate vmem_free(spdsock_vmem, (void *)(uintptr_t)ss->spdsock_minor, 1); 35723448Sdh155122 netstack_rele(ss->spdsock_spds->spds_netstack); 35730Sstevel@tonic-gate 35740Sstevel@tonic-gate kmem_free(ss, sizeof (spdsock_t)); 35750Sstevel@tonic-gate return (0); 35760Sstevel@tonic-gate } 35770Sstevel@tonic-gate 35780Sstevel@tonic-gate /* 35790Sstevel@tonic-gate * Merge the IPsec algorithms tables with the received algorithm information. 35800Sstevel@tonic-gate */ 35810Sstevel@tonic-gate void 35823448Sdh155122 spdsock_merge_algs(spd_stack_t *spds) 35830Sstevel@tonic-gate { 35840Sstevel@tonic-gate ipsec_alginfo_t *alg, *oalg; 35850Sstevel@tonic-gate ipsec_algtype_t algtype; 35860Sstevel@tonic-gate uint_t algidx, algid, nalgs; 35870Sstevel@tonic-gate crypto_mech_name_t *mechs; 35880Sstevel@tonic-gate uint_t mech_count, mech_idx; 35893448Sdh155122 netstack_t *ns = spds->spds_netstack; 35903448Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec; 35913448Sdh155122 35923448Sdh155122 ASSERT(MUTEX_HELD(&spds->spds_alg_lock)); 35930Sstevel@tonic-gate 35940Sstevel@tonic-gate /* 35950Sstevel@tonic-gate * Get the list of supported mechanisms from the crypto framework. 35960Sstevel@tonic-gate * If a mechanism is supported by KCF, resolve its mechanism 35970Sstevel@tonic-gate * id and mark it as being valid. This operation must be done 35980Sstevel@tonic-gate * without holding alg_lock, since it can cause a provider 35990Sstevel@tonic-gate * module to be loaded and the provider notification callback to 36000Sstevel@tonic-gate * be invoked. 36010Sstevel@tonic-gate */ 36020Sstevel@tonic-gate mechs = crypto_get_mech_list(&mech_count, KM_SLEEP); 36030Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 36040Sstevel@tonic-gate for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) { 36050Sstevel@tonic-gate int algflags = 0; 36060Sstevel@tonic-gate crypto_mech_type_t mt = CRYPTO_MECHANISM_INVALID; 36070Sstevel@tonic-gate 36083448Sdh155122 alg = spds->spds_algs[algtype][algid]; 36093448Sdh155122 if (alg == NULL) 36100Sstevel@tonic-gate continue; 36110Sstevel@tonic-gate 36120Sstevel@tonic-gate /* 36130Sstevel@tonic-gate * The NULL encryption algorithm is a special 36140Sstevel@tonic-gate * case because there are no mechanisms, yet 36150Sstevel@tonic-gate * the algorithm is still valid. 36160Sstevel@tonic-gate */ 36170Sstevel@tonic-gate if (alg->alg_id == SADB_EALG_NULL) { 36180Sstevel@tonic-gate alg->alg_mech_type = CRYPTO_MECHANISM_INVALID; 361910824SMark.Fenwick@Sun.COM alg->alg_flags |= ALG_FLAG_VALID; 36200Sstevel@tonic-gate continue; 36210Sstevel@tonic-gate } 36220Sstevel@tonic-gate 36230Sstevel@tonic-gate for (mech_idx = 0; mech_idx < mech_count; mech_idx++) { 36240Sstevel@tonic-gate if (strncmp(alg->alg_mech_name, mechs[mech_idx], 36250Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME) == 0) { 36260Sstevel@tonic-gate mt = crypto_mech2id(alg->alg_mech_name); 36270Sstevel@tonic-gate ASSERT(mt != CRYPTO_MECHANISM_INVALID); 36280Sstevel@tonic-gate algflags = ALG_FLAG_VALID; 36290Sstevel@tonic-gate break; 36300Sstevel@tonic-gate } 36310Sstevel@tonic-gate } 36320Sstevel@tonic-gate alg->alg_mech_type = mt; 363310824SMark.Fenwick@Sun.COM alg->alg_flags |= algflags; 36340Sstevel@tonic-gate } 36350Sstevel@tonic-gate } 36360Sstevel@tonic-gate 36373448Sdh155122 mutex_enter(&ipss->ipsec_alg_lock); 36380Sstevel@tonic-gate 36390Sstevel@tonic-gate /* 36400Sstevel@tonic-gate * For each algorithm currently defined, check if it is 36410Sstevel@tonic-gate * present in the new tables created from the SPD_UPDATEALGS 36420Sstevel@tonic-gate * message received from user-space. 36430Sstevel@tonic-gate * Delete the algorithm entries that are currently defined 36440Sstevel@tonic-gate * but not part of the new tables. 36450Sstevel@tonic-gate */ 36460Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 36473448Sdh155122 nalgs = ipss->ipsec_nalgs[algtype]; 36480Sstevel@tonic-gate for (algidx = 0; algidx < nalgs; algidx++) { 36493448Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx]; 36503448Sdh155122 if (spds->spds_algs[algtype][algid] == NULL) 36513448Sdh155122 ipsec_alg_unreg(algtype, algid, ns); 36520Sstevel@tonic-gate } 36530Sstevel@tonic-gate } 36540Sstevel@tonic-gate 36550Sstevel@tonic-gate /* 36560Sstevel@tonic-gate * For each algorithm we just received, check if it is 36570Sstevel@tonic-gate * present in the currently defined tables. If it is, swap 36580Sstevel@tonic-gate * the entry with the one we just allocated. 36590Sstevel@tonic-gate * If the new algorithm is not in the current tables, 36600Sstevel@tonic-gate * add it. 36610Sstevel@tonic-gate */ 36620Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 36630Sstevel@tonic-gate for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) { 36643448Sdh155122 alg = spds->spds_algs[algtype][algid]; 36653448Sdh155122 if (alg == NULL) 36660Sstevel@tonic-gate continue; 36670Sstevel@tonic-gate 36683448Sdh155122 if ((oalg = ipss->ipsec_alglists[algtype][algid]) == 36693448Sdh155122 NULL) { 36700Sstevel@tonic-gate /* 36710Sstevel@tonic-gate * New algorithm, add it to the algorithm 36720Sstevel@tonic-gate * table. 36730Sstevel@tonic-gate */ 36743448Sdh155122 ipsec_alg_reg(algtype, alg, ns); 36750Sstevel@tonic-gate } else { 36760Sstevel@tonic-gate /* 36770Sstevel@tonic-gate * Algorithm is already in the table. Swap 36780Sstevel@tonic-gate * the existing entry with the new one. 36790Sstevel@tonic-gate */ 36803448Sdh155122 ipsec_alg_fix_min_max(alg, algtype, ns); 36813448Sdh155122 ipss->ipsec_alglists[algtype][algid] = alg; 36820Sstevel@tonic-gate ipsec_alg_free(oalg); 36830Sstevel@tonic-gate } 36843448Sdh155122 spds->spds_algs[algtype][algid] = NULL; 36850Sstevel@tonic-gate } 36860Sstevel@tonic-gate } 36870Sstevel@tonic-gate 36883448Sdh155122 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 36893448Sdh155122 ipss->ipsec_algs_exec_mode[algtype] = 36903448Sdh155122 spds->spds_algs_exec_mode[algtype]; 36913448Sdh155122 } 36923448Sdh155122 36933448Sdh155122 mutex_exit(&ipss->ipsec_alg_lock); 36940Sstevel@tonic-gate 36950Sstevel@tonic-gate crypto_free_mech_list(mechs, mech_count); 36960Sstevel@tonic-gate 36973448Sdh155122 ipsecah_algs_changed(ns); 36983448Sdh155122 ipsecesp_algs_changed(ns); 36990Sstevel@tonic-gate } 3700