4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 Nexenta Systems, Inc. All rights reserved.
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/stream.h>
29 #include <sys/strsubr.h>
30 #include <sys/strsun.h>
31 #include <sys/stropts.h>
33 #include <sys/vnode.h>
34 #include <sys/sysmacros.h>
35 #define _SUN_TPI_VERSION 2
36 #include <sys/tihdr.h>
38 #include <sys/sunddi.h>
39 #include <sys/mkdev.h>
40 #include <sys/debug.h>
42 #include <sys/cmn_err.h>
43 #include <sys/suntpi.h>
44 #include <sys/policy.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <net/pfkeyv2.h>
50 #include <net/pfpolicy.h>
52 #include <inet/common.h>
53 #include <netinet/ip6.h>
57 #include <inet/proto_set.h>
59 #include <inet/ip_if.h>
60 #include <inet/optcom.h>
61 #include <inet/ipsec_impl.h>
62 #include <inet/spdsock.h>
63 #include <inet/sadb.h>
64 #include <inet/iptun.h>
65 #include <inet/iptun/iptun_impl.h>
67 #include <sys/isa_defs.h>
72 * This is a transport provider for the PF_POLICY IPsec policy
73 * management socket, which provides a management interface into the
74 * SPD, allowing policy rules to be added, deleted, and queried.
76 * This effectively replaces the old private SIOC*IPSECONFIG ioctls
77 * with an extensible interface which will hopefully be public some
80 * See <net/pfpolicy.h> for more details on the protocol.
82 * We link against drv/ip and call directly into it to manipulate the
83 * SPD; see ipsec_impl.h for the policy data structures and spd.c for
84 * the code which maintains them.
86 * The MT model of this is QPAIR with the addition of some explicit
87 * locking to protect system-wide policy data structures.
90 static vmem_t
*spdsock_vmem
; /* for minor numbers. */
92 #define ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t))
94 /* Default structure copied into T_INFO_ACK messages (from rts.c...) */
95 static struct T_info_ack spdsock_g_t_info_ack
= {
97 T_INFINITE
, /* TSDU_size. Maximum size messages. */
98 T_INVALID
, /* ETSDU_size. No expedited data. */
99 T_INVALID
, /* CDATA_size. No connect data. */
100 T_INVALID
, /* DDATA_size. No disconnect data. */
102 0, /* OPT_size. No user-settable options */
103 64 * 1024, /* TIDU_size. spdsock allows maximum size messages. */
104 T_COTS
, /* SERV_type. spdsock supports connection oriented. */
105 TS_UNBND
, /* CURRENT_state. This is set from spdsock_state. */
106 (XPG4_1
) /* Provider flags */
109 /* Named Dispatch Parameter Management Structure */
110 typedef struct spdsockparam_s
{
111 uint_t spdsock_param_min
;
112 uint_t spdsock_param_max
;
113 uint_t spdsock_param_value
;
114 char *spdsock_param_name
;
118 * Table of NDD variables supported by spdsock. These are loaded into
119 * spdsock_g_nd in spdsock_init_nd.
120 * All of these are alterable, within the min/max values given, at run time.
122 static spdsockparam_t lcl_param_arr
[] = {
123 /* min max value name */
124 { 4096, 65536, 8192, "spdsock_xmit_hiwat"},
125 { 0, 65536, 1024, "spdsock_xmit_lowat"},
126 { 4096, 65536, 8192, "spdsock_recv_hiwat"},
127 { 65536, 1024*1024*1024, 256*1024, "spdsock_max_buf"},
128 { 0, 3, 0, "spdsock_debug"},
130 #define spds_xmit_hiwat spds_params[0].spdsock_param_value
131 #define spds_xmit_lowat spds_params[1].spdsock_param_value
132 #define spds_recv_hiwat spds_params[2].spdsock_param_value
133 #define spds_max_buf spds_params[3].spdsock_param_value
134 #define spds_debug spds_params[4].spdsock_param_value
136 #define ss0dbg(a) printf a
137 /* NOTE: != 0 instead of > 0 so lint doesn't complain. */
138 #define ss1dbg(spds, a) if (spds->spds_debug != 0) printf a
139 #define ss2dbg(spds, a) if (spds->spds_debug > 1) printf a
140 #define ss3dbg(spds, a) if (spds->spds_debug > 2) printf a
142 #define RESET_SPDSOCK_DUMP_POLHEAD(ss, iph) { \
143 ASSERT(RW_READ_HELD(&(iph)->iph_lock)); \
144 (ss)->spdsock_dump_head = (iph); \
145 (ss)->spdsock_dump_gen = (iph)->iph_gen; \
146 (ss)->spdsock_dump_cur_type = 0; \
147 (ss)->spdsock_dump_cur_af = IPSEC_AF_V4; \
148 (ss)->spdsock_dump_cur_rule = NULL; \
149 (ss)->spdsock_dump_count = 0; \
150 (ss)->spdsock_dump_cur_chain = 0; \
153 static int spdsock_close(queue_t
*);
154 static int spdsock_open(queue_t
*, dev_t
*, int, int, cred_t
*);
155 static void spdsock_wput(queue_t
*, mblk_t
*);
156 static void spdsock_wsrv(queue_t
*);
157 static void spdsock_rsrv(queue_t
*);
158 static void *spdsock_stack_init(netstackid_t stackid
, netstack_t
*ns
);
159 static void spdsock_stack_shutdown(netstackid_t stackid
, void *arg
);
160 static void spdsock_stack_fini(netstackid_t stackid
, void *arg
);
161 static void spdsock_loadcheck(void *);
162 static void spdsock_merge_algs(spd_stack_t
*);
163 static void spdsock_flush_one(ipsec_policy_head_t
*, netstack_t
*);
164 static mblk_t
*spdsock_dump_next_record(spdsock_t
*);
165 static void update_iptun_policy(ipsec_tun_pol_t
*);
167 static struct module_info info
= {
168 5138, "spdsock", 1, INFPSZ
, 512, 128
171 static struct qinit rinit
= {
172 NULL
, (pfi_t
)spdsock_rsrv
, spdsock_open
, spdsock_close
,
176 static struct qinit winit
= {
177 (pfi_t
)spdsock_wput
, (pfi_t
)spdsock_wsrv
, NULL
, NULL
, NULL
, &info
180 struct streamtab spdsockinfo
= {
184 /* mapping from alg type to protocol number, as per RFC 2407 */
185 static const uint_t algproto
[] = {
190 #define NALGPROTOS (sizeof (algproto) / sizeof (algproto[0]))
192 /* mapping from kernel exec mode to spdsock exec mode */
193 static const uint_t execmodes
[] = {
194 SPD_ALG_EXEC_MODE_SYNC
,
195 SPD_ALG_EXEC_MODE_ASYNC
198 #define NEXECMODES (sizeof (execmodes) / sizeof (execmodes[0]))
200 #define ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1)
201 #define ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2)
203 #define ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL)
213 spdsockparam_t
*spdsockpa
= (spdsockparam_t
*)cp
;
215 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
216 spd_stack_t
*spds
= ss
->spdsock_spds
;
218 mutex_enter(&spds
->spds_param_lock
);
219 value
= spdsockpa
->spdsock_param_value
;
220 mutex_exit(&spds
->spds_param_lock
);
222 (void) mi_mpprintf(mp
, "%u", value
);
226 /* This routine sets an NDD variable in a spdsockparam_t structure. */
237 spdsockparam_t
*spdsockpa
= (spdsockparam_t
*)cp
;
238 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
239 spd_stack_t
*spds
= ss
->spdsock_spds
;
241 /* Convert the value from a string into a long integer. */
242 if (ddi_strtoul(value
, NULL
, 10, &new_value
) != 0)
245 mutex_enter(&spds
->spds_param_lock
);
247 * Fail the request if the new value does not lie within the
250 if (new_value
< spdsockpa
->spdsock_param_min
||
251 new_value
> spdsockpa
->spdsock_param_max
) {
252 mutex_exit(&spds
->spds_param_lock
);
256 /* Set the new value */
257 spdsockpa
->spdsock_param_value
= new_value
;
258 mutex_exit(&spds
->spds_param_lock
);
264 * Initialize at module load time
267 spdsock_ddi_init(void)
269 spdsock_max_optsize
= optcom_max_optsize(
270 spdsock_opt_obj
.odb_opt_des_arr
, spdsock_opt_obj
.odb_opt_arr_cnt
);
272 spdsock_vmem
= vmem_create("spdsock", (void *)1, MAXMIN
, 1,
273 NULL
, NULL
, NULL
, 1, VM_SLEEP
| VMC_IDENTIFIER
);
276 * We want to be informed each time a stack is created or
277 * destroyed in the kernel, so we can maintain the
278 * set of spd_stack_t's.
280 netstack_register(NS_SPDSOCK
, spdsock_stack_init
,
281 spdsock_stack_shutdown
, spdsock_stack_fini
);
287 * Walk through the param array specified registering each element with the
288 * named dispatch handler.
291 spdsock_param_register(IDP
*ndp
, spdsockparam_t
*ssp
, int cnt
)
293 for (; cnt
-- > 0; ssp
++) {
294 if (ssp
->spdsock_param_name
!= NULL
&&
295 ssp
->spdsock_param_name
[0]) {
297 ssp
->spdsock_param_name
,
298 spdsock_param_get
, spdsock_param_set
,
309 * Initialize for each stack instance
313 spdsock_stack_init(netstackid_t stackid
, netstack_t
*ns
)
318 spds
= (spd_stack_t
*)kmem_zalloc(sizeof (*spds
), KM_SLEEP
);
319 spds
->spds_netstack
= ns
;
321 ASSERT(spds
->spds_g_nd
== NULL
);
323 ssp
= (spdsockparam_t
*)kmem_alloc(sizeof (lcl_param_arr
), KM_SLEEP
);
324 spds
->spds_params
= ssp
;
325 bcopy(lcl_param_arr
, ssp
, sizeof (lcl_param_arr
));
327 (void) spdsock_param_register(&spds
->spds_g_nd
, ssp
,
328 A_CNT(lcl_param_arr
));
330 mutex_init(&spds
->spds_param_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
331 mutex_init(&spds
->spds_alg_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
337 spdsock_ddi_destroy(void)
339 vmem_destroy(spdsock_vmem
);
341 netstack_unregister(NS_SPDSOCK
);
345 * Do pre-removal cleanup.
349 spdsock_stack_shutdown(netstackid_t stackid
, void *arg
)
351 spd_stack_t
*spds
= (spd_stack_t
*)arg
;
353 if (spds
->spds_mp_algs
!= NULL
) {
354 freemsg(spds
->spds_mp_algs
);
355 spds
->spds_mp_algs
= NULL
;
361 spdsock_stack_fini(netstackid_t stackid
, void *arg
)
363 spd_stack_t
*spds
= (spd_stack_t
*)arg
;
365 ASSERT(spds
->spds_mp_algs
== NULL
);
366 mutex_destroy(&spds
->spds_param_lock
);
367 mutex_destroy(&spds
->spds_alg_lock
);
368 nd_free(&spds
->spds_g_nd
);
369 kmem_free(spds
->spds_params
, sizeof (lcl_param_arr
));
370 spds
->spds_params
= NULL
;
372 kmem_free(spds
, sizeof (*spds
));
376 * NOTE: large quantities of this should be shared with keysock.
377 * Would be nice to combine some of this into a common module, but
378 * not possible given time pressures.
382 * High-level reality checking of extensions.
384 /* ARGSUSED */ /* XXX */
386 ext_check(spd_ext_t
*ext
)
388 spd_if_t
*tunname
= (spd_if_t
*)ext
;
392 if (ext
->spd_ext_type
== SPD_EXT_TUN_NAME
) {
393 /* (NOTE: Modified from SADB_EXT_IDENTITY..) */
396 * Make sure the strings in these identities are
397 * null-terminated. Let's "proactively" null-terminate the
398 * string at the last byte if it's not terminated sooner.
400 i
= SPD_64TO8(tunname
->spd_if_len
) - sizeof (spd_if_t
);
401 idstr
= (char *)(tunname
+ 1);
402 while (*idstr
!= '\0' && i
> 0) {
408 * I.e., if the bozo user didn't NULL-terminate the
415 return (B_TRUE
); /* For now... */
420 /* Return values for spdsock_get_ext(). */
428 * Parse basic extension headers and return in the passed-in pointer vector.
429 * Return values include:
431 * KGE_OK Everything's nice and parsed out.
432 * If there are no extensions, place NULL in extv[0].
433 * KGE_DUP There is a duplicate extension.
434 * First instance in appropriate bin. First duplicate in
436 * KGE_UNK Unknown extension type encountered. extv[0] contains
438 * KGE_LEN Extension length error.
439 * KGE_CHK High-level reality check failed on specific extension.
441 * My apologies for some of the pointer arithmetic in here. I'm thinking
442 * like an assembly programmer, yet trying to make the compiler happy.
445 spdsock_get_ext(spd_ext_t
*extv
[], spd_msg_t
*basehdr
, uint_t msgsize
)
447 bzero(extv
, sizeof (spd_ext_t
*) * (SPD_EXT_MAX
+ 1));
449 /* Use extv[0] as the "current working pointer". */
451 extv
[0] = (spd_ext_t
*)(basehdr
+ 1);
453 while (extv
[0] < (spd_ext_t
*)(((uint8_t *)basehdr
) + msgsize
)) {
454 /* Check for unknown headers. */
455 if (extv
[0]->spd_ext_type
== 0 ||
456 extv
[0]->spd_ext_type
> SPD_EXT_MAX
)
460 * Check length. Use uint64_t because extlen is in units
461 * of 64-bit words. If length goes beyond the msgsize,
462 * return an error. (Zero length also qualifies here.)
464 if (extv
[0]->spd_ext_len
== 0 ||
465 (void *)((uint64_t *)extv
[0] + extv
[0]->spd_ext_len
) >
466 (void *)((uint8_t *)basehdr
+ msgsize
))
469 /* Check for redundant headers. */
470 if (extv
[extv
[0]->spd_ext_type
] != NULL
)
474 * Reality check the extension if possible at the spdsock
477 if (!ext_check(extv
[0]))
480 /* If I make it here, assign the appropriate bin. */
481 extv
[extv
[0]->spd_ext_type
] = extv
[0];
483 /* Advance pointer (See above for uint64_t ptr reasoning.) */
484 extv
[0] = (spd_ext_t
*)
485 ((uint64_t *)extv
[0] + extv
[0]->spd_ext_len
);
488 /* Everything's cool. */
491 * If extv[0] == NULL, then there are no extension headers in this
492 * message. Ensure that this is the case.
494 if (extv
[0] == (spd_ext_t
*)(basehdr
+ 1))
500 static const int bad_ext_diag
[] = {
501 SPD_DIAGNOSTIC_MALFORMED_LCLPORT
,
502 SPD_DIAGNOSTIC_MALFORMED_REMPORT
,
503 SPD_DIAGNOSTIC_MALFORMED_PROTO
,
504 SPD_DIAGNOSTIC_MALFORMED_LCLADDR
,
505 SPD_DIAGNOSTIC_MALFORMED_REMADDR
,
506 SPD_DIAGNOSTIC_MALFORMED_ACTION
,
507 SPD_DIAGNOSTIC_MALFORMED_RULE
,
508 SPD_DIAGNOSTIC_MALFORMED_RULESET
,
509 SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE
512 static const int dup_ext_diag
[] = {
513 SPD_DIAGNOSTIC_DUPLICATE_LCLPORT
,
514 SPD_DIAGNOSTIC_DUPLICATE_REMPORT
,
515 SPD_DIAGNOSTIC_DUPLICATE_PROTO
,
516 SPD_DIAGNOSTIC_DUPLICATE_LCLADDR
,
517 SPD_DIAGNOSTIC_DUPLICATE_REMADDR
,
518 SPD_DIAGNOSTIC_DUPLICATE_ACTION
,
519 SPD_DIAGNOSTIC_DUPLICATE_RULE
,
520 SPD_DIAGNOSTIC_DUPLICATE_RULESET
,
521 SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE
525 * Transmit a PF_POLICY error message to the instance either pointed to
526 * by ks, the instance with serial number serial, or more, depending.
528 * The faulty message (or a reasonable facsimile thereof) is in mp.
529 * This function will free mp or recycle it for delivery, thereby causing
530 * the stream head to free it.
533 spdsock_error(queue_t
*q
, mblk_t
*mp
, int error
, int diagnostic
)
535 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
537 ASSERT(mp
->b_datap
->db_type
== M_DATA
);
539 if (spmsg
->spd_msg_type
< SPD_MIN
||
540 spmsg
->spd_msg_type
> SPD_MAX
)
541 spmsg
->spd_msg_type
= SPD_RESERVED
;
544 * Strip out extension headers.
546 ASSERT(mp
->b_rptr
+ sizeof (*spmsg
) <= mp
->b_datap
->db_lim
);
547 mp
->b_wptr
= mp
->b_rptr
+ sizeof (*spmsg
);
548 spmsg
->spd_msg_len
= SPD_8TO64(sizeof (spd_msg_t
));
549 spmsg
->spd_msg_errno
= (uint8_t)error
;
550 spmsg
->spd_msg_diagnostic
= (uint16_t)diagnostic
;
556 spdsock_diag(queue_t
*q
, mblk_t
*mp
, int diagnostic
)
558 spdsock_error(q
, mp
, EINVAL
, diagnostic
);
562 spd_echo(queue_t
*q
, mblk_t
*mp
)
568 * Do NOT consume a reference to itp.
572 spdsock_flush_node(ipsec_tun_pol_t
*itp
, void *cookie
, netstack_t
*ns
)
574 boolean_t active
= (boolean_t
)cookie
;
575 ipsec_policy_head_t
*iph
;
577 iph
= active
? itp
->itp_policy
: itp
->itp_inactive
;
579 mutex_enter(&itp
->itp_lock
);
580 spdsock_flush_one(iph
, ns
); /* Releases iph refhold. */
582 itp
->itp_flags
&= ~ITPF_PFLAGS
;
584 itp
->itp_flags
&= ~ITPF_IFLAGS
;
585 mutex_exit(&itp
->itp_lock
);
586 /* SPD_FLUSH is worth a tunnel MTU check. */
587 update_iptun_policy(itp
);
591 * Clear out one polhead.
594 spdsock_flush_one(ipsec_policy_head_t
*iph
, netstack_t
*ns
)
596 rw_enter(&iph
->iph_lock
, RW_WRITER
);
597 ipsec_polhead_flush(iph
, ns
);
598 rw_exit(&iph
->iph_lock
);
599 IPPH_REFRELE(iph
, ns
);
603 spdsock_flush(queue_t
*q
, ipsec_policy_head_t
*iph
, ipsec_tun_pol_t
*itp
,
607 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
608 netstack_t
*ns
= ss
->spdsock_spds
->spds_netstack
;
609 uint32_t auditing
= AU_AUDITING();
611 if (iph
!= ALL_ACTIVE_POLHEADS
&& iph
!= ALL_INACTIVE_POLHEADS
) {
612 spdsock_flush_one(iph
, ns
);
614 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
618 cr
= msg_getcred(mp
, &cpid
);
619 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
620 audit_pf_policy(SPD_FLUSH
, cr
, ns
,
621 ITP_NAME(itp
), active
, 0, cpid
);
624 active
= (iph
== ALL_ACTIVE_POLHEADS
);
626 /* First flush the global policy. */
627 spdsock_flush_one(active
? ipsec_system_policy(ns
) :
628 ipsec_inactive_policy(ns
), ns
);
633 cr
= msg_getcred(mp
, &cpid
);
634 audit_pf_policy(SPD_FLUSH
, cr
, ns
, NULL
,
637 /* Then flush every tunnel's appropriate one. */
638 itp_walk(spdsock_flush_node
, (void *)active
, ns
);
643 cr
= msg_getcred(mp
, &cpid
);
644 audit_pf_policy(SPD_FLUSH
, cr
, ns
,
645 "all tunnels", active
, 0, cpid
);
653 spdsock_ext_to_sel(spd_ext_t
**extv
, ipsec_selkey_t
*sel
, int *diag
)
655 bzero(sel
, sizeof (*sel
));
657 if (extv
[SPD_EXT_PROTO
] != NULL
) {
658 struct spd_proto
*pr
=
659 (struct spd_proto
*)extv
[SPD_EXT_PROTO
];
660 sel
->ipsl_proto
= pr
->spd_proto_number
;
661 sel
->ipsl_valid
|= IPSL_PROTOCOL
;
663 if (extv
[SPD_EXT_LCLPORT
] != NULL
) {
664 struct spd_portrange
*pr
=
665 (struct spd_portrange
*)extv
[SPD_EXT_LCLPORT
];
666 sel
->ipsl_lport
= pr
->spd_ports_minport
;
667 sel
->ipsl_valid
|= IPSL_LOCAL_PORT
;
669 if (extv
[SPD_EXT_REMPORT
] != NULL
) {
670 struct spd_portrange
*pr
=
671 (struct spd_portrange
*)extv
[SPD_EXT_REMPORT
];
672 sel
->ipsl_rport
= pr
->spd_ports_minport
;
673 sel
->ipsl_valid
|= IPSL_REMOTE_PORT
;
676 if (extv
[SPD_EXT_ICMP_TYPECODE
] != NULL
) {
677 struct spd_typecode
*tc
=
678 (struct spd_typecode
*)extv
[SPD_EXT_ICMP_TYPECODE
];
680 sel
->ipsl_valid
|= IPSL_ICMP_TYPE
;
681 sel
->ipsl_icmp_type
= tc
->spd_typecode_type
;
682 if (tc
->spd_typecode_type_end
< tc
->spd_typecode_type
)
683 sel
->ipsl_icmp_type_end
= tc
->spd_typecode_type
;
685 sel
->ipsl_icmp_type_end
= tc
->spd_typecode_type_end
;
687 if (tc
->spd_typecode_code
!= 255) {
688 sel
->ipsl_valid
|= IPSL_ICMP_CODE
;
689 sel
->ipsl_icmp_code
= tc
->spd_typecode_code
;
690 if (tc
->spd_typecode_code_end
< tc
->spd_typecode_code
)
691 sel
->ipsl_icmp_code_end
= tc
->spd_typecode_code
;
693 sel
->ipsl_icmp_code_end
=
694 tc
->spd_typecode_code_end
;
697 #define ADDR2SEL(sel, extv, field, pfield, extn, bit) \
698 if ((extv)[(extn)] != NULL) { \
700 struct spd_address *ap = \
701 (struct spd_address *)((extv)[(extn)]); \
702 addrlen = (ap->spd_address_af == AF_INET6) ? \
703 IPV6_ADDR_LEN : IP_ADDR_LEN; \
704 if (SPD_64TO8(ap->spd_address_len) < \
705 (addrlen + sizeof (*ap))) { \
706 *diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN; \
709 bcopy((ap+1), &((sel)->field), addrlen); \
710 (sel)->pfield = ap->spd_address_prefixlen; \
711 (sel)->ipsl_valid |= (bit); \
712 (sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ? \
713 IPSL_IPV6 : IPSL_IPV4; \
716 ADDR2SEL(sel
, extv
, ipsl_local
, ipsl_local_pfxlen
,
717 SPD_EXT_LCLADDR
, IPSL_LOCAL_ADDR
);
718 ADDR2SEL(sel
, extv
, ipsl_remote
, ipsl_remote_pfxlen
,
719 SPD_EXT_REMADDR
, IPSL_REMOTE_ADDR
);
721 if ((sel
->ipsl_valid
& (IPSL_IPV6
|IPSL_IPV4
)) ==
722 (IPSL_IPV6
|IPSL_IPV4
)) {
723 *diag
= SPD_DIAGNOSTIC_MIXED_AF
;
733 spd_convert_type(uint32_t type
, ipsec_act_t
*act
)
736 case SPD_ACTTYPE_DROP
:
737 act
->ipa_type
= IPSEC_ACT_DISCARD
;
740 case SPD_ACTTYPE_PASS
:
741 act
->ipa_type
= IPSEC_ACT_CLEAR
;
744 case SPD_ACTTYPE_IPSEC
:
745 act
->ipa_type
= IPSEC_ACT_APPLY
;
752 spd_convert_flags(uint32_t flags
, ipsec_act_t
*act
)
755 * Note use of !! for boolean canonicalization.
757 act
->ipa_apply
.ipp_use_ah
= !!(flags
& SPD_APPLY_AH
);
758 act
->ipa_apply
.ipp_use_esp
= !!(flags
& SPD_APPLY_ESP
);
759 act
->ipa_apply
.ipp_use_espa
= !!(flags
& SPD_APPLY_ESPA
);
760 act
->ipa_apply
.ipp_use_se
= !!(flags
& SPD_APPLY_SE
);
761 act
->ipa_apply
.ipp_use_unique
= !!(flags
& SPD_APPLY_UNIQUE
);
766 spdsock_reset_act(ipsec_act_t
*act
)
768 bzero(act
, sizeof (*act
));
769 act
->ipa_apply
.ipp_espe_maxbits
= IPSEC_MAX_KEYBITS
;
770 act
->ipa_apply
.ipp_espa_maxbits
= IPSEC_MAX_KEYBITS
;
771 act
->ipa_apply
.ipp_ah_maxbits
= IPSEC_MAX_KEYBITS
;
775 * Sanity check action against reality, and shrink-wrap key sizes..
778 spdsock_check_action(ipsec_act_t
*act
, boolean_t tunnel_polhead
, int *diag
,
781 if (tunnel_polhead
&& act
->ipa_apply
.ipp_use_unique
) {
782 *diag
= SPD_DIAGNOSTIC_ADD_INCON_FLAGS
;
785 if ((act
->ipa_type
!= IPSEC_ACT_APPLY
) &&
786 (act
->ipa_apply
.ipp_use_ah
||
787 act
->ipa_apply
.ipp_use_esp
||
788 act
->ipa_apply
.ipp_use_espa
||
789 act
->ipa_apply
.ipp_use_se
||
790 act
->ipa_apply
.ipp_use_unique
)) {
791 *diag
= SPD_DIAGNOSTIC_ADD_INCON_FLAGS
;
794 if ((act
->ipa_type
== IPSEC_ACT_APPLY
) &&
795 !act
->ipa_apply
.ipp_use_ah
&&
796 !act
->ipa_apply
.ipp_use_esp
) {
797 *diag
= SPD_DIAGNOSTIC_ADD_INCON_FLAGS
;
800 return (ipsec_check_action(act
, diag
, spds
->spds_netstack
));
804 * We may be short a few error checks here..
807 spdsock_ext_to_actvec(spd_ext_t
**extv
, ipsec_act_t
**actpp
, uint_t
*nactp
,
808 int *diag
, spd_stack_t
*spds
)
810 struct spd_ext_actions
*sactp
=
811 (struct spd_ext_actions
*)extv
[SPD_EXT_ACTION
];
812 ipsec_act_t act
, *actp
, *endactp
;
813 struct spd_attribute
*attrp
, *endattrp
;
816 boolean_t tunnel_polhead
;
818 tunnel_polhead
= (extv
[SPD_EXT_TUN_NAME
] != NULL
&&
819 (((struct spd_rule
*)extv
[SPD_EXT_RULE
])->spd_rule_flags
&
820 SPD_RULE_FLAG_TUNNEL
));
826 *diag
= SPD_DIAGNOSTIC_NO_ACTION_EXT
;
831 * Parse the "action" extension and convert into an action chain.
834 nact
= sactp
->spd_actions_count
;
836 endp
= (uint64_t *)sactp
;
837 endp
+= sactp
->spd_actions_len
;
838 endattrp
= (struct spd_attribute
*)endp
;
840 actp
= kmem_alloc(sizeof (*actp
) * nact
, KM_NOSLEEP
);
842 *diag
= SPD_DIAGNOSTIC_ADD_NO_MEM
;
847 endactp
= actp
+ nact
;
849 spdsock_reset_act(&act
);
850 attrp
= (struct spd_attribute
*)(&sactp
[1]);
852 for (; attrp
< endattrp
; attrp
++) {
853 switch (attrp
->spd_attr_tag
) {
858 spdsock_reset_act(&act
);
865 if (actp
>= endactp
) {
866 *diag
= SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT
;
869 if (!spdsock_check_action(&act
, tunnel_polhead
,
873 spdsock_reset_act(&act
);
877 if (!spd_convert_type(attrp
->spd_attr_value
, &act
)) {
878 *diag
= SPD_DIAGNOSTIC_ADD_BAD_TYPE
;
884 if (!tunnel_polhead
&& extv
[SPD_EXT_TUN_NAME
] != NULL
) {
886 * Set "sa unique" for transport-mode
887 * tunnels whether we want to or not.
889 attrp
->spd_attr_value
|= SPD_APPLY_UNIQUE
;
891 if (!spd_convert_flags(attrp
->spd_attr_value
, &act
)) {
892 *diag
= SPD_DIAGNOSTIC_ADD_BAD_FLAGS
;
897 case SPD_ATTR_AH_AUTH
:
898 if (attrp
->spd_attr_value
== 0) {
899 *diag
= SPD_DIAGNOSTIC_UNSUPP_AH_ALG
;
902 act
.ipa_apply
.ipp_auth_alg
= attrp
->spd_attr_value
;
905 case SPD_ATTR_ESP_ENCR
:
906 if (attrp
->spd_attr_value
== 0) {
907 *diag
= SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG
;
910 act
.ipa_apply
.ipp_encr_alg
= attrp
->spd_attr_value
;
913 case SPD_ATTR_ESP_AUTH
:
914 if (attrp
->spd_attr_value
== 0) {
915 *diag
= SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG
;
918 act
.ipa_apply
.ipp_esp_auth_alg
= attrp
->spd_attr_value
;
921 case SPD_ATTR_ENCR_MINBITS
:
922 act
.ipa_apply
.ipp_espe_minbits
= attrp
->spd_attr_value
;
925 case SPD_ATTR_ENCR_MAXBITS
:
926 act
.ipa_apply
.ipp_espe_maxbits
= attrp
->spd_attr_value
;
929 case SPD_ATTR_AH_MINBITS
:
930 act
.ipa_apply
.ipp_ah_minbits
= attrp
->spd_attr_value
;
933 case SPD_ATTR_AH_MAXBITS
:
934 act
.ipa_apply
.ipp_ah_maxbits
= attrp
->spd_attr_value
;
937 case SPD_ATTR_ESPA_MINBITS
:
938 act
.ipa_apply
.ipp_espa_minbits
= attrp
->spd_attr_value
;
941 case SPD_ATTR_ESPA_MAXBITS
:
942 act
.ipa_apply
.ipp_espa_maxbits
= attrp
->spd_attr_value
;
945 case SPD_ATTR_LIFE_SOFT_TIME
:
946 case SPD_ATTR_LIFE_HARD_TIME
:
947 case SPD_ATTR_LIFE_SOFT_BYTES
:
948 case SPD_ATTR_LIFE_HARD_BYTES
:
951 case SPD_ATTR_KM_PROTO
:
952 act
.ipa_apply
.ipp_km_proto
= attrp
->spd_attr_value
;
955 case SPD_ATTR_KM_COOKIE
:
956 act
.ipa_apply
.ipp_km_cookie
= attrp
->spd_attr_value
;
959 case SPD_ATTR_REPLAY_DEPTH
:
960 act
.ipa_apply
.ipp_replay_depth
= attrp
->spd_attr_value
;
964 if (actp
!= endactp
) {
965 *diag
= SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT
;
971 ipsec_actvec_free(*actpp
, nact
);
983 mkrule(ipsec_policy_head_t
*iph
, struct spd_rule
*rule
,
984 ipsec_selkey_t
*sel
, ipsec_act_t
*actp
, int nact
, uint_t dir
, uint_t af
,
985 tmprule_t
**rp
, uint64_t *index
, spd_stack_t
*spds
)
989 sel
->ipsl_valid
&= ~(IPSL_IPV6
|IPSL_IPV4
);
990 sel
->ipsl_valid
|= af
;
992 pol
= ipsec_policy_create(sel
, actp
, nact
, rule
->spd_rule_priority
,
993 index
, spds
->spds_netstack
);
1001 if (!ipsec_check_policy(iph
, pol
, dir
))
1004 rule
->spd_rule_index
= pol
->ipsp_index
;
1009 mkrulepair(ipsec_policy_head_t
*iph
, struct spd_rule
*rule
,
1010 ipsec_selkey_t
*sel
, ipsec_act_t
*actp
, int nact
, uint_t dir
, uint_t afs
,
1011 tmprule_t
**rp
, uint64_t *index
, spd_stack_t
*spds
)
1015 if (afs
& IPSL_IPV4
) {
1016 error
= mkrule(iph
, rule
, sel
, actp
, nact
, dir
, IPSL_IPV4
, rp
,
1021 if (afs
& IPSL_IPV6
) {
1022 error
= mkrule(iph
, rule
, sel
, actp
, nact
, dir
, IPSL_IPV6
, rp
,
1032 spdsock_addrule(queue_t
*q
, ipsec_policy_head_t
*iph
, mblk_t
*mp
,
1033 spd_ext_t
**extv
, ipsec_tun_pol_t
*itp
)
1038 int diag
= 0, error
, afs
;
1039 struct spd_rule
*rule
= (struct spd_rule
*)extv
[SPD_EXT_RULE
];
1040 tmprule_t rules
[4], *rulep
= &rules
[0];
1041 boolean_t tunnel_mode
, empty_itp
, active
;
1042 uint64_t *index
= (itp
== NULL
) ? NULL
: &itp
->itp_next_policy_index
;
1043 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
1044 spd_stack_t
*spds
= ss
->spdsock_spds
;
1045 uint32_t auditing
= AU_AUDITING();
1048 spdsock_diag(q
, mp
, SPD_DIAGNOSTIC_NO_RULE_EXT
);
1050 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
1054 cr
= msg_getcred(mp
, &cpid
);
1055 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
1056 audit_pf_policy(SPD_ADDRULE
, cr
,
1057 spds
->spds_netstack
, ITP_NAME(itp
), active
,
1058 SPD_DIAGNOSTIC_NO_RULE_EXT
, cpid
);
1063 tunnel_mode
= (rule
->spd_rule_flags
& SPD_RULE_FLAG_TUNNEL
);
1066 mutex_enter(&itp
->itp_lock
);
1067 ASSERT(itp
->itp_policy
== iph
|| itp
->itp_inactive
== iph
);
1068 active
= (itp
->itp_policy
== iph
);
1069 if (ITP_P_ISACTIVE(itp
, iph
)) {
1070 /* Check for mix-and-match of tunnel/transport. */
1071 if ((tunnel_mode
&& !ITP_P_ISTUNNEL(itp
, iph
)) ||
1072 (!tunnel_mode
&& ITP_P_ISTUNNEL(itp
, iph
))) {
1073 mutex_exit(&itp
->itp_lock
);
1074 spdsock_error(q
, mp
, EBUSY
, 0);
1077 empty_itp
= B_FALSE
;
1080 itp
->itp_flags
= active
? ITPF_P_ACTIVE
: ITPF_I_ACTIVE
;
1082 itp
->itp_flags
|= active
? ITPF_P_TUNNEL
:
1086 empty_itp
= B_FALSE
;
1089 if (rule
->spd_rule_index
!= 0) {
1090 diag
= SPD_DIAGNOSTIC_INVALID_RULE_INDEX
;
1095 if (!spdsock_ext_to_sel(extv
, &sel
, &diag
)) {
1102 if (sel
.ipsl_valid
&
1103 (IPSL_REMOTE_PORT
| IPSL_LOCAL_PORT
)) {
1104 itp
->itp_flags
|= active
?
1105 ITPF_P_PER_PORT_SECURITY
:
1106 ITPF_I_PER_PORT_SECURITY
;
1110 * For now, we don't allow transport-mode on a tunnel
1111 * with ANY specific selectors. Bail if we have such
1114 if (sel
.ipsl_valid
& IPSL_WILDCARD
) {
1115 diag
= SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS
;
1122 if (!spdsock_ext_to_actvec(extv
, &actp
, &nact
, &diag
, spds
)) {
1127 * If no addresses were specified, add both.
1129 afs
= sel
.ipsl_valid
& (IPSL_IPV6
|IPSL_IPV4
);
1131 afs
= (IPSL_IPV6
|IPSL_IPV4
);
1133 rw_enter(&iph
->iph_lock
, RW_WRITER
);
1135 if (rule
->spd_rule_flags
& SPD_RULE_FLAG_OUTBOUND
) {
1136 error
= mkrulepair(iph
, rule
, &sel
, actp
, nact
,
1137 IPSEC_TYPE_OUTBOUND
, afs
, &rulep
, index
, spds
);
1142 if (rule
->spd_rule_flags
& SPD_RULE_FLAG_INBOUND
) {
1143 error
= mkrulepair(iph
, rule
, &sel
, actp
, nact
,
1144 IPSEC_TYPE_INBOUND
, afs
, &rulep
, index
, spds
);
1149 while ((--rulep
) >= &rules
[0]) {
1150 ipsec_enter_policy(iph
, rulep
->pol
, rulep
->dir
,
1151 spds
->spds_netstack
);
1153 rw_exit(&iph
->iph_lock
);
1155 mutex_exit(&itp
->itp_lock
);
1157 ipsec_actvec_free(actp
, nact
);
1160 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
1164 cr
= msg_getcred(mp
, &cpid
);
1165 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
1166 audit_pf_policy(SPD_ADDRULE
, cr
, spds
->spds_netstack
,
1167 ITP_NAME(itp
), active
, 0, cpid
);
1172 rw_exit(&iph
->iph_lock
);
1173 while ((--rulep
) >= &rules
[0])
1174 IPPOL_REFRELE(rulep
->pol
);
1175 ipsec_actvec_free(actp
, nact
);
1180 mutex_exit(&itp
->itp_lock
);
1182 spdsock_error(q
, mp
, error
, diag
);
1184 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
1188 cr
= msg_getcred(mp
, &cpid
);
1189 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
1190 audit_pf_policy(SPD_ADDRULE
, cr
, spds
->spds_netstack
,
1191 ITP_NAME(itp
), active
, error
, cpid
);
1196 spdsock_deleterule(queue_t
*q
, ipsec_policy_head_t
*iph
, mblk_t
*mp
,
1197 spd_ext_t
**extv
, ipsec_tun_pol_t
*itp
)
1200 struct spd_rule
*rule
= (struct spd_rule
*)extv
[SPD_EXT_RULE
];
1202 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
1203 netstack_t
*ns
= ss
->spdsock_spds
->spds_netstack
;
1204 uint32_t auditing
= AU_AUDITING();
1207 spdsock_diag(q
, mp
, SPD_DIAGNOSTIC_NO_RULE_EXT
);
1210 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
1214 cr
= msg_getcred(mp
, &cpid
);
1215 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
1216 audit_pf_policy(SPD_DELETERULE
, cr
, ns
,
1217 ITP_NAME(itp
), active
, SPD_DIAGNOSTIC_NO_RULE_EXT
,
1224 * Must enter itp_lock first to avoid deadlock. See tun.c's
1225 * set_sec_simple() for the other case of itp_lock and iph_lock.
1228 mutex_enter(&itp
->itp_lock
);
1230 if (rule
->spd_rule_index
!= 0) {
1231 if (ipsec_policy_delete_index(iph
, rule
->spd_rule_index
, ns
) !=
1237 if (!spdsock_ext_to_sel(extv
, &sel
, &diag
)) {
1238 err
= EINVAL
; /* diag already set... */
1242 if ((rule
->spd_rule_flags
& SPD_RULE_FLAG_INBOUND
) &&
1243 !ipsec_policy_delete(iph
, &sel
, IPSEC_TYPE_INBOUND
, ns
)) {
1248 if ((rule
->spd_rule_flags
& SPD_RULE_FLAG_OUTBOUND
) &&
1249 !ipsec_policy_delete(iph
, &sel
, IPSEC_TYPE_OUTBOUND
, ns
)) {
1256 ASSERT(iph
== itp
->itp_policy
|| iph
== itp
->itp_inactive
);
1257 rw_enter(&iph
->iph_lock
, RW_READER
);
1258 if (avl_numnodes(&iph
->iph_rulebyid
) == 0) {
1259 if (iph
== itp
->itp_policy
)
1260 itp
->itp_flags
&= ~ITPF_PFLAGS
;
1262 itp
->itp_flags
&= ~ITPF_IFLAGS
;
1264 /* Can exit locks in any order. */
1265 rw_exit(&iph
->iph_lock
);
1266 mutex_exit(&itp
->itp_lock
);
1271 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
1275 cr
= msg_getcred(mp
, &cpid
);
1276 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
1277 audit_pf_policy(SPD_DELETERULE
, cr
, ns
, ITP_NAME(itp
),
1283 mutex_exit(&itp
->itp_lock
);
1284 spdsock_error(q
, mp
, err
, diag
);
1287 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
1291 cr
= msg_getcred(mp
, &cpid
);
1292 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
1293 audit_pf_policy(SPD_DELETERULE
, cr
, ns
, ITP_NAME(itp
),
1298 /* Do NOT consume a reference to itp. */
1301 spdsock_flip_node(ipsec_tun_pol_t
*itp
, void *ignoreme
, netstack_t
*ns
)
1303 mutex_enter(&itp
->itp_lock
);
1304 ITPF_SWAP(itp
->itp_flags
);
1305 ipsec_swap_policy(itp
->itp_policy
, itp
->itp_inactive
, ns
);
1306 mutex_exit(&itp
->itp_lock
);
1307 /* SPD_FLIP is worth a tunnel MTU check. */
1308 update_iptun_policy(itp
);
1312 spdsock_flip(queue_t
*q
, mblk_t
*mp
, spd_if_t
*tunname
)
1315 ipsec_tun_pol_t
*itp
;
1316 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
1317 netstack_t
*ns
= ss
->spdsock_spds
->spds_netstack
;
1318 uint32_t auditing
= AU_AUDITING();
1320 if (tunname
!= NULL
) {
1321 tname
= (char *)tunname
->spd_if_name
;
1322 if (*tname
== '\0') {
1324 ipsec_swap_global_policy(ns
);
1327 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
1331 cr
= msg_getcred(mp
, &cpid
);
1332 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
1333 audit_pf_policy(SPD_FLIP
, cr
, ns
,
1334 NULL
, active
, 0, cpid
);
1336 itp_walk(spdsock_flip_node
, NULL
, ns
);
1339 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
1343 cr
= msg_getcred(mp
, &cpid
);
1344 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
1345 audit_pf_policy(SPD_FLIP
, cr
, ns
,
1346 "all tunnels", active
, 0, cpid
);
1349 itp
= get_tunnel_policy(tname
, ns
);
1351 /* Better idea for "tunnel not found"? */
1352 spdsock_error(q
, mp
, ESRCH
, 0);
1356 (spd_msg_t
*)mp
->b_rptr
;
1360 cr
= msg_getcred(mp
, &cpid
);
1361 active
= (spmsg
->spd_msg_spdid
==
1363 audit_pf_policy(SPD_FLIP
, cr
, ns
,
1364 ITP_NAME(itp
), active
,
1369 spdsock_flip_node(itp
, NULL
, ns
);
1372 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
1376 cr
= msg_getcred(mp
, &cpid
);
1377 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
1378 audit_pf_policy(SPD_FLIP
, cr
, ns
,
1379 ITP_NAME(itp
), active
, 0, cpid
);
1381 ITP_REFRELE(itp
, ns
);
1384 ipsec_swap_global_policy(ns
); /* can't fail */
1387 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
1391 cr
= msg_getcred(mp
, &cpid
);
1392 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
1393 audit_pf_policy(SPD_FLIP
, cr
,
1394 ns
, NULL
, active
, 0, cpid
);
1401 * Unimplemented feature
1405 spdsock_lookup(queue_t
*q
, ipsec_policy_head_t
*iph
, mblk_t
*mp
,
1406 spd_ext_t
**extv
, ipsec_tun_pol_t
*itp
)
1408 spdsock_error(q
, mp
, EINVAL
, 0);
1413 spdsock_dump_ruleset(mblk_t
*req
, ipsec_policy_head_t
*iph
,
1414 uint32_t count
, uint16_t error
)
1416 size_t len
= sizeof (spd_ruleset_ext_t
) + sizeof (spd_msg_t
);
1418 spd_ruleset_ext_t
*ruleset
;
1419 mblk_t
*m
= allocb(len
, BPRI_HI
);
1421 ASSERT(RW_READ_HELD(&iph
->iph_lock
));
1426 msg
= (spd_msg_t
*)m
->b_rptr
;
1427 ruleset
= (spd_ruleset_ext_t
*)(&msg
[1]);
1429 m
->b_wptr
= (uint8_t *)&ruleset
[1];
1431 *msg
= *(spd_msg_t
*)(req
->b_rptr
);
1432 msg
->spd_msg_len
= SPD_8TO64(len
);
1433 msg
->spd_msg_errno
= error
;
1435 ruleset
->spd_ruleset_len
= SPD_8TO64(sizeof (*ruleset
));
1436 ruleset
->spd_ruleset_type
= SPD_EXT_RULESET
;
1437 ruleset
->spd_ruleset_count
= count
;
1438 ruleset
->spd_ruleset_version
= iph
->iph_gen
;
1443 spdsock_dump_finish(spdsock_t
*ss
, int error
)
1446 ipsec_policy_head_t
*iph
= ss
->spdsock_dump_head
;
1447 mblk_t
*req
= ss
->spdsock_dump_req
;
1448 netstack_t
*ns
= ss
->spdsock_spds
->spds_netstack
;
1450 rw_enter(&iph
->iph_lock
, RW_READER
);
1451 m
= spdsock_dump_ruleset(req
, iph
, ss
->spdsock_dump_count
, error
);
1452 rw_exit(&iph
->iph_lock
);
1453 IPPH_REFRELE(iph
, ns
);
1454 if (ss
->spdsock_itp
!= NULL
) {
1455 ITP_REFRELE(ss
->spdsock_itp
, ns
);
1456 ss
->spdsock_itp
= NULL
;
1458 ss
->spdsock_dump_req
= NULL
;
1465 * Rule encoding functions.
1466 * We do a two-pass encode.
1467 * If base != NULL, fill in encoded rule part starting at base+offset.
1468 * Always return "offset" plus length of to-be-encoded data.
1471 spdsock_encode_typecode(uint8_t *base
, uint_t offset
, uint8_t type
,
1472 uint8_t type_end
, uint8_t code
, uint8_t code_end
)
1474 struct spd_typecode
*tcp
;
1476 ASSERT(ALIGNED64(offset
));
1479 tcp
= (struct spd_typecode
*)(base
+ offset
);
1480 tcp
->spd_typecode_len
= SPD_8TO64(sizeof (*tcp
));
1481 tcp
->spd_typecode_exttype
= SPD_EXT_ICMP_TYPECODE
;
1482 tcp
->spd_typecode_code
= code
;
1483 tcp
->spd_typecode_type
= type
;
1484 tcp
->spd_typecode_type_end
= type_end
;
1485 tcp
->spd_typecode_code_end
= code_end
;
1487 offset
+= sizeof (*tcp
);
1489 ASSERT(ALIGNED64(offset
));
1495 spdsock_encode_proto(uint8_t *base
, uint_t offset
, uint8_t proto
)
1497 struct spd_proto
*spp
;
1499 ASSERT(ALIGNED64(offset
));
1502 spp
= (struct spd_proto
*)(base
+ offset
);
1503 spp
->spd_proto_len
= SPD_8TO64(sizeof (*spp
));
1504 spp
->spd_proto_exttype
= SPD_EXT_PROTO
;
1505 spp
->spd_proto_number
= proto
;
1506 spp
->spd_proto_reserved1
= 0;
1507 spp
->spd_proto_reserved2
= 0;
1509 offset
+= sizeof (*spp
);
1511 ASSERT(ALIGNED64(offset
));
1517 spdsock_encode_port(uint8_t *base
, uint_t offset
, uint16_t ext
, uint16_t port
)
1519 struct spd_portrange
*spp
;
1521 ASSERT(ALIGNED64(offset
));
1524 spp
= (struct spd_portrange
*)(base
+ offset
);
1525 spp
->spd_ports_len
= SPD_8TO64(sizeof (*spp
));
1526 spp
->spd_ports_exttype
= ext
;
1527 spp
->spd_ports_minport
= port
;
1528 spp
->spd_ports_maxport
= port
;
1530 offset
+= sizeof (*spp
);
1532 ASSERT(ALIGNED64(offset
));
1538 spdsock_encode_addr(uint8_t *base
, uint_t offset
, uint16_t ext
,
1539 const ipsec_selkey_t
*sel
, const ipsec_addr_t
*addr
, uint_t pfxlen
)
1541 struct spd_address
*sae
;
1542 ipsec_addr_t
*spdaddr
;
1543 uint_t start
= offset
;
1547 if (sel
->ipsl_valid
& IPSL_IPV4
) {
1549 addrlen
= IP_ADDR_LEN
;
1552 addrlen
= IPV6_ADDR_LEN
;
1555 ASSERT(ALIGNED64(offset
));
1558 sae
= (struct spd_address
*)(base
+ offset
);
1559 sae
->spd_address_exttype
= ext
;
1560 sae
->spd_address_af
= af
;
1561 sae
->spd_address_prefixlen
= pfxlen
;
1562 sae
->spd_address_reserved2
= 0;
1564 spdaddr
= (ipsec_addr_t
*)(&sae
[1]);
1565 bcopy(addr
, spdaddr
, addrlen
);
1567 offset
+= sizeof (*sae
);
1568 addrlen
= roundup(addrlen
, sizeof (uint64_t));
1571 ASSERT(ALIGNED64(offset
));
1574 sae
->spd_address_len
= SPD_8TO64(offset
- start
);
1579 spdsock_encode_sel(uint8_t *base
, uint_t offset
, const ipsec_sel_t
*sel
)
1581 const ipsec_selkey_t
*selkey
= &sel
->ipsl_key
;
1583 if (selkey
->ipsl_valid
& IPSL_PROTOCOL
)
1584 offset
= spdsock_encode_proto(base
, offset
, selkey
->ipsl_proto
);
1585 if (selkey
->ipsl_valid
& IPSL_LOCAL_PORT
)
1586 offset
= spdsock_encode_port(base
, offset
, SPD_EXT_LCLPORT
,
1587 selkey
->ipsl_lport
);
1588 if (selkey
->ipsl_valid
& IPSL_REMOTE_PORT
)
1589 offset
= spdsock_encode_port(base
, offset
, SPD_EXT_REMPORT
,
1590 selkey
->ipsl_rport
);
1591 if (selkey
->ipsl_valid
& IPSL_REMOTE_ADDR
)
1592 offset
= spdsock_encode_addr(base
, offset
, SPD_EXT_REMADDR
,
1593 selkey
, &selkey
->ipsl_remote
, selkey
->ipsl_remote_pfxlen
);
1594 if (selkey
->ipsl_valid
& IPSL_LOCAL_ADDR
)
1595 offset
= spdsock_encode_addr(base
, offset
, SPD_EXT_LCLADDR
,
1596 selkey
, &selkey
->ipsl_local
, selkey
->ipsl_local_pfxlen
);
1597 if (selkey
->ipsl_valid
& IPSL_ICMP_TYPE
) {
1598 offset
= spdsock_encode_typecode(base
, offset
,
1599 selkey
->ipsl_icmp_type
, selkey
->ipsl_icmp_type_end
,
1600 (selkey
->ipsl_valid
& IPSL_ICMP_CODE
) ?
1601 selkey
->ipsl_icmp_code
: 255,
1602 (selkey
->ipsl_valid
& IPSL_ICMP_CODE
) ?
1603 selkey
->ipsl_icmp_code_end
: 255);
1609 spdsock_encode_actattr(uint8_t *base
, uint_t offset
, uint32_t tag
,
1612 struct spd_attribute
*attr
;
1614 ASSERT(ALIGNED64(offset
));
1617 attr
= (struct spd_attribute
*)(base
+ offset
);
1618 attr
->spd_attr_tag
= tag
;
1619 attr
->spd_attr_value
= value
;
1621 offset
+= sizeof (struct spd_attribute
);
1623 ASSERT(ALIGNED64(offset
));
1629 #define EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v))
1632 spdsock_encode_action(uint8_t *base
, uint_t offset
, const ipsec_action_t
*ap
)
1634 const struct ipsec_act
*act
= &(ap
->ipa_act
);
1637 EMIT(SPD_ATTR_EMPTY
, 0);
1638 switch (act
->ipa_type
) {
1639 case IPSEC_ACT_DISCARD
:
1640 case IPSEC_ACT_REJECT
:
1641 EMIT(SPD_ATTR_TYPE
, SPD_ACTTYPE_DROP
);
1643 case IPSEC_ACT_BYPASS
:
1644 case IPSEC_ACT_CLEAR
:
1645 EMIT(SPD_ATTR_TYPE
, SPD_ACTTYPE_PASS
);
1648 case IPSEC_ACT_APPLY
:
1649 EMIT(SPD_ATTR_TYPE
, SPD_ACTTYPE_IPSEC
);
1651 if (act
->ipa_apply
.ipp_use_ah
)
1652 flags
|= SPD_APPLY_AH
;
1653 if (act
->ipa_apply
.ipp_use_esp
)
1654 flags
|= SPD_APPLY_ESP
;
1655 if (act
->ipa_apply
.ipp_use_espa
)
1656 flags
|= SPD_APPLY_ESPA
;
1657 if (act
->ipa_apply
.ipp_use_se
)
1658 flags
|= SPD_APPLY_SE
;
1659 if (act
->ipa_apply
.ipp_use_unique
)
1660 flags
|= SPD_APPLY_UNIQUE
;
1661 EMIT(SPD_ATTR_FLAGS
, flags
);
1662 if (flags
& SPD_APPLY_AH
) {
1663 EMIT(SPD_ATTR_AH_AUTH
, act
->ipa_apply
.ipp_auth_alg
);
1664 EMIT(SPD_ATTR_AH_MINBITS
,
1665 act
->ipa_apply
.ipp_ah_minbits
);
1666 EMIT(SPD_ATTR_AH_MAXBITS
,
1667 act
->ipa_apply
.ipp_ah_maxbits
);
1669 if (flags
& SPD_APPLY_ESP
) {
1670 EMIT(SPD_ATTR_ESP_ENCR
, act
->ipa_apply
.ipp_encr_alg
);
1671 EMIT(SPD_ATTR_ENCR_MINBITS
,
1672 act
->ipa_apply
.ipp_espe_minbits
);
1673 EMIT(SPD_ATTR_ENCR_MAXBITS
,
1674 act
->ipa_apply
.ipp_espe_maxbits
);
1675 if (flags
& SPD_APPLY_ESPA
) {
1676 EMIT(SPD_ATTR_ESP_AUTH
,
1677 act
->ipa_apply
.ipp_esp_auth_alg
);
1678 EMIT(SPD_ATTR_ESPA_MINBITS
,
1679 act
->ipa_apply
.ipp_espa_minbits
);
1680 EMIT(SPD_ATTR_ESPA_MAXBITS
,
1681 act
->ipa_apply
.ipp_espa_maxbits
);
1684 if (act
->ipa_apply
.ipp_km_proto
!= 0)
1685 EMIT(SPD_ATTR_KM_PROTO
, act
->ipa_apply
.ipp_km_proto
);
1686 if (act
->ipa_apply
.ipp_km_cookie
!= 0)
1687 EMIT(SPD_ATTR_KM_PROTO
, act
->ipa_apply
.ipp_km_cookie
);
1688 if (act
->ipa_apply
.ipp_replay_depth
!= 0)
1689 EMIT(SPD_ATTR_REPLAY_DEPTH
,
1690 act
->ipa_apply
.ipp_replay_depth
);
1699 spdsock_encode_action_list(uint8_t *base
, uint_t offset
,
1700 const ipsec_action_t
*ap
)
1702 struct spd_ext_actions
*act
;
1704 uint_t start
= offset
;
1706 ASSERT(ALIGNED64(offset
));
1709 act
= (struct spd_ext_actions
*)(base
+ offset
);
1710 act
->spd_actions_len
= 0;
1711 act
->spd_actions_exttype
= SPD_EXT_ACTION
;
1712 act
->spd_actions_count
= 0;
1713 act
->spd_actions_reserved
= 0;
1716 offset
+= sizeof (*act
);
1718 ASSERT(ALIGNED64(offset
));
1720 while (ap
!= NULL
) {
1721 offset
= spdsock_encode_action(base
, offset
, ap
);
1725 EMIT(SPD_ATTR_NEXT
, 0);
1728 EMIT(SPD_ATTR_END
, 0);
1730 ASSERT(ALIGNED64(offset
));
1733 act
->spd_actions_count
= nact
;
1734 act
->spd_actions_len
= SPD_8TO64(offset
- start
);
1744 spdsock_rule_flags(uint_t dir
, uint_t af
)
1748 if (dir
== IPSEC_TYPE_INBOUND
)
1749 flags
|= SPD_RULE_FLAG_INBOUND
;
1750 if (dir
== IPSEC_TYPE_OUTBOUND
)
1751 flags
|= SPD_RULE_FLAG_OUTBOUND
;
1758 spdsock_encode_rule_head(uint8_t *base
, uint_t offset
, spd_msg_t
*req
,
1759 const ipsec_policy_t
*rule
, uint_t dir
, uint_t af
, char *name
,
1762 struct spd_msg
*spmsg
;
1763 struct spd_rule
*spr
;
1766 uint_t start
= offset
;
1768 ASSERT(ALIGNED64(offset
));
1771 spmsg
= (struct spd_msg
*)(base
+ offset
);
1772 bzero(spmsg
, sizeof (*spmsg
));
1773 spmsg
->spd_msg_version
= PF_POLICY_V1
;
1774 spmsg
->spd_msg_type
= SPD_DUMP
;
1775 spmsg
->spd_msg_seq
= req
->spd_msg_seq
;
1776 spmsg
->spd_msg_pid
= req
->spd_msg_pid
;
1778 offset
+= sizeof (struct spd_msg
);
1780 ASSERT(ALIGNED64(offset
));
1783 spr
= (struct spd_rule
*)(base
+ offset
);
1784 spr
->spd_rule_type
= SPD_EXT_RULE
;
1785 spr
->spd_rule_priority
= rule
->ipsp_prio
;
1786 spr
->spd_rule_flags
= spdsock_rule_flags(dir
, af
);
1788 spr
->spd_rule_flags
|= SPD_RULE_FLAG_TUNNEL
;
1789 spr
->spd_rule_unused
= 0;
1790 spr
->spd_rule_len
= SPD_8TO64(sizeof (*spr
));
1791 spr
->spd_rule_index
= rule
->ipsp_index
;
1793 offset
+= sizeof (struct spd_rule
);
1796 * If we have an interface name (i.e. if this policy head came from
1797 * a tunnel), add the SPD_EXT_TUN_NAME extension.
1801 ASSERT(ALIGNED64(offset
));
1804 sid
= (spd_if_t
*)(base
+ offset
);
1805 sid
->spd_if_exttype
= SPD_EXT_TUN_NAME
;
1806 sid
->spd_if_len
= SPD_8TO64(sizeof (spd_if_t
) +
1807 roundup((strlen(name
) - 4), 8));
1808 (void) strlcpy((char *)sid
->spd_if_name
, name
,
1812 offset
+= sizeof (spd_if_t
) + roundup((strlen(name
) - 4), 8);
1815 offset
= spdsock_encode_sel(base
, offset
, rule
->ipsp_sel
);
1816 offset
= spdsock_encode_action_list(base
, offset
, rule
->ipsp_act
);
1818 ASSERT(ALIGNED64(offset
));
1821 spmsg
->spd_msg_len
= SPD_8TO64(offset
- start
);
1828 spdsock_encode_rule(mblk_t
*req
, const ipsec_policy_t
*rule
,
1829 uint_t dir
, uint_t af
, char *name
, boolean_t tunnel
)
1833 spd_msg_t
*mreq
= (spd_msg_t
*)req
->b_rptr
;
1836 * Figure out how much space we'll need.
1838 len
= spdsock_encode_rule_head(NULL
, 0, mreq
, rule
, dir
, af
, name
,
1844 m
= allocb(len
, BPRI_HI
);
1851 m
->b_wptr
= m
->b_rptr
+ len
;
1852 bzero(m
->b_rptr
, len
);
1853 (void) spdsock_encode_rule_head(m
->b_rptr
, 0, mreq
, rule
, dir
, af
,
1858 static ipsec_policy_t
*
1859 spdsock_dump_next_in_chain(spdsock_t
*ss
, ipsec_policy_head_t
*iph
,
1860 ipsec_policy_t
*cur
)
1862 ASSERT(RW_READ_HELD(&iph
->iph_lock
));
1864 ss
->spdsock_dump_count
++;
1865 ss
->spdsock_dump_cur_rule
= cur
->ipsp_hash
.hash_next
;
1869 static ipsec_policy_t
*
1870 spdsock_dump_next_rule(spdsock_t
*ss
, ipsec_policy_head_t
*iph
)
1872 ipsec_policy_t
*cur
;
1873 ipsec_policy_root_t
*ipr
;
1874 int chain
, nchains
, type
, af
;
1876 ASSERT(RW_READ_HELD(&iph
->iph_lock
));
1878 cur
= ss
->spdsock_dump_cur_rule
;
1881 return (spdsock_dump_next_in_chain(ss
, iph
, cur
));
1883 type
= ss
->spdsock_dump_cur_type
;
1886 chain
= ss
->spdsock_dump_cur_chain
;
1887 ipr
= &iph
->iph_root
[type
];
1888 nchains
= ipr
->ipr_nchains
;
1890 while (chain
< nchains
) {
1891 cur
= ipr
->ipr_hash
[chain
].hash_head
;
1894 ss
->spdsock_dump_cur_chain
= chain
;
1895 return (spdsock_dump_next_in_chain(ss
, iph
, cur
));
1898 ss
->spdsock_dump_cur_chain
= nchains
;
1900 af
= ss
->spdsock_dump_cur_af
;
1901 while (af
< IPSEC_NAF
) {
1902 cur
= ipr
->ipr_nonhash
[af
];
1905 ss
->spdsock_dump_cur_af
= af
;
1906 return (spdsock_dump_next_in_chain(ss
, iph
, cur
));
1911 if (type
>= IPSEC_NTYPES
)
1914 ss
->spdsock_dump_cur_chain
= 0;
1915 ss
->spdsock_dump_cur_type
= type
;
1916 ss
->spdsock_dump_cur_af
= IPSEC_AF_V4
;
1922 * If we're done with one policy head, but have more to go, we iterate through
1923 * another IPsec tunnel policy head (itp). Return NULL if it is an error
1924 * worthy of returning EAGAIN via PF_POLICY.
1926 static ipsec_tun_pol_t
*
1927 spdsock_dump_iterate_next_tunnel(spdsock_t
*ss
, ipsec_stack_t
*ipss
)
1929 ipsec_tun_pol_t
*itp
;
1931 ASSERT(RW_READ_HELD(&ipss
->ipsec_tunnel_policy_lock
));
1932 if (ipss
->ipsec_tunnel_policy_gen
> ss
->spdsock_dump_tun_gen
) {
1933 /* Oops, state of the tunnel polheads changed. */
1935 } else if (ss
->spdsock_itp
== NULL
) {
1936 /* Just finished global, find first node. */
1937 itp
= avl_first(&ipss
->ipsec_tunnel_policies
);
1939 /* We just finished current polhead, find the next one. */
1940 itp
= AVL_NEXT(&ipss
->ipsec_tunnel_policies
, ss
->spdsock_itp
);
1945 if (ss
->spdsock_itp
!= NULL
) {
1946 ITP_REFRELE(ss
->spdsock_itp
, ipss
->ipsec_netstack
);
1948 ss
->spdsock_itp
= itp
;
1953 spdsock_dump_next_record(spdsock_t
*ss
)
1955 ipsec_policy_head_t
*iph
;
1956 ipsec_policy_t
*rule
;
1958 ipsec_tun_pol_t
*itp
;
1959 netstack_t
*ns
= ss
->spdsock_spds
->spds_netstack
;
1960 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
1962 iph
= ss
->spdsock_dump_head
;
1964 ASSERT(iph
!= NULL
);
1966 rw_enter(&iph
->iph_lock
, RW_READER
);
1968 if (iph
->iph_gen
!= ss
->spdsock_dump_gen
) {
1969 rw_exit(&iph
->iph_lock
);
1970 return (spdsock_dump_finish(ss
, EAGAIN
));
1973 while ((rule
= spdsock_dump_next_rule(ss
, iph
)) == NULL
) {
1974 rw_exit(&iph
->iph_lock
);
1975 if (--(ss
->spdsock_dump_remaining_polheads
) == 0)
1976 return (spdsock_dump_finish(ss
, 0));
1980 * If we reach here, we have more policy heads (tunnel
1981 * entries) to dump. Let's reset to a new policy head
1982 * and get some more rules.
1984 * An empty policy head will have spdsock_dump_next_rule()
1985 * return NULL, and we loop (while dropping the number of
1986 * remaining polheads). If we loop to 0, we finish. We
1987 * keep looping until we hit 0 or until we have a rule to
1990 * NOTE: No need for ITP_REF*() macros here as we're only
1991 * going after and refholding the policy head itself.
1993 rw_enter(&ipss
->ipsec_tunnel_policy_lock
, RW_READER
);
1994 itp
= spdsock_dump_iterate_next_tunnel(ss
, ipss
);
1996 rw_exit(&ipss
->ipsec_tunnel_policy_lock
);
1997 return (spdsock_dump_finish(ss
, EAGAIN
));
2000 /* Reset other spdsock_dump thingies. */
2001 IPPH_REFRELE(ss
->spdsock_dump_head
, ns
);
2002 if (ss
->spdsock_dump_active
) {
2003 ss
->spdsock_dump_tunnel
=
2004 itp
->itp_flags
& ITPF_P_TUNNEL
;
2005 iph
= itp
->itp_policy
;
2007 ss
->spdsock_dump_tunnel
=
2008 itp
->itp_flags
& ITPF_I_TUNNEL
;
2009 iph
= itp
->itp_inactive
;
2012 rw_exit(&ipss
->ipsec_tunnel_policy_lock
);
2014 rw_enter(&iph
->iph_lock
, RW_READER
);
2015 RESET_SPDSOCK_DUMP_POLHEAD(ss
, iph
);
2018 m
= spdsock_encode_rule(ss
->spdsock_dump_req
, rule
,
2019 ss
->spdsock_dump_cur_type
, ss
->spdsock_dump_cur_af
,
2020 (ss
->spdsock_itp
== NULL
) ? NULL
: ss
->spdsock_itp
->itp_name
,
2021 ss
->spdsock_dump_tunnel
);
2022 rw_exit(&iph
->iph_lock
);
2025 return (spdsock_dump_finish(ss
, ENOMEM
));
2030 * Dump records until we run into flow-control back-pressure.
2033 spdsock_dump_some(queue_t
*q
, spdsock_t
*ss
)
2035 mblk_t
*m
, *dataind
;
2037 while ((ss
->spdsock_dump_req
!= NULL
) && canputnext(q
)) {
2038 m
= spdsock_dump_next_record(ss
);
2041 dataind
= allocb(sizeof (struct T_data_req
), BPRI_HI
);
2042 if (dataind
== NULL
) {
2046 dataind
->b_cont
= m
;
2047 dataind
->b_wptr
+= sizeof (struct T_data_req
);
2048 ((struct T_data_ind
*)dataind
->b_rptr
)->PRIM_type
= T_DATA_IND
;
2049 ((struct T_data_ind
*)dataind
->b_rptr
)->MORE_flag
= 0;
2050 dataind
->b_datap
->db_type
= M_PROTO
;
2051 putnext(q
, dataind
);
2057 * Format a start-of-dump record, and set up the stream and kick the rsrv
2058 * procedure to continue the job..
2062 spdsock_dump(queue_t
*q
, ipsec_policy_head_t
*iph
, mblk_t
*mp
)
2064 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
2065 netstack_t
*ns
= ss
->spdsock_spds
->spds_netstack
;
2066 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
2069 /* spdsock_open() already set spdsock_itp to NULL. */
2070 if (iph
== ALL_ACTIVE_POLHEADS
|| iph
== ALL_INACTIVE_POLHEADS
) {
2071 rw_enter(&ipss
->ipsec_tunnel_policy_lock
, RW_READER
);
2072 ss
->spdsock_dump_remaining_polheads
= 1 +
2073 avl_numnodes(&ipss
->ipsec_tunnel_policies
);
2074 ss
->spdsock_dump_tun_gen
= ipss
->ipsec_tunnel_policy_gen
;
2075 rw_exit(&ipss
->ipsec_tunnel_policy_lock
);
2076 if (iph
== ALL_ACTIVE_POLHEADS
) {
2077 iph
= ipsec_system_policy(ns
);
2078 ss
->spdsock_dump_active
= B_TRUE
;
2080 iph
= ipsec_inactive_policy(ns
);
2081 ss
->spdsock_dump_active
= B_FALSE
;
2083 ASSERT(ss
->spdsock_itp
== NULL
);
2085 ss
->spdsock_dump_remaining_polheads
= 1;
2088 rw_enter(&iph
->iph_lock
, RW_READER
);
2090 mr
= spdsock_dump_ruleset(mp
, iph
, 0, 0);
2093 rw_exit(&iph
->iph_lock
);
2094 spdsock_error(q
, mp
, ENOMEM
, 0);
2098 ss
->spdsock_dump_req
= mp
;
2099 RESET_SPDSOCK_DUMP_POLHEAD(ss
, iph
);
2101 rw_exit(&iph
->iph_lock
);
2107 /* Do NOT consume a reference to ITP. */
2109 spdsock_clone_node(ipsec_tun_pol_t
*itp
, void *ep
, netstack_t
*ns
)
2111 int *errptr
= (int *)ep
;
2114 return; /* We've failed already for some reason. */
2115 mutex_enter(&itp
->itp_lock
);
2116 ITPF_CLONE(itp
->itp_flags
);
2117 *errptr
= ipsec_copy_polhead(itp
->itp_policy
, itp
->itp_inactive
, ns
);
2118 mutex_exit(&itp
->itp_lock
);
2122 spdsock_clone(queue_t
*q
, mblk_t
*mp
, spd_if_t
*tunname
)
2126 ipsec_tun_pol_t
*itp
;
2127 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
2128 netstack_t
*ns
= ss
->spdsock_spds
->spds_netstack
;
2129 uint32_t auditing
= AU_AUDITING();
2131 if (tunname
!= NULL
) {
2132 tname
= (char *)tunname
->spd_if_name
;
2133 if (*tname
== '\0') {
2134 error
= ipsec_clone_system_policy(ns
);
2137 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
2141 cr
= msg_getcred(mp
, &cpid
);
2142 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
2143 audit_pf_policy(SPD_CLONE
, cr
, ns
,
2144 NULL
, active
, error
, cpid
);
2147 itp_walk(spdsock_clone_node
, &error
, ns
);
2151 (spd_msg_t
*)mp
->b_rptr
;
2155 cr
= msg_getcred(mp
, &cpid
);
2156 active
= (spmsg
->spd_msg_spdid
==
2158 audit_pf_policy(SPD_CLONE
, cr
,
2159 ns
, "all tunnels", active
, 0,
2164 itp
= get_tunnel_policy(tname
, ns
);
2166 spdsock_error(q
, mp
, ENOENT
, 0);
2170 (spd_msg_t
*)mp
->b_rptr
;
2174 cr
= msg_getcred(mp
, &cpid
);
2175 active
= (spmsg
->spd_msg_spdid
==
2177 audit_pf_policy(SPD_CLONE
, cr
,
2178 ns
, NULL
, active
, ENOENT
, cpid
);
2182 spdsock_clone_node(itp
, &error
, NULL
);
2185 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
2189 cr
= msg_getcred(mp
, &cpid
);
2190 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
2191 audit_pf_policy(SPD_CLONE
, cr
, ns
,
2192 ITP_NAME(itp
), active
, error
, cpid
);
2194 ITP_REFRELE(itp
, ns
);
2197 error
= ipsec_clone_system_policy(ns
);
2200 spd_msg_t
*spmsg
= (spd_msg_t
*)mp
->b_rptr
;
2204 cr
= msg_getcred(mp
, &cpid
);
2205 active
= (spmsg
->spd_msg_spdid
== SPD_ACTIVE
);
2206 audit_pf_policy(SPD_CLONE
, cr
, ns
, NULL
,
2207 active
, error
, cpid
);
2212 spdsock_error(q
, mp
, error
, 0);
2218 * Process a SPD_ALGLIST request. The caller expects separate alg entries
2219 * for AH authentication, ESP authentication, and ESP encryption.
2220 * The same distinction is then used when setting the min and max key
2221 * sizes when defining policies.
2224 #define SPDSOCK_AH_AUTH 0
2225 #define SPDSOCK_ESP_AUTH 1
2226 #define SPDSOCK_ESP_ENCR 2
2227 #define SPDSOCK_NTYPES 3
2229 static const uint_t algattr
[SPDSOCK_NTYPES
] = {
2234 static const uint_t minbitsattr
[SPDSOCK_NTYPES
] = {
2235 SPD_ATTR_AH_MINBITS
,
2236 SPD_ATTR_ESPA_MINBITS
,
2237 SPD_ATTR_ENCR_MINBITS
2239 static const uint_t maxbitsattr
[SPDSOCK_NTYPES
] = {
2240 SPD_ATTR_AH_MAXBITS
,
2241 SPD_ATTR_ESPA_MAXBITS
,
2242 SPD_ATTR_ENCR_MAXBITS
2244 static const uint_t defbitsattr
[SPDSOCK_NTYPES
] = {
2245 SPD_ATTR_AH_DEFBITS
,
2246 SPD_ATTR_ESPA_DEFBITS
,
2247 SPD_ATTR_ENCR_DEFBITS
2249 static const uint_t incrbitsattr
[SPDSOCK_NTYPES
] = {
2250 SPD_ATTR_AH_INCRBITS
,
2251 SPD_ATTR_ESPA_INCRBITS
,
2252 SPD_ATTR_ENCR_INCRBITS
2255 #define ATTRPERALG 6 /* fixed attributes per algs */
2258 spdsock_alglist(queue_t
*q
, mblk_t
*mp
)
2267 struct spd_ext_actions
*act
;
2268 struct spd_attribute
*attr
;
2269 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
2270 ipsec_stack_t
*ipss
= ss
->spdsock_spds
->spds_netstack
->netstack_ipsec
;
2272 rw_enter(&ipss
->ipsec_alg_lock
, RW_READER
);
2274 * The SPD client expects to receive separate entries for
2275 * AH authentication and ESP authentication supported algorithms.
2277 * Don't return the "any" algorithms, if defined, as no
2278 * kernel policies can be set for these algorithms.
2280 algcount
= 2 * ipss
->ipsec_nalgs
[IPSEC_ALG_AUTH
] +
2281 ipss
->ipsec_nalgs
[IPSEC_ALG_ENCR
];
2283 if (ipss
->ipsec_alglists
[IPSEC_ALG_AUTH
][SADB_AALG_NONE
] != NULL
)
2285 if (ipss
->ipsec_alglists
[IPSEC_ALG_ENCR
][SADB_EALG_NONE
] != NULL
)
2289 * For each algorithm, we encode:
2290 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
2293 size
= sizeof (spd_msg_t
) + sizeof (struct spd_ext_actions
) +
2294 ATTRPERALG
* sizeof (struct spd_attribute
) * algcount
;
2296 ASSERT(ALIGNED64(size
));
2298 m
= allocb(size
, BPRI_HI
);
2300 rw_exit(&ipss
->ipsec_alg_lock
);
2301 spdsock_error(q
, mp
, ENOMEM
, 0);
2305 m
->b_wptr
= m
->b_rptr
+ size
;
2308 msg
= (spd_msg_t
*)cur
;
2309 bcopy(mp
->b_rptr
, cur
, sizeof (*msg
));
2311 msg
->spd_msg_len
= SPD_8TO64(size
);
2312 msg
->spd_msg_errno
= 0;
2313 msg
->spd_msg_diagnostic
= 0;
2315 cur
+= sizeof (*msg
);
2317 act
= (struct spd_ext_actions
*)cur
;
2318 cur
+= sizeof (*act
);
2320 act
->spd_actions_len
= SPD_8TO64(size
- sizeof (spd_msg_t
));
2321 act
->spd_actions_exttype
= SPD_EXT_ACTION
;
2322 act
->spd_actions_count
= algcount
;
2323 act
->spd_actions_reserved
= 0;
2325 attr
= (struct spd_attribute
*)cur
;
2327 #define EMIT(tag, value) { \
2328 attr->spd_attr_tag = (tag); \
2329 attr->spd_attr_value = (value); \
2334 * If you change the number of EMIT's here, change
2335 * ATTRPERALG above to match
2337 #define EMITALGATTRS(_type) { \
2338 EMIT(algattr[_type], algid); /* 1 */ \
2339 EMIT(minbitsattr[_type], minbits); /* 2 */ \
2340 EMIT(maxbitsattr[_type], maxbits); /* 3 */ \
2341 EMIT(defbitsattr[_type], defbits); /* 4 */ \
2342 EMIT(incrbitsattr[_type], incr); /* 5 */ \
2343 EMIT(SPD_ATTR_NEXT, 0); /* 6 */ \
2346 for (algtype
= 0; algtype
< IPSEC_NALGTYPES
; algtype
++) {
2347 for (algidx
= 0; algidx
< ipss
->ipsec_nalgs
[algtype
];
2349 int algid
= ipss
->ipsec_sortlist
[algtype
][algidx
];
2350 ipsec_alginfo_t
*alg
=
2351 ipss
->ipsec_alglists
[algtype
][algid
];
2352 uint_t minbits
= alg
->alg_minbits
;
2353 uint_t maxbits
= alg
->alg_maxbits
;
2354 uint_t defbits
= alg
->alg_default_bits
;
2355 uint_t incr
= alg
->alg_increment
;
2357 if (algtype
== IPSEC_ALG_AUTH
) {
2358 if (algid
== SADB_AALG_NONE
)
2360 EMITALGATTRS(SPDSOCK_AH_AUTH
);
2361 EMITALGATTRS(SPDSOCK_ESP_AUTH
);
2363 if (algid
== SADB_EALG_NONE
)
2365 ASSERT(algtype
== IPSEC_ALG_ENCR
);
2366 EMITALGATTRS(SPDSOCK_ESP_ENCR
);
2371 rw_exit(&ipss
->ipsec_alg_lock
);
2378 attr
->spd_attr_tag
= SPD_ATTR_END
;
2385 * Process a SPD_DUMPALGS request.
2388 #define ATTRPERALG 9 /* fixed attributes per algs */
2391 spdsock_dumpalgs(queue_t
*q
, mblk_t
*mp
)
2399 struct spd_ext_actions
*act
;
2400 struct spd_attribute
*attr
;
2401 ipsec_alginfo_t
*alg
;
2405 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
2406 ipsec_stack_t
*ipss
= ss
->spdsock_spds
->spds_netstack
->netstack_ipsec
;
2408 rw_enter(&ipss
->ipsec_alg_lock
, RW_READER
);
2411 * For each algorithm, we encode:
2412 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT}
2414 * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE*
2415 * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_NPARAMS / ALG_PARAMS* /
2416 * ALG_MECHNAME / ALG_FLAGS / {END, NEXT}
2420 * Compute the size of the SPD message.
2422 size
= sizeof (spd_msg_t
) + sizeof (struct spd_ext_actions
);
2424 for (algtype
= 0; algtype
< IPSEC_NALGTYPES
; algtype
++) {
2425 for (algidx
= 0; algidx
< ipss
->ipsec_nalgs
[algtype
];
2427 algid
= ipss
->ipsec_sortlist
[algtype
][algidx
];
2428 alg
= ipss
->ipsec_alglists
[algtype
][algid
];
2429 alg_size
= sizeof (struct spd_attribute
) *
2430 (ATTRPERALG
+ alg
->alg_nkey_sizes
+
2431 alg
->alg_nblock_sizes
+ alg
->alg_nparams
) +
2432 CRYPTO_MAX_MECH_NAME
;
2437 ASSERT(ALIGNED64(size
));
2439 m
= allocb(size
, BPRI_HI
);
2441 rw_exit(&ipss
->ipsec_alg_lock
);
2442 spdsock_error(q
, mp
, ENOMEM
, 0);
2446 m
->b_wptr
= m
->b_rptr
+ size
;
2449 msg
= (spd_msg_t
*)cur
;
2450 bcopy(mp
->b_rptr
, cur
, sizeof (*msg
));
2452 msg
->spd_msg_len
= SPD_8TO64(size
);
2453 msg
->spd_msg_errno
= 0;
2454 msg
->spd_msg_type
= SPD_ALGLIST
;
2456 msg
->spd_msg_diagnostic
= 0;
2458 cur
+= sizeof (*msg
);
2460 act
= (struct spd_ext_actions
*)cur
;
2461 cur
+= sizeof (*act
);
2463 act
->spd_actions_len
= SPD_8TO64(size
- sizeof (spd_msg_t
));
2464 act
->spd_actions_exttype
= SPD_EXT_ACTION
;
2465 act
->spd_actions_count
= ipss
->ipsec_nalgs
[IPSEC_ALG_AUTH
] +
2466 ipss
->ipsec_nalgs
[IPSEC_ALG_ENCR
];
2467 act
->spd_actions_reserved
= 0;
2470 * If there aren't any algorithms registered, return an empty message.
2471 * spdsock_get_ext() knows how to deal with this.
2473 if (act
->spd_actions_count
== 0) {
2474 act
->spd_actions_len
= 0;
2475 rw_exit(&ipss
->ipsec_alg_lock
);
2479 attr
= (struct spd_attribute
*)cur
;
2481 #define EMIT(tag, value) { \
2482 attr->spd_attr_tag = (tag); \
2483 attr->spd_attr_value = (value); \
2487 for (algtype
= 0; algtype
< IPSEC_NALGTYPES
; algtype
++) {
2488 for (algidx
= 0; algidx
< ipss
->ipsec_nalgs
[algtype
];
2491 algid
= ipss
->ipsec_sortlist
[algtype
][algidx
];
2492 alg
= ipss
->ipsec_alglists
[algtype
][algid
];
2495 * If you change the number of EMIT's here, change
2496 * ATTRPERALG above to match
2498 EMIT(SPD_ATTR_ALG_ID
, algid
);
2499 EMIT(SPD_ATTR_ALG_PROTO
, algproto
[algtype
]);
2500 EMIT(SPD_ATTR_ALG_INCRBITS
, alg
->alg_increment
);
2501 EMIT(SPD_ATTR_ALG_NKEYSIZES
, alg
->alg_nkey_sizes
);
2502 for (i
= 0; i
< alg
->alg_nkey_sizes
; i
++)
2503 EMIT(SPD_ATTR_ALG_KEYSIZE
,
2504 alg
->alg_key_sizes
[i
]);
2506 EMIT(SPD_ATTR_ALG_NBLOCKSIZES
, alg
->alg_nblock_sizes
);
2507 for (i
= 0; i
< alg
->alg_nblock_sizes
; i
++)
2508 EMIT(SPD_ATTR_ALG_BLOCKSIZE
,
2509 alg
->alg_block_sizes
[i
]);
2511 EMIT(SPD_ATTR_ALG_NPARAMS
, alg
->alg_nparams
);
2512 for (i
= 0; i
< alg
->alg_nparams
; i
++)
2513 EMIT(SPD_ATTR_ALG_PARAMS
,
2514 alg
->alg_params
[i
]);
2516 EMIT(SPD_ATTR_ALG_FLAGS
, alg
->alg_flags
);
2518 EMIT(SPD_ATTR_ALG_MECHNAME
, CRYPTO_MAX_MECH_NAME
);
2519 bcopy(alg
->alg_mech_name
, attr
, CRYPTO_MAX_MECH_NAME
);
2520 attr
= (struct spd_attribute
*)((char *)attr
+
2521 CRYPTO_MAX_MECH_NAME
);
2523 EMIT(SPD_ATTR_NEXT
, 0);
2527 rw_exit(&ipss
->ipsec_alg_lock
);
2534 attr
->spd_attr_tag
= SPD_ATTR_END
;
2542 * Do the actual work of processing an SPD_UPDATEALGS request. Can
2543 * be invoked either once IPsec is loaded on a cached request, or
2544 * when a request is received while IPsec is loaded.
2547 spdsock_do_updatealg(spd_ext_t
*extv
[], spd_stack_t
*spds
)
2549 struct spd_ext_actions
*actp
;
2550 struct spd_attribute
*attr
, *endattr
;
2551 uint64_t *start
, *end
;
2552 ipsec_alginfo_t
*alg
= NULL
;
2553 ipsec_algtype_t alg_type
= 0;
2554 boolean_t skip_alg
= B_TRUE
, doing_proto
= B_FALSE
;
2555 uint_t i
, cur_key
, cur_block
, algid
;
2558 ASSERT(MUTEX_HELD(&spds
->spds_alg_lock
));
2560 /* parse the message, building the list of algorithms */
2562 actp
= (struct spd_ext_actions
*)extv
[SPD_EXT_ACTION
];
2564 return (SPD_DIAGNOSTIC_NO_ACTION_EXT
);
2566 start
= (uint64_t *)actp
;
2567 end
= (start
+ actp
->spd_actions_len
);
2568 endattr
= (struct spd_attribute
*)end
;
2569 attr
= (struct spd_attribute
*)&actp
[1];
2571 bzero(spds
->spds_algs
, IPSEC_NALGTYPES
* IPSEC_MAX_ALGS
*
2572 sizeof (ipsec_alginfo_t
*));
2574 alg
= kmem_zalloc(sizeof (*alg
), KM_SLEEP
);
2576 #define ALG_KEY_SIZES(a) (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t))
2577 #define ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t))
2578 #define ALG_PARAM_SIZES(a) (((a)->alg_nparams + 1) * sizeof (uint16_t))
2580 while (attr
< endattr
) {
2581 switch (attr
->spd_attr_tag
) {
2583 case SPD_ATTR_EMPTY
:
2590 doing_proto
= B_FALSE
;
2594 ipsec_alg_free(alg
);
2597 spds
->spds_algs
[alg_type
][alg
->alg_id
]);
2598 spds
->spds_algs
[alg_type
][alg
->alg_id
] =
2601 alg
= kmem_zalloc(sizeof (*alg
), KM_SLEEP
);
2604 case SPD_ATTR_ALG_ID
:
2605 if (attr
->spd_attr_value
>= IPSEC_MAX_ALGS
) {
2606 ss1dbg(spds
, ("spdsock_do_updatealg: "
2607 "invalid alg id %d\n",
2608 attr
->spd_attr_value
));
2609 diag
= SPD_DIAGNOSTIC_ALG_ID_RANGE
;
2612 alg
->alg_id
= attr
->spd_attr_value
;
2615 case SPD_ATTR_ALG_PROTO
:
2616 /* find the alg type */
2617 for (i
= 0; i
< NALGPROTOS
; i
++)
2618 if (algproto
[i
] == attr
->spd_attr_value
)
2620 skip_alg
= (i
== NALGPROTOS
);
2625 case SPD_ATTR_ALG_INCRBITS
:
2626 alg
->alg_increment
= attr
->spd_attr_value
;
2629 case SPD_ATTR_ALG_NKEYSIZES
:
2630 if (alg
->alg_key_sizes
!= NULL
) {
2631 kmem_free(alg
->alg_key_sizes
,
2632 ALG_KEY_SIZES(alg
));
2634 alg
->alg_nkey_sizes
= attr
->spd_attr_value
;
2636 * Allocate room for the trailing zero key size
2639 alg
->alg_key_sizes
= kmem_zalloc(ALG_KEY_SIZES(alg
),
2644 case SPD_ATTR_ALG_KEYSIZE
:
2645 if (alg
->alg_key_sizes
== NULL
||
2646 cur_key
>= alg
->alg_nkey_sizes
) {
2647 ss1dbg(spds
, ("spdsock_do_updatealg: "
2648 "too many key sizes\n"));
2649 diag
= SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES
;
2652 alg
->alg_key_sizes
[cur_key
++] = attr
->spd_attr_value
;
2655 case SPD_ATTR_ALG_FLAGS
:
2657 * Flags (bit mask). The alg_flags element of
2658 * ipsecalg_flags_t is only 8 bits wide. The
2659 * user can set the VALID bit, but we will ignore it
2660 * and make the decision is the algorithm is valid.
2662 alg
->alg_flags
|= (uint8_t)attr
->spd_attr_value
;
2665 case SPD_ATTR_ALG_NBLOCKSIZES
:
2666 if (alg
->alg_block_sizes
!= NULL
) {
2667 kmem_free(alg
->alg_block_sizes
,
2668 ALG_BLOCK_SIZES(alg
));
2670 alg
->alg_nblock_sizes
= attr
->spd_attr_value
;
2672 * Allocate room for the trailing zero block size
2675 alg
->alg_block_sizes
= kmem_zalloc(ALG_BLOCK_SIZES(alg
),
2680 case SPD_ATTR_ALG_BLOCKSIZE
:
2681 if (alg
->alg_block_sizes
== NULL
||
2682 cur_block
>= alg
->alg_nblock_sizes
) {
2683 ss1dbg(spds
, ("spdsock_do_updatealg: "
2684 "too many block sizes\n"));
2685 diag
= SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES
;
2688 alg
->alg_block_sizes
[cur_block
++] =
2689 attr
->spd_attr_value
;
2692 case SPD_ATTR_ALG_NPARAMS
:
2693 if (alg
->alg_params
!= NULL
) {
2694 kmem_free(alg
->alg_params
,
2695 ALG_PARAM_SIZES(alg
));
2697 alg
->alg_nparams
= attr
->spd_attr_value
;
2699 * Allocate room for the trailing zero block size
2702 alg
->alg_params
= kmem_zalloc(ALG_PARAM_SIZES(alg
),
2707 case SPD_ATTR_ALG_PARAMS
:
2708 if (alg
->alg_params
== NULL
||
2709 cur_block
>= alg
->alg_nparams
) {
2710 ss1dbg(spds
, ("spdsock_do_updatealg: "
2711 "too many params\n"));
2712 diag
= SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES
;
2716 * Array contains: iv_len, icv_len, salt_len
2717 * Any additional parameters are currently ignored.
2719 alg
->alg_params
[cur_block
++] =
2720 attr
->spd_attr_value
;
2723 case SPD_ATTR_ALG_MECHNAME
: {
2726 if (attr
->spd_attr_value
> CRYPTO_MAX_MECH_NAME
) {
2727 ss1dbg(spds
, ("spdsock_do_updatealg: "
2728 "mech name too long\n"));
2729 diag
= SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN
;
2732 mech_name
= (char *)(attr
+ 1);
2733 bcopy(mech_name
, alg
->alg_mech_name
,
2734 attr
->spd_attr_value
);
2735 alg
->alg_mech_name
[CRYPTO_MAX_MECH_NAME
-1] = '\0';
2736 attr
= (struct spd_attribute
*)((char *)attr
+
2737 attr
->spd_attr_value
);
2741 case SPD_ATTR_PROTO_ID
:
2742 doing_proto
= B_TRUE
;
2743 for (i
= 0; i
< NALGPROTOS
; i
++) {
2744 if (algproto
[i
] == attr
->spd_attr_value
) {
2751 case SPD_ATTR_PROTO_EXEC_MODE
:
2754 for (i
= 0; i
< NEXECMODES
; i
++) {
2755 if (execmodes
[i
] == attr
->spd_attr_value
) {
2756 spds
->spds_algs_exec_mode
[alg_type
] = i
;
2765 #undef ALG_KEY_SIZES
2766 #undef ALG_BLOCK_SIZES
2767 #undef ALG_PARAM_SIZES
2769 /* update the algorithm tables */
2770 spdsock_merge_algs(spds
);
2773 ipsec_alg_free(alg
);
2774 for (alg_type
= 0; alg_type
< IPSEC_NALGTYPES
; alg_type
++)
2775 for (algid
= 0; algid
< IPSEC_MAX_ALGS
; algid
++)
2776 if (spds
->spds_algs
[alg_type
][algid
] != NULL
)
2777 ipsec_alg_free(spds
->spds_algs
[alg_type
][algid
]);
2782 * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue
2783 * the request until IPsec loads. If IPsec is loaded, act on it
2788 spdsock_updatealg(queue_t
*q
, mblk_t
*mp
, spd_ext_t
*extv
[])
2790 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
2791 spd_stack_t
*spds
= ss
->spdsock_spds
;
2792 ipsec_stack_t
*ipss
= spds
->spds_netstack
->netstack_ipsec
;
2793 uint32_t auditing
= AU_AUDITING();
2795 if (!ipsec_loaded(ipss
)) {
2797 * IPsec is not loaded, save request and return nicely,
2798 * the message will be processed once IPsec loads.
2802 /* last update message wins */
2803 if ((new_mp
= copymsg(mp
)) == NULL
) {
2804 spdsock_error(q
, mp
, ENOMEM
, 0);
2807 mutex_enter(&spds
->spds_alg_lock
);
2808 bcopy(extv
, spds
->spds_extv_algs
,
2809 sizeof (spd_ext_t
*) * (SPD_EXT_MAX
+ 1));
2810 if (spds
->spds_mp_algs
!= NULL
)
2811 freemsg(spds
->spds_mp_algs
);
2812 spds
->spds_mp_algs
= mp
;
2813 mutex_exit(&spds
->spds_alg_lock
);
2818 cr
= msg_getcred(mp
, &cpid
);
2819 audit_pf_policy(SPD_UPDATEALGS
, cr
,
2820 spds
->spds_netstack
, NULL
, B_TRUE
, EAGAIN
,
2823 spd_echo(q
, new_mp
);
2826 * IPsec is loaded, act on the message immediately.
2830 mutex_enter(&spds
->spds_alg_lock
);
2831 diag
= spdsock_do_updatealg(extv
, spds
);
2833 /* Keep the lock held while we walk the SA tables. */
2834 sadb_alg_update(IPSEC_ALG_ALL
, 0, 0,
2835 spds
->spds_netstack
);
2836 mutex_exit(&spds
->spds_alg_lock
);
2842 cr
= msg_getcred(mp
, &cpid
);
2843 audit_pf_policy(SPD_UPDATEALGS
, cr
,
2844 spds
->spds_netstack
, NULL
, B_TRUE
, 0,
2848 mutex_exit(&spds
->spds_alg_lock
);
2849 spdsock_diag(q
, mp
, diag
);
2854 cr
= msg_getcred(mp
, &cpid
);
2855 audit_pf_policy(SPD_UPDATEALGS
, cr
,
2856 spds
->spds_netstack
, NULL
, B_TRUE
, diag
,
2864 * Find a tunnel instance (using the name to link ID mapping), and
2865 * update it after an IPsec change. We need to do this always in case
2866 * we add policy AFTER plumbing a tunnel. We also need to do this
2867 * because, as a side-effect, the tunnel's MTU is updated to reflect
2868 * any IPsec overhead in the itp's policy.
2871 update_iptun_policy(ipsec_tun_pol_t
*itp
)
2873 datalink_id_t linkid
;
2875 if (dls_mgmt_get_linkid(itp
->itp_name
, &linkid
) == 0)
2876 iptun_set_policy(linkid
, itp
);
2880 * Sort through the mess of polhead options to retrieve an appropriate one.
2881 * Returns NULL if we send an spdsock error. Returns a valid pointer if we
2882 * found a valid polhead. Returns ALL_ACTIVE_POLHEADS (aka. -1) or
2883 * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to
2884 * act on ALL policy heads.
2886 static ipsec_policy_head_t
*
2887 get_appropriate_polhead(queue_t
*q
, mblk_t
*mp
, spd_if_t
*tunname
, int spdid
,
2888 int msgtype
, ipsec_tun_pol_t
**itpp
)
2890 ipsec_tun_pol_t
*itp
;
2891 ipsec_policy_head_t
*iph
;
2895 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
2896 netstack_t
*ns
= ss
->spdsock_spds
->spds_netstack
;
2897 uint64_t gen
; /* Placeholder */
2899 active
= (spdid
== SPD_ACTIVE
);
2901 if (!active
&& spdid
!= SPD_STANDBY
) {
2902 spdsock_diag(q
, mp
, SPD_DIAGNOSTIC_BAD_SPDID
);
2906 if (tunname
!= NULL
) {
2907 /* Acting on a tunnel's SPD. */
2908 tname
= (char *)tunname
->spd_if_name
;
2909 if (*tname
== '\0') {
2910 /* Handle all-polhead cases here. */
2911 if (msgtype
!= SPD_FLUSH
&& msgtype
!= SPD_DUMP
) {
2913 SPD_DIAGNOSTIC_NOT_GLOBAL_OP
);
2916 return (active
? ALL_ACTIVE_POLHEADS
:
2917 ALL_INACTIVE_POLHEADS
);
2920 itp
= get_tunnel_policy(tname
, ns
);
2922 if (msgtype
!= SPD_ADDRULE
) {
2923 /* "Tunnel not found" */
2924 spdsock_error(q
, mp
, ENOENT
, 0);
2929 itp
= create_tunnel_policy(tname
, &errno
, &gen
, ns
);
2932 * Something very bad happened, most likely
2933 * ENOMEM. Return an indicator.
2935 spdsock_error(q
, mp
, errno
, 0);
2940 /* Match up the itp to an iptun instance. */
2941 update_iptun_policy(itp
);
2944 /* For spdsock dump state, set the polhead's name. */
2945 if (msgtype
== SPD_DUMP
) {
2947 ss
->spdsock_itp
= itp
;
2948 ss
->spdsock_dump_tunnel
= itp
->itp_flags
&
2949 (active
? ITPF_P_TUNNEL
: ITPF_I_TUNNEL
);
2953 /* For spdsock dump state, indicate it's global policy. */
2954 if (msgtype
== SPD_DUMP
)
2955 ss
->spdsock_itp
= NULL
;
2959 iph
= (itp
== NULL
) ? ipsec_system_policy(ns
) : itp
->itp_policy
;
2961 iph
= (itp
== NULL
) ? ipsec_inactive_policy(ns
) :
2964 ASSERT(iph
!= NULL
);
2973 spdsock_parse(queue_t
*q
, mblk_t
*mp
)
2976 spd_ext_t
*extv
[SPD_EXT_MAX
+ 1];
2978 ipsec_policy_head_t
*iph
;
2979 ipsec_tun_pol_t
*itp
;
2981 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
2982 spd_stack_t
*spds
= ss
->spdsock_spds
;
2983 netstack_t
*ns
= spds
->spds_netstack
;
2984 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
2986 /* Make sure nothing's below me. */
2987 ASSERT(WR(q
)->q_next
== NULL
);
2989 spmsg
= (spd_msg_t
*)mp
->b_rptr
;
2991 msgsize
= SPD_64TO8(spmsg
->spd_msg_len
);
2993 if (msgdsize(mp
) != msgsize
) {
2995 * Message len incorrect w.r.t. actual size. Send an error
2996 * (EMSGSIZE). It may be necessary to massage things a
2997 * bit. For example, if the spd_msg_type is hosed,
2998 * I need to set it to SPD_RESERVED to get delivery to
2999 * do the right thing. Then again, maybe just letting
3000 * the error delivery do the right thing.
3003 ("mblk (%lu) and base (%d) message sizes don't jibe.\n",
3004 msgdsize(mp
), msgsize
));
3005 spdsock_error(q
, mp
, EMSGSIZE
, SPD_DIAGNOSTIC_NONE
);
3009 if (msgsize
> (uint_t
)(mp
->b_wptr
- mp
->b_rptr
)) {
3010 /* Get all message into one mblk. */
3011 if (pullupmsg(mp
, -1) == 0) {
3013 * Something screwy happened.
3015 ss3dbg(spds
, ("spdsock_parse: pullupmsg() failed.\n"));
3018 spmsg
= (spd_msg_t
*)mp
->b_rptr
;
3022 switch (spdsock_get_ext(extv
, spmsg
, msgsize
)) {
3024 /* Handle duplicate extension. */
3025 ss1dbg(spds
, ("Got duplicate extension of type %d.\n",
3026 extv
[0]->spd_ext_type
));
3027 spdsock_diag(q
, mp
, dup_ext_diag
[extv
[0]->spd_ext_type
]);
3030 /* Handle unknown extension. */
3031 ss1dbg(spds
, ("Got unknown extension of type %d.\n",
3032 extv
[0]->spd_ext_type
));
3033 spdsock_diag(q
, mp
, SPD_DIAGNOSTIC_UNKNOWN_EXT
);
3037 ss1dbg(spds
, ("Length %d on extension type %d overrun or 0.\n",
3038 extv
[0]->spd_ext_len
, extv
[0]->spd_ext_type
));
3039 spdsock_diag(q
, mp
, SPD_DIAGNOSTIC_BAD_EXTLEN
);
3042 /* Reality check failed. */
3043 ss1dbg(spds
, ("Reality check failed on extension type %d.\n",
3044 extv
[0]->spd_ext_type
));
3045 spdsock_diag(q
, mp
, bad_ext_diag
[extv
[0]->spd_ext_type
]);
3048 /* Default case is no errors. */
3053 * Special-case SPD_UPDATEALGS so as not to load IPsec.
3055 if (!ipsec_loaded(ipss
) && spmsg
->spd_msg_type
!= SPD_UPDATEALGS
) {
3056 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
3059 ipsec_loader_loadnow(ipss
);
3060 ss
->spdsock_timeout_arg
= mp
;
3061 ss
->spdsock_timeout
= qtimeout(q
, spdsock_loadcheck
,
3062 q
, LOADCHECK_INTERVAL
);
3066 /* First check for messages that need no polheads at all. */
3067 switch (spmsg
->spd_msg_type
) {
3068 case SPD_UPDATEALGS
:
3069 spdsock_updatealg(q
, mp
, extv
);
3072 spdsock_alglist(q
, mp
);
3075 spdsock_dumpalgs(q
, mp
);
3080 * Then check for ones that need both primary/secondary polheads,
3081 * finding the appropriate tunnel policy if need be.
3083 tunname
= (spd_if_t
*)extv
[SPD_EXT_TUN_NAME
];
3084 switch (spmsg
->spd_msg_type
) {
3086 spdsock_flip(q
, mp
, tunname
);
3089 spdsock_clone(q
, mp
, tunname
);
3094 * Finally, find ones that operate on exactly one polhead, or
3095 * "all polheads" of a given type (active/inactive).
3097 iph
= get_appropriate_polhead(q
, mp
, tunname
, spmsg
->spd_msg_spdid
,
3098 spmsg
->spd_msg_type
, &itp
);
3102 /* All-polheads-ready operations. */
3103 switch (spmsg
->spd_msg_type
) {
3106 mutex_enter(&itp
->itp_lock
);
3107 if (spmsg
->spd_msg_spdid
== SPD_ACTIVE
)
3108 itp
->itp_flags
&= ~ITPF_PFLAGS
;
3110 itp
->itp_flags
&= ~ITPF_IFLAGS
;
3111 mutex_exit(&itp
->itp_lock
);
3114 spdsock_flush(q
, iph
, itp
, mp
);
3117 /* SPD_FLUSH is worth a tunnel MTU check. */
3118 update_iptun_policy(itp
);
3119 ITP_REFRELE(itp
, ns
);
3124 ITP_REFRELE(itp
, ns
);
3125 spdsock_dump(q
, iph
, mp
);
3129 if (iph
== ALL_ACTIVE_POLHEADS
|| iph
== ALL_INACTIVE_POLHEADS
) {
3130 spdsock_diag(q
, mp
, SPD_DIAGNOSTIC_NOT_GLOBAL_OP
);
3134 /* Single-polhead-only operations. */
3135 switch (spmsg
->spd_msg_type
) {
3137 spdsock_addrule(q
, iph
, mp
, extv
, itp
);
3139 case SPD_DELETERULE
:
3140 spdsock_deleterule(q
, iph
, mp
, extv
, itp
);
3143 spdsock_lookup(q
, iph
, mp
, extv
, itp
);
3146 spdsock_diag(q
, mp
, SPD_DIAGNOSTIC_BAD_MSG_TYPE
);
3150 IPPH_REFRELE(iph
, ns
);
3152 /* SPD_{ADD,DELETE}RULE are worth a tunnel MTU check. */
3153 if (spmsg
->spd_msg_type
== SPD_ADDRULE
||
3154 spmsg
->spd_msg_type
== SPD_DELETERULE
)
3155 update_iptun_policy(itp
);
3156 ITP_REFRELE(itp
, ns
);
3161 * If an algorithm mapping was received before IPsec was loaded, process it.
3162 * Called from the IPsec loader.
3165 spdsock_update_pending_algs(netstack_t
*ns
)
3167 spd_stack_t
*spds
= ns
->netstack_spdsock
;
3169 mutex_enter(&spds
->spds_alg_lock
);
3170 if (spds
->spds_mp_algs
!= NULL
) {
3171 (void) spdsock_do_updatealg(spds
->spds_extv_algs
, spds
);
3172 freemsg(spds
->spds_mp_algs
);
3173 spds
->spds_mp_algs
= NULL
;
3175 mutex_exit(&spds
->spds_alg_lock
);
3179 spdsock_loadcheck(void *arg
)
3181 queue_t
*q
= (queue_t
*)arg
;
3182 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
3184 ipsec_stack_t
*ipss
= ss
->spdsock_spds
->spds_netstack
->netstack_ipsec
;
3188 ss
->spdsock_timeout
= 0;
3189 mp
= ss
->spdsock_timeout_arg
;
3191 ss
->spdsock_timeout_arg
= NULL
;
3192 if (ipsec_failed(ipss
))
3193 spdsock_error(q
, mp
, EPROTONOSUPPORT
, 0);
3195 spdsock_parse(q
, mp
);
3199 * Copy relevant state bits.
3202 spdsock_copy_info(struct T_info_ack
*tap
, spdsock_t
*ss
)
3204 *tap
= spdsock_g_t_info_ack
;
3205 tap
->CURRENT_state
= ss
->spdsock_state
;
3206 tap
->OPT_size
= spdsock_max_optsize
;
3210 * This routine responds to T_CAPABILITY_REQ messages. It is called by
3211 * spdsock_wput. Much of the T_CAPABILITY_ACK information is copied from
3212 * spdsock_g_t_info_ack. The current state of the stream is copied from
3216 spdsock_capability_req(queue_t
*q
, mblk_t
*mp
)
3218 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
3219 t_uscalar_t cap_bits1
;
3220 struct T_capability_ack
*tcap
;
3222 cap_bits1
= ((struct T_capability_req
*)mp
->b_rptr
)->CAP_bits1
;
3224 mp
= tpi_ack_alloc(mp
, sizeof (struct T_capability_ack
),
3225 mp
->b_datap
->db_type
, T_CAPABILITY_ACK
);
3229 tcap
= (struct T_capability_ack
*)mp
->b_rptr
;
3230 tcap
->CAP_bits1
= 0;
3232 if (cap_bits1
& TC1_INFO
) {
3233 spdsock_copy_info(&tcap
->INFO_ack
, ss
);
3234 tcap
->CAP_bits1
|= TC1_INFO
;
3241 * This routine responds to T_INFO_REQ messages. It is called by
3242 * spdsock_wput_other.
3243 * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack.
3244 * The current state of the stream is copied from spdsock_state.
3251 mp
= tpi_ack_alloc(mp
, sizeof (struct T_info_ack
), M_PCPROTO
,
3255 spdsock_copy_info((struct T_info_ack
*)mp
->b_rptr
,
3256 (spdsock_t
*)q
->q_ptr
);
3261 * spdsock_err_ack. This routine creates a
3262 * T_ERROR_ACK message and passes it
3272 if ((mp
= mi_tpi_err_ack_alloc(mp
, t_error
, sys_error
)) != NULL
)
3277 * This routine retrieves the current status of socket options.
3278 * It returns the size of the option retrieved.
3282 spdsock_opt_get(queue_t
*q
, int level
, int name
, uchar_t
*ptr
)
3284 int *i1
= (int *)ptr
;
3293 * The following two items can be manipulated,
3294 * but changing them should do nothing.
3297 *i1
= (int)q
->q_hiwat
;
3300 *i1
= (int)(RD(q
)->q_hiwat
);
3307 return (sizeof (int));
3311 * This routine sets socket options.
3315 spdsock_opt_set(queue_t
*q
, uint_t mgmt_flags
, int level
, int name
,
3316 uint_t inlen
, uchar_t
*invalp
, uint_t
*outlenp
, uchar_t
*outvalp
,
3317 void *thisdg_attrs
, cred_t
*cr
)
3319 int *i1
= (int *)invalp
;
3320 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
3321 spd_stack_t
*spds
= ss
->spdsock_spds
;
3327 if (*i1
> spds
->spds_max_buf
)
3332 if (*i1
> spds
->spds_max_buf
)
3334 RD(q
)->q_hiwat
= *i1
;
3335 (void) proto_set_rx_hiwat(RD(q
), NULL
, *i1
);
3345 * Handle STREAMS messages.
3348 spdsock_wput_other(queue_t
*q
, mblk_t
*mp
)
3350 struct iocblk
*iocp
;
3352 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
3353 spd_stack_t
*spds
= ss
->spdsock_spds
;
3356 switch (mp
->b_datap
->db_type
) {
3359 if ((mp
->b_wptr
- mp
->b_rptr
) < sizeof (long)) {
3361 "spdsock_wput_other: Not big enough M_PROTO\n"));
3365 switch (((union T_primitives
*)mp
->b_rptr
)->type
) {
3366 case T_CAPABILITY_REQ
:
3367 spdsock_capability_req(q
, mp
);
3370 spdsock_info_req(q
, mp
);
3372 case T_SVR4_OPTMGMT_REQ
:
3375 * All Solaris components should pass a db_credp
3376 * for this TPI message, hence we ASSERT.
3377 * But in case there is some other M_PROTO that looks
3378 * like a TPI message sent by some other kernel
3379 * component, we check and return an error.
3381 cr
= msg_getcred(mp
, NULL
);
3384 spdsock_err_ack(q
, mp
, TSYSERR
, EINVAL
);
3387 if (((union T_primitives
*)mp
->b_rptr
)->type
==
3388 T_SVR4_OPTMGMT_REQ
) {
3389 svr4_optcom_req(q
, mp
, cr
, &spdsock_opt_obj
);
3391 tpi_optcom_req(q
, mp
, cr
, &spdsock_opt_obj
);
3397 /* Illegal for spdsock. */
3399 (void) putnextctl1(RD(q
), M_ERROR
, EPROTO
);
3402 /* Not supported by spdsock. */
3403 spdsock_err_ack(q
, mp
, TNOTSUPPORT
, 0);
3408 iocp
= (struct iocblk
*)mp
->b_rptr
;
3411 switch (iocp
->ioc_cmd
) {
3414 if (nd_getset(q
, spds
->spds_g_nd
, mp
)) {
3421 miocnak(q
, mp
, 0, error
);
3425 if (*mp
->b_rptr
& FLUSHW
) {
3426 flushq(q
, FLUSHALL
);
3427 *mp
->b_rptr
&= ~FLUSHW
;
3429 if (*mp
->b_rptr
& FLUSHR
) {
3436 /* If fell through, just black-hole the message. */
3441 spdsock_wput(queue_t
*q
, mblk_t
*mp
)
3443 uint8_t *rptr
= mp
->b_rptr
;
3445 spdsock_t
*ss
= (spdsock_t
*)q
->q_ptr
;
3446 spd_stack_t
*spds
= ss
->spdsock_spds
;
3449 * If we're dumping, defer processing other messages until the
3452 if (ss
->spdsock_dump_req
!= NULL
) {
3458 switch (mp
->b_datap
->db_type
) {
3463 ss2dbg(spds
, ("raw M_DATA in spdsock.\n"));
3468 if ((mp
->b_wptr
- rptr
) >= sizeof (struct T_data_req
)) {
3469 if (((union T_primitives
*)rptr
)->type
== T_DATA_REQ
) {
3470 if ((mp1
= mp
->b_cont
) == NULL
) {
3471 /* No data after T_DATA_REQ. */
3473 ("No data after DATA_REQ.\n"));
3479 ss2dbg(spds
, ("T_DATA_REQ\n"));
3480 break; /* Out of switch. */
3485 ss3dbg(spds
, ("In default wput case (%d %d).\n",
3486 mp
->b_datap
->db_type
, ((union T_primitives
*)rptr
)->type
));
3487 spdsock_wput_other(q
, mp
);
3491 /* I now have a PF_POLICY message in an M_DATA block. */
3492 spdsock_parse(q
, mp
);
3496 * Device open procedure, called when new queue pair created.
3497 * We are passed the read-side queue.
3501 spdsock_open(queue_t
*q
, dev_t
*devp
, int flag
, int sflag
, cred_t
*credp
)
3504 queue_t
*oq
= OTHERQ(q
);
3509 if (secpolicy_ip_config(credp
, B_FALSE
) != 0)
3512 if (q
->q_ptr
!= NULL
)
3513 return (0); /* Re-open of an already open instance. */
3515 if (sflag
& MODOPEN
)
3518 ns
= netstack_find_by_cred(credp
);
3520 spds
= ns
->netstack_spdsock
;
3521 ASSERT(spds
!= NULL
);
3523 ss2dbg(spds
, ("Made it into PF_POLICY socket open.\n"));
3525 ssminor
= (minor_t
)(uintptr_t)vmem_alloc(spdsock_vmem
, 1, VM_NOSLEEP
);
3527 netstack_rele(spds
->spds_netstack
);
3530 ss
= kmem_zalloc(sizeof (spdsock_t
), KM_NOSLEEP
);
3532 vmem_free(spdsock_vmem
, (void *)(uintptr_t)ssminor
, 1);
3533 netstack_rele(spds
->spds_netstack
);
3537 ss
->spdsock_minor
= ssminor
;
3538 ss
->spdsock_state
= TS_UNBND
;
3539 ss
->spdsock_dump_req
= NULL
;
3541 ss
->spdsock_spds
= spds
;
3546 q
->q_hiwat
= spds
->spds_recv_hiwat
;
3548 oq
->q_hiwat
= spds
->spds_xmit_hiwat
;
3549 oq
->q_lowat
= spds
->spds_xmit_lowat
;
3552 (void) proto_set_rx_hiwat(q
, NULL
, spds
->spds_recv_hiwat
);
3554 *devp
= makedevice(getmajor(*devp
), ss
->spdsock_minor
);
3559 * Read-side service procedure, invoked when we get back-enabled
3560 * when buffer space becomes available.
3562 * Dump another chunk if we were dumping before; when we finish, kick
3563 * the write-side queue in case it's waiting for read queue space.
3566 spdsock_rsrv(queue_t
*q
)
3568 spdsock_t
*ss
= q
->q_ptr
;
3570 if (ss
->spdsock_dump_req
!= NULL
)
3571 spdsock_dump_some(q
, ss
);
3573 if (ss
->spdsock_dump_req
== NULL
)
3578 * Write-side service procedure, invoked when we defer processing
3579 * if another message is received while a dump is in progress.
3582 spdsock_wsrv(queue_t
*q
)
3584 spdsock_t
*ss
= q
->q_ptr
;
3586 ipsec_stack_t
*ipss
= ss
->spdsock_spds
->spds_netstack
->netstack_ipsec
;
3588 if (ss
->spdsock_dump_req
!= NULL
) {
3593 while ((mp
= getq(q
)) != NULL
) {
3594 if (ipsec_loaded(ipss
)) {
3595 spdsock_wput(q
, mp
);
3596 if (ss
->spdsock_dump_req
!= NULL
)
3598 } else if (!ipsec_failed(ipss
)) {
3601 spdsock_error(q
, mp
, EPFNOSUPPORT
, 0);
3607 spdsock_close(queue_t
*q
)
3609 spdsock_t
*ss
= q
->q_ptr
;
3610 spd_stack_t
*spds
= ss
->spdsock_spds
;
3614 /* Safe assumption. */
3617 if (ss
->spdsock_timeout
!= 0)
3618 (void) quntimeout(q
, ss
->spdsock_timeout
);
3620 ss3dbg(spds
, ("Driver close, PF_POLICY socket is going away.\n"));
3622 vmem_free(spdsock_vmem
, (void *)(uintptr_t)ss
->spdsock_minor
, 1);
3623 netstack_rele(ss
->spdsock_spds
->spds_netstack
);
3625 kmem_free(ss
, sizeof (spdsock_t
));
3630 * Merge the IPsec algorithms tables with the received algorithm information.
3633 spdsock_merge_algs(spd_stack_t
*spds
)
3635 ipsec_alginfo_t
*alg
, *oalg
;
3636 ipsec_algtype_t algtype
;
3637 uint_t algidx
, algid
, nalgs
;
3638 crypto_mech_name_t
*mechs
;
3639 uint_t mech_count
, mech_idx
;
3640 netstack_t
*ns
= spds
->spds_netstack
;
3641 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
3643 ASSERT(MUTEX_HELD(&spds
->spds_alg_lock
));
3646 * Get the list of supported mechanisms from the crypto framework.
3647 * If a mechanism is supported by KCF, resolve its mechanism
3648 * id and mark it as being valid. This operation must be done
3649 * without holding alg_lock, since it can cause a provider
3650 * module to be loaded and the provider notification callback to
3653 mechs
= crypto_get_mech_list(&mech_count
, KM_SLEEP
);
3654 for (algtype
= 0; algtype
< IPSEC_NALGTYPES
; algtype
++) {
3655 for (algid
= 0; algid
< IPSEC_MAX_ALGS
; algid
++) {
3657 crypto_mech_type_t mt
= CRYPTO_MECHANISM_INVALID
;
3659 alg
= spds
->spds_algs
[algtype
][algid
];
3664 * The NULL encryption algorithm is a special
3665 * case because there are no mechanisms, yet
3666 * the algorithm is still valid.
3668 if (alg
->alg_id
== SADB_EALG_NULL
) {
3669 alg
->alg_mech_type
= CRYPTO_MECHANISM_INVALID
;
3670 alg
->alg_flags
|= ALG_FLAG_VALID
;
3674 for (mech_idx
= 0; mech_idx
< mech_count
; mech_idx
++) {
3675 if (strncmp(alg
->alg_mech_name
, mechs
[mech_idx
],
3676 CRYPTO_MAX_MECH_NAME
) == 0) {
3677 mt
= crypto_mech2id(alg
->alg_mech_name
);
3678 ASSERT(mt
!= CRYPTO_MECHANISM_INVALID
);
3679 algflags
= ALG_FLAG_VALID
;
3683 alg
->alg_mech_type
= mt
;
3684 alg
->alg_flags
|= algflags
;
3688 rw_enter(&ipss
->ipsec_alg_lock
, RW_WRITER
);
3691 * For each algorithm currently defined, check if it is
3692 * present in the new tables created from the SPD_UPDATEALGS
3693 * message received from user-space.
3694 * Delete the algorithm entries that are currently defined
3695 * but not part of the new tables.
3697 for (algtype
= 0; algtype
< IPSEC_NALGTYPES
; algtype
++) {
3698 nalgs
= ipss
->ipsec_nalgs
[algtype
];
3699 for (algidx
= 0; algidx
< nalgs
; algidx
++) {
3700 algid
= ipss
->ipsec_sortlist
[algtype
][algidx
];
3701 if (spds
->spds_algs
[algtype
][algid
] == NULL
)
3702 ipsec_alg_unreg(algtype
, algid
, ns
);
3707 * For each algorithm we just received, check if it is
3708 * present in the currently defined tables. If it is, swap
3709 * the entry with the one we just allocated.
3710 * If the new algorithm is not in the current tables,
3713 for (algtype
= 0; algtype
< IPSEC_NALGTYPES
; algtype
++) {
3714 for (algid
= 0; algid
< IPSEC_MAX_ALGS
; algid
++) {
3715 alg
= spds
->spds_algs
[algtype
][algid
];
3719 if ((oalg
= ipss
->ipsec_alglists
[algtype
][algid
]) ==
3722 * New algorithm, add it to the algorithm
3725 ipsec_alg_reg(algtype
, alg
, ns
);
3728 * Algorithm is already in the table. Swap
3729 * the existing entry with the new one.
3731 ipsec_alg_fix_min_max(alg
, algtype
, ns
);
3732 ipss
->ipsec_alglists
[algtype
][algid
] = alg
;
3733 ipsec_alg_free(oalg
);
3735 spds
->spds_algs
[algtype
][algid
] = NULL
;
3739 for (algtype
= 0; algtype
< IPSEC_NALGTYPES
; algtype
++) {
3740 ipss
->ipsec_algs_exec_mode
[algtype
] =
3741 spds
->spds_algs_exec_mode
[algtype
];
3744 rw_exit(&ipss
->ipsec_alg_lock
);
3746 crypto_free_mech_list(mechs
, mech_count
);
3748 ipsecah_algs_changed(ns
);
3749 ipsecesp_algs_changed(ns
);