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 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright (c) 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2017 Joyent, Inc.
28 #include <sys/types.h>
29 #include <sys/stream.h>
30 #include <sys/stropts.h>
31 #include <sys/errno.h>
32 #include <sys/strlog.h>
33 #include <sys/tihdr.h>
34 #include <sys/socket.h>
36 #include <sys/sunddi.h>
37 #include <sys/mkdev.h>
40 #include <sys/sysmacros.h>
41 #include <sys/cmn_err.h>
42 #include <sys/vtrace.h>
43 #include <sys/debug.h>
44 #include <sys/atomic.h>
45 #include <sys/strsun.h>
46 #include <sys/random.h>
47 #include <netinet/in.h>
49 #include <netinet/ip6.h>
50 #include <netinet/icmp6.h>
51 #include <net/pfkeyv2.h>
52 #include <net/pfpolicy.h>
54 #include <inet/common.h>
59 #include <inet/ip_if.h>
60 #include <inet/ip_ndp.h>
61 #include <inet/ipsec_info.h>
62 #include <inet/ipsec_impl.h>
63 #include <inet/sadb.h>
64 #include <inet/ipsecah.h>
65 #include <inet/ipsec_impl.h>
66 #include <inet/ipdrop.h>
67 #include <sys/taskq.h>
68 #include <sys/policy.h>
69 #include <sys/strsun.h>
71 #include <sys/crypto/common.h>
72 #include <sys/crypto/api.h>
73 #include <sys/kstat.h>
74 #include <sys/strsubr.h>
77 * Table of ND variables supported by ipsecah. These are loaded into
78 * ipsecah_g_nd in ipsecah_init_nd.
79 * All of these are alterable, within the min/max values given, at run time.
81 static ipsecahparam_t lcl_param_arr
[] = {
82 /* min max value name */
83 { 0, 3, 0, "ipsecah_debug"},
84 { 125, 32000, SADB_AGE_INTERVAL_DEFAULT
, "ipsecah_age_interval"},
85 { 1, 10, 1, "ipsecah_reap_delay"},
86 { 1, SADB_MAX_REPLAY
, 64, "ipsecah_replay_size"},
87 { 1, 300, 15, "ipsecah_acquire_timeout"},
88 { 1, 1800, 90, "ipsecah_larval_timeout"},
89 /* Default lifetime values for ACQUIRE messages. */
90 { 0, 0xffffffffU
, 0, "ipsecah_default_soft_bytes"},
91 { 0, 0xffffffffU
, 0, "ipsecah_default_hard_bytes"},
92 { 0, 0xffffffffU
, 24000, "ipsecah_default_soft_addtime"},
93 { 0, 0xffffffffU
, 28800, "ipsecah_default_hard_addtime"},
94 { 0, 0xffffffffU
, 0, "ipsecah_default_soft_usetime"},
95 { 0, 0xffffffffU
, 0, "ipsecah_default_hard_usetime"},
96 { 0, 1, 0, "ipsecah_log_unknown_spi"},
99 #define ah0dbg(a) printf a
100 /* NOTE: != 0 instead of > 0 so lint doesn't complain. */
101 #define ah1dbg(ahstack, a) if (ahstack->ipsecah_debug != 0) printf a
102 #define ah2dbg(ahstack, a) if (ahstack->ipsecah_debug > 1) printf a
103 #define ah3dbg(ahstack, a) if (ahstack->ipsecah_debug > 2) printf a
106 * XXX This is broken. Padding should be determined dynamically
107 * depending on the ICV size and IP version number so that the
108 * total AH header size is a multiple of 32 bits or 64 bits
109 * for V4 and V6 respectively. For 96bit ICVs we have no problems.
110 * Anything different from that, we need to fix our code.
112 #define IPV4_PADDING_ALIGN 0x04 /* Multiple of 32 bits */
113 #define IPV6_PADDING_ALIGN 0x04 /* Multiple of 32 bits */
116 * Helper macro. Avoids a call to msgdsize if there is only one
119 #define AH_MSGSIZE(mp) ((mp)->b_cont != NULL ? msgdsize(mp) : MBLKL(mp))
122 static mblk_t
*ah_auth_out_done(mblk_t
*, ip_xmit_attr_t
*, ipsec_crypto_t
*);
123 static mblk_t
*ah_auth_in_done(mblk_t
*, ip_recv_attr_t
*, ipsec_crypto_t
*);
124 static mblk_t
*ah_process_ip_options_v4(mblk_t
*, ipsa_t
*, int *, uint_t
,
125 boolean_t
, ipsecah_stack_t
*);
126 static mblk_t
*ah_process_ip_options_v6(mblk_t
*, ipsa_t
*, int *, uint_t
,
127 boolean_t
, ipsecah_stack_t
*);
128 static void ah_getspi(mblk_t
*, keysock_in_t
*, ipsecah_stack_t
*);
129 static void ah_inbound_restart(mblk_t
*, ip_recv_attr_t
*);
131 static mblk_t
*ah_outbound(mblk_t
*, ip_xmit_attr_t
*);
132 static void ah_outbound_finish(mblk_t
*, ip_xmit_attr_t
*);
134 static int ipsecah_open(queue_t
*, dev_t
*, int, int, cred_t
*);
135 static int ipsecah_close(queue_t
*);
136 static void ipsecah_wput(queue_t
*, mblk_t
*);
137 static boolean_t
ah_register_out(uint32_t, uint32_t, uint_t
, ipsecah_stack_t
*,
139 static void *ipsecah_stack_init(netstackid_t stackid
, netstack_t
*ns
);
140 static void ipsecah_stack_fini(netstackid_t stackid
, void *arg
);
142 /* Setable in /etc/system */
143 uint32_t ah_hash_size
= IPSEC_DEFAULT_HASH_SIZE
;
145 static taskq_t
*ah_taskq
;
147 static struct module_info info
= {
148 5136, "ipsecah", 0, INFPSZ
, 65536, 1024
151 static struct qinit rinit
= {
152 (pfi_t
)putnext
, NULL
, ipsecah_open
, ipsecah_close
, NULL
, &info
,
156 static struct qinit winit
= {
157 (pfi_t
)ipsecah_wput
, NULL
, ipsecah_open
, ipsecah_close
, NULL
, &info
,
161 struct streamtab ipsecahinfo
= {
162 &rinit
, &winit
, NULL
, NULL
165 static int ah_kstat_update(kstat_t
*, int);
167 uint64_t ipsacq_maxpackets
= IPSACQ_MAXPACKETS
;
170 ah_kstat_init(ipsecah_stack_t
*ahstack
, netstackid_t stackid
)
172 ipsec_stack_t
*ipss
= ahstack
->ipsecah_netstack
->netstack_ipsec
;
174 ahstack
->ah_ksp
= kstat_create_netstack("ipsecah", 0, "ah_stat", "net",
175 KSTAT_TYPE_NAMED
, sizeof (ah_kstats_t
) / sizeof (kstat_named_t
), 0,
178 if (ahstack
->ah_ksp
== NULL
|| ahstack
->ah_ksp
->ks_data
== NULL
)
181 ahstack
->ah_kstats
= ahstack
->ah_ksp
->ks_data
;
183 ahstack
->ah_ksp
->ks_update
= ah_kstat_update
;
184 ahstack
->ah_ksp
->ks_private
= (void *)(uintptr_t)stackid
;
186 #define K64 KSTAT_DATA_UINT64
187 #define KI(x) kstat_named_init(&(ahstack->ah_kstats->ah_stat_##x), #x, K64)
193 KI(replay_early_failures
);
196 KI(acquire_requests
);
206 kstat_install(ahstack
->ah_ksp
);
207 IP_ACQUIRE_STAT(ipss
, maxpackets
, ipsacq_maxpackets
);
212 ah_kstat_update(kstat_t
*kp
, int rw
)
215 netstackid_t stackid
= (netstackid_t
)(uintptr_t)kp
->ks_private
;
219 if ((kp
== NULL
) || (kp
->ks_data
== NULL
))
222 if (rw
== KSTAT_WRITE
)
225 ns
= netstack_find_by_stackid(stackid
);
228 ipss
= ns
->netstack_ipsec
;
233 ekp
= (ah_kstats_t
*)kp
->ks_data
;
235 rw_enter(&ipss
->ipsec_alg_lock
, RW_READER
);
236 ekp
->ah_stat_num_aalgs
.value
.ui64
= ipss
->ipsec_nalgs
[IPSEC_ALG_AUTH
];
237 rw_exit(&ipss
->ipsec_alg_lock
);
244 * Don't have to lock ipsec_age_interval, as only one thread will access it at
245 * a time, because I control the one function that does a qtimeout() on
251 ipsecah_stack_t
*ahstack
= (ipsecah_stack_t
*)arg
;
252 netstack_t
*ns
= ahstack
->ipsecah_netstack
;
253 hrtime_t begin
= gethrtime();
255 sadb_ager(&ahstack
->ah_sadb
.s_v4
, ahstack
->ah_pfkey_q
,
256 ahstack
->ipsecah_reap_delay
, ns
);
257 sadb_ager(&ahstack
->ah_sadb
.s_v6
, ahstack
->ah_pfkey_q
,
258 ahstack
->ipsecah_reap_delay
, ns
);
260 ahstack
->ah_event
= sadb_retimeout(begin
, ahstack
->ah_pfkey_q
,
262 &ahstack
->ipsecah_age_interval
, ahstack
->ipsecah_age_int_max
,
267 * Get an AH NDD parameter.
277 ipsecahparam_t
*ipsecahpa
= (ipsecahparam_t
*)cp
;
279 ipsecah_stack_t
*ahstack
= (ipsecah_stack_t
*)q
->q_ptr
;
281 mutex_enter(&ahstack
->ipsecah_param_lock
);
282 value
= ipsecahpa
->ipsecah_param_value
;
283 mutex_exit(&ahstack
->ipsecah_param_lock
);
285 (void) mi_mpprintf(mp
, "%u", value
);
290 * This routine sets an NDD variable in a ipsecahparam_t structure.
302 ipsecahparam_t
*ipsecahpa
= (ipsecahparam_t
*)cp
;
303 ipsecah_stack_t
*ahstack
= (ipsecah_stack_t
*)q
->q_ptr
;
306 * Fail the request if the new value does not lie within the
309 if (ddi_strtoul(value
, NULL
, 10, &new_value
) != 0 ||
310 new_value
< ipsecahpa
->ipsecah_param_min
||
311 new_value
> ipsecahpa
->ipsecah_param_max
) {
315 /* Set the new value */
316 mutex_enter(&ahstack
->ipsecah_param_lock
);
317 ipsecahpa
->ipsecah_param_value
= new_value
;
318 mutex_exit(&ahstack
->ipsecah_param_lock
);
323 * Using lifetime NDD variables, fill in an extended combination's
324 * lifetime information.
327 ipsecah_fill_defs(sadb_x_ecomb_t
*ecomb
, netstack_t
*ns
)
329 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
331 ecomb
->sadb_x_ecomb_soft_bytes
= ahstack
->ipsecah_default_soft_bytes
;
332 ecomb
->sadb_x_ecomb_hard_bytes
= ahstack
->ipsecah_default_hard_bytes
;
333 ecomb
->sadb_x_ecomb_soft_addtime
=
334 ahstack
->ipsecah_default_soft_addtime
;
335 ecomb
->sadb_x_ecomb_hard_addtime
=
336 ahstack
->ipsecah_default_hard_addtime
;
337 ecomb
->sadb_x_ecomb_soft_usetime
=
338 ahstack
->ipsecah_default_soft_usetime
;
339 ecomb
->sadb_x_ecomb_hard_usetime
=
340 ahstack
->ipsecah_default_hard_usetime
;
344 * Initialize things for AH at module load time.
347 ipsecah_ddi_init(void)
349 ah_taskq
= taskq_create("ah_taskq", 1, minclsyspri
,
350 IPSEC_TASKQ_MIN
, IPSEC_TASKQ_MAX
, 0);
353 * We want to be informed each time a stack is created or
354 * destroyed in the kernel, so we can maintain the
355 * set of ipsecah_stack_t's.
357 netstack_register(NS_IPSECAH
, ipsecah_stack_init
, NULL
,
364 * Walk through the param array specified registering each element with the
365 * named dispatch handler.
368 ipsecah_param_register(IDP
*ndp
, ipsecahparam_t
*ahp
, int cnt
)
370 for (; cnt
-- > 0; ahp
++) {
371 if (ahp
->ipsecah_param_name
!= NULL
&&
372 ahp
->ipsecah_param_name
[0]) {
374 ahp
->ipsecah_param_name
,
375 ipsecah_param_get
, ipsecah_param_set
,
386 * Initialize things for AH for each stack instance
389 ipsecah_stack_init(netstackid_t stackid
, netstack_t
*ns
)
391 ipsecah_stack_t
*ahstack
;
394 ahstack
= (ipsecah_stack_t
*)kmem_zalloc(sizeof (*ahstack
), KM_SLEEP
);
395 ahstack
->ipsecah_netstack
= ns
;
397 ahp
= (ipsecahparam_t
*)kmem_alloc(sizeof (lcl_param_arr
), KM_SLEEP
);
398 ahstack
->ipsecah_params
= ahp
;
399 bcopy(lcl_param_arr
, ahp
, sizeof (lcl_param_arr
));
401 (void) ipsecah_param_register(&ahstack
->ipsecah_g_nd
, ahp
,
402 A_CNT(lcl_param_arr
));
404 (void) ah_kstat_init(ahstack
, stackid
);
406 ahstack
->ah_sadb
.s_acquire_timeout
= &ahstack
->ipsecah_acquire_timeout
;
407 sadbp_init("AH", &ahstack
->ah_sadb
, SADB_SATYPE_AH
, ah_hash_size
,
408 ahstack
->ipsecah_netstack
);
410 mutex_init(&ahstack
->ipsecah_param_lock
, NULL
, MUTEX_DEFAULT
, 0);
412 ip_drop_register(&ahstack
->ah_dropper
, "IPsec AH");
417 * Destroy things for AH at module unload time.
420 ipsecah_ddi_destroy(void)
422 netstack_unregister(NS_IPSECAH
);
423 taskq_destroy(ah_taskq
);
427 * Destroy things for AH for one stack... Never called?
430 ipsecah_stack_fini(netstackid_t stackid
, void *arg
)
432 ipsecah_stack_t
*ahstack
= (ipsecah_stack_t
*)arg
;
434 if (ahstack
->ah_pfkey_q
!= NULL
) {
435 (void) quntimeout(ahstack
->ah_pfkey_q
, ahstack
->ah_event
);
437 ahstack
->ah_sadb
.s_acquire_timeout
= NULL
;
438 sadbp_destroy(&ahstack
->ah_sadb
, ahstack
->ipsecah_netstack
);
439 ip_drop_unregister(&ahstack
->ah_dropper
);
440 mutex_destroy(&ahstack
->ipsecah_param_lock
);
441 nd_free(&ahstack
->ipsecah_g_nd
);
443 kmem_free(ahstack
->ipsecah_params
, sizeof (lcl_param_arr
));
444 ahstack
->ipsecah_params
= NULL
;
445 kstat_delete_netstack(ahstack
->ah_ksp
, stackid
);
446 ahstack
->ah_ksp
= NULL
;
447 ahstack
->ah_kstats
= NULL
;
449 kmem_free(ahstack
, sizeof (*ahstack
));
453 * AH module open routine, which is here for keysock plumbing.
454 * Keysock is pushed over {AH,ESP} which is an artifact from the Bad Old
455 * Days of export control, and fears that ESP would not be allowed
456 * to be shipped at all by default. Eventually, keysock should
457 * either access AH and ESP via modstubs or krtld dependencies, or
458 * perhaps be folded in with AH and ESP into a single IPsec/netsec
459 * module ("netsec" if PF_KEY provides more than AH/ESP keying tables).
463 ipsecah_open(queue_t
*q
, dev_t
*devp
, int flag
, int sflag
, cred_t
*credp
)
466 ipsecah_stack_t
*ahstack
;
468 if (secpolicy_ip_config(credp
, B_FALSE
) != 0)
471 if (q
->q_ptr
!= NULL
)
472 return (0); /* Re-open of an already open instance. */
474 if (sflag
!= MODOPEN
)
477 ns
= netstack_find_by_cred(credp
);
479 ahstack
= ns
->netstack_ipsecah
;
480 ASSERT(ahstack
!= NULL
);
483 WR(q
)->q_ptr
= q
->q_ptr
;
490 * AH module close routine.
493 ipsecah_close(queue_t
*q
)
495 ipsecah_stack_t
*ahstack
= (ipsecah_stack_t
*)q
->q_ptr
;
498 * Clean up q_ptr, if needed.
502 /* Keysock queue check is safe, because of OCEXCL perimeter. */
504 if (q
== ahstack
->ah_pfkey_q
) {
506 ("ipsecah_close: Ummm... keysock is closing AH.\n"));
507 ahstack
->ah_pfkey_q
= NULL
;
508 /* Detach qtimeouts. */
509 (void) quntimeout(q
, ahstack
->ah_event
);
512 netstack_rele(ahstack
->ipsecah_netstack
);
517 * Construct an SADB_REGISTER message with the current algorithms.
520 ah_register_out(uint32_t sequence
, uint32_t pid
, uint_t serial
,
521 ipsecah_stack_t
*ahstack
, cred_t
*cr
)
524 boolean_t rc
= B_TRUE
;
526 sadb_supported_t
*sasupp
;
528 uint_t allocsize
= sizeof (*samsg
);
529 uint_t i
, numalgs_snap
;
530 ipsec_alginfo_t
**authalgs
;
532 ipsec_stack_t
*ipss
= ahstack
->ipsecah_netstack
->netstack_ipsec
;
535 /* Allocate the KEYSOCK_OUT. */
536 mp
= sadb_keysock_out(serial
);
538 ah0dbg(("ah_register_out: couldn't allocate mblk.\n"));
543 * Allocate the PF_KEY message that follows KEYSOCK_OUT.
544 * The alg reader lock needs to be held while allocating
545 * the variable part (i.e. the algorithms) of the message.
548 rw_enter(&ipss
->ipsec_alg_lock
, RW_READER
);
551 * Return only valid algorithms, so the number of algorithms
552 * to send up may be less than the number of algorithm entries
555 authalgs
= ipss
->ipsec_alglists
[IPSEC_ALG_AUTH
];
556 for (num_aalgs
= 0, i
= 0; i
< IPSEC_MAX_ALGS
; i
++)
557 if (authalgs
[i
] != NULL
&& ALG_VALID(authalgs
[i
]))
561 * Fill SADB_REGISTER message's algorithm descriptors. Hold
562 * down the lock while filling it.
564 if (num_aalgs
!= 0) {
565 allocsize
+= (num_aalgs
* sizeof (*saalg
));
566 allocsize
+= sizeof (*sasupp
);
568 mp
->b_cont
= allocb(allocsize
, BPRI_HI
);
569 if (mp
->b_cont
== NULL
) {
570 rw_exit(&ipss
->ipsec_alg_lock
);
575 mp
->b_cont
->b_wptr
+= allocsize
;
576 nextext
= (sadb_ext_t
*)(mp
->b_cont
->b_rptr
+ sizeof (*samsg
));
578 if (num_aalgs
!= 0) {
580 saalg
= (sadb_alg_t
*)(((uint8_t *)nextext
) + sizeof (*sasupp
));
581 ASSERT(((ulong_t
)saalg
& 0x7) == 0);
585 ((i
< IPSEC_MAX_ALGS
) && (numalgs_snap
< num_aalgs
));
587 if (authalgs
[i
] == NULL
|| !ALG_VALID(authalgs
[i
]))
590 saalg
->sadb_alg_id
= authalgs
[i
]->alg_id
;
591 saalg
->sadb_alg_ivlen
= 0;
592 saalg
->sadb_alg_minbits
= authalgs
[i
]->alg_ef_minbits
;
593 saalg
->sadb_alg_maxbits
= authalgs
[i
]->alg_ef_maxbits
;
594 saalg
->sadb_x_alg_increment
=
595 authalgs
[i
]->alg_increment
;
596 /* For now, salt is meaningless in AH. */
597 ASSERT(authalgs
[i
]->alg_saltlen
== 0);
598 saalg
->sadb_x_alg_saltbits
=
599 SADB_8TO1(authalgs
[i
]->alg_saltlen
);
603 ASSERT(numalgs_snap
== num_aalgs
);
606 * Reality check to make sure I snagged all of the
609 for (; i
< IPSEC_MAX_ALGS
; i
++)
610 if (authalgs
[i
] != NULL
&& ALG_VALID(authalgs
[i
]))
612 "ah_register_out()! Missed #%d.\n", i
);
614 nextext
= (sadb_ext_t
*)saalg
;
617 rw_exit(&ipss
->ipsec_alg_lock
);
619 /* Now fill the restof the SADB_REGISTER message. */
621 samsg
= (sadb_msg_t
*)mp
->b_cont
->b_rptr
;
622 samsg
->sadb_msg_version
= PF_KEY_V2
;
623 samsg
->sadb_msg_type
= SADB_REGISTER
;
624 samsg
->sadb_msg_errno
= 0;
625 samsg
->sadb_msg_satype
= SADB_SATYPE_AH
;
626 samsg
->sadb_msg_len
= SADB_8TO64(allocsize
);
627 samsg
->sadb_msg_reserved
= 0;
629 * Assume caller has sufficient sequence/pid number info. If it's one
630 * from me over a new alg., I could give two hoots about sequence.
632 samsg
->sadb_msg_seq
= sequence
;
633 samsg
->sadb_msg_pid
= pid
;
635 if (num_aalgs
!= 0) {
636 sasupp
= (sadb_supported_t
*)(samsg
+ 1);
637 sasupp
->sadb_supported_len
= SADB_8TO64(
638 sizeof (*sasupp
) + sizeof (*saalg
) * num_aalgs
);
639 sasupp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
640 sasupp
->sadb_supported_reserved
= 0;
643 if (ahstack
->ah_pfkey_q
!= NULL
)
644 putnext(ahstack
->ah_pfkey_q
, mp
);
654 * Invoked when the algorithm table changes. Causes SADB_REGISTER
655 * messages continaining the current list of algorithms to be
656 * sent up to the AH listeners.
659 ipsecah_algs_changed(netstack_t
*ns
)
661 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
664 * Time to send a PF_KEY SADB_REGISTER message to AH listeners
665 * everywhere. (The function itself checks for NULL ah_pfkey_q.)
667 (void) ah_register_out(0, 0, 0, ahstack
, NULL
);
671 * Stub function that taskq_dispatch() invokes to take the mblk (in arg)
672 * and send it into AH and IP again.
675 inbound_task(void *arg
)
677 mblk_t
*mp
= (mblk_t
*)arg
;
682 mp
= async_mp
->b_cont
;
683 async_mp
->b_cont
= NULL
;
684 if (!ip_recv_attr_from_mblk(async_mp
, &iras
)) {
685 /* The ill or ip_stack_t disappeared on us */
686 ip_drop_input("ip_recv_attr_from_mblk", mp
, NULL
);
691 ah_inbound_restart(mp
, &iras
);
693 ira_cleanup(&iras
, B_TRUE
);
697 * Restart ESP after the SA has been added.
700 ah_inbound_restart(mblk_t
*mp
, ip_recv_attr_t
*ira
)
704 ipsecah_stack_t
*ahstack
;
706 ns
= ira
->ira_ill
->ill_ipst
->ips_netstack
;
707 ahstack
= ns
->netstack_ipsecah
;
709 ASSERT(ahstack
!= NULL
);
710 mp
= ipsec_inbound_ah_sa(mp
, ira
, &ah
);
715 ASSERT(ira
->ira_flags
& IRAF_IPSEC_SECURE
);
716 ASSERT(ira
->ira_ipsec_ah_sa
!= NULL
);
718 mp
= ira
->ira_ipsec_ah_sa
->ipsa_input_func(mp
, ah
, ira
);
721 * Either it failed or is pending. In the former case
722 * ipIfStatsInDiscards was increased.
726 ip_input_post_ipsec(mp
, ira
);
730 * Now that weak-key passed, actually ADD the security association, and
731 * send back a reply ADD message.
734 ah_add_sa_finish(mblk_t
*mp
, sadb_msg_t
*samsg
, keysock_in_t
*ksi
,
735 int *diagnostic
, ipsecah_stack_t
*ahstack
)
737 isaf_t
*primary
= NULL
, *secondary
;
738 boolean_t clone
= B_FALSE
, is_inbound
= B_FALSE
;
739 sadb_sa_t
*assoc
= (sadb_sa_t
*)ksi
->ks_in_extv
[SADB_EXT_SA
];
743 mblk_t
*acq_msgs
= NULL
;
748 netstack_t
*ns
= ahstack
->ipsecah_netstack
;
749 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
752 * Locate the appropriate table(s).
755 sq
.spp
= &ahstack
->ah_sadb
;
756 error
= sadb_form_query(ksi
, IPSA_Q_SA
|IPSA_Q_DST
,
757 IPSA_Q_SA
|IPSA_Q_DST
|IPSA_Q_INBOUND
|IPSA_Q_OUTBOUND
,
763 * Use the direction flags provided by the KMD to determine
764 * if the inbound or outbound table should be the primary
765 * for this SA. If these flags were absent then make this
766 * decision based on the addresses.
768 if (assoc
->sadb_sa_flags
& IPSA_F_INBOUND
) {
769 primary
= sq
.inbound
;
770 secondary
= sq
.outbound
;
772 if (assoc
->sadb_sa_flags
& IPSA_F_OUTBOUND
)
775 if (assoc
->sadb_sa_flags
& IPSA_F_OUTBOUND
) {
776 primary
= sq
.outbound
;
777 secondary
= sq
.inbound
;
780 if (primary
== NULL
) {
782 * The KMD did not set a direction flag, determine which
783 * table to insert the SA into based on addresses.
785 switch (ksi
->ks_in_dsttype
) {
786 case KS_IN_ADDR_MBCAST
:
787 clone
= B_TRUE
; /* All mcast SAs can be bidirectional */
788 assoc
->sadb_sa_flags
|= IPSA_F_OUTBOUND
;
791 * If the source address is either one of mine, or unspecified
792 * (which is best summed up by saying "not 'not mine'"),
793 * then the association is potentially bi-directional,
794 * in that it can be used for inbound traffic and outbound
795 * traffic. The best example of such and SA is a multicast
796 * SA (which allows me to receive the outbound traffic).
799 assoc
->sadb_sa_flags
|= IPSA_F_INBOUND
;
800 primary
= sq
.inbound
;
801 secondary
= sq
.outbound
;
802 if (ksi
->ks_in_srctype
!= KS_IN_ADDR_NOTME
)
808 * If the source address literally not mine (either
809 * unspecified or not mine), then this SA may have an
810 * address that WILL be mine after some configuration.
811 * We pay the price for this by making it a bi-directional
814 case KS_IN_ADDR_NOTME
:
815 assoc
->sadb_sa_flags
|= IPSA_F_OUTBOUND
;
816 primary
= sq
.outbound
;
817 secondary
= sq
.inbound
;
818 if (ksi
->ks_in_srctype
!= KS_IN_ADDR_ME
) {
819 assoc
->sadb_sa_flags
|= IPSA_F_INBOUND
;
824 *diagnostic
= SADB_X_DIAGNOSTIC_BAD_DST
;
830 * Find a ACQUIRE list entry if possible. If we've added an SA that
831 * suits the needs of an ACQUIRE list entry, we can eliminate the
832 * ACQUIRE list entry and transmit the enqueued packets. Use the
833 * high-bit of the sequence number to queue it. Key off destination
834 * addr, and change acqrec's state.
837 if (samsg
->sadb_msg_seq
& IACQF_LOWEST_SEQ
) {
838 acq_bucket
= &(sq
.sp
->sdb_acq
[sq
.outhash
]);
839 mutex_enter(&acq_bucket
->iacqf_lock
);
840 for (acqrec
= acq_bucket
->iacqf_ipsacq
; acqrec
!= NULL
;
841 acqrec
= acqrec
->ipsacq_next
) {
842 mutex_enter(&acqrec
->ipsacq_lock
);
844 * Q: I only check sequence. Should I check dst?
845 * A: Yes, check dest because those are the packets
846 * that are queued up.
848 if (acqrec
->ipsacq_seq
== samsg
->sadb_msg_seq
&&
849 IPSA_ARE_ADDR_EQUAL(sq
.dstaddr
,
850 acqrec
->ipsacq_dstaddr
, acqrec
->ipsacq_addrfam
))
852 mutex_exit(&acqrec
->ipsacq_lock
);
854 if (acqrec
!= NULL
) {
856 * AHA! I found an ACQUIRE record for this SA.
857 * Grab the msg list, and free the acquire record.
858 * I already am holding the lock for this record,
859 * so all I have to do is free it.
861 acq_msgs
= acqrec
->ipsacq_mp
;
862 acqrec
->ipsacq_mp
= NULL
;
863 mutex_exit(&acqrec
->ipsacq_lock
);
864 sadb_destroy_acquire(acqrec
, ns
);
866 mutex_exit(&acq_bucket
->iacqf_lock
);
870 * Find PF_KEY message, and see if I'm an update. If so, find entry
871 * in larval list (if there).
876 if (samsg
->sadb_msg_type
== SADB_UPDATE
) {
877 mutex_enter(&sq
.inbound
->isaf_lock
);
878 larval
= ipsec_getassocbyspi(sq
.inbound
, sq
.assoc
->sadb_sa_spi
,
879 ALL_ZEROES_PTR
, sq
.dstaddr
, sq
.dst
->sin_family
);
880 mutex_exit(&sq
.inbound
->isaf_lock
);
882 if ((larval
== NULL
) ||
883 (larval
->ipsa_state
!= IPSA_STATE_LARVAL
)) {
884 *diagnostic
= SADB_X_DIAGNOSTIC_SA_NOTFOUND
;
885 if (larval
!= NULL
) {
886 IPSA_REFRELE(larval
);
888 ah0dbg(("Larval update, but larval disappeared.\n"));
890 } /* Else sadb_common_add unlinks it for me! */
893 if (larval
!= NULL
) {
895 * Hold again, because sadb_common_add() consumes a reference,
896 * and we don't want to clear_lpkt() without a reference.
898 IPSA_REFHOLD(larval
);
901 rc
= sadb_common_add(ahstack
->ah_pfkey_q
, mp
,
902 samsg
, ksi
, primary
, secondary
, larval
, clone
, is_inbound
,
903 diagnostic
, ns
, &ahstack
->ah_sadb
);
905 if (larval
!= NULL
) {
907 lpkt
= sadb_clear_lpkt(larval
);
909 rc
= !taskq_dispatch(ah_taskq
, inbound_task
,
913 IPSA_REFRELE(larval
);
917 * How much more stack will I create with all of these
918 * ah_outbound_*() calls?
921 /* Handle the packets queued waiting for the SA */
922 while (acq_msgs
!= NULL
) {
929 acq_msgs
= acq_msgs
->b_next
;
930 asyncmp
->b_next
= NULL
;
933 * Extract the ip_xmit_attr_t from the first mblk.
934 * Verifies that the netstack and ill is still around; could
935 * have vanished while iked was doing its work.
936 * On succesful return we have a nce_t and the ill/ipst can't
937 * disappear until we do the nce_refrele in ixa_cleanup.
939 data_mp
= asyncmp
->b_cont
;
940 asyncmp
->b_cont
= NULL
;
941 if (!ip_xmit_attr_from_mblk(asyncmp
, &ixas
)) {
942 AH_BUMP_STAT(ahstack
, out_discards
);
943 ip_drop_packet(data_mp
, B_FALSE
, NULL
,
944 DROPPER(ipss
, ipds_sadb_acquire_timeout
),
945 &ahstack
->ah_dropper
);
946 } else if (rc
!= 0) {
947 ill
= ixas
.ixa_nce
->nce_ill
;
948 AH_BUMP_STAT(ahstack
, out_discards
);
949 ip_drop_packet(data_mp
, B_FALSE
, ill
,
950 DROPPER(ipss
, ipds_sadb_acquire_timeout
),
951 &ahstack
->ah_dropper
);
952 BUMP_MIB(ill
->ill_ip_mib
, ipIfStatsOutDiscards
);
954 ah_outbound_finish(data_mp
, &ixas
);
964 * Process one of the queued messages (from ipsacq_mp) once the SA
968 ah_outbound_finish(mblk_t
*data_mp
, ip_xmit_attr_t
*ixa
)
970 netstack_t
*ns
= ixa
->ixa_ipst
->ips_netstack
;
971 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
972 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
973 ill_t
*ill
= ixa
->ixa_nce
->nce_ill
;
975 if (!ipsec_outbound_sa(data_mp
, ixa
, IPPROTO_AH
)) {
976 AH_BUMP_STAT(ahstack
, out_discards
);
977 ip_drop_packet(data_mp
, B_FALSE
, ill
,
978 DROPPER(ipss
, ipds_sadb_acquire_timeout
),
979 &ahstack
->ah_dropper
);
980 BUMP_MIB(ill
->ill_ip_mib
, ipIfStatsOutDiscards
);
984 data_mp
= ah_outbound(data_mp
, ixa
);
988 (void) ip_output_post_ipsec(data_mp
, ixa
);
992 * Add new AH security association. This may become a generic AH/ESP
993 * routine eventually.
996 ah_add_sa(mblk_t
*mp
, keysock_in_t
*ksi
, int *diagnostic
, netstack_t
*ns
)
998 sadb_sa_t
*assoc
= (sadb_sa_t
*)ksi
->ks_in_extv
[SADB_EXT_SA
];
999 sadb_address_t
*srcext
=
1000 (sadb_address_t
*)ksi
->ks_in_extv
[SADB_EXT_ADDRESS_SRC
];
1001 sadb_address_t
*dstext
=
1002 (sadb_address_t
*)ksi
->ks_in_extv
[SADB_EXT_ADDRESS_DST
];
1003 sadb_address_t
*isrcext
=
1004 (sadb_address_t
*)ksi
->ks_in_extv
[SADB_X_EXT_ADDRESS_INNER_SRC
];
1005 sadb_address_t
*idstext
=
1006 (sadb_address_t
*)ksi
->ks_in_extv
[SADB_X_EXT_ADDRESS_INNER_DST
];
1007 sadb_key_t
*key
= (sadb_key_t
*)ksi
->ks_in_extv
[SADB_EXT_KEY_AUTH
];
1008 struct sockaddr_in
*src
, *dst
;
1009 /* We don't need sockaddr_in6 for now. */
1010 sadb_lifetime_t
*soft
=
1011 (sadb_lifetime_t
*)ksi
->ks_in_extv
[SADB_EXT_LIFETIME_SOFT
];
1012 sadb_lifetime_t
*hard
=
1013 (sadb_lifetime_t
*)ksi
->ks_in_extv
[SADB_EXT_LIFETIME_HARD
];
1014 sadb_lifetime_t
*idle
=
1015 (sadb_lifetime_t
*)ksi
->ks_in_extv
[SADB_X_EXT_LIFETIME_IDLE
];
1016 ipsec_alginfo_t
*aalg
;
1017 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
1018 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
1020 /* I need certain extensions present for an ADD message. */
1021 if (srcext
== NULL
) {
1022 *diagnostic
= SADB_X_DIAGNOSTIC_MISSING_SRC
;
1025 if (dstext
== NULL
) {
1026 *diagnostic
= SADB_X_DIAGNOSTIC_MISSING_DST
;
1029 if (isrcext
== NULL
&& idstext
!= NULL
) {
1030 *diagnostic
= SADB_X_DIAGNOSTIC_MISSING_INNER_SRC
;
1033 if (isrcext
!= NULL
&& idstext
== NULL
) {
1034 *diagnostic
= SADB_X_DIAGNOSTIC_MISSING_INNER_DST
;
1037 if (assoc
== NULL
) {
1038 *diagnostic
= SADB_X_DIAGNOSTIC_MISSING_SA
;
1042 *diagnostic
= SADB_X_DIAGNOSTIC_MISSING_AKEY
;
1046 src
= (struct sockaddr_in
*)(srcext
+ 1);
1047 dst
= (struct sockaddr_in
*)(dstext
+ 1);
1049 /* Sundry ADD-specific reality checks. */
1050 /* XXX STATS : Logging/stats here? */
1052 if ((assoc
->sadb_sa_state
!= SADB_SASTATE_MATURE
) &&
1053 (assoc
->sadb_sa_state
!= SADB_X_SASTATE_ACTIVE_ELSEWHERE
)) {
1054 *diagnostic
= SADB_X_DIAGNOSTIC_BAD_SASTATE
;
1057 if (assoc
->sadb_sa_encrypt
!= SADB_EALG_NONE
) {
1058 *diagnostic
= SADB_X_DIAGNOSTIC_ENCR_NOTSUPP
;
1061 if (assoc
->sadb_sa_flags
& ~ahstack
->ah_sadb
.s_addflags
) {
1062 *diagnostic
= SADB_X_DIAGNOSTIC_BAD_SAFLAGS
;
1065 if ((*diagnostic
= sadb_hardsoftchk(hard
, soft
, idle
)) != 0)
1068 ASSERT(src
->sin_family
== dst
->sin_family
);
1070 /* Stuff I don't support, for now. XXX Diagnostic? */
1071 if (ksi
->ks_in_extv
[SADB_EXT_LIFETIME_CURRENT
] != NULL
)
1072 return (EOPNOTSUPP
);
1074 if (ksi
->ks_in_extv
[SADB_EXT_SENSITIVITY
] != NULL
) {
1075 return (EOPNOTSUPP
);
1078 if (ksi
->ks_in_extv
[SADB_X_EXT_OUTER_SENS
] != NULL
) {
1079 return (EOPNOTSUPP
);
1082 * XXX Policy : I'm not checking identities at this time, but
1083 * if I did, I'd do them here, before I sent the weak key
1084 * check up to the algorithm.
1087 /* verify that there is a mapping for the specified algorithm */
1088 rw_enter(&ipss
->ipsec_alg_lock
, RW_READER
);
1089 aalg
= ipss
->ipsec_alglists
[IPSEC_ALG_AUTH
][assoc
->sadb_sa_auth
];
1090 if (aalg
== NULL
|| !ALG_VALID(aalg
)) {
1091 rw_exit(&ipss
->ipsec_alg_lock
);
1092 ah1dbg(ahstack
, ("Couldn't find auth alg #%d.\n",
1093 assoc
->sadb_sa_auth
));
1094 *diagnostic
= SADB_X_DIAGNOSTIC_BAD_AALG
;
1097 ASSERT(aalg
->alg_mech_type
!= CRYPTO_MECHANISM_INVALID
);
1099 /* sanity check key sizes */
1100 if (!ipsec_valid_key_size(key
->sadb_key_bits
, aalg
)) {
1101 rw_exit(&ipss
->ipsec_alg_lock
);
1102 *diagnostic
= SADB_X_DIAGNOSTIC_BAD_AKEYBITS
;
1106 /* check key and fix parity if needed */
1107 if (ipsec_check_key(aalg
->alg_mech_type
, key
, B_TRUE
,
1109 rw_exit(&ipss
->ipsec_alg_lock
);
1113 rw_exit(&ipss
->ipsec_alg_lock
);
1115 return (ah_add_sa_finish(mp
, (sadb_msg_t
*)mp
->b_cont
->b_rptr
, ksi
,
1116 diagnostic
, ahstack
));
1121 * Update a security association. Updates come in two varieties. The first
1122 * is an update of lifetimes on a non-larval SA. The second is an update of
1123 * a larval SA, which ends up looking a lot more like an add.
1126 ah_update_sa(mblk_t
*mp
, keysock_in_t
*ksi
, int *diagnostic
,
1127 ipsecah_stack_t
*ahstack
, uint8_t sadb_msg_type
)
1129 sadb_sa_t
*assoc
= (sadb_sa_t
*)ksi
->ks_in_extv
[SADB_EXT_SA
];
1130 sadb_address_t
*dstext
=
1131 (sadb_address_t
*)ksi
->ks_in_extv
[SADB_EXT_ADDRESS_DST
];
1135 if (dstext
== NULL
) {
1136 *diagnostic
= SADB_X_DIAGNOSTIC_MISSING_DST
;
1140 rcode
= sadb_update_sa(mp
, ksi
, &buf_pkt
, &ahstack
->ah_sadb
,
1141 diagnostic
, ahstack
->ah_pfkey_q
, ah_add_sa
,
1142 ahstack
->ipsecah_netstack
, sadb_msg_type
);
1144 if ((assoc
->sadb_sa_state
!= SADB_X_SASTATE_ACTIVE
) ||
1149 HANDLE_BUF_PKT(ah_taskq
, ahstack
->ipsecah_netstack
->netstack_ipsec
,
1150 ahstack
->ah_dropper
, buf_pkt
);
1157 * Delete a security association. This is REALLY likely to be code common to
1158 * both AH and ESP. Find the association, then unlink it.
1161 ah_del_sa(mblk_t
*mp
, keysock_in_t
*ksi
, int *diagnostic
,
1162 ipsecah_stack_t
*ahstack
, uint8_t sadb_msg_type
)
1164 sadb_sa_t
*assoc
= (sadb_sa_t
*)ksi
->ks_in_extv
[SADB_EXT_SA
];
1165 sadb_address_t
*dstext
=
1166 (sadb_address_t
*)ksi
->ks_in_extv
[SADB_EXT_ADDRESS_DST
];
1167 sadb_address_t
*srcext
=
1168 (sadb_address_t
*)ksi
->ks_in_extv
[SADB_EXT_ADDRESS_SRC
];
1169 struct sockaddr_in
*sin
;
1171 if (assoc
== NULL
) {
1173 sin
= (struct sockaddr_in
*)(dstext
+ 1);
1174 else if (srcext
!= NULL
)
1175 sin
= (struct sockaddr_in
*)(srcext
+ 1);
1177 *diagnostic
= SADB_X_DIAGNOSTIC_MISSING_SA
;
1180 return (sadb_purge_sa(mp
, ksi
,
1181 (sin
->sin_family
== AF_INET6
) ? &ahstack
->ah_sadb
.s_v6
:
1182 &ahstack
->ah_sadb
.s_v4
, diagnostic
, ahstack
->ah_pfkey_q
));
1185 return (sadb_delget_sa(mp
, ksi
, &ahstack
->ah_sadb
, diagnostic
,
1186 ahstack
->ah_pfkey_q
, sadb_msg_type
));
1191 * Convert the entire contents of all of AH's SA tables into PF_KEY SADB_DUMP
1195 ah_dump(mblk_t
*mp
, keysock_in_t
*ksi
, ipsecah_stack_t
*ahstack
)
1201 * Dump each fanout, bailing if error is non-zero.
1204 error
= sadb_dump(ahstack
->ah_pfkey_q
, mp
, ksi
, &ahstack
->ah_sadb
.s_v4
);
1208 error
= sadb_dump(ahstack
->ah_pfkey_q
, mp
, ksi
, &ahstack
->ah_sadb
.s_v6
);
1210 ASSERT(mp
->b_cont
!= NULL
);
1211 samsg
= (sadb_msg_t
*)mp
->b_cont
->b_rptr
;
1212 samsg
->sadb_msg_errno
= (uint8_t)error
;
1213 sadb_pfkey_echo(ahstack
->ah_pfkey_q
, mp
,
1214 (sadb_msg_t
*)mp
->b_cont
->b_rptr
, ksi
, NULL
);
1218 * First-cut reality check for an inbound PF_KEY message.
1221 ah_pfkey_reality_failures(mblk_t
*mp
, keysock_in_t
*ksi
,
1222 ipsecah_stack_t
*ahstack
)
1226 if (mp
->b_cont
== NULL
) {
1231 if (ksi
->ks_in_extv
[SADB_EXT_KEY_ENCRYPT
] != NULL
) {
1232 diagnostic
= SADB_X_DIAGNOSTIC_EKEY_PRESENT
;
1235 if (ksi
->ks_in_extv
[SADB_EXT_PROPOSAL
] != NULL
) {
1236 diagnostic
= SADB_X_DIAGNOSTIC_PROP_PRESENT
;
1239 if (ksi
->ks_in_extv
[SADB_EXT_SUPPORTED_AUTH
] != NULL
||
1240 ksi
->ks_in_extv
[SADB_EXT_SUPPORTED_ENCRYPT
] != NULL
) {
1241 diagnostic
= SADB_X_DIAGNOSTIC_SUPP_PRESENT
;
1244 return (B_FALSE
); /* False ==> no failures */
1247 sadb_pfkey_error(ahstack
->ah_pfkey_q
, mp
, EINVAL
,
1248 diagnostic
, ksi
->ks_in_serial
);
1249 return (B_TRUE
); /* True ==> failures */
1253 * AH parsing of PF_KEY messages. Keysock did most of the really silly
1254 * error cases. What I receive is a fully-formed, syntactically legal
1255 * PF_KEY message. I then need to check semantics...
1257 * This code may become common to AH and ESP. Stay tuned.
1259 * I also make the assumption that db_ref's are cool. If this assumption
1260 * is wrong, this means that someone other than keysock or me has been
1261 * mucking with PF_KEY messages.
1264 ah_parse_pfkey(mblk_t
*mp
, ipsecah_stack_t
*ahstack
)
1266 mblk_t
*msg
= mp
->b_cont
;
1270 int diagnostic
= SADB_X_DIAGNOSTIC_NONE
;
1272 ASSERT(msg
!= NULL
);
1274 samsg
= (sadb_msg_t
*)msg
->b_rptr
;
1275 ksi
= (keysock_in_t
*)mp
->b_rptr
;
1278 * If applicable, convert unspecified AF_INET6 to unspecified
1281 if (!sadb_addrfix(ksi
, ahstack
->ah_pfkey_q
, mp
,
1282 ahstack
->ipsecah_netstack
) ||
1283 ah_pfkey_reality_failures(mp
, ksi
, ahstack
)) {
1287 switch (samsg
->sadb_msg_type
) {
1289 error
= ah_add_sa(mp
, ksi
, &diagnostic
,
1290 ahstack
->ipsecah_netstack
);
1292 sadb_pfkey_error(ahstack
->ah_pfkey_q
, mp
, error
,
1293 diagnostic
, ksi
->ks_in_serial
);
1295 /* else ah_add_sa() took care of things. */
1298 case SADB_X_DELPAIR
:
1299 case SADB_X_DELPAIR_STATE
:
1300 error
= ah_del_sa(mp
, ksi
, &diagnostic
, ahstack
,
1301 samsg
->sadb_msg_type
);
1303 sadb_pfkey_error(ahstack
->ah_pfkey_q
, mp
, error
,
1304 diagnostic
, ksi
->ks_in_serial
);
1306 /* Else ah_del_sa() took care of things. */
1309 error
= sadb_delget_sa(mp
, ksi
, &ahstack
->ah_sadb
, &diagnostic
,
1310 ahstack
->ah_pfkey_q
, samsg
->sadb_msg_type
);
1312 sadb_pfkey_error(ahstack
->ah_pfkey_q
, mp
, error
,
1313 diagnostic
, ksi
->ks_in_serial
);
1315 /* Else sadb_get_sa() took care of things. */
1318 sadbp_flush(&ahstack
->ah_sadb
, ahstack
->ipsecah_netstack
);
1319 sadb_pfkey_echo(ahstack
->ah_pfkey_q
, mp
, samsg
, ksi
, NULL
);
1323 * Hmmm, let's do it! Check for extensions (there should
1324 * be none), extract the fields, call ah_register_out(),
1325 * then either free or report an error.
1327 * Keysock takes care of the PF_KEY bookkeeping for this.
1329 if (ah_register_out(samsg
->sadb_msg_seq
, samsg
->sadb_msg_pid
,
1330 ksi
->ks_in_serial
, ahstack
, msg_getcred(mp
, NULL
))) {
1334 * Only way this path hits is if there is a memory
1335 * failure. It will not return B_FALSE because of
1336 * lack of ah_pfkey_q if I am in wput().
1338 sadb_pfkey_error(ahstack
->ah_pfkey_q
, mp
, ENOMEM
,
1339 diagnostic
, ksi
->ks_in_serial
);
1343 case SADB_X_UPDATEPAIR
:
1345 * Find a larval, if not there, find a full one and get
1348 error
= ah_update_sa(mp
, ksi
, &diagnostic
, ahstack
,
1349 samsg
->sadb_msg_type
);
1351 sadb_pfkey_error(ahstack
->ah_pfkey_q
, mp
, error
,
1352 diagnostic
, ksi
->ks_in_serial
);
1354 /* else ah_update_sa() took care of things. */
1358 * Reserve a new larval entry.
1360 ah_getspi(mp
, ksi
, ahstack
);
1364 * Find larval and/or ACQUIRE record and kill it (them), I'm
1365 * most likely an error. Inbound ACQUIRE messages should only
1366 * have the base header.
1368 sadb_in_acquire(samsg
, &ahstack
->ah_sadb
, ahstack
->ah_pfkey_q
,
1369 ahstack
->ipsecah_netstack
);
1376 ah_dump(mp
, ksi
, ahstack
);
1377 /* ah_dump will take care of the return message, etc. */
1380 /* Should never reach me. */
1381 sadb_pfkey_error(ahstack
->ah_pfkey_q
, mp
, EOPNOTSUPP
,
1382 diagnostic
, ksi
->ks_in_serial
);
1385 sadb_pfkey_error(ahstack
->ah_pfkey_q
, mp
, EINVAL
,
1386 SADB_X_DIAGNOSTIC_UNKNOWN_MSG
, ksi
->ks_in_serial
);
1392 * Handle case where PF_KEY says it can't find a keysock for one of my
1396 ah_keysock_no_socket(mblk_t
*mp
, ipsecah_stack_t
*ahstack
)
1399 keysock_out_err_t
*kse
= (keysock_out_err_t
*)mp
->b_rptr
;
1401 if (mp
->b_cont
== NULL
) {
1405 samsg
= (sadb_msg_t
*)mp
->b_cont
->b_rptr
;
1408 * If keysock can't find any registered, delete the acquire record
1409 * immediately, and handle errors.
1411 if (samsg
->sadb_msg_type
== SADB_ACQUIRE
) {
1412 samsg
->sadb_msg_errno
= kse
->ks_err_errno
;
1413 samsg
->sadb_msg_len
= SADB_8TO64(sizeof (*samsg
));
1415 * Use the write-side of the ah_pfkey_q
1417 sadb_in_acquire(samsg
, &ahstack
->ah_sadb
,
1418 WR(ahstack
->ah_pfkey_q
), ahstack
->ipsecah_netstack
);
1425 * AH module write put routine.
1428 ipsecah_wput(queue_t
*q
, mblk_t
*mp
)
1431 struct iocblk
*iocp
;
1432 ipsecah_stack_t
*ahstack
= (ipsecah_stack_t
*)q
->q_ptr
;
1434 ah3dbg(ahstack
, ("In ah_wput().\n"));
1436 /* NOTE: Each case must take care of freeing or passing mp. */
1437 switch (mp
->b_datap
->db_type
) {
1439 if ((mp
->b_wptr
- mp
->b_rptr
) < sizeof (ipsec_info_t
)) {
1440 /* Not big enough message. */
1444 ii
= (ipsec_info_t
*)mp
->b_rptr
;
1446 switch (ii
->ipsec_info_type
) {
1447 case KEYSOCK_OUT_ERR
:
1448 ah1dbg(ahstack
, ("Got KEYSOCK_OUT_ERR message.\n"));
1449 ah_keysock_no_socket(mp
, ahstack
);
1452 AH_BUMP_STAT(ahstack
, keysock_in
);
1453 ah3dbg(ahstack
, ("Got KEYSOCK_IN message.\n"));
1455 /* Parse the message. */
1456 ah_parse_pfkey(mp
, ahstack
);
1459 sadb_keysock_hello(&ahstack
->ah_pfkey_q
, q
, mp
,
1460 ah_ager
, (void *)ahstack
, &ahstack
->ah_event
,
1464 ah1dbg(ahstack
, ("Got M_CTL from above of 0x%x.\n",
1465 ii
->ipsec_info_type
));
1471 iocp
= (struct iocblk
*)mp
->b_rptr
;
1472 switch (iocp
->ioc_cmd
) {
1475 if (nd_getset(q
, ahstack
->ipsecah_g_nd
, mp
)) {
1479 iocp
->ioc_error
= ENOENT
;
1483 /* We really don't support any other ioctls, do we? */
1486 if (iocp
->ioc_error
!= ENOENT
)
1487 iocp
->ioc_error
= EINVAL
;
1488 iocp
->ioc_count
= 0;
1489 mp
->b_datap
->db_type
= M_IOCACK
;
1495 ("Got default message, type %d, passing to IP.\n",
1496 mp
->b_datap
->db_type
));
1503 * Updating use times can be tricky business if the ipsa_haspeer flag is
1504 * set. This function is called once in an SA's lifetime.
1506 * Caller has to REFRELE "assoc" which is passed in. This function has
1507 * to REFRELE any peer SA that is obtained.
1510 ah_set_usetime(ipsa_t
*assoc
, boolean_t inbound
)
1512 ipsa_t
*inassoc
, *outassoc
;
1517 netstack_t
*ns
= assoc
->ipsa_netstack
;
1518 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
1520 /* No peer? No problem! */
1521 if (!assoc
->ipsa_haspeer
) {
1522 sadb_set_usetime(assoc
);
1527 * Otherwise, we want to grab both the original assoc and its peer.
1528 * There might be a race for this, but if it's a real race, the times
1529 * will be out-of-synch by at most a second, and since our time
1530 * granularity is a second, this won't be a problem.
1532 * If we need tight synchronization on the peer SA, then we need to
1536 /* Use address family to select IPv6/IPv4 */
1537 isv6
= (assoc
->ipsa_addrfam
== AF_INET6
);
1539 sp
= &ahstack
->ah_sadb
.s_v6
;
1541 sp
= &ahstack
->ah_sadb
.s_v4
;
1542 ASSERT(assoc
->ipsa_addrfam
== AF_INET
);
1547 outhash
= OUTBOUND_HASH_V6(sp
,
1548 *((in6_addr_t
*)&inassoc
->ipsa_dstaddr
));
1550 outhash
= OUTBOUND_HASH_V4(sp
,
1551 *((ipaddr_t
*)&inassoc
->ipsa_dstaddr
));
1552 bucket
= &sp
->sdb_of
[outhash
];
1554 mutex_enter(&bucket
->isaf_lock
);
1555 outassoc
= ipsec_getassocbyspi(bucket
, inassoc
->ipsa_spi
,
1556 inassoc
->ipsa_srcaddr
, inassoc
->ipsa_dstaddr
,
1557 inassoc
->ipsa_addrfam
);
1558 mutex_exit(&bucket
->isaf_lock
);
1559 if (outassoc
== NULL
) {
1560 /* Q: Do we wish to set haspeer == B_FALSE? */
1561 ah0dbg(("ah_set_usetime: "
1562 "can't find peer for inbound.\n"));
1563 sadb_set_usetime(inassoc
);
1568 bucket
= INBOUND_BUCKET(sp
, outassoc
->ipsa_spi
);
1569 mutex_enter(&bucket
->isaf_lock
);
1570 inassoc
= ipsec_getassocbyspi(bucket
, outassoc
->ipsa_spi
,
1571 outassoc
->ipsa_srcaddr
, outassoc
->ipsa_dstaddr
,
1572 outassoc
->ipsa_addrfam
);
1573 mutex_exit(&bucket
->isaf_lock
);
1574 if (inassoc
== NULL
) {
1575 /* Q: Do we wish to set haspeer == B_FALSE? */
1576 ah0dbg(("ah_set_usetime: "
1577 "can't find peer for outbound.\n"));
1578 sadb_set_usetime(outassoc
);
1583 /* Update usetime on both. */
1584 sadb_set_usetime(inassoc
);
1585 sadb_set_usetime(outassoc
);
1588 * REFRELE any peer SA.
1590 * Because of the multi-line macro nature of IPSA_REFRELE, keep
1594 IPSA_REFRELE(outassoc
);
1596 IPSA_REFRELE(inassoc
);
1602 * Add a number of bytes to what the SA has protected so far. Return
1603 * B_TRUE if the SA can still protect that many bytes.
1605 * Caller must REFRELE the passed-in assoc. This function must REFRELE
1606 * any obtained peer SA.
1609 ah_age_bytes(ipsa_t
*assoc
, uint64_t bytes
, boolean_t inbound
)
1611 ipsa_t
*inassoc
, *outassoc
;
1613 boolean_t inrc
, outrc
, isv6
;
1616 netstack_t
*ns
= assoc
->ipsa_netstack
;
1617 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
1619 /* No peer? No problem! */
1620 if (!assoc
->ipsa_haspeer
) {
1621 return (sadb_age_bytes(ahstack
->ah_pfkey_q
, assoc
, bytes
,
1626 * Otherwise, we want to grab both the original assoc and its peer.
1627 * There might be a race for this, but if it's a real race, two
1628 * expire messages may occur. We limit this by only sending the
1629 * expire message on one of the peers, we'll pick the inbound
1632 * If we need tight synchronization on the peer SA, then we need to
1636 /* Pick v4/v6 bucket based on addrfam. */
1637 isv6
= (assoc
->ipsa_addrfam
== AF_INET6
);
1639 sp
= &ahstack
->ah_sadb
.s_v6
;
1641 sp
= &ahstack
->ah_sadb
.s_v4
;
1642 ASSERT(assoc
->ipsa_addrfam
== AF_INET
);
1647 outhash
= OUTBOUND_HASH_V6(sp
,
1648 *((in6_addr_t
*)&inassoc
->ipsa_dstaddr
));
1650 outhash
= OUTBOUND_HASH_V4(sp
,
1651 *((ipaddr_t
*)&inassoc
->ipsa_dstaddr
));
1652 bucket
= &sp
->sdb_of
[outhash
];
1653 mutex_enter(&bucket
->isaf_lock
);
1654 outassoc
= ipsec_getassocbyspi(bucket
, inassoc
->ipsa_spi
,
1655 inassoc
->ipsa_srcaddr
, inassoc
->ipsa_dstaddr
,
1656 inassoc
->ipsa_addrfam
);
1657 mutex_exit(&bucket
->isaf_lock
);
1658 if (outassoc
== NULL
) {
1659 /* Q: Do we wish to set haspeer == B_FALSE? */
1660 ah0dbg(("ah_age_bytes: "
1661 "can't find peer for inbound.\n"));
1662 return (sadb_age_bytes(ahstack
->ah_pfkey_q
, inassoc
,
1667 bucket
= INBOUND_BUCKET(sp
, outassoc
->ipsa_spi
);
1668 mutex_enter(&bucket
->isaf_lock
);
1669 inassoc
= ipsec_getassocbyspi(bucket
, outassoc
->ipsa_spi
,
1670 outassoc
->ipsa_srcaddr
, outassoc
->ipsa_dstaddr
,
1671 outassoc
->ipsa_addrfam
);
1672 mutex_exit(&bucket
->isaf_lock
);
1673 if (inassoc
== NULL
) {
1674 /* Q: Do we wish to set haspeer == B_FALSE? */
1675 ah0dbg(("ah_age_bytes: "
1676 "can't find peer for outbound.\n"));
1677 return (sadb_age_bytes(ahstack
->ah_pfkey_q
, outassoc
,
1682 inrc
= sadb_age_bytes(ahstack
->ah_pfkey_q
, inassoc
, bytes
, B_TRUE
);
1683 outrc
= sadb_age_bytes(ahstack
->ah_pfkey_q
, outassoc
, bytes
, B_FALSE
);
1686 * REFRELE any peer SA.
1688 * Because of the multi-line macro nature of IPSA_REFRELE, keep
1692 IPSA_REFRELE(outassoc
);
1694 IPSA_REFRELE(inassoc
);
1697 return (inrc
&& outrc
);
1702 * Handle the SADB_GETSPI message. Create a larval SA.
1705 ah_getspi(mblk_t
*mp
, keysock_in_t
*ksi
, ipsecah_stack_t
*ahstack
)
1707 ipsa_t
*newbie
, *target
;
1708 isaf_t
*outbound
, *inbound
;
1715 * Randomly generate a proposed SPI value.
1717 (void) random_get_pseudo_bytes((uint8_t *)&newspi
, sizeof (uint32_t));
1719 newbie
= sadb_getspi(ksi
, newspi
, &diagnostic
,
1720 ahstack
->ipsecah_netstack
);
1721 if (newbie
== NULL
) {
1722 sadb_pfkey_error(ahstack
->ah_pfkey_q
, mp
, ENOMEM
, diagnostic
,
1725 } else if (newbie
== (ipsa_t
*)-1) {
1726 sadb_pfkey_error(ahstack
->ah_pfkey_q
, mp
, EINVAL
, diagnostic
,
1732 * XXX - We may randomly collide. We really should recover from this.
1733 * Unfortunately, that could require spending way-too-much-time
1734 * in here. For now, let the user retry.
1737 if (newbie
->ipsa_addrfam
== AF_INET6
) {
1738 outbound
= OUTBOUND_BUCKET_V6(&ahstack
->ah_sadb
.s_v6
,
1739 *(uint32_t *)(newbie
->ipsa_dstaddr
));
1740 inbound
= INBOUND_BUCKET(&ahstack
->ah_sadb
.s_v6
,
1743 outbound
= OUTBOUND_BUCKET_V4(&ahstack
->ah_sadb
.s_v4
,
1744 *(uint32_t *)(newbie
->ipsa_dstaddr
));
1745 inbound
= INBOUND_BUCKET(&ahstack
->ah_sadb
.s_v4
,
1749 mutex_enter(&outbound
->isaf_lock
);
1750 mutex_enter(&inbound
->isaf_lock
);
1753 * Check for collisions (i.e. did sadb_getspi() return with something
1754 * that already exists?).
1756 * Try outbound first. Even though SADB_GETSPI is traditionally
1757 * for inbound SAs, you never know what a user might do.
1759 target
= ipsec_getassocbyspi(outbound
, newbie
->ipsa_spi
,
1760 newbie
->ipsa_srcaddr
, newbie
->ipsa_dstaddr
, newbie
->ipsa_addrfam
);
1761 if (target
== NULL
) {
1762 target
= ipsec_getassocbyspi(inbound
, newbie
->ipsa_spi
,
1763 newbie
->ipsa_srcaddr
, newbie
->ipsa_dstaddr
,
1764 newbie
->ipsa_addrfam
);
1768 * I don't have collisions elsewhere!
1769 * (Nor will I because I'm still holding inbound/outbound locks.)
1772 if (target
!= NULL
) {
1774 IPSA_REFRELE(target
);
1777 * sadb_insertassoc() also checks for collisions, so
1778 * if there's a colliding larval entry, rc will be set
1781 rc
= sadb_insertassoc(newbie
, inbound
);
1782 newbie
->ipsa_hardexpiretime
= gethrestime_sec();
1783 newbie
->ipsa_hardexpiretime
+= ahstack
->ipsecah_larval_timeout
;
1787 * Can exit outbound mutex. Hold inbound until we're done with
1790 mutex_exit(&outbound
->isaf_lock
);
1793 mutex_exit(&inbound
->isaf_lock
);
1794 IPSA_REFRELE(newbie
);
1795 sadb_pfkey_error(ahstack
->ah_pfkey_q
, mp
, rc
,
1796 SADB_X_DIAGNOSTIC_NONE
, ksi
->ks_in_serial
);
1800 /* Can write here because I'm still holding the bucket lock. */
1801 newbie
->ipsa_type
= SADB_SATYPE_AH
;
1804 * Construct successful return message. We have one thing going
1805 * for us in PF_KEY v2. That's the fact that
1806 * sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
1808 assoc
= (sadb_sa_t
*)ksi
->ks_in_extv
[SADB_EXT_SPIRANGE
];
1809 assoc
->sadb_sa_exttype
= SADB_EXT_SA
;
1810 assoc
->sadb_sa_spi
= newbie
->ipsa_spi
;
1811 *((uint64_t *)(&assoc
->sadb_sa_replay
)) = 0;
1812 mutex_exit(&inbound
->isaf_lock
);
1814 /* Convert KEYSOCK_IN to KEYSOCK_OUT. */
1815 kso
= (keysock_out_t
*)ksi
;
1816 kso
->ks_out_len
= sizeof (*kso
);
1817 kso
->ks_out_serial
= ksi
->ks_in_serial
;
1818 kso
->ks_out_type
= KEYSOCK_OUT
;
1821 * Can safely putnext() to ah_pfkey_q, because this is a turnaround
1822 * from the ah_pfkey_q.
1824 putnext(ahstack
->ah_pfkey_q
, mp
);
1828 * IPv6 sends up the ICMP errors for validation and the removal of the AH
1830 * If succesful, the mp has been modified to not include the AH header so
1831 * that the caller can fanout to the ULP's icmp error handler.
1834 ah_icmp_error_v6(mblk_t
*mp
, ip_recv_attr_t
*ira
, ipsecah_stack_t
*ahstack
)
1836 ip6_t
*ip6h
, *oip6h
;
1837 uint16_t hdr_length
, ah_length
;
1843 uint8_t *post_ah_ptr
;
1844 ipsec_stack_t
*ipss
= ahstack
->ipsecah_netstack
->netstack_ipsec
;
1847 * Eat the cost of a pullupmsg() for now. It makes the rest of this
1848 * code far less convoluted.
1850 if (!pullupmsg(mp
, -1) ||
1851 !ip_hdr_length_nexthdr_v6(mp
, (ip6_t
*)mp
->b_rptr
, &hdr_length
,
1853 mp
->b_rptr
+ hdr_length
+ sizeof (icmp6_t
) + sizeof (ip6_t
) +
1854 sizeof (ah_t
) > mp
->b_wptr
) {
1855 IP_AH_BUMP_STAT(ipss
, in_discards
);
1856 ip_drop_packet(mp
, B_TRUE
, ira
->ira_ill
,
1857 DROPPER(ipss
, ipds_ah_nomem
),
1858 &ahstack
->ah_dropper
);
1862 oip6h
= (ip6_t
*)mp
->b_rptr
;
1863 icmp6
= (icmp6_t
*)((uint8_t *)oip6h
+ hdr_length
);
1864 ip6h
= (ip6_t
*)(icmp6
+ 1);
1865 if (!ip_hdr_length_nexthdr_v6(mp
, ip6h
, &hdr_length
, &nexthdrp
)) {
1866 IP_AH_BUMP_STAT(ipss
, in_discards
);
1867 ip_drop_packet(mp
, B_TRUE
, ira
->ira_ill
,
1868 DROPPER(ipss
, ipds_ah_bad_v6_hdrs
),
1869 &ahstack
->ah_dropper
);
1872 ah
= (ah_t
*)((uint8_t *)ip6h
+ hdr_length
);
1874 isaf
= OUTBOUND_BUCKET_V6(&ahstack
->ah_sadb
.s_v6
, ip6h
->ip6_dst
);
1875 mutex_enter(&isaf
->isaf_lock
);
1876 assoc
= ipsec_getassocbyspi(isaf
, ah
->ah_spi
,
1877 (uint32_t *)&ip6h
->ip6_src
, (uint32_t *)&ip6h
->ip6_dst
, AF_INET6
);
1878 mutex_exit(&isaf
->isaf_lock
);
1880 if (assoc
== NULL
) {
1881 IP_AH_BUMP_STAT(ipss
, lookup_failure
);
1882 IP_AH_BUMP_STAT(ipss
, in_discards
);
1883 if (ahstack
->ipsecah_log_unknown_spi
) {
1884 ipsec_assocfailure(info
.mi_idnum
, 0, 0,
1885 SL_CONSOLE
| SL_WARN
| SL_ERROR
,
1886 "Bad ICMP message - No association for the "
1887 "attached AH header whose spi is 0x%x, "
1889 ah
->ah_spi
, &oip6h
->ip6_src
, AF_INET6
,
1890 ahstack
->ipsecah_netstack
);
1892 ip_drop_packet(mp
, B_TRUE
, ira
->ira_ill
,
1893 DROPPER(ipss
, ipds_ah_no_sa
),
1894 &ahstack
->ah_dropper
);
1898 IPSA_REFRELE(assoc
);
1901 * There seems to be a valid association. If there is enough of AH
1902 * header remove it, otherwise bail. One could check whether it has
1903 * complete AH header plus 8 bytes but it does not make sense if an
1904 * icmp error is returned for ICMP messages e.g ICMP time exceeded,
1905 * that are being sent up. Let the caller figure out.
1907 * NOTE: ah_length is the number of 32 bit words minus 2.
1909 ah_length
= (ah
->ah_length
<< 2) + 8;
1910 post_ah_ptr
= (uint8_t *)ah
+ ah_length
;
1912 if (post_ah_ptr
> mp
->b_wptr
) {
1913 IP_AH_BUMP_STAT(ipss
, in_discards
);
1914 ip_drop_packet(mp
, B_TRUE
, ira
->ira_ill
,
1915 DROPPER(ipss
, ipds_ah_bad_length
),
1916 &ahstack
->ah_dropper
);
1920 ip6h
->ip6_plen
= htons(ntohs(ip6h
->ip6_plen
) - ah_length
);
1921 *nexthdrp
= ah
->ah_nexthdr
;
1922 ovbcopy(post_ah_ptr
, ah
,
1923 (size_t)((uintptr_t)mp
->b_wptr
- (uintptr_t)post_ah_ptr
));
1924 mp
->b_wptr
-= ah_length
;
1930 * IP sends up the ICMP errors for validation and the removal of
1932 * If succesful, the mp has been modified to not include the AH header so
1933 * that the caller can fanout to the ULP's icmp error handler.
1936 ah_icmp_error_v4(mblk_t
*mp
, ip_recv_attr_t
*ira
, ipsecah_stack_t
*ahstack
)
1951 ipsec_stack_t
*ipss
= ahstack
->ipsecah_netstack
->netstack_ipsec
;
1953 oipha
= ipha
= (ipha_t
*)mp
->b_rptr
;
1954 iph_hdr_length
= IPH_HDR_LENGTH(ipha
);
1955 icmph
= (icmph_t
*)&mp
->b_rptr
[iph_hdr_length
];
1957 ipha
= (ipha_t
*)&icmph
[1];
1958 hdr_length
= IPH_HDR_LENGTH(ipha
);
1961 * See if we have enough to locate the SPI
1963 if ((uchar_t
*)ipha
+ hdr_length
+ 8 > mp
->b_wptr
) {
1964 if (!pullupmsg(mp
, (uchar_t
*)ipha
+ hdr_length
+ 8 -
1966 ipsec_rl_strlog(ahstack
->ipsecah_netstack
,
1967 info
.mi_idnum
, 0, 0,
1969 "ICMP error: Small AH header\n");
1970 IP_AH_BUMP_STAT(ipss
, in_discards
);
1971 ip_drop_packet(mp
, B_TRUE
, ira
->ira_ill
,
1972 DROPPER(ipss
, ipds_ah_bad_length
),
1973 &ahstack
->ah_dropper
);
1976 icmph
= (icmph_t
*)&mp
->b_rptr
[iph_hdr_length
];
1977 ipha
= (ipha_t
*)&icmph
[1];
1980 ah
= (ah_t
*)((uint8_t *)ipha
+ hdr_length
);
1981 nexthdr
= ah
->ah_nexthdr
;
1983 hptr
= OUTBOUND_BUCKET_V4(&ahstack
->ah_sadb
.s_v4
, ipha
->ipha_dst
);
1984 mutex_enter(&hptr
->isaf_lock
);
1985 assoc
= ipsec_getassocbyspi(hptr
, ah
->ah_spi
,
1986 (uint32_t *)&ipha
->ipha_src
, (uint32_t *)&ipha
->ipha_dst
, AF_INET
);
1987 mutex_exit(&hptr
->isaf_lock
);
1989 if (assoc
== NULL
) {
1990 IP_AH_BUMP_STAT(ipss
, lookup_failure
);
1991 IP_AH_BUMP_STAT(ipss
, in_discards
);
1992 if (ahstack
->ipsecah_log_unknown_spi
) {
1993 ipsec_assocfailure(info
.mi_idnum
, 0, 0,
1994 SL_CONSOLE
| SL_WARN
| SL_ERROR
,
1995 "Bad ICMP message - No association for the "
1996 "attached AH header whose spi is 0x%x, "
1998 ah
->ah_spi
, &oipha
->ipha_src
, AF_INET
,
1999 ahstack
->ipsecah_netstack
);
2001 ip_drop_packet(mp
, B_TRUE
, ira
->ira_ill
,
2002 DROPPER(ipss
, ipds_ah_no_sa
),
2003 &ahstack
->ah_dropper
);
2007 IPSA_REFRELE(assoc
);
2009 * There seems to be a valid association. If there
2010 * is enough of AH header remove it, otherwise remove
2011 * as much as possible and send it back. One could check
2012 * whether it has complete AH header plus 8 bytes but it
2013 * does not make sense if an icmp error is returned for
2014 * ICMP messages e.g ICMP time exceeded, that are being
2015 * sent up. Let the caller figure out.
2017 * NOTE: ah_length is the number of 32 bit words minus 2.
2019 ah_length
= (ah
->ah_length
<< 2) + 8;
2021 if ((uchar_t
*)ipha
+ hdr_length
+ ah_length
> mp
->b_wptr
) {
2022 if (mp
->b_cont
== NULL
) {
2024 * There is nothing to pullup. Just remove as
2025 * much as possible. This is a common case for
2028 ah_length
= (mp
->b_wptr
- ((uchar_t
*)ipha
+
2032 /* Pullup the full ah header */
2033 if (!pullupmsg(mp
, (uchar_t
*)ah
+ ah_length
- mp
->b_rptr
)) {
2035 * pullupmsg could have failed if there was not
2036 * enough to pullup or memory allocation failed.
2037 * We tried hard, give up now.
2039 IP_AH_BUMP_STAT(ipss
, in_discards
);
2040 ip_drop_packet(mp
, B_TRUE
, ira
->ira_ill
,
2041 DROPPER(ipss
, ipds_ah_nomem
),
2042 &ahstack
->ah_dropper
);
2045 icmph
= (icmph_t
*)&mp
->b_rptr
[iph_hdr_length
];
2046 ipha
= (ipha_t
*)&icmph
[1];
2050 * Remove the AH header and change the protocol.
2051 * Don't update the spi fields in the ip_recv_attr_t
2052 * as we are called just to validate the
2053 * message attached to the ICMP message.
2055 * If we never pulled up since all of the message
2056 * is in one single mblk, we can't remove the AH header
2057 * by just setting the b_wptr to the beginning of the
2058 * AH header. We need to allocate a mblk that can hold
2059 * up until the inner IP header and copy them.
2061 alloc_size
= iph_hdr_length
+ sizeof (icmph_t
) + hdr_length
;
2063 if ((mp1
= allocb(alloc_size
, BPRI_LO
)) == NULL
) {
2064 IP_AH_BUMP_STAT(ipss
, in_discards
);
2065 ip_drop_packet(mp
, B_TRUE
, ira
->ira_ill
,
2066 DROPPER(ipss
, ipds_ah_nomem
),
2067 &ahstack
->ah_dropper
);
2070 bcopy(mp
->b_rptr
, mp1
->b_rptr
, alloc_size
);
2071 mp1
->b_wptr
+= alloc_size
;
2074 * Skip whatever we have copied and as much of AH header
2075 * possible. If we still have something left in the original
2078 mp
->b_rptr
= (uchar_t
*)ipha
+ hdr_length
+ ah_length
;
2080 if (mp
->b_rptr
!= mp
->b_wptr
) {
2083 if (mp
->b_cont
!= NULL
)
2084 mp1
->b_cont
= mp
->b_cont
;
2088 ipha
= (ipha_t
*)(mp1
->b_rptr
+ iph_hdr_length
+ sizeof (icmph_t
));
2089 ipha
->ipha_protocol
= nexthdr
;
2090 length
= ntohs(ipha
->ipha_length
);
2091 length
-= ah_length
;
2092 ipha
->ipha_length
= htons((uint16_t)length
);
2093 ipha
->ipha_hdr_checksum
= 0;
2094 ipha
->ipha_hdr_checksum
= (uint16_t)ip_csum_hdr(ipha
);
2100 * IP calls this to validate the ICMP errors that
2101 * we got from the network.
2104 ipsecah_icmp_error(mblk_t
*data_mp
, ip_recv_attr_t
*ira
)
2106 netstack_t
*ns
= ira
->ira_ill
->ill_ipst
->ips_netstack
;
2107 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
2109 if (ira
->ira_flags
& IRAF_IS_IPV4
)
2110 return (ah_icmp_error_v4(data_mp
, ira
, ahstack
));
2112 return (ah_icmp_error_v6(data_mp
, ira
, ahstack
));
2116 ah_fix_tlv_options_v6(uint8_t *oi_opt
, uint8_t *pi_opt
, uint_t ehdrlen
,
2117 uint8_t hdr_type
, boolean_t copy_always
)
2122 ASSERT(hdr_type
== IPPROTO_DSTOPTS
|| hdr_type
== IPPROTO_HOPOPTS
);
2125 * Copy the next header and hdr ext. len of the HOP-by-HOP
2126 * and Destination option.
2128 *pi_opt
++ = *oi_opt
++;
2129 *pi_opt
++ = *oi_opt
++;
2133 * Now handle all the TLV encoded options.
2135 while (ehdrlen
!= 0) {
2138 if (opt_type
== IP6OPT_PAD1
) {
2143 optlen
= 2 + oi_opt
[1];
2144 if (optlen
> ehdrlen
)
2147 if (copy_always
|| !(opt_type
& IP6OPT_MUTABLE
)) {
2148 bcopy(oi_opt
, pi_opt
, optlen
);
2154 * Copy the type and data length fields.
2155 * Zero the option data by skipping
2156 * option type and option data len
2160 *(pi_opt
+ 1) = *(oi_opt
+ 1);
2161 bzero(pi_opt
+ 2, optlen
- 2);
2174 * Construct a pseudo header for AH, processing all the options.
2176 * oip6h is the IPv6 header of the incoming or outgoing packet.
2177 * ip6h is the pointer to the pseudo headers IPV6 header. All
2178 * the space needed for the options have been allocated including
2181 * If copy_always is set, all the options that appear before AH are copied
2182 * blindly without checking for IP6OPT_MUTABLE. This is used by
2183 * ah_auth_out_done(). Please refer to that function for details.
2187 * * AH header is never copied in this function even if copy_always
2188 * is set. It just returns the ah_offset - offset of the AH header
2189 * and the caller needs to do the copying. This is done so that we
2190 * don't have pass extra arguments e.g. SA etc. and also,
2191 * it is not needed when ah_auth_out_done is calling this function.
2194 ah_fix_phdr_v6(ip6_t
*ip6h
, ip6_t
*oip6h
, boolean_t outbound
,
2195 boolean_t copy_always
)
2200 uint8_t *prev_nexthdr
;
2202 ip6_dest_t
*dsthdr
= NULL
;
2203 ip6_rthdr0_t
*rthdr
;
2209 * In the outbound case for source route, ULP has already moved
2210 * the first hop, which is now in ip6_dst. We need to re-arrange
2211 * the header to make it look like how it would appear in the
2214 * Because of ip_massage_options_v6 the header looks like
2217 * ip6_src = S, ip6_dst = I1. followed by I2,I3,D.
2219 * When it reaches the receiver, it would look like
2221 * ip6_src = S, ip6_dst = D. followed by I1,I2,I3.
2223 * NOTE : We assume that there are no problems with the options
2224 * as IP should have already checked this.
2227 oi_opt
= (uchar_t
*)&oip6h
[1];
2228 pi_opt
= (uchar_t
*)&ip6h
[1];
2231 * We set the prev_nexthdr properly in the pseudo header.
2232 * After we finish authentication and come back from the
2233 * algorithm module, pseudo header will become the real
2236 prev_nexthdr
= (uint8_t *)&ip6h
->ip6_nxt
;
2237 nexthdr
= oip6h
->ip6_nxt
;
2238 /* Assume IP has already stripped it */
2239 ASSERT(nexthdr
!= IPPROTO_FRAGMENT
);
2244 case IPPROTO_HOPOPTS
:
2245 hbhhdr
= (ip6_hbh_t
*)oi_opt
;
2246 nexthdr
= hbhhdr
->ip6h_nxt
;
2247 ehdrlen
= 8 * (hbhhdr
->ip6h_len
+ 1);
2248 ret
= ah_fix_tlv_options_v6(oi_opt
, pi_opt
, ehdrlen
,
2249 IPPROTO_HOPOPTS
, copy_always
);
2251 * Return a zero offset indicating error if there
2256 hbhhdr
= (ip6_hbh_t
*)pi_opt
;
2257 prev_nexthdr
= (uint8_t *)&hbhhdr
->ip6h_nxt
;
2259 case IPPROTO_ROUTING
:
2260 rthdr
= (ip6_rthdr0_t
*)oi_opt
;
2261 nexthdr
= rthdr
->ip6r0_nxt
;
2262 ehdrlen
= 8 * (rthdr
->ip6r0_len
+ 1);
2263 if (!copy_always
&& outbound
) {
2265 ip6_rthdr0_t
*prthdr
;
2266 in6_addr_t
*ap
, *pap
;
2268 left
= rthdr
->ip6r0_segleft
;
2269 prthdr
= (ip6_rthdr0_t
*)pi_opt
;
2270 pap
= (in6_addr_t
*)(prthdr
+ 1);
2271 ap
= (in6_addr_t
*)(rthdr
+ 1);
2273 * First eight bytes except seg_left
2274 * does not change en route.
2276 bcopy(oi_opt
, pi_opt
, 8);
2277 prthdr
->ip6r0_segleft
= 0;
2279 * First address has been moved to
2280 * the destination address of the
2281 * ip header by ip_massage_options_v6.
2282 * And the real destination address is
2283 * in the last address part of the
2286 *pap
= oip6h
->ip6_dst
;
2287 for (i
= 1; i
< left
- 1; i
++)
2289 ip6h
->ip6_dst
= *(ap
+ left
- 1);
2291 bcopy(oi_opt
, pi_opt
, ehdrlen
);
2293 rthdr
= (ip6_rthdr0_t
*)pi_opt
;
2294 prev_nexthdr
= (uint8_t *)&rthdr
->ip6r0_nxt
;
2296 case IPPROTO_DSTOPTS
:
2298 * Destination options are tricky. If there is
2299 * a terminal (e.g. non-IPv6-extension) header
2300 * following the destination options, don't
2301 * reset prev_nexthdr or advance the AH insertion
2302 * point and just treat this as a terminal header.
2304 * If this is an inbound packet, just deal with
2307 dsthdr
= (ip6_dest_t
*)oi_opt
;
2309 * XXX I hope common-subexpression elimination
2310 * saves us the double-evaluate.
2312 if (outbound
&& dsthdr
->ip6d_nxt
!= IPPROTO_ROUTING
&&
2313 dsthdr
->ip6d_nxt
!= IPPROTO_HOPOPTS
)
2315 nexthdr
= dsthdr
->ip6d_nxt
;
2316 ehdrlen
= 8 * (dsthdr
->ip6d_len
+ 1);
2317 ret
= ah_fix_tlv_options_v6(oi_opt
, pi_opt
, ehdrlen
,
2318 IPPROTO_DSTOPTS
, copy_always
);
2320 * Return a zero offset indicating error if there
2328 * Be conservative in what you send. We shouldn't
2329 * see two same-scoped AH's in one packet.
2330 * (Inner-IP-scoped AH will be hit by terminal
2331 * header of IP or IPv6.)
2334 return ((uint_t
)(pi_opt
- (uint8_t *)ip6h
));
2338 *prev_nexthdr
= IPPROTO_AH
;
2339 ah
= (ah_t
*)pi_opt
;
2340 ah
->ah_nexthdr
= nexthdr
;
2341 return ((uint_t
)(pi_opt
- (uint8_t *)ip6h
));
2350 ah_finish_up(ah_t
*phdr_ah
, ah_t
*inbound_ah
, ipsa_t
*assoc
,
2351 int ah_data_sz
, int ah_align_sz
, ipsecah_stack_t
*ahstack
)
2358 * 1) Authentication data may have to be padded
2359 * before ICV calculation if ICV is not a multiple
2360 * of 64 bits. This padding is arbitrary and transmitted
2361 * with the packet at the end of the authentication data.
2362 * Payload length should include the padding bytes.
2364 * 2) Explicit padding of the whole datagram may be
2365 * required by the algorithm which need not be
2366 * transmitted. It is assumed that this will be taken
2367 * care by the algorithm module.
2369 bzero(phdr_ah
+ 1, ah_data_sz
); /* Zero out ICV for pseudo-hdr. */
2371 if (inbound_ah
== NULL
) {
2372 /* Outbound AH datagram. */
2374 phdr_ah
->ah_length
= (ah_align_sz
>> 2) + 1;
2375 phdr_ah
->ah_reserved
= 0;
2376 phdr_ah
->ah_spi
= assoc
->ipsa_spi
;
2378 phdr_ah
->ah_replay
=
2379 htonl(atomic_inc_32_nv(&assoc
->ipsa_replay
));
2380 if (phdr_ah
->ah_replay
== 0 && assoc
->ipsa_replay_wsize
!= 0) {
2382 * XXX We have replay counter wrapping. We probably
2383 * want to nuke this SA (and its peer).
2385 ipsec_assocfailure(info
.mi_idnum
, 0, 0,
2386 SL_ERROR
| SL_CONSOLE
| SL_WARN
,
2387 "Outbound AH SA (0x%x), dst %s has wrapped "
2388 "sequence.\n", phdr_ah
->ah_spi
,
2389 assoc
->ipsa_dstaddr
, assoc
->ipsa_addrfam
,
2390 ahstack
->ipsecah_netstack
);
2392 sadb_replay_delete(assoc
);
2393 /* Caller will free phdr_mp and return NULL. */
2397 if (ah_data_sz
!= ah_align_sz
) {
2398 uchar_t
*pad
= ((uchar_t
*)phdr_ah
+ sizeof (ah_t
) +
2401 for (i
= 0; i
< (ah_align_sz
- ah_data_sz
); i
++) {
2402 pad
[i
] = (uchar_t
)i
; /* Fill the padding */
2406 /* Inbound AH datagram. */
2407 phdr_ah
->ah_nexthdr
= inbound_ah
->ah_nexthdr
;
2408 phdr_ah
->ah_length
= inbound_ah
->ah_length
;
2409 phdr_ah
->ah_reserved
= 0;
2410 ASSERT(inbound_ah
->ah_spi
== assoc
->ipsa_spi
);
2411 phdr_ah
->ah_spi
= inbound_ah
->ah_spi
;
2412 phdr_ah
->ah_replay
= inbound_ah
->ah_replay
;
2414 if (ah_data_sz
!= ah_align_sz
) {
2415 uchar_t
*opad
= ((uchar_t
*)inbound_ah
+
2416 sizeof (ah_t
) + ah_data_sz
);
2417 uchar_t
*pad
= ((uchar_t
*)phdr_ah
+ sizeof (ah_t
) +
2420 for (i
= 0; i
< (ah_align_sz
- ah_data_sz
); i
++) {
2421 pad
[i
] = opad
[i
]; /* Copy the padding */
2430 * Called upon failing the inbound ICV check. The message passed as
2431 * argument is freed.
2434 ah_log_bad_auth(mblk_t
*mp
, ip_recv_attr_t
*ira
, ipsec_crypto_t
*ic
)
2436 boolean_t isv4
= (ira
->ira_flags
& IRAF_IS_IPV4
);
2437 ipsa_t
*assoc
= ira
->ira_ipsec_ah_sa
;
2440 netstack_t
*ns
= ira
->ira_ill
->ill_ipst
->ips_netstack
;
2441 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
2442 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
2444 ASSERT(mp
->b_datap
->db_type
== M_DATA
);
2446 mp
->b_rptr
-= ic
->ic_skip_len
;
2449 ipha_t
*ipha
= (ipha_t
*)mp
->b_rptr
;
2450 addr
= &ipha
->ipha_dst
;
2453 ip6_t
*ip6h
= (ip6_t
*)mp
->b_rptr
;
2454 addr
= &ip6h
->ip6_dst
;
2459 * Log the event. Don't print to the console, block
2460 * potential denial-of-service attack.
2462 AH_BUMP_STAT(ahstack
, bad_auth
);
2464 ipsec_assocfailure(info
.mi_idnum
, 0, 0, SL_ERROR
| SL_WARN
,
2465 "AH Authentication failed spi %x, dst_addr %s",
2466 assoc
->ipsa_spi
, addr
, af
, ahstack
->ipsecah_netstack
);
2468 IP_AH_BUMP_STAT(ipss
, in_discards
);
2469 ip_drop_packet(mp
, B_TRUE
, ira
->ira_ill
,
2470 DROPPER(ipss
, ipds_ah_bad_auth
),
2471 &ahstack
->ah_dropper
);
2475 * Kernel crypto framework callback invoked after completion of async
2476 * crypto requests for outbound packets.
2479 ah_kcf_callback_outbound(void *arg
, int status
)
2481 mblk_t
*mp
= (mblk_t
*)arg
;
2484 ipsec_stack_t
*ipss
;
2485 ipsecah_stack_t
*ahstack
;
2487 ip_xmit_attr_t ixas
;
2492 * First remove the ipsec_crypto_t mblk
2493 * Note that we need to ipsec_free_crypto_data(mp) once done with ic.
2495 async_mp
= ipsec_remove_crypto_data(mp
, &ic
);
2496 ASSERT(async_mp
!= NULL
);
2499 * Extract the ip_xmit_attr_t from the first mblk.
2500 * Verifies that the netstack and ill is still around; could
2501 * have vanished while kEf was doing its work.
2502 * On succesful return we have a nce_t and the ill/ipst can't
2503 * disappear until we do the nce_refrele in ixa_cleanup.
2505 data_mp
= async_mp
->b_cont
;
2506 async_mp
->b_cont
= NULL
;
2507 if (!ip_xmit_attr_from_mblk(async_mp
, &ixas
)) {
2508 /* Disappeared on us - no ill/ipst for MIB */
2509 if (ixas
.ixa_nce
!= NULL
) {
2510 ill
= ixas
.ixa_nce
->nce_ill
;
2511 BUMP_MIB(ill
->ill_ip_mib
, ipIfStatsOutDiscards
);
2512 ip_drop_output("ipIfStatsOutDiscards", data_mp
, ill
);
2517 ns
= ixas
.ixa_ipst
->ips_netstack
;
2518 ahstack
= ns
->netstack_ipsecah
;
2519 ipss
= ns
->netstack_ipsec
;
2520 ill
= ixas
.ixa_nce
->nce_ill
;
2522 if (status
== CRYPTO_SUCCESS
) {
2523 data_mp
= ah_auth_out_done(data_mp
, &ixas
, ic
);
2524 if (data_mp
== NULL
)
2527 (void) ip_output_post_ipsec(data_mp
, &ixas
);
2529 /* Outbound shouldn't see invalid MAC */
2530 ASSERT(status
!= CRYPTO_INVALID_MAC
);
2533 ("ah_kcf_callback_outbound: crypto failed with 0x%x\n",
2535 AH_BUMP_STAT(ahstack
, crypto_failures
);
2536 AH_BUMP_STAT(ahstack
, out_discards
);
2538 ip_drop_packet(data_mp
, B_FALSE
, ill
,
2539 DROPPER(ipss
, ipds_ah_crypto_failed
),
2540 &ahstack
->ah_dropper
);
2541 BUMP_MIB(ill
->ill_ip_mib
, ipIfStatsOutDiscards
);
2545 (void) ipsec_free_crypto_data(mp
);
2549 * Kernel crypto framework callback invoked after completion of async
2550 * crypto requests for inbound packets.
2553 ah_kcf_callback_inbound(void *arg
, int status
)
2555 mblk_t
*mp
= (mblk_t
*)arg
;
2558 ipsec_stack_t
*ipss
;
2559 ipsecah_stack_t
*ahstack
;
2561 ip_recv_attr_t iras
;
2565 * First remove the ipsec_crypto_t mblk
2566 * Note that we need to ipsec_free_crypto_data(mp) once done with ic.
2568 async_mp
= ipsec_remove_crypto_data(mp
, &ic
);
2569 ASSERT(async_mp
!= NULL
);
2572 * Extract the ip_xmit_attr_t from the first mblk.
2573 * Verifies that the netstack and ill is still around; could
2574 * have vanished while kEf was doing its work.
2576 data_mp
= async_mp
->b_cont
;
2577 async_mp
->b_cont
= NULL
;
2578 if (!ip_recv_attr_from_mblk(async_mp
, &iras
)) {
2579 /* The ill or ip_stack_t disappeared on us */
2580 ip_drop_input("ip_recv_attr_from_mblk", data_mp
, NULL
);
2584 ns
= iras
.ira_ill
->ill_ipst
->ips_netstack
;
2585 ahstack
= ns
->netstack_ipsecah
;
2586 ipss
= ns
->netstack_ipsec
;
2588 if (status
== CRYPTO_SUCCESS
) {
2589 data_mp
= ah_auth_in_done(data_mp
, &iras
, ic
);
2590 if (data_mp
== NULL
)
2593 /* finish IPsec processing */
2594 ip_input_post_ipsec(data_mp
, &iras
);
2596 } else if (status
== CRYPTO_INVALID_MAC
) {
2597 ah_log_bad_auth(data_mp
, &iras
, ic
);
2600 ("ah_kcf_callback_inbound: crypto failed with 0x%x\n",
2602 AH_BUMP_STAT(ahstack
, crypto_failures
);
2603 IP_AH_BUMP_STAT(ipss
, in_discards
);
2604 ip_drop_packet(data_mp
, B_TRUE
, iras
.ira_ill
,
2605 DROPPER(ipss
, ipds_ah_crypto_failed
),
2606 &ahstack
->ah_dropper
);
2607 BUMP_MIB(iras
.ira_ill
->ill_ip_mib
, ipIfStatsInDiscards
);
2610 ira_cleanup(&iras
, B_TRUE
);
2611 (void) ipsec_free_crypto_data(mp
);
2615 * Invoked on kernel crypto failure during inbound and outbound processing.
2618 ah_crypto_failed(mblk_t
*data_mp
, boolean_t is_inbound
, int kef_rc
,
2619 ill_t
*ill
, ipsecah_stack_t
*ahstack
)
2621 ipsec_stack_t
*ipss
= ahstack
->ipsecah_netstack
->netstack_ipsec
;
2623 ah1dbg(ahstack
, ("crypto failed for %s AH with 0x%x\n",
2624 is_inbound
? "inbound" : "outbound", kef_rc
));
2625 ip_drop_packet(data_mp
, is_inbound
, ill
,
2626 DROPPER(ipss
, ipds_ah_crypto_failed
),
2627 &ahstack
->ah_dropper
);
2628 AH_BUMP_STAT(ahstack
, crypto_failures
);
2630 IP_AH_BUMP_STAT(ipss
, in_discards
);
2632 AH_BUMP_STAT(ahstack
, out_discards
);
2636 * Helper macros for the ah_submit_req_{inbound,outbound}() functions.
2640 * A statement-equivalent macro, _cr MUST point to a modifiable
2641 * crypto_call_req_t.
2643 #define AH_INIT_CALLREQ(_cr, _mp, _callback) \
2644 (_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_ALWAYS_QUEUE; \
2645 (_cr)->cr_callback_arg = (_mp); \
2646 (_cr)->cr_callback_func = (_callback)
2648 #define AH_INIT_CRYPTO_DATA(data, msglen, mblk) { \
2649 (data)->cd_format = CRYPTO_DATA_MBLK; \
2650 (data)->cd_mp = mblk; \
2651 (data)->cd_offset = 0; \
2652 (data)->cd_length = msglen; \
2655 #define AH_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) { \
2656 (mac)->cd_format = CRYPTO_DATA_RAW; \
2657 (mac)->cd_offset = 0; \
2658 (mac)->cd_length = icvlen; \
2659 (mac)->cd_raw.iov_base = icvbuf; \
2660 (mac)->cd_raw.iov_len = icvlen; \
2664 * Submit an inbound packet for processing by the crypto framework.
2667 ah_submit_req_inbound(mblk_t
*phdr_mp
, ip_recv_attr_t
*ira
,
2668 size_t skip_len
, uint32_t ah_offset
, ipsa_t
*assoc
)
2672 crypto_call_req_t call_req
, *callrp
;
2673 uint_t icv_len
= assoc
->ipsa_mac_len
;
2674 crypto_ctx_template_t ctx_tmpl
;
2675 ipsecah_stack_t
*ahstack
;
2676 ipsec_crypto_t
*ic
, icstack
;
2677 boolean_t force
= (assoc
->ipsa_flags
& IPSA_F_ASYNC
);
2679 ahstack
= ira
->ira_ill
->ill_ipst
->ips_netstack
->netstack_ipsecah
;
2681 ASSERT(phdr_mp
!= NULL
);
2682 ASSERT(phdr_mp
->b_datap
->db_type
== M_DATA
);
2685 /* We are doing asynch; allocate mblks to hold state */
2686 if ((mp
= ip_recv_attr_to_mblk(ira
)) == NULL
||
2687 (mp
= ipsec_add_crypto_data(mp
, &ic
)) == NULL
) {
2688 BUMP_MIB(ira
->ira_ill
->ill_ip_mib
, ipIfStatsInDiscards
);
2689 ip_drop_input("ipIfStatsInDiscards", phdr_mp
,
2697 AH_INIT_CALLREQ(callrp
, mp
, ah_kcf_callback_inbound
);
2700 * If we know we are going to do sync then ipsec_crypto_t
2701 * should be on the stack.
2704 bzero(ic
, sizeof (*ic
));
2708 /* init arguments for the crypto framework */
2709 AH_INIT_CRYPTO_DATA(&ic
->ic_crypto_data
, AH_MSGSIZE(phdr_mp
),
2712 AH_INIT_CRYPTO_MAC(&ic
->ic_crypto_mac
, icv_len
,
2713 (char *)phdr_mp
->b_cont
->b_rptr
- skip_len
+ ah_offset
+
2716 ic
->ic_skip_len
= skip_len
;
2718 IPSEC_CTX_TMPL(assoc
, ipsa_authtmpl
, IPSEC_ALG_AUTH
, ctx_tmpl
);
2720 /* call KEF to do the MAC operation */
2721 kef_rc
= crypto_mac_verify(&assoc
->ipsa_amech
,
2722 &ic
->ic_crypto_data
, &assoc
->ipsa_kcfauthkey
, ctx_tmpl
,
2723 &ic
->ic_crypto_mac
, callrp
);
2726 case CRYPTO_SUCCESS
:
2727 AH_BUMP_STAT(ahstack
, crypto_sync
);
2728 phdr_mp
= ah_auth_in_done(phdr_mp
, ira
, ic
);
2730 /* Free mp after we are done with ic */
2731 mp
= ipsec_free_crypto_data(mp
);
2732 (void) ip_recv_attr_free_mblk(mp
);
2736 /* ah_kcf_callback_inbound() will be invoked on completion */
2737 AH_BUMP_STAT(ahstack
, crypto_async
);
2739 case CRYPTO_INVALID_MAC
:
2740 /* Free mp after we are done with ic */
2741 AH_BUMP_STAT(ahstack
, crypto_sync
);
2742 BUMP_MIB(ira
->ira_ill
->ill_ip_mib
, ipIfStatsInDiscards
);
2743 ah_log_bad_auth(phdr_mp
, ira
, ic
);
2744 /* phdr_mp was passed to ip_drop_packet */
2746 mp
= ipsec_free_crypto_data(mp
);
2747 (void) ip_recv_attr_free_mblk(mp
);
2753 mp
= ipsec_free_crypto_data(mp
);
2754 phdr_mp
= ip_recv_attr_free_mblk(mp
);
2756 BUMP_MIB(ira
->ira_ill
->ill_ip_mib
, ipIfStatsInDiscards
);
2757 ah_crypto_failed(phdr_mp
, B_TRUE
, kef_rc
, ira
->ira_ill
, ahstack
);
2758 /* phdr_mp was passed to ip_drop_packet */
2763 * Submit an outbound packet for processing by the crypto framework.
2766 ah_submit_req_outbound(mblk_t
*phdr_mp
, ip_xmit_attr_t
*ixa
,
2767 size_t skip_len
, ipsa_t
*assoc
)
2771 crypto_call_req_t call_req
, *callrp
;
2772 uint_t icv_len
= assoc
->ipsa_mac_len
;
2773 ipsecah_stack_t
*ahstack
;
2774 ipsec_crypto_t
*ic
, icstack
;
2775 ill_t
*ill
= ixa
->ixa_nce
->nce_ill
;
2776 boolean_t force
= (assoc
->ipsa_flags
& IPSA_F_ASYNC
);
2778 ahstack
= ill
->ill_ipst
->ips_netstack
->netstack_ipsecah
;
2780 ASSERT(phdr_mp
!= NULL
);
2781 ASSERT(phdr_mp
->b_datap
->db_type
== M_DATA
);
2784 /* We are doing asynch; allocate mblks to hold state */
2785 if ((mp
= ip_xmit_attr_to_mblk(ixa
)) == NULL
||
2786 (mp
= ipsec_add_crypto_data(mp
, &ic
)) == NULL
) {
2787 BUMP_MIB(ill
->ill_ip_mib
, ipIfStatsOutDiscards
);
2788 ip_drop_output("ipIfStatsOutDiscards", phdr_mp
, ill
);
2794 AH_INIT_CALLREQ(callrp
, mp
, ah_kcf_callback_outbound
);
2797 * If we know we are going to do sync then ipsec_crypto_t
2798 * should be on the stack.
2801 bzero(ic
, sizeof (*ic
));
2805 /* init arguments for the crypto framework */
2806 AH_INIT_CRYPTO_DATA(&ic
->ic_crypto_data
, AH_MSGSIZE(phdr_mp
),
2809 AH_INIT_CRYPTO_MAC(&ic
->ic_crypto_mac
, icv_len
,
2810 (char *)phdr_mp
->b_wptr
);
2812 ic
->ic_skip_len
= skip_len
;
2814 ASSERT(ixa
->ixa_ipsec_ah_sa
!= NULL
);
2816 /* call KEF to do the MAC operation */
2817 kef_rc
= crypto_mac(&assoc
->ipsa_amech
, &ic
->ic_crypto_data
,
2818 &assoc
->ipsa_kcfauthkey
, assoc
->ipsa_authtmpl
,
2819 &ic
->ic_crypto_mac
, callrp
);
2822 case CRYPTO_SUCCESS
:
2823 AH_BUMP_STAT(ahstack
, crypto_sync
);
2824 phdr_mp
= ah_auth_out_done(phdr_mp
, ixa
, ic
);
2826 /* Free mp after we are done with ic */
2827 mp
= ipsec_free_crypto_data(mp
);
2828 (void) ip_xmit_attr_free_mblk(mp
);
2832 /* ah_kcf_callback_outbound() will be invoked on completion */
2833 AH_BUMP_STAT(ahstack
, crypto_async
);
2838 mp
= ipsec_free_crypto_data(mp
);
2839 phdr_mp
= ip_xmit_attr_free_mblk(mp
);
2841 BUMP_MIB(ill
->ill_ip_mib
, ipIfStatsOutDiscards
);
2842 ah_crypto_failed(phdr_mp
, B_FALSE
, kef_rc
, NULL
, ahstack
);
2843 /* phdr_mp was passed to ip_drop_packet */
2848 * This function constructs a pseudo header by looking at the IP header
2849 * and options if any. This is called for both outbound and inbound,
2850 * before computing the ICV.
2853 ah_process_ip_options_v6(mblk_t
*mp
, ipsa_t
*assoc
, int *length_to_skip
,
2854 uint_t ah_data_sz
, boolean_t outbound
, ipsecah_stack_t
*ahstack
)
2865 * Allocate space for the authentication data also. It is
2866 * useful both during the ICV calculation where we need to
2867 * feed in zeroes and while sending the datagram back to IP
2868 * where we will be using the same space.
2870 * We need to allocate space for padding bytes if it is not
2871 * a multiple of IPV6_PADDING_ALIGN.
2873 * In addition, we allocate space for the ICV computed by
2874 * the kernel crypto framework, saving us a separate kmem
2875 * allocation down the road.
2878 ah_align_sz
= P2ALIGN(ah_data_sz
+ IPV6_PADDING_ALIGN
- 1,
2879 IPV6_PADDING_ALIGN
);
2881 ASSERT(ah_align_sz
>= ah_data_sz
);
2883 hdr_size
= ipsec_ah_get_hdr_size_v6(mp
, B_FALSE
);
2884 option_length
= hdr_size
- IPV6_HDR_LEN
;
2886 /* This was not included in ipsec_ah_get_hdr_size_v6() */
2887 hdr_size
+= (sizeof (ah_t
) + ah_align_sz
);
2889 if (!outbound
&& (MBLKL(mp
) < hdr_size
)) {
2891 * We have post-AH header options in a separate mblk,
2892 * a pullup is required.
2894 if (!pullupmsg(mp
, hdr_size
))
2898 if ((phdr_mp
= allocb_tmpl(hdr_size
+ ah_data_sz
, mp
)) == NULL
) {
2902 oip6h
= (ip6_t
*)mp
->b_rptr
;
2905 * Form the basic IP header first. Zero out the header
2906 * so that the mutable fields are zeroed out.
2908 ip6h
= (ip6_t
*)phdr_mp
->b_rptr
;
2909 bzero(ip6h
, sizeof (ip6_t
));
2910 ip6h
->ip6_vcf
= IPV6_DEFAULT_VERS_AND_FLOW
;
2914 * Include the size of AH and authentication data.
2915 * This is how our recipient would compute the
2916 * authentication data. Look at what we do in the
2917 * inbound case below.
2919 ip6h
->ip6_plen
= htons(ntohs(oip6h
->ip6_plen
) +
2920 sizeof (ah_t
) + ah_align_sz
);
2922 ip6h
->ip6_plen
= oip6h
->ip6_plen
;
2925 ip6h
->ip6_src
= oip6h
->ip6_src
;
2926 ip6h
->ip6_dst
= oip6h
->ip6_dst
;
2928 *length_to_skip
= IPV6_HDR_LEN
;
2929 if (option_length
== 0) {
2930 /* Form the AH header */
2931 ip6h
->ip6_nxt
= IPPROTO_AH
;
2932 ((ah_t
*)(ip6h
+ 1))->ah_nexthdr
= oip6h
->ip6_nxt
;
2933 ah_offset
= *length_to_skip
;
2935 ip6h
->ip6_nxt
= oip6h
->ip6_nxt
;
2936 /* option_length does not include the AH header's size */
2937 *length_to_skip
+= option_length
;
2939 ah_offset
= ah_fix_phdr_v6(ip6h
, oip6h
, outbound
, B_FALSE
);
2940 if (ah_offset
== 0) {
2945 if (!ah_finish_up(((ah_t
*)((uint8_t *)ip6h
+ ah_offset
)),
2946 (outbound
? NULL
: ((ah_t
*)((uint8_t *)oip6h
+ ah_offset
))),
2947 assoc
, ah_data_sz
, ah_align_sz
, ahstack
)) {
2950 * Returning NULL will tell the caller to
2951 * IPSA_REFELE(), free the memory, etc.
2956 phdr_mp
->b_wptr
= ((uint8_t *)ip6h
+ ah_offset
+ sizeof (ah_t
) +
2959 *length_to_skip
+= sizeof (ah_t
) + ah_align_sz
;
2964 * This function constructs a pseudo header by looking at the IP header
2965 * and options if any. This is called for both outbound and inbound,
2966 * before computing the ICV.
2969 ah_process_ip_options_v4(mblk_t
*mp
, ipsa_t
*assoc
, int *length_to_skip
,
2970 uint_t ah_data_sz
, boolean_t outbound
, ipsecah_stack_t
*ahstack
)
2973 uint32_t option_length
;
2982 uint32_t v_hlen_tos_len
;
2988 #define V_HLEN (v_hlen_tos_len >> 24)
2990 #define V_HLEN (v_hlen_tos_len & 0xFF)
2993 oipha
= (ipha_t
*)mp
->b_rptr
;
2994 v_hlen_tos_len
= ((uint32_t *)oipha
)[0];
2997 * Allocate space for the authentication data also. It is
2998 * useful both during the ICV calculation where we need to
2999 * feed in zeroes and while sending the datagram back to IP
3000 * where we will be using the same space.
3002 * We need to allocate space for padding bytes if it is not
3003 * a multiple of IPV4_PADDING_ALIGN.
3005 * In addition, we allocate space for the ICV computed by
3006 * the kernel crypto framework, saving us a separate kmem
3007 * allocation down the road.
3010 ah_align_sz
= P2ALIGN(ah_data_sz
+ IPV4_PADDING_ALIGN
- 1,
3011 IPV4_PADDING_ALIGN
);
3013 ASSERT(ah_align_sz
>= ah_data_sz
);
3015 size
= IP_SIMPLE_HDR_LENGTH
+ sizeof (ah_t
) + ah_align_sz
+
3018 if (V_HLEN
!= IP_SIMPLE_HDR_VERSION
) {
3019 option_length
= oipha
->ipha_version_and_hdr_length
-
3020 (uint8_t)((IP_VERSION
<< 4) +
3021 IP_SIMPLE_HDR_LENGTH_IN_WORDS
);
3022 option_length
<<= 2;
3023 size
+= option_length
;
3026 if ((phdr_mp
= allocb_tmpl(size
, mp
)) == NULL
) {
3031 * Form the basic IP header first.
3033 ipha
= (ipha_t
*)phdr_mp
->b_rptr
;
3034 ipha
->ipha_version_and_hdr_length
= oipha
->ipha_version_and_hdr_length
;
3035 ipha
->ipha_type_of_service
= 0;
3039 * Include the size of AH and authentication data.
3040 * This is how our recipient would compute the
3041 * authentication data. Look at what we do in the
3042 * inbound case below.
3044 ipha
->ipha_length
= ntohs(htons(oipha
->ipha_length
) +
3045 sizeof (ah_t
) + ah_align_sz
);
3047 ipha
->ipha_length
= oipha
->ipha_length
;
3050 ipha
->ipha_ident
= oipha
->ipha_ident
;
3051 ipha
->ipha_fragment_offset_and_flags
= 0;
3053 ipha
->ipha_protocol
= IPPROTO_AH
;
3054 ipha
->ipha_hdr_checksum
= 0;
3055 ipha
->ipha_src
= oipha
->ipha_src
;
3056 ipha
->ipha_dst
= dst
= oipha
->ipha_dst
;
3059 * If there is no option to process return now.
3061 ip_hdr_length
= IP_SIMPLE_HDR_LENGTH
;
3063 if (V_HLEN
== IP_SIMPLE_HDR_VERSION
) {
3064 /* Form the AH header */
3068 ip_hdr_length
+= option_length
;
3071 * We have options. In the outbound case for source route,
3072 * ULP has already moved the first hop, which is now in
3073 * ipha_dst. We need the final destination for the calculation
3074 * of authentication data. And also make sure that mutable
3075 * and experimental fields are zeroed out in the IP options.
3078 bcopy(&oipha
[1], &ipha
[1], option_length
);
3080 for (optval
= ipoptp_first(&opts
, ipha
);
3081 optval
!= IPOPT_EOL
;
3082 optval
= ipoptp_next(&opts
)) {
3083 optptr
= opts
.ipoptp_cur
;
3084 optlen
= opts
.ipoptp_len
;
3088 case IPOPT_SECURITY
:
3090 * These options are Immutable, leave them as-is.
3091 * Note that IPOPT_NOP is also Immutable, but it
3092 * was skipped by ipoptp_next() and thus remains
3093 * intact in the header.
3098 if ((opts
.ipoptp_flags
& IPOPTP_ERROR
) != 0)
3101 * These two are mutable and will be zeroed, but
3102 * first get the final destination.
3104 off
= optptr
[IPOPT_OFFSET
];
3106 * If one of the conditions is true, it means
3107 * end of options and dst already has the right
3108 * value. So, just fall through.
3110 if (!(optlen
< IP_ADDR_LEN
|| off
> optlen
- 3)) {
3111 off
= optlen
- IP_ADDR_LEN
;
3112 bcopy(&optptr
[off
], &dst
, IP_ADDR_LEN
);
3120 * optlen should include from the beginning of an
3122 * NOTE : Stream Identifier Option (SID): RFC 791
3123 * shows the bit pattern of optlen as 2 and documents
3124 * the length as 4. We assume it to be 2 here.
3126 bzero(optptr
, optlen
);
3131 if ((opts
.ipoptp_flags
& IPOPTP_ERROR
) != 0) {
3133 ah1dbg(ahstack
, ("AH : bad IPv4 option"));
3139 * Don't change ipha_dst for an inbound datagram as it points
3140 * to the right value. Only for the outbound with LSRR/SSRR,
3141 * because of ip_massage_options called by the ULP, ipha_dst
3142 * points to the first hop and we need to use the final
3143 * destination for computing the ICV.
3147 ipha
->ipha_dst
= dst
;
3149 ((ah_t
*)((uint8_t *)ipha
+ ip_hdr_length
))->ah_nexthdr
=
3150 oipha
->ipha_protocol
;
3151 if (!ah_finish_up(((ah_t
*)((uint8_t *)ipha
+ ip_hdr_length
)),
3152 (outbound
? NULL
: ((ah_t
*)((uint8_t *)oipha
+ ip_hdr_length
))),
3153 assoc
, ah_data_sz
, ah_align_sz
, ahstack
)) {
3156 * Returning NULL will tell the caller to IPSA_REFELE(), free
3162 phdr_mp
->b_wptr
= ((uchar_t
*)ipha
+ ip_hdr_length
+
3163 sizeof (ah_t
) + ah_align_sz
);
3165 ASSERT(phdr_mp
->b_wptr
<= phdr_mp
->b_datap
->db_lim
);
3167 *length_to_skip
= ip_hdr_length
;
3169 *length_to_skip
= ip_hdr_length
+ sizeof (ah_t
) + ah_align_sz
;
3174 * Authenticate an outbound datagram. This function is called
3175 * whenever IP sends an outbound datagram that needs authentication.
3176 * Returns a modified packet if done. Returns NULL if error or queued.
3177 * If error return then ipIfStatsOutDiscards has been increased.
3180 ah_outbound(mblk_t
*data_mp
, ip_xmit_attr_t
*ixa
)
3187 netstack_t
*ns
= ixa
->ixa_ipst
->ips_netstack
;
3188 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
3189 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
3190 ill_t
*ill
= ixa
->ixa_nce
->nce_ill
;
3191 boolean_t need_refrele
= B_FALSE
;
3194 * Construct the chain of mblks
3201 AH_BUMP_STAT(ahstack
, out_requests
);
3203 ASSERT(data_mp
->b_datap
->db_type
== M_DATA
);
3205 assoc
= ixa
->ixa_ipsec_ah_sa
;
3206 ASSERT(assoc
!= NULL
);
3209 * Age SA according to number of bytes that will be sent after
3210 * adding the AH header, ICV, and padding to the packet.
3213 if (ixa
->ixa_flags
& IXAF_IS_IPV4
) {
3214 ipha_t
*ipha
= (ipha_t
*)data_mp
->b_rptr
;
3215 ah_align_sz
= P2ALIGN(assoc
->ipsa_mac_len
+
3216 IPV4_PADDING_ALIGN
- 1, IPV4_PADDING_ALIGN
);
3217 age_bytes
= ntohs(ipha
->ipha_length
) + sizeof (ah_t
) +
3220 ip6_t
*ip6h
= (ip6_t
*)data_mp
->b_rptr
;
3221 ah_align_sz
= P2ALIGN(assoc
->ipsa_mac_len
+
3222 IPV6_PADDING_ALIGN
- 1, IPV6_PADDING_ALIGN
);
3223 age_bytes
= sizeof (ip6_t
) + ntohs(ip6h
->ip6_plen
) +
3224 sizeof (ah_t
) + ah_align_sz
;
3227 if (!ah_age_bytes(assoc
, age_bytes
, B_FALSE
)) {
3228 /* rig things as if ipsec_getassocbyconn() failed */
3229 ipsec_assocfailure(info
.mi_idnum
, 0, 0, SL_ERROR
| SL_WARN
,
3230 "AH association 0x%x, dst %s had bytes expire.\n",
3231 ntohl(assoc
->ipsa_spi
), assoc
->ipsa_dstaddr
, AF_INET
,
3232 ahstack
->ipsecah_netstack
);
3233 BUMP_MIB(ill
->ill_ip_mib
, ipIfStatsOutDiscards
);
3234 ip_drop_output("ipIfStatsOutDiscards", data_mp
, ill
);
3242 * Insert pseudo header:
3243 * [IP, ULP] => [IP, AH, ICV] -> ULP
3246 if (ixa
->ixa_flags
& IXAF_IS_IPV4
) {
3247 phdr_mp
= ah_process_ip_options_v4(data_mp
, assoc
,
3248 &length_to_skip
, assoc
->ipsa_mac_len
, B_TRUE
, ahstack
);
3250 phdr_mp
= ah_process_ip_options_v6(data_mp
, assoc
,
3251 &length_to_skip
, assoc
->ipsa_mac_len
, B_TRUE
, ahstack
);
3254 if (phdr_mp
== NULL
) {
3255 AH_BUMP_STAT(ahstack
, out_discards
);
3256 ip_drop_packet(data_mp
, B_FALSE
, ixa
->ixa_nce
->nce_ill
,
3257 DROPPER(ipss
, ipds_ah_bad_v4_opts
),
3258 &ahstack
->ah_dropper
);
3259 BUMP_MIB(ill
->ill_ip_mib
, ipIfStatsOutDiscards
);
3265 phdr_mp
->b_cont
= data_mp
;
3266 data_mp
->b_rptr
+= length_to_skip
;
3270 * At this point data_mp points to
3271 * an mblk containing the pseudo header (IP header,
3272 * AH header, and ICV with mutable fields zero'ed out).
3273 * mp points to the mblk containing the ULP data. The original
3274 * IP header is kept before the ULP data in data_mp.
3277 /* submit MAC request to KCF */
3278 data_mp
= ah_submit_req_outbound(data_mp
, ixa
, length_to_skip
, assoc
);
3285 ah_inbound(mblk_t
*data_mp
, void *arg
, ip_recv_attr_t
*ira
)
3287 ah_t
*ah
= (ah_t
*)arg
;
3288 ipsa_t
*assoc
= ira
->ira_ipsec_ah_sa
;
3293 netstack_t
*ns
= ira
->ira_ill
->ill_ipst
->ips_netstack
;
3294 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
3295 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
3297 ASSERT(assoc
!= NULL
);
3300 * We may wish to check replay in-range-only here as an optimization.
3301 * Include the reality check of ipsa->ipsa_replay >
3302 * ipsa->ipsa_replay_wsize for times when it's the first N packets,
3303 * where N == ipsa->ipsa_replay_wsize.
3305 * Another check that may come here later is the "collision" check.
3306 * If legitimate packets flow quickly enough, this won't be a problem,
3307 * but collisions may cause authentication algorithm crunching to
3308 * take place when it doesn't need to.
3310 if (!sadb_replay_peek(assoc
, ah
->ah_replay
)) {
3311 AH_BUMP_STAT(ahstack
, replay_early_failures
);
3312 IP_AH_BUMP_STAT(ipss
, in_discards
);
3313 ip_drop_packet(data_mp
, B_TRUE
, ira
->ira_ill
,
3314 DROPPER(ipss
, ipds_ah_early_replay
),
3315 &ahstack
->ah_dropper
);
3316 BUMP_MIB(ira
->ira_ill
->ill_ip_mib
, ipIfStatsInDiscards
);
3321 * The offset of the AH header can be computed from its pointer
3322 * within the data mblk, which was pulled up until the AH header
3323 * by ipsec_inbound_ah_sa() during SA selection.
3325 ah_offset
= (uchar_t
*)ah
- data_mp
->b_rptr
;
3328 * We need to pullup until the ICV before we call
3329 * ah_process_ip_options_v6.
3331 ah_length
= (ah
->ah_length
<< 2) + 8;
3334 * NOTE : If we want to use any field of IP/AH header, you need
3335 * to re-assign following the pullup.
3337 if (((uchar_t
*)ah
+ ah_length
) > data_mp
->b_wptr
) {
3338 if (!pullupmsg(data_mp
, (uchar_t
*)ah
+ ah_length
-
3340 (void) ipsec_rl_strlog(ns
, info
.mi_idnum
, 0, 0,
3342 "ah_inbound: Small AH header\n");
3343 IP_AH_BUMP_STAT(ipss
, in_discards
);
3344 ip_drop_packet(data_mp
, B_TRUE
, ira
->ira_ill
,
3345 DROPPER(ipss
, ipds_ah_nomem
),
3346 &ahstack
->ah_dropper
);
3347 BUMP_MIB(ira
->ira_ill
->ill_ip_mib
, ipIfStatsInDiscards
);
3353 * Insert pseudo header:
3354 * [IP, ULP] => [IP, AH, ICV] -> ULP
3356 if (ira
->ira_flags
& IRAF_IS_IPV4
) {
3357 phdr_mp
= ah_process_ip_options_v4(data_mp
, assoc
,
3358 &length_to_skip
, assoc
->ipsa_mac_len
, B_FALSE
, ahstack
);
3360 phdr_mp
= ah_process_ip_options_v6(data_mp
, assoc
,
3361 &length_to_skip
, assoc
->ipsa_mac_len
, B_FALSE
, ahstack
);
3364 if (phdr_mp
== NULL
) {
3365 IP_AH_BUMP_STAT(ipss
, in_discards
);
3366 ip_drop_packet(data_mp
, B_TRUE
, ira
->ira_ill
,
3367 ((ira
->ira_flags
& IRAF_IS_IPV4
) ?
3368 DROPPER(ipss
, ipds_ah_bad_v4_opts
) :
3369 DROPPER(ipss
, ipds_ah_bad_v6_hdrs
)),
3370 &ahstack
->ah_dropper
);
3371 BUMP_MIB(ira
->ira_ill
->ill_ip_mib
, ipIfStatsInDiscards
);
3375 phdr_mp
->b_cont
= data_mp
;
3376 data_mp
->b_rptr
+= length_to_skip
;
3379 /* submit request to KCF */
3380 return (ah_submit_req_inbound(data_mp
, ira
, length_to_skip
, ah_offset
,
3385 * Invoked after processing of an inbound packet by the
3386 * kernel crypto framework. Called by ah_submit_req() for a sync request,
3387 * or by the kcf callback for an async request.
3388 * Returns NULL if the mblk chain is consumed.
3391 ah_auth_in_done(mblk_t
*phdr_mp
, ip_recv_attr_t
*ira
, ipsec_crypto_t
*ic
)
3394 uint_t ah_offset
= 0;
3396 int align_len
, newpos
;
3405 kstat_named_t
*counter
;
3406 netstack_t
*ns
= ira
->ira_ill
->ill_ipst
->ips_netstack
;
3407 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
3408 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
3410 isv4
= (ira
->ira_flags
& IRAF_IS_IPV4
);
3411 assoc
= ira
->ira_ipsec_ah_sa
;
3412 icv_len
= (uint_t
)ic
->ic_crypto_mac
.cd_raw
.iov_len
;
3414 if (phdr_mp
== NULL
) {
3415 ip_drop_packet(phdr_mp
, B_TRUE
, ira
->ira_ill
,
3416 DROPPER(ipss
, ipds_ah_nomem
),
3417 &ahstack
->ah_dropper
);
3418 BUMP_MIB(ira
->ira_ill
->ill_ip_mib
, ipIfStatsInDiscards
);
3422 mp
= phdr_mp
->b_cont
;
3424 ip_drop_packet(phdr_mp
, B_TRUE
, ira
->ira_ill
,
3425 DROPPER(ipss
, ipds_ah_nomem
),
3426 &ahstack
->ah_dropper
);
3427 BUMP_MIB(ira
->ira_ill
->ill_ip_mib
, ipIfStatsInDiscards
);
3430 mp
->b_rptr
-= ic
->ic_skip_len
;
3432 ah_set_usetime(assoc
, B_TRUE
);
3435 ipha
= (ipha_t
*)mp
->b_rptr
;
3436 ah_offset
= ipha
->ipha_version_and_hdr_length
-
3437 (uint8_t)((IP_VERSION
<< 4));
3439 align_len
= P2ALIGN(icv_len
+ IPV4_PADDING_ALIGN
- 1,
3440 IPV4_PADDING_ALIGN
);
3442 ip6h
= (ip6_t
*)mp
->b_rptr
;
3443 ah_offset
= ipsec_ah_get_hdr_size_v6(mp
, B_TRUE
);
3444 ASSERT((mp
->b_wptr
- mp
->b_rptr
) >= ah_offset
);
3445 align_len
= P2ALIGN(icv_len
+ IPV6_PADDING_ALIGN
- 1,
3446 IPV6_PADDING_ALIGN
);
3449 ah
= (ah_t
*)(mp
->b_rptr
+ ah_offset
);
3450 newpos
= sizeof (ah_t
) + align_len
;
3453 * We get here only when authentication passed.
3456 ah3dbg(ahstack
, ("AH succeeded, checking replay\n"));
3457 AH_BUMP_STAT(ahstack
, good_auth
);
3459 if (!sadb_replay_check(assoc
, ah
->ah_replay
)) {
3464 addr
= &ipha
->ipha_dst
;
3467 addr
= &ip6h
->ip6_dst
;
3472 * Log the event. As of now we print out an event.
3473 * Do not print the replay failure number, or else
3474 * syslog cannot collate the error messages. Printing
3475 * the replay number that failed (or printing to the
3476 * console) opens a denial-of-service attack.
3478 AH_BUMP_STAT(ahstack
, replay_failures
);
3479 ipsec_assocfailure(info
.mi_idnum
, 0, 0,
3481 "Replay failed for AH spi %x, dst_addr %s",
3482 assoc
->ipsa_spi
, addr
, af
, ahstack
->ipsecah_netstack
);
3483 counter
= DROPPER(ipss
, ipds_ah_replay
);
3488 * We need to remove the AH header from the original
3489 * datagram. Best way to do this is to move the pre-AH headers
3490 * forward in the (relatively simple) IPv4 case. In IPv6, it's
3491 * a bit more complicated because of IPv6's next-header chaining,
3496 * Assign the right protocol, adjust the length as we
3497 * are removing the AH header and adjust the checksum to
3498 * account for the protocol and length.
3500 length
= ntohs(ipha
->ipha_length
);
3501 if (!ah_age_bytes(assoc
, length
, B_TRUE
)) {
3502 /* The ipsa has hit hard expiration, LOG and AUDIT. */
3503 ipsec_assocfailure(info
.mi_idnum
, 0, 0,
3505 "AH Association 0x%x, dst %s had bytes expire.\n",
3506 assoc
->ipsa_spi
, assoc
->ipsa_dstaddr
,
3507 AF_INET
, ahstack
->ipsecah_netstack
);
3508 AH_BUMP_STAT(ahstack
, bytes_expired
);
3509 counter
= DROPPER(ipss
, ipds_ah_bytes_expire
);
3512 ipha
->ipha_protocol
= ah
->ah_nexthdr
;
3515 ipha
->ipha_length
= htons((uint16_t)length
);
3516 ipha
->ipha_hdr_checksum
= 0;
3517 ipha
->ipha_hdr_checksum
= (uint16_t)ip_csum_hdr(ipha
);
3524 ip6_rthdr0_t
*rthdr
;
3527 * Make phdr_mp hold until the AH header and make
3528 * mp hold everything past AH header.
3530 length
= ntohs(ip6h
->ip6_plen
);
3531 if (!ah_age_bytes(assoc
, length
+ sizeof (ip6_t
), B_TRUE
)) {
3532 /* The ipsa has hit hard expiration, LOG and AUDIT. */
3533 ipsec_assocfailure(info
.mi_idnum
, 0, 0,
3535 "AH Association 0x%x, dst %s had bytes "
3536 "expire.\n", assoc
->ipsa_spi
, &ip6h
->ip6_dst
,
3537 AF_INET6
, ahstack
->ipsecah_netstack
);
3538 AH_BUMP_STAT(ahstack
, bytes_expired
);
3539 counter
= DROPPER(ipss
, ipds_ah_bytes_expire
);
3544 * Update the next header field of the header preceding
3545 * AH with the next header field of AH. Start with the
3546 * IPv6 header and proceed with the extension headers
3547 * until we find what we're looking for.
3549 nexthdr
= &ip6h
->ip6_nxt
;
3550 whereptr
= (uchar_t
*)ip6h
;
3551 hdrlen
= sizeof (ip6_t
);
3553 while (*nexthdr
!= IPPROTO_AH
) {
3555 /* Assume IP has already stripped it */
3556 ASSERT(*nexthdr
!= IPPROTO_FRAGMENT
);
3558 case IPPROTO_HOPOPTS
:
3559 hbhhdr
= (ip6_hbh_t
*)whereptr
;
3560 nexthdr
= &hbhhdr
->ip6h_nxt
;
3561 hdrlen
= 8 * (hbhhdr
->ip6h_len
+ 1);
3563 case IPPROTO_DSTOPTS
:
3564 dsthdr
= (ip6_dest_t
*)whereptr
;
3565 nexthdr
= &dsthdr
->ip6d_nxt
;
3566 hdrlen
= 8 * (dsthdr
->ip6d_len
+ 1);
3568 case IPPROTO_ROUTING
:
3569 rthdr
= (ip6_rthdr0_t
*)whereptr
;
3570 nexthdr
= &rthdr
->ip6r0_nxt
;
3571 hdrlen
= 8 * (rthdr
->ip6r0_len
+ 1);
3575 *nexthdr
= ah
->ah_nexthdr
;
3577 ip6h
->ip6_plen
= htons((uint16_t)length
);
3580 /* Now that we've fixed the IP header, move it forward. */
3581 mp
->b_rptr
+= newpos
;
3582 if (IS_P2ALIGNED(mp
->b_rptr
, sizeof (uint32_t))) {
3583 dest32
= (uint32_t *)(mp
->b_rptr
+ ah_offset
);
3584 while (--dest32
>= (uint32_t *)mp
->b_rptr
)
3585 *dest32
= *(dest32
- (newpos
>> 2));
3587 dest
= mp
->b_rptr
+ ah_offset
;
3588 while (--dest
>= mp
->b_rptr
)
3589 *dest
= *(dest
- newpos
);
3593 if (assoc
->ipsa_state
== IPSA_STATE_IDLE
) {
3595 * Cluster buffering case. Tell caller that we're
3596 * handling the packet.
3598 sadb_buf_pkt(assoc
, mp
, ira
);
3605 IP_AH_BUMP_STAT(ipss
, in_discards
);
3606 ip_drop_packet(phdr_mp
, B_TRUE
, ira
->ira_ill
, counter
,
3607 &ahstack
->ah_dropper
);
3608 BUMP_MIB(ira
->ira_ill
->ill_ip_mib
, ipIfStatsInDiscards
);
3613 * Invoked after processing of an outbound packet by the
3614 * kernel crypto framework, either by ah_submit_req() for a request
3615 * executed syncrhonously, or by the KEF callback for a request
3616 * executed asynchronously.
3619 ah_auth_out_done(mblk_t
*phdr_mp
, ip_xmit_attr_t
*ixa
, ipsec_crypto_t
*ic
)
3623 uint32_t hdrs_length
;
3628 netstack_t
*ns
= ixa
->ixa_ipst
->ips_netstack
;
3629 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
3630 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
3631 ill_t
*ill
= ixa
->ixa_nce
->nce_ill
;
3633 isv4
= (ixa
->ixa_flags
& IXAF_IS_IPV4
);
3634 icv_len
= ic
->ic_crypto_mac
.cd_raw
.iov_len
;
3636 mp
= phdr_mp
->b_cont
;
3638 ip_drop_packet(phdr_mp
, B_FALSE
, ill
,
3639 DROPPER(ipss
, ipds_ah_nomem
),
3640 &ahstack
->ah_dropper
);
3641 BUMP_MIB(ill
->ill_ip_mib
, ipIfStatsOutDiscards
);
3644 mp
->b_rptr
-= ic
->ic_skip_len
;
3646 ASSERT(ixa
->ixa_flags
& IXAF_IPSEC_SECURE
);
3647 ASSERT(ixa
->ixa_ipsec_ah_sa
!= NULL
);
3648 ah_set_usetime(ixa
->ixa_ipsec_ah_sa
, B_FALSE
);
3654 ipha
= (ipha_t
*)mp
->b_rptr
;
3655 hdrs_length
= ipha
->ipha_version_and_hdr_length
-
3656 (uint8_t)((IP_VERSION
<< 4));
3658 align_len
= P2ALIGN(icv_len
+ IPV4_PADDING_ALIGN
- 1,
3659 IPV4_PADDING_ALIGN
);
3661 * phdr_mp must have the right amount of space for the
3662 * combined IP and AH header. Copy the IP header and
3663 * the ack_data onto AH. Note that the AH header was
3664 * already formed before the ICV calculation and hence
3665 * you don't have to copy it here.
3667 bcopy(mp
->b_rptr
, phdr_mp
->b_rptr
, hdrs_length
);
3669 ptr
= phdr_mp
->b_rptr
+ hdrs_length
+ sizeof (ah_t
);
3670 bcopy(phdr_mp
->b_wptr
, ptr
, icv_len
);
3673 * Compute the new header checksum as we are assigning
3674 * IPPROTO_AH and adjusting the length here.
3676 nipha
= (ipha_t
*)phdr_mp
->b_rptr
;
3678 nipha
->ipha_protocol
= IPPROTO_AH
;
3679 length
= ntohs(nipha
->ipha_length
);
3680 length
+= (sizeof (ah_t
) + align_len
);
3681 nipha
->ipha_length
= htons((uint16_t)length
);
3682 nipha
->ipha_hdr_checksum
= 0;
3683 nipha
->ipha_hdr_checksum
= (uint16_t)ip_csum_hdr(nipha
);
3689 ip6h
= (ip6_t
*)mp
->b_rptr
;
3690 nip6h
= (ip6_t
*)phdr_mp
->b_rptr
;
3691 align_len
= P2ALIGN(icv_len
+ IPV6_PADDING_ALIGN
- 1,
3692 IPV6_PADDING_ALIGN
);
3694 * phdr_mp must have the right amount of space for the
3695 * combined IP and AH header. Copy the IP header with
3696 * options into the pseudo header. When we constructed
3697 * a pseudo header, we did not copy some of the mutable
3698 * fields. We do it now by calling ah_fix_phdr_v6()
3699 * with the last argument B_TRUE. It returns the
3700 * ah_offset into the pseudo header.
3703 bcopy(ip6h
, nip6h
, IPV6_HDR_LEN
);
3704 ah_offset
= ah_fix_phdr_v6(nip6h
, ip6h
, B_TRUE
, B_TRUE
);
3705 ASSERT(ah_offset
!= 0);
3707 * phdr_mp can hold exactly the whole IP header with options
3708 * plus the AH header also. Thus subtracting the AH header's
3709 * size should give exactly how much of the original header
3710 * should be skipped.
3712 hdrs_length
= (phdr_mp
->b_wptr
- phdr_mp
->b_rptr
) -
3713 sizeof (ah_t
) - icv_len
;
3714 bcopy(phdr_mp
->b_wptr
, ((uint8_t *)nip6h
+ ah_offset
+
3715 sizeof (ah_t
)), icv_len
);
3716 length
= ntohs(nip6h
->ip6_plen
);
3717 length
+= (sizeof (ah_t
) + align_len
);
3718 nip6h
->ip6_plen
= htons((uint16_t)length
);
3721 /* Skip the original IP header */
3722 mp
->b_rptr
+= hdrs_length
;
3723 if (mp
->b_rptr
== mp
->b_wptr
) {
3724 phdr_mp
->b_cont
= mp
->b_cont
;
3733 * Wrapper to allow IP to trigger an AH association failure message
3734 * during SA inbound selection.
3737 ipsecah_in_assocfailure(mblk_t
*mp
, char level
, ushort_t sl
, char *fmt
,
3738 uint32_t spi
, void *addr
, int af
, ip_recv_attr_t
*ira
)
3740 netstack_t
*ns
= ira
->ira_ill
->ill_ipst
->ips_netstack
;
3741 ipsecah_stack_t
*ahstack
= ns
->netstack_ipsecah
;
3742 ipsec_stack_t
*ipss
= ns
->netstack_ipsec
;
3744 if (ahstack
->ipsecah_log_unknown_spi
) {
3745 ipsec_assocfailure(info
.mi_idnum
, 0, level
, sl
, fmt
, spi
,
3746 addr
, af
, ahstack
->ipsecah_netstack
);
3749 ip_drop_packet(mp
, B_TRUE
, ira
->ira_ill
,
3750 DROPPER(ipss
, ipds_ah_no_sa
),
3751 &ahstack
->ah_dropper
);
3755 * Initialize the AH input and output processing functions.
3758 ipsecah_init_funcs(ipsa_t
*sa
)
3760 if (sa
->ipsa_output_func
== NULL
)
3761 sa
->ipsa_output_func
= ah_outbound
;
3762 if (sa
->ipsa_input_func
== NULL
)
3763 sa
->ipsa_input_func
= ah_inbound
;