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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright 2017 Joyent, Inc.
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/stream.h>
32 #include <sys/strsubr.h>
33 #include <sys/strsun.h>
34 #include <sys/stropts.h>
35 #include <sys/vnode.h>
37 #include <sys/strlog.h>
38 #include <sys/sysmacros.h>
39 #define _SUN_TPI_VERSION 2
40 #include <sys/tihdr.h>
41 #include <sys/timod.h>
42 #include <sys/tiuser.h>
44 #include <sys/sunddi.h>
45 #include <sys/sunldi.h>
47 #include <sys/modctl.h>
48 #include <sys/debug.h>
50 #include <sys/cmn_err.h>
52 #include <sys/suntpi.h>
53 #include <sys/atomic.h>
54 #include <sys/mkdev.h>
55 #include <sys/policy.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <net/pfkeyv2.h>
62 #include <inet/common.h>
63 #include <netinet/ip6.h>
65 #include <inet/proto_set.h>
67 #include <inet/optcom.h>
68 #include <inet/ipsec_info.h>
69 #include <inet/ipsec_impl.h>
70 #include <inet/keysock.h>
72 #include <sys/isa_defs.h>
75 * This is a transport provider for the PF_KEY key mangement socket.
76 * (See RFC 2367 for details.)
77 * Downstream messages are wrapped in a keysock consumer interface KEYSOCK_IN
78 * messages (see ipsec_info.h), and passed to the appropriate consumer.
79 * Upstream messages are generated for all open PF_KEY sockets, when
80 * appropriate, as well as the sender (as long as SO_USELOOPBACK is enabled)
81 * in reply to downstream messages.
83 * Upstream messages must be created asynchronously for the following
86 * 1.) A keysock consumer requires an SA, and there is currently none.
87 * 2.) An SA expires, either hard or soft lifetime.
88 * 3.) Other events a consumer deems fit.
90 * The MT model of this is PERMOD, with shared put procedures. Two types of
91 * messages, SADB_FLUSH and SADB_DUMP, need to lock down the perimeter to send
92 * down the *multiple* messages they create.
95 static vmem_t
*keysock_vmem
; /* for minor numbers. */
97 #define KEYSOCK_MAX_CONSUMERS 256
99 /* Default structure copied into T_INFO_ACK messages (from rts.c...) */
100 static struct T_info_ack keysock_g_t_info_ack
= {
102 T_INFINITE
, /* TSDU_size. Maximum size messages. */
103 T_INVALID
, /* ETSDU_size. No expedited data. */
104 T_INVALID
, /* CDATA_size. No connect data. */
105 T_INVALID
, /* DDATA_size. No disconnect data. */
107 0, /* OPT_size. No user-settable options */
108 64 * 1024, /* TIDU_size. keysock allows maximum size messages. */
109 T_COTS
, /* SERV_type. keysock supports connection oriented. */
110 TS_UNBND
, /* CURRENT_state. This is set from keysock_state. */
111 (XPG4_1
) /* Provider flags */
114 /* Named Dispatch Parameter Management Structure */
115 typedef struct keysockparam_s
{
116 uint_t keysock_param_min
;
117 uint_t keysock_param_max
;
118 uint_t keysock_param_value
;
119 char *keysock_param_name
;
123 * Table of NDD variables supported by keysock. These are loaded into
124 * keysock_g_nd in keysock_init_nd.
125 * All of these are alterable, within the min/max values given, at run time.
127 static keysockparam_t lcl_param_arr
[] = {
128 /* min max value name */
129 { 4096, 65536, 8192, "keysock_xmit_hiwat"},
130 { 0, 65536, 1024, "keysock_xmit_lowat"},
131 { 4096, 65536, 8192, "keysock_recv_hiwat"},
132 { 65536, 1024*1024*1024, 256*1024, "keysock_max_buf"},
133 { 0, 3, 0, "keysock_debug"},
135 #define keystack_xmit_hiwat keystack_params[0].keysock_param_value
136 #define keystack_xmit_lowat keystack_params[1].keysock_param_value
137 #define keystack_recv_hiwat keystack_params[2].keysock_param_value
138 #define keystack_max_buf keystack_params[3].keysock_param_value
139 #define keystack_debug keystack_params[4].keysock_param_value
141 #define ks0dbg(a) printf a
142 /* NOTE: != 0 instead of > 0 so lint doesn't complain. */
143 #define ks1dbg(keystack, a) if (keystack->keystack_debug != 0) printf a
144 #define ks2dbg(keystack, a) if (keystack->keystack_debug > 1) printf a
145 #define ks3dbg(keystack, a) if (keystack->keystack_debug > 2) printf a
147 static int keysock_close(queue_t
*, int, cred_t
*);
148 static int keysock_open(queue_t
*, dev_t
*, int, int, cred_t
*);
149 static void keysock_wput(queue_t
*, mblk_t
*);
150 static void keysock_rput(queue_t
*, mblk_t
*);
151 static void keysock_rsrv(queue_t
*);
152 static void keysock_passup(mblk_t
*, sadb_msg_t
*, minor_t
,
153 keysock_consumer_t
*, boolean_t
, keysock_stack_t
*);
154 static void *keysock_stack_init(netstackid_t stackid
, netstack_t
*ns
);
155 static void keysock_stack_fini(netstackid_t stackid
, void *arg
);
157 static struct module_info info
= {
158 5138, "keysock", 1, INFPSZ
, 512, 128
161 static struct qinit rinit
= {
162 (pfi_t
)keysock_rput
, (pfi_t
)keysock_rsrv
, keysock_open
, keysock_close
,
166 static struct qinit winit
= {
167 (pfi_t
)keysock_wput
, NULL
, NULL
, NULL
, NULL
, &info
170 struct streamtab keysockinfo
= {
174 extern struct modlinkage
*keysock_modlp
;
179 * NOTE: New "default" modules will need to be loaded here if needed before
183 /* Keep these in global space to keep the lint from complaining. */
184 static char *IPSECESP
= "ipsecesp";
185 static char *IPSECESPDEV
= "/devices/pseudo/ipsecesp@0:ipsecesp";
186 static char *IPSECAH
= "ipsecah";
187 static char *IPSECAHDEV
= "/devices/pseudo/ipsecah@0:ipsecah";
188 static char *IP6DEV
= "/devices/pseudo/ip6@0:ip6";
189 static char *KEYSOCK
= "keysock";
190 static char *STRMOD
= "strmod";
193 * Load the other ipsec modules and plumb them together.
196 keysock_plumb_ipsec(netstack_t
*ns
)
198 ldi_handle_t lh
, ip6_lh
= NULL
;
199 ldi_ident_t li
= NULL
;
202 boolean_t esp_present
= B_TRUE
;
204 keysock_stack_t
*keystack
= ns
->netstack_keysock
;
207 (void) printf("keysock_plumb_ipsec(%d)\n",
208 ns
->netstack_stackid
);
211 keystack
->keystack_plumbed
= 0; /* we're trying again.. */
213 cr
= zone_get_kcred(netstackid_to_zoneid(
214 keystack
->keystack_netstack
->netstack_stackid
));
217 * Load up the drivers (AH/ESP).
219 * I do this separately from the actual plumbing in case this function
220 * ever gets called from a diskless boot before the root filesystem is
221 * up. I don't have to worry about "keysock" because, well, if I'm
222 * here, keysock must've loaded successfully.
224 if (i_ddi_attach_pseudo_node(IPSECAH
) == NULL
) {
225 ks0dbg(("IPsec: AH failed to attach.\n"));
228 if (i_ddi_attach_pseudo_node(IPSECESP
) == NULL
) {
229 ks0dbg(("IPsec: ESP failed to attach.\n"));
230 esp_present
= B_FALSE
;
234 * Set up the IP streams for AH and ESP, as well as tacking keysock
235 * on top of them. Assume keysock has set the autopushes up already.
239 err
= ldi_ident_from_mod(keysock_modlp
, &li
);
241 ks0dbg(("IPsec: lid_ident_from_mod failed (err %d).\n",
246 err
= ldi_open_by_name(IP6DEV
, FREAD
|FWRITE
, cr
, &ip6_lh
, li
);
248 ks0dbg(("IPsec: Open of IP6 failed (err %d).\n", err
));
252 /* PLINK KEYSOCK/AH */
253 err
= ldi_open_by_name(IPSECAHDEV
, FREAD
|FWRITE
, cr
, &lh
, li
);
255 ks0dbg(("IPsec: Open of AH failed (err %d).\n", err
));
259 I_PUSH
, (intptr_t)KEYSOCK
, FKIOCTL
, cr
, &rval
);
261 ks0dbg(("IPsec: Push of KEYSOCK onto AH failed (err %d).\n",
263 (void) ldi_close(lh
, FREAD
|FWRITE
, cr
);
266 err
= ldi_ioctl(ip6_lh
, I_PLINK
, (intptr_t)lh
,
267 FREAD
+FWRITE
+FNOCTTY
+FKIOCTL
, cr
, &muxid
);
269 ks0dbg(("IPsec: PLINK of KEYSOCK/AH failed (err %d).\n", err
));
270 (void) ldi_close(lh
, FREAD
|FWRITE
, cr
);
273 (void) ldi_close(lh
, FREAD
|FWRITE
, cr
);
275 /* PLINK KEYSOCK/ESP */
277 err
= ldi_open_by_name(IPSECESPDEV
,
278 FREAD
|FWRITE
, cr
, &lh
, li
);
280 ks0dbg(("IPsec: Open of ESP failed (err %d).\n", err
));
284 I_PUSH
, (intptr_t)KEYSOCK
, FKIOCTL
, cr
, &rval
);
287 "Push of KEYSOCK onto ESP failed (err %d).\n",
289 (void) ldi_close(lh
, FREAD
|FWRITE
, cr
);
292 err
= ldi_ioctl(ip6_lh
, I_PLINK
, (intptr_t)lh
,
293 FREAD
+FWRITE
+FNOCTTY
+FKIOCTL
, cr
, &muxid
);
296 "PLINK of KEYSOCK/ESP failed (err %d).\n", err
));
297 (void) ldi_close(lh
, FREAD
|FWRITE
, cr
);
300 (void) ldi_close(lh
, FREAD
|FWRITE
, cr
);
304 keystack
->keystack_plumbed
= (err
== 0) ? 1 : -1;
305 if (ip6_lh
!= NULL
) {
306 (void) ldi_close(ip6_lh
, FREAD
|FWRITE
, cr
);
309 ldi_ident_release(li
);
311 (void) printf("keysock_plumb_ipsec -> %d\n",
312 keystack
->keystack_plumbed
);
320 keysock_param_get(queue_t
*q
, mblk_t
*mp
, caddr_t cp
, cred_t
*cr
)
322 keysockparam_t
*keysockpa
= (keysockparam_t
*)cp
;
324 keysock_t
*ks
= (keysock_t
*)q
->q_ptr
;
325 keysock_stack_t
*keystack
= ks
->keysock_keystack
;
327 mutex_enter(&keystack
->keystack_param_lock
);
328 value
= keysockpa
->keysock_param_value
;
329 mutex_exit(&keystack
->keystack_param_lock
);
331 (void) mi_mpprintf(mp
, "%u", value
);
335 /* This routine sets an NDD variable in a keysockparam_t structure. */
338 keysock_param_set(queue_t
*q
, mblk_t
*mp
, char *value
, caddr_t cp
, cred_t
*cr
)
341 keysockparam_t
*keysockpa
= (keysockparam_t
*)cp
;
342 keysock_t
*ks
= (keysock_t
*)q
->q_ptr
;
343 keysock_stack_t
*keystack
= ks
->keysock_keystack
;
345 /* Convert the value from a string into a long integer. */
346 if (ddi_strtoul(value
, NULL
, 10, &new_value
) != 0)
349 mutex_enter(&keystack
->keystack_param_lock
);
351 * Fail the request if the new value does not lie within the
354 if (new_value
< keysockpa
->keysock_param_min
||
355 new_value
> keysockpa
->keysock_param_max
) {
356 mutex_exit(&keystack
->keystack_param_lock
);
360 /* Set the new value */
361 keysockpa
->keysock_param_value
= new_value
;
362 mutex_exit(&keystack
->keystack_param_lock
);
368 * Initialize keysock at module load time
371 keysock_ddi_init(void)
373 keysock_max_optsize
= optcom_max_optsize(
374 keysock_opt_obj
.odb_opt_des_arr
, keysock_opt_obj
.odb_opt_arr_cnt
);
376 keysock_vmem
= vmem_create("keysock", (void *)1, MAXMIN
, 1,
377 NULL
, NULL
, NULL
, 1, VM_SLEEP
| VMC_IDENTIFIER
);
380 * We want to be informed each time a stack is created or
381 * destroyed in the kernel, so we can maintain the
382 * set of keysock_stack_t's.
384 netstack_register(NS_KEYSOCK
, keysock_stack_init
, NULL
,
391 * Walk through the param array specified registering each element with the
392 * named dispatch handler.
395 keysock_param_register(IDP
*ndp
, keysockparam_t
*ksp
, int cnt
)
397 for (; cnt
-- > 0; ksp
++) {
398 if (ksp
->keysock_param_name
!= NULL
&&
399 ksp
->keysock_param_name
[0]) {
401 ksp
->keysock_param_name
,
402 keysock_param_get
, keysock_param_set
,
413 * Initialize keysock for one stack instance
417 keysock_stack_init(netstackid_t stackid
, netstack_t
*ns
)
419 keysock_stack_t
*keystack
;
422 keystack
= (keysock_stack_t
*)kmem_zalloc(sizeof (*keystack
), KM_SLEEP
);
423 keystack
->keystack_netstack
= ns
;
425 keystack
->keystack_acquire_seq
= 0xffffffff;
427 ksp
= (keysockparam_t
*)kmem_alloc(sizeof (lcl_param_arr
), KM_SLEEP
);
428 keystack
->keystack_params
= ksp
;
429 bcopy(lcl_param_arr
, ksp
, sizeof (lcl_param_arr
));
431 (void) keysock_param_register(&keystack
->keystack_g_nd
, ksp
,
432 A_CNT(lcl_param_arr
));
434 mutex_init(&keystack
->keystack_list_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
435 mutex_init(&keystack
->keystack_consumers_lock
,
436 NULL
, MUTEX_DEFAULT
, NULL
);
437 mutex_init(&keystack
->keystack_param_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
442 * Free NDD variable space, and other destructors, for keysock.
445 keysock_ddi_destroy(void)
447 netstack_unregister(NS_KEYSOCK
);
448 vmem_destroy(keysock_vmem
);
452 * Remove one stack instance from keysock
456 keysock_stack_fini(netstackid_t stackid
, void *arg
)
458 keysock_stack_t
*keystack
= (keysock_stack_t
*)arg
;
460 nd_free(&keystack
->keystack_g_nd
);
461 kmem_free(keystack
->keystack_params
, sizeof (lcl_param_arr
));
462 keystack
->keystack_params
= NULL
;
464 mutex_destroy(&keystack
->keystack_list_lock
);
465 mutex_destroy(&keystack
->keystack_consumers_lock
);
466 mutex_destroy(&keystack
->keystack_param_lock
);
468 kmem_free(keystack
, sizeof (*keystack
));
472 * Close routine for keysock.
476 keysock_close(queue_t
*q
, int flags __unused
, cred_t
*credp __unused
)
479 keysock_consumer_t
*kc
;
480 void *ptr
= q
->q_ptr
;
482 keysock_stack_t
*keystack
;
487 /* Safe assumption. */
491 kc
= (keysock_consumer_t
*)ptr
;
492 keystack
= kc
->kc_keystack
;
494 ks1dbg(keystack
, ("Module close, removing a consumer (%d).\n",
497 * Because of PERMOD open/close exclusive perimeter, I
498 * can inspect KC_FLUSHING w/o locking down kc->kc_lock.
500 if (kc
->kc_flags
& KC_FLUSHING
) {
502 * If this decrement was the last one, send
503 * down the next pending one, if any.
505 * With a PERMOD perimeter, the mutexes ops aren't
506 * really necessary, but if we ever loosen up, we will
507 * have this bit covered already.
509 keystack
->keystack_flushdump
--;
510 if (keystack
->keystack_flushdump
== 0) {
512 * The flush/dump terminated by having a
513 * consumer go away. I need to send up to the
514 * appropriate keysock all of the relevant
515 * information. Unfortunately, I don't
518 ks0dbg(("Consumer went away while flushing or"
522 size
= sizeof (keysock_consumer_t
);
523 mutex_enter(&keystack
->keystack_consumers_lock
);
524 keystack
->keystack_consumers
[kc
->kc_sa_type
] = NULL
;
525 mutex_exit(&keystack
->keystack_consumers_lock
);
526 mutex_destroy(&kc
->kc_lock
);
527 netstack_rele(kc
->kc_keystack
->keystack_netstack
);
529 ks
= (keysock_t
*)ptr
;
530 keystack
= ks
->keysock_keystack
;
533 ("Driver close, PF_KEY socket is going away.\n"));
534 if ((ks
->keysock_flags
& KEYSOCK_EXTENDED
) != 0)
535 atomic_dec_32(&keystack
->keystack_num_extended
);
536 size
= sizeof (keysock_t
);
537 mutex_enter(&keystack
->keystack_list_lock
);
538 *(ks
->keysock_ptpn
) = ks
->keysock_next
;
539 if (ks
->keysock_next
!= NULL
)
540 ks
->keysock_next
->keysock_ptpn
= ks
->keysock_ptpn
;
541 mutex_exit(&keystack
->keystack_list_lock
);
542 mutex_destroy(&ks
->keysock_lock
);
543 vmem_free(keysock_vmem
, (void *)(uintptr_t)ks
->keysock_serial
,
545 netstack_rele(ks
->keysock_keystack
->keystack_netstack
);
549 kmem_free(ptr
, size
);
553 * Open routine for keysock.
557 keysock_open(queue_t
*q
, dev_t
*devp
, int flag
, int sflag
, cred_t
*credp
)
560 keysock_consumer_t
*kc
;
564 keysock_stack_t
*keystack
;
566 if (secpolicy_ip_config(credp
, B_FALSE
) != 0) {
567 /* Privilege debugging will log the error */
571 if (q
->q_ptr
!= NULL
)
572 return (0); /* Re-open of an already open instance. */
574 ns
= netstack_find_by_cred(credp
);
576 keystack
= ns
->netstack_keysock
;
577 ASSERT(keystack
!= NULL
);
579 ks3dbg(keystack
, ("Entering keysock open.\n"));
581 if (keystack
->keystack_plumbed
< 1) {
582 netstack_t
*ns
= keystack
->keystack_netstack
;
584 keystack
->keystack_plumbed
= 0;
586 printf("keysock_open(%d) - plumb\n",
587 keystack
->keystack_netstack
->netstack_stackid
);
590 * Don't worry about ipsec_failure being true here.
591 * (See ip.c). An open of keysock should try and force
592 * the issue. Maybe it was a transient failure.
594 ipsec_loader_loadnow(ns
->netstack_ipsec
);
597 if (sflag
& MODOPEN
) {
598 /* Initialize keysock_consumer state here. */
599 kc
= kmem_zalloc(sizeof (keysock_consumer_t
), KM_NOSLEEP
);
601 netstack_rele(keystack
->keystack_netstack
);
604 mutex_init(&kc
->kc_lock
, NULL
, MUTEX_DEFAULT
, 0);
611 kc
->kc_keystack
= keystack
;
615 * Send down initial message to whatever I was pushed on top
616 * of asking for its consumer type. The reply will set it.
620 mp
= allocb(sizeof (ipsec_info_t
), BPRI_HI
);
623 "keysock_open: Cannot allocate KEYSOCK_HELLO.\n"));
624 /* Do I need to set these to null? */
627 mutex_destroy(&kc
->kc_lock
);
628 kmem_free(kc
, sizeof (*kc
));
629 netstack_rele(keystack
->keystack_netstack
);
633 /* If I allocated okay, putnext to what I was pushed atop. */
634 mp
->b_wptr
+= sizeof (ipsec_info_t
);
635 mp
->b_datap
->db_type
= M_CTL
;
636 ii
= (ipsec_info_t
*)mp
->b_rptr
;
637 ii
->ipsec_info_type
= KEYSOCK_HELLO
;
638 /* Length only of type/len. */
639 ii
->ipsec_info_len
= sizeof (ii
->ipsec_allu
);
640 ks2dbg(keystack
, ("Ready to putnext KEYSOCK_HELLO.\n"));
641 putnext(kc
->kc_wq
, mp
);
645 /* Initialize keysock state. */
647 ks2dbg(keystack
, ("Made it into PF_KEY socket open.\n"));
649 ksminor
= (minor_t
)(uintptr_t)
650 vmem_alloc(keysock_vmem
, 1, VM_NOSLEEP
);
652 netstack_rele(keystack
->keystack_netstack
);
655 ks
= kmem_zalloc(sizeof (keysock_t
), KM_NOSLEEP
);
657 vmem_free(keysock_vmem
, (void *)(uintptr_t)ksminor
, 1);
658 netstack_rele(keystack
->keystack_netstack
);
662 mutex_init(&ks
->keysock_lock
, NULL
, MUTEX_DEFAULT
, 0);
664 ks
->keysock_wq
= WR(q
);
665 ks
->keysock_state
= TS_UNBND
;
666 ks
->keysock_serial
= ksminor
;
670 ks
->keysock_keystack
= keystack
;
673 * The receive hiwat is only looked at on the stream head
674 * queue. Store in q_hiwat in order to return on SO_RCVBUF
678 q
->q_hiwat
= keystack
->keystack_recv_hiwat
;
681 * The transmit hiwat/lowat is only looked at on IP's queue.
682 * Store in q_hiwat/q_lowat in order to return on
683 * SO_SNDBUF/SO_SNDLOWAT getsockopts.
686 WR(q
)->q_hiwat
= keystack
->keystack_xmit_hiwat
;
687 WR(q
)->q_lowat
= keystack
->keystack_xmit_lowat
;
689 *devp
= makedevice(getmajor(*devp
), ksminor
);
692 * Thread keysock into the global keysock list.
694 mutex_enter(&keystack
->keystack_list_lock
);
695 ks
->keysock_next
= keystack
->keystack_list
;
696 ks
->keysock_ptpn
= &keystack
->keystack_list
;
697 if (keystack
->keystack_list
!= NULL
) {
698 keystack
->keystack_list
->keysock_ptpn
=
701 keystack
->keystack_list
= ks
;
702 mutex_exit(&keystack
->keystack_list_lock
);
705 (void) proto_set_rx_hiwat(q
, NULL
,
706 keystack
->keystack_recv_hiwat
);
708 * Wait outside the keysock module perimeter for IPsec
709 * plumbing to be completed. If it fails, keysock_close()
710 * undoes everything we just did.
712 if (!ipsec_loader_wait(q
,
713 keystack
->keystack_netstack
->netstack_ipsec
)) {
714 (void) keysock_close(q
, 0, credp
);
715 return (EPFNOSUPPORT
);
722 /* BELOW THIS LINE ARE ROUTINES INCLUDING AND RELATED TO keysock_wput(). */
725 * Copy relevant state bits.
728 keysock_copy_info(struct T_info_ack
*tap
, keysock_t
*ks
)
730 *tap
= keysock_g_t_info_ack
;
731 tap
->CURRENT_state
= ks
->keysock_state
;
732 tap
->OPT_size
= keysock_max_optsize
;
736 * This routine responds to T_CAPABILITY_REQ messages. It is called by
737 * keysock_wput. Much of the T_CAPABILITY_ACK information is copied from
738 * keysock_g_t_info_ack. The current state of the stream is copied from
742 keysock_capability_req(queue_t
*q
, mblk_t
*mp
)
744 keysock_t
*ks
= (keysock_t
*)q
->q_ptr
;
745 t_uscalar_t cap_bits1
;
746 struct T_capability_ack
*tcap
;
748 cap_bits1
= ((struct T_capability_req
*)mp
->b_rptr
)->CAP_bits1
;
750 mp
= tpi_ack_alloc(mp
, sizeof (struct T_capability_ack
),
751 mp
->b_datap
->db_type
, T_CAPABILITY_ACK
);
755 tcap
= (struct T_capability_ack
*)mp
->b_rptr
;
758 if (cap_bits1
& TC1_INFO
) {
759 keysock_copy_info(&tcap
->INFO_ack
, ks
);
760 tcap
->CAP_bits1
|= TC1_INFO
;
767 * This routine responds to T_INFO_REQ messages. It is called by
768 * keysock_wput_other.
769 * Most of the T_INFO_ACK information is copied from keysock_g_t_info_ack.
770 * The current state of the stream is copied from keysock_state.
773 keysock_info_req(queue_t
*q
, mblk_t
*mp
)
775 mp
= tpi_ack_alloc(mp
, sizeof (struct T_info_ack
), M_PCPROTO
,
779 keysock_copy_info((struct T_info_ack
*)mp
->b_rptr
,
780 (keysock_t
*)q
->q_ptr
);
785 * keysock_err_ack. This routine creates a
786 * T_ERROR_ACK message and passes it
790 keysock_err_ack(queue_t
*q
, mblk_t
*mp
, int t_error
, int sys_error
)
792 if ((mp
= mi_tpi_err_ack_alloc(mp
, t_error
, sys_error
)) != NULL
)
797 * This routine retrieves the current status of socket options.
798 * It returns the size of the option retrieved.
802 keysock_opt_get(queue_t
*q
, int level
, int name
, uchar_t
*ptr
)
804 int *i1
= (int *)ptr
;
805 keysock_t
*ks
= (keysock_t
*)q
->q_ptr
;
809 mutex_enter(&ks
->keysock_lock
);
815 *i1
= (int)(!((ks
->keysock_flags
& KEYSOCK_NOLOOP
) ==
819 * The following two items can be manipulated,
820 * but changing them should do nothing.
823 *i1
= (int)q
->q_hiwat
;
826 *i1
= (int)(RD(q
)->q_hiwat
);
829 mutex_exit(&ks
->keysock_lock
);
834 return (sizeof (int));
838 * This routine sets socket options.
842 keysock_opt_set(queue_t
*q
, uint_t mgmt_flags
, int level
,
843 int name
, uint_t inlen
, uchar_t
*invalp
, uint_t
*outlenp
,
844 uchar_t
*outvalp
, void *thisdg_attrs
, cred_t
*cr
)
846 int *i1
= (int *)invalp
, errno
= 0;
847 keysock_t
*ks
= (keysock_t
*)q
->q_ptr
;
848 keysock_stack_t
*keystack
= ks
->keysock_keystack
;
852 mutex_enter(&ks
->keysock_lock
);
856 ks
->keysock_flags
|= KEYSOCK_NOLOOP
;
857 else ks
->keysock_flags
&= ~KEYSOCK_NOLOOP
;
860 if (*i1
> keystack
->keystack_max_buf
)
862 else q
->q_hiwat
= *i1
;
865 if (*i1
> keystack
->keystack_max_buf
) {
868 RD(q
)->q_hiwat
= *i1
;
869 (void) proto_set_rx_hiwat(RD(q
), NULL
, *i1
);
875 mutex_exit(&ks
->keysock_lock
);
884 * Handle STREAMS ioctl copyin for getsockname() for both PF_KEY and
888 keysock_spdsock_wput_iocdata(queue_t
*q
, mblk_t
*mp
, sa_family_t family
)
891 STRUCT_HANDLE(strbuf
, sb
);
892 /* What size of sockaddr do we need? */
893 const uint_t addrlen
= sizeof (struct sockaddr
);
895 /* We only handle TI_GET{MY,PEER}NAME (get{sock,peer}name()). */
896 switch (((struct iocblk
*)mp
->b_rptr
)->ioc_cmd
) {
905 switch (mi_copy_state(q
, mp
, &mp1
)) {
908 case MI_COPY_CASE(MI_COPY_IN
, 1):
910 case MI_COPY_CASE(MI_COPY_OUT
, 1):
912 * The address has been copied out, so now
913 * copyout the strbuf.
917 case MI_COPY_CASE(MI_COPY_OUT
, 2):
919 * The address and strbuf have been copied out.
920 * We're done, so just acknowledge the original
923 mi_copy_done(q
, mp
, 0);
927 * Something strange has happened, so acknowledge
928 * the original M_IOCTL with an EPROTO error.
930 mi_copy_done(q
, mp
, EPROTO
);
935 * Now we have the strbuf structure for TI_GET{MY,PEER}NAME. Next we
936 * copyout the requested address and then we'll copyout the strbuf.
937 * Regardless of sockname or peername, we just return a sockaddr with
940 STRUCT_SET_HANDLE(sb
, ((struct iocblk
*)mp
->b_rptr
)->ioc_flag
,
941 (void *)mp1
->b_rptr
);
943 if (STRUCT_FGET(sb
, maxlen
) < addrlen
) {
944 mi_copy_done(q
, mp
, EINVAL
);
948 mp1
= mi_copyout_alloc(q
, mp
, STRUCT_FGETP(sb
, buf
), addrlen
, B_TRUE
);
952 STRUCT_FSET(sb
, len
, addrlen
);
953 ((struct sockaddr
*)mp1
->b_wptr
)->sa_family
= family
;
954 mp1
->b_wptr
+= addrlen
;
959 * Handle STREAMS messages.
962 keysock_wput_other(queue_t
*q
, mblk_t
*mp
)
966 keysock_t
*ks
= (keysock_t
*)q
->q_ptr
;
967 keysock_stack_t
*keystack
= ks
->keysock_keystack
;
970 switch (mp
->b_datap
->db_type
) {
973 if ((mp
->b_wptr
- mp
->b_rptr
) < sizeof (long)) {
975 "keysock_wput_other: Not big enough M_PROTO\n"));
979 switch (((union T_primitives
*)mp
->b_rptr
)->type
) {
980 case T_CAPABILITY_REQ
:
981 keysock_capability_req(q
, mp
);
984 keysock_info_req(q
, mp
);
986 case T_SVR4_OPTMGMT_REQ
:
989 * All Solaris components should pass a db_credp
990 * for this TPI message, hence we ASSERT.
991 * But in case there is some other M_PROTO that looks
992 * like a TPI message sent by some other kernel
993 * component, we check and return an error.
995 cr
= msg_getcred(mp
, NULL
);
998 keysock_err_ack(q
, mp
, TSYSERR
, EINVAL
);
1001 if (((union T_primitives
*)mp
->b_rptr
)->type
==
1002 T_SVR4_OPTMGMT_REQ
) {
1003 svr4_optcom_req(q
, mp
, cr
, &keysock_opt_obj
);
1005 tpi_optcom_req(q
, mp
, cr
, &keysock_opt_obj
);
1011 /* Illegal for keysock. */
1013 (void) putnextctl1(RD(q
), M_ERROR
, EPROTO
);
1016 /* Not supported by keysock. */
1017 keysock_err_ack(q
, mp
, TNOTSUPPORT
, 0);
1022 keysock_spdsock_wput_iocdata(q
, mp
, PF_KEY
);
1025 iocp
= (struct iocblk
*)mp
->b_rptr
;
1028 switch (iocp
->ioc_cmd
) {
1030 case TI_GETPEERNAME
:
1032 * For pfiles(1) observability with getsockname().
1033 * See keysock_spdsock_wput_iocdata() for the rest of
1036 mi_copyin(q
, mp
, NULL
,
1037 SIZEOF_STRUCT(strbuf
, iocp
->ioc_flag
));
1041 if (nd_getset(q
, keystack
->keystack_g_nd
, mp
)) {
1048 miocnak(q
, mp
, 0, error
);
1052 if (*mp
->b_rptr
& FLUSHW
) {
1053 flushq(q
, FLUSHALL
);
1054 *mp
->b_rptr
&= ~FLUSHW
;
1056 if (*mp
->b_rptr
& FLUSHR
) {
1063 /* If fell through, just black-hole the message. */
1068 * Transmit a PF_KEY error message to the instance either pointed to
1069 * by ks, the instance with serial number serial, or more, depending.
1071 * The faulty message (or a reasonable facsimile thereof) is in mp.
1072 * This function will free mp or recycle it for delivery, thereby causing
1073 * the stream head to free it.
1076 keysock_error(keysock_t
*ks
, mblk_t
*mp
, int error
, int diagnostic
)
1078 sadb_msg_t
*samsg
= (sadb_msg_t
*)mp
->b_rptr
;
1079 keysock_stack_t
*keystack
= ks
->keysock_keystack
;
1081 ASSERT(mp
->b_datap
->db_type
== M_DATA
);
1083 if (samsg
->sadb_msg_type
< SADB_GETSPI
||
1084 samsg
->sadb_msg_type
> SADB_MAX
)
1085 samsg
->sadb_msg_type
= SADB_RESERVED
;
1088 * Strip out extension headers.
1090 ASSERT(mp
->b_rptr
+ sizeof (*samsg
) <= mp
->b_datap
->db_lim
);
1091 mp
->b_wptr
= mp
->b_rptr
+ sizeof (*samsg
);
1092 samsg
->sadb_msg_len
= SADB_8TO64(sizeof (sadb_msg_t
));
1093 samsg
->sadb_msg_errno
= (uint8_t)error
;
1094 samsg
->sadb_x_msg_diagnostic
= (uint16_t)diagnostic
;
1096 keysock_passup(mp
, samsg
, ks
->keysock_serial
, NULL
, B_FALSE
, keystack
);
1100 * Pass down a message to a consumer. Wrap it in KEYSOCK_IN, and copy
1101 * in the extv if passed in.
1104 keysock_passdown(keysock_t
*ks
, mblk_t
*mp
, uint8_t satype
, sadb_ext_t
*extv
[],
1107 keysock_consumer_t
*kc
;
1111 keysock_stack_t
*keystack
= ks
->keysock_keystack
;
1113 wrapper
= allocb(sizeof (ipsec_info_t
), BPRI_HI
);
1114 if (wrapper
== NULL
) {
1115 ks3dbg(keystack
, ("keysock_passdown: allocb failed.\n"));
1116 if (extv
[SADB_EXT_KEY_ENCRYPT
] != NULL
)
1117 bzero(extv
[SADB_EXT_KEY_ENCRYPT
],
1119 extv
[SADB_EXT_KEY_ENCRYPT
]->sadb_ext_len
));
1120 if (extv
[SADB_EXT_KEY_AUTH
] != NULL
)
1121 bzero(extv
[SADB_EXT_KEY_AUTH
],
1123 extv
[SADB_EXT_KEY_AUTH
]->sadb_ext_len
));
1126 "keysock: Downwards flush/dump message failed!\n"));
1127 /* If this is true, I hold the perimeter. */
1128 keystack
->keystack_flushdump
--;
1134 wrapper
->b_datap
->db_type
= M_CTL
;
1135 ksi
= (keysock_in_t
*)wrapper
->b_rptr
;
1136 ksi
->ks_in_type
= KEYSOCK_IN
;
1137 ksi
->ks_in_len
= sizeof (keysock_in_t
);
1138 if (extv
[SADB_EXT_ADDRESS_SRC
] != NULL
)
1139 ksi
->ks_in_srctype
= KS_IN_ADDR_UNKNOWN
;
1140 else ksi
->ks_in_srctype
= KS_IN_ADDR_NOTTHERE
;
1141 if (extv
[SADB_EXT_ADDRESS_DST
] != NULL
)
1142 ksi
->ks_in_dsttype
= KS_IN_ADDR_UNKNOWN
;
1143 else ksi
->ks_in_dsttype
= KS_IN_ADDR_NOTTHERE
;
1144 for (i
= 0; i
<= SADB_EXT_MAX
; i
++)
1145 ksi
->ks_in_extv
[i
] = extv
[i
];
1146 ksi
->ks_in_serial
= ks
->keysock_serial
;
1147 wrapper
->b_wptr
+= sizeof (ipsec_info_t
);
1148 wrapper
->b_cont
= mp
;
1151 * Find the appropriate consumer where the message is passed down.
1153 kc
= keystack
->keystack_consumers
[satype
];
1156 keysock_error(ks
, mp
, EINVAL
, SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE
);
1159 "keysock: Downwards flush/dump message failed!\n"));
1160 /* If this is true, I hold the perimeter. */
1161 keystack
->keystack_flushdump
--;
1167 * NOTE: There used to be code in here to spin while a flush or
1168 * dump finished. Keysock now assumes that consumers have enough
1169 * MT-savviness to deal with that.
1173 * Current consumers (AH and ESP) are guaranteed to return a
1174 * FLUSH or DUMP message back, so when we reach here, we don't
1175 * have to worry about keysock_flushdumps.
1178 putnext(kc
->kc_wq
, wrapper
);
1182 * High-level reality checking of extensions.
1185 ext_check(sadb_ext_t
*ext
, keysock_stack_t
*keystack
)
1192 switch (ext
->sadb_ext_type
) {
1193 case SADB_EXT_ADDRESS_SRC
:
1194 case SADB_EXT_ADDRESS_DST
:
1195 case SADB_X_EXT_ADDRESS_INNER_SRC
:
1196 case SADB_X_EXT_ADDRESS_INNER_DST
:
1197 /* Check for at least enough addtl length for a sockaddr. */
1198 if (ext
->sadb_ext_len
<= SADB_8TO64(sizeof (sadb_address_t
)))
1201 case SADB_EXT_LIFETIME_HARD
:
1202 case SADB_EXT_LIFETIME_SOFT
:
1203 case SADB_EXT_LIFETIME_CURRENT
:
1204 if (ext
->sadb_ext_len
!= SADB_8TO64(sizeof (sadb_lifetime_t
)))
1207 case SADB_EXT_SPIRANGE
:
1208 /* See if the SPI range is legit. */
1209 if (htonl(((sadb_spirange_t
*)ext
)->sadb_spirange_min
) >
1210 htonl(((sadb_spirange_t
*)ext
)->sadb_spirange_max
))
1213 case SADB_EXT_KEY_AUTH
:
1214 case SADB_EXT_KEY_ENCRYPT
:
1215 /* Key length check. */
1216 if (((sadb_key_t
*)ext
)->sadb_key_bits
== 0)
1219 * Check to see if the key length (in bits) is less than the
1220 * extension length (in 8-bits words).
1222 if ((roundup(SADB_1TO8(((sadb_key_t
*)ext
)->sadb_key_bits
), 8) +
1223 sizeof (sadb_key_t
)) != SADB_64TO8(ext
->sadb_ext_len
)) {
1225 "ext_check: Key bits/length inconsistent.\n"));
1226 ks1dbg(keystack
, ("%d bits, len is %d bytes.\n",
1227 ((sadb_key_t
*)ext
)->sadb_key_bits
,
1228 SADB_64TO8(ext
->sadb_ext_len
)));
1232 /* All-zeroes key check. */
1233 lp
= (uint64_t *)(((char *)ext
) + sizeof (sadb_key_t
));
1235 i
< (ext
->sadb_ext_len
- SADB_8TO64(sizeof (sadb_key_t
)));
1238 break; /* Out of for loop. */
1239 /* If finished the loop naturally, it's an all zero key. */
1243 case SADB_EXT_IDENTITY_SRC
:
1244 case SADB_EXT_IDENTITY_DST
:
1246 * Make sure the strings in these identities are
1247 * null-terminated. RFC 2367 underspecified how to handle
1248 * such a case. I "proactively" null-terminate the string
1249 * at the last byte if it's not terminated sooner.
1251 id
= (sadb_ident_t
*)ext
;
1252 i
= SADB_64TO8(id
->sadb_ident_len
);
1253 i
-= sizeof (sadb_ident_t
);
1254 idstr
= (char *)(id
+ 1);
1255 while (*idstr
!= '\0' && i
> 0) {
1261 * I.e., if the bozo user didn't NULL-terminate the
1269 return (B_TRUE
); /* For now... */
1272 /* Return values for keysock_get_ext(). */
1280 * Parse basic extension headers and return in the passed-in pointer vector.
1281 * Return values include:
1283 * KGE_OK Everything's nice and parsed out.
1284 * If there are no extensions, place NULL in extv[0].
1285 * KGE_DUP There is a duplicate extension.
1286 * First instance in appropriate bin. First duplicate in
1288 * KGE_UNK Unknown extension type encountered. extv[0] contains
1290 * KGE_LEN Extension length error.
1291 * KGE_CHK High-level reality check failed on specific extension.
1293 * My apologies for some of the pointer arithmetic in here. I'm thinking
1294 * like an assembly programmer, yet trying to make the compiler happy.
1297 keysock_get_ext(sadb_ext_t
*extv
[], sadb_msg_t
*basehdr
, uint_t msgsize
,
1298 keysock_stack_t
*keystack
)
1300 bzero(extv
, sizeof (sadb_ext_t
*) * (SADB_EXT_MAX
+ 1));
1302 /* Use extv[0] as the "current working pointer". */
1304 extv
[0] = (sadb_ext_t
*)(basehdr
+ 1);
1306 while (extv
[0] < (sadb_ext_t
*)(((uint8_t *)basehdr
) + msgsize
)) {
1307 /* Check for unknown headers. */
1308 if (extv
[0]->sadb_ext_type
== 0 ||
1309 extv
[0]->sadb_ext_type
> SADB_EXT_MAX
)
1313 * Check length. Use uint64_t because extlen is in units
1314 * of 64-bit words. If length goes beyond the msgsize,
1315 * return an error. (Zero length also qualifies here.)
1317 if (extv
[0]->sadb_ext_len
== 0 ||
1318 (void *)((uint64_t *)extv
[0] + extv
[0]->sadb_ext_len
) >
1319 (void *)((uint8_t *)basehdr
+ msgsize
))
1322 /* Check for redundant headers. */
1323 if (extv
[extv
[0]->sadb_ext_type
] != NULL
)
1327 * Reality check the extension if possible at the keysock
1330 if (!ext_check(extv
[0], keystack
))
1333 /* If I make it here, assign the appropriate bin. */
1334 extv
[extv
[0]->sadb_ext_type
] = extv
[0];
1336 /* Advance pointer (See above for uint64_t ptr reasoning.) */
1337 extv
[0] = (sadb_ext_t
*)
1338 ((uint64_t *)extv
[0] + extv
[0]->sadb_ext_len
);
1341 /* Everything's cool. */
1344 * If extv[0] == NULL, then there are no extension headers in this
1345 * message. Ensure that this is the case.
1347 if (extv
[0] == (sadb_ext_t
*)(basehdr
+ 1))
1354 * qwriter() callback to handle flushes and dumps. This routine will hold
1355 * the inner perimeter.
1358 keysock_do_flushdump(queue_t
*q
, mblk_t
*mp
)
1360 int i
, start
, finish
;
1362 keysock_t
*ks
= (keysock_t
*)q
->q_ptr
;
1363 sadb_ext_t
*extv
[SADB_EXT_MAX
+ 1];
1364 sadb_msg_t
*samsg
= (sadb_msg_t
*)mp
->b_rptr
;
1365 keysock_stack_t
*keystack
= ks
->keysock_keystack
;
1368 * I am guaranteed this will work. I did the work in keysock_parse()
1371 (void) keysock_get_ext(extv
, samsg
, SADB_64TO8(samsg
->sadb_msg_len
),
1375 * I hold the perimeter, therefore I don't need to use atomic ops.
1377 if (keystack
->keystack_flushdump
!= 0) {
1378 /* XXX Should I instead use EBUSY? */
1379 /* XXX Or is there a way to queue these up? */
1380 keysock_error(ks
, mp
, ENOMEM
, SADB_X_DIAGNOSTIC_NONE
);
1384 if (samsg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
) {
1386 finish
= KEYSOCK_MAX_CONSUMERS
- 1;
1388 start
= samsg
->sadb_msg_satype
;
1389 finish
= samsg
->sadb_msg_satype
;
1393 * Fill up keysock_flushdump with the number of outstanding dumps
1397 keystack
->keystack_flushdump_errno
= 0;
1400 * Okay, I hold the perimeter. Eventually keysock_flushdump will
1401 * contain the number of consumers with outstanding flush operations.
1403 * SO, here's the plan:
1404 * * For each relevant consumer (Might be one, might be all)
1405 * * Twiddle on the FLUSHING flag.
1406 * * Pass down the FLUSH/DUMP message.
1408 * When I see upbound FLUSH/DUMP messages, I will decrement the
1409 * keysock_flushdump. When I decrement it to 0, I will pass the
1410 * FLUSH/DUMP message back up to the PF_KEY sockets. Because I will
1411 * pass down the right SA type to the consumer (either its own, or
1412 * that of UNSPEC), the right one will be reflected from each consumer,
1413 * and accordingly back to the socket.
1416 mutex_enter(&keystack
->keystack_consumers_lock
);
1417 for (i
= start
; i
<= finish
; i
++) {
1418 if (keystack
->keystack_consumers
[i
] != NULL
) {
1421 ks0dbg(("SADB_FLUSH copymsg() failed.\n"));
1423 * Error? And what about outstanding
1424 * flushes? Oh, yeah, they get sucked up and
1425 * the counter is decremented. Consumers
1426 * (see keysock_passdown()) are guaranteed
1427 * to deliver back a flush request, even if
1430 keysock_error(ks
, mp
, ENOMEM
,
1431 SADB_X_DIAGNOSTIC_NONE
);
1435 * Because my entry conditions are met above, the
1436 * following assertion should hold true.
1438 mutex_enter(&keystack
->keystack_consumers
[i
]->kc_lock
);
1439 ASSERT((keystack
->keystack_consumers
[i
]->kc_flags
&
1441 keystack
->keystack_consumers
[i
]->kc_flags
|=
1443 mutex_exit(&(keystack
->keystack_consumers
[i
]->kc_lock
));
1444 /* Always increment the number of flushes... */
1445 keystack
->keystack_flushdump
++;
1446 /* Guaranteed to return a message. */
1447 keysock_passdown(ks
, mp1
, i
, extv
, B_TRUE
);
1448 } else if (start
== finish
) {
1450 * In case where start == finish, and there's no
1451 * consumer, should we force an error? Yes.
1453 mutex_exit(&keystack
->keystack_consumers_lock
);
1454 keysock_error(ks
, mp
, EINVAL
,
1455 SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE
);
1459 mutex_exit(&keystack
->keystack_consumers_lock
);
1461 if (keystack
->keystack_flushdump
== 0) {
1463 * There were no consumers at all for this message.
1464 * XXX For now return ESRCH.
1466 keysock_error(ks
, mp
, ESRCH
, SADB_X_DIAGNOSTIC_NO_SADBS
);
1468 /* Otherwise, free the original message. */
1474 * Get the right diagnostic for a duplicate. Should probably use a static
1478 keysock_duplicate(int ext_type
)
1483 case SADB_EXT_ADDRESS_SRC
:
1484 rc
= SADB_X_DIAGNOSTIC_DUPLICATE_SRC
;
1486 case SADB_EXT_ADDRESS_DST
:
1487 rc
= SADB_X_DIAGNOSTIC_DUPLICATE_DST
;
1489 case SADB_X_EXT_ADDRESS_INNER_SRC
:
1490 rc
= SADB_X_DIAGNOSTIC_DUPLICATE_INNER_SRC
;
1492 case SADB_X_EXT_ADDRESS_INNER_DST
:
1493 rc
= SADB_X_DIAGNOSTIC_DUPLICATE_INNER_DST
;
1496 rc
= SADB_X_DIAGNOSTIC_DUPLICATE_SA
;
1498 case SADB_EXT_SPIRANGE
:
1499 rc
= SADB_X_DIAGNOSTIC_DUPLICATE_RANGE
;
1501 case SADB_EXT_KEY_AUTH
:
1502 rc
= SADB_X_DIAGNOSTIC_DUPLICATE_AKEY
;
1504 case SADB_EXT_KEY_ENCRYPT
:
1505 rc
= SADB_X_DIAGNOSTIC_DUPLICATE_EKEY
;
1512 * Get the right diagnostic for a reality check failure. Should probably use
1513 * a static table lookup.
1516 keysock_malformed(int ext_type
)
1521 case SADB_EXT_ADDRESS_SRC
:
1522 rc
= SADB_X_DIAGNOSTIC_MALFORMED_SRC
;
1524 case SADB_EXT_ADDRESS_DST
:
1525 rc
= SADB_X_DIAGNOSTIC_MALFORMED_DST
;
1527 case SADB_X_EXT_ADDRESS_INNER_SRC
:
1528 rc
= SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC
;
1530 case SADB_X_EXT_ADDRESS_INNER_DST
:
1531 rc
= SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST
;
1534 rc
= SADB_X_DIAGNOSTIC_MALFORMED_SA
;
1536 case SADB_EXT_SPIRANGE
:
1537 rc
= SADB_X_DIAGNOSTIC_MALFORMED_RANGE
;
1539 case SADB_EXT_KEY_AUTH
:
1540 rc
= SADB_X_DIAGNOSTIC_MALFORMED_AKEY
;
1542 case SADB_EXT_KEY_ENCRYPT
:
1543 rc
= SADB_X_DIAGNOSTIC_MALFORMED_EKEY
;
1550 * Keysock massaging of an inverse ACQUIRE. Consult policy,
1551 * and construct an appropriate response.
1554 keysock_inverse_acquire(mblk_t
*mp
, sadb_msg_t
*samsg
, sadb_ext_t
*extv
[],
1558 keysock_stack_t
*keystack
= ks
->keysock_keystack
;
1561 * Reality check things...
1563 if (extv
[SADB_EXT_ADDRESS_SRC
] == NULL
) {
1564 keysock_error(ks
, mp
, EINVAL
, SADB_X_DIAGNOSTIC_MISSING_SRC
);
1567 if (extv
[SADB_EXT_ADDRESS_DST
] == NULL
) {
1568 keysock_error(ks
, mp
, EINVAL
, SADB_X_DIAGNOSTIC_MISSING_DST
);
1572 if (extv
[SADB_X_EXT_ADDRESS_INNER_SRC
] != NULL
&&
1573 extv
[SADB_X_EXT_ADDRESS_INNER_DST
] == NULL
) {
1574 keysock_error(ks
, mp
, EINVAL
,
1575 SADB_X_DIAGNOSTIC_MISSING_INNER_DST
);
1579 if (extv
[SADB_X_EXT_ADDRESS_INNER_SRC
] == NULL
&&
1580 extv
[SADB_X_EXT_ADDRESS_INNER_DST
] != NULL
) {
1581 keysock_error(ks
, mp
, EINVAL
,
1582 SADB_X_DIAGNOSTIC_MISSING_INNER_SRC
);
1586 reply_mp
= ipsec_construct_inverse_acquire(samsg
, extv
,
1587 keystack
->keystack_netstack
);
1589 if (reply_mp
!= NULL
) {
1591 keysock_passup(reply_mp
, (sadb_msg_t
*)reply_mp
->b_rptr
,
1592 ks
->keysock_serial
, NULL
, B_FALSE
, keystack
);
1594 keysock_error(ks
, mp
, samsg
->sadb_msg_errno
,
1595 samsg
->sadb_x_msg_diagnostic
);
1600 * Spew an extended REGISTER down to the relevant consumers.
1603 keysock_extended_register(keysock_t
*ks
, mblk_t
*mp
, sadb_ext_t
*extv
[])
1605 sadb_x_ereg_t
*ereg
= (sadb_x_ereg_t
*)extv
[SADB_X_EXT_EREG
];
1606 uint8_t *satypes
, *fencepost
;
1608 sadb_ext_t
*downextv
[SADB_EXT_MAX
+ 1];
1609 keysock_stack_t
*keystack
= ks
->keysock_keystack
;
1611 if (ks
->keysock_registered
[0] != 0 || ks
->keysock_registered
[1] != 0 ||
1612 ks
->keysock_registered
[2] != 0 || ks
->keysock_registered
[3] != 0) {
1613 keysock_error(ks
, mp
, EBUSY
, 0);
1616 ks
->keysock_flags
|= KEYSOCK_EXTENDED
;
1618 keysock_error(ks
, mp
, EINVAL
, SADB_X_DIAGNOSTIC_SATYPE_NEEDED
);
1620 ASSERT(mp
->b_rptr
+ msgdsize(mp
) == mp
->b_wptr
);
1621 fencepost
= (uint8_t *)mp
->b_wptr
;
1622 satypes
= ereg
->sadb_x_ereg_satypes
;
1623 while (*satypes
!= SADB_SATYPE_UNSPEC
&& satypes
!= fencepost
) {
1624 downmp
= copymsg(mp
);
1625 if (downmp
== NULL
) {
1626 keysock_error(ks
, mp
, ENOMEM
, 0);
1630 * Since we've made it here, keysock_get_ext will work!
1632 (void) keysock_get_ext(downextv
,
1633 (sadb_msg_t
*)downmp
->b_rptr
, msgdsize(downmp
),
1635 keysock_passdown(ks
, downmp
, *satypes
, downextv
,
1643 * Set global to indicate we prefer an extended ACQUIRE.
1645 atomic_inc_32(&keystack
->keystack_num_extended
);
1649 keysock_delpair_all(keysock_t
*ks
, mblk_t
*mp
, sadb_ext_t
*extv
[])
1651 int i
, start
, finish
;
1653 keysock_stack_t
*keystack
= ks
->keysock_keystack
;
1656 finish
= KEYSOCK_MAX_CONSUMERS
- 1;
1658 for (i
= start
; i
<= finish
; i
++) {
1659 if (keystack
->keystack_consumers
[i
] != NULL
) {
1662 keysock_error(ks
, mp
, ENOMEM
,
1663 SADB_X_DIAGNOSTIC_NONE
);
1666 keysock_passdown(ks
, mp1
, i
, extv
, B_FALSE
);
1672 * Handle PF_KEY messages.
1675 keysock_parse(queue_t
*q
, mblk_t
*mp
)
1678 sadb_ext_t
*extv
[SADB_EXT_MAX
+ 1];
1679 keysock_t
*ks
= (keysock_t
*)q
->q_ptr
;
1682 keysock_stack_t
*keystack
= ks
->keysock_keystack
;
1684 /* Make sure I'm a PF_KEY socket. (i.e. nothing's below me) */
1685 ASSERT(WR(q
)->q_next
== NULL
);
1687 samsg
= (sadb_msg_t
*)mp
->b_rptr
;
1688 ks2dbg(keystack
, ("Received possible PF_KEY message, type %d.\n",
1689 samsg
->sadb_msg_type
));
1691 msgsize
= SADB_64TO8(samsg
->sadb_msg_len
);
1693 if (msgdsize(mp
) != msgsize
) {
1695 * Message len incorrect w.r.t. actual size. Send an error
1696 * (EMSGSIZE). It may be necessary to massage things a
1697 * bit. For example, if the sadb_msg_type is hosed,
1698 * I need to set it to SADB_RESERVED to get delivery to
1699 * do the right thing. Then again, maybe just letting
1700 * the error delivery do the right thing.
1703 ("mblk (%lu) and base (%d) message sizes don't jibe.\n",
1704 msgdsize(mp
), msgsize
));
1705 keysock_error(ks
, mp
, EMSGSIZE
, SADB_X_DIAGNOSTIC_NONE
);
1709 if (msgsize
> (uint_t
)(mp
->b_wptr
- mp
->b_rptr
)) {
1710 /* Get all message into one mblk. */
1711 if (pullupmsg(mp
, -1) == 0) {
1713 * Something screwy happened.
1716 ("keysock_parse: pullupmsg() failed.\n"));
1719 samsg
= (sadb_msg_t
*)mp
->b_rptr
;
1723 switch (keysock_get_ext(extv
, samsg
, msgsize
, keystack
)) {
1725 /* Handle duplicate extension. */
1726 ks1dbg(keystack
, ("Got duplicate extension of type %d.\n",
1727 extv
[0]->sadb_ext_type
));
1728 keysock_error(ks
, mp
, EINVAL
,
1729 keysock_duplicate(extv
[0]->sadb_ext_type
));
1732 /* Handle unknown extension. */
1733 ks1dbg(keystack
, ("Got unknown extension of type %d.\n",
1734 extv
[0]->sadb_ext_type
));
1735 keysock_error(ks
, mp
, EINVAL
, SADB_X_DIAGNOSTIC_UNKNOWN_EXT
);
1740 ("Length %d on extension type %d overrun or 0.\n",
1741 extv
[0]->sadb_ext_len
, extv
[0]->sadb_ext_type
));
1742 keysock_error(ks
, mp
, EINVAL
, SADB_X_DIAGNOSTIC_BAD_EXTLEN
);
1745 /* Reality check failed. */
1747 ("Reality check failed on extension type %d.\n",
1748 extv
[0]->sadb_ext_type
));
1749 keysock_error(ks
, mp
, EINVAL
,
1750 keysock_malformed(extv
[0]->sadb_ext_type
));
1753 /* Default case is no errors. */
1757 switch (samsg
->sadb_msg_type
) {
1760 * There's a semantic weirdness in that a message OTHER than
1761 * the return REGISTER message may be passed up if I set the
1762 * registered bit BEFORE I pass it down.
1764 * SOOOO, I'll not twiddle any registered bits until I see
1765 * the upbound REGISTER (with a serial number in it).
1767 if (samsg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
) {
1768 /* Handle extended register here. */
1769 keysock_extended_register(ks
, mp
, extv
);
1771 } else if (ks
->keysock_flags
& KEYSOCK_EXTENDED
) {
1772 keysock_error(ks
, mp
, EBUSY
, 0);
1779 case SADB_X_UPDATEPAIR
:
1781 case SADB_X_DELPAIR
:
1784 * Pass down to appropriate consumer.
1786 if (samsg
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
)
1787 keysock_passdown(ks
, mp
, samsg
->sadb_msg_satype
, extv
,
1789 else keysock_error(ks
, mp
, EINVAL
,
1790 SADB_X_DIAGNOSTIC_SATYPE_NEEDED
);
1792 case SADB_X_DELPAIR_STATE
:
1793 if (samsg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
) {
1794 keysock_delpair_all(ks
, mp
, extv
);
1796 keysock_passdown(ks
, mp
, samsg
->sadb_msg_satype
, extv
,
1802 * If I _receive_ an acquire, this means I should spread it
1803 * out to registered sockets. Unless there's an errno...
1805 * Need ADDRESS, may have ID, SENS, and PROP, unless errno,
1806 * in which case there should be NO extensions.
1808 * Return to registered.
1810 if (samsg
->sadb_msg_errno
!= 0) {
1811 satype
= samsg
->sadb_msg_satype
;
1812 if (satype
== SADB_SATYPE_UNSPEC
) {
1813 if (!(ks
->keysock_flags
& KEYSOCK_EXTENDED
)) {
1814 keysock_error(ks
, mp
, EINVAL
,
1815 SADB_X_DIAGNOSTIC_SATYPE_NEEDED
);
1819 * Reassign satype based on the first
1820 * flags that KEYSOCK_SETREG says.
1822 while (satype
<= SADB_SATYPE_MAX
) {
1823 if (KEYSOCK_ISREG(ks
, satype
))
1827 if (satype
> SADB_SATYPE_MAX
) {
1828 keysock_error(ks
, mp
, EBUSY
, 0);
1832 keysock_passdown(ks
, mp
, satype
, extv
, B_FALSE
);
1834 if (samsg
->sadb_msg_satype
== SADB_SATYPE_UNSPEC
) {
1835 keysock_error(ks
, mp
, EINVAL
,
1836 SADB_X_DIAGNOSTIC_SATYPE_NEEDED
);
1838 keysock_passup(mp
, samsg
, 0, NULL
, B_FALSE
,
1845 * If someone sends this in, then send out to all senders.
1846 * (Save maybe ESP or AH, I have to be careful here.)
1848 * Need ADDRESS, may have ID and SENS.
1850 * XXX for now this is unsupported.
1857 * No extensions at all. Return to all listeners.
1859 * Question: Should I hold a lock here to prevent
1860 * additions/deletions while flushing?
1861 * Answer: No. (See keysock_passdown() for details.)
1863 if (extv
[0] != NULL
) {
1865 * FLUSH messages shouldn't have extensions.
1868 ks2dbg(keystack
, ("FLUSH message with extension.\n"));
1869 keysock_error(ks
, mp
, EINVAL
, SADB_X_DIAGNOSTIC_NO_EXT
);
1873 /* Passing down of DUMP/FLUSH messages are special. */
1874 qwriter(q
, mp
, keysock_do_flushdump
, PERIM_INNER
);
1876 case SADB_DUMP
: /* not used by normal applications */
1877 if ((extv
[0] != NULL
) &&
1879 (sizeof (sadb_msg_t
) + sizeof (sadb_x_edump_t
))) ||
1880 (extv
[SADB_X_EXT_EDUMP
] == NULL
))) {
1881 keysock_error(ks
, mp
, EINVAL
,
1882 SADB_X_DIAGNOSTIC_NO_EXT
);
1885 qwriter(q
, mp
, keysock_do_flushdump
, PERIM_INNER
);
1887 case SADB_X_PROMISC
:
1889 * Promiscuous processing message.
1891 if (samsg
->sadb_msg_satype
== 0)
1892 ks
->keysock_flags
&= ~KEYSOCK_PROMISC
;
1894 ks
->keysock_flags
|= KEYSOCK_PROMISC
;
1895 keysock_passup(mp
, samsg
, ks
->keysock_serial
, NULL
, B_FALSE
,
1898 case SADB_X_INVERSE_ACQUIRE
:
1899 keysock_inverse_acquire(mp
, samsg
, extv
, ks
);
1902 ks2dbg(keystack
, ("Got unknown message type %d.\n",
1903 samsg
->sadb_msg_type
));
1904 keysock_error(ks
, mp
, EINVAL
, SADB_X_DIAGNOSTIC_UNKNOWN_MSG
);
1908 /* As a placeholder... */
1909 ks0dbg(("keysock_parse(): Hit EOPNOTSUPP\n"));
1910 keysock_error(ks
, mp
, EOPNOTSUPP
, SADB_X_DIAGNOSTIC_NONE
);
1914 * wput routing for PF_KEY/keysock/whatever. Unlike the routing socket,
1915 * I don't convert to ioctl()'s for IP. I am the end-all driver as far
1916 * as PF_KEY sockets are concerned. I do some conversion, but not as much
1920 keysock_wput(queue_t
*q
, mblk_t
*mp
)
1922 uchar_t
*rptr
= mp
->b_rptr
;
1925 keysock_stack_t
*keystack
;
1927 if (WR(q
)->q_next
) {
1928 keysock_consumer_t
*kc
= (keysock_consumer_t
*)q
->q_ptr
;
1929 keystack
= kc
->kc_keystack
;
1931 ks3dbg(keystack
, ("In keysock_wput\n"));
1934 * We shouldn't get writes on a consumer instance.
1935 * But for now, just passthru.
1937 ks1dbg(keystack
, ("Huh? wput for an consumer instance (%d)?\n",
1942 ks
= (keysock_t
*)q
->q_ptr
;
1943 keystack
= ks
->keysock_keystack
;
1945 ks3dbg(keystack
, ("In keysock_wput\n"));
1947 switch (mp
->b_datap
->db_type
) {
1952 ks2dbg(keystack
, ("raw M_DATA in keysock.\n"));
1957 if ((mp
->b_wptr
- rptr
) >= sizeof (struct T_data_req
)) {
1958 if (((union T_primitives
*)rptr
)->type
== T_DATA_REQ
) {
1959 if ((mp1
= mp
->b_cont
) == NULL
) {
1960 /* No data after T_DATA_REQ. */
1962 ("No data after DATA_REQ.\n"));
1968 ks2dbg(keystack
, ("T_DATA_REQ\n"));
1969 break; /* Out of switch. */
1974 ks3dbg(keystack
, ("In default wput case (%d %d).\n",
1975 mp
->b_datap
->db_type
, ((union T_primitives
*)rptr
)->type
));
1976 keysock_wput_other(q
, mp
);
1980 /* I now have a PF_KEY message in an M_DATA block, pointed to by mp. */
1981 keysock_parse(q
, mp
);
1984 /* BELOW THIS LINE ARE ROUTINES INCLUDING AND RELATED TO keysock_rput(). */
1987 * Called upon receipt of a KEYSOCK_HELLO_ACK to set up the appropriate
1991 keysock_link_consumer(uint8_t satype
, keysock_consumer_t
*kc
)
1994 keysock_stack_t
*keystack
= kc
->kc_keystack
;
1996 mutex_enter(&keystack
->keystack_consumers_lock
);
1997 mutex_enter(&kc
->kc_lock
);
1998 if (keystack
->keystack_consumers
[satype
] != NULL
) {
2000 "Hmmmm, someone closed %d before the HELLO_ACK happened.\n",
2003 * Perhaps updating the new below-me consumer with what I have
2004 * so far would work too?
2006 mutex_exit(&kc
->kc_lock
);
2007 mutex_exit(&keystack
->keystack_consumers_lock
);
2009 /* Add new below-me consumer. */
2010 keystack
->keystack_consumers
[satype
] = kc
;
2013 kc
->kc_sa_type
= satype
;
2014 mutex_exit(&kc
->kc_lock
);
2015 mutex_exit(&keystack
->keystack_consumers_lock
);
2017 /* Scan the keysock list. */
2018 mutex_enter(&keystack
->keystack_list_lock
);
2019 for (ks
= keystack
->keystack_list
; ks
!= NULL
;
2020 ks
= ks
->keysock_next
) {
2021 if (KEYSOCK_ISREG(ks
, satype
)) {
2023 * XXX Perhaps send an SADB_REGISTER down on
2024 * the socket's behalf.
2027 ("Socket %u registered already for "
2028 "new consumer.\n", ks
->keysock_serial
));
2031 mutex_exit(&keystack
->keystack_list_lock
);
2036 * Generate a KEYSOCK_OUT_ERR message for my consumer.
2039 keysock_out_err(keysock_consumer_t
*kc
, int ks_errno
, mblk_t
*mp
)
2041 keysock_out_err_t
*kse
;
2043 keysock_stack_t
*keystack
= kc
->kc_keystack
;
2045 imp
= allocb(sizeof (ipsec_info_t
), BPRI_HI
);
2047 ks1dbg(keystack
, ("keysock_out_err: Can't alloc message.\n"));
2051 imp
->b_datap
->db_type
= M_CTL
;
2052 imp
->b_wptr
+= sizeof (ipsec_info_t
);
2054 kse
= (keysock_out_err_t
*)imp
->b_rptr
;
2056 kse
->ks_err_type
= KEYSOCK_OUT_ERR
;
2057 kse
->ks_err_len
= sizeof (*kse
);
2058 /* Is serial necessary? */
2059 kse
->ks_err_serial
= 0;
2060 kse
->ks_err_errno
= ks_errno
;
2063 * XXX What else do I need to do here w.r.t. information
2064 * to tell the consumer what caused this error?
2066 * I believe the answer is the PF_KEY ACQUIRE (or other) message
2067 * attached in mp, which is appended at the end. I believe the
2068 * db_ref won't matter here, because the PF_KEY message is only read
2069 * for KEYSOCK_OUT_ERR.
2072 putnext(kc
->kc_wq
, imp
);
2075 /* XXX this is a hack errno. */
2076 #define EIPSECNOSA 255
2079 * Route message (pointed by mp, header in samsg) toward appropriate
2080 * sockets. Assume the message's creator did its job correctly.
2082 * This should be a function that is followed by a return in its caller.
2083 * The compiler _should_ be able to use tail-call optimizations to make the
2084 * large ## of parameters not a huge deal.
2087 keysock_passup(mblk_t
*mp
, sadb_msg_t
*samsg
, minor_t serial
,
2088 keysock_consumer_t
*kc
, boolean_t persistent
, keysock_stack_t
*keystack
)
2091 uint8_t satype
= samsg
->sadb_msg_satype
;
2092 boolean_t toall
= B_FALSE
, allreg
= B_FALSE
, allereg
= B_FALSE
,
2095 int err
= EIPSECNOSA
;
2097 /* Convert mp, which is M_DATA, into an M_PROTO of type T_DATA_IND */
2098 mp1
= allocb(sizeof (struct T_data_req
), BPRI_HI
);
2103 mp1
->b_wptr
+= sizeof (struct T_data_req
);
2104 ((struct T_data_ind
*)mp1
->b_rptr
)->PRIM_type
= T_DATA_IND
;
2105 ((struct T_data_ind
*)mp1
->b_rptr
)->MORE_flag
= 0;
2106 mp1
->b_datap
->db_type
= M_PROTO
;
2110 switch (samsg
->sadb_msg_type
) {
2114 case SADB_X_UPDATEPAIR
:
2117 case SADB_X_DELPAIR
:
2120 * These are most likely replies. Don't worry about
2121 * KEYSOCK_OUT_ERR handling. Deliver to all sockets.
2124 ("Delivering normal message (%d) to all sockets.\n",
2125 samsg
->sadb_msg_type
));
2130 * REGISTERs come up for one of three reasons:
2132 * 1.) In response to a normal SADB_REGISTER
2133 * (samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
2135 * Deliver to normal SADB_REGISTERed sockets.
2136 * 2.) In response to an extended REGISTER
2137 * (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC)
2138 * Deliver to extended REGISTERed socket.
2139 * 3.) Spontaneous algorithm changes
2140 * (samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC &&
2142 * Deliver to REGISTERed sockets of all sorts.
2145 /* Here because of keysock_error() call. */
2146 ASSERT(samsg
->sadb_msg_errno
!= 0);
2147 break; /* Out of switch. */
2149 ks3dbg(keystack
, ("Delivering REGISTER.\n"));
2150 if (satype
== SADB_SATYPE_UNSPEC
) {
2151 /* REGISTER Reason #2 */
2154 * Rewhack SA type so PF_KEY socket holder knows what
2155 * consumer generated this algorithm list.
2157 satype
= kc
->kc_sa_type
;
2158 samsg
->sadb_msg_satype
= satype
;
2160 } else if (serial
== 0) {
2161 /* REGISTER Reason #3 */
2165 /* REGISTER Reason #1 */
2172 * ACQUIREs are either extended (sadb_msg_satype == 0) or
2173 * regular (sadb_msg_satype != 0). And we're guaranteed
2174 * that serial == 0 for an ACQUIRE.
2176 ks3dbg(keystack
, ("Delivering ACQUIRE.\n"));
2177 allereg
= (satype
== SADB_SATYPE_UNSPEC
);
2180 * Corner case - if we send a regular ACQUIRE and there's
2181 * extended ones registered, don't send an error down to
2182 * consumers if nobody's listening and prematurely destroy
2183 * their ACQUIRE record. This might be too hackish of a
2186 if (allreg
&& keystack
->keystack_num_extended
> 0)
2189 case SADB_X_PROMISC
:
2190 case SADB_X_INVERSE_ACQUIRE
:
2195 * Deliver to the sender and promiscuous only.
2197 ks3dbg(keystack
, ("Delivering sender/promisc only (%d).\n",
2198 samsg
->sadb_msg_type
));
2202 mutex_enter(&keystack
->keystack_list_lock
);
2203 for (ks
= keystack
->keystack_list
; ks
!= NULL
; ks
= ks
->keysock_next
) {
2204 /* Delivery loop. */
2207 * Check special keysock-setting cases (REGISTER replies)
2210 if (setalg
&& serial
== ks
->keysock_serial
) {
2212 ASSERT(kc
->kc_sa_type
== satype
);
2213 KEYSOCK_SETREG(ks
, satype
);
2217 * NOLOOP takes precedence over PROMISC. So if you've set
2218 * !SO_USELOOPBACK, don't expect to see any data...
2220 if (ks
->keysock_flags
& KEYSOCK_NOLOOP
)
2224 * Messages to all, or promiscuous sockets just GET the
2225 * message. Perform rules-type checking iff it's not for all
2226 * listeners or the socket is in promiscuous mode.
2228 * NOTE:Because of the (kc != NULL && ISREG()), make sure
2229 * extended ACQUIREs arrive off a consumer that is
2230 * part of the extended REGISTER set of consumers.
2232 if (serial
!= ks
->keysock_serial
&&
2234 !(ks
->keysock_flags
& KEYSOCK_PROMISC
) &&
2235 !((ks
->keysock_flags
& KEYSOCK_EXTENDED
) ?
2236 allereg
: allreg
&& kc
!= NULL
&&
2237 KEYSOCK_ISREG(ks
, kc
->kc_sa_type
)))
2243 "keysock_passup(): dupmsg() failed.\n"));
2250 * At this point, we can deliver or attempt to deliver
2251 * this message. We're free of obligation to report
2252 * no listening PF_KEY sockets. So set err to 0.
2257 * See if we canputnext(), as well as see if the message
2258 * needs to be queued if we can't.
2260 if (!canputnext(ks
->keysock_rq
)) {
2262 if (putq(ks
->keysock_rq
, mp1
) == 0) {
2264 "keysock_passup: putq failed.\n"));
2274 ("Putting to serial %d.\n", ks
->keysock_serial
));
2276 * Unlike the specific keysock instance case, this
2277 * will only hit for listeners, so we will only
2278 * putnext() if we can.
2280 putnext(ks
->keysock_rq
, mp1
);
2282 break; /* out of for loop. */
2284 mutex_exit(&keystack
->keystack_list_lock
);
2287 if ((err
!= 0) && (kc
!= NULL
)) {
2289 * Generate KEYSOCK_OUT_ERR for consumer.
2290 * Basically, I send this back if I have not been able to
2291 * transmit (for whatever reason)
2294 ("keysock_passup(): No registered of type %d.\n",
2297 if (mp
->b_datap
->db_type
== M_PROTO
) {
2303 * Do a copymsg() because people who get
2304 * KEYSOCK_OUT_ERR may alter the message contents.
2309 ("keysock_passup: copymsg() failed.\n"));
2313 keysock_out_err(kc
, err
, mp1
);
2318 * XXX Blank the message somehow. This is difficult because we don't
2319 * know at this point if the message has db_ref > 1, etc.
2321 * Optimally, keysock messages containing actual keying material would
2322 * be allocated with esballoc(), with a zeroing free function.
2329 * Keysock's read service procedure is there only for PF_KEY reply
2330 * messages that really need to reach the top.
2333 keysock_rsrv(queue_t
*q
)
2337 while ((mp
= getq(q
)) != NULL
) {
2338 if (canputnext(q
)) {
2341 (void) putbq(q
, mp
);
2348 * The read procedure should only be invoked by a keysock consumer, like
2349 * ESP, AH, etc. I should only see KEYSOCK_OUT and KEYSOCK_HELLO_ACK
2350 * messages on my read queues.
2353 keysock_rput(queue_t
*q
, mblk_t
*mp
)
2355 keysock_consumer_t
*kc
= (keysock_consumer_t
*)q
->q_ptr
;
2357 keysock_hello_ack_t
*ksa
;
2361 keysock_stack_t
*keystack
= kc
->kc_keystack
;
2363 /* Make sure I'm a consumer instance. (i.e. something's below me) */
2364 ASSERT(WR(q
)->q_next
!= NULL
);
2366 if (mp
->b_datap
->db_type
!= M_CTL
) {
2368 * Keysock should only see keysock consumer interface
2369 * messages (see ipsec_info.h) on its read procedure.
2370 * To be robust, however, putnext() up so the STREAM head can
2371 * deal with it appropriately.
2374 ("Hmmm, a non M_CTL (%d, 0x%x) on keysock_rput.\n",
2375 mp
->b_datap
->db_type
, mp
->b_datap
->db_type
));
2380 ii
= (ipsec_info_t
*)mp
->b_rptr
;
2382 switch (ii
->ipsec_info_type
) {
2385 * A consumer needs to pass a response message or an ACQUIRE
2386 * UP. I assume that the consumer has done the right
2387 * thing w.r.t. message creation, etc.
2389 serial
= ((keysock_out_t
*)mp
->b_rptr
)->ks_out_serial
;
2390 mp1
= mp
->b_cont
; /* Get M_DATA portion. */
2392 samsg
= (sadb_msg_t
*)mp1
->b_rptr
;
2393 if (samsg
->sadb_msg_type
== SADB_FLUSH
||
2394 (samsg
->sadb_msg_type
== SADB_DUMP
&&
2395 samsg
->sadb_msg_len
== SADB_8TO64(sizeof (*samsg
)))) {
2397 * If I'm an end-of-FLUSH or an end-of-DUMP marker...
2399 ASSERT(keystack
->keystack_flushdump
!= 0);
2400 /* Am I flushing? */
2402 mutex_enter(&kc
->kc_lock
);
2403 kc
->kc_flags
&= ~KC_FLUSHING
;
2404 mutex_exit(&kc
->kc_lock
);
2406 if (samsg
->sadb_msg_errno
!= 0)
2407 keystack
->keystack_flushdump_errno
=
2408 samsg
->sadb_msg_errno
;
2411 * Lower the atomic "flushing" count. If it's
2412 * the last one, send up the end-of-{FLUSH,DUMP} to
2413 * the appropriate PF_KEY socket.
2415 if (atomic_dec_32_nv(&keystack
->keystack_flushdump
) !=
2418 ("One flush/dump message back from %d,"
2419 " more to go.\n", samsg
->sadb_msg_satype
));
2424 samsg
->sadb_msg_errno
=
2425 (uint8_t)keystack
->keystack_flushdump_errno
;
2426 if (samsg
->sadb_msg_type
== SADB_DUMP
) {
2427 samsg
->sadb_msg_seq
= 0;
2430 keysock_passup(mp1
, samsg
, serial
, kc
,
2431 (samsg
->sadb_msg_type
== SADB_DUMP
), keystack
);
2433 case KEYSOCK_HELLO_ACK
:
2434 /* Aha, now we can link in the consumer! */
2435 ksa
= (keysock_hello_ack_t
*)ii
;
2436 keysock_link_consumer(ksa
->ks_hello_satype
, kc
);
2440 ks1dbg(keystack
, ("Hmmm, an IPsec info I'm not used to, 0x%x\n",
2441 ii
->ipsec_info_type
));
2447 * So we can avoid external linking problems....
2450 keysock_extended_reg(netstack_t
*ns
)
2452 keysock_stack_t
*keystack
= ns
->netstack_keysock
;
2454 return (keystack
->keystack_num_extended
!= 0);
2458 keysock_next_seq(netstack_t
*ns
)
2460 keysock_stack_t
*keystack
= ns
->netstack_keysock
;
2462 return (atomic_dec_32_nv(&keystack
->keystack_acquire_seq
));