17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
58810c16bSdanmcd * Common Development and Distribution License (the "License").
68810c16bSdanmcd * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*134a1f4eSCasper H.S. Dik * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate #include <sys/param.h>
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/stream.h>
287c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
297c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
307c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
31f4b3ec61Sdh155122 #include <sys/zone.h>
327c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
357c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
397c478bd9Sstevel@tonic-gate #include <sys/debug.h>
407c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
417c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
427c478bd9Sstevel@tonic-gate #include <sys/suntpi.h>
437c478bd9Sstevel@tonic-gate #include <sys/policy.h>
442b24ab6bSSebastien Roy #include <sys/dls.h>
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #include <sys/socket.h>
477c478bd9Sstevel@tonic-gate #include <netinet/in.h>
487c478bd9Sstevel@tonic-gate #include <net/pfkeyv2.h>
497c478bd9Sstevel@tonic-gate #include <net/pfpolicy.h>
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate #include <inet/common.h>
527c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
537c478bd9Sstevel@tonic-gate #include <inet/ip.h>
547c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
557c478bd9Sstevel@tonic-gate #include <inet/mi.h>
560f1702c5SYu Xiangning #include <inet/proto_set.h>
577c478bd9Sstevel@tonic-gate #include <inet/nd.h>
588810c16bSdanmcd #include <inet/ip_if.h>
597c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
607c478bd9Sstevel@tonic-gate #include <inet/ipsec_impl.h>
617c478bd9Sstevel@tonic-gate #include <inet/spdsock.h>
627c478bd9Sstevel@tonic-gate #include <inet/sadb.h>
632b24ab6bSSebastien Roy #include <inet/iptun.h>
642b24ab6bSSebastien Roy #include <inet/iptun/iptun_impl.h>
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
677c478bd9Sstevel@tonic-gate
68799bd290Spwernau #include <c2/audit.h>
69799bd290Spwernau
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate * This is a transport provider for the PF_POLICY IPsec policy
727c478bd9Sstevel@tonic-gate * management socket, which provides a management interface into the
737c478bd9Sstevel@tonic-gate * SPD, allowing policy rules to be added, deleted, and queried.
747c478bd9Sstevel@tonic-gate *
757c478bd9Sstevel@tonic-gate * This effectively replaces the old private SIOC*IPSECONFIG ioctls
767c478bd9Sstevel@tonic-gate * with an extensible interface which will hopefully be public some
777c478bd9Sstevel@tonic-gate * day.
787c478bd9Sstevel@tonic-gate *
797c478bd9Sstevel@tonic-gate * See <net/pfpolicy.h> for more details on the protocol.
807c478bd9Sstevel@tonic-gate *
817c478bd9Sstevel@tonic-gate * We link against drv/ip and call directly into it to manipulate the
827c478bd9Sstevel@tonic-gate * SPD; see ipsec_impl.h for the policy data structures and spd.c for
837c478bd9Sstevel@tonic-gate * the code which maintains them.
847c478bd9Sstevel@tonic-gate *
857c478bd9Sstevel@tonic-gate * The MT model of this is QPAIR with the addition of some explicit
867c478bd9Sstevel@tonic-gate * locking to protect system-wide policy data structures.
877c478bd9Sstevel@tonic-gate */
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate static vmem_t *spdsock_vmem; /* for minor numbers. */
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate #define ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t))
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages (from rts.c...) */
947c478bd9Sstevel@tonic-gate static struct T_info_ack spdsock_g_t_info_ack = {
957c478bd9Sstevel@tonic-gate T_INFO_ACK,
967c478bd9Sstevel@tonic-gate T_INFINITE, /* TSDU_size. Maximum size messages. */
977c478bd9Sstevel@tonic-gate T_INVALID, /* ETSDU_size. No expedited data. */
987c478bd9Sstevel@tonic-gate T_INVALID, /* CDATA_size. No connect data. */
997c478bd9Sstevel@tonic-gate T_INVALID, /* DDATA_size. No disconnect data. */
1007c478bd9Sstevel@tonic-gate 0, /* ADDR_size. */
1017c478bd9Sstevel@tonic-gate 0, /* OPT_size. No user-settable options */
1027c478bd9Sstevel@tonic-gate 64 * 1024, /* TIDU_size. spdsock allows maximum size messages. */
1037c478bd9Sstevel@tonic-gate T_COTS, /* SERV_type. spdsock supports connection oriented. */
1047c478bd9Sstevel@tonic-gate TS_UNBND, /* CURRENT_state. This is set from spdsock_state. */
1057c478bd9Sstevel@tonic-gate (XPG4_1) /* Provider flags */
1067c478bd9Sstevel@tonic-gate };
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */
109f4b3ec61Sdh155122 typedef struct spdsockparam_s {
1107c478bd9Sstevel@tonic-gate uint_t spdsock_param_min;
1117c478bd9Sstevel@tonic-gate uint_t spdsock_param_max;
1127c478bd9Sstevel@tonic-gate uint_t spdsock_param_value;
1137c478bd9Sstevel@tonic-gate char *spdsock_param_name;
1147c478bd9Sstevel@tonic-gate } spdsockparam_t;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * Table of NDD variables supported by spdsock. These are loaded into
1187c478bd9Sstevel@tonic-gate * spdsock_g_nd in spdsock_init_nd.
1197c478bd9Sstevel@tonic-gate * All of these are alterable, within the min/max values given, at run time.
1207c478bd9Sstevel@tonic-gate */
121f4b3ec61Sdh155122 static spdsockparam_t lcl_param_arr[] = {
1227c478bd9Sstevel@tonic-gate /* min max value name */
1237c478bd9Sstevel@tonic-gate { 4096, 65536, 8192, "spdsock_xmit_hiwat"},
1247c478bd9Sstevel@tonic-gate { 0, 65536, 1024, "spdsock_xmit_lowat"},
1257c478bd9Sstevel@tonic-gate { 4096, 65536, 8192, "spdsock_recv_hiwat"},
1267c478bd9Sstevel@tonic-gate { 65536, 1024*1024*1024, 256*1024, "spdsock_max_buf"},
1277c478bd9Sstevel@tonic-gate { 0, 3, 0, "spdsock_debug"},
1287c478bd9Sstevel@tonic-gate };
129f4b3ec61Sdh155122 #define spds_xmit_hiwat spds_params[0].spdsock_param_value
130f4b3ec61Sdh155122 #define spds_xmit_lowat spds_params[1].spdsock_param_value
131f4b3ec61Sdh155122 #define spds_recv_hiwat spds_params[2].spdsock_param_value
132f4b3ec61Sdh155122 #define spds_max_buf spds_params[3].spdsock_param_value
133f4b3ec61Sdh155122 #define spds_debug spds_params[4].spdsock_param_value
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate #define ss0dbg(a) printf a
1367c478bd9Sstevel@tonic-gate /* NOTE: != 0 instead of > 0 so lint doesn't complain. */
137f4b3ec61Sdh155122 #define ss1dbg(spds, a) if (spds->spds_debug != 0) printf a
138f4b3ec61Sdh155122 #define ss2dbg(spds, a) if (spds->spds_debug > 1) printf a
139f4b3ec61Sdh155122 #define ss3dbg(spds, a) if (spds->spds_debug > 2) printf a
1407c478bd9Sstevel@tonic-gate
141db061fdfSdanmcd #define RESET_SPDSOCK_DUMP_POLHEAD(ss, iph) { \
142db061fdfSdanmcd ASSERT(RW_READ_HELD(&(iph)->iph_lock)); \
143db061fdfSdanmcd (ss)->spdsock_dump_head = (iph); \
144db061fdfSdanmcd (ss)->spdsock_dump_gen = (iph)->iph_gen; \
145db061fdfSdanmcd (ss)->spdsock_dump_cur_type = 0; \
146db061fdfSdanmcd (ss)->spdsock_dump_cur_af = IPSEC_AF_V4; \
147db061fdfSdanmcd (ss)->spdsock_dump_cur_rule = NULL; \
148db061fdfSdanmcd (ss)->spdsock_dump_count = 0; \
149db061fdfSdanmcd (ss)->spdsock_dump_cur_chain = 0; \
150db061fdfSdanmcd }
151db061fdfSdanmcd
1527c478bd9Sstevel@tonic-gate static int spdsock_close(queue_t *);
1537c478bd9Sstevel@tonic-gate static int spdsock_open(queue_t *, dev_t *, int, int, cred_t *);
1547c478bd9Sstevel@tonic-gate static void spdsock_wput(queue_t *, mblk_t *);
1557c478bd9Sstevel@tonic-gate static void spdsock_wsrv(queue_t *);
1567c478bd9Sstevel@tonic-gate static void spdsock_rsrv(queue_t *);
157f4b3ec61Sdh155122 static void *spdsock_stack_init(netstackid_t stackid, netstack_t *ns);
158*134a1f4eSCasper H.S. Dik static void spdsock_stack_shutdown(netstackid_t stackid, void *arg);
159f4b3ec61Sdh155122 static void spdsock_stack_fini(netstackid_t stackid, void *arg);
1607c478bd9Sstevel@tonic-gate static void spdsock_loadcheck(void *);
161f4b3ec61Sdh155122 static void spdsock_merge_algs(spd_stack_t *);
162f4b3ec61Sdh155122 static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *);
1638810c16bSdanmcd static mblk_t *spdsock_dump_next_record(spdsock_t *);
16401ac885fSDan McDonald static void update_iptun_policy(ipsec_tun_pol_t *);
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate static struct module_info info = {
1677c478bd9Sstevel@tonic-gate 5138, "spdsock", 1, INFPSZ, 512, 128
1687c478bd9Sstevel@tonic-gate };
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate static struct qinit rinit = {
1717c478bd9Sstevel@tonic-gate NULL, (pfi_t)spdsock_rsrv, spdsock_open, spdsock_close,
1727c478bd9Sstevel@tonic-gate NULL, &info
1737c478bd9Sstevel@tonic-gate };
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate static struct qinit winit = {
1767c478bd9Sstevel@tonic-gate (pfi_t)spdsock_wput, (pfi_t)spdsock_wsrv, NULL, NULL, NULL, &info
1777c478bd9Sstevel@tonic-gate };
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate struct streamtab spdsockinfo = {
1807c478bd9Sstevel@tonic-gate &rinit, &winit
1817c478bd9Sstevel@tonic-gate };
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate /* mapping from alg type to protocol number, as per RFC 2407 */
1847c478bd9Sstevel@tonic-gate static const uint_t algproto[] = {
1857c478bd9Sstevel@tonic-gate PROTO_IPSEC_AH,
1867c478bd9Sstevel@tonic-gate PROTO_IPSEC_ESP,
1877c478bd9Sstevel@tonic-gate };
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate #define NALGPROTOS (sizeof (algproto) / sizeof (algproto[0]))
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /* mapping from kernel exec mode to spdsock exec mode */
1927c478bd9Sstevel@tonic-gate static const uint_t execmodes[] = {
1937c478bd9Sstevel@tonic-gate SPD_ALG_EXEC_MODE_SYNC,
1947c478bd9Sstevel@tonic-gate SPD_ALG_EXEC_MODE_ASYNC
1957c478bd9Sstevel@tonic-gate };
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate #define NEXECMODES (sizeof (execmodes) / sizeof (execmodes[0]))
1987c478bd9Sstevel@tonic-gate
1998810c16bSdanmcd #define ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1)
2008810c16bSdanmcd #define ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2)
2018810c16bSdanmcd
202799bd290Spwernau #define ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL)
203799bd290Spwernau
2047c478bd9Sstevel@tonic-gate /* ARGSUSED */
2057c478bd9Sstevel@tonic-gate static int
spdsock_param_get(q,mp,cp,cr)2067c478bd9Sstevel@tonic-gate spdsock_param_get(q, mp, cp, cr)
2077c478bd9Sstevel@tonic-gate queue_t *q;
2087c478bd9Sstevel@tonic-gate mblk_t *mp;
2097c478bd9Sstevel@tonic-gate caddr_t cp;
2107c478bd9Sstevel@tonic-gate cred_t *cr;
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate spdsockparam_t *spdsockpa = (spdsockparam_t *)cp;
2137c478bd9Sstevel@tonic-gate uint_t value;
214f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
215f4b3ec61Sdh155122 spd_stack_t *spds = ss->spdsock_spds;
2167c478bd9Sstevel@tonic-gate
217f4b3ec61Sdh155122 mutex_enter(&spds->spds_param_lock);
2187c478bd9Sstevel@tonic-gate value = spdsockpa->spdsock_param_value;
219f4b3ec61Sdh155122 mutex_exit(&spds->spds_param_lock);
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate (void) mi_mpprintf(mp, "%u", value);
2227c478bd9Sstevel@tonic-gate return (0);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /* This routine sets an NDD variable in a spdsockparam_t structure. */
2267c478bd9Sstevel@tonic-gate /* ARGSUSED */
2277c478bd9Sstevel@tonic-gate static int
spdsock_param_set(q,mp,value,cp,cr)2287c478bd9Sstevel@tonic-gate spdsock_param_set(q, mp, value, cp, cr)
2297c478bd9Sstevel@tonic-gate queue_t *q;
2307c478bd9Sstevel@tonic-gate mblk_t *mp;
2317c478bd9Sstevel@tonic-gate char *value;
2327c478bd9Sstevel@tonic-gate caddr_t cp;
2337c478bd9Sstevel@tonic-gate cred_t *cr;
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate ulong_t new_value;
2367c478bd9Sstevel@tonic-gate spdsockparam_t *spdsockpa = (spdsockparam_t *)cp;
237f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
238f4b3ec61Sdh155122 spd_stack_t *spds = ss->spdsock_spds;
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /* Convert the value from a string into a long integer. */
2417c478bd9Sstevel@tonic-gate if (ddi_strtoul(value, NULL, 10, &new_value) != 0)
2427c478bd9Sstevel@tonic-gate return (EINVAL);
2437c478bd9Sstevel@tonic-gate
244f4b3ec61Sdh155122 mutex_enter(&spds->spds_param_lock);
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate * Fail the request if the new value does not lie within the
2477c478bd9Sstevel@tonic-gate * required bounds.
2487c478bd9Sstevel@tonic-gate */
2497c478bd9Sstevel@tonic-gate if (new_value < spdsockpa->spdsock_param_min ||
2507c478bd9Sstevel@tonic-gate new_value > spdsockpa->spdsock_param_max) {
251f4b3ec61Sdh155122 mutex_exit(&spds->spds_param_lock);
2527c478bd9Sstevel@tonic-gate return (EINVAL);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate /* Set the new value */
2567c478bd9Sstevel@tonic-gate spdsockpa->spdsock_param_value = new_value;
257f4b3ec61Sdh155122 mutex_exit(&spds->spds_param_lock);
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate return (0);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate
262f4b3ec61Sdh155122 /*
263f4b3ec61Sdh155122 * Initialize at module load time
264f4b3ec61Sdh155122 */
2657c478bd9Sstevel@tonic-gate boolean_t
spdsock_ddi_init(void)2667c478bd9Sstevel@tonic-gate spdsock_ddi_init(void)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate spdsock_max_optsize = optcom_max_optsize(
2697c478bd9Sstevel@tonic-gate spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt);
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1,
2727c478bd9Sstevel@tonic-gate NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER);
2737c478bd9Sstevel@tonic-gate
274f4b3ec61Sdh155122 /*
275f4b3ec61Sdh155122 * We want to be informed each time a stack is created or
276f4b3ec61Sdh155122 * destroyed in the kernel, so we can maintain the
277f4b3ec61Sdh155122 * set of spd_stack_t's.
278f4b3ec61Sdh155122 */
279*134a1f4eSCasper H.S. Dik netstack_register(NS_SPDSOCK, spdsock_stack_init,
280*134a1f4eSCasper H.S. Dik spdsock_stack_shutdown, spdsock_stack_fini);
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate return (B_TRUE);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate
285f4b3ec61Sdh155122 /*
286f4b3ec61Sdh155122 * Walk through the param array specified registering each element with the
287f4b3ec61Sdh155122 * named dispatch handler.
288f4b3ec61Sdh155122 */
289f4b3ec61Sdh155122 static boolean_t
spdsock_param_register(IDP * ndp,spdsockparam_t * ssp,int cnt)290f4b3ec61Sdh155122 spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt)
291f4b3ec61Sdh155122 {
292f4b3ec61Sdh155122 for (; cnt-- > 0; ssp++) {
293f4b3ec61Sdh155122 if (ssp->spdsock_param_name != NULL &&
294f4b3ec61Sdh155122 ssp->spdsock_param_name[0]) {
295f4b3ec61Sdh155122 if (!nd_load(ndp,
296f4b3ec61Sdh155122 ssp->spdsock_param_name,
297f4b3ec61Sdh155122 spdsock_param_get, spdsock_param_set,
298f4b3ec61Sdh155122 (caddr_t)ssp)) {
299f4b3ec61Sdh155122 nd_free(ndp);
300f4b3ec61Sdh155122 return (B_FALSE);
301f4b3ec61Sdh155122 }
302f4b3ec61Sdh155122 }
303f4b3ec61Sdh155122 }
304f4b3ec61Sdh155122 return (B_TRUE);
305f4b3ec61Sdh155122 }
306f4b3ec61Sdh155122
307f4b3ec61Sdh155122 /*
308f4b3ec61Sdh155122 * Initialize for each stack instance
309f4b3ec61Sdh155122 */
310f4b3ec61Sdh155122 /* ARGSUSED */
311f4b3ec61Sdh155122 static void *
spdsock_stack_init(netstackid_t stackid,netstack_t * ns)312f4b3ec61Sdh155122 spdsock_stack_init(netstackid_t stackid, netstack_t *ns)
313f4b3ec61Sdh155122 {
314f4b3ec61Sdh155122 spd_stack_t *spds;
315f4b3ec61Sdh155122 spdsockparam_t *ssp;
316f4b3ec61Sdh155122
317f4b3ec61Sdh155122 spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP);
318f4b3ec61Sdh155122 spds->spds_netstack = ns;
319f4b3ec61Sdh155122
320f4b3ec61Sdh155122 ASSERT(spds->spds_g_nd == NULL);
321f4b3ec61Sdh155122
322f4b3ec61Sdh155122 ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
323f4b3ec61Sdh155122 spds->spds_params = ssp;
324f4b3ec61Sdh155122 bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr));
325f4b3ec61Sdh155122
326f4b3ec61Sdh155122 (void) spdsock_param_register(&spds->spds_g_nd, ssp,
327f4b3ec61Sdh155122 A_CNT(lcl_param_arr));
328f4b3ec61Sdh155122
329f4b3ec61Sdh155122 mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL);
330f4b3ec61Sdh155122 mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL);
331f4b3ec61Sdh155122
332f4b3ec61Sdh155122 return (spds);
333f4b3ec61Sdh155122 }
334f4b3ec61Sdh155122
3357c478bd9Sstevel@tonic-gate void
spdsock_ddi_destroy(void)3367c478bd9Sstevel@tonic-gate spdsock_ddi_destroy(void)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate vmem_destroy(spdsock_vmem);
339f4b3ec61Sdh155122
340f4b3ec61Sdh155122 netstack_unregister(NS_SPDSOCK);
341f4b3ec61Sdh155122 }
342f4b3ec61Sdh155122
343*134a1f4eSCasper H.S. Dik /*
344*134a1f4eSCasper H.S. Dik * Do pre-removal cleanup.
345*134a1f4eSCasper H.S. Dik */
346*134a1f4eSCasper H.S. Dik /* ARGSUSED */
347*134a1f4eSCasper H.S. Dik static void
spdsock_stack_shutdown(netstackid_t stackid,void * arg)348*134a1f4eSCasper H.S. Dik spdsock_stack_shutdown(netstackid_t stackid, void *arg)
349*134a1f4eSCasper H.S. Dik {
350*134a1f4eSCasper H.S. Dik spd_stack_t *spds = (spd_stack_t *)arg;
351*134a1f4eSCasper H.S. Dik
352*134a1f4eSCasper H.S. Dik if (spds->spds_mp_algs != NULL) {
353*134a1f4eSCasper H.S. Dik freemsg(spds->spds_mp_algs);
354*134a1f4eSCasper H.S. Dik spds->spds_mp_algs = NULL;
355*134a1f4eSCasper H.S. Dik }
356*134a1f4eSCasper H.S. Dik }
357*134a1f4eSCasper H.S. Dik
358f4b3ec61Sdh155122 /* ARGSUSED */
359f4b3ec61Sdh155122 static void
spdsock_stack_fini(netstackid_t stackid,void * arg)360f4b3ec61Sdh155122 spdsock_stack_fini(netstackid_t stackid, void *arg)
361f4b3ec61Sdh155122 {
362f4b3ec61Sdh155122 spd_stack_t *spds = (spd_stack_t *)arg;
363f4b3ec61Sdh155122
364*134a1f4eSCasper H.S. Dik ASSERT(spds->spds_mp_algs == NULL);
365f4b3ec61Sdh155122 mutex_destroy(&spds->spds_param_lock);
366f4b3ec61Sdh155122 mutex_destroy(&spds->spds_alg_lock);
367f4b3ec61Sdh155122 nd_free(&spds->spds_g_nd);
368f4b3ec61Sdh155122 kmem_free(spds->spds_params, sizeof (lcl_param_arr));
369f4b3ec61Sdh155122 spds->spds_params = NULL;
370f4b3ec61Sdh155122
371f4b3ec61Sdh155122 kmem_free(spds, sizeof (*spds));
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate * NOTE: large quantities of this should be shared with keysock.
3767c478bd9Sstevel@tonic-gate * Would be nice to combine some of this into a common module, but
3777c478bd9Sstevel@tonic-gate * not possible given time pressures.
3787c478bd9Sstevel@tonic-gate */
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate * High-level reality checking of extensions.
3827c478bd9Sstevel@tonic-gate */
3837c478bd9Sstevel@tonic-gate /* ARGSUSED */ /* XXX */
3847c478bd9Sstevel@tonic-gate static boolean_t
ext_check(spd_ext_t * ext)3857c478bd9Sstevel@tonic-gate ext_check(spd_ext_t *ext)
3867c478bd9Sstevel@tonic-gate {
3878810c16bSdanmcd spd_if_t *tunname = (spd_if_t *)ext;
3888810c16bSdanmcd int i;
3898810c16bSdanmcd char *idstr;
3907c478bd9Sstevel@tonic-gate
3918810c16bSdanmcd if (ext->spd_ext_type == SPD_EXT_TUN_NAME) {
3928810c16bSdanmcd /* (NOTE: Modified from SADB_EXT_IDENTITY..) */
3938810c16bSdanmcd
3948810c16bSdanmcd /*
3958810c16bSdanmcd * Make sure the strings in these identities are
3968810c16bSdanmcd * null-terminated. Let's "proactively" null-terminate the
3978810c16bSdanmcd * string at the last byte if it's not terminated sooner.
3988810c16bSdanmcd */
3998810c16bSdanmcd i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t);
4008810c16bSdanmcd idstr = (char *)(tunname + 1);
4018810c16bSdanmcd while (*idstr != '\0' && i > 0) {
4028810c16bSdanmcd i--;
4038810c16bSdanmcd idstr++;
4048810c16bSdanmcd }
4058810c16bSdanmcd if (i == 0) {
4068810c16bSdanmcd /*
4078810c16bSdanmcd * I.e., if the bozo user didn't NULL-terminate the
4088810c16bSdanmcd * string...
4098810c16bSdanmcd */
4108810c16bSdanmcd idstr--;
4118810c16bSdanmcd *idstr = '\0';
4128810c16bSdanmcd }
4138810c16bSdanmcd }
4147c478bd9Sstevel@tonic-gate return (B_TRUE); /* For now... */
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate /* Return values for spdsock_get_ext(). */
4207c478bd9Sstevel@tonic-gate #define KGE_OK 0
4217c478bd9Sstevel@tonic-gate #define KGE_DUP 1
4227c478bd9Sstevel@tonic-gate #define KGE_UNK 2
4237c478bd9Sstevel@tonic-gate #define KGE_LEN 3
4247c478bd9Sstevel@tonic-gate #define KGE_CHK 4
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate * Parse basic extension headers and return in the passed-in pointer vector.
4287c478bd9Sstevel@tonic-gate * Return values include:
4297c478bd9Sstevel@tonic-gate *
4307c478bd9Sstevel@tonic-gate * KGE_OK Everything's nice and parsed out.
4317c478bd9Sstevel@tonic-gate * If there are no extensions, place NULL in extv[0].
4327c478bd9Sstevel@tonic-gate * KGE_DUP There is a duplicate extension.
4337c478bd9Sstevel@tonic-gate * First instance in appropriate bin. First duplicate in
4347c478bd9Sstevel@tonic-gate * extv[0].
4357c478bd9Sstevel@tonic-gate * KGE_UNK Unknown extension type encountered. extv[0] contains
4367c478bd9Sstevel@tonic-gate * unknown header.
4377c478bd9Sstevel@tonic-gate * KGE_LEN Extension length error.
4387c478bd9Sstevel@tonic-gate * KGE_CHK High-level reality check failed on specific extension.
4397c478bd9Sstevel@tonic-gate *
4407c478bd9Sstevel@tonic-gate * My apologies for some of the pointer arithmetic in here. I'm thinking
4417c478bd9Sstevel@tonic-gate * like an assembly programmer, yet trying to make the compiler happy.
4427c478bd9Sstevel@tonic-gate */
4437c478bd9Sstevel@tonic-gate static int
spdsock_get_ext(spd_ext_t * extv[],spd_msg_t * basehdr,uint_t msgsize)4447c478bd9Sstevel@tonic-gate spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate /* Use extv[0] as the "current working pointer". */
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate extv[0] = (spd_ext_t *)(basehdr + 1);
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) {
4537c478bd9Sstevel@tonic-gate /* Check for unknown headers. */
4547c478bd9Sstevel@tonic-gate if (extv[0]->spd_ext_type == 0 ||
4557c478bd9Sstevel@tonic-gate extv[0]->spd_ext_type > SPD_EXT_MAX)
4567c478bd9Sstevel@tonic-gate return (KGE_UNK);
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate * Check length. Use uint64_t because extlen is in units
4607c478bd9Sstevel@tonic-gate * of 64-bit words. If length goes beyond the msgsize,
4617c478bd9Sstevel@tonic-gate * return an error. (Zero length also qualifies here.)
4627c478bd9Sstevel@tonic-gate */
4637c478bd9Sstevel@tonic-gate if (extv[0]->spd_ext_len == 0 ||
4647c478bd9Sstevel@tonic-gate (void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) >
4657c478bd9Sstevel@tonic-gate (void *)((uint8_t *)basehdr + msgsize))
4667c478bd9Sstevel@tonic-gate return (KGE_LEN);
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate /* Check for redundant headers. */
4697c478bd9Sstevel@tonic-gate if (extv[extv[0]->spd_ext_type] != NULL)
4707c478bd9Sstevel@tonic-gate return (KGE_DUP);
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate * Reality check the extension if possible at the spdsock
4747c478bd9Sstevel@tonic-gate * level.
4757c478bd9Sstevel@tonic-gate */
4767c478bd9Sstevel@tonic-gate if (!ext_check(extv[0]))
4777c478bd9Sstevel@tonic-gate return (KGE_CHK);
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate /* If I make it here, assign the appropriate bin. */
4807c478bd9Sstevel@tonic-gate extv[extv[0]->spd_ext_type] = extv[0];
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate /* Advance pointer (See above for uint64_t ptr reasoning.) */
4837c478bd9Sstevel@tonic-gate extv[0] = (spd_ext_t *)
4847c478bd9Sstevel@tonic-gate ((uint64_t *)extv[0] + extv[0]->spd_ext_len);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate /* Everything's cool. */
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate * If extv[0] == NULL, then there are no extension headers in this
4917c478bd9Sstevel@tonic-gate * message. Ensure that this is the case.
4927c478bd9Sstevel@tonic-gate */
4937c478bd9Sstevel@tonic-gate if (extv[0] == (spd_ext_t *)(basehdr + 1))
4947c478bd9Sstevel@tonic-gate extv[0] = NULL;
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate return (KGE_OK);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate static const int bad_ext_diag[] = {
5007c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_LCLPORT,
5017c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_REMPORT,
5027c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_PROTO,
5037c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_LCLADDR,
5047c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_REMADDR,
5057c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_ACTION,
5067c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_RULE,
5077c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_RULESET,
5087c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE
5097c478bd9Sstevel@tonic-gate };
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate static const int dup_ext_diag[] = {
5127c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_LCLPORT,
5137c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_REMPORT,
5147c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_PROTO,
5157c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_LCLADDR,
5167c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_REMADDR,
5177c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_ACTION,
5187c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_RULE,
5197c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_RULESET,
5207c478bd9Sstevel@tonic-gate SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE
5217c478bd9Sstevel@tonic-gate };
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate * Transmit a PF_POLICY error message to the instance either pointed to
5257c478bd9Sstevel@tonic-gate * by ks, the instance with serial number serial, or more, depending.
5267c478bd9Sstevel@tonic-gate *
5277c478bd9Sstevel@tonic-gate * The faulty message (or a reasonable facsimile thereof) is in mp.
5287c478bd9Sstevel@tonic-gate * This function will free mp or recycle it for delivery, thereby causing
5297c478bd9Sstevel@tonic-gate * the stream head to free it.
5307c478bd9Sstevel@tonic-gate */
5317c478bd9Sstevel@tonic-gate static void
spdsock_error(queue_t * q,mblk_t * mp,int error,int diagnostic)5327c478bd9Sstevel@tonic-gate spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate ASSERT(mp->b_datap->db_type == M_DATA);
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate if (spmsg->spd_msg_type < SPD_MIN ||
5397c478bd9Sstevel@tonic-gate spmsg->spd_msg_type > SPD_MAX)
5407c478bd9Sstevel@tonic-gate spmsg->spd_msg_type = SPD_RESERVED;
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate * Strip out extension headers.
5447c478bd9Sstevel@tonic-gate */
5457c478bd9Sstevel@tonic-gate ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim);
5467c478bd9Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (*spmsg);
5477c478bd9Sstevel@tonic-gate spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t));
5487c478bd9Sstevel@tonic-gate spmsg->spd_msg_errno = (uint8_t)error;
5497c478bd9Sstevel@tonic-gate spmsg->spd_msg_diagnostic = (uint16_t)diagnostic;
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate qreply(q, mp);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate static void
spdsock_diag(queue_t * q,mblk_t * mp,int diagnostic)5557c478bd9Sstevel@tonic-gate spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate spdsock_error(q, mp, EINVAL, diagnostic);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate static void
spd_echo(queue_t * q,mblk_t * mp)5617c478bd9Sstevel@tonic-gate spd_echo(queue_t *q, mblk_t *mp)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate qreply(q, mp);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate
5668810c16bSdanmcd /*
5678810c16bSdanmcd * Do NOT consume a reference to itp.
5688810c16bSdanmcd */
569f4b3ec61Sdh155122 /*ARGSUSED*/
5707c478bd9Sstevel@tonic-gate static void
spdsock_flush_node(ipsec_tun_pol_t * itp,void * cookie,netstack_t * ns)571f4b3ec61Sdh155122 spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns)
5728810c16bSdanmcd {
5738810c16bSdanmcd boolean_t active = (boolean_t)cookie;
5748810c16bSdanmcd ipsec_policy_head_t *iph;
5758810c16bSdanmcd
5768810c16bSdanmcd iph = active ? itp->itp_policy : itp->itp_inactive;
5778810c16bSdanmcd IPPH_REFHOLD(iph);
5788810c16bSdanmcd mutex_enter(&itp->itp_lock);
57901ac885fSDan McDonald spdsock_flush_one(iph, ns); /* Releases iph refhold. */
5808810c16bSdanmcd if (active)
5818810c16bSdanmcd itp->itp_flags &= ~ITPF_PFLAGS;
5828810c16bSdanmcd else
5838810c16bSdanmcd itp->itp_flags &= ~ITPF_IFLAGS;
5848810c16bSdanmcd mutex_exit(&itp->itp_lock);
58501ac885fSDan McDonald /* SPD_FLUSH is worth a tunnel MTU check. */
58601ac885fSDan McDonald update_iptun_policy(itp);
5878810c16bSdanmcd }
5888810c16bSdanmcd
5898810c16bSdanmcd /*
5908810c16bSdanmcd * Clear out one polhead.
5918810c16bSdanmcd */
5928810c16bSdanmcd static void
spdsock_flush_one(ipsec_policy_head_t * iph,netstack_t * ns)593f4b3ec61Sdh155122 spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_WRITER);
596f4b3ec61Sdh155122 ipsec_polhead_flush(iph, ns);
5977c478bd9Sstevel@tonic-gate rw_exit(&iph->iph_lock);
598f4b3ec61Sdh155122 IPPH_REFRELE(iph, ns);
5998810c16bSdanmcd }
6008810c16bSdanmcd
6018810c16bSdanmcd static void
spdsock_flush(queue_t * q,ipsec_policy_head_t * iph,ipsec_tun_pol_t * itp,mblk_t * mp)602799bd290Spwernau spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, ipsec_tun_pol_t *itp,
603799bd290Spwernau mblk_t *mp)
6048810c16bSdanmcd {
6058810c16bSdanmcd boolean_t active;
606f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
607db061fdfSdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack;
608005d3febSMarek Pospisil uint32_t auditing = AU_AUDITING();
6098810c16bSdanmcd
6108810c16bSdanmcd if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) {
611db061fdfSdanmcd spdsock_flush_one(iph, ns);
612005d3febSMarek Pospisil if (auditing) {
613799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
614de8c4a14SErik Nordmark cred_t *cr;
615de8c4a14SErik Nordmark pid_t cpid;
616799bd290Spwernau
617de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
618799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
619de8c4a14SErik Nordmark audit_pf_policy(SPD_FLUSH, cr, ns,
620de8c4a14SErik Nordmark ITP_NAME(itp), active, 0, cpid);
621799bd290Spwernau }
6228810c16bSdanmcd } else {
6238810c16bSdanmcd active = (iph == ALL_ACTIVE_POLHEADS);
6248810c16bSdanmcd
6258810c16bSdanmcd /* First flush the global policy. */
626f4b3ec61Sdh155122 spdsock_flush_one(active ? ipsec_system_policy(ns) :
627f4b3ec61Sdh155122 ipsec_inactive_policy(ns), ns);
628005d3febSMarek Pospisil if (auditing) {
629de8c4a14SErik Nordmark cred_t *cr;
630de8c4a14SErik Nordmark pid_t cpid;
631de8c4a14SErik Nordmark
632de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
633de8c4a14SErik Nordmark audit_pf_policy(SPD_FLUSH, cr, ns, NULL,
634de8c4a14SErik Nordmark active, 0, cpid);
635799bd290Spwernau }
6368810c16bSdanmcd /* Then flush every tunnel's appropriate one. */
637db061fdfSdanmcd itp_walk(spdsock_flush_node, (void *)active, ns);
638005d3febSMarek Pospisil if (auditing) {
639de8c4a14SErik Nordmark cred_t *cr;
640de8c4a14SErik Nordmark pid_t cpid;
641de8c4a14SErik Nordmark
642de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
643de8c4a14SErik Nordmark audit_pf_policy(SPD_FLUSH, cr, ns,
644de8c4a14SErik Nordmark "all tunnels", active, 0, cpid);
645de8c4a14SErik Nordmark }
6468810c16bSdanmcd }
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate spd_echo(q, mp);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate static boolean_t
spdsock_ext_to_sel(spd_ext_t ** extv,ipsec_selkey_t * sel,int * diag)6527c478bd9Sstevel@tonic-gate spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag)
6537c478bd9Sstevel@tonic-gate {
6547c478bd9Sstevel@tonic-gate bzero(sel, sizeof (*sel));
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate if (extv[SPD_EXT_PROTO] != NULL) {
6577c478bd9Sstevel@tonic-gate struct spd_proto *pr =
6587c478bd9Sstevel@tonic-gate (struct spd_proto *)extv[SPD_EXT_PROTO];
6597c478bd9Sstevel@tonic-gate sel->ipsl_proto = pr->spd_proto_number;
6607c478bd9Sstevel@tonic-gate sel->ipsl_valid |= IPSL_PROTOCOL;
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate if (extv[SPD_EXT_LCLPORT] != NULL) {
6637c478bd9Sstevel@tonic-gate struct spd_portrange *pr =
6647c478bd9Sstevel@tonic-gate (struct spd_portrange *)extv[SPD_EXT_LCLPORT];
6657c478bd9Sstevel@tonic-gate sel->ipsl_lport = pr->spd_ports_minport;
6667c478bd9Sstevel@tonic-gate sel->ipsl_valid |= IPSL_LOCAL_PORT;
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate if (extv[SPD_EXT_REMPORT] != NULL) {
6697c478bd9Sstevel@tonic-gate struct spd_portrange *pr =
6707c478bd9Sstevel@tonic-gate (struct spd_portrange *)extv[SPD_EXT_REMPORT];
6717c478bd9Sstevel@tonic-gate sel->ipsl_rport = pr->spd_ports_minport;
6727c478bd9Sstevel@tonic-gate sel->ipsl_valid |= IPSL_REMOTE_PORT;
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) {
6767c478bd9Sstevel@tonic-gate struct spd_typecode *tc=
6777c478bd9Sstevel@tonic-gate (struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE];
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate sel->ipsl_valid |= IPSL_ICMP_TYPE;
6807c478bd9Sstevel@tonic-gate sel->ipsl_icmp_type = tc->spd_typecode_type;
6817c478bd9Sstevel@tonic-gate if (tc->spd_typecode_type_end < tc->spd_typecode_type)
6827c478bd9Sstevel@tonic-gate sel->ipsl_icmp_type_end = tc->spd_typecode_type;
6837c478bd9Sstevel@tonic-gate else
6847c478bd9Sstevel@tonic-gate sel->ipsl_icmp_type_end = tc->spd_typecode_type_end;
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate if (tc->spd_typecode_code != 255) {
6877c478bd9Sstevel@tonic-gate sel->ipsl_valid |= IPSL_ICMP_CODE;
6887c478bd9Sstevel@tonic-gate sel->ipsl_icmp_code = tc->spd_typecode_code;
6897c478bd9Sstevel@tonic-gate if (tc->spd_typecode_code_end < tc->spd_typecode_code)
6907c478bd9Sstevel@tonic-gate sel->ipsl_icmp_code_end = tc->spd_typecode_code;
6917c478bd9Sstevel@tonic-gate else
6927c478bd9Sstevel@tonic-gate sel->ipsl_icmp_code_end =
6937c478bd9Sstevel@tonic-gate tc->spd_typecode_code_end;
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate #define ADDR2SEL(sel, extv, field, pfield, extn, bit) \
6977c478bd9Sstevel@tonic-gate if ((extv)[(extn)] != NULL) { \
6987c478bd9Sstevel@tonic-gate uint_t addrlen; \
6997c478bd9Sstevel@tonic-gate struct spd_address *ap = \
7007c478bd9Sstevel@tonic-gate (struct spd_address *)((extv)[(extn)]); \
7017c478bd9Sstevel@tonic-gate addrlen = (ap->spd_address_af == AF_INET6) ? \
7027c478bd9Sstevel@tonic-gate IPV6_ADDR_LEN : IP_ADDR_LEN; \
7037c478bd9Sstevel@tonic-gate if (SPD_64TO8(ap->spd_address_len) < \
7047c478bd9Sstevel@tonic-gate (addrlen + sizeof (*ap))) { \
7057c478bd9Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN; \
7067c478bd9Sstevel@tonic-gate return (B_FALSE); \
7077c478bd9Sstevel@tonic-gate } \
7087c478bd9Sstevel@tonic-gate bcopy((ap+1), &((sel)->field), addrlen); \
7097c478bd9Sstevel@tonic-gate (sel)->pfield = ap->spd_address_prefixlen; \
7107c478bd9Sstevel@tonic-gate (sel)->ipsl_valid |= (bit); \
7117c478bd9Sstevel@tonic-gate (sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ? \
7127c478bd9Sstevel@tonic-gate IPSL_IPV6 : IPSL_IPV4; \
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate
7157c478bd9Sstevel@tonic-gate ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen,
7167c478bd9Sstevel@tonic-gate SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR);
7177c478bd9Sstevel@tonic-gate ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen,
7187c478bd9Sstevel@tonic-gate SPD_EXT_REMADDR, IPSL_REMOTE_ADDR);
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) ==
7217c478bd9Sstevel@tonic-gate (IPSL_IPV6|IPSL_IPV4)) {
7227c478bd9Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_MIXED_AF;
7237c478bd9Sstevel@tonic-gate return (B_FALSE);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate #undef ADDR2SEL
7277c478bd9Sstevel@tonic-gate
7287c478bd9Sstevel@tonic-gate return (B_TRUE);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate
7317c478bd9Sstevel@tonic-gate static boolean_t
spd_convert_type(uint32_t type,ipsec_act_t * act)7327c478bd9Sstevel@tonic-gate spd_convert_type(uint32_t type, ipsec_act_t *act)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate switch (type) {
7357c478bd9Sstevel@tonic-gate case SPD_ACTTYPE_DROP:
7367c478bd9Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_DISCARD;
7377c478bd9Sstevel@tonic-gate return (B_TRUE);
7387c478bd9Sstevel@tonic-gate
7397c478bd9Sstevel@tonic-gate case SPD_ACTTYPE_PASS:
7407c478bd9Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_CLEAR;
7417c478bd9Sstevel@tonic-gate return (B_TRUE);
7427c478bd9Sstevel@tonic-gate
7437c478bd9Sstevel@tonic-gate case SPD_ACTTYPE_IPSEC:
7447c478bd9Sstevel@tonic-gate act->ipa_type = IPSEC_ACT_APPLY;
7457c478bd9Sstevel@tonic-gate return (B_TRUE);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate return (B_FALSE);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate
7507c478bd9Sstevel@tonic-gate static boolean_t
spd_convert_flags(uint32_t flags,ipsec_act_t * act)7517c478bd9Sstevel@tonic-gate spd_convert_flags(uint32_t flags, ipsec_act_t *act)
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate /*
7547c478bd9Sstevel@tonic-gate * Note use of !! for boolean canonicalization.
7557c478bd9Sstevel@tonic-gate */
7567c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH);
7577c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP);
7587c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA);
7597c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE);
7607c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE);
7617c478bd9Sstevel@tonic-gate return (B_TRUE);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate
7647c478bd9Sstevel@tonic-gate static void
spdsock_reset_act(ipsec_act_t * act)7657c478bd9Sstevel@tonic-gate spdsock_reset_act(ipsec_act_t *act)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate bzero(act, sizeof (*act));
7687c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS;
7697c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS;
7707c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS;
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate
7737c478bd9Sstevel@tonic-gate /*
7747c478bd9Sstevel@tonic-gate * Sanity check action against reality, and shrink-wrap key sizes..
7757c478bd9Sstevel@tonic-gate */
7767c478bd9Sstevel@tonic-gate static boolean_t
spdsock_check_action(ipsec_act_t * act,boolean_t tunnel_polhead,int * diag,spd_stack_t * spds)777f4b3ec61Sdh155122 spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag,
778f4b3ec61Sdh155122 spd_stack_t *spds)
7797c478bd9Sstevel@tonic-gate {
7808810c16bSdanmcd if (tunnel_polhead && act->ipa_apply.ipp_use_unique) {
7818810c16bSdanmcd *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
7828810c16bSdanmcd return (B_FALSE);
7838810c16bSdanmcd }
7847c478bd9Sstevel@tonic-gate if ((act->ipa_type != IPSEC_ACT_APPLY) &&
7857c478bd9Sstevel@tonic-gate (act->ipa_apply.ipp_use_ah ||
7867c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_use_esp ||
7877c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_use_espa ||
7887c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_use_se ||
7897c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_use_unique)) {
7907c478bd9Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
7917c478bd9Sstevel@tonic-gate return (B_FALSE);
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate if ((act->ipa_type == IPSEC_ACT_APPLY) &&
7947c478bd9Sstevel@tonic-gate !act->ipa_apply.ipp_use_ah &&
7957c478bd9Sstevel@tonic-gate !act->ipa_apply.ipp_use_esp) {
7967c478bd9Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS;
7977c478bd9Sstevel@tonic-gate return (B_FALSE);
7987c478bd9Sstevel@tonic-gate }
799f4b3ec61Sdh155122 return (ipsec_check_action(act, diag, spds->spds_netstack));
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate
8027c478bd9Sstevel@tonic-gate /*
8037c478bd9Sstevel@tonic-gate * We may be short a few error checks here..
8047c478bd9Sstevel@tonic-gate */
8057c478bd9Sstevel@tonic-gate static boolean_t
spdsock_ext_to_actvec(spd_ext_t ** extv,ipsec_act_t ** actpp,uint_t * nactp,int * diag,spd_stack_t * spds)8067c478bd9Sstevel@tonic-gate spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp,
807f4b3ec61Sdh155122 int *diag, spd_stack_t *spds)
8087c478bd9Sstevel@tonic-gate {
8097c478bd9Sstevel@tonic-gate struct spd_ext_actions *sactp =
8107c478bd9Sstevel@tonic-gate (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
8117c478bd9Sstevel@tonic-gate ipsec_act_t act, *actp, *endactp;
8127c478bd9Sstevel@tonic-gate struct spd_attribute *attrp, *endattrp;
8137c478bd9Sstevel@tonic-gate uint64_t *endp;
8147c478bd9Sstevel@tonic-gate int nact;
8158810c16bSdanmcd boolean_t tunnel_polhead;
8168810c16bSdanmcd
8178810c16bSdanmcd tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL &&
8188810c16bSdanmcd (((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags &
8198810c16bSdanmcd SPD_RULE_FLAG_TUNNEL));
8207c478bd9Sstevel@tonic-gate
8217c478bd9Sstevel@tonic-gate *actpp = NULL;
8227c478bd9Sstevel@tonic-gate *nactp = 0;
8237c478bd9Sstevel@tonic-gate
8247c478bd9Sstevel@tonic-gate if (sactp == NULL) {
8257c478bd9Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_NO_ACTION_EXT;
8267c478bd9Sstevel@tonic-gate return (B_FALSE);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate
8297c478bd9Sstevel@tonic-gate /*
8307c478bd9Sstevel@tonic-gate * Parse the "action" extension and convert into an action chain.
8317c478bd9Sstevel@tonic-gate */
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate nact = sactp->spd_actions_count;
8347c478bd9Sstevel@tonic-gate
8357c478bd9Sstevel@tonic-gate endp = (uint64_t *)sactp;
8367c478bd9Sstevel@tonic-gate endp += sactp->spd_actions_len;
8377c478bd9Sstevel@tonic-gate endattrp = (struct spd_attribute *)endp;
8387c478bd9Sstevel@tonic-gate
8397c478bd9Sstevel@tonic-gate actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP);
8407c478bd9Sstevel@tonic-gate if (actp == NULL) {
8417c478bd9Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_NO_MEM;
8427c478bd9Sstevel@tonic-gate return (B_FALSE);
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate *actpp = actp;
8457c478bd9Sstevel@tonic-gate *nactp = nact;
8467c478bd9Sstevel@tonic-gate endactp = actp + nact;
8477c478bd9Sstevel@tonic-gate
8487c478bd9Sstevel@tonic-gate spdsock_reset_act(&act);
8497c478bd9Sstevel@tonic-gate attrp = (struct spd_attribute *)(&sactp[1]);
8507c478bd9Sstevel@tonic-gate
8517c478bd9Sstevel@tonic-gate for (; attrp < endattrp; attrp++) {
8527c478bd9Sstevel@tonic-gate switch (attrp->spd_attr_tag) {
8537c478bd9Sstevel@tonic-gate case SPD_ATTR_NOP:
8547c478bd9Sstevel@tonic-gate break;
8557c478bd9Sstevel@tonic-gate
8567c478bd9Sstevel@tonic-gate case SPD_ATTR_EMPTY:
8577c478bd9Sstevel@tonic-gate spdsock_reset_act(&act);
8587c478bd9Sstevel@tonic-gate break;
8597c478bd9Sstevel@tonic-gate
8607c478bd9Sstevel@tonic-gate case SPD_ATTR_END:
8617c478bd9Sstevel@tonic-gate attrp = endattrp;
8627c478bd9Sstevel@tonic-gate /* FALLTHRU */
8637c478bd9Sstevel@tonic-gate case SPD_ATTR_NEXT:
8647c478bd9Sstevel@tonic-gate if (actp >= endactp) {
8657c478bd9Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
8667c478bd9Sstevel@tonic-gate goto fail;
8677c478bd9Sstevel@tonic-gate }
868f4b3ec61Sdh155122 if (!spdsock_check_action(&act, tunnel_polhead,
869f4b3ec61Sdh155122 diag, spds))
8707c478bd9Sstevel@tonic-gate goto fail;
8717c478bd9Sstevel@tonic-gate *actp++ = act;
8728810c16bSdanmcd spdsock_reset_act(&act);
8737c478bd9Sstevel@tonic-gate break;
8747c478bd9Sstevel@tonic-gate
8757c478bd9Sstevel@tonic-gate case SPD_ATTR_TYPE:
8767c478bd9Sstevel@tonic-gate if (!spd_convert_type(attrp->spd_attr_value, &act)) {
8777c478bd9Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE;
8787c478bd9Sstevel@tonic-gate goto fail;
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate break;
8817c478bd9Sstevel@tonic-gate
8827c478bd9Sstevel@tonic-gate case SPD_ATTR_FLAGS:
8838810c16bSdanmcd if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) {
8848810c16bSdanmcd /*
8858810c16bSdanmcd * Set "sa unique" for transport-mode
8868810c16bSdanmcd * tunnels whether we want to or not.
8878810c16bSdanmcd */
8888810c16bSdanmcd attrp->spd_attr_value |= SPD_APPLY_UNIQUE;
8898810c16bSdanmcd }
8907c478bd9Sstevel@tonic-gate if (!spd_convert_flags(attrp->spd_attr_value, &act)) {
8917c478bd9Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS;
8927c478bd9Sstevel@tonic-gate goto fail;
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate break;
8957c478bd9Sstevel@tonic-gate
8967c478bd9Sstevel@tonic-gate case SPD_ATTR_AH_AUTH:
8978810c16bSdanmcd if (attrp->spd_attr_value == 0) {
8988810c16bSdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG;
8998810c16bSdanmcd goto fail;
9008810c16bSdanmcd }
9017c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value;
9027c478bd9Sstevel@tonic-gate break;
9037c478bd9Sstevel@tonic-gate
9047c478bd9Sstevel@tonic-gate case SPD_ATTR_ESP_ENCR:
9058810c16bSdanmcd if (attrp->spd_attr_value == 0) {
9068810c16bSdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG;
9078810c16bSdanmcd goto fail;
9088810c16bSdanmcd }
9097c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value;
9107c478bd9Sstevel@tonic-gate break;
9117c478bd9Sstevel@tonic-gate
9127c478bd9Sstevel@tonic-gate case SPD_ATTR_ESP_AUTH:
9138810c16bSdanmcd if (attrp->spd_attr_value == 0) {
9148810c16bSdanmcd *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG;
9158810c16bSdanmcd goto fail;
9168810c16bSdanmcd }
9177c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value;
9187c478bd9Sstevel@tonic-gate break;
9197c478bd9Sstevel@tonic-gate
9207c478bd9Sstevel@tonic-gate case SPD_ATTR_ENCR_MINBITS:
9217c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value;
9227c478bd9Sstevel@tonic-gate break;
9237c478bd9Sstevel@tonic-gate
9247c478bd9Sstevel@tonic-gate case SPD_ATTR_ENCR_MAXBITS:
9257c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value;
9267c478bd9Sstevel@tonic-gate break;
9277c478bd9Sstevel@tonic-gate
9287c478bd9Sstevel@tonic-gate case SPD_ATTR_AH_MINBITS:
9297c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value;
9307c478bd9Sstevel@tonic-gate break;
9317c478bd9Sstevel@tonic-gate
9327c478bd9Sstevel@tonic-gate case SPD_ATTR_AH_MAXBITS:
9337c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value;
9347c478bd9Sstevel@tonic-gate break;
9357c478bd9Sstevel@tonic-gate
9367c478bd9Sstevel@tonic-gate case SPD_ATTR_ESPA_MINBITS:
9377c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value;
9387c478bd9Sstevel@tonic-gate break;
9397c478bd9Sstevel@tonic-gate
9407c478bd9Sstevel@tonic-gate case SPD_ATTR_ESPA_MAXBITS:
9417c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value;
9427c478bd9Sstevel@tonic-gate break;
9437c478bd9Sstevel@tonic-gate
9447c478bd9Sstevel@tonic-gate case SPD_ATTR_LIFE_SOFT_TIME:
9457c478bd9Sstevel@tonic-gate case SPD_ATTR_LIFE_HARD_TIME:
9467c478bd9Sstevel@tonic-gate case SPD_ATTR_LIFE_SOFT_BYTES:
9477c478bd9Sstevel@tonic-gate case SPD_ATTR_LIFE_HARD_BYTES:
9487c478bd9Sstevel@tonic-gate break;
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate case SPD_ATTR_KM_PROTO:
9517c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_km_proto = attrp->spd_attr_value;
9527c478bd9Sstevel@tonic-gate break;
9537c478bd9Sstevel@tonic-gate
9547c478bd9Sstevel@tonic-gate case SPD_ATTR_KM_COOKIE:
9557c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value;
9567c478bd9Sstevel@tonic-gate break;
9577c478bd9Sstevel@tonic-gate
9587c478bd9Sstevel@tonic-gate case SPD_ATTR_REPLAY_DEPTH:
9597c478bd9Sstevel@tonic-gate act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value;
9607c478bd9Sstevel@tonic-gate break;
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate if (actp != endactp) {
9647c478bd9Sstevel@tonic-gate *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT;
9657c478bd9Sstevel@tonic-gate goto fail;
9667c478bd9Sstevel@tonic-gate }
9677c478bd9Sstevel@tonic-gate
9687c478bd9Sstevel@tonic-gate return (B_TRUE);
9697c478bd9Sstevel@tonic-gate fail:
9707c478bd9Sstevel@tonic-gate ipsec_actvec_free(*actpp, nact);
9717c478bd9Sstevel@tonic-gate *actpp = NULL;
9727c478bd9Sstevel@tonic-gate return (B_FALSE);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate
9757c478bd9Sstevel@tonic-gate typedef struct
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate ipsec_policy_t *pol;
9787c478bd9Sstevel@tonic-gate int dir;
9797c478bd9Sstevel@tonic-gate } tmprule_t;
9807c478bd9Sstevel@tonic-gate
9817c478bd9Sstevel@tonic-gate static int
mkrule(ipsec_policy_head_t * iph,struct spd_rule * rule,ipsec_selkey_t * sel,ipsec_act_t * actp,int nact,uint_t dir,uint_t af,tmprule_t ** rp,uint64_t * index,spd_stack_t * spds)9827c478bd9Sstevel@tonic-gate mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule,
9837c478bd9Sstevel@tonic-gate ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af,
984f4b3ec61Sdh155122 tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate ipsec_policy_t *pol;
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4);
9897c478bd9Sstevel@tonic-gate sel->ipsl_valid |= af;
9907c478bd9Sstevel@tonic-gate
9918810c16bSdanmcd pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority,
992f4b3ec61Sdh155122 index, spds->spds_netstack);
9937c478bd9Sstevel@tonic-gate if (pol == NULL)
9947c478bd9Sstevel@tonic-gate return (ENOMEM);
9957c478bd9Sstevel@tonic-gate
9967c478bd9Sstevel@tonic-gate (*rp)->pol = pol;
9977c478bd9Sstevel@tonic-gate (*rp)->dir = dir;
9987c478bd9Sstevel@tonic-gate (*rp)++;
9997c478bd9Sstevel@tonic-gate
10007c478bd9Sstevel@tonic-gate if (!ipsec_check_policy(iph, pol, dir))
10017c478bd9Sstevel@tonic-gate return (EEXIST);
10027c478bd9Sstevel@tonic-gate
10037c478bd9Sstevel@tonic-gate rule->spd_rule_index = pol->ipsp_index;
10047c478bd9Sstevel@tonic-gate return (0);
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate
10077c478bd9Sstevel@tonic-gate static int
mkrulepair(ipsec_policy_head_t * iph,struct spd_rule * rule,ipsec_selkey_t * sel,ipsec_act_t * actp,int nact,uint_t dir,uint_t afs,tmprule_t ** rp,uint64_t * index,spd_stack_t * spds)10087c478bd9Sstevel@tonic-gate mkrulepair(ipsec_policy_head_t *iph, struct spd_rule *rule,
10097c478bd9Sstevel@tonic-gate ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t afs,
1010f4b3ec61Sdh155122 tmprule_t **rp, uint64_t *index, spd_stack_t *spds)
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate int error;
10137c478bd9Sstevel@tonic-gate
10147c478bd9Sstevel@tonic-gate if (afs & IPSL_IPV4) {
10158810c16bSdanmcd error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV4, rp,
1016f4b3ec61Sdh155122 index, spds);
10177c478bd9Sstevel@tonic-gate if (error != 0)
10187c478bd9Sstevel@tonic-gate return (error);
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate if (afs & IPSL_IPV6) {
10218810c16bSdanmcd error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV6, rp,
1022f4b3ec61Sdh155122 index, spds);
10237c478bd9Sstevel@tonic-gate if (error != 0)
10247c478bd9Sstevel@tonic-gate return (error);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate return (0);
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate
10297c478bd9Sstevel@tonic-gate
10307c478bd9Sstevel@tonic-gate static void
spdsock_addrule(queue_t * q,ipsec_policy_head_t * iph,mblk_t * mp,spd_ext_t ** extv,ipsec_tun_pol_t * itp)10318810c16bSdanmcd spdsock_addrule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
10328810c16bSdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp)
10337c478bd9Sstevel@tonic-gate {
10347c478bd9Sstevel@tonic-gate ipsec_selkey_t sel;
10357c478bd9Sstevel@tonic-gate ipsec_act_t *actp;
10367c478bd9Sstevel@tonic-gate uint_t nact;
10378810c16bSdanmcd int diag = 0, error, afs;
10387c478bd9Sstevel@tonic-gate struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
10397c478bd9Sstevel@tonic-gate tmprule_t rules[4], *rulep = &rules[0];
10408810c16bSdanmcd boolean_t tunnel_mode, empty_itp, active;
10418810c16bSdanmcd uint64_t *index = (itp == NULL) ? NULL : &itp->itp_next_policy_index;
1042f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
1043f4b3ec61Sdh155122 spd_stack_t *spds = ss->spdsock_spds;
1044005d3febSMarek Pospisil uint32_t auditing = AU_AUDITING();
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate if (rule == NULL) {
10477c478bd9Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
1048005d3febSMarek Pospisil if (auditing) {
1049799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1050de8c4a14SErik Nordmark cred_t *cr;
1051de8c4a14SErik Nordmark pid_t cpid;
1052799bd290Spwernau
1053de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1054799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1055de8c4a14SErik Nordmark audit_pf_policy(SPD_ADDRULE, cr,
1056799bd290Spwernau spds->spds_netstack, ITP_NAME(itp), active,
1057de8c4a14SErik Nordmark SPD_DIAGNOSTIC_NO_RULE_EXT, cpid);
1058799bd290Spwernau }
10597c478bd9Sstevel@tonic-gate return;
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate
10628810c16bSdanmcd tunnel_mode = (rule->spd_rule_flags & SPD_RULE_FLAG_TUNNEL);
10638810c16bSdanmcd
10648810c16bSdanmcd if (itp != NULL) {
10658810c16bSdanmcd mutex_enter(&itp->itp_lock);
10668810c16bSdanmcd ASSERT(itp->itp_policy == iph || itp->itp_inactive == iph);
10678810c16bSdanmcd active = (itp->itp_policy == iph);
10688810c16bSdanmcd if (ITP_P_ISACTIVE(itp, iph)) {
10698810c16bSdanmcd /* Check for mix-and-match of tunnel/transport. */
10708810c16bSdanmcd if ((tunnel_mode && !ITP_P_ISTUNNEL(itp, iph)) ||
10718810c16bSdanmcd (!tunnel_mode && ITP_P_ISTUNNEL(itp, iph))) {
10728810c16bSdanmcd mutex_exit(&itp->itp_lock);
10738810c16bSdanmcd spdsock_error(q, mp, EBUSY, 0);
10747c478bd9Sstevel@tonic-gate return;
10757c478bd9Sstevel@tonic-gate }
10768810c16bSdanmcd empty_itp = B_FALSE;
10778810c16bSdanmcd } else {
10788810c16bSdanmcd empty_itp = B_TRUE;
10798810c16bSdanmcd itp->itp_flags = active ? ITPF_P_ACTIVE : ITPF_I_ACTIVE;
10808810c16bSdanmcd if (tunnel_mode)
10818810c16bSdanmcd itp->itp_flags |= active ? ITPF_P_TUNNEL :
10828810c16bSdanmcd ITPF_I_TUNNEL;
10838810c16bSdanmcd }
10848810c16bSdanmcd } else {
10858810c16bSdanmcd empty_itp = B_FALSE;
10868810c16bSdanmcd }
10878810c16bSdanmcd
10888810c16bSdanmcd if (rule->spd_rule_index != 0) {
10898810c16bSdanmcd diag = SPD_DIAGNOSTIC_INVALID_RULE_INDEX;
10908810c16bSdanmcd error = EINVAL;
10918810c16bSdanmcd goto fail2;
10928810c16bSdanmcd }
10937c478bd9Sstevel@tonic-gate
10947c478bd9Sstevel@tonic-gate if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
10958810c16bSdanmcd error = EINVAL;
10968810c16bSdanmcd goto fail2;
10978810c16bSdanmcd }
10988810c16bSdanmcd
10998810c16bSdanmcd if (itp != NULL) {
11008810c16bSdanmcd if (tunnel_mode) {
11018810c16bSdanmcd if (sel.ipsl_valid &
11028810c16bSdanmcd (IPSL_REMOTE_PORT | IPSL_LOCAL_PORT)) {
11038810c16bSdanmcd itp->itp_flags |= active ?
11048810c16bSdanmcd ITPF_P_PER_PORT_SECURITY :
11058810c16bSdanmcd ITPF_I_PER_PORT_SECURITY;
11068810c16bSdanmcd }
11078810c16bSdanmcd } else {
11088810c16bSdanmcd /*
11098810c16bSdanmcd * For now, we don't allow transport-mode on a tunnel
11108810c16bSdanmcd * with ANY specific selectors. Bail if we have such
11118810c16bSdanmcd * a request.
11128810c16bSdanmcd */
11138810c16bSdanmcd if (sel.ipsl_valid & IPSL_WILDCARD) {
11148810c16bSdanmcd diag = SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS;
11158810c16bSdanmcd error = EINVAL;
11168810c16bSdanmcd goto fail2;
11178810c16bSdanmcd }
11188810c16bSdanmcd }
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate
1121f4b3ec61Sdh155122 if (!spdsock_ext_to_actvec(extv, &actp, &nact, &diag, spds)) {
11228810c16bSdanmcd error = EINVAL;
11238810c16bSdanmcd goto fail2;
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate /*
11267c478bd9Sstevel@tonic-gate * If no addresses were specified, add both.
11277c478bd9Sstevel@tonic-gate */
11287c478bd9Sstevel@tonic-gate afs = sel.ipsl_valid & (IPSL_IPV6|IPSL_IPV4);
11297c478bd9Sstevel@tonic-gate if (afs == 0)
11307c478bd9Sstevel@tonic-gate afs = (IPSL_IPV6|IPSL_IPV4);
11317c478bd9Sstevel@tonic-gate
11327c478bd9Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_WRITER);
11337c478bd9Sstevel@tonic-gate
11347c478bd9Sstevel@tonic-gate if (rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) {
11357c478bd9Sstevel@tonic-gate error = mkrulepair(iph, rule, &sel, actp, nact,
1136f4b3ec61Sdh155122 IPSEC_TYPE_OUTBOUND, afs, &rulep, index, spds);
11377c478bd9Sstevel@tonic-gate if (error != 0)
11387c478bd9Sstevel@tonic-gate goto fail;
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate
11417c478bd9Sstevel@tonic-gate if (rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) {
11427c478bd9Sstevel@tonic-gate error = mkrulepair(iph, rule, &sel, actp, nact,
1143f4b3ec61Sdh155122 IPSEC_TYPE_INBOUND, afs, &rulep, index, spds);
11447c478bd9Sstevel@tonic-gate if (error != 0)
11457c478bd9Sstevel@tonic-gate goto fail;
11467c478bd9Sstevel@tonic-gate }
11477c478bd9Sstevel@tonic-gate
1148f4b3ec61Sdh155122 while ((--rulep) >= &rules[0]) {
1149f4b3ec61Sdh155122 ipsec_enter_policy(iph, rulep->pol, rulep->dir,
1150f4b3ec61Sdh155122 spds->spds_netstack);
1151f4b3ec61Sdh155122 }
11527c478bd9Sstevel@tonic-gate rw_exit(&iph->iph_lock);
11538810c16bSdanmcd if (itp != NULL)
11548810c16bSdanmcd mutex_exit(&itp->itp_lock);
11557c478bd9Sstevel@tonic-gate
11567c478bd9Sstevel@tonic-gate ipsec_actvec_free(actp, nact);
11577c478bd9Sstevel@tonic-gate spd_echo(q, mp);
1158005d3febSMarek Pospisil if (auditing) {
1159799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1160de8c4a14SErik Nordmark cred_t *cr;
1161de8c4a14SErik Nordmark pid_t cpid;
1162799bd290Spwernau
1163de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1164799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1165de8c4a14SErik Nordmark audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
1166de8c4a14SErik Nordmark ITP_NAME(itp), active, 0, cpid);
1167799bd290Spwernau }
11687c478bd9Sstevel@tonic-gate return;
11697c478bd9Sstevel@tonic-gate
11707c478bd9Sstevel@tonic-gate fail:
11717c478bd9Sstevel@tonic-gate rw_exit(&iph->iph_lock);
1172bd670b35SErik Nordmark while ((--rulep) >= &rules[0])
1173bd670b35SErik Nordmark IPPOL_REFRELE(rulep->pol);
11747c478bd9Sstevel@tonic-gate ipsec_actvec_free(actp, nact);
11758810c16bSdanmcd fail2:
11768810c16bSdanmcd if (itp != NULL) {
11778810c16bSdanmcd if (empty_itp)
11788810c16bSdanmcd itp->itp_flags = 0;
11798810c16bSdanmcd mutex_exit(&itp->itp_lock);
11808810c16bSdanmcd }
11818810c16bSdanmcd spdsock_error(q, mp, error, diag);
1182005d3febSMarek Pospisil if (auditing) {
1183799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1184de8c4a14SErik Nordmark cred_t *cr;
1185de8c4a14SErik Nordmark pid_t cpid;
1186799bd290Spwernau
1187de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1188799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1189de8c4a14SErik Nordmark audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack,
1190de8c4a14SErik Nordmark ITP_NAME(itp), active, error, cpid);
1191799bd290Spwernau }
11927c478bd9Sstevel@tonic-gate }
11937c478bd9Sstevel@tonic-gate
11947c478bd9Sstevel@tonic-gate void
spdsock_deleterule(queue_t * q,ipsec_policy_head_t * iph,mblk_t * mp,spd_ext_t ** extv,ipsec_tun_pol_t * itp)11958810c16bSdanmcd spdsock_deleterule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
11968810c16bSdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp)
11977c478bd9Sstevel@tonic-gate {
11987c478bd9Sstevel@tonic-gate ipsec_selkey_t sel;
11997c478bd9Sstevel@tonic-gate struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE];
12008810c16bSdanmcd int err, diag = 0;
1201f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
1202db061fdfSdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack;
1203005d3febSMarek Pospisil uint32_t auditing = AU_AUDITING();
12047c478bd9Sstevel@tonic-gate
12057c478bd9Sstevel@tonic-gate if (rule == NULL) {
12067c478bd9Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT);
1207005d3febSMarek Pospisil if (auditing) {
1208799bd290Spwernau boolean_t active;
1209799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1210de8c4a14SErik Nordmark cred_t *cr;
1211de8c4a14SErik Nordmark pid_t cpid;
1212799bd290Spwernau
1213de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1214799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1215de8c4a14SErik Nordmark audit_pf_policy(SPD_DELETERULE, cr, ns,
1216db061fdfSdanmcd ITP_NAME(itp), active, SPD_DIAGNOSTIC_NO_RULE_EXT,
1217de8c4a14SErik Nordmark cpid);
1218799bd290Spwernau }
12197c478bd9Sstevel@tonic-gate return;
12207c478bd9Sstevel@tonic-gate }
12217c478bd9Sstevel@tonic-gate
12228810c16bSdanmcd /*
12238810c16bSdanmcd * Must enter itp_lock first to avoid deadlock. See tun.c's
12248810c16bSdanmcd * set_sec_simple() for the other case of itp_lock and iph_lock.
12258810c16bSdanmcd */
12268810c16bSdanmcd if (itp != NULL)
12278810c16bSdanmcd mutex_enter(&itp->itp_lock);
12288810c16bSdanmcd
12297c478bd9Sstevel@tonic-gate if (rule->spd_rule_index != 0) {
1230db061fdfSdanmcd if (ipsec_policy_delete_index(iph, rule->spd_rule_index, ns) !=
1231db061fdfSdanmcd 0) {
12328810c16bSdanmcd err = ESRCH;
12338810c16bSdanmcd goto fail;
12347c478bd9Sstevel@tonic-gate }
12357c478bd9Sstevel@tonic-gate } else {
12367c478bd9Sstevel@tonic-gate if (!spdsock_ext_to_sel(extv, &sel, &diag)) {
12378810c16bSdanmcd err = EINVAL; /* diag already set... */
12387c478bd9Sstevel@tonic-gate goto fail;
12397c478bd9Sstevel@tonic-gate }
12407c478bd9Sstevel@tonic-gate
12418810c16bSdanmcd if ((rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) &&
1242db061fdfSdanmcd !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_INBOUND, ns)) {
12438810c16bSdanmcd err = ESRCH;
12447c478bd9Sstevel@tonic-gate goto fail;
12457c478bd9Sstevel@tonic-gate }
12468810c16bSdanmcd
12478810c16bSdanmcd if ((rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) &&
1248db061fdfSdanmcd !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_OUTBOUND, ns)) {
12498810c16bSdanmcd err = ESRCH;
12508810c16bSdanmcd goto fail;
12518810c16bSdanmcd }
12528810c16bSdanmcd }
12538810c16bSdanmcd
12548810c16bSdanmcd if (itp != NULL) {
12558810c16bSdanmcd ASSERT(iph == itp->itp_policy || iph == itp->itp_inactive);
12568810c16bSdanmcd rw_enter(&iph->iph_lock, RW_READER);
12578810c16bSdanmcd if (avl_numnodes(&iph->iph_rulebyid) == 0) {
12588810c16bSdanmcd if (iph == itp->itp_policy)
12598810c16bSdanmcd itp->itp_flags &= ~ITPF_PFLAGS;
12608810c16bSdanmcd else
12618810c16bSdanmcd itp->itp_flags &= ~ITPF_IFLAGS;
12628810c16bSdanmcd }
12638810c16bSdanmcd /* Can exit locks in any order. */
12648810c16bSdanmcd rw_exit(&iph->iph_lock);
12658810c16bSdanmcd mutex_exit(&itp->itp_lock);
12667c478bd9Sstevel@tonic-gate }
12677c478bd9Sstevel@tonic-gate spd_echo(q, mp);
1268005d3febSMarek Pospisil if (auditing) {
1269799bd290Spwernau boolean_t active;
1270799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1271de8c4a14SErik Nordmark cred_t *cr;
1272de8c4a14SErik Nordmark pid_t cpid;
1273799bd290Spwernau
1274de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1275799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1276de8c4a14SErik Nordmark audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
1277de8c4a14SErik Nordmark active, 0, cpid);
1278799bd290Spwernau }
12797c478bd9Sstevel@tonic-gate return;
12807c478bd9Sstevel@tonic-gate fail:
12818810c16bSdanmcd if (itp != NULL)
12828810c16bSdanmcd mutex_exit(&itp->itp_lock);
12838810c16bSdanmcd spdsock_error(q, mp, err, diag);
1284005d3febSMarek Pospisil if (auditing) {
1285799bd290Spwernau boolean_t active;
1286799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1287de8c4a14SErik Nordmark cred_t *cr;
1288de8c4a14SErik Nordmark pid_t cpid;
1289799bd290Spwernau
1290de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1291799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1292de8c4a14SErik Nordmark audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp),
1293de8c4a14SErik Nordmark active, err, cpid);
1294799bd290Spwernau }
12958810c16bSdanmcd }
12968810c16bSdanmcd
12978810c16bSdanmcd /* Do NOT consume a reference to itp. */
12988810c16bSdanmcd /* ARGSUSED */
12998810c16bSdanmcd static void
spdsock_flip_node(ipsec_tun_pol_t * itp,void * ignoreme,netstack_t * ns)1300f4b3ec61Sdh155122 spdsock_flip_node(ipsec_tun_pol_t *itp, void *ignoreme, netstack_t *ns)
13018810c16bSdanmcd {
13028810c16bSdanmcd mutex_enter(&itp->itp_lock);
13038810c16bSdanmcd ITPF_SWAP(itp->itp_flags);
1304f4b3ec61Sdh155122 ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns);
13058810c16bSdanmcd mutex_exit(&itp->itp_lock);
130601ac885fSDan McDonald /* SPD_FLIP is worth a tunnel MTU check. */
130701ac885fSDan McDonald update_iptun_policy(itp);
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate
13107c478bd9Sstevel@tonic-gate void
spdsock_flip(queue_t * q,mblk_t * mp,spd_if_t * tunname)13118810c16bSdanmcd spdsock_flip(queue_t *q, mblk_t *mp, spd_if_t *tunname)
13127c478bd9Sstevel@tonic-gate {
13138810c16bSdanmcd char *tname;
13148810c16bSdanmcd ipsec_tun_pol_t *itp;
1315f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
1316db061fdfSdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack;
1317005d3febSMarek Pospisil uint32_t auditing = AU_AUDITING();
13188810c16bSdanmcd
13198810c16bSdanmcd if (tunname != NULL) {
13208810c16bSdanmcd tname = (char *)tunname->spd_if_name;
13218810c16bSdanmcd if (*tname == '\0') {
1322f4b3ec61Sdh155122 /* can't fail */
1323db061fdfSdanmcd ipsec_swap_global_policy(ns);
1324005d3febSMarek Pospisil if (auditing) {
1325799bd290Spwernau boolean_t active;
1326799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1327de8c4a14SErik Nordmark cred_t *cr;
1328de8c4a14SErik Nordmark pid_t cpid;
1329799bd290Spwernau
1330de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1331799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1332de8c4a14SErik Nordmark audit_pf_policy(SPD_FLIP, cr, ns,
1333de8c4a14SErik Nordmark NULL, active, 0, cpid);
1334799bd290Spwernau }
1335db061fdfSdanmcd itp_walk(spdsock_flip_node, NULL, ns);
1336005d3febSMarek Pospisil if (auditing) {
1337799bd290Spwernau boolean_t active;
1338799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1339de8c4a14SErik Nordmark cred_t *cr;
1340de8c4a14SErik Nordmark pid_t cpid;
1341799bd290Spwernau
1342de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1343799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1344de8c4a14SErik Nordmark audit_pf_policy(SPD_FLIP, cr, ns,
1345de8c4a14SErik Nordmark "all tunnels", active, 0, cpid);
1346799bd290Spwernau }
13478810c16bSdanmcd } else {
1348db061fdfSdanmcd itp = get_tunnel_policy(tname, ns);
13498810c16bSdanmcd if (itp == NULL) {
13508810c16bSdanmcd /* Better idea for "tunnel not found"? */
13518810c16bSdanmcd spdsock_error(q, mp, ESRCH, 0);
1352005d3febSMarek Pospisil if (auditing) {
1353799bd290Spwernau boolean_t active;
1354799bd290Spwernau spd_msg_t *spmsg =
1355799bd290Spwernau (spd_msg_t *)mp->b_rptr;
1356de8c4a14SErik Nordmark cred_t *cr;
1357de8c4a14SErik Nordmark pid_t cpid;
1358799bd290Spwernau
1359de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1360799bd290Spwernau active = (spmsg->spd_msg_spdid ==
1361799bd290Spwernau SPD_ACTIVE);
1362de8c4a14SErik Nordmark audit_pf_policy(SPD_FLIP, cr, ns,
1363de8c4a14SErik Nordmark ITP_NAME(itp), active,
1364de8c4a14SErik Nordmark ESRCH, cpid);
1365799bd290Spwernau }
13668810c16bSdanmcd return;
13678810c16bSdanmcd }
136801ac885fSDan McDonald spdsock_flip_node(itp, NULL, ns);
1369005d3febSMarek Pospisil if (auditing) {
1370799bd290Spwernau boolean_t active;
1371799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1372de8c4a14SErik Nordmark cred_t *cr;
1373de8c4a14SErik Nordmark pid_t cpid;
1374799bd290Spwernau
1375de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1376799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1377de8c4a14SErik Nordmark audit_pf_policy(SPD_FLIP, cr, ns,
1378de8c4a14SErik Nordmark ITP_NAME(itp), active, 0, cpid);
1379799bd290Spwernau }
1380db061fdfSdanmcd ITP_REFRELE(itp, ns);
13818810c16bSdanmcd }
13828810c16bSdanmcd } else {
1383db061fdfSdanmcd ipsec_swap_global_policy(ns); /* can't fail */
1384005d3febSMarek Pospisil if (auditing) {
1385799bd290Spwernau boolean_t active;
1386799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
1387de8c4a14SErik Nordmark cred_t *cr;
1388de8c4a14SErik Nordmark pid_t cpid;
1389799bd290Spwernau
1390de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
1391799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
1392de8c4a14SErik Nordmark audit_pf_policy(SPD_FLIP, cr,
1393de8c4a14SErik Nordmark ns, NULL, active, 0, cpid);
1394799bd290Spwernau }
13958810c16bSdanmcd }
13967c478bd9Sstevel@tonic-gate spd_echo(q, mp);
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate
13997c478bd9Sstevel@tonic-gate /*
14007c478bd9Sstevel@tonic-gate * Unimplemented feature
14017c478bd9Sstevel@tonic-gate */
14027c478bd9Sstevel@tonic-gate /* ARGSUSED */
14037c478bd9Sstevel@tonic-gate static void
spdsock_lookup(queue_t * q,ipsec_policy_head_t * iph,mblk_t * mp,spd_ext_t ** extv,ipsec_tun_pol_t * itp)14048810c16bSdanmcd spdsock_lookup(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp,
14058810c16bSdanmcd spd_ext_t **extv, ipsec_tun_pol_t *itp)
14067c478bd9Sstevel@tonic-gate {
14077c478bd9Sstevel@tonic-gate spdsock_error(q, mp, EINVAL, 0);
14087c478bd9Sstevel@tonic-gate }
14097c478bd9Sstevel@tonic-gate
14107c478bd9Sstevel@tonic-gate
14117c478bd9Sstevel@tonic-gate static mblk_t *
spdsock_dump_ruleset(mblk_t * req,ipsec_policy_head_t * iph,uint32_t count,uint16_t error)14127c478bd9Sstevel@tonic-gate spdsock_dump_ruleset(mblk_t *req, ipsec_policy_head_t *iph,
14137c478bd9Sstevel@tonic-gate uint32_t count, uint16_t error)
14147c478bd9Sstevel@tonic-gate {
14157c478bd9Sstevel@tonic-gate size_t len = sizeof (spd_ruleset_ext_t) + sizeof (spd_msg_t);
14167c478bd9Sstevel@tonic-gate spd_msg_t *msg;
14177c478bd9Sstevel@tonic-gate spd_ruleset_ext_t *ruleset;
14187c478bd9Sstevel@tonic-gate mblk_t *m = allocb(len, BPRI_HI);
14197c478bd9Sstevel@tonic-gate
14207c478bd9Sstevel@tonic-gate ASSERT(RW_READ_HELD(&iph->iph_lock));
14217c478bd9Sstevel@tonic-gate
14227c478bd9Sstevel@tonic-gate if (m == NULL) {
14237c478bd9Sstevel@tonic-gate return (NULL);
14247c478bd9Sstevel@tonic-gate }
14257c478bd9Sstevel@tonic-gate msg = (spd_msg_t *)m->b_rptr;
14267c478bd9Sstevel@tonic-gate ruleset = (spd_ruleset_ext_t *)(&msg[1]);
14277c478bd9Sstevel@tonic-gate
14287c478bd9Sstevel@tonic-gate m->b_wptr = (uint8_t *)&ruleset[1];
14297c478bd9Sstevel@tonic-gate
14307c478bd9Sstevel@tonic-gate *msg = *(spd_msg_t *)(req->b_rptr);
14317c478bd9Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(len);
14327c478bd9Sstevel@tonic-gate msg->spd_msg_errno = error;
14337c478bd9Sstevel@tonic-gate
14347c478bd9Sstevel@tonic-gate ruleset->spd_ruleset_len = SPD_8TO64(sizeof (*ruleset));
14357c478bd9Sstevel@tonic-gate ruleset->spd_ruleset_type = SPD_EXT_RULESET;
14367c478bd9Sstevel@tonic-gate ruleset->spd_ruleset_count = count;
14377c478bd9Sstevel@tonic-gate ruleset->spd_ruleset_version = iph->iph_gen;
14387c478bd9Sstevel@tonic-gate return (m);
14397c478bd9Sstevel@tonic-gate }
14407c478bd9Sstevel@tonic-gate
14417c478bd9Sstevel@tonic-gate static mblk_t *
spdsock_dump_finish(spdsock_t * ss,int error)14427c478bd9Sstevel@tonic-gate spdsock_dump_finish(spdsock_t *ss, int error)
14437c478bd9Sstevel@tonic-gate {
14447c478bd9Sstevel@tonic-gate mblk_t *m;
14457c478bd9Sstevel@tonic-gate ipsec_policy_head_t *iph = ss->spdsock_dump_head;
14467c478bd9Sstevel@tonic-gate mblk_t *req = ss->spdsock_dump_req;
14476be82dacSDan McDonald netstack_t *ns = ss->spdsock_spds->spds_netstack;
14487c478bd9Sstevel@tonic-gate
14497c478bd9Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER);
14507c478bd9Sstevel@tonic-gate m = spdsock_dump_ruleset(req, iph, ss->spdsock_dump_count, error);
14517c478bd9Sstevel@tonic-gate rw_exit(&iph->iph_lock);
14526be82dacSDan McDonald IPPH_REFRELE(iph, ns);
14536be82dacSDan McDonald if (ss->spdsock_itp != NULL) {
14546be82dacSDan McDonald ITP_REFRELE(ss->spdsock_itp, ns);
14556be82dacSDan McDonald ss->spdsock_itp = NULL;
14566be82dacSDan McDonald }
14577c478bd9Sstevel@tonic-gate ss->spdsock_dump_req = NULL;
14587c478bd9Sstevel@tonic-gate freemsg(req);
14597c478bd9Sstevel@tonic-gate
14607c478bd9Sstevel@tonic-gate return (m);
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate
14637c478bd9Sstevel@tonic-gate /*
14647c478bd9Sstevel@tonic-gate * Rule encoding functions.
14657c478bd9Sstevel@tonic-gate * We do a two-pass encode.
14667c478bd9Sstevel@tonic-gate * If base != NULL, fill in encoded rule part starting at base+offset.
14677c478bd9Sstevel@tonic-gate * Always return "offset" plus length of to-be-encoded data.
14687c478bd9Sstevel@tonic-gate */
14697c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_typecode(uint8_t * base,uint_t offset,uint8_t type,uint8_t type_end,uint8_t code,uint8_t code_end)14707c478bd9Sstevel@tonic-gate spdsock_encode_typecode(uint8_t *base, uint_t offset, uint8_t type,
14717c478bd9Sstevel@tonic-gate uint8_t type_end, uint8_t code, uint8_t code_end)
14727c478bd9Sstevel@tonic-gate {
14737c478bd9Sstevel@tonic-gate struct spd_typecode *tcp;
14747c478bd9Sstevel@tonic-gate
14757c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
14767c478bd9Sstevel@tonic-gate
14777c478bd9Sstevel@tonic-gate if (base != NULL) {
14787c478bd9Sstevel@tonic-gate tcp = (struct spd_typecode *)(base + offset);
14797c478bd9Sstevel@tonic-gate tcp->spd_typecode_len = SPD_8TO64(sizeof (*tcp));
14807c478bd9Sstevel@tonic-gate tcp->spd_typecode_exttype = SPD_EXT_ICMP_TYPECODE;
14817c478bd9Sstevel@tonic-gate tcp->spd_typecode_code = code;
14827c478bd9Sstevel@tonic-gate tcp->spd_typecode_type = type;
14837c478bd9Sstevel@tonic-gate tcp->spd_typecode_type_end = type_end;
14847c478bd9Sstevel@tonic-gate tcp->spd_typecode_code_end = code_end;
14857c478bd9Sstevel@tonic-gate }
14867c478bd9Sstevel@tonic-gate offset += sizeof (*tcp);
14877c478bd9Sstevel@tonic-gate
14887c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
14897c478bd9Sstevel@tonic-gate
14907c478bd9Sstevel@tonic-gate return (offset);
14917c478bd9Sstevel@tonic-gate }
14927c478bd9Sstevel@tonic-gate
14937c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_proto(uint8_t * base,uint_t offset,uint8_t proto)14947c478bd9Sstevel@tonic-gate spdsock_encode_proto(uint8_t *base, uint_t offset, uint8_t proto)
14957c478bd9Sstevel@tonic-gate {
14967c478bd9Sstevel@tonic-gate struct spd_proto *spp;
14977c478bd9Sstevel@tonic-gate
14987c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
14997c478bd9Sstevel@tonic-gate
15007c478bd9Sstevel@tonic-gate if (base != NULL) {
15017c478bd9Sstevel@tonic-gate spp = (struct spd_proto *)(base + offset);
15027c478bd9Sstevel@tonic-gate spp->spd_proto_len = SPD_8TO64(sizeof (*spp));
15037c478bd9Sstevel@tonic-gate spp->spd_proto_exttype = SPD_EXT_PROTO;
15047c478bd9Sstevel@tonic-gate spp->spd_proto_number = proto;
15057c478bd9Sstevel@tonic-gate spp->spd_proto_reserved1 = 0;
15067c478bd9Sstevel@tonic-gate spp->spd_proto_reserved2 = 0;
15077c478bd9Sstevel@tonic-gate }
15087c478bd9Sstevel@tonic-gate offset += sizeof (*spp);
15097c478bd9Sstevel@tonic-gate
15107c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
15117c478bd9Sstevel@tonic-gate
15127c478bd9Sstevel@tonic-gate return (offset);
15137c478bd9Sstevel@tonic-gate }
15147c478bd9Sstevel@tonic-gate
15157c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_port(uint8_t * base,uint_t offset,uint16_t ext,uint16_t port)15167c478bd9Sstevel@tonic-gate spdsock_encode_port(uint8_t *base, uint_t offset, uint16_t ext, uint16_t port)
15177c478bd9Sstevel@tonic-gate {
15187c478bd9Sstevel@tonic-gate struct spd_portrange *spp;
15197c478bd9Sstevel@tonic-gate
15207c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
15217c478bd9Sstevel@tonic-gate
15227c478bd9Sstevel@tonic-gate if (base != NULL) {
15237c478bd9Sstevel@tonic-gate spp = (struct spd_portrange *)(base + offset);
15247c478bd9Sstevel@tonic-gate spp->spd_ports_len = SPD_8TO64(sizeof (*spp));
15257c478bd9Sstevel@tonic-gate spp->spd_ports_exttype = ext;
15267c478bd9Sstevel@tonic-gate spp->spd_ports_minport = port;
15277c478bd9Sstevel@tonic-gate spp->spd_ports_maxport = port;
15287c478bd9Sstevel@tonic-gate }
15297c478bd9Sstevel@tonic-gate offset += sizeof (*spp);
15307c478bd9Sstevel@tonic-gate
15317c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
15327c478bd9Sstevel@tonic-gate
15337c478bd9Sstevel@tonic-gate return (offset);
15347c478bd9Sstevel@tonic-gate }
15357c478bd9Sstevel@tonic-gate
15367c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_addr(uint8_t * base,uint_t offset,uint16_t ext,const ipsec_selkey_t * sel,const ipsec_addr_t * addr,uint_t pfxlen)15377c478bd9Sstevel@tonic-gate spdsock_encode_addr(uint8_t *base, uint_t offset, uint16_t ext,
15387c478bd9Sstevel@tonic-gate const ipsec_selkey_t *sel, const ipsec_addr_t *addr, uint_t pfxlen)
15397c478bd9Sstevel@tonic-gate {
15407c478bd9Sstevel@tonic-gate struct spd_address *sae;
15417c478bd9Sstevel@tonic-gate ipsec_addr_t *spdaddr;
15427c478bd9Sstevel@tonic-gate uint_t start = offset;
15437c478bd9Sstevel@tonic-gate uint_t addrlen;
15447c478bd9Sstevel@tonic-gate uint_t af;
15457c478bd9Sstevel@tonic-gate
15467c478bd9Sstevel@tonic-gate if (sel->ipsl_valid & IPSL_IPV4) {
15477c478bd9Sstevel@tonic-gate af = AF_INET;
15487c478bd9Sstevel@tonic-gate addrlen = IP_ADDR_LEN;
15497c478bd9Sstevel@tonic-gate } else {
15507c478bd9Sstevel@tonic-gate af = AF_INET6;
15517c478bd9Sstevel@tonic-gate addrlen = IPV6_ADDR_LEN;
15527c478bd9Sstevel@tonic-gate }
15537c478bd9Sstevel@tonic-gate
15547c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
15557c478bd9Sstevel@tonic-gate
15567c478bd9Sstevel@tonic-gate if (base != NULL) {
15577c478bd9Sstevel@tonic-gate sae = (struct spd_address *)(base + offset);
15587c478bd9Sstevel@tonic-gate sae->spd_address_exttype = ext;
15597c478bd9Sstevel@tonic-gate sae->spd_address_af = af;
15607c478bd9Sstevel@tonic-gate sae->spd_address_prefixlen = pfxlen;
15617c478bd9Sstevel@tonic-gate sae->spd_address_reserved2 = 0;
15627c478bd9Sstevel@tonic-gate
15637c478bd9Sstevel@tonic-gate spdaddr = (ipsec_addr_t *)(&sae[1]);
15647c478bd9Sstevel@tonic-gate bcopy(addr, spdaddr, addrlen);
15657c478bd9Sstevel@tonic-gate }
15667c478bd9Sstevel@tonic-gate offset += sizeof (*sae);
15677c478bd9Sstevel@tonic-gate addrlen = roundup(addrlen, sizeof (uint64_t));
15687c478bd9Sstevel@tonic-gate offset += addrlen;
15697c478bd9Sstevel@tonic-gate
15707c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
15717c478bd9Sstevel@tonic-gate
15727c478bd9Sstevel@tonic-gate if (base != NULL)
15737c478bd9Sstevel@tonic-gate sae->spd_address_len = SPD_8TO64(offset - start);
15747c478bd9Sstevel@tonic-gate return (offset);
15757c478bd9Sstevel@tonic-gate }
15767c478bd9Sstevel@tonic-gate
15777c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_sel(uint8_t * base,uint_t offset,const ipsec_sel_t * sel)15787c478bd9Sstevel@tonic-gate spdsock_encode_sel(uint8_t *base, uint_t offset, const ipsec_sel_t *sel)
15797c478bd9Sstevel@tonic-gate {
15807c478bd9Sstevel@tonic-gate const ipsec_selkey_t *selkey = &sel->ipsl_key;
15817c478bd9Sstevel@tonic-gate
15827c478bd9Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_PROTOCOL)
15837c478bd9Sstevel@tonic-gate offset = spdsock_encode_proto(base, offset, selkey->ipsl_proto);
15847c478bd9Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_LOCAL_PORT)
15857c478bd9Sstevel@tonic-gate offset = spdsock_encode_port(base, offset, SPD_EXT_LCLPORT,
15867c478bd9Sstevel@tonic-gate selkey->ipsl_lport);
15877c478bd9Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_REMOTE_PORT)
15887c478bd9Sstevel@tonic-gate offset = spdsock_encode_port(base, offset, SPD_EXT_REMPORT,
15897c478bd9Sstevel@tonic-gate selkey->ipsl_rport);
15907c478bd9Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_REMOTE_ADDR)
15917c478bd9Sstevel@tonic-gate offset = spdsock_encode_addr(base, offset, SPD_EXT_REMADDR,
15927c478bd9Sstevel@tonic-gate selkey, &selkey->ipsl_remote, selkey->ipsl_remote_pfxlen);
15937c478bd9Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_LOCAL_ADDR)
15947c478bd9Sstevel@tonic-gate offset = spdsock_encode_addr(base, offset, SPD_EXT_LCLADDR,
15957c478bd9Sstevel@tonic-gate selkey, &selkey->ipsl_local, selkey->ipsl_local_pfxlen);
15967c478bd9Sstevel@tonic-gate if (selkey->ipsl_valid & IPSL_ICMP_TYPE) {
15977c478bd9Sstevel@tonic-gate offset = spdsock_encode_typecode(base, offset,
15987c478bd9Sstevel@tonic-gate selkey->ipsl_icmp_type, selkey->ipsl_icmp_type_end,
15997c478bd9Sstevel@tonic-gate (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
16007c478bd9Sstevel@tonic-gate selkey->ipsl_icmp_code : 255,
16017c478bd9Sstevel@tonic-gate (selkey->ipsl_valid & IPSL_ICMP_CODE) ?
16027c478bd9Sstevel@tonic-gate selkey->ipsl_icmp_code_end : 255);
16037c478bd9Sstevel@tonic-gate }
16047c478bd9Sstevel@tonic-gate return (offset);
16057c478bd9Sstevel@tonic-gate }
16067c478bd9Sstevel@tonic-gate
16077c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_actattr(uint8_t * base,uint_t offset,uint32_t tag,uint32_t value)16087c478bd9Sstevel@tonic-gate spdsock_encode_actattr(uint8_t *base, uint_t offset, uint32_t tag,
16097c478bd9Sstevel@tonic-gate uint32_t value)
16107c478bd9Sstevel@tonic-gate {
16117c478bd9Sstevel@tonic-gate struct spd_attribute *attr;
16127c478bd9Sstevel@tonic-gate
16137c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
16147c478bd9Sstevel@tonic-gate
16157c478bd9Sstevel@tonic-gate if (base != NULL) {
16167c478bd9Sstevel@tonic-gate attr = (struct spd_attribute *)(base + offset);
16177c478bd9Sstevel@tonic-gate attr->spd_attr_tag = tag;
16187c478bd9Sstevel@tonic-gate attr->spd_attr_value = value;
16197c478bd9Sstevel@tonic-gate }
16207c478bd9Sstevel@tonic-gate offset += sizeof (struct spd_attribute);
16217c478bd9Sstevel@tonic-gate
16227c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
16237c478bd9Sstevel@tonic-gate
16247c478bd9Sstevel@tonic-gate return (offset);
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate
16277c478bd9Sstevel@tonic-gate
16287c478bd9Sstevel@tonic-gate #define EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v))
16297c478bd9Sstevel@tonic-gate
16307c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_action(uint8_t * base,uint_t offset,const ipsec_action_t * ap)16317c478bd9Sstevel@tonic-gate spdsock_encode_action(uint8_t *base, uint_t offset, const ipsec_action_t *ap)
16327c478bd9Sstevel@tonic-gate {
16337c478bd9Sstevel@tonic-gate const struct ipsec_act *act = &(ap->ipa_act);
16347c478bd9Sstevel@tonic-gate uint_t flags;
16357c478bd9Sstevel@tonic-gate
16367c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_EMPTY, 0);
16377c478bd9Sstevel@tonic-gate switch (act->ipa_type) {
16387c478bd9Sstevel@tonic-gate case IPSEC_ACT_DISCARD:
16397c478bd9Sstevel@tonic-gate case IPSEC_ACT_REJECT:
16407c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_DROP);
16417c478bd9Sstevel@tonic-gate break;
16427c478bd9Sstevel@tonic-gate case IPSEC_ACT_BYPASS:
16437c478bd9Sstevel@tonic-gate case IPSEC_ACT_CLEAR:
16447c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_PASS);
16457c478bd9Sstevel@tonic-gate break;
16467c478bd9Sstevel@tonic-gate
16477c478bd9Sstevel@tonic-gate case IPSEC_ACT_APPLY:
16487c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_IPSEC);
16497c478bd9Sstevel@tonic-gate flags = 0;
16507c478bd9Sstevel@tonic-gate if (act->ipa_apply.ipp_use_ah)
16517c478bd9Sstevel@tonic-gate flags |= SPD_APPLY_AH;
16527c478bd9Sstevel@tonic-gate if (act->ipa_apply.ipp_use_esp)
16537c478bd9Sstevel@tonic-gate flags |= SPD_APPLY_ESP;
16547c478bd9Sstevel@tonic-gate if (act->ipa_apply.ipp_use_espa)
16557c478bd9Sstevel@tonic-gate flags |= SPD_APPLY_ESPA;
16567c478bd9Sstevel@tonic-gate if (act->ipa_apply.ipp_use_se)
16577c478bd9Sstevel@tonic-gate flags |= SPD_APPLY_SE;
16587c478bd9Sstevel@tonic-gate if (act->ipa_apply.ipp_use_unique)
16597c478bd9Sstevel@tonic-gate flags |= SPD_APPLY_UNIQUE;
16607c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_FLAGS, flags);
16617c478bd9Sstevel@tonic-gate if (flags & SPD_APPLY_AH) {
16627c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_AH_AUTH, act->ipa_apply.ipp_auth_alg);
16637c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_AH_MINBITS,
16647c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_ah_minbits);
16657c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_AH_MAXBITS,
16667c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_ah_maxbits);
16677c478bd9Sstevel@tonic-gate }
16687c478bd9Sstevel@tonic-gate if (flags & SPD_APPLY_ESP) {
16697c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ESP_ENCR, act->ipa_apply.ipp_encr_alg);
16707c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ENCR_MINBITS,
16717c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_espe_minbits);
16727c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ENCR_MAXBITS,
16737c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_espe_maxbits);
16747c478bd9Sstevel@tonic-gate if (flags & SPD_APPLY_ESPA) {
16757c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ESP_AUTH,
16767c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_esp_auth_alg);
16777c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ESPA_MINBITS,
16787c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_espa_minbits);
16797c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ESPA_MAXBITS,
16807c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_espa_maxbits);
16817c478bd9Sstevel@tonic-gate }
16827c478bd9Sstevel@tonic-gate }
16837c478bd9Sstevel@tonic-gate if (act->ipa_apply.ipp_km_proto != 0)
16847c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_proto);
16857c478bd9Sstevel@tonic-gate if (act->ipa_apply.ipp_km_cookie != 0)
16867c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_cookie);
16877c478bd9Sstevel@tonic-gate if (act->ipa_apply.ipp_replay_depth != 0)
16887c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_REPLAY_DEPTH,
16897c478bd9Sstevel@tonic-gate act->ipa_apply.ipp_replay_depth);
16907c478bd9Sstevel@tonic-gate /* Add more here */
16917c478bd9Sstevel@tonic-gate break;
16927c478bd9Sstevel@tonic-gate }
16937c478bd9Sstevel@tonic-gate
16947c478bd9Sstevel@tonic-gate return (offset);
16957c478bd9Sstevel@tonic-gate }
16967c478bd9Sstevel@tonic-gate
16977c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_action_list(uint8_t * base,uint_t offset,const ipsec_action_t * ap)16987c478bd9Sstevel@tonic-gate spdsock_encode_action_list(uint8_t *base, uint_t offset,
16997c478bd9Sstevel@tonic-gate const ipsec_action_t *ap)
17007c478bd9Sstevel@tonic-gate {
17017c478bd9Sstevel@tonic-gate struct spd_ext_actions *act;
17027c478bd9Sstevel@tonic-gate uint_t nact = 0;
17037c478bd9Sstevel@tonic-gate uint_t start = offset;
17047c478bd9Sstevel@tonic-gate
17057c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
17067c478bd9Sstevel@tonic-gate
17077c478bd9Sstevel@tonic-gate if (base != NULL) {
17087c478bd9Sstevel@tonic-gate act = (struct spd_ext_actions *)(base + offset);
17097c478bd9Sstevel@tonic-gate act->spd_actions_len = 0;
17107c478bd9Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION;
17117c478bd9Sstevel@tonic-gate act->spd_actions_count = 0;
17127c478bd9Sstevel@tonic-gate act->spd_actions_reserved = 0;
17137c478bd9Sstevel@tonic-gate }
17147c478bd9Sstevel@tonic-gate
17157c478bd9Sstevel@tonic-gate offset += sizeof (*act);
17167c478bd9Sstevel@tonic-gate
17177c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
17187c478bd9Sstevel@tonic-gate
17197c478bd9Sstevel@tonic-gate while (ap != NULL) {
17207c478bd9Sstevel@tonic-gate offset = spdsock_encode_action(base, offset, ap);
17217c478bd9Sstevel@tonic-gate ap = ap->ipa_next;
17227c478bd9Sstevel@tonic-gate nact++;
17237c478bd9Sstevel@tonic-gate if (ap != NULL) {
17247c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0);
17257c478bd9Sstevel@tonic-gate }
17267c478bd9Sstevel@tonic-gate }
17277c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_END, 0);
17287c478bd9Sstevel@tonic-gate
17297c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
17307c478bd9Sstevel@tonic-gate
17317c478bd9Sstevel@tonic-gate if (base != NULL) {
17327c478bd9Sstevel@tonic-gate act->spd_actions_count = nact;
17337c478bd9Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(offset - start);
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate
17367c478bd9Sstevel@tonic-gate return (offset);
17377c478bd9Sstevel@tonic-gate }
17387c478bd9Sstevel@tonic-gate
17397c478bd9Sstevel@tonic-gate #undef EMIT
17407c478bd9Sstevel@tonic-gate
17417c478bd9Sstevel@tonic-gate /* ARGSUSED */
17427c478bd9Sstevel@tonic-gate static uint_t
spdsock_rule_flags(uint_t dir,uint_t af)17437c478bd9Sstevel@tonic-gate spdsock_rule_flags(uint_t dir, uint_t af)
17447c478bd9Sstevel@tonic-gate {
17457c478bd9Sstevel@tonic-gate uint_t flags = 0;
17467c478bd9Sstevel@tonic-gate
17477c478bd9Sstevel@tonic-gate if (dir == IPSEC_TYPE_INBOUND)
17487c478bd9Sstevel@tonic-gate flags |= SPD_RULE_FLAG_INBOUND;
17497c478bd9Sstevel@tonic-gate if (dir == IPSEC_TYPE_OUTBOUND)
17507c478bd9Sstevel@tonic-gate flags |= SPD_RULE_FLAG_OUTBOUND;
17517c478bd9Sstevel@tonic-gate
17527c478bd9Sstevel@tonic-gate return (flags);
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate
17557c478bd9Sstevel@tonic-gate
17567c478bd9Sstevel@tonic-gate static uint_t
spdsock_encode_rule_head(uint8_t * base,uint_t offset,spd_msg_t * req,const ipsec_policy_t * rule,uint_t dir,uint_t af,char * name,boolean_t tunnel)17578810c16bSdanmcd spdsock_encode_rule_head(uint8_t *base, uint_t offset, spd_msg_t *req,
17588810c16bSdanmcd const ipsec_policy_t *rule, uint_t dir, uint_t af, char *name,
17598810c16bSdanmcd boolean_t tunnel)
17607c478bd9Sstevel@tonic-gate {
17617c478bd9Sstevel@tonic-gate struct spd_msg *spmsg;
17627c478bd9Sstevel@tonic-gate struct spd_rule *spr;
17638810c16bSdanmcd spd_if_t *sid;
17647c478bd9Sstevel@tonic-gate
17657c478bd9Sstevel@tonic-gate uint_t start = offset;
17667c478bd9Sstevel@tonic-gate
17677c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
17687c478bd9Sstevel@tonic-gate
17697c478bd9Sstevel@tonic-gate if (base != NULL) {
17707c478bd9Sstevel@tonic-gate spmsg = (struct spd_msg *)(base + offset);
17717c478bd9Sstevel@tonic-gate bzero(spmsg, sizeof (*spmsg));
17727c478bd9Sstevel@tonic-gate spmsg->spd_msg_version = PF_POLICY_V1;
17737c478bd9Sstevel@tonic-gate spmsg->spd_msg_type = SPD_DUMP;
17747c478bd9Sstevel@tonic-gate spmsg->spd_msg_seq = req->spd_msg_seq;
17757c478bd9Sstevel@tonic-gate spmsg->spd_msg_pid = req->spd_msg_pid;
17767c478bd9Sstevel@tonic-gate }
17777c478bd9Sstevel@tonic-gate offset += sizeof (struct spd_msg);
17787c478bd9Sstevel@tonic-gate
17797c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
17807c478bd9Sstevel@tonic-gate
17817c478bd9Sstevel@tonic-gate if (base != NULL) {
17827c478bd9Sstevel@tonic-gate spr = (struct spd_rule *)(base + offset);
17837c478bd9Sstevel@tonic-gate spr->spd_rule_type = SPD_EXT_RULE;
17847c478bd9Sstevel@tonic-gate spr->spd_rule_priority = rule->ipsp_prio;
17857c478bd9Sstevel@tonic-gate spr->spd_rule_flags = spdsock_rule_flags(dir, af);
17868810c16bSdanmcd if (tunnel)
17878810c16bSdanmcd spr->spd_rule_flags |= SPD_RULE_FLAG_TUNNEL;
17887c478bd9Sstevel@tonic-gate spr->spd_rule_unused = 0;
17897c478bd9Sstevel@tonic-gate spr->spd_rule_len = SPD_8TO64(sizeof (*spr));
17907c478bd9Sstevel@tonic-gate spr->spd_rule_index = rule->ipsp_index;
17917c478bd9Sstevel@tonic-gate }
17927c478bd9Sstevel@tonic-gate offset += sizeof (struct spd_rule);
17938810c16bSdanmcd
17948810c16bSdanmcd /*
17958810c16bSdanmcd * If we have an interface name (i.e. if this policy head came from
17968810c16bSdanmcd * a tunnel), add the SPD_EXT_TUN_NAME extension.
17978810c16bSdanmcd */
1798cceae6f8Sdanmcd if (name != NULL) {
17998810c16bSdanmcd
18008810c16bSdanmcd ASSERT(ALIGNED64(offset));
18018810c16bSdanmcd
18028810c16bSdanmcd if (base != NULL) {
18038810c16bSdanmcd sid = (spd_if_t *)(base + offset);
18048810c16bSdanmcd sid->spd_if_exttype = SPD_EXT_TUN_NAME;
18058810c16bSdanmcd sid->spd_if_len = SPD_8TO64(sizeof (spd_if_t) +
18068810c16bSdanmcd roundup((strlen(name) - 4), 8));
18078810c16bSdanmcd (void) strlcpy((char *)sid->spd_if_name, name,
18088810c16bSdanmcd LIFNAMSIZ);
18098810c16bSdanmcd }
18108810c16bSdanmcd
18118810c16bSdanmcd offset += sizeof (spd_if_t) + roundup((strlen(name) - 4), 8);
18128810c16bSdanmcd }
18138810c16bSdanmcd
18147c478bd9Sstevel@tonic-gate offset = spdsock_encode_sel(base, offset, rule->ipsp_sel);
18157c478bd9Sstevel@tonic-gate offset = spdsock_encode_action_list(base, offset, rule->ipsp_act);
18167c478bd9Sstevel@tonic-gate
18177c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(offset));
18187c478bd9Sstevel@tonic-gate
18197c478bd9Sstevel@tonic-gate if (base != NULL) {
18207c478bd9Sstevel@tonic-gate spmsg->spd_msg_len = SPD_8TO64(offset - start);
18217c478bd9Sstevel@tonic-gate }
18227c478bd9Sstevel@tonic-gate return (offset);
18237c478bd9Sstevel@tonic-gate }
18247c478bd9Sstevel@tonic-gate
18257c478bd9Sstevel@tonic-gate /* ARGSUSED */
18267c478bd9Sstevel@tonic-gate static mblk_t *
spdsock_encode_rule(mblk_t * req,const ipsec_policy_t * rule,uint_t dir,uint_t af,char * name,boolean_t tunnel)18277c478bd9Sstevel@tonic-gate spdsock_encode_rule(mblk_t *req, const ipsec_policy_t *rule,
18288810c16bSdanmcd uint_t dir, uint_t af, char *name, boolean_t tunnel)
18297c478bd9Sstevel@tonic-gate {
18307c478bd9Sstevel@tonic-gate mblk_t *m;
18317c478bd9Sstevel@tonic-gate uint_t len;
18327c478bd9Sstevel@tonic-gate spd_msg_t *mreq = (spd_msg_t *)req->b_rptr;
18337c478bd9Sstevel@tonic-gate
18347c478bd9Sstevel@tonic-gate /*
18357c478bd9Sstevel@tonic-gate * Figure out how much space we'll need.
18367c478bd9Sstevel@tonic-gate */
18378810c16bSdanmcd len = spdsock_encode_rule_head(NULL, 0, mreq, rule, dir, af, name,
18388810c16bSdanmcd tunnel);
18397c478bd9Sstevel@tonic-gate
18407c478bd9Sstevel@tonic-gate /*
18417c478bd9Sstevel@tonic-gate * Allocate mblk.
18427c478bd9Sstevel@tonic-gate */
18437c478bd9Sstevel@tonic-gate m = allocb(len, BPRI_HI);
18447c478bd9Sstevel@tonic-gate if (m == NULL)
18457c478bd9Sstevel@tonic-gate return (NULL);
18467c478bd9Sstevel@tonic-gate
18477c478bd9Sstevel@tonic-gate /*
18487c478bd9Sstevel@tonic-gate * Fill it in..
18497c478bd9Sstevel@tonic-gate */
18507c478bd9Sstevel@tonic-gate m->b_wptr = m->b_rptr + len;
18517c478bd9Sstevel@tonic-gate bzero(m->b_rptr, len);
18528810c16bSdanmcd (void) spdsock_encode_rule_head(m->b_rptr, 0, mreq, rule, dir, af,
18538810c16bSdanmcd name, tunnel);
18547c478bd9Sstevel@tonic-gate return (m);
18557c478bd9Sstevel@tonic-gate }
18567c478bd9Sstevel@tonic-gate
18577c478bd9Sstevel@tonic-gate static ipsec_policy_t *
spdsock_dump_next_in_chain(spdsock_t * ss,ipsec_policy_head_t * iph,ipsec_policy_t * cur)18586a182920Ssommerfe spdsock_dump_next_in_chain(spdsock_t *ss, ipsec_policy_head_t *iph,
18596a182920Ssommerfe ipsec_policy_t *cur)
18606a182920Ssommerfe {
18616a182920Ssommerfe ASSERT(RW_READ_HELD(&iph->iph_lock));
18626a182920Ssommerfe
18636a182920Ssommerfe ss->spdsock_dump_count++;
18646a182920Ssommerfe ss->spdsock_dump_cur_rule = cur->ipsp_hash.hash_next;
18656a182920Ssommerfe return (cur);
18666a182920Ssommerfe }
18676a182920Ssommerfe
18686a182920Ssommerfe static ipsec_policy_t *
spdsock_dump_next_rule(spdsock_t * ss,ipsec_policy_head_t * iph)18697c478bd9Sstevel@tonic-gate spdsock_dump_next_rule(spdsock_t *ss, ipsec_policy_head_t *iph)
18707c478bd9Sstevel@tonic-gate {
18717c478bd9Sstevel@tonic-gate ipsec_policy_t *cur;
18726a182920Ssommerfe ipsec_policy_root_t *ipr;
18736a182920Ssommerfe int chain, nchains, type, af;
18747c478bd9Sstevel@tonic-gate
18757c478bd9Sstevel@tonic-gate ASSERT(RW_READ_HELD(&iph->iph_lock));
18767c478bd9Sstevel@tonic-gate
18777c478bd9Sstevel@tonic-gate cur = ss->spdsock_dump_cur_rule;
18787c478bd9Sstevel@tonic-gate
18796a182920Ssommerfe if (cur != NULL)
18806a182920Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur));
18816a182920Ssommerfe
18826a182920Ssommerfe type = ss->spdsock_dump_cur_type;
18836a182920Ssommerfe
18846a182920Ssommerfe next:
18856a182920Ssommerfe chain = ss->spdsock_dump_cur_chain;
18866a182920Ssommerfe ipr = &iph->iph_root[type];
18876a182920Ssommerfe nchains = ipr->ipr_nchains;
18886a182920Ssommerfe
18896a182920Ssommerfe while (chain < nchains) {
18906a182920Ssommerfe cur = ipr->ipr_hash[chain].hash_head;
18916a182920Ssommerfe chain++;
18926a182920Ssommerfe if (cur != NULL) {
18936a182920Ssommerfe ss->spdsock_dump_cur_chain = chain;
18946a182920Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur));
18956a182920Ssommerfe }
18966a182920Ssommerfe }
18976a182920Ssommerfe ss->spdsock_dump_cur_chain = nchains;
18986a182920Ssommerfe
18996a182920Ssommerfe af = ss->spdsock_dump_cur_af;
19006a182920Ssommerfe while (af < IPSEC_NAF) {
19016a182920Ssommerfe cur = ipr->ipr_nonhash[af];
19027c478bd9Sstevel@tonic-gate af++;
19036a182920Ssommerfe if (cur != NULL) {
19046a182920Ssommerfe ss->spdsock_dump_cur_af = af;
19056a182920Ssommerfe return (spdsock_dump_next_in_chain(ss, iph, cur));
19066a182920Ssommerfe }
19076a182920Ssommerfe }
19086a182920Ssommerfe
19097c478bd9Sstevel@tonic-gate type++;
19107c478bd9Sstevel@tonic-gate if (type >= IPSEC_NTYPES)
19117c478bd9Sstevel@tonic-gate return (NULL);
19126a182920Ssommerfe
19136a182920Ssommerfe ss->spdsock_dump_cur_chain = 0;
19147c478bd9Sstevel@tonic-gate ss->spdsock_dump_cur_type = type;
19156a182920Ssommerfe ss->spdsock_dump_cur_af = IPSEC_AF_V4;
19166a182920Ssommerfe goto next;
19176a182920Ssommerfe
19187c478bd9Sstevel@tonic-gate }
19197c478bd9Sstevel@tonic-gate
1920db061fdfSdanmcd /*
1921db061fdfSdanmcd * If we're done with one policy head, but have more to go, we iterate through
1922db061fdfSdanmcd * another IPsec tunnel policy head (itp). Return NULL if it is an error
1923db061fdfSdanmcd * worthy of returning EAGAIN via PF_POLICY.
1924db061fdfSdanmcd */
1925db061fdfSdanmcd static ipsec_tun_pol_t *
spdsock_dump_iterate_next_tunnel(spdsock_t * ss,ipsec_stack_t * ipss)1926db061fdfSdanmcd spdsock_dump_iterate_next_tunnel(spdsock_t *ss, ipsec_stack_t *ipss)
1927db061fdfSdanmcd {
1928db061fdfSdanmcd ipsec_tun_pol_t *itp;
1929db061fdfSdanmcd
1930db061fdfSdanmcd ASSERT(RW_READ_HELD(&ipss->ipsec_tunnel_policy_lock));
1931db061fdfSdanmcd if (ipss->ipsec_tunnel_policy_gen > ss->spdsock_dump_tun_gen) {
1932db061fdfSdanmcd /* Oops, state of the tunnel polheads changed. */
1933db061fdfSdanmcd itp = NULL;
1934db061fdfSdanmcd } else if (ss->spdsock_itp == NULL) {
1935db061fdfSdanmcd /* Just finished global, find first node. */
1936db061fdfSdanmcd itp = avl_first(&ipss->ipsec_tunnel_policies);
1937db061fdfSdanmcd } else {
1938db061fdfSdanmcd /* We just finished current polhead, find the next one. */
1939db061fdfSdanmcd itp = AVL_NEXT(&ipss->ipsec_tunnel_policies, ss->spdsock_itp);
1940db061fdfSdanmcd }
1941db061fdfSdanmcd if (itp != NULL) {
1942db061fdfSdanmcd ITP_REFHOLD(itp);
1943db061fdfSdanmcd }
1944db061fdfSdanmcd if (ss->spdsock_itp != NULL) {
1945db061fdfSdanmcd ITP_REFRELE(ss->spdsock_itp, ipss->ipsec_netstack);
1946db061fdfSdanmcd }
1947db061fdfSdanmcd ss->spdsock_itp = itp;
1948db061fdfSdanmcd return (itp);
1949db061fdfSdanmcd }
1950db061fdfSdanmcd
19517c478bd9Sstevel@tonic-gate static mblk_t *
spdsock_dump_next_record(spdsock_t * ss)19527c478bd9Sstevel@tonic-gate spdsock_dump_next_record(spdsock_t *ss)
19537c478bd9Sstevel@tonic-gate {
19547c478bd9Sstevel@tonic-gate ipsec_policy_head_t *iph;
19557c478bd9Sstevel@tonic-gate ipsec_policy_t *rule;
19567c478bd9Sstevel@tonic-gate mblk_t *m;
1957db061fdfSdanmcd ipsec_tun_pol_t *itp;
1958db061fdfSdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack;
1959db061fdfSdanmcd ipsec_stack_t *ipss = ns->netstack_ipsec;
19607c478bd9Sstevel@tonic-gate
19617c478bd9Sstevel@tonic-gate iph = ss->spdsock_dump_head;
19627c478bd9Sstevel@tonic-gate
19637c478bd9Sstevel@tonic-gate ASSERT(iph != NULL);
19647c478bd9Sstevel@tonic-gate
19657c478bd9Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER);
19667c478bd9Sstevel@tonic-gate
19677c478bd9Sstevel@tonic-gate if (iph->iph_gen != ss->spdsock_dump_gen) {
19687c478bd9Sstevel@tonic-gate rw_exit(&iph->iph_lock);
19697c478bd9Sstevel@tonic-gate return (spdsock_dump_finish(ss, EAGAIN));
19707c478bd9Sstevel@tonic-gate }
19717c478bd9Sstevel@tonic-gate
1972db061fdfSdanmcd while ((rule = spdsock_dump_next_rule(ss, iph)) == NULL) {
19737c478bd9Sstevel@tonic-gate rw_exit(&iph->iph_lock);
1974db061fdfSdanmcd if (--(ss->spdsock_dump_remaining_polheads) == 0)
19757c478bd9Sstevel@tonic-gate return (spdsock_dump_finish(ss, 0));
1976db061fdfSdanmcd
1977db061fdfSdanmcd
1978db061fdfSdanmcd /*
1979db061fdfSdanmcd * If we reach here, we have more policy heads (tunnel
1980db061fdfSdanmcd * entries) to dump. Let's reset to a new policy head
1981db061fdfSdanmcd * and get some more rules.
1982db061fdfSdanmcd *
1983db061fdfSdanmcd * An empty policy head will have spdsock_dump_next_rule()
1984db061fdfSdanmcd * return NULL, and we loop (while dropping the number of
1985db061fdfSdanmcd * remaining polheads). If we loop to 0, we finish. We
1986db061fdfSdanmcd * keep looping until we hit 0 or until we have a rule to
1987db061fdfSdanmcd * encode.
1988db061fdfSdanmcd *
1989db061fdfSdanmcd * NOTE: No need for ITP_REF*() macros here as we're only
1990db061fdfSdanmcd * going after and refholding the policy head itself.
1991db061fdfSdanmcd */
1992db061fdfSdanmcd rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
1993db061fdfSdanmcd itp = spdsock_dump_iterate_next_tunnel(ss, ipss);
1994db061fdfSdanmcd if (itp == NULL) {
1995db061fdfSdanmcd rw_exit(&ipss->ipsec_tunnel_policy_lock);
1996db061fdfSdanmcd return (spdsock_dump_finish(ss, EAGAIN));
19977c478bd9Sstevel@tonic-gate }
19987c478bd9Sstevel@tonic-gate
1999db061fdfSdanmcd /* Reset other spdsock_dump thingies. */
2000db061fdfSdanmcd IPPH_REFRELE(ss->spdsock_dump_head, ns);
2001db061fdfSdanmcd if (ss->spdsock_dump_active) {
2002db061fdfSdanmcd ss->spdsock_dump_tunnel =
2003db061fdfSdanmcd itp->itp_flags & ITPF_P_TUNNEL;
2004db061fdfSdanmcd iph = itp->itp_policy;
2005db061fdfSdanmcd } else {
2006db061fdfSdanmcd ss->spdsock_dump_tunnel =
2007db061fdfSdanmcd itp->itp_flags & ITPF_I_TUNNEL;
2008db061fdfSdanmcd iph = itp->itp_inactive;
2009db061fdfSdanmcd }
2010db061fdfSdanmcd IPPH_REFHOLD(iph);
2011db061fdfSdanmcd rw_exit(&ipss->ipsec_tunnel_policy_lock);
2012db061fdfSdanmcd
2013db061fdfSdanmcd rw_enter(&iph->iph_lock, RW_READER);
2014db061fdfSdanmcd RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
2015db061fdfSdanmcd }
2016db061fdfSdanmcd
2017db061fdfSdanmcd m = spdsock_encode_rule(ss->spdsock_dump_req, rule,
2018db061fdfSdanmcd ss->spdsock_dump_cur_type, ss->spdsock_dump_cur_af,
2019cceae6f8Sdanmcd (ss->spdsock_itp == NULL) ? NULL : ss->spdsock_itp->itp_name,
2020cceae6f8Sdanmcd ss->spdsock_dump_tunnel);
20217c478bd9Sstevel@tonic-gate rw_exit(&iph->iph_lock);
20227c478bd9Sstevel@tonic-gate
20237c478bd9Sstevel@tonic-gate if (m == NULL)
20247c478bd9Sstevel@tonic-gate return (spdsock_dump_finish(ss, ENOMEM));
20257c478bd9Sstevel@tonic-gate return (m);
20267c478bd9Sstevel@tonic-gate }
20277c478bd9Sstevel@tonic-gate
20287c478bd9Sstevel@tonic-gate /*
20297c478bd9Sstevel@tonic-gate * Dump records until we run into flow-control back-pressure.
20307c478bd9Sstevel@tonic-gate */
20317c478bd9Sstevel@tonic-gate static void
spdsock_dump_some(queue_t * q,spdsock_t * ss)20327c478bd9Sstevel@tonic-gate spdsock_dump_some(queue_t *q, spdsock_t *ss)
20337c478bd9Sstevel@tonic-gate {
20347c478bd9Sstevel@tonic-gate mblk_t *m, *dataind;
20357c478bd9Sstevel@tonic-gate
20367c478bd9Sstevel@tonic-gate while ((ss->spdsock_dump_req != NULL) && canputnext(q)) {
20377c478bd9Sstevel@tonic-gate m = spdsock_dump_next_record(ss);
20387c478bd9Sstevel@tonic-gate if (m == NULL)
20397c478bd9Sstevel@tonic-gate return;
20407c478bd9Sstevel@tonic-gate dataind = allocb(sizeof (struct T_data_req), BPRI_HI);
20417c478bd9Sstevel@tonic-gate if (dataind == NULL) {
20427c478bd9Sstevel@tonic-gate freemsg(m);
20437c478bd9Sstevel@tonic-gate return;
20447c478bd9Sstevel@tonic-gate }
20457c478bd9Sstevel@tonic-gate dataind->b_cont = m;
20467c478bd9Sstevel@tonic-gate dataind->b_wptr += sizeof (struct T_data_req);
20477c478bd9Sstevel@tonic-gate ((struct T_data_ind *)dataind->b_rptr)->PRIM_type = T_DATA_IND;
20487c478bd9Sstevel@tonic-gate ((struct T_data_ind *)dataind->b_rptr)->MORE_flag = 0;
20497c478bd9Sstevel@tonic-gate dataind->b_datap->db_type = M_PROTO;
20507c478bd9Sstevel@tonic-gate putnext(q, dataind);
20517c478bd9Sstevel@tonic-gate }
20527c478bd9Sstevel@tonic-gate }
20537c478bd9Sstevel@tonic-gate
20547c478bd9Sstevel@tonic-gate /*
20557c478bd9Sstevel@tonic-gate * Start dumping.
20567c478bd9Sstevel@tonic-gate * Format a start-of-dump record, and set up the stream and kick the rsrv
20577c478bd9Sstevel@tonic-gate * procedure to continue the job..
20587c478bd9Sstevel@tonic-gate */
20597c478bd9Sstevel@tonic-gate /* ARGSUSED */
20607c478bd9Sstevel@tonic-gate static void
spdsock_dump(queue_t * q,ipsec_policy_head_t * iph,mblk_t * mp)20618810c16bSdanmcd spdsock_dump(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp)
20627c478bd9Sstevel@tonic-gate {
20637c478bd9Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr;
2064db061fdfSdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack;
2065f4b3ec61Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec;
20667c478bd9Sstevel@tonic-gate mblk_t *mr;
20677c478bd9Sstevel@tonic-gate
2068db061fdfSdanmcd /* spdsock_open() already set spdsock_itp to NULL. */
20698810c16bSdanmcd if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
2070f4b3ec61Sdh155122 rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER);
20718810c16bSdanmcd ss->spdsock_dump_remaining_polheads = 1 +
2072f4b3ec61Sdh155122 avl_numnodes(&ipss->ipsec_tunnel_policies);
2073f4b3ec61Sdh155122 ss->spdsock_dump_tun_gen = ipss->ipsec_tunnel_policy_gen;
2074f4b3ec61Sdh155122 rw_exit(&ipss->ipsec_tunnel_policy_lock);
20758810c16bSdanmcd if (iph == ALL_ACTIVE_POLHEADS) {
2076f4b3ec61Sdh155122 iph = ipsec_system_policy(ns);
20778810c16bSdanmcd ss->spdsock_dump_active = B_TRUE;
20788810c16bSdanmcd } else {
2079db061fdfSdanmcd iph = ipsec_inactive_policy(ns);
20808810c16bSdanmcd ss->spdsock_dump_active = B_FALSE;
20818810c16bSdanmcd }
2082db061fdfSdanmcd ASSERT(ss->spdsock_itp == NULL);
20838810c16bSdanmcd } else {
20848810c16bSdanmcd ss->spdsock_dump_remaining_polheads = 1;
20858810c16bSdanmcd }
20868810c16bSdanmcd
20877c478bd9Sstevel@tonic-gate rw_enter(&iph->iph_lock, RW_READER);
20887c478bd9Sstevel@tonic-gate
20897c478bd9Sstevel@tonic-gate mr = spdsock_dump_ruleset(mp, iph, 0, 0);
20907c478bd9Sstevel@tonic-gate
20917c478bd9Sstevel@tonic-gate if (!mr) {
20927c478bd9Sstevel@tonic-gate rw_exit(&iph->iph_lock);
20937c478bd9Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0);
20947c478bd9Sstevel@tonic-gate return;
20957c478bd9Sstevel@tonic-gate }
20967c478bd9Sstevel@tonic-gate
20977c478bd9Sstevel@tonic-gate ss->spdsock_dump_req = mp;
2098db061fdfSdanmcd RESET_SPDSOCK_DUMP_POLHEAD(ss, iph);
2099db061fdfSdanmcd
21007c478bd9Sstevel@tonic-gate rw_exit(&iph->iph_lock);
21017c478bd9Sstevel@tonic-gate
21027c478bd9Sstevel@tonic-gate qreply(q, mr);
21037c478bd9Sstevel@tonic-gate qenable(OTHERQ(q));
21047c478bd9Sstevel@tonic-gate }
21057c478bd9Sstevel@tonic-gate
21068810c16bSdanmcd /* Do NOT consume a reference to ITP. */
21077c478bd9Sstevel@tonic-gate void
spdsock_clone_node(ipsec_tun_pol_t * itp,void * ep,netstack_t * ns)2108f4b3ec61Sdh155122 spdsock_clone_node(ipsec_tun_pol_t *itp, void *ep, netstack_t *ns)
21097c478bd9Sstevel@tonic-gate {
21108810c16bSdanmcd int *errptr = (int *)ep;
21118810c16bSdanmcd
21128810c16bSdanmcd if (*errptr != 0)
21138810c16bSdanmcd return; /* We've failed already for some reason. */
21148810c16bSdanmcd mutex_enter(&itp->itp_lock);
21158810c16bSdanmcd ITPF_CLONE(itp->itp_flags);
2116f4b3ec61Sdh155122 *errptr = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns);
21178810c16bSdanmcd mutex_exit(&itp->itp_lock);
21188810c16bSdanmcd }
21198810c16bSdanmcd
21208810c16bSdanmcd void
spdsock_clone(queue_t * q,mblk_t * mp,spd_if_t * tunname)21218810c16bSdanmcd spdsock_clone(queue_t *q, mblk_t *mp, spd_if_t *tunname)
21228810c16bSdanmcd {
21238810c16bSdanmcd int error;
21248810c16bSdanmcd char *tname;
21258810c16bSdanmcd ipsec_tun_pol_t *itp;
2126f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2127db061fdfSdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack;
2128005d3febSMarek Pospisil uint32_t auditing = AU_AUDITING();
21298810c16bSdanmcd
21308810c16bSdanmcd if (tunname != NULL) {
21318810c16bSdanmcd tname = (char *)tunname->spd_if_name;
21328810c16bSdanmcd if (*tname == '\0') {
2133db061fdfSdanmcd error = ipsec_clone_system_policy(ns);
2134005d3febSMarek Pospisil if (auditing) {
2135799bd290Spwernau boolean_t active;
2136799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2137de8c4a14SErik Nordmark cred_t *cr;
2138de8c4a14SErik Nordmark pid_t cpid;
2139799bd290Spwernau
2140de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
2141799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2142de8c4a14SErik Nordmark audit_pf_policy(SPD_CLONE, cr, ns,
2143de8c4a14SErik Nordmark NULL, active, error, cpid);
2144799bd290Spwernau }
2145799bd290Spwernau if (error == 0) {
2146db061fdfSdanmcd itp_walk(spdsock_clone_node, &error, ns);
2147005d3febSMarek Pospisil if (auditing) {
2148799bd290Spwernau boolean_t active;
2149799bd290Spwernau spd_msg_t *spmsg =
2150799bd290Spwernau (spd_msg_t *)mp->b_rptr;
2151de8c4a14SErik Nordmark cred_t *cr;
2152de8c4a14SErik Nordmark pid_t cpid;
2153799bd290Spwernau
2154de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
2155799bd290Spwernau active = (spmsg->spd_msg_spdid ==
2156799bd290Spwernau SPD_ACTIVE);
2157de8c4a14SErik Nordmark audit_pf_policy(SPD_CLONE, cr,
2158db061fdfSdanmcd ns, "all tunnels", active, 0,
2159de8c4a14SErik Nordmark cpid);
2160799bd290Spwernau }
2161799bd290Spwernau }
21628810c16bSdanmcd } else {
2163db061fdfSdanmcd itp = get_tunnel_policy(tname, ns);
21648810c16bSdanmcd if (itp == NULL) {
21658810c16bSdanmcd spdsock_error(q, mp, ENOENT, 0);
2166005d3febSMarek Pospisil if (auditing) {
2167799bd290Spwernau boolean_t active;
2168799bd290Spwernau spd_msg_t *spmsg =
2169799bd290Spwernau (spd_msg_t *)mp->b_rptr;
2170de8c4a14SErik Nordmark cred_t *cr;
2171de8c4a14SErik Nordmark pid_t cpid;
2172799bd290Spwernau
2173de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
2174799bd290Spwernau active = (spmsg->spd_msg_spdid ==
2175799bd290Spwernau SPD_ACTIVE);
2176de8c4a14SErik Nordmark audit_pf_policy(SPD_CLONE, cr,
2177051c7f8aSDan McDonald ns, NULL, active, ENOENT, cpid);
2178799bd290Spwernau }
21798810c16bSdanmcd return;
21808810c16bSdanmcd }
2181f4b3ec61Sdh155122 spdsock_clone_node(itp, &error, NULL);
2182005d3febSMarek Pospisil if (auditing) {
2183799bd290Spwernau boolean_t active;
2184799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2185de8c4a14SErik Nordmark cred_t *cr;
2186de8c4a14SErik Nordmark pid_t cpid;
2187799bd290Spwernau
2188de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
2189799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2190de8c4a14SErik Nordmark audit_pf_policy(SPD_CLONE, cr, ns,
2191de8c4a14SErik Nordmark ITP_NAME(itp), active, error, cpid);
2192799bd290Spwernau }
2193051c7f8aSDan McDonald ITP_REFRELE(itp, ns);
21948810c16bSdanmcd }
21958810c16bSdanmcd } else {
2196db061fdfSdanmcd error = ipsec_clone_system_policy(ns);
2197005d3febSMarek Pospisil if (auditing) {
2198799bd290Spwernau boolean_t active;
2199799bd290Spwernau spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr;
2200de8c4a14SErik Nordmark cred_t *cr;
2201de8c4a14SErik Nordmark pid_t cpid;
2202799bd290Spwernau
2203de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
2204799bd290Spwernau active = (spmsg->spd_msg_spdid == SPD_ACTIVE);
2205de8c4a14SErik Nordmark audit_pf_policy(SPD_CLONE, cr, ns, NULL,
2206de8c4a14SErik Nordmark active, error, cpid);
2207799bd290Spwernau }
22088810c16bSdanmcd }
22098810c16bSdanmcd
22107c478bd9Sstevel@tonic-gate if (error != 0)
22117c478bd9Sstevel@tonic-gate spdsock_error(q, mp, error, 0);
22127c478bd9Sstevel@tonic-gate else
22137c478bd9Sstevel@tonic-gate spd_echo(q, mp);
22147c478bd9Sstevel@tonic-gate }
22157c478bd9Sstevel@tonic-gate
22167c478bd9Sstevel@tonic-gate /*
22177c478bd9Sstevel@tonic-gate * Process a SPD_ALGLIST request. The caller expects separate alg entries
22187c478bd9Sstevel@tonic-gate * for AH authentication, ESP authentication, and ESP encryption.
22197c478bd9Sstevel@tonic-gate * The same distinction is then used when setting the min and max key
22207c478bd9Sstevel@tonic-gate * sizes when defining policies.
22217c478bd9Sstevel@tonic-gate */
22227c478bd9Sstevel@tonic-gate
22237c478bd9Sstevel@tonic-gate #define SPDSOCK_AH_AUTH 0
22247c478bd9Sstevel@tonic-gate #define SPDSOCK_ESP_AUTH 1
22257c478bd9Sstevel@tonic-gate #define SPDSOCK_ESP_ENCR 2
22267c478bd9Sstevel@tonic-gate #define SPDSOCK_NTYPES 3
22277c478bd9Sstevel@tonic-gate
22287c478bd9Sstevel@tonic-gate static const uint_t algattr[SPDSOCK_NTYPES] = {
22297c478bd9Sstevel@tonic-gate SPD_ATTR_AH_AUTH,
22307c478bd9Sstevel@tonic-gate SPD_ATTR_ESP_AUTH,
22317c478bd9Sstevel@tonic-gate SPD_ATTR_ESP_ENCR
22327c478bd9Sstevel@tonic-gate };
22337c478bd9Sstevel@tonic-gate static const uint_t minbitsattr[SPDSOCK_NTYPES] = {
22347c478bd9Sstevel@tonic-gate SPD_ATTR_AH_MINBITS,
22357c478bd9Sstevel@tonic-gate SPD_ATTR_ESPA_MINBITS,
22367c478bd9Sstevel@tonic-gate SPD_ATTR_ENCR_MINBITS
22377c478bd9Sstevel@tonic-gate };
22387c478bd9Sstevel@tonic-gate static const uint_t maxbitsattr[SPDSOCK_NTYPES] = {
22397c478bd9Sstevel@tonic-gate SPD_ATTR_AH_MAXBITS,
22407c478bd9Sstevel@tonic-gate SPD_ATTR_ESPA_MAXBITS,
22417c478bd9Sstevel@tonic-gate SPD_ATTR_ENCR_MAXBITS
22427c478bd9Sstevel@tonic-gate };
22437c478bd9Sstevel@tonic-gate static const uint_t defbitsattr[SPDSOCK_NTYPES] = {
22447c478bd9Sstevel@tonic-gate SPD_ATTR_AH_DEFBITS,
22457c478bd9Sstevel@tonic-gate SPD_ATTR_ESPA_DEFBITS,
22467c478bd9Sstevel@tonic-gate SPD_ATTR_ENCR_DEFBITS
22477c478bd9Sstevel@tonic-gate };
22487c478bd9Sstevel@tonic-gate static const uint_t incrbitsattr[SPDSOCK_NTYPES] = {
22497c478bd9Sstevel@tonic-gate SPD_ATTR_AH_INCRBITS,
22507c478bd9Sstevel@tonic-gate SPD_ATTR_ESPA_INCRBITS,
22517c478bd9Sstevel@tonic-gate SPD_ATTR_ENCR_INCRBITS
22527c478bd9Sstevel@tonic-gate };
22537c478bd9Sstevel@tonic-gate
22547c478bd9Sstevel@tonic-gate #define ATTRPERALG 6 /* fixed attributes per algs */
22557c478bd9Sstevel@tonic-gate
22567c478bd9Sstevel@tonic-gate void
spdsock_alglist(queue_t * q,mblk_t * mp)22577c478bd9Sstevel@tonic-gate spdsock_alglist(queue_t *q, mblk_t *mp)
22587c478bd9Sstevel@tonic-gate {
22597c478bd9Sstevel@tonic-gate uint_t algtype;
22607c478bd9Sstevel@tonic-gate uint_t algidx;
22617c478bd9Sstevel@tonic-gate uint_t algcount;
22627c478bd9Sstevel@tonic-gate uint_t size;
22637c478bd9Sstevel@tonic-gate mblk_t *m;
22647c478bd9Sstevel@tonic-gate uint8_t *cur;
22657c478bd9Sstevel@tonic-gate spd_msg_t *msg;
22667c478bd9Sstevel@tonic-gate struct spd_ext_actions *act;
22677c478bd9Sstevel@tonic-gate struct spd_attribute *attr;
2268f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2269db061fdfSdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
22707c478bd9Sstevel@tonic-gate
2271f4b3ec61Sdh155122 mutex_enter(&ipss->ipsec_alg_lock);
22727c478bd9Sstevel@tonic-gate /*
22737c478bd9Sstevel@tonic-gate * The SPD client expects to receive separate entries for
22747c478bd9Sstevel@tonic-gate * AH authentication and ESP authentication supported algorithms.
22757c478bd9Sstevel@tonic-gate *
22767c478bd9Sstevel@tonic-gate * Don't return the "any" algorithms, if defined, as no
22777c478bd9Sstevel@tonic-gate * kernel policies can be set for these algorithms.
22787c478bd9Sstevel@tonic-gate */
2279f4b3ec61Sdh155122 algcount = 2 * ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
2280f4b3ec61Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
22817c478bd9Sstevel@tonic-gate
2282f4b3ec61Sdh155122 if (ipss->ipsec_alglists[IPSEC_ALG_AUTH][SADB_AALG_NONE] != NULL)
22837c478bd9Sstevel@tonic-gate algcount--;
2284f4b3ec61Sdh155122 if (ipss->ipsec_alglists[IPSEC_ALG_ENCR][SADB_EALG_NONE] != NULL)
22857c478bd9Sstevel@tonic-gate algcount--;
22867c478bd9Sstevel@tonic-gate
22877c478bd9Sstevel@tonic-gate /*
22887c478bd9Sstevel@tonic-gate * For each algorithm, we encode:
22897c478bd9Sstevel@tonic-gate * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
22907c478bd9Sstevel@tonic-gate */
22917c478bd9Sstevel@tonic-gate
22927c478bd9Sstevel@tonic-gate size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions) +
22937c478bd9Sstevel@tonic-gate ATTRPERALG * sizeof (struct spd_attribute) * algcount;
22947c478bd9Sstevel@tonic-gate
22957c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(size));
22967c478bd9Sstevel@tonic-gate
22977c478bd9Sstevel@tonic-gate m = allocb(size, BPRI_HI);
22987c478bd9Sstevel@tonic-gate if (m == NULL) {
2299f4b3ec61Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
23007c478bd9Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0);
23017c478bd9Sstevel@tonic-gate return;
23027c478bd9Sstevel@tonic-gate }
23037c478bd9Sstevel@tonic-gate
23047c478bd9Sstevel@tonic-gate m->b_wptr = m->b_rptr + size;
23057c478bd9Sstevel@tonic-gate cur = m->b_rptr;
23067c478bd9Sstevel@tonic-gate
23077c478bd9Sstevel@tonic-gate msg = (spd_msg_t *)cur;
23087c478bd9Sstevel@tonic-gate bcopy(mp->b_rptr, cur, sizeof (*msg));
23097c478bd9Sstevel@tonic-gate
23107c478bd9Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(size);
23117c478bd9Sstevel@tonic-gate msg->spd_msg_errno = 0;
23127c478bd9Sstevel@tonic-gate msg->spd_msg_diagnostic = 0;
23137c478bd9Sstevel@tonic-gate
23147c478bd9Sstevel@tonic-gate cur += sizeof (*msg);
23157c478bd9Sstevel@tonic-gate
23167c478bd9Sstevel@tonic-gate act = (struct spd_ext_actions *)cur;
23177c478bd9Sstevel@tonic-gate cur += sizeof (*act);
23187c478bd9Sstevel@tonic-gate
23197c478bd9Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
23207c478bd9Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION;
23217c478bd9Sstevel@tonic-gate act->spd_actions_count = algcount;
23227c478bd9Sstevel@tonic-gate act->spd_actions_reserved = 0;
23237c478bd9Sstevel@tonic-gate
23247c478bd9Sstevel@tonic-gate attr = (struct spd_attribute *)cur;
23257c478bd9Sstevel@tonic-gate
23267c478bd9Sstevel@tonic-gate #define EMIT(tag, value) { \
23277c478bd9Sstevel@tonic-gate attr->spd_attr_tag = (tag); \
23287c478bd9Sstevel@tonic-gate attr->spd_attr_value = (value); \
23297c478bd9Sstevel@tonic-gate attr++; \
23307c478bd9Sstevel@tonic-gate }
23317c478bd9Sstevel@tonic-gate
23327c478bd9Sstevel@tonic-gate /*
23337c478bd9Sstevel@tonic-gate * If you change the number of EMIT's here, change
23347c478bd9Sstevel@tonic-gate * ATTRPERALG above to match
23357c478bd9Sstevel@tonic-gate */
23367c478bd9Sstevel@tonic-gate #define EMITALGATTRS(_type) { \
23377c478bd9Sstevel@tonic-gate EMIT(algattr[_type], algid); /* 1 */ \
23387c478bd9Sstevel@tonic-gate EMIT(minbitsattr[_type], minbits); /* 2 */ \
23397c478bd9Sstevel@tonic-gate EMIT(maxbitsattr[_type], maxbits); /* 3 */ \
23407c478bd9Sstevel@tonic-gate EMIT(defbitsattr[_type], defbits); /* 4 */ \
23417c478bd9Sstevel@tonic-gate EMIT(incrbitsattr[_type], incr); /* 5 */ \
23427c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0); /* 6 */ \
23437c478bd9Sstevel@tonic-gate }
23447c478bd9Sstevel@tonic-gate
23457c478bd9Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2346f4b3ec61Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2347f4b3ec61Sdh155122 algidx++) {
2348f4b3ec61Sdh155122 int algid = ipss->ipsec_sortlist[algtype][algidx];
2349f4b3ec61Sdh155122 ipsec_alginfo_t *alg =
2350f4b3ec61Sdh155122 ipss->ipsec_alglists[algtype][algid];
23517c478bd9Sstevel@tonic-gate uint_t minbits = alg->alg_minbits;
23527c478bd9Sstevel@tonic-gate uint_t maxbits = alg->alg_maxbits;
23537c478bd9Sstevel@tonic-gate uint_t defbits = alg->alg_default_bits;
23547c478bd9Sstevel@tonic-gate uint_t incr = alg->alg_increment;
23557c478bd9Sstevel@tonic-gate
23567c478bd9Sstevel@tonic-gate if (algtype == IPSEC_ALG_AUTH) {
23577c478bd9Sstevel@tonic-gate if (algid == SADB_AALG_NONE)
23587c478bd9Sstevel@tonic-gate continue;
23597c478bd9Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_AH_AUTH);
23607c478bd9Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_ESP_AUTH);
23617c478bd9Sstevel@tonic-gate } else {
23627c478bd9Sstevel@tonic-gate if (algid == SADB_EALG_NONE)
23637c478bd9Sstevel@tonic-gate continue;
23647c478bd9Sstevel@tonic-gate ASSERT(algtype == IPSEC_ALG_ENCR);
23657c478bd9Sstevel@tonic-gate EMITALGATTRS(SPDSOCK_ESP_ENCR);
23667c478bd9Sstevel@tonic-gate }
23677c478bd9Sstevel@tonic-gate }
23687c478bd9Sstevel@tonic-gate }
23697c478bd9Sstevel@tonic-gate
2370f4b3ec61Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
23717c478bd9Sstevel@tonic-gate
23727c478bd9Sstevel@tonic-gate #undef EMITALGATTRS
23737c478bd9Sstevel@tonic-gate #undef EMIT
23747c478bd9Sstevel@tonic-gate #undef ATTRPERALG
23757c478bd9Sstevel@tonic-gate
23767c478bd9Sstevel@tonic-gate attr--;
23777c478bd9Sstevel@tonic-gate attr->spd_attr_tag = SPD_ATTR_END;
23787c478bd9Sstevel@tonic-gate
23797c478bd9Sstevel@tonic-gate freemsg(mp);
23807c478bd9Sstevel@tonic-gate qreply(q, m);
23817c478bd9Sstevel@tonic-gate }
23827c478bd9Sstevel@tonic-gate
23837c478bd9Sstevel@tonic-gate /*
23847c478bd9Sstevel@tonic-gate * Process a SPD_DUMPALGS request.
23857c478bd9Sstevel@tonic-gate */
23867c478bd9Sstevel@tonic-gate
2387628b0c67SMark Fenwick #define ATTRPERALG 9 /* fixed attributes per algs */
23887c478bd9Sstevel@tonic-gate
23897c478bd9Sstevel@tonic-gate void
spdsock_dumpalgs(queue_t * q,mblk_t * mp)23907c478bd9Sstevel@tonic-gate spdsock_dumpalgs(queue_t *q, mblk_t *mp)
23917c478bd9Sstevel@tonic-gate {
23927c478bd9Sstevel@tonic-gate uint_t algtype;
23937c478bd9Sstevel@tonic-gate uint_t algidx;
23947c478bd9Sstevel@tonic-gate uint_t size;
23957c478bd9Sstevel@tonic-gate mblk_t *m;
23967c478bd9Sstevel@tonic-gate uint8_t *cur;
23977c478bd9Sstevel@tonic-gate spd_msg_t *msg;
23987c478bd9Sstevel@tonic-gate struct spd_ext_actions *act;
23997c478bd9Sstevel@tonic-gate struct spd_attribute *attr;
24007c478bd9Sstevel@tonic-gate ipsec_alginfo_t *alg;
24017c478bd9Sstevel@tonic-gate uint_t algid;
24027c478bd9Sstevel@tonic-gate uint_t i;
24037c478bd9Sstevel@tonic-gate uint_t alg_size;
2404f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2405db061fdfSdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
24067c478bd9Sstevel@tonic-gate
2407f4b3ec61Sdh155122 mutex_enter(&ipss->ipsec_alg_lock);
24087c478bd9Sstevel@tonic-gate
24097c478bd9Sstevel@tonic-gate /*
24107c478bd9Sstevel@tonic-gate * For each algorithm, we encode:
24117c478bd9Sstevel@tonic-gate * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
24127c478bd9Sstevel@tonic-gate *
24137c478bd9Sstevel@tonic-gate * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE*
2414628b0c67SMark Fenwick * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_NPARAMS / ALG_PARAMS* /
2415628b0c67SMark Fenwick * ALG_MECHNAME / ALG_FLAGS / {END, NEXT}
24167c478bd9Sstevel@tonic-gate */
24177c478bd9Sstevel@tonic-gate
24187c478bd9Sstevel@tonic-gate /*
24197c478bd9Sstevel@tonic-gate * Compute the size of the SPD message.
24207c478bd9Sstevel@tonic-gate */
24217c478bd9Sstevel@tonic-gate size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions);
24227c478bd9Sstevel@tonic-gate
24237c478bd9Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2424f4b3ec61Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2425f4b3ec61Sdh155122 algidx++) {
2426f4b3ec61Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx];
2427f4b3ec61Sdh155122 alg = ipss->ipsec_alglists[algtype][algid];
24287c478bd9Sstevel@tonic-gate alg_size = sizeof (struct spd_attribute) *
24297c478bd9Sstevel@tonic-gate (ATTRPERALG + alg->alg_nkey_sizes +
2430628b0c67SMark Fenwick alg->alg_nblock_sizes + alg->alg_nparams) +
2431628b0c67SMark Fenwick CRYPTO_MAX_MECH_NAME;
24327c478bd9Sstevel@tonic-gate size += alg_size;
24337c478bd9Sstevel@tonic-gate }
24347c478bd9Sstevel@tonic-gate }
24357c478bd9Sstevel@tonic-gate
24367c478bd9Sstevel@tonic-gate ASSERT(ALIGNED64(size));
24377c478bd9Sstevel@tonic-gate
24387c478bd9Sstevel@tonic-gate m = allocb(size, BPRI_HI);
24397c478bd9Sstevel@tonic-gate if (m == NULL) {
2440f4b3ec61Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
24417c478bd9Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0);
24427c478bd9Sstevel@tonic-gate return;
24437c478bd9Sstevel@tonic-gate }
24447c478bd9Sstevel@tonic-gate
24457c478bd9Sstevel@tonic-gate m->b_wptr = m->b_rptr + size;
24467c478bd9Sstevel@tonic-gate cur = m->b_rptr;
24477c478bd9Sstevel@tonic-gate
24487c478bd9Sstevel@tonic-gate msg = (spd_msg_t *)cur;
24497c478bd9Sstevel@tonic-gate bcopy(mp->b_rptr, cur, sizeof (*msg));
24507c478bd9Sstevel@tonic-gate
24517c478bd9Sstevel@tonic-gate msg->spd_msg_len = SPD_8TO64(size);
24527c478bd9Sstevel@tonic-gate msg->spd_msg_errno = 0;
2453a1ba8781SMark Fenwick msg->spd_msg_type = SPD_ALGLIST;
2454a1ba8781SMark Fenwick
24557c478bd9Sstevel@tonic-gate msg->spd_msg_diagnostic = 0;
24567c478bd9Sstevel@tonic-gate
24577c478bd9Sstevel@tonic-gate cur += sizeof (*msg);
24587c478bd9Sstevel@tonic-gate
24597c478bd9Sstevel@tonic-gate act = (struct spd_ext_actions *)cur;
24607c478bd9Sstevel@tonic-gate cur += sizeof (*act);
24617c478bd9Sstevel@tonic-gate
24627c478bd9Sstevel@tonic-gate act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t));
24637c478bd9Sstevel@tonic-gate act->spd_actions_exttype = SPD_EXT_ACTION;
2464f4b3ec61Sdh155122 act->spd_actions_count = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] +
2465f4b3ec61Sdh155122 ipss->ipsec_nalgs[IPSEC_ALG_ENCR];
24667c478bd9Sstevel@tonic-gate act->spd_actions_reserved = 0;
24677c478bd9Sstevel@tonic-gate
2468a1ba8781SMark Fenwick /*
2469a1ba8781SMark Fenwick * If there aren't any algorithms registered, return an empty message.
2470a1ba8781SMark Fenwick * spdsock_get_ext() knows how to deal with this.
2471a1ba8781SMark Fenwick */
2472a1ba8781SMark Fenwick if (act->spd_actions_count == 0) {
2473a1ba8781SMark Fenwick act->spd_actions_len = 0;
2474a1ba8781SMark Fenwick mutex_exit(&ipss->ipsec_alg_lock);
2475a1ba8781SMark Fenwick goto error;
2476a1ba8781SMark Fenwick }
2477a1ba8781SMark Fenwick
24787c478bd9Sstevel@tonic-gate attr = (struct spd_attribute *)cur;
24797c478bd9Sstevel@tonic-gate
24807c478bd9Sstevel@tonic-gate #define EMIT(tag, value) { \
24817c478bd9Sstevel@tonic-gate attr->spd_attr_tag = (tag); \
24827c478bd9Sstevel@tonic-gate attr->spd_attr_value = (value); \
24837c478bd9Sstevel@tonic-gate attr++; \
24847c478bd9Sstevel@tonic-gate }
24857c478bd9Sstevel@tonic-gate
24867c478bd9Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
2487f4b3ec61Sdh155122 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype];
2488f4b3ec61Sdh155122 algidx++) {
24897c478bd9Sstevel@tonic-gate
2490f4b3ec61Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx];
2491f4b3ec61Sdh155122 alg = ipss->ipsec_alglists[algtype][algid];
24927c478bd9Sstevel@tonic-gate
24937c478bd9Sstevel@tonic-gate /*
24947c478bd9Sstevel@tonic-gate * If you change the number of EMIT's here, change
24957c478bd9Sstevel@tonic-gate * ATTRPERALG above to match
24967c478bd9Sstevel@tonic-gate */
24977c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_ID, algid);
24987c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_PROTO, algproto[algtype]);
24997c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_INCRBITS, alg->alg_increment);
25007c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_NKEYSIZES, alg->alg_nkey_sizes);
25017c478bd9Sstevel@tonic-gate for (i = 0; i < alg->alg_nkey_sizes; i++)
25027c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_KEYSIZE,
25037c478bd9Sstevel@tonic-gate alg->alg_key_sizes[i]);
25047c478bd9Sstevel@tonic-gate
25057c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_NBLOCKSIZES, alg->alg_nblock_sizes);
25067c478bd9Sstevel@tonic-gate for (i = 0; i < alg->alg_nblock_sizes; i++)
25077c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_BLOCKSIZE,
25087c478bd9Sstevel@tonic-gate alg->alg_block_sizes[i]);
25097c478bd9Sstevel@tonic-gate
2510628b0c67SMark Fenwick EMIT(SPD_ATTR_ALG_NPARAMS, alg->alg_nparams);
2511628b0c67SMark Fenwick for (i = 0; i < alg->alg_nparams; i++)
2512628b0c67SMark Fenwick EMIT(SPD_ATTR_ALG_PARAMS,
2513628b0c67SMark Fenwick alg->alg_params[i]);
2514628b0c67SMark Fenwick
2515628b0c67SMark Fenwick EMIT(SPD_ATTR_ALG_FLAGS, alg->alg_flags);
2516628b0c67SMark Fenwick
25177c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_ALG_MECHNAME, CRYPTO_MAX_MECH_NAME);
25187c478bd9Sstevel@tonic-gate bcopy(alg->alg_mech_name, attr, CRYPTO_MAX_MECH_NAME);
25197c478bd9Sstevel@tonic-gate attr = (struct spd_attribute *)((char *)attr +
25207c478bd9Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME);
25217c478bd9Sstevel@tonic-gate
25227c478bd9Sstevel@tonic-gate EMIT(SPD_ATTR_NEXT, 0);
25237c478bd9Sstevel@tonic-gate }
25247c478bd9Sstevel@tonic-gate }
25257c478bd9Sstevel@tonic-gate
2526f4b3ec61Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
25277c478bd9Sstevel@tonic-gate
25287c478bd9Sstevel@tonic-gate #undef EMITALGATTRS
25297c478bd9Sstevel@tonic-gate #undef EMIT
25307c478bd9Sstevel@tonic-gate #undef ATTRPERALG
25317c478bd9Sstevel@tonic-gate
25327c478bd9Sstevel@tonic-gate attr--;
25337c478bd9Sstevel@tonic-gate attr->spd_attr_tag = SPD_ATTR_END;
25347c478bd9Sstevel@tonic-gate
2535a1ba8781SMark Fenwick error:
25367c478bd9Sstevel@tonic-gate freemsg(mp);
25377c478bd9Sstevel@tonic-gate qreply(q, m);
25387c478bd9Sstevel@tonic-gate }
25397c478bd9Sstevel@tonic-gate
25407c478bd9Sstevel@tonic-gate /*
25417c478bd9Sstevel@tonic-gate * Do the actual work of processing an SPD_UPDATEALGS request. Can
25427c478bd9Sstevel@tonic-gate * be invoked either once IPsec is loaded on a cached request, or
25437c478bd9Sstevel@tonic-gate * when a request is received while IPsec is loaded.
25447c478bd9Sstevel@tonic-gate */
2545bd670b35SErik Nordmark static int
spdsock_do_updatealg(spd_ext_t * extv[],spd_stack_t * spds)2546bd670b35SErik Nordmark spdsock_do_updatealg(spd_ext_t *extv[], spd_stack_t *spds)
25477c478bd9Sstevel@tonic-gate {
25487c478bd9Sstevel@tonic-gate struct spd_ext_actions *actp;
25497c478bd9Sstevel@tonic-gate struct spd_attribute *attr, *endattr;
25507c478bd9Sstevel@tonic-gate uint64_t *start, *end;
25517c478bd9Sstevel@tonic-gate ipsec_alginfo_t *alg = NULL;
25527c478bd9Sstevel@tonic-gate ipsec_algtype_t alg_type = 0;
25537c478bd9Sstevel@tonic-gate boolean_t skip_alg = B_TRUE, doing_proto = B_FALSE;
25547c478bd9Sstevel@tonic-gate uint_t i, cur_key, cur_block, algid;
2555bd670b35SErik Nordmark int diag = -1;
25567c478bd9Sstevel@tonic-gate
2557f4b3ec61Sdh155122 ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
25587c478bd9Sstevel@tonic-gate
25597c478bd9Sstevel@tonic-gate /* parse the message, building the list of algorithms */
25607c478bd9Sstevel@tonic-gate
25617c478bd9Sstevel@tonic-gate actp = (struct spd_ext_actions *)extv[SPD_EXT_ACTION];
2562bd670b35SErik Nordmark if (actp == NULL)
2563bd670b35SErik Nordmark return (SPD_DIAGNOSTIC_NO_ACTION_EXT);
25647c478bd9Sstevel@tonic-gate
25657c478bd9Sstevel@tonic-gate start = (uint64_t *)actp;
25667c478bd9Sstevel@tonic-gate end = (start + actp->spd_actions_len);
25677c478bd9Sstevel@tonic-gate endattr = (struct spd_attribute *)end;
25687c478bd9Sstevel@tonic-gate attr = (struct spd_attribute *)&actp[1];
25697c478bd9Sstevel@tonic-gate
2570f4b3ec61Sdh155122 bzero(spds->spds_algs, IPSEC_NALGTYPES * IPSEC_MAX_ALGS *
25717c478bd9Sstevel@tonic-gate sizeof (ipsec_alginfo_t *));
25727c478bd9Sstevel@tonic-gate
25737c478bd9Sstevel@tonic-gate alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
25747c478bd9Sstevel@tonic-gate
25757c478bd9Sstevel@tonic-gate #define ALG_KEY_SIZES(a) (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t))
25767c478bd9Sstevel@tonic-gate #define ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t))
2577d0115d88SMark Fenwick #define ALG_PARAM_SIZES(a) (((a)->alg_nparams + 1) * sizeof (uint16_t))
25787c478bd9Sstevel@tonic-gate
25797c478bd9Sstevel@tonic-gate while (attr < endattr) {
25807c478bd9Sstevel@tonic-gate switch (attr->spd_attr_tag) {
25817c478bd9Sstevel@tonic-gate case SPD_ATTR_NOP:
25827c478bd9Sstevel@tonic-gate case SPD_ATTR_EMPTY:
25837c478bd9Sstevel@tonic-gate break;
25847c478bd9Sstevel@tonic-gate case SPD_ATTR_END:
25857c478bd9Sstevel@tonic-gate attr = endattr;
25867c478bd9Sstevel@tonic-gate /* FALLTHRU */
25877c478bd9Sstevel@tonic-gate case SPD_ATTR_NEXT:
25887c478bd9Sstevel@tonic-gate if (doing_proto) {
25897c478bd9Sstevel@tonic-gate doing_proto = B_FALSE;
25907c478bd9Sstevel@tonic-gate break;
25917c478bd9Sstevel@tonic-gate }
25927c478bd9Sstevel@tonic-gate if (skip_alg) {
25937c478bd9Sstevel@tonic-gate ipsec_alg_free(alg);
25947c478bd9Sstevel@tonic-gate } else {
25957c478bd9Sstevel@tonic-gate ipsec_alg_free(
2596f4b3ec61Sdh155122 spds->spds_algs[alg_type][alg->alg_id]);
2597f4b3ec61Sdh155122 spds->spds_algs[alg_type][alg->alg_id] =
2598f4b3ec61Sdh155122 alg;
25997c478bd9Sstevel@tonic-gate }
26007c478bd9Sstevel@tonic-gate alg = kmem_zalloc(sizeof (*alg), KM_SLEEP);
26017c478bd9Sstevel@tonic-gate break;
26027c478bd9Sstevel@tonic-gate
26037c478bd9Sstevel@tonic-gate case SPD_ATTR_ALG_ID:
26047c478bd9Sstevel@tonic-gate if (attr->spd_attr_value >= IPSEC_MAX_ALGS) {
2605f4b3ec61Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: "
26067c478bd9Sstevel@tonic-gate "invalid alg id %d\n",
26077c478bd9Sstevel@tonic-gate attr->spd_attr_value));
2608bd670b35SErik Nordmark diag = SPD_DIAGNOSTIC_ALG_ID_RANGE;
26097c478bd9Sstevel@tonic-gate goto bail;
26107c478bd9Sstevel@tonic-gate }
26117c478bd9Sstevel@tonic-gate alg->alg_id = attr->spd_attr_value;
26127c478bd9Sstevel@tonic-gate break;
26137c478bd9Sstevel@tonic-gate
26147c478bd9Sstevel@tonic-gate case SPD_ATTR_ALG_PROTO:
26157c478bd9Sstevel@tonic-gate /* find the alg type */
26167c478bd9Sstevel@tonic-gate for (i = 0; i < NALGPROTOS; i++)
26177c478bd9Sstevel@tonic-gate if (algproto[i] == attr->spd_attr_value)
26187c478bd9Sstevel@tonic-gate break;
26197c478bd9Sstevel@tonic-gate skip_alg = (i == NALGPROTOS);
26207c478bd9Sstevel@tonic-gate if (!skip_alg)
26217c478bd9Sstevel@tonic-gate alg_type = i;
26227c478bd9Sstevel@tonic-gate break;
26237c478bd9Sstevel@tonic-gate
26247c478bd9Sstevel@tonic-gate case SPD_ATTR_ALG_INCRBITS:
26257c478bd9Sstevel@tonic-gate alg->alg_increment = attr->spd_attr_value;
26267c478bd9Sstevel@tonic-gate break;
26277c478bd9Sstevel@tonic-gate
26287c478bd9Sstevel@tonic-gate case SPD_ATTR_ALG_NKEYSIZES:
26297c478bd9Sstevel@tonic-gate if (alg->alg_key_sizes != NULL) {
26307c478bd9Sstevel@tonic-gate kmem_free(alg->alg_key_sizes,
26317c478bd9Sstevel@tonic-gate ALG_KEY_SIZES(alg));
26327c478bd9Sstevel@tonic-gate }
26337c478bd9Sstevel@tonic-gate alg->alg_nkey_sizes = attr->spd_attr_value;
26347c478bd9Sstevel@tonic-gate /*
26357c478bd9Sstevel@tonic-gate * Allocate room for the trailing zero key size
26367c478bd9Sstevel@tonic-gate * value as well.
26377c478bd9Sstevel@tonic-gate */
26387c478bd9Sstevel@tonic-gate alg->alg_key_sizes = kmem_zalloc(ALG_KEY_SIZES(alg),
26397c478bd9Sstevel@tonic-gate KM_SLEEP);
26407c478bd9Sstevel@tonic-gate cur_key = 0;
26417c478bd9Sstevel@tonic-gate break;
26427c478bd9Sstevel@tonic-gate
26437c478bd9Sstevel@tonic-gate case SPD_ATTR_ALG_KEYSIZE:
26447c478bd9Sstevel@tonic-gate if (alg->alg_key_sizes == NULL ||
26457c478bd9Sstevel@tonic-gate cur_key >= alg->alg_nkey_sizes) {
2646f4b3ec61Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: "
26477c478bd9Sstevel@tonic-gate "too many key sizes\n"));
2648bd670b35SErik Nordmark diag = SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES;
26497c478bd9Sstevel@tonic-gate goto bail;
26507c478bd9Sstevel@tonic-gate }
26517c478bd9Sstevel@tonic-gate alg->alg_key_sizes[cur_key++] = attr->spd_attr_value;
26527c478bd9Sstevel@tonic-gate break;
26537c478bd9Sstevel@tonic-gate
2654628b0c67SMark Fenwick case SPD_ATTR_ALG_FLAGS:
2655628b0c67SMark Fenwick /*
2656628b0c67SMark Fenwick * Flags (bit mask). The alg_flags element of
2657628b0c67SMark Fenwick * ipsecalg_flags_t is only 8 bits wide. The
2658628b0c67SMark Fenwick * user can set the VALID bit, but we will ignore it
2659628b0c67SMark Fenwick * and make the decision is the algorithm is valid.
2660628b0c67SMark Fenwick */
2661628b0c67SMark Fenwick alg->alg_flags |= (uint8_t)attr->spd_attr_value;
2662628b0c67SMark Fenwick break;
2663628b0c67SMark Fenwick
26647c478bd9Sstevel@tonic-gate case SPD_ATTR_ALG_NBLOCKSIZES:
26657c478bd9Sstevel@tonic-gate if (alg->alg_block_sizes != NULL) {
26667c478bd9Sstevel@tonic-gate kmem_free(alg->alg_block_sizes,
26677c478bd9Sstevel@tonic-gate ALG_BLOCK_SIZES(alg));
26687c478bd9Sstevel@tonic-gate }
26697c478bd9Sstevel@tonic-gate alg->alg_nblock_sizes = attr->spd_attr_value;
26707c478bd9Sstevel@tonic-gate /*
26717c478bd9Sstevel@tonic-gate * Allocate room for the trailing zero block size
26727c478bd9Sstevel@tonic-gate * value as well.
26737c478bd9Sstevel@tonic-gate */
26747c478bd9Sstevel@tonic-gate alg->alg_block_sizes = kmem_zalloc(ALG_BLOCK_SIZES(alg),
26757c478bd9Sstevel@tonic-gate KM_SLEEP);
26767c478bd9Sstevel@tonic-gate cur_block = 0;
26777c478bd9Sstevel@tonic-gate break;
26787c478bd9Sstevel@tonic-gate
26797c478bd9Sstevel@tonic-gate case SPD_ATTR_ALG_BLOCKSIZE:
26807c478bd9Sstevel@tonic-gate if (alg->alg_block_sizes == NULL ||
26817c478bd9Sstevel@tonic-gate cur_block >= alg->alg_nblock_sizes) {
2682f4b3ec61Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: "
26837c478bd9Sstevel@tonic-gate "too many block sizes\n"));
2684bd670b35SErik Nordmark diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES;
26857c478bd9Sstevel@tonic-gate goto bail;
26867c478bd9Sstevel@tonic-gate }
26877c478bd9Sstevel@tonic-gate alg->alg_block_sizes[cur_block++] =
26887c478bd9Sstevel@tonic-gate attr->spd_attr_value;
26897c478bd9Sstevel@tonic-gate break;
26907c478bd9Sstevel@tonic-gate
2691628b0c67SMark Fenwick case SPD_ATTR_ALG_NPARAMS:
2692628b0c67SMark Fenwick if (alg->alg_params != NULL) {
2693628b0c67SMark Fenwick kmem_free(alg->alg_params,
2694d0115d88SMark Fenwick ALG_PARAM_SIZES(alg));
2695628b0c67SMark Fenwick }
2696628b0c67SMark Fenwick alg->alg_nparams = attr->spd_attr_value;
2697628b0c67SMark Fenwick /*
2698628b0c67SMark Fenwick * Allocate room for the trailing zero block size
2699628b0c67SMark Fenwick * value as well.
2700628b0c67SMark Fenwick */
2701d0115d88SMark Fenwick alg->alg_params = kmem_zalloc(ALG_PARAM_SIZES(alg),
2702628b0c67SMark Fenwick KM_SLEEP);
2703628b0c67SMark Fenwick cur_block = 0;
2704628b0c67SMark Fenwick break;
2705628b0c67SMark Fenwick
2706628b0c67SMark Fenwick case SPD_ATTR_ALG_PARAMS:
2707628b0c67SMark Fenwick if (alg->alg_params == NULL ||
2708628b0c67SMark Fenwick cur_block >= alg->alg_nparams) {
2709628b0c67SMark Fenwick ss1dbg(spds, ("spdsock_do_updatealg: "
2710628b0c67SMark Fenwick "too many params\n"));
2711bd670b35SErik Nordmark diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES;
2712628b0c67SMark Fenwick goto bail;
2713628b0c67SMark Fenwick }
2714628b0c67SMark Fenwick /*
2715628b0c67SMark Fenwick * Array contains: iv_len, icv_len, salt_len
2716628b0c67SMark Fenwick * Any additional parameters are currently ignored.
2717628b0c67SMark Fenwick */
2718628b0c67SMark Fenwick alg->alg_params[cur_block++] =
2719628b0c67SMark Fenwick attr->spd_attr_value;
2720628b0c67SMark Fenwick break;
2721628b0c67SMark Fenwick
27227c478bd9Sstevel@tonic-gate case SPD_ATTR_ALG_MECHNAME: {
27237c478bd9Sstevel@tonic-gate char *mech_name;
27247c478bd9Sstevel@tonic-gate
27257c478bd9Sstevel@tonic-gate if (attr->spd_attr_value > CRYPTO_MAX_MECH_NAME) {
2726f4b3ec61Sdh155122 ss1dbg(spds, ("spdsock_do_updatealg: "
27277c478bd9Sstevel@tonic-gate "mech name too long\n"));
2728bd670b35SErik Nordmark diag = SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN;
27297c478bd9Sstevel@tonic-gate goto bail;
27307c478bd9Sstevel@tonic-gate }
27317c478bd9Sstevel@tonic-gate mech_name = (char *)(attr + 1);
27327c478bd9Sstevel@tonic-gate bcopy(mech_name, alg->alg_mech_name,
27337c478bd9Sstevel@tonic-gate attr->spd_attr_value);
27347c478bd9Sstevel@tonic-gate alg->alg_mech_name[CRYPTO_MAX_MECH_NAME-1] = '\0';
27357c478bd9Sstevel@tonic-gate attr = (struct spd_attribute *)((char *)attr +
27367c478bd9Sstevel@tonic-gate attr->spd_attr_value);
27377c478bd9Sstevel@tonic-gate break;
27387c478bd9Sstevel@tonic-gate }
27397c478bd9Sstevel@tonic-gate
27407c478bd9Sstevel@tonic-gate case SPD_ATTR_PROTO_ID:
27417c478bd9Sstevel@tonic-gate doing_proto = B_TRUE;
27427c478bd9Sstevel@tonic-gate for (i = 0; i < NALGPROTOS; i++) {
27437c478bd9Sstevel@tonic-gate if (algproto[i] == attr->spd_attr_value) {
27447c478bd9Sstevel@tonic-gate alg_type = i;
27457c478bd9Sstevel@tonic-gate break;
27467c478bd9Sstevel@tonic-gate }
27477c478bd9Sstevel@tonic-gate }
27487c478bd9Sstevel@tonic-gate break;
27497c478bd9Sstevel@tonic-gate
27507c478bd9Sstevel@tonic-gate case SPD_ATTR_PROTO_EXEC_MODE:
27517c478bd9Sstevel@tonic-gate if (!doing_proto)
27527c478bd9Sstevel@tonic-gate break;
27537c478bd9Sstevel@tonic-gate for (i = 0; i < NEXECMODES; i++) {
27547c478bd9Sstevel@tonic-gate if (execmodes[i] == attr->spd_attr_value) {
2755f4b3ec61Sdh155122 spds->spds_algs_exec_mode[alg_type] = i;
27567c478bd9Sstevel@tonic-gate break;
27577c478bd9Sstevel@tonic-gate }
27587c478bd9Sstevel@tonic-gate }
27597c478bd9Sstevel@tonic-gate break;
27607c478bd9Sstevel@tonic-gate }
27617c478bd9Sstevel@tonic-gate attr++;
27627c478bd9Sstevel@tonic-gate }
27637c478bd9Sstevel@tonic-gate
27647c478bd9Sstevel@tonic-gate #undef ALG_KEY_SIZES
27657c478bd9Sstevel@tonic-gate #undef ALG_BLOCK_SIZES
2766d0115d88SMark Fenwick #undef ALG_PARAM_SIZES
27677c478bd9Sstevel@tonic-gate
27687c478bd9Sstevel@tonic-gate /* update the algorithm tables */
2769f4b3ec61Sdh155122 spdsock_merge_algs(spds);
27707c478bd9Sstevel@tonic-gate bail:
27717c478bd9Sstevel@tonic-gate /* cleanup */
27727c478bd9Sstevel@tonic-gate ipsec_alg_free(alg);
27737c478bd9Sstevel@tonic-gate for (alg_type = 0; alg_type < IPSEC_NALGTYPES; alg_type++)
27747c478bd9Sstevel@tonic-gate for (algid = 0; algid < IPSEC_MAX_ALGS; algid++)
2775f4b3ec61Sdh155122 if (spds->spds_algs[alg_type][algid] != NULL)
2776f4b3ec61Sdh155122 ipsec_alg_free(spds->spds_algs[alg_type][algid]);
2777bd670b35SErik Nordmark return (diag);
27787c478bd9Sstevel@tonic-gate }
27797c478bd9Sstevel@tonic-gate
27807c478bd9Sstevel@tonic-gate /*
27817c478bd9Sstevel@tonic-gate * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue
27827c478bd9Sstevel@tonic-gate * the request until IPsec loads. If IPsec is loaded, act on it
27837c478bd9Sstevel@tonic-gate * immediately.
27847c478bd9Sstevel@tonic-gate */
27857c478bd9Sstevel@tonic-gate
27867c478bd9Sstevel@tonic-gate static void
spdsock_updatealg(queue_t * q,mblk_t * mp,spd_ext_t * extv[])27877c478bd9Sstevel@tonic-gate spdsock_updatealg(queue_t *q, mblk_t *mp, spd_ext_t *extv[])
27887c478bd9Sstevel@tonic-gate {
2789f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2790f4b3ec61Sdh155122 spd_stack_t *spds = ss->spdsock_spds;
2791f4b3ec61Sdh155122 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec;
2792005d3febSMarek Pospisil uint32_t auditing = AU_AUDITING();
2793f4b3ec61Sdh155122
2794f4b3ec61Sdh155122 if (!ipsec_loaded(ipss)) {
27957c478bd9Sstevel@tonic-gate /*
27967c478bd9Sstevel@tonic-gate * IPsec is not loaded, save request and return nicely,
27977c478bd9Sstevel@tonic-gate * the message will be processed once IPsec loads.
27987c478bd9Sstevel@tonic-gate */
27997c478bd9Sstevel@tonic-gate mblk_t *new_mp;
28007c478bd9Sstevel@tonic-gate
28017c478bd9Sstevel@tonic-gate /* last update message wins */
28027c478bd9Sstevel@tonic-gate if ((new_mp = copymsg(mp)) == NULL) {
28037c478bd9Sstevel@tonic-gate spdsock_error(q, mp, ENOMEM, 0);
28047c478bd9Sstevel@tonic-gate return;
28057c478bd9Sstevel@tonic-gate }
2806f4b3ec61Sdh155122 mutex_enter(&spds->spds_alg_lock);
2807f4b3ec61Sdh155122 bcopy(extv, spds->spds_extv_algs,
28087c478bd9Sstevel@tonic-gate sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1));
2809f4b3ec61Sdh155122 if (spds->spds_mp_algs != NULL)
2810f4b3ec61Sdh155122 freemsg(spds->spds_mp_algs);
2811f4b3ec61Sdh155122 spds->spds_mp_algs = mp;
2812f4b3ec61Sdh155122 mutex_exit(&spds->spds_alg_lock);
2813005d3febSMarek Pospisil if (auditing) {
2814de8c4a14SErik Nordmark cred_t *cr;
2815de8c4a14SErik Nordmark pid_t cpid;
2816de8c4a14SErik Nordmark
2817de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
2818de8c4a14SErik Nordmark audit_pf_policy(SPD_UPDATEALGS, cr,
2819799bd290Spwernau spds->spds_netstack, NULL, B_TRUE, EAGAIN,
2820de8c4a14SErik Nordmark cpid);
2821de8c4a14SErik Nordmark }
28227c478bd9Sstevel@tonic-gate spd_echo(q, new_mp);
28237c478bd9Sstevel@tonic-gate } else {
28247c478bd9Sstevel@tonic-gate /*
28257c478bd9Sstevel@tonic-gate * IPsec is loaded, act on the message immediately.
28267c478bd9Sstevel@tonic-gate */
28277c478bd9Sstevel@tonic-gate int diag;
28287c478bd9Sstevel@tonic-gate
2829f4b3ec61Sdh155122 mutex_enter(&spds->spds_alg_lock);
2830bd670b35SErik Nordmark diag = spdsock_do_updatealg(extv, spds);
2831799bd290Spwernau if (diag == -1) {
2832bd670b35SErik Nordmark /* Keep the lock held while we walk the SA tables. */
2833bd670b35SErik Nordmark sadb_alg_update(IPSEC_ALG_ALL, 0, 0,
2834bd670b35SErik Nordmark spds->spds_netstack);
2835bd670b35SErik Nordmark mutex_exit(&spds->spds_alg_lock);
28367c478bd9Sstevel@tonic-gate spd_echo(q, mp);
2837005d3febSMarek Pospisil if (auditing) {
2838de8c4a14SErik Nordmark cred_t *cr;
2839de8c4a14SErik Nordmark pid_t cpid;
2840de8c4a14SErik Nordmark
2841de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
2842de8c4a14SErik Nordmark audit_pf_policy(SPD_UPDATEALGS, cr,
2843799bd290Spwernau spds->spds_netstack, NULL, B_TRUE, 0,
2844de8c4a14SErik Nordmark cpid);
2845de8c4a14SErik Nordmark }
2846799bd290Spwernau } else {
2847bd670b35SErik Nordmark mutex_exit(&spds->spds_alg_lock);
28487c478bd9Sstevel@tonic-gate spdsock_diag(q, mp, diag);
2849005d3febSMarek Pospisil if (auditing) {
2850de8c4a14SErik Nordmark cred_t *cr;
2851de8c4a14SErik Nordmark pid_t cpid;
2852de8c4a14SErik Nordmark
2853de8c4a14SErik Nordmark cr = msg_getcred(mp, &cpid);
2854de8c4a14SErik Nordmark audit_pf_policy(SPD_UPDATEALGS, cr,
2855799bd290Spwernau spds->spds_netstack, NULL, B_TRUE, diag,
2856de8c4a14SErik Nordmark cpid);
2857de8c4a14SErik Nordmark }
2858799bd290Spwernau }
28597c478bd9Sstevel@tonic-gate }
28607c478bd9Sstevel@tonic-gate }
28617c478bd9Sstevel@tonic-gate
28628810c16bSdanmcd /*
286301ac885fSDan McDonald * Find a tunnel instance (using the name to link ID mapping), and
286401ac885fSDan McDonald * update it after an IPsec change. We need to do this always in case
286501ac885fSDan McDonald * we add policy AFTER plumbing a tunnel. We also need to do this
286601ac885fSDan McDonald * because, as a side-effect, the tunnel's MTU is updated to reflect
286701ac885fSDan McDonald * any IPsec overhead in the itp's policy.
286801ac885fSDan McDonald */
286901ac885fSDan McDonald static void
update_iptun_policy(ipsec_tun_pol_t * itp)287001ac885fSDan McDonald update_iptun_policy(ipsec_tun_pol_t *itp)
287101ac885fSDan McDonald {
287201ac885fSDan McDonald datalink_id_t linkid;
287301ac885fSDan McDonald
287401ac885fSDan McDonald if (dls_mgmt_get_linkid(itp->itp_name, &linkid) == 0)
287501ac885fSDan McDonald iptun_set_policy(linkid, itp);
287601ac885fSDan McDonald }
287701ac885fSDan McDonald
287801ac885fSDan McDonald /*
28798810c16bSdanmcd * Sort through the mess of polhead options to retrieve an appropriate one.
28808810c16bSdanmcd * Returns NULL if we send an spdsock error. Returns a valid pointer if we
28818810c16bSdanmcd * found a valid polhead. Returns ALL_ACTIVE_POLHEADS (aka. -1) or
28828810c16bSdanmcd * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to
28838810c16bSdanmcd * act on ALL policy heads.
28848810c16bSdanmcd */
28858810c16bSdanmcd static ipsec_policy_head_t *
get_appropriate_polhead(queue_t * q,mblk_t * mp,spd_if_t * tunname,int spdid,int msgtype,ipsec_tun_pol_t ** itpp)28868810c16bSdanmcd get_appropriate_polhead(queue_t *q, mblk_t *mp, spd_if_t *tunname, int spdid,
28878810c16bSdanmcd int msgtype, ipsec_tun_pol_t **itpp)
28888810c16bSdanmcd {
28898810c16bSdanmcd ipsec_tun_pol_t *itp;
28908810c16bSdanmcd ipsec_policy_head_t *iph;
28918810c16bSdanmcd int errno;
28928810c16bSdanmcd char *tname;
28938810c16bSdanmcd boolean_t active;
28948810c16bSdanmcd spdsock_t *ss = (spdsock_t *)q->q_ptr;
2895db061fdfSdanmcd netstack_t *ns = ss->spdsock_spds->spds_netstack;
28968810c16bSdanmcd uint64_t gen; /* Placeholder */
28978810c16bSdanmcd
28988810c16bSdanmcd active = (spdid == SPD_ACTIVE);
28998810c16bSdanmcd *itpp = NULL;
29008810c16bSdanmcd if (!active && spdid != SPD_STANDBY) {
29018810c16bSdanmcd spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_SPDID);
29028810c16bSdanmcd return (NULL);
29038810c16bSdanmcd }
29048810c16bSdanmcd
29058810c16bSdanmcd if (tunname != NULL) {
29068810c16bSdanmcd /* Acting on a tunnel's SPD. */
29078810c16bSdanmcd tname = (char *)tunname->spd_if_name;
29088810c16bSdanmcd if (*tname == '\0') {
29098810c16bSdanmcd /* Handle all-polhead cases here. */
29108810c16bSdanmcd if (msgtype != SPD_FLUSH && msgtype != SPD_DUMP) {
29118810c16bSdanmcd spdsock_diag(q, mp,
29128810c16bSdanmcd SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
29138810c16bSdanmcd return (NULL);
29148810c16bSdanmcd }
29158810c16bSdanmcd return (active ? ALL_ACTIVE_POLHEADS :
29168810c16bSdanmcd ALL_INACTIVE_POLHEADS);
29178810c16bSdanmcd }
29188810c16bSdanmcd
2919db061fdfSdanmcd itp = get_tunnel_policy(tname, ns);
29208810c16bSdanmcd if (itp == NULL) {
29218810c16bSdanmcd if (msgtype != SPD_ADDRULE) {
29228810c16bSdanmcd /* "Tunnel not found" */
29238810c16bSdanmcd spdsock_error(q, mp, ENOENT, 0);
29248810c16bSdanmcd return (NULL);
29258810c16bSdanmcd }
29268810c16bSdanmcd
29278810c16bSdanmcd errno = 0;
2928db061fdfSdanmcd itp = create_tunnel_policy(tname, &errno, &gen, ns);
29298810c16bSdanmcd if (itp == NULL) {
29308810c16bSdanmcd /*
29318810c16bSdanmcd * Something very bad happened, most likely
29328810c16bSdanmcd * ENOMEM. Return an indicator.
29338810c16bSdanmcd */
29348810c16bSdanmcd spdsock_error(q, mp, errno, 0);
29358810c16bSdanmcd return (NULL);
29368810c16bSdanmcd }
29378810c16bSdanmcd }
293801ac885fSDan McDonald
293901ac885fSDan McDonald /* Match up the itp to an iptun instance. */
294001ac885fSDan McDonald update_iptun_policy(itp);
29412b24ab6bSSebastien Roy
29428810c16bSdanmcd *itpp = itp;
29438810c16bSdanmcd /* For spdsock dump state, set the polhead's name. */
29448810c16bSdanmcd if (msgtype == SPD_DUMP) {
2945db061fdfSdanmcd ITP_REFHOLD(itp);
2946db061fdfSdanmcd ss->spdsock_itp = itp;
29478810c16bSdanmcd ss->spdsock_dump_tunnel = itp->itp_flags &
29488810c16bSdanmcd (active ? ITPF_P_TUNNEL : ITPF_I_TUNNEL);
29498810c16bSdanmcd }
29508810c16bSdanmcd } else {
29518810c16bSdanmcd itp = NULL;
29528810c16bSdanmcd /* For spdsock dump state, indicate it's global policy. */
29538810c16bSdanmcd if (msgtype == SPD_DUMP)
2954db061fdfSdanmcd ss->spdsock_itp = NULL;
29558810c16bSdanmcd }
29568810c16bSdanmcd
29578810c16bSdanmcd if (active)
2958f4b3ec61Sdh155122 iph = (itp == NULL) ? ipsec_system_policy(ns) : itp->itp_policy;
29598810c16bSdanmcd else
2960f4b3ec61Sdh155122 iph = (itp == NULL) ? ipsec_inactive_policy(ns) :
29618810c16bSdanmcd itp->itp_inactive;
29628810c16bSdanmcd
29638810c16bSdanmcd ASSERT(iph != NULL);
29648810c16bSdanmcd if (itp != NULL) {
29658810c16bSdanmcd IPPH_REFHOLD(iph);
29668810c16bSdanmcd }
29678810c16bSdanmcd
29688810c16bSdanmcd return (iph);
29698810c16bSdanmcd }
29708810c16bSdanmcd
29717c478bd9Sstevel@tonic-gate static void
spdsock_parse(queue_t * q,mblk_t * mp)29727c478bd9Sstevel@tonic-gate spdsock_parse(queue_t *q, mblk_t *mp)
29737c478bd9Sstevel@tonic-gate {
29747c478bd9Sstevel@tonic-gate spd_msg_t *spmsg;
29757c478bd9Sstevel@tonic-gate spd_ext_t *extv[SPD_EXT_MAX + 1];
29767c478bd9Sstevel@tonic-gate uint_t msgsize;
29777c478bd9Sstevel@tonic-gate ipsec_policy_head_t *iph;
29788810c16bSdanmcd ipsec_tun_pol_t *itp;
29798810c16bSdanmcd spd_if_t *tunname;
2980f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
2981f4b3ec61Sdh155122 spd_stack_t *spds = ss->spdsock_spds;
2982f4b3ec61Sdh155122 netstack_t *ns = spds->spds_netstack;
2983f4b3ec61Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec;
29847c478bd9Sstevel@tonic-gate
29857c478bd9Sstevel@tonic-gate /* Make sure nothing's below me. */
29867c478bd9Sstevel@tonic-gate ASSERT(WR(q)->q_next == NULL);
29877c478bd9Sstevel@tonic-gate
29887c478bd9Sstevel@tonic-gate spmsg = (spd_msg_t *)mp->b_rptr;
29897c478bd9Sstevel@tonic-gate
29907c478bd9Sstevel@tonic-gate msgsize = SPD_64TO8(spmsg->spd_msg_len);
29917c478bd9Sstevel@tonic-gate
29927c478bd9Sstevel@tonic-gate if (msgdsize(mp) != msgsize) {
29937c478bd9Sstevel@tonic-gate /*
29947c478bd9Sstevel@tonic-gate * Message len incorrect w.r.t. actual size. Send an error
29957c478bd9Sstevel@tonic-gate * (EMSGSIZE). It may be necessary to massage things a
29967c478bd9Sstevel@tonic-gate * bit. For example, if the spd_msg_type is hosed,
29977c478bd9Sstevel@tonic-gate * I need to set it to SPD_RESERVED to get delivery to
29987c478bd9Sstevel@tonic-gate * do the right thing. Then again, maybe just letting
29997c478bd9Sstevel@tonic-gate * the error delivery do the right thing.
30007c478bd9Sstevel@tonic-gate */
3001f4b3ec61Sdh155122 ss2dbg(spds,
3002f4b3ec61Sdh155122 ("mblk (%lu) and base (%d) message sizes don't jibe.\n",
30037c478bd9Sstevel@tonic-gate msgdsize(mp), msgsize));
30047c478bd9Sstevel@tonic-gate spdsock_error(q, mp, EMSGSIZE, SPD_DIAGNOSTIC_NONE);
30057c478bd9Sstevel@tonic-gate return;
30067c478bd9Sstevel@tonic-gate }
30077c478bd9Sstevel@tonic-gate
30087c478bd9Sstevel@tonic-gate if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) {
30097c478bd9Sstevel@tonic-gate /* Get all message into one mblk. */
30107c478bd9Sstevel@tonic-gate if (pullupmsg(mp, -1) == 0) {
30117c478bd9Sstevel@tonic-gate /*
30127c478bd9Sstevel@tonic-gate * Something screwy happened.
30137c478bd9Sstevel@tonic-gate */
3014f4b3ec61Sdh155122 ss3dbg(spds, ("spdsock_parse: pullupmsg() failed.\n"));
30157c478bd9Sstevel@tonic-gate return;
30167c478bd9Sstevel@tonic-gate } else {
30177c478bd9Sstevel@tonic-gate spmsg = (spd_msg_t *)mp->b_rptr;
30187c478bd9Sstevel@tonic-gate }
30197c478bd9Sstevel@tonic-gate }
30207c478bd9Sstevel@tonic-gate
30217c478bd9Sstevel@tonic-gate switch (spdsock_get_ext(extv, spmsg, msgsize)) {
30227c478bd9Sstevel@tonic-gate case KGE_DUP:
30237c478bd9Sstevel@tonic-gate /* Handle duplicate extension. */
3024f4b3ec61Sdh155122 ss1dbg(spds, ("Got duplicate extension of type %d.\n",
30257c478bd9Sstevel@tonic-gate extv[0]->spd_ext_type));
30267c478bd9Sstevel@tonic-gate spdsock_diag(q, mp, dup_ext_diag[extv[0]->spd_ext_type]);
30277c478bd9Sstevel@tonic-gate return;
30287c478bd9Sstevel@tonic-gate case KGE_UNK:
30297c478bd9Sstevel@tonic-gate /* Handle unknown extension. */
3030f4b3ec61Sdh155122 ss1dbg(spds, ("Got unknown extension of type %d.\n",
30317c478bd9Sstevel@tonic-gate extv[0]->spd_ext_type));
30327c478bd9Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_UNKNOWN_EXT);
30337c478bd9Sstevel@tonic-gate return;
30347c478bd9Sstevel@tonic-gate case KGE_LEN:
30357c478bd9Sstevel@tonic-gate /* Length error. */
3036f4b3ec61Sdh155122 ss1dbg(spds, ("Length %d on extension type %d overrun or 0.\n",
30377c478bd9Sstevel@tonic-gate extv[0]->spd_ext_len, extv[0]->spd_ext_type));
30387c478bd9Sstevel@tonic-gate spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_EXTLEN);
30397c478bd9Sstevel@tonic-gate return;
30407c478bd9Sstevel@tonic-gate case KGE_CHK:
30417c478bd9Sstevel@tonic-gate /* Reality check failed. */
3042f4b3ec61Sdh155122 ss1dbg(spds, ("Reality check failed on extension type %d.\n",
30437c478bd9Sstevel@tonic-gate extv[0]->spd_ext_type));
30447c478bd9Sstevel@tonic-gate spdsock_diag(q, mp, bad_ext_diag[extv[0]->spd_ext_type]);
30457c478bd9Sstevel@tonic-gate return;
30467c478bd9Sstevel@tonic-gate default:
30477c478bd9Sstevel@tonic-gate /* Default case is no errors. */
30487c478bd9Sstevel@tonic-gate break;
30497c478bd9Sstevel@tonic-gate }
30507c478bd9Sstevel@tonic-gate
30517c478bd9Sstevel@tonic-gate /*
30527c478bd9Sstevel@tonic-gate * Special-case SPD_UPDATEALGS so as not to load IPsec.
30537c478bd9Sstevel@tonic-gate */
3054f4b3ec61Sdh155122 if (!ipsec_loaded(ipss) && spmsg->spd_msg_type != SPD_UPDATEALGS) {
30557c478bd9Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr;
30567c478bd9Sstevel@tonic-gate
30577c478bd9Sstevel@tonic-gate ASSERT(ss != NULL);
3058f4b3ec61Sdh155122 ipsec_loader_loadnow(ipss);
30597c478bd9Sstevel@tonic-gate ss->spdsock_timeout_arg = mp;
30607c478bd9Sstevel@tonic-gate ss->spdsock_timeout = qtimeout(q, spdsock_loadcheck,
30617c478bd9Sstevel@tonic-gate q, LOADCHECK_INTERVAL);
30627c478bd9Sstevel@tonic-gate return;
30637c478bd9Sstevel@tonic-gate }
30647c478bd9Sstevel@tonic-gate
30658810c16bSdanmcd /* First check for messages that need no polheads at all. */
30667c478bd9Sstevel@tonic-gate switch (spmsg->spd_msg_type) {
30677c478bd9Sstevel@tonic-gate case SPD_UPDATEALGS:
30687c478bd9Sstevel@tonic-gate spdsock_updatealg(q, mp, extv);
30697c478bd9Sstevel@tonic-gate return;
30707c478bd9Sstevel@tonic-gate case SPD_ALGLIST:
30717c478bd9Sstevel@tonic-gate spdsock_alglist(q, mp);
30727c478bd9Sstevel@tonic-gate return;
30737c478bd9Sstevel@tonic-gate case SPD_DUMPALGS:
30747c478bd9Sstevel@tonic-gate spdsock_dumpalgs(q, mp);
30757c478bd9Sstevel@tonic-gate return;
30768810c16bSdanmcd }
30777c478bd9Sstevel@tonic-gate
30788810c16bSdanmcd /*
30798810c16bSdanmcd * Then check for ones that need both primary/secondary polheads,
30808810c16bSdanmcd * finding the appropriate tunnel policy if need be.
30818810c16bSdanmcd */
30828810c16bSdanmcd tunname = (spd_if_t *)extv[SPD_EXT_TUN_NAME];
30838810c16bSdanmcd switch (spmsg->spd_msg_type) {
30848810c16bSdanmcd case SPD_FLIP:
30858810c16bSdanmcd spdsock_flip(q, mp, tunname);
30868810c16bSdanmcd return;
30878810c16bSdanmcd case SPD_CLONE:
30888810c16bSdanmcd spdsock_clone(q, mp, tunname);
30897c478bd9Sstevel@tonic-gate return;
30907c478bd9Sstevel@tonic-gate }
30918810c16bSdanmcd
30928810c16bSdanmcd /*
30938810c16bSdanmcd * Finally, find ones that operate on exactly one polhead, or
30948810c16bSdanmcd * "all polheads" of a given type (active/inactive).
30958810c16bSdanmcd */
30968810c16bSdanmcd iph = get_appropriate_polhead(q, mp, tunname, spmsg->spd_msg_spdid,
30978810c16bSdanmcd spmsg->spd_msg_type, &itp);
30988810c16bSdanmcd if (iph == NULL)
30998810c16bSdanmcd return;
31008810c16bSdanmcd
31018810c16bSdanmcd /* All-polheads-ready operations. */
31028810c16bSdanmcd switch (spmsg->spd_msg_type) {
31038810c16bSdanmcd case SPD_FLUSH:
31048810c16bSdanmcd if (itp != NULL) {
31058810c16bSdanmcd mutex_enter(&itp->itp_lock);
31068810c16bSdanmcd if (spmsg->spd_msg_spdid == SPD_ACTIVE)
31078810c16bSdanmcd itp->itp_flags &= ~ITPF_PFLAGS;
31088810c16bSdanmcd else
31098810c16bSdanmcd itp->itp_flags &= ~ITPF_IFLAGS;
31108810c16bSdanmcd mutex_exit(&itp->itp_lock);
311101ac885fSDan McDonald }
311201ac885fSDan McDonald
311301ac885fSDan McDonald spdsock_flush(q, iph, itp, mp);
311401ac885fSDan McDonald
311501ac885fSDan McDonald if (itp != NULL) {
311601ac885fSDan McDonald /* SPD_FLUSH is worth a tunnel MTU check. */
311701ac885fSDan McDonald update_iptun_policy(itp);
3118f4b3ec61Sdh155122 ITP_REFRELE(itp, ns);
31198810c16bSdanmcd }
31208810c16bSdanmcd return;
31218810c16bSdanmcd case SPD_DUMP:
31228810c16bSdanmcd if (itp != NULL)
3123f4b3ec61Sdh155122 ITP_REFRELE(itp, ns);
31248810c16bSdanmcd spdsock_dump(q, iph, mp);
31258810c16bSdanmcd return;
31268810c16bSdanmcd }
31278810c16bSdanmcd
31288810c16bSdanmcd if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) {
31298810c16bSdanmcd spdsock_diag(q, mp, SPD_DIAGNOSTIC_NOT_GLOBAL_OP);
31308810c16bSdanmcd return;
31318810c16bSdanmcd }
31328810c16bSdanmcd
31338810c16bSdanmcd /* Single-polhead-only operations. */
31348810c16bSdanmcd switch (spmsg->spd_msg_type) {
31358810c16bSdanmcd case SPD_ADDRULE:
31368810c16bSdanmcd spdsock_addrule(q, iph, mp, extv, itp);
31378810c16bSdanmcd break;
31388810c16bSdanmcd case SPD_DELETERULE:
31398810c16bSdanmcd spdsock_deleterule(q, iph, mp, extv, itp);
31408810c16bSdanmcd break;
31418810c16bSdanmcd case SPD_LOOKUP:
31428810c16bSdanmcd spdsock_lookup(q, iph, mp, extv, itp);
31438810c16bSdanmcd break;
31448810c16bSdanmcd default:
31458810c16bSdanmcd spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_MSG_TYPE);
31468810c16bSdanmcd break;
31478810c16bSdanmcd }
31488810c16bSdanmcd
3149db061fdfSdanmcd IPPH_REFRELE(iph, ns);
315001ac885fSDan McDonald if (itp != NULL) {
315101ac885fSDan McDonald /* SPD_{ADD,DELETE}RULE are worth a tunnel MTU check. */
315201ac885fSDan McDonald if (spmsg->spd_msg_type == SPD_ADDRULE ||
315301ac885fSDan McDonald spmsg->spd_msg_type == SPD_DELETERULE)
315401ac885fSDan McDonald update_iptun_policy(itp);
3155f4b3ec61Sdh155122 ITP_REFRELE(itp, ns);
31567c478bd9Sstevel@tonic-gate }
315701ac885fSDan McDonald }
31587c478bd9Sstevel@tonic-gate
31597c478bd9Sstevel@tonic-gate /*
31607c478bd9Sstevel@tonic-gate * If an algorithm mapping was received before IPsec was loaded, process it.
31617c478bd9Sstevel@tonic-gate * Called from the IPsec loader.
31627c478bd9Sstevel@tonic-gate */
31637c478bd9Sstevel@tonic-gate void
spdsock_update_pending_algs(netstack_t * ns)3164f4b3ec61Sdh155122 spdsock_update_pending_algs(netstack_t *ns)
31657c478bd9Sstevel@tonic-gate {
3166f4b3ec61Sdh155122 spd_stack_t *spds = ns->netstack_spdsock;
3167f4b3ec61Sdh155122
3168f4b3ec61Sdh155122 mutex_enter(&spds->spds_alg_lock);
3169*134a1f4eSCasper H.S. Dik if (spds->spds_mp_algs != NULL) {
3170bd670b35SErik Nordmark (void) spdsock_do_updatealg(spds->spds_extv_algs, spds);
3171*134a1f4eSCasper H.S. Dik freemsg(spds->spds_mp_algs);
3172*134a1f4eSCasper H.S. Dik spds->spds_mp_algs = NULL;
31737c478bd9Sstevel@tonic-gate }
3174f4b3ec61Sdh155122 mutex_exit(&spds->spds_alg_lock);
31757c478bd9Sstevel@tonic-gate }
31767c478bd9Sstevel@tonic-gate
31777c478bd9Sstevel@tonic-gate static void
spdsock_loadcheck(void * arg)31787c478bd9Sstevel@tonic-gate spdsock_loadcheck(void *arg)
31797c478bd9Sstevel@tonic-gate {
31807c478bd9Sstevel@tonic-gate queue_t *q = (queue_t *)arg;
31817c478bd9Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr;
31827c478bd9Sstevel@tonic-gate mblk_t *mp;
3183db061fdfSdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
31847c478bd9Sstevel@tonic-gate
31857c478bd9Sstevel@tonic-gate ASSERT(ss != NULL);
31867c478bd9Sstevel@tonic-gate
31877c478bd9Sstevel@tonic-gate ss->spdsock_timeout = 0;
31887c478bd9Sstevel@tonic-gate mp = ss->spdsock_timeout_arg;
31897c478bd9Sstevel@tonic-gate ASSERT(mp != NULL);
31907c478bd9Sstevel@tonic-gate ss->spdsock_timeout_arg = NULL;
3191f4b3ec61Sdh155122 if (ipsec_failed(ipss))
31927c478bd9Sstevel@tonic-gate spdsock_error(q, mp, EPROTONOSUPPORT, 0);
31937c478bd9Sstevel@tonic-gate else
31947c478bd9Sstevel@tonic-gate spdsock_parse(q, mp);
31957c478bd9Sstevel@tonic-gate }
31967c478bd9Sstevel@tonic-gate
31977c478bd9Sstevel@tonic-gate /*
31987c478bd9Sstevel@tonic-gate * Copy relevant state bits.
31997c478bd9Sstevel@tonic-gate */
32007c478bd9Sstevel@tonic-gate static void
spdsock_copy_info(struct T_info_ack * tap,spdsock_t * ss)32017c478bd9Sstevel@tonic-gate spdsock_copy_info(struct T_info_ack *tap, spdsock_t *ss)
32027c478bd9Sstevel@tonic-gate {
32037c478bd9Sstevel@tonic-gate *tap = spdsock_g_t_info_ack;
32047c478bd9Sstevel@tonic-gate tap->CURRENT_state = ss->spdsock_state;
32057c478bd9Sstevel@tonic-gate tap->OPT_size = spdsock_max_optsize;
32067c478bd9Sstevel@tonic-gate }
32077c478bd9Sstevel@tonic-gate
32087c478bd9Sstevel@tonic-gate /*
32097c478bd9Sstevel@tonic-gate * This routine responds to T_CAPABILITY_REQ messages. It is called by
32107c478bd9Sstevel@tonic-gate * spdsock_wput. Much of the T_CAPABILITY_ACK information is copied from
32117c478bd9Sstevel@tonic-gate * spdsock_g_t_info_ack. The current state of the stream is copied from
32127c478bd9Sstevel@tonic-gate * spdsock_state.
32137c478bd9Sstevel@tonic-gate */
32147c478bd9Sstevel@tonic-gate static void
spdsock_capability_req(queue_t * q,mblk_t * mp)32157c478bd9Sstevel@tonic-gate spdsock_capability_req(queue_t *q, mblk_t *mp)
32167c478bd9Sstevel@tonic-gate {
32177c478bd9Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr;
32187c478bd9Sstevel@tonic-gate t_uscalar_t cap_bits1;
32197c478bd9Sstevel@tonic-gate struct T_capability_ack *tcap;
32207c478bd9Sstevel@tonic-gate
32217c478bd9Sstevel@tonic-gate cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
32227c478bd9Sstevel@tonic-gate
32237c478bd9Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
32247c478bd9Sstevel@tonic-gate mp->b_datap->db_type, T_CAPABILITY_ACK);
32257c478bd9Sstevel@tonic-gate if (mp == NULL)
32267c478bd9Sstevel@tonic-gate return;
32277c478bd9Sstevel@tonic-gate
32287c478bd9Sstevel@tonic-gate tcap = (struct T_capability_ack *)mp->b_rptr;
32297c478bd9Sstevel@tonic-gate tcap->CAP_bits1 = 0;
32307c478bd9Sstevel@tonic-gate
32317c478bd9Sstevel@tonic-gate if (cap_bits1 & TC1_INFO) {
32327c478bd9Sstevel@tonic-gate spdsock_copy_info(&tcap->INFO_ack, ss);
32337c478bd9Sstevel@tonic-gate tcap->CAP_bits1 |= TC1_INFO;
32347c478bd9Sstevel@tonic-gate }
32357c478bd9Sstevel@tonic-gate
32367c478bd9Sstevel@tonic-gate qreply(q, mp);
32377c478bd9Sstevel@tonic-gate }
32387c478bd9Sstevel@tonic-gate
32397c478bd9Sstevel@tonic-gate /*
32407c478bd9Sstevel@tonic-gate * This routine responds to T_INFO_REQ messages. It is called by
32417c478bd9Sstevel@tonic-gate * spdsock_wput_other.
32427c478bd9Sstevel@tonic-gate * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack.
32437c478bd9Sstevel@tonic-gate * The current state of the stream is copied from spdsock_state.
32447c478bd9Sstevel@tonic-gate */
32457c478bd9Sstevel@tonic-gate static void
spdsock_info_req(q,mp)32467c478bd9Sstevel@tonic-gate spdsock_info_req(q, mp)
32477c478bd9Sstevel@tonic-gate queue_t *q;
32487c478bd9Sstevel@tonic-gate mblk_t *mp;
32497c478bd9Sstevel@tonic-gate {
32507c478bd9Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
32517c478bd9Sstevel@tonic-gate T_INFO_ACK);
32527c478bd9Sstevel@tonic-gate if (mp == NULL)
32537c478bd9Sstevel@tonic-gate return;
32547c478bd9Sstevel@tonic-gate spdsock_copy_info((struct T_info_ack *)mp->b_rptr,
32557c478bd9Sstevel@tonic-gate (spdsock_t *)q->q_ptr);
32567c478bd9Sstevel@tonic-gate qreply(q, mp);
32577c478bd9Sstevel@tonic-gate }
32587c478bd9Sstevel@tonic-gate
32597c478bd9Sstevel@tonic-gate /*
32607c478bd9Sstevel@tonic-gate * spdsock_err_ack. This routine creates a
32617c478bd9Sstevel@tonic-gate * T_ERROR_ACK message and passes it
32627c478bd9Sstevel@tonic-gate * upstream.
32637c478bd9Sstevel@tonic-gate */
32647c478bd9Sstevel@tonic-gate static void
spdsock_err_ack(q,mp,t_error,sys_error)32657c478bd9Sstevel@tonic-gate spdsock_err_ack(q, mp, t_error, sys_error)
32667c478bd9Sstevel@tonic-gate queue_t *q;
32677c478bd9Sstevel@tonic-gate mblk_t *mp;
32687c478bd9Sstevel@tonic-gate int t_error;
32697c478bd9Sstevel@tonic-gate int sys_error;
32707c478bd9Sstevel@tonic-gate {
32717c478bd9Sstevel@tonic-gate if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
32727c478bd9Sstevel@tonic-gate qreply(q, mp);
32737c478bd9Sstevel@tonic-gate }
32747c478bd9Sstevel@tonic-gate
32757c478bd9Sstevel@tonic-gate /*
32767c478bd9Sstevel@tonic-gate * This routine retrieves the current status of socket options.
32777c478bd9Sstevel@tonic-gate * It returns the size of the option retrieved.
32787c478bd9Sstevel@tonic-gate */
32797c478bd9Sstevel@tonic-gate /* ARGSUSED */
32807c478bd9Sstevel@tonic-gate int
spdsock_opt_get(queue_t * q,int level,int name,uchar_t * ptr)32817c478bd9Sstevel@tonic-gate spdsock_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
32827c478bd9Sstevel@tonic-gate {
32837c478bd9Sstevel@tonic-gate int *i1 = (int *)ptr;
32847c478bd9Sstevel@tonic-gate
32857c478bd9Sstevel@tonic-gate switch (level) {
32867c478bd9Sstevel@tonic-gate case SOL_SOCKET:
32877c478bd9Sstevel@tonic-gate switch (name) {
32887c478bd9Sstevel@tonic-gate case SO_TYPE:
32897c478bd9Sstevel@tonic-gate *i1 = SOCK_RAW;
32907c478bd9Sstevel@tonic-gate break;
32917c478bd9Sstevel@tonic-gate /*
32927c478bd9Sstevel@tonic-gate * The following two items can be manipulated,
32937c478bd9Sstevel@tonic-gate * but changing them should do nothing.
32947c478bd9Sstevel@tonic-gate */
32957c478bd9Sstevel@tonic-gate case SO_SNDBUF:
32967c478bd9Sstevel@tonic-gate *i1 = (int)q->q_hiwat;
32977c478bd9Sstevel@tonic-gate break;
32987c478bd9Sstevel@tonic-gate case SO_RCVBUF:
32997c478bd9Sstevel@tonic-gate *i1 = (int)(RD(q)->q_hiwat);
33007c478bd9Sstevel@tonic-gate break;
33017c478bd9Sstevel@tonic-gate }
33027c478bd9Sstevel@tonic-gate break;
33037c478bd9Sstevel@tonic-gate default:
33047c478bd9Sstevel@tonic-gate return (0);
33057c478bd9Sstevel@tonic-gate }
33067c478bd9Sstevel@tonic-gate return (sizeof (int));
33077c478bd9Sstevel@tonic-gate }
33087c478bd9Sstevel@tonic-gate
33097c478bd9Sstevel@tonic-gate /*
33107c478bd9Sstevel@tonic-gate * This routine sets socket options.
33117c478bd9Sstevel@tonic-gate */
33127c478bd9Sstevel@tonic-gate /* ARGSUSED */
33137c478bd9Sstevel@tonic-gate int
spdsock_opt_set(queue_t * q,uint_t mgmt_flags,int level,int name,uint_t inlen,uchar_t * invalp,uint_t * outlenp,uchar_t * outvalp,void * thisdg_attrs,cred_t * cr)33147c478bd9Sstevel@tonic-gate spdsock_opt_set(queue_t *q, uint_t mgmt_flags, int level, int name,
33157c478bd9Sstevel@tonic-gate uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
3316bd670b35SErik Nordmark void *thisdg_attrs, cred_t *cr)
33177c478bd9Sstevel@tonic-gate {
33187c478bd9Sstevel@tonic-gate int *i1 = (int *)invalp;
3319f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
3320f4b3ec61Sdh155122 spd_stack_t *spds = ss->spdsock_spds;
33217c478bd9Sstevel@tonic-gate
33227c478bd9Sstevel@tonic-gate switch (level) {
33237c478bd9Sstevel@tonic-gate case SOL_SOCKET:
33247c478bd9Sstevel@tonic-gate switch (name) {
33257c478bd9Sstevel@tonic-gate case SO_SNDBUF:
3326f4b3ec61Sdh155122 if (*i1 > spds->spds_max_buf)
33277c478bd9Sstevel@tonic-gate return (ENOBUFS);
33287c478bd9Sstevel@tonic-gate q->q_hiwat = *i1;
33297c478bd9Sstevel@tonic-gate break;
33307c478bd9Sstevel@tonic-gate case SO_RCVBUF:
3331f4b3ec61Sdh155122 if (*i1 > spds->spds_max_buf)
33327c478bd9Sstevel@tonic-gate return (ENOBUFS);
33337c478bd9Sstevel@tonic-gate RD(q)->q_hiwat = *i1;
33340f1702c5SYu Xiangning (void) proto_set_rx_hiwat(RD(q), NULL, *i1);
33357c478bd9Sstevel@tonic-gate break;
33367c478bd9Sstevel@tonic-gate }
33377c478bd9Sstevel@tonic-gate break;
33387c478bd9Sstevel@tonic-gate }
33397c478bd9Sstevel@tonic-gate return (0);
33407c478bd9Sstevel@tonic-gate }
33417c478bd9Sstevel@tonic-gate
33427c478bd9Sstevel@tonic-gate
33437c478bd9Sstevel@tonic-gate /*
33447c478bd9Sstevel@tonic-gate * Handle STREAMS messages.
33457c478bd9Sstevel@tonic-gate */
33467c478bd9Sstevel@tonic-gate static void
spdsock_wput_other(queue_t * q,mblk_t * mp)33477c478bd9Sstevel@tonic-gate spdsock_wput_other(queue_t *q, mblk_t *mp)
33487c478bd9Sstevel@tonic-gate {
33497c478bd9Sstevel@tonic-gate struct iocblk *iocp;
33507c478bd9Sstevel@tonic-gate int error;
3351f4b3ec61Sdh155122 spdsock_t *ss = (spdsock_t *)q->q_ptr;
3352f4b3ec61Sdh155122 spd_stack_t *spds = ss->spdsock_spds;
3353f4b3ec61Sdh155122 cred_t *cr;
33547c478bd9Sstevel@tonic-gate
33557c478bd9Sstevel@tonic-gate switch (mp->b_datap->db_type) {
33567c478bd9Sstevel@tonic-gate case M_PROTO:
33577c478bd9Sstevel@tonic-gate case M_PCPROTO:
33587c478bd9Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) {
3359f4b3ec61Sdh155122 ss3dbg(spds, (
33607c478bd9Sstevel@tonic-gate "spdsock_wput_other: Not big enough M_PROTO\n"));
33617c478bd9Sstevel@tonic-gate freemsg(mp);
33627c478bd9Sstevel@tonic-gate return;
33637c478bd9Sstevel@tonic-gate }
33647c478bd9Sstevel@tonic-gate switch (((union T_primitives *)mp->b_rptr)->type) {
33657c478bd9Sstevel@tonic-gate case T_CAPABILITY_REQ:
33667c478bd9Sstevel@tonic-gate spdsock_capability_req(q, mp);
3367f4b3ec61Sdh155122 break;
33687c478bd9Sstevel@tonic-gate case T_INFO_REQ:
33697c478bd9Sstevel@tonic-gate spdsock_info_req(q, mp);
3370f4b3ec61Sdh155122 break;
33717c478bd9Sstevel@tonic-gate case T_SVR4_OPTMGMT_REQ:
33727c478bd9Sstevel@tonic-gate case T_OPTMGMT_REQ:
3373de8c4a14SErik Nordmark /*
3374de8c4a14SErik Nordmark * All Solaris components should pass a db_credp
3375de8c4a14SErik Nordmark * for this TPI message, hence we ASSERT.
3376de8c4a14SErik Nordmark * But in case there is some other M_PROTO that looks
3377de8c4a14SErik Nordmark * like a TPI message sent by some other kernel
3378de8c4a14SErik Nordmark * component, we check and return an error.
3379de8c4a14SErik Nordmark */
3380de8c4a14SErik Nordmark cr = msg_getcred(mp, NULL);
3381de8c4a14SErik Nordmark ASSERT(cr != NULL);
3382de8c4a14SErik Nordmark if (cr == NULL) {
3383de8c4a14SErik Nordmark spdsock_err_ack(q, mp, TSYSERR, EINVAL);
3384de8c4a14SErik Nordmark return;
3385de8c4a14SErik Nordmark }
3386de8c4a14SErik Nordmark if (((union T_primitives *)mp->b_rptr)->type ==
3387de8c4a14SErik Nordmark T_SVR4_OPTMGMT_REQ) {
3388bd670b35SErik Nordmark svr4_optcom_req(q, mp, cr, &spdsock_opt_obj);
3389de8c4a14SErik Nordmark } else {
3390bd670b35SErik Nordmark tpi_optcom_req(q, mp, cr, &spdsock_opt_obj);
3391de8c4a14SErik Nordmark }
3392f4b3ec61Sdh155122 break;
33937c478bd9Sstevel@tonic-gate case T_DATA_REQ:
33947c478bd9Sstevel@tonic-gate case T_EXDATA_REQ:
33957c478bd9Sstevel@tonic-gate case T_ORDREL_REQ:
33967c478bd9Sstevel@tonic-gate /* Illegal for spdsock. */
33977c478bd9Sstevel@tonic-gate freemsg(mp);
33987c478bd9Sstevel@tonic-gate (void) putnextctl1(RD(q), M_ERROR, EPROTO);
3399f4b3ec61Sdh155122 break;
34007c478bd9Sstevel@tonic-gate default:
34017c478bd9Sstevel@tonic-gate /* Not supported by spdsock. */
34027c478bd9Sstevel@tonic-gate spdsock_err_ack(q, mp, TNOTSUPPORT, 0);
3403f4b3ec61Sdh155122 break;
34047c478bd9Sstevel@tonic-gate }
3405f4b3ec61Sdh155122 return;
34067c478bd9Sstevel@tonic-gate case M_IOCTL:
34077c478bd9Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr;
34087c478bd9Sstevel@tonic-gate error = EINVAL;
34097c478bd9Sstevel@tonic-gate
34107c478bd9Sstevel@tonic-gate switch (iocp->ioc_cmd) {
34117c478bd9Sstevel@tonic-gate case ND_SET:
34127c478bd9Sstevel@tonic-gate case ND_GET:
3413f4b3ec61Sdh155122 if (nd_getset(q, spds->spds_g_nd, mp)) {
34147c478bd9Sstevel@tonic-gate qreply(q, mp);
34157c478bd9Sstevel@tonic-gate return;
34167c478bd9Sstevel@tonic-gate } else
34177c478bd9Sstevel@tonic-gate error = ENOENT;
34187c478bd9Sstevel@tonic-gate /* FALLTHRU */
34197c478bd9Sstevel@tonic-gate default:
34207c478bd9Sstevel@tonic-gate miocnak(q, mp, 0, error);
34217c478bd9Sstevel@tonic-gate return;
34227c478bd9Sstevel@tonic-gate }
34237c478bd9Sstevel@tonic-gate case M_FLUSH:
34247c478bd9Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) {
34257c478bd9Sstevel@tonic-gate flushq(q, FLUSHALL);
34267c478bd9Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW;
34277c478bd9Sstevel@tonic-gate }
34287c478bd9Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) {
34297c478bd9Sstevel@tonic-gate qreply(q, mp);
34307c478bd9Sstevel@tonic-gate return;
34317c478bd9Sstevel@tonic-gate }
34327c478bd9Sstevel@tonic-gate /* Else FALLTHRU */
34337c478bd9Sstevel@tonic-gate }
34347c478bd9Sstevel@tonic-gate
34357c478bd9Sstevel@tonic-gate /* If fell through, just black-hole the message. */
34367c478bd9Sstevel@tonic-gate freemsg(mp);
34377c478bd9Sstevel@tonic-gate }
34387c478bd9Sstevel@tonic-gate
34397c478bd9Sstevel@tonic-gate static void
spdsock_wput(queue_t * q,mblk_t * mp)34407c478bd9Sstevel@tonic-gate spdsock_wput(queue_t *q, mblk_t *mp)
34417c478bd9Sstevel@tonic-gate {
34427c478bd9Sstevel@tonic-gate uint8_t *rptr = mp->b_rptr;
34437c478bd9Sstevel@tonic-gate mblk_t *mp1;
34447c478bd9Sstevel@tonic-gate spdsock_t *ss = (spdsock_t *)q->q_ptr;
3445f4b3ec61Sdh155122 spd_stack_t *spds = ss->spdsock_spds;
34467c478bd9Sstevel@tonic-gate
34477c478bd9Sstevel@tonic-gate /*
34487c478bd9Sstevel@tonic-gate * If we're dumping, defer processing other messages until the
34497c478bd9Sstevel@tonic-gate * dump completes.
34507c478bd9Sstevel@tonic-gate */
34517c478bd9Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) {
34527c478bd9Sstevel@tonic-gate if (!putq(q, mp))
34537c478bd9Sstevel@tonic-gate freemsg(mp);
34547c478bd9Sstevel@tonic-gate return;
34557c478bd9Sstevel@tonic-gate }
34567c478bd9Sstevel@tonic-gate
34577c478bd9Sstevel@tonic-gate switch (mp->b_datap->db_type) {
34587c478bd9Sstevel@tonic-gate case M_DATA:
34597c478bd9Sstevel@tonic-gate /*
34607c478bd9Sstevel@tonic-gate * Silently discard.
34617c478bd9Sstevel@tonic-gate */
3462f4b3ec61Sdh155122 ss2dbg(spds, ("raw M_DATA in spdsock.\n"));
34637c478bd9Sstevel@tonic-gate freemsg(mp);
34647c478bd9Sstevel@tonic-gate return;
34657c478bd9Sstevel@tonic-gate case M_PROTO:
34667c478bd9Sstevel@tonic-gate case M_PCPROTO:
34677c478bd9Sstevel@tonic-gate if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) {
34687c478bd9Sstevel@tonic-gate if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
34697c478bd9Sstevel@tonic-gate if ((mp1 = mp->b_cont) == NULL) {
34707c478bd9Sstevel@tonic-gate /* No data after T_DATA_REQ. */
3471f4b3ec61Sdh155122 ss2dbg(spds,
3472f4b3ec61Sdh155122 ("No data after DATA_REQ.\n"));
34737c478bd9Sstevel@tonic-gate freemsg(mp);
34747c478bd9Sstevel@tonic-gate return;
34757c478bd9Sstevel@tonic-gate }
34767c478bd9Sstevel@tonic-gate freeb(mp);
34777c478bd9Sstevel@tonic-gate mp = mp1;
3478f4b3ec61Sdh155122 ss2dbg(spds, ("T_DATA_REQ\n"));
34797c478bd9Sstevel@tonic-gate break; /* Out of switch. */
34807c478bd9Sstevel@tonic-gate }
34817c478bd9Sstevel@tonic-gate }
34827c478bd9Sstevel@tonic-gate /* FALLTHRU */
34837c478bd9Sstevel@tonic-gate default:
3484f4b3ec61Sdh155122 ss3dbg(spds, ("In default wput case (%d %d).\n",
34857c478bd9Sstevel@tonic-gate mp->b_datap->db_type, ((union T_primitives *)rptr)->type));
34867c478bd9Sstevel@tonic-gate spdsock_wput_other(q, mp);
34877c478bd9Sstevel@tonic-gate return;
34887c478bd9Sstevel@tonic-gate }
34897c478bd9Sstevel@tonic-gate
34907c478bd9Sstevel@tonic-gate /* I now have a PF_POLICY message in an M_DATA block. */
34917c478bd9Sstevel@tonic-gate spdsock_parse(q, mp);
34927c478bd9Sstevel@tonic-gate }
34937c478bd9Sstevel@tonic-gate
34947c478bd9Sstevel@tonic-gate /*
34957c478bd9Sstevel@tonic-gate * Device open procedure, called when new queue pair created.
34967c478bd9Sstevel@tonic-gate * We are passed the read-side queue.
34977c478bd9Sstevel@tonic-gate */
34987c478bd9Sstevel@tonic-gate /* ARGSUSED */
34997c478bd9Sstevel@tonic-gate static int
spdsock_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)35007c478bd9Sstevel@tonic-gate spdsock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
35017c478bd9Sstevel@tonic-gate {
35027c478bd9Sstevel@tonic-gate spdsock_t *ss;
35037c478bd9Sstevel@tonic-gate queue_t *oq = OTHERQ(q);
35047c478bd9Sstevel@tonic-gate minor_t ssminor;
3505f4b3ec61Sdh155122 netstack_t *ns;
3506f4b3ec61Sdh155122 spd_stack_t *spds;
35077c478bd9Sstevel@tonic-gate
3508f4b3ec61Sdh155122 if (secpolicy_ip_config(credp, B_FALSE) != 0)
35097c478bd9Sstevel@tonic-gate return (EPERM);
35107c478bd9Sstevel@tonic-gate
35117c478bd9Sstevel@tonic-gate if (q->q_ptr != NULL)
35127c478bd9Sstevel@tonic-gate return (0); /* Re-open of an already open instance. */
35137c478bd9Sstevel@tonic-gate
35147c478bd9Sstevel@tonic-gate if (sflag & MODOPEN)
35157c478bd9Sstevel@tonic-gate return (EINVAL);
35167c478bd9Sstevel@tonic-gate
3517f4b3ec61Sdh155122 ns = netstack_find_by_cred(credp);
3518f4b3ec61Sdh155122 ASSERT(ns != NULL);
3519f4b3ec61Sdh155122 spds = ns->netstack_spdsock;
3520f4b3ec61Sdh155122 ASSERT(spds != NULL);
3521f4b3ec61Sdh155122
3522f4b3ec61Sdh155122 ss2dbg(spds, ("Made it into PF_POLICY socket open.\n"));
35237c478bd9Sstevel@tonic-gate
35247c478bd9Sstevel@tonic-gate ssminor = (minor_t)(uintptr_t)vmem_alloc(spdsock_vmem, 1, VM_NOSLEEP);
3525f4b3ec61Sdh155122 if (ssminor == 0) {
3526f4b3ec61Sdh155122 netstack_rele(spds->spds_netstack);
35277c478bd9Sstevel@tonic-gate return (ENOMEM);
3528f4b3ec61Sdh155122 }
35297c478bd9Sstevel@tonic-gate ss = kmem_zalloc(sizeof (spdsock_t), KM_NOSLEEP);
35307c478bd9Sstevel@tonic-gate if (ss == NULL) {
35317c478bd9Sstevel@tonic-gate vmem_free(spdsock_vmem, (void *)(uintptr_t)ssminor, 1);
3532f4b3ec61Sdh155122 netstack_rele(spds->spds_netstack);
35337c478bd9Sstevel@tonic-gate return (ENOMEM);
35347c478bd9Sstevel@tonic-gate }
35357c478bd9Sstevel@tonic-gate
35367c478bd9Sstevel@tonic-gate ss->spdsock_minor = ssminor;
35377c478bd9Sstevel@tonic-gate ss->spdsock_state = TS_UNBND;
35387c478bd9Sstevel@tonic-gate ss->spdsock_dump_req = NULL;
35397c478bd9Sstevel@tonic-gate
3540f4b3ec61Sdh155122 ss->spdsock_spds = spds;
3541f4b3ec61Sdh155122
35427c478bd9Sstevel@tonic-gate q->q_ptr = ss;
35437c478bd9Sstevel@tonic-gate oq->q_ptr = ss;
35447c478bd9Sstevel@tonic-gate
3545f4b3ec61Sdh155122 q->q_hiwat = spds->spds_recv_hiwat;
35467c478bd9Sstevel@tonic-gate
3547f4b3ec61Sdh155122 oq->q_hiwat = spds->spds_xmit_hiwat;
3548f4b3ec61Sdh155122 oq->q_lowat = spds->spds_xmit_lowat;
35497c478bd9Sstevel@tonic-gate
35507c478bd9Sstevel@tonic-gate qprocson(q);
35510f1702c5SYu Xiangning (void) proto_set_rx_hiwat(q, NULL, spds->spds_recv_hiwat);
35527c478bd9Sstevel@tonic-gate
35537c478bd9Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), ss->spdsock_minor);
35547c478bd9Sstevel@tonic-gate return (0);
35557c478bd9Sstevel@tonic-gate }
35567c478bd9Sstevel@tonic-gate
35577c478bd9Sstevel@tonic-gate /*
35587c478bd9Sstevel@tonic-gate * Read-side service procedure, invoked when we get back-enabled
35597c478bd9Sstevel@tonic-gate * when buffer space becomes available.
35607c478bd9Sstevel@tonic-gate *
35617c478bd9Sstevel@tonic-gate * Dump another chunk if we were dumping before; when we finish, kick
35627c478bd9Sstevel@tonic-gate * the write-side queue in case it's waiting for read queue space.
35637c478bd9Sstevel@tonic-gate */
35647c478bd9Sstevel@tonic-gate void
spdsock_rsrv(queue_t * q)35657c478bd9Sstevel@tonic-gate spdsock_rsrv(queue_t *q)
35667c478bd9Sstevel@tonic-gate {
35677c478bd9Sstevel@tonic-gate spdsock_t *ss = q->q_ptr;
35687c478bd9Sstevel@tonic-gate
35697c478bd9Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL)
35707c478bd9Sstevel@tonic-gate spdsock_dump_some(q, ss);
35717c478bd9Sstevel@tonic-gate
35727c478bd9Sstevel@tonic-gate if (ss->spdsock_dump_req == NULL)
35737c478bd9Sstevel@tonic-gate qenable(OTHERQ(q));
35747c478bd9Sstevel@tonic-gate }
35757c478bd9Sstevel@tonic-gate
35767c478bd9Sstevel@tonic-gate /*
35777c478bd9Sstevel@tonic-gate * Write-side service procedure, invoked when we defer processing
35787c478bd9Sstevel@tonic-gate * if another message is received while a dump is in progress.
35797c478bd9Sstevel@tonic-gate */
35807c478bd9Sstevel@tonic-gate void
spdsock_wsrv(queue_t * q)35817c478bd9Sstevel@tonic-gate spdsock_wsrv(queue_t *q)
35827c478bd9Sstevel@tonic-gate {
35837c478bd9Sstevel@tonic-gate spdsock_t *ss = q->q_ptr;
35847c478bd9Sstevel@tonic-gate mblk_t *mp;
3585db061fdfSdanmcd ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec;
35867c478bd9Sstevel@tonic-gate
35877c478bd9Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL) {
35887c478bd9Sstevel@tonic-gate qenable(OTHERQ(q));
35897c478bd9Sstevel@tonic-gate return;
35907c478bd9Sstevel@tonic-gate }
35917c478bd9Sstevel@tonic-gate
35927c478bd9Sstevel@tonic-gate while ((mp = getq(q)) != NULL) {
3593f4b3ec61Sdh155122 if (ipsec_loaded(ipss)) {
35947c478bd9Sstevel@tonic-gate spdsock_wput(q, mp);
35957c478bd9Sstevel@tonic-gate if (ss->spdsock_dump_req != NULL)
35967c478bd9Sstevel@tonic-gate return;
3597f4b3ec61Sdh155122 } else if (!ipsec_failed(ipss)) {
35987c478bd9Sstevel@tonic-gate (void) putq(q, mp);
35997c478bd9Sstevel@tonic-gate } else {
36007c478bd9Sstevel@tonic-gate spdsock_error(q, mp, EPFNOSUPPORT, 0);
36017c478bd9Sstevel@tonic-gate }
36027c478bd9Sstevel@tonic-gate }
36037c478bd9Sstevel@tonic-gate }
36047c478bd9Sstevel@tonic-gate
36057c478bd9Sstevel@tonic-gate static int
spdsock_close(queue_t * q)36067c478bd9Sstevel@tonic-gate spdsock_close(queue_t *q)
36077c478bd9Sstevel@tonic-gate {
36087c478bd9Sstevel@tonic-gate spdsock_t *ss = q->q_ptr;
3609f4b3ec61Sdh155122 spd_stack_t *spds = ss->spdsock_spds;
36107c478bd9Sstevel@tonic-gate
36117c478bd9Sstevel@tonic-gate qprocsoff(q);
36127c478bd9Sstevel@tonic-gate
36137c478bd9Sstevel@tonic-gate /* Safe assumption. */
36147c478bd9Sstevel@tonic-gate ASSERT(ss != NULL);
36157c478bd9Sstevel@tonic-gate
36167c478bd9Sstevel@tonic-gate if (ss->spdsock_timeout != 0)
36177c478bd9Sstevel@tonic-gate (void) quntimeout(q, ss->spdsock_timeout);
36187c478bd9Sstevel@tonic-gate
3619f4b3ec61Sdh155122 ss3dbg(spds, ("Driver close, PF_POLICY socket is going away.\n"));
36207c478bd9Sstevel@tonic-gate
36217c478bd9Sstevel@tonic-gate vmem_free(spdsock_vmem, (void *)(uintptr_t)ss->spdsock_minor, 1);
3622f4b3ec61Sdh155122 netstack_rele(ss->spdsock_spds->spds_netstack);
36237c478bd9Sstevel@tonic-gate
36247c478bd9Sstevel@tonic-gate kmem_free(ss, sizeof (spdsock_t));
36257c478bd9Sstevel@tonic-gate return (0);
36267c478bd9Sstevel@tonic-gate }
36277c478bd9Sstevel@tonic-gate
36287c478bd9Sstevel@tonic-gate /*
36297c478bd9Sstevel@tonic-gate * Merge the IPsec algorithms tables with the received algorithm information.
36307c478bd9Sstevel@tonic-gate */
36317c478bd9Sstevel@tonic-gate void
spdsock_merge_algs(spd_stack_t * spds)3632f4b3ec61Sdh155122 spdsock_merge_algs(spd_stack_t *spds)
36337c478bd9Sstevel@tonic-gate {
36347c478bd9Sstevel@tonic-gate ipsec_alginfo_t *alg, *oalg;
36357c478bd9Sstevel@tonic-gate ipsec_algtype_t algtype;
36367c478bd9Sstevel@tonic-gate uint_t algidx, algid, nalgs;
36377c478bd9Sstevel@tonic-gate crypto_mech_name_t *mechs;
36387c478bd9Sstevel@tonic-gate uint_t mech_count, mech_idx;
3639f4b3ec61Sdh155122 netstack_t *ns = spds->spds_netstack;
3640f4b3ec61Sdh155122 ipsec_stack_t *ipss = ns->netstack_ipsec;
36417c478bd9Sstevel@tonic-gate
3642f4b3ec61Sdh155122 ASSERT(MUTEX_HELD(&spds->spds_alg_lock));
36437c478bd9Sstevel@tonic-gate
36447c478bd9Sstevel@tonic-gate /*
36457c478bd9Sstevel@tonic-gate * Get the list of supported mechanisms from the crypto framework.
36467c478bd9Sstevel@tonic-gate * If a mechanism is supported by KCF, resolve its mechanism
36477c478bd9Sstevel@tonic-gate * id and mark it as being valid. This operation must be done
36487c478bd9Sstevel@tonic-gate * without holding alg_lock, since it can cause a provider
36497c478bd9Sstevel@tonic-gate * module to be loaded and the provider notification callback to
36507c478bd9Sstevel@tonic-gate * be invoked.
36517c478bd9Sstevel@tonic-gate */
36527c478bd9Sstevel@tonic-gate mechs = crypto_get_mech_list(&mech_count, KM_SLEEP);
36537c478bd9Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
36547c478bd9Sstevel@tonic-gate for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
36557c478bd9Sstevel@tonic-gate int algflags = 0;
36567c478bd9Sstevel@tonic-gate crypto_mech_type_t mt = CRYPTO_MECHANISM_INVALID;
36577c478bd9Sstevel@tonic-gate
3658f4b3ec61Sdh155122 alg = spds->spds_algs[algtype][algid];
3659f4b3ec61Sdh155122 if (alg == NULL)
36607c478bd9Sstevel@tonic-gate continue;
36617c478bd9Sstevel@tonic-gate
36627c478bd9Sstevel@tonic-gate /*
36637c478bd9Sstevel@tonic-gate * The NULL encryption algorithm is a special
36647c478bd9Sstevel@tonic-gate * case because there are no mechanisms, yet
36657c478bd9Sstevel@tonic-gate * the algorithm is still valid.
36667c478bd9Sstevel@tonic-gate */
36677c478bd9Sstevel@tonic-gate if (alg->alg_id == SADB_EALG_NULL) {
36687c478bd9Sstevel@tonic-gate alg->alg_mech_type = CRYPTO_MECHANISM_INVALID;
3669628b0c67SMark Fenwick alg->alg_flags |= ALG_FLAG_VALID;
36707c478bd9Sstevel@tonic-gate continue;
36717c478bd9Sstevel@tonic-gate }
36727c478bd9Sstevel@tonic-gate
36737c478bd9Sstevel@tonic-gate for (mech_idx = 0; mech_idx < mech_count; mech_idx++) {
36747c478bd9Sstevel@tonic-gate if (strncmp(alg->alg_mech_name, mechs[mech_idx],
36757c478bd9Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME) == 0) {
36767c478bd9Sstevel@tonic-gate mt = crypto_mech2id(alg->alg_mech_name);
36777c478bd9Sstevel@tonic-gate ASSERT(mt != CRYPTO_MECHANISM_INVALID);
36787c478bd9Sstevel@tonic-gate algflags = ALG_FLAG_VALID;
36797c478bd9Sstevel@tonic-gate break;
36807c478bd9Sstevel@tonic-gate }
36817c478bd9Sstevel@tonic-gate }
36827c478bd9Sstevel@tonic-gate alg->alg_mech_type = mt;
3683628b0c67SMark Fenwick alg->alg_flags |= algflags;
36847c478bd9Sstevel@tonic-gate }
36857c478bd9Sstevel@tonic-gate }
36867c478bd9Sstevel@tonic-gate
3687f4b3ec61Sdh155122 mutex_enter(&ipss->ipsec_alg_lock);
36887c478bd9Sstevel@tonic-gate
36897c478bd9Sstevel@tonic-gate /*
36907c478bd9Sstevel@tonic-gate * For each algorithm currently defined, check if it is
36917c478bd9Sstevel@tonic-gate * present in the new tables created from the SPD_UPDATEALGS
36927c478bd9Sstevel@tonic-gate * message received from user-space.
36937c478bd9Sstevel@tonic-gate * Delete the algorithm entries that are currently defined
36947c478bd9Sstevel@tonic-gate * but not part of the new tables.
36957c478bd9Sstevel@tonic-gate */
36967c478bd9Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3697f4b3ec61Sdh155122 nalgs = ipss->ipsec_nalgs[algtype];
36987c478bd9Sstevel@tonic-gate for (algidx = 0; algidx < nalgs; algidx++) {
3699f4b3ec61Sdh155122 algid = ipss->ipsec_sortlist[algtype][algidx];
3700f4b3ec61Sdh155122 if (spds->spds_algs[algtype][algid] == NULL)
3701f4b3ec61Sdh155122 ipsec_alg_unreg(algtype, algid, ns);
37027c478bd9Sstevel@tonic-gate }
37037c478bd9Sstevel@tonic-gate }
37047c478bd9Sstevel@tonic-gate
37057c478bd9Sstevel@tonic-gate /*
37067c478bd9Sstevel@tonic-gate * For each algorithm we just received, check if it is
37077c478bd9Sstevel@tonic-gate * present in the currently defined tables. If it is, swap
37087c478bd9Sstevel@tonic-gate * the entry with the one we just allocated.
37097c478bd9Sstevel@tonic-gate * If the new algorithm is not in the current tables,
37107c478bd9Sstevel@tonic-gate * add it.
37117c478bd9Sstevel@tonic-gate */
37127c478bd9Sstevel@tonic-gate for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
37137c478bd9Sstevel@tonic-gate for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) {
3714f4b3ec61Sdh155122 alg = spds->spds_algs[algtype][algid];
3715f4b3ec61Sdh155122 if (alg == NULL)
37167c478bd9Sstevel@tonic-gate continue;
37177c478bd9Sstevel@tonic-gate
3718f4b3ec61Sdh155122 if ((oalg = ipss->ipsec_alglists[algtype][algid]) ==
3719f4b3ec61Sdh155122 NULL) {
37207c478bd9Sstevel@tonic-gate /*
37217c478bd9Sstevel@tonic-gate * New algorithm, add it to the algorithm
37227c478bd9Sstevel@tonic-gate * table.
37237c478bd9Sstevel@tonic-gate */
3724f4b3ec61Sdh155122 ipsec_alg_reg(algtype, alg, ns);
37257c478bd9Sstevel@tonic-gate } else {
37267c478bd9Sstevel@tonic-gate /*
37277c478bd9Sstevel@tonic-gate * Algorithm is already in the table. Swap
37287c478bd9Sstevel@tonic-gate * the existing entry with the new one.
37297c478bd9Sstevel@tonic-gate */
3730f4b3ec61Sdh155122 ipsec_alg_fix_min_max(alg, algtype, ns);
3731f4b3ec61Sdh155122 ipss->ipsec_alglists[algtype][algid] = alg;
37327c478bd9Sstevel@tonic-gate ipsec_alg_free(oalg);
37337c478bd9Sstevel@tonic-gate }
3734f4b3ec61Sdh155122 spds->spds_algs[algtype][algid] = NULL;
37357c478bd9Sstevel@tonic-gate }
37367c478bd9Sstevel@tonic-gate }
37377c478bd9Sstevel@tonic-gate
3738f4b3ec61Sdh155122 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) {
3739f4b3ec61Sdh155122 ipss->ipsec_algs_exec_mode[algtype] =
3740f4b3ec61Sdh155122 spds->spds_algs_exec_mode[algtype];
3741f4b3ec61Sdh155122 }
37427c478bd9Sstevel@tonic-gate
3743f4b3ec61Sdh155122 mutex_exit(&ipss->ipsec_alg_lock);
37447c478bd9Sstevel@tonic-gate
37457c478bd9Sstevel@tonic-gate crypto_free_mech_list(mechs, mech_count);
37467c478bd9Sstevel@tonic-gate
3747f4b3ec61Sdh155122 ipsecah_algs_changed(ns);
3748f4b3ec61Sdh155122 ipsecesp_algs_changed(ns);
37497c478bd9Sstevel@tonic-gate }
3750