Merge commit '281819e5f8b19cd8627541a22d261906fd190276' into merges
[unleashed.git] / kernel / net / ip / ipsecah.c
blob756a6566970206745901705834e298fe183a6032
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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>
35 #include <sys/ddi.h>
36 #include <sys/sunddi.h>
37 #include <sys/mkdev.h>
38 #include <sys/kmem.h>
39 #include <sys/zone.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>
48 #include <net/if.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>
55 #include <inet/mi.h>
56 #include <inet/ip.h>
57 #include <inet/ip6.h>
58 #include <inet/nd.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
117 * mblk in the chain.
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 *,
138 cred_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,
153 NULL
156 static struct qinit winit = {
157 (pfi_t)ipsecah_wput, NULL, ipsecah_open, ipsecah_close, NULL, &info,
158 NULL
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;
169 static boolean_t
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,
176 stackid);
178 if (ahstack->ah_ksp == NULL || ahstack->ah_ksp->ks_data == NULL)
179 return (B_FALSE);
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)
189 KI(num_aalgs);
190 KI(good_auth);
191 KI(bad_auth);
192 KI(replay_failures);
193 KI(replay_early_failures);
194 KI(keysock_in);
195 KI(out_requests);
196 KI(acquire_requests);
197 KI(bytes_expired);
198 KI(out_discards);
199 KI(crypto_sync);
200 KI(crypto_async);
201 KI(crypto_failures);
203 #undef KI
204 #undef K64
206 kstat_install(ahstack->ah_ksp);
207 IP_ACQUIRE_STAT(ipss, maxpackets, ipsacq_maxpackets);
208 return (B_TRUE);
211 static int
212 ah_kstat_update(kstat_t *kp, int rw)
214 ah_kstats_t *ekp;
215 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private;
216 netstack_t *ns;
217 ipsec_stack_t *ipss;
219 if ((kp == NULL) || (kp->ks_data == NULL))
220 return (EIO);
222 if (rw == KSTAT_WRITE)
223 return (EACCES);
225 ns = netstack_find_by_stackid(stackid);
226 if (ns == NULL)
227 return (-1);
228 ipss = ns->netstack_ipsec;
229 if (ipss == NULL) {
230 netstack_rele(ns);
231 return (-1);
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);
239 netstack_rele(ns);
240 return (0);
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
246 * ah_pfkey_q.
248 static void
249 ah_ager(void *arg)
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,
261 ah_ager, ahstack,
262 &ahstack->ipsecah_age_interval, ahstack->ipsecah_age_int_max,
263 info.mi_idnum);
267 * Get an AH NDD parameter.
269 /* ARGSUSED */
270 static int
271 ipsecah_param_get(
272 queue_t *q,
273 mblk_t *mp,
274 caddr_t cp,
275 cred_t *cr)
277 ipsecahparam_t *ipsecahpa = (ipsecahparam_t *)cp;
278 uint_t value;
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);
286 return (0);
290 * This routine sets an NDD variable in a ipsecahparam_t structure.
292 /* ARGSUSED */
293 static int
294 ipsecah_param_set(
295 queue_t *q,
296 mblk_t *mp,
297 char *value,
298 caddr_t cp,
299 cred_t *cr)
301 ulong_t new_value;
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
307 * required bounds.
309 if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
310 new_value < ipsecahpa->ipsecah_param_min ||
311 new_value > ipsecahpa->ipsecah_param_max) {
312 return (EINVAL);
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);
319 return (0);
323 * Using lifetime NDD variables, fill in an extended combination's
324 * lifetime information.
326 void
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.
346 boolean_t
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,
358 ipsecah_stack_fini);
360 return (B_TRUE);
364 * Walk through the param array specified registering each element with the
365 * named dispatch handler.
367 static boolean_t
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]) {
373 if (!nd_load(ndp,
374 ahp->ipsecah_param_name,
375 ipsecah_param_get, ipsecah_param_set,
376 (caddr_t)ahp)) {
377 nd_free(ndp);
378 return (B_FALSE);
382 return (B_TRUE);
386 * Initialize things for AH for each stack instance
388 static void *
389 ipsecah_stack_init(netstackid_t stackid, netstack_t *ns)
391 ipsecah_stack_t *ahstack;
392 ipsecahparam_t *ahp;
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");
413 return (ahstack);
417 * Destroy things for AH at module unload time.
419 void
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?
429 static void
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).
461 /* ARGSUSED */
462 static int
463 ipsecah_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
465 netstack_t *ns;
466 ipsecah_stack_t *ahstack;
468 if (secpolicy_ip_config(credp, B_FALSE) != 0)
469 return (EPERM);
471 if (q->q_ptr != NULL)
472 return (0); /* Re-open of an already open instance. */
474 if (sflag != MODOPEN)
475 return (EINVAL);
477 ns = netstack_find_by_cred(credp);
478 ASSERT(ns != NULL);
479 ahstack = ns->netstack_ipsecah;
480 ASSERT(ahstack != NULL);
482 q->q_ptr = ahstack;
483 WR(q)->q_ptr = q->q_ptr;
485 qprocson(q);
486 return (0);
490 * AH module close routine.
492 static int
493 ipsecah_close(queue_t *q)
495 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)q->q_ptr;
498 * Clean up q_ptr, if needed.
500 qprocsoff(q);
502 /* Keysock queue check is safe, because of OCEXCL perimeter. */
504 if (q == ahstack->ah_pfkey_q) {
505 ah1dbg(ahstack,
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);
513 return (0);
517 * Construct an SADB_REGISTER message with the current algorithms.
519 static boolean_t
520 ah_register_out(uint32_t sequence, uint32_t pid, uint_t serial,
521 ipsecah_stack_t *ahstack, cred_t *cr)
523 mblk_t *mp;
524 boolean_t rc = B_TRUE;
525 sadb_msg_t *samsg;
526 sadb_supported_t *sasupp;
527 sadb_alg_t *saalg;
528 uint_t allocsize = sizeof (*samsg);
529 uint_t i, numalgs_snap;
530 ipsec_alginfo_t **authalgs;
531 uint_t num_aalgs;
532 ipsec_stack_t *ipss = ahstack->ipsecah_netstack->netstack_ipsec;
533 sadb_ext_t *nextext;
535 /* Allocate the KEYSOCK_OUT. */
536 mp = sadb_keysock_out(serial);
537 if (mp == NULL) {
538 ah0dbg(("ah_register_out: couldn't allocate mblk.\n"));
539 return (B_FALSE);
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
553 * in the table.
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]))
558 num_aalgs++;
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);
571 freemsg(mp);
572 return (B_FALSE);
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);
583 numalgs_snap = 0;
584 for (i = 0;
585 ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs));
586 i++) {
587 if (authalgs[i] == NULL || !ALG_VALID(authalgs[i]))
588 continue;
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);
600 numalgs_snap++;
601 saalg++;
603 ASSERT(numalgs_snap == num_aalgs);
604 #ifdef DEBUG
606 * Reality check to make sure I snagged all of the
607 * algorithms.
609 for (; i < IPSEC_MAX_ALGS; i++)
610 if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
611 cmn_err(CE_PANIC,
612 "ah_register_out()! Missed #%d.\n", i);
613 #endif /* DEBUG */
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);
645 else {
646 rc = B_FALSE;
647 freemsg(mp);
650 return (rc);
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.
658 void
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.
674 static void
675 inbound_task(void *arg)
677 mblk_t *mp = (mblk_t *)arg;
678 mblk_t *async_mp;
679 ip_recv_attr_t iras;
681 async_mp = mp;
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);
687 freemsg(mp);
688 goto done;
691 ah_inbound_restart(mp, &iras);
692 done:
693 ira_cleanup(&iras, B_TRUE);
697 * Restart ESP after the SA has been added.
699 static void
700 ah_inbound_restart(mblk_t *mp, ip_recv_attr_t *ira)
702 ah_t *ah;
703 netstack_t *ns;
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);
711 if (mp == NULL)
712 return;
714 ASSERT(ah != NULL);
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);
719 if (mp == NULL) {
721 * Either it failed or is pending. In the former case
722 * ipIfStatsInDiscards was increased.
724 return;
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.
733 static int
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];
740 ipsa_t *larval;
741 ipsacq_t *acqrec;
742 iacqf_t *acq_bucket;
743 mblk_t *acq_msgs = NULL;
744 mblk_t *lpkt;
745 int rc;
746 ipsa_query_t sq;
747 int error;
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,
758 &sq, diagnostic);
759 if (error)
760 return (error);
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;
771 is_inbound = B_TRUE;
772 if (assoc->sadb_sa_flags & IPSA_F_OUTBOUND)
773 clone = B_TRUE;
774 } else {
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;
789 /* FALLTHRU */
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).
798 case KS_IN_ADDR_ME:
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)
803 clone = B_TRUE;
804 is_inbound = B_TRUE;
805 break;
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
812 * SA.
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;
820 clone = B_TRUE;
822 break;
823 default:
824 *diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
825 return (EINVAL);
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))
851 break;
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).
874 larval = NULL;
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"));
889 return (ESRCH);
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) {
906 if (rc == 0) {
907 lpkt = sadb_clear_lpkt(larval);
908 if (lpkt != NULL) {
909 rc = !taskq_dispatch(ah_taskq, inbound_task,
910 lpkt, TQ_NOSLEEP);
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) {
923 mblk_t *asyncmp;
924 mblk_t *data_mp;
925 ip_xmit_attr_t ixas;
926 ill_t *ill;
928 asyncmp = acq_msgs;
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);
953 } else {
954 ah_outbound_finish(data_mp, &ixas);
956 ixa_cleanup(&ixas);
959 return (rc);
964 * Process one of the queued messages (from ipsacq_mp) once the SA
965 * has been added.
967 static void
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);
981 return;
984 data_mp = ah_outbound(data_mp, ixa);
985 if (data_mp == NULL)
986 return;
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.
995 static int
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;
1023 return (EINVAL);
1025 if (dstext == NULL) {
1026 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
1027 return (EINVAL);
1029 if (isrcext == NULL && idstext != NULL) {
1030 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC;
1031 return (EINVAL);
1033 if (isrcext != NULL && idstext == NULL) {
1034 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST;
1035 return (EINVAL);
1037 if (assoc == NULL) {
1038 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
1039 return (EINVAL);
1041 if (key == NULL) {
1042 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_AKEY;
1043 return (EINVAL);
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;
1055 return (EINVAL);
1057 if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
1058 *diagnostic = SADB_X_DIAGNOSTIC_ENCR_NOTSUPP;
1059 return (EINVAL);
1061 if (assoc->sadb_sa_flags & ~ahstack->ah_sadb.s_addflags) {
1062 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS;
1063 return (EINVAL);
1065 if ((*diagnostic = sadb_hardsoftchk(hard, soft, idle)) != 0)
1066 return (EINVAL);
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;
1095 return (EINVAL);
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;
1103 return (EINVAL);
1106 /* check key and fix parity if needed */
1107 if (ipsec_check_key(aalg->alg_mech_type, key, B_TRUE,
1108 diagnostic) != 0) {
1109 rw_exit(&ipss->ipsec_alg_lock);
1110 return (EINVAL);
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));
1119 /* Refactor me */
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.
1125 static int
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];
1132 mblk_t *buf_pkt;
1133 int rcode;
1135 if (dstext == NULL) {
1136 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
1137 return (EINVAL);
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) ||
1145 (rcode != 0)) {
1146 return (rcode);
1149 HANDLE_BUF_PKT(ah_taskq, ahstack->ipsecah_netstack->netstack_ipsec,
1150 ahstack->ah_dropper, buf_pkt);
1152 return (rcode);
1155 /* Refactor me */
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.
1160 static int
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) {
1172 if (dstext != NULL)
1173 sin = (struct sockaddr_in *)(dstext + 1);
1174 else if (srcext != NULL)
1175 sin = (struct sockaddr_in *)(srcext + 1);
1176 else {
1177 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
1178 return (EINVAL);
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));
1189 /* Refactor me */
1191 * Convert the entire contents of all of AH's SA tables into PF_KEY SADB_DUMP
1192 * messages.
1194 static void
1195 ah_dump(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
1197 int error;
1198 sadb_msg_t *samsg;
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);
1205 if (error != 0)
1206 goto bail;
1208 error = sadb_dump(ahstack->ah_pfkey_q, mp, ksi, &ahstack->ah_sadb.s_v6);
1209 bail:
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.
1220 static boolean_t
1221 ah_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi,
1222 ipsecah_stack_t *ahstack)
1224 int diagnostic;
1226 if (mp->b_cont == NULL) {
1227 freemsg(mp);
1228 return (B_TRUE);
1231 if (ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT] != NULL) {
1232 diagnostic = SADB_X_DIAGNOSTIC_EKEY_PRESENT;
1233 goto badmsg;
1235 if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) {
1236 diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT;
1237 goto badmsg;
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;
1242 goto badmsg;
1244 return (B_FALSE); /* False ==> no failures */
1246 badmsg:
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.
1263 static void
1264 ah_parse_pfkey(mblk_t *mp, ipsecah_stack_t *ahstack)
1266 mblk_t *msg = mp->b_cont;
1267 sadb_msg_t *samsg;
1268 keysock_in_t *ksi;
1269 int error;
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
1279 * AF_INET.
1281 if (!sadb_addrfix(ksi, ahstack->ah_pfkey_q, mp,
1282 ahstack->ipsecah_netstack) ||
1283 ah_pfkey_reality_failures(mp, ksi, ahstack)) {
1284 return;
1287 switch (samsg->sadb_msg_type) {
1288 case SADB_ADD:
1289 error = ah_add_sa(mp, ksi, &diagnostic,
1290 ahstack->ipsecah_netstack);
1291 if (error != 0) {
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. */
1296 break;
1297 case SADB_DELETE:
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);
1302 if (error != 0) {
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. */
1307 break;
1308 case SADB_GET:
1309 error = sadb_delget_sa(mp, ksi, &ahstack->ah_sadb, &diagnostic,
1310 ahstack->ah_pfkey_q, samsg->sadb_msg_type);
1311 if (error != 0) {
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. */
1316 break;
1317 case SADB_FLUSH:
1318 sadbp_flush(&ahstack->ah_sadb, ahstack->ipsecah_netstack);
1319 sadb_pfkey_echo(ahstack->ah_pfkey_q, mp, samsg, ksi, NULL);
1320 break;
1321 case SADB_REGISTER:
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))) {
1331 freemsg(mp);
1332 } else {
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);
1341 break;
1342 case SADB_UPDATE:
1343 case SADB_X_UPDATEPAIR:
1345 * Find a larval, if not there, find a full one and get
1346 * strict.
1348 error = ah_update_sa(mp, ksi, &diagnostic, ahstack,
1349 samsg->sadb_msg_type);
1350 if (error != 0) {
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. */
1355 break;
1356 case SADB_GETSPI:
1358 * Reserve a new larval entry.
1360 ah_getspi(mp, ksi, ahstack);
1361 break;
1362 case SADB_ACQUIRE:
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);
1370 freemsg(mp);
1371 break;
1372 case SADB_DUMP:
1374 * Dump all entries.
1376 ah_dump(mp, ksi, ahstack);
1377 /* ah_dump will take care of the return message, etc. */
1378 break;
1379 case SADB_EXPIRE:
1380 /* Should never reach me. */
1381 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EOPNOTSUPP,
1382 diagnostic, ksi->ks_in_serial);
1383 break;
1384 default:
1385 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL,
1386 SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial);
1387 break;
1392 * Handle case where PF_KEY says it can't find a keysock for one of my
1393 * ACQUIRE messages.
1395 static void
1396 ah_keysock_no_socket(mblk_t *mp, ipsecah_stack_t *ahstack)
1398 sadb_msg_t *samsg;
1399 keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr;
1401 if (mp->b_cont == NULL) {
1402 freemsg(mp);
1403 return;
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);
1421 freemsg(mp);
1425 * AH module write put routine.
1427 static void
1428 ipsecah_wput(queue_t *q, mblk_t *mp)
1430 ipsec_info_t *ii;
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) {
1438 case M_CTL:
1439 if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) {
1440 /* Not big enough message. */
1441 freemsg(mp);
1442 break;
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);
1450 break;
1451 case KEYSOCK_IN:
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);
1457 break;
1458 case KEYSOCK_HELLO:
1459 sadb_keysock_hello(&ahstack->ah_pfkey_q, q, mp,
1460 ah_ager, (void *)ahstack, &ahstack->ah_event,
1461 SADB_SATYPE_AH);
1462 break;
1463 default:
1464 ah1dbg(ahstack, ("Got M_CTL from above of 0x%x.\n",
1465 ii->ipsec_info_type));
1466 freemsg(mp);
1467 break;
1469 break;
1470 case M_IOCTL:
1471 iocp = (struct iocblk *)mp->b_rptr;
1472 switch (iocp->ioc_cmd) {
1473 case ND_SET:
1474 case ND_GET:
1475 if (nd_getset(q, ahstack->ipsecah_g_nd, mp)) {
1476 qreply(q, mp);
1477 return;
1478 } else {
1479 iocp->ioc_error = ENOENT;
1481 /* FALLTHRU */
1482 default:
1483 /* We really don't support any other ioctls, do we? */
1485 /* Return EINVAL */
1486 if (iocp->ioc_error != ENOENT)
1487 iocp->ioc_error = EINVAL;
1488 iocp->ioc_count = 0;
1489 mp->b_datap->db_type = M_IOCACK;
1490 qreply(q, mp);
1491 return;
1493 default:
1494 ah3dbg(ahstack,
1495 ("Got default message, type %d, passing to IP.\n",
1496 mp->b_datap->db_type));
1497 putnext(q, mp);
1501 /* Refactor me */
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.
1509 static void
1510 ah_set_usetime(ipsa_t *assoc, boolean_t inbound)
1512 ipsa_t *inassoc, *outassoc;
1513 isaf_t *bucket;
1514 sadb_t *sp;
1515 int outhash;
1516 boolean_t isv6;
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);
1523 return;
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
1533 * reconsider.
1536 /* Use address family to select IPv6/IPv4 */
1537 isv6 = (assoc->ipsa_addrfam == AF_INET6);
1538 if (isv6) {
1539 sp = &ahstack->ah_sadb.s_v6;
1540 } else {
1541 sp = &ahstack->ah_sadb.s_v4;
1542 ASSERT(assoc->ipsa_addrfam == AF_INET);
1544 if (inbound) {
1545 inassoc = assoc;
1546 if (isv6)
1547 outhash = OUTBOUND_HASH_V6(sp,
1548 *((in6_addr_t *)&inassoc->ipsa_dstaddr));
1549 else
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);
1564 return;
1566 } else {
1567 outassoc = assoc;
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);
1579 return;
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
1591 * them in { }.
1593 if (inbound) {
1594 IPSA_REFRELE(outassoc);
1595 } else {
1596 IPSA_REFRELE(inassoc);
1600 /* Refactor me */
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.
1608 static boolean_t
1609 ah_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound)
1611 ipsa_t *inassoc, *outassoc;
1612 isaf_t *bucket;
1613 boolean_t inrc, outrc, isv6;
1614 sadb_t *sp;
1615 int outhash;
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,
1622 B_TRUE));
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
1630 * arbitrarily.
1632 * If we need tight synchronization on the peer SA, then we need to
1633 * reconsider.
1636 /* Pick v4/v6 bucket based on addrfam. */
1637 isv6 = (assoc->ipsa_addrfam == AF_INET6);
1638 if (isv6) {
1639 sp = &ahstack->ah_sadb.s_v6;
1640 } else {
1641 sp = &ahstack->ah_sadb.s_v4;
1642 ASSERT(assoc->ipsa_addrfam == AF_INET);
1644 if (inbound) {
1645 inassoc = assoc;
1646 if (isv6)
1647 outhash = OUTBOUND_HASH_V6(sp,
1648 *((in6_addr_t *)&inassoc->ipsa_dstaddr));
1649 else
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,
1663 bytes, B_TRUE));
1665 } else {
1666 outassoc = assoc;
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,
1678 bytes, B_TRUE));
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
1689 * them in { }.
1691 if (inbound) {
1692 IPSA_REFRELE(outassoc);
1693 } else {
1694 IPSA_REFRELE(inassoc);
1697 return (inrc && outrc);
1700 /* Refactor me */
1702 * Handle the SADB_GETSPI message. Create a larval SA.
1704 static void
1705 ah_getspi(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
1707 ipsa_t *newbie, *target;
1708 isaf_t *outbound, *inbound;
1709 int rc, diagnostic;
1710 sadb_sa_t *assoc;
1711 keysock_out_t *kso;
1712 uint32_t newspi;
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,
1723 ksi->ks_in_serial);
1724 return;
1725 } else if (newbie == (ipsa_t *)-1) {
1726 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL, diagnostic,
1727 ksi->ks_in_serial);
1728 return;
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,
1741 newbie->ipsa_spi);
1742 } else {
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,
1746 newbie->ipsa_spi);
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) {
1773 rc = EEXIST;
1774 IPSA_REFRELE(target);
1775 } else {
1777 * sadb_insertassoc() also checks for collisions, so
1778 * if there's a colliding larval entry, rc will be set
1779 * to EEXIST.
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
1788 * newbie.
1790 mutex_exit(&outbound->isaf_lock);
1792 if (rc != 0) {
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);
1797 return;
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
1829 * header.
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.
1833 static mblk_t *
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;
1838 uint8_t *nexthdrp;
1839 ah_t *ah;
1840 icmp6_t *icmp6;
1841 isaf_t *isaf;
1842 ipsa_t *assoc;
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,
1852 &nexthdrp) ||
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);
1859 return (NULL);
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);
1870 return (NULL);
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, "
1888 "sender is 0x%x\n",
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);
1895 return (NULL);
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);
1917 return (NULL);
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;
1926 return (mp);
1930 * IP sends up the ICMP errors for validation and the removal of
1931 * the AH header.
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.
1935 static mblk_t *
1936 ah_icmp_error_v4(mblk_t *mp, ip_recv_attr_t *ira, ipsecah_stack_t *ahstack)
1938 mblk_t *mp1;
1939 icmph_t *icmph;
1940 int iph_hdr_length;
1941 int hdr_length;
1942 isaf_t *hptr;
1943 ipsa_t *assoc;
1944 int ah_length;
1945 ipha_t *ipha;
1946 ipha_t *oipha;
1947 ah_t *ah;
1948 uint32_t length;
1949 int alloc_size;
1950 uint8_t nexthdr;
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 -
1965 mp->b_rptr)) {
1966 ipsec_rl_strlog(ahstack->ipsecah_netstack,
1967 info.mi_idnum, 0, 0,
1968 SL_WARN | SL_ERROR,
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);
1974 return (NULL);
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, "
1997 "sender is 0x%x\n",
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);
2004 return (NULL);
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
2026 * IPV4.
2028 ah_length = (mp->b_wptr - ((uchar_t *)ipha +
2029 hdr_length));
2030 goto done;
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);
2043 return (NULL);
2045 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2046 ipha = (ipha_t *)&icmph[1];
2048 done:
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);
2068 return (NULL);
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
2076 * message, tag on.
2078 mp->b_rptr = (uchar_t *)ipha + hdr_length + ah_length;
2080 if (mp->b_rptr != mp->b_wptr) {
2081 mp1->b_cont = mp;
2082 } else {
2083 if (mp->b_cont != NULL)
2084 mp1->b_cont = mp->b_cont;
2085 freeb(mp);
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);
2096 return (mp1);
2100 * IP calls this to validate the ICMP errors that
2101 * we got from the network.
2103 mblk_t *
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));
2111 else
2112 return (ah_icmp_error_v6(data_mp, ira, ahstack));
2115 static int
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)
2119 uint8_t opt_type;
2120 uint_t optlen;
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++;
2130 ehdrlen -= 2;
2133 * Now handle all the TLV encoded options.
2135 while (ehdrlen != 0) {
2136 opt_type = *oi_opt;
2138 if (opt_type == IP6OPT_PAD1) {
2139 optlen = 1;
2140 } else {
2141 if (ehdrlen < 2)
2142 goto bad_opt;
2143 optlen = 2 + oi_opt[1];
2144 if (optlen > ehdrlen)
2145 goto bad_opt;
2147 if (copy_always || !(opt_type & IP6OPT_MUTABLE)) {
2148 bcopy(oi_opt, pi_opt, optlen);
2149 } else {
2150 if (optlen == 1) {
2151 *pi_opt = 0;
2152 } else {
2154 * Copy the type and data length fields.
2155 * Zero the option data by skipping
2156 * option type and option data len
2157 * fields.
2159 *pi_opt = *oi_opt;
2160 *(pi_opt + 1) = *(oi_opt + 1);
2161 bzero(pi_opt + 2, optlen - 2);
2164 ehdrlen -= optlen;
2165 oi_opt += optlen;
2166 pi_opt += optlen;
2168 return (0);
2169 bad_opt:
2170 return (-1);
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
2179 * the AH header.
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.
2185 * NOTE :
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.
2193 static uint_t
2194 ah_fix_phdr_v6(ip6_t *ip6h, ip6_t *oip6h, boolean_t outbound,
2195 boolean_t copy_always)
2197 uint8_t *oi_opt;
2198 uint8_t *pi_opt;
2199 uint8_t nexthdr;
2200 uint8_t *prev_nexthdr;
2201 ip6_hbh_t *hbhhdr;
2202 ip6_dest_t *dsthdr = NULL;
2203 ip6_rthdr0_t *rthdr;
2204 int ehdrlen;
2205 ah_t *ah;
2206 int ret;
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
2212 * receiver i.e
2214 * Because of ip_massage_options_v6 the header looks like
2215 * this :
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
2234 * IP header.
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);
2240 ah = NULL;
2241 dsthdr = NULL;
2242 for (;;) {
2243 switch (nexthdr) {
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
2252 * was error.
2254 if (ret == -1)
2255 return (0);
2256 hbhhdr = (ip6_hbh_t *)pi_opt;
2257 prev_nexthdr = (uint8_t *)&hbhhdr->ip6h_nxt;
2258 break;
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) {
2264 int i, left;
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
2284 * option.
2286 *pap = oip6h->ip6_dst;
2287 for (i = 1; i < left - 1; i++)
2288 pap[i] = ap[i - 1];
2289 ip6h->ip6_dst = *(ap + left - 1);
2290 } else {
2291 bcopy(oi_opt, pi_opt, ehdrlen);
2293 rthdr = (ip6_rthdr0_t *)pi_opt;
2294 prev_nexthdr = (uint8_t *)&rthdr->ip6r0_nxt;
2295 break;
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
2305 * it as is.
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)
2314 goto terminal_hdr;
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
2321 * was error.
2323 if (ret == -1)
2324 return (0);
2325 break;
2326 case IPPROTO_AH:
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.)
2333 ASSERT(!outbound);
2334 return ((uint_t)(pi_opt - (uint8_t *)ip6h));
2335 default:
2336 ASSERT(outbound);
2337 terminal_hdr:
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));
2343 pi_opt += ehdrlen;
2344 oi_opt += ehdrlen;
2346 /* NOTREACHED */
2349 static boolean_t
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)
2353 int i;
2356 * Padding :
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. */
2394 return (B_FALSE);
2397 if (ah_data_sz != ah_align_sz) {
2398 uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
2399 ah_data_sz);
2401 for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2402 pad[i] = (uchar_t)i; /* Fill the padding */
2405 } else {
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) +
2418 ah_data_sz);
2420 for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2421 pad[i] = opad[i]; /* Copy the padding */
2426 return (B_TRUE);
2430 * Called upon failing the inbound ICV check. The message passed as
2431 * argument is freed.
2433 static void
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;
2438 int af;
2439 void *addr;
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;
2448 if (isv4) {
2449 ipha_t *ipha = (ipha_t *)mp->b_rptr;
2450 addr = &ipha->ipha_dst;
2451 af = AF_INET;
2452 } else {
2453 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
2454 addr = &ip6h->ip6_dst;
2455 af = AF_INET6;
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.
2478 static void
2479 ah_kcf_callback_outbound(void *arg, int status)
2481 mblk_t *mp = (mblk_t *)arg;
2482 mblk_t *async_mp;
2483 netstack_t *ns;
2484 ipsec_stack_t *ipss;
2485 ipsecah_stack_t *ahstack;
2486 mblk_t *data_mp;
2487 ip_xmit_attr_t ixas;
2488 ipsec_crypto_t *ic;
2489 ill_t *ill;
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);
2514 freemsg(data_mp);
2515 goto done;
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)
2525 goto done;
2527 (void) ip_output_post_ipsec(data_mp, &ixas);
2528 } else {
2529 /* Outbound shouldn't see invalid MAC */
2530 ASSERT(status != CRYPTO_INVALID_MAC);
2532 ah1dbg(ahstack,
2533 ("ah_kcf_callback_outbound: crypto failed with 0x%x\n",
2534 status));
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);
2543 done:
2544 ixa_cleanup(&ixas);
2545 (void) ipsec_free_crypto_data(mp);
2549 * Kernel crypto framework callback invoked after completion of async
2550 * crypto requests for inbound packets.
2552 static void
2553 ah_kcf_callback_inbound(void *arg, int status)
2555 mblk_t *mp = (mblk_t *)arg;
2556 mblk_t *async_mp;
2557 netstack_t *ns;
2558 ipsec_stack_t *ipss;
2559 ipsecah_stack_t *ahstack;
2560 mblk_t *data_mp;
2561 ip_recv_attr_t iras;
2562 ipsec_crypto_t *ic;
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);
2581 freemsg(data_mp);
2582 goto done;
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)
2591 goto done;
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);
2598 } else {
2599 ah1dbg(ahstack,
2600 ("ah_kcf_callback_inbound: crypto failed with 0x%x\n",
2601 status));
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);
2609 done:
2610 ira_cleanup(&iras, B_TRUE);
2611 (void) ipsec_free_crypto_data(mp);
2615 * Invoked on kernel crypto failure during inbound and outbound processing.
2617 static void
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);
2629 if (is_inbound)
2630 IP_AH_BUMP_STAT(ipss, in_discards);
2631 else
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.
2666 static mblk_t *
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)
2670 int kef_rc;
2671 mblk_t *mp;
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);
2684 if (force) {
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,
2690 ira->ira_ill);
2691 freemsg(phdr_mp);
2692 return (NULL);
2695 linkb(mp, phdr_mp);
2696 callrp = &call_req;
2697 AH_INIT_CALLREQ(callrp, mp, ah_kcf_callback_inbound);
2698 } else {
2700 * If we know we are going to do sync then ipsec_crypto_t
2701 * should be on the stack.
2703 ic = &icstack;
2704 bzero(ic, sizeof (*ic));
2705 callrp = NULL;
2708 /* init arguments for the crypto framework */
2709 AH_INIT_CRYPTO_DATA(&ic->ic_crypto_data, AH_MSGSIZE(phdr_mp),
2710 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 +
2714 sizeof (ah_t));
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);
2725 switch (kef_rc) {
2726 case CRYPTO_SUCCESS:
2727 AH_BUMP_STAT(ahstack, crypto_sync);
2728 phdr_mp = ah_auth_in_done(phdr_mp, ira, ic);
2729 if (force) {
2730 /* Free mp after we are done with ic */
2731 mp = ipsec_free_crypto_data(mp);
2732 (void) ip_recv_attr_free_mblk(mp);
2734 return (phdr_mp);
2735 case CRYPTO_QUEUED:
2736 /* ah_kcf_callback_inbound() will be invoked on completion */
2737 AH_BUMP_STAT(ahstack, crypto_async);
2738 return (NULL);
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 */
2745 if (force) {
2746 mp = ipsec_free_crypto_data(mp);
2747 (void) ip_recv_attr_free_mblk(mp);
2749 return (NULL);
2752 if (force) {
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 */
2759 return (NULL);
2763 * Submit an outbound packet for processing by the crypto framework.
2765 static mblk_t *
2766 ah_submit_req_outbound(mblk_t *phdr_mp, ip_xmit_attr_t *ixa,
2767 size_t skip_len, ipsa_t *assoc)
2769 int kef_rc;
2770 mblk_t *mp;
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);
2783 if (force) {
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);
2789 freemsg(phdr_mp);
2790 return (NULL);
2792 linkb(mp, phdr_mp);
2793 callrp = &call_req;
2794 AH_INIT_CALLREQ(callrp, mp, ah_kcf_callback_outbound);
2795 } else {
2797 * If we know we are going to do sync then ipsec_crypto_t
2798 * should be on the stack.
2800 ic = &icstack;
2801 bzero(ic, sizeof (*ic));
2802 callrp = NULL;
2805 /* init arguments for the crypto framework */
2806 AH_INIT_CRYPTO_DATA(&ic->ic_crypto_data, AH_MSGSIZE(phdr_mp),
2807 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);
2821 switch (kef_rc) {
2822 case CRYPTO_SUCCESS:
2823 AH_BUMP_STAT(ahstack, crypto_sync);
2824 phdr_mp = ah_auth_out_done(phdr_mp, ixa, ic);
2825 if (force) {
2826 /* Free mp after we are done with ic */
2827 mp = ipsec_free_crypto_data(mp);
2828 (void) ip_xmit_attr_free_mblk(mp);
2830 return (phdr_mp);
2831 case CRYPTO_QUEUED:
2832 /* ah_kcf_callback_outbound() will be invoked on completion */
2833 AH_BUMP_STAT(ahstack, crypto_async);
2834 return (NULL);
2837 if (force) {
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 */
2844 return (NULL);
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.
2852 static mblk_t *
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)
2856 ip6_t *ip6h;
2857 ip6_t *oip6h;
2858 mblk_t *phdr_mp;
2859 int option_length;
2860 uint_t ah_align_sz;
2861 uint_t ah_offset;
2862 int hdr_size;
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))
2895 return (NULL);
2898 if ((phdr_mp = allocb_tmpl(hdr_size + ah_data_sz, mp)) == NULL) {
2899 return (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;
2912 if (outbound) {
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);
2921 } else {
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;
2934 } else {
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) {
2941 return (NULL);
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)) {
2948 freeb(phdr_mp);
2950 * Returning NULL will tell the caller to
2951 * IPSA_REFELE(), free the memory, etc.
2953 return (NULL);
2956 phdr_mp->b_wptr = ((uint8_t *)ip6h + ah_offset + sizeof (ah_t) +
2957 ah_align_sz);
2958 if (!outbound)
2959 *length_to_skip += sizeof (ah_t) + ah_align_sz;
2960 return (phdr_mp);
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.
2968 static mblk_t *
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)
2972 ipoptp_t opts;
2973 uint32_t option_length;
2974 ipha_t *ipha;
2975 ipha_t *oipha;
2976 mblk_t *phdr_mp;
2977 int size;
2978 uchar_t *optptr;
2979 uint8_t optval;
2980 uint8_t optlen;
2981 ipaddr_t dst;
2982 uint32_t v_hlen_tos_len;
2983 int ip_hdr_length;
2984 uint_t ah_align_sz;
2985 uint32_t off;
2987 #ifdef _BIG_ENDIAN
2988 #define V_HLEN (v_hlen_tos_len >> 24)
2989 #else
2990 #define V_HLEN (v_hlen_tos_len & 0xFF)
2991 #endif
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 +
3016 ah_data_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) {
3027 return (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;
3037 if (outbound) {
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);
3046 } else {
3047 ipha->ipha_length = oipha->ipha_length;
3050 ipha->ipha_ident = oipha->ipha_ident;
3051 ipha->ipha_fragment_offset_and_flags = 0;
3052 ipha->ipha_ttl = 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 */
3065 goto ah_hdr;
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;
3085 switch (optval) {
3086 case IPOPT_RA:
3087 case IPOPT_SDMDD:
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.
3095 break;
3096 case IPOPT_SSRR:
3097 case IPOPT_LSRR:
3098 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0)
3099 goto bad_ipv4opt;
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);
3114 /* FALLTHRU */
3115 case IPOPT_RR:
3116 case IPOPT_TS:
3117 case IPOPT_SATID:
3118 default:
3120 * optlen should include from the beginning of an
3121 * option.
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);
3127 break;
3131 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
3132 bad_ipv4opt:
3133 ah1dbg(ahstack, ("AH : bad IPv4 option"));
3134 freeb(phdr_mp);
3135 return (NULL);
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.
3146 if (outbound)
3147 ipha->ipha_dst = dst;
3148 ah_hdr:
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)) {
3154 freeb(phdr_mp);
3156 * Returning NULL will tell the caller to IPSA_REFELE(), free
3157 * the memory, etc.
3159 return (NULL);
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);
3166 if (outbound)
3167 *length_to_skip = ip_hdr_length;
3168 else
3169 *length_to_skip = ip_hdr_length + sizeof (ah_t) + ah_align_sz;
3170 return (phdr_mp);
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.
3179 static mblk_t *
3180 ah_outbound(mblk_t *data_mp, ip_xmit_attr_t *ixa)
3182 mblk_t *phdr_mp;
3183 ipsa_t *assoc;
3184 int length_to_skip;
3185 uint_t ah_align_sz;
3186 uint_t age_bytes;
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
3196 * PSEUDO_HDR->DATA
3198 * one by one.
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) +
3218 ah_align_sz;
3219 } else {
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);
3235 freemsg(data_mp);
3236 if (need_refrele)
3237 ixa_refrele(ixa);
3238 return (NULL);
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);
3249 } else {
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);
3260 if (need_refrele)
3261 ixa_refrele(ixa);
3262 return (NULL);
3265 phdr_mp->b_cont = data_mp;
3266 data_mp->b_rptr += length_to_skip;
3267 data_mp = phdr_mp;
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);
3279 if (need_refrele)
3280 ixa_refrele(ixa);
3281 return (data_mp);
3284 static mblk_t *
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;
3289 int length_to_skip;
3290 int ah_length;
3291 mblk_t *phdr_mp;
3292 uint32_t ah_offset;
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);
3317 return (NULL);
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 -
3339 data_mp->b_rptr)) {
3340 (void) ipsec_rl_strlog(ns, info.mi_idnum, 0, 0,
3341 SL_WARN | SL_ERROR,
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);
3348 return (NULL);
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);
3359 } else {
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);
3372 return (NULL);
3375 phdr_mp->b_cont = data_mp;
3376 data_mp->b_rptr += length_to_skip;
3377 data_mp = phdr_mp;
3379 /* submit request to KCF */
3380 return (ah_submit_req_inbound(data_mp, ira, length_to_skip, ah_offset,
3381 assoc));
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.
3390 static mblk_t *
3391 ah_auth_in_done(mblk_t *phdr_mp, ip_recv_attr_t *ira, ipsec_crypto_t *ic)
3393 ipha_t *ipha;
3394 uint_t ah_offset = 0;
3395 mblk_t *mp;
3396 int align_len, newpos;
3397 ah_t *ah;
3398 uint32_t length;
3399 uint32_t *dest32;
3400 uint8_t *dest;
3401 boolean_t isv4;
3402 ip6_t *ip6h;
3403 uint_t icv_len;
3404 ipsa_t *assoc;
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);
3419 return (NULL);
3422 mp = phdr_mp->b_cont;
3423 if (mp == NULL) {
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);
3428 return (NULL);
3430 mp->b_rptr -= ic->ic_skip_len;
3432 ah_set_usetime(assoc, B_TRUE);
3434 if (isv4) {
3435 ipha = (ipha_t *)mp->b_rptr;
3436 ah_offset = ipha->ipha_version_and_hdr_length -
3437 (uint8_t)((IP_VERSION << 4));
3438 ah_offset <<= 2;
3439 align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
3440 IPV4_PADDING_ALIGN);
3441 } else {
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)) {
3460 int af;
3461 void *addr;
3463 if (isv4) {
3464 addr = &ipha->ipha_dst;
3465 af = AF_INET;
3466 } else {
3467 addr = &ip6h->ip6_dst;
3468 af = AF_INET6;
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,
3480 SL_ERROR | SL_WARN,
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);
3484 goto ah_in_discard;
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,
3492 * but it's doable.
3494 if (isv4) {
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,
3504 SL_ERROR | SL_WARN,
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);
3510 goto ah_in_discard;
3512 ipha->ipha_protocol = ah->ah_nexthdr;
3513 length -= newpos;
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);
3518 } else {
3519 uchar_t *whereptr;
3520 int hdrlen;
3521 uint8_t *nexthdr;
3522 ip6_hbh_t *hbhhdr;
3523 ip6_dest_t *dsthdr;
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,
3534 SL_ERROR | SL_WARN,
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);
3540 goto ah_in_discard;
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) {
3554 whereptr += hdrlen;
3555 /* Assume IP has already stripped it */
3556 ASSERT(*nexthdr != IPPROTO_FRAGMENT);
3557 switch (*nexthdr) {
3558 case IPPROTO_HOPOPTS:
3559 hbhhdr = (ip6_hbh_t *)whereptr;
3560 nexthdr = &hbhhdr->ip6h_nxt;
3561 hdrlen = 8 * (hbhhdr->ip6h_len + 1);
3562 break;
3563 case IPPROTO_DSTOPTS:
3564 dsthdr = (ip6_dest_t *)whereptr;
3565 nexthdr = &dsthdr->ip6d_nxt;
3566 hdrlen = 8 * (dsthdr->ip6d_len + 1);
3567 break;
3568 case IPPROTO_ROUTING:
3569 rthdr = (ip6_rthdr0_t *)whereptr;
3570 nexthdr = &rthdr->ip6r0_nxt;
3571 hdrlen = 8 * (rthdr->ip6r0_len + 1);
3572 break;
3575 *nexthdr = ah->ah_nexthdr;
3576 length -= newpos;
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));
3586 } else {
3587 dest = mp->b_rptr + ah_offset;
3588 while (--dest >= mp->b_rptr)
3589 *dest = *(dest - newpos);
3591 freeb(phdr_mp);
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);
3599 return (NULL);
3602 return (mp);
3604 ah_in_discard:
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);
3609 return (NULL);
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.
3618 static mblk_t *
3619 ah_auth_out_done(mblk_t *phdr_mp, ip_xmit_attr_t *ixa, ipsec_crypto_t *ic)
3621 mblk_t *mp;
3622 int align_len;
3623 uint32_t hdrs_length;
3624 uchar_t *ptr;
3625 uint32_t length;
3626 boolean_t isv4;
3627 size_t icv_len;
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;
3637 if (mp == NULL) {
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);
3642 return (NULL);
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);
3650 if (isv4) {
3651 ipha_t *ipha;
3652 ipha_t *nipha;
3654 ipha = (ipha_t *)mp->b_rptr;
3655 hdrs_length = ipha->ipha_version_and_hdr_length -
3656 (uint8_t)((IP_VERSION << 4));
3657 hdrs_length <<= 2;
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);
3684 } else {
3685 ip6_t *ip6h;
3686 ip6_t *nip6h;
3687 uint_t ah_offset;
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;
3725 freeb(mp);
3728 return (phdr_mp);
3731 /* Refactor me */
3733 * Wrapper to allow IP to trigger an AH association failure message
3734 * during SA inbound selection.
3736 void
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.
3757 void
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;