1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 *
25 * Copyright 2015 Garrett D'Amore <garrett@damore.org>
26 */
27
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>
53
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>
70
71 #include <sys/crypto/common.h>
72 #include <sys/crypto/api.h>
73 #include <sys/kstat.h>
74 #include <sys/strsubr.h>
75
76 #include <sys/tsol/tnet.h>
77
78 /*
79 * Table of ND variables supported by ipsecah. These are loaded into
80 * ipsecah_g_nd in ipsecah_init_nd.
81 * All of these are alterable, within the min/max values given, at run time.
82 */
83 static ipsecahparam_t lcl_param_arr[] = {
84 /* min max value name */
85 { 0, 3, 0, "ipsecah_debug"},
86 { 125, 32000, SADB_AGE_INTERVAL_DEFAULT, "ipsecah_age_interval"},
87 { 1, 10, 1, "ipsecah_reap_delay"},
88 { 1, SADB_MAX_REPLAY, 64, "ipsecah_replay_size"},
89 { 1, 300, 15, "ipsecah_acquire_timeout"},
90 { 1, 1800, 90, "ipsecah_larval_timeout"},
91 /* Default lifetime values for ACQUIRE messages. */
92 { 0, 0xffffffffU, 0, "ipsecah_default_soft_bytes"},
93 { 0, 0xffffffffU, 0, "ipsecah_default_hard_bytes"},
94 { 0, 0xffffffffU, 24000, "ipsecah_default_soft_addtime"},
95 { 0, 0xffffffffU, 28800, "ipsecah_default_hard_addtime"},
96 { 0, 0xffffffffU, 0, "ipsecah_default_soft_usetime"},
97 { 0, 0xffffffffU, 0, "ipsecah_default_hard_usetime"},
98 { 0, 1, 0, "ipsecah_log_unknown_spi"},
99 };
100 #define ipsecah_debug ipsecah_params[0].ipsecah_param_value
101 #define ipsecah_age_interval ipsecah_params[1].ipsecah_param_value
102 #define ipsecah_age_int_max ipsecah_params[1].ipsecah_param_max
103 #define ipsecah_reap_delay ipsecah_params[2].ipsecah_param_value
104 #define ipsecah_replay_size ipsecah_params[3].ipsecah_param_value
105 #define ipsecah_acquire_timeout ipsecah_params[4].ipsecah_param_value
106 #define ipsecah_larval_timeout ipsecah_params[5].ipsecah_param_value
107 #define ipsecah_default_soft_bytes ipsecah_params[6].ipsecah_param_value
108 #define ipsecah_default_hard_bytes ipsecah_params[7].ipsecah_param_value
109 #define ipsecah_default_soft_addtime ipsecah_params[8].ipsecah_param_value
110 #define ipsecah_default_hard_addtime ipsecah_params[9].ipsecah_param_value
111 #define ipsecah_default_soft_usetime ipsecah_params[10].ipsecah_param_value
112 #define ipsecah_default_hard_usetime ipsecah_params[11].ipsecah_param_value
113 #define ipsecah_log_unknown_spi ipsecah_params[12].ipsecah_param_value
114
115 #define ah0dbg(a) printf a
116 /* NOTE: != 0 instead of > 0 so lint doesn't complain. */
117 #define ah1dbg(ahstack, a) if (ahstack->ipsecah_debug != 0) printf a
118 #define ah2dbg(ahstack, a) if (ahstack->ipsecah_debug > 1) printf a
119 #define ah3dbg(ahstack, a) if (ahstack->ipsecah_debug > 2) printf a
120
121 /*
122 * XXX This is broken. Padding should be determined dynamically
123 * depending on the ICV size and IP version number so that the
124 * total AH header size is a multiple of 32 bits or 64 bits
125 * for V4 and V6 respectively. For 96bit ICVs we have no problems.
126 * Anything different from that, we need to fix our code.
127 */
128 #define IPV4_PADDING_ALIGN 0x04 /* Multiple of 32 bits */
129 #define IPV6_PADDING_ALIGN 0x04 /* Multiple of 32 bits */
130
131 /*
132 * Helper macro. Avoids a call to msgdsize if there is only one
133 * mblk in the chain.
134 */
135 #define AH_MSGSIZE(mp) ((mp)->b_cont != NULL ? msgdsize(mp) : MBLKL(mp))
136
137
138 static mblk_t *ah_auth_out_done(mblk_t *, ip_xmit_attr_t *, ipsec_crypto_t *);
139 static mblk_t *ah_auth_in_done(mblk_t *, ip_recv_attr_t *, ipsec_crypto_t *);
140 static mblk_t *ah_process_ip_options_v4(mblk_t *, ipsa_t *, int *, uint_t,
141 boolean_t, ipsecah_stack_t *);
142 static mblk_t *ah_process_ip_options_v6(mblk_t *, ipsa_t *, int *, uint_t,
143 boolean_t, ipsecah_stack_t *);
144 static void ah_getspi(mblk_t *, keysock_in_t *, ipsecah_stack_t *);
145 static void ah_inbound_restart(mblk_t *, ip_recv_attr_t *);
146
147 static mblk_t *ah_outbound(mblk_t *, ip_xmit_attr_t *);
148 static void ah_outbound_finish(mblk_t *, ip_xmit_attr_t *);
149
150 static int ipsecah_open(queue_t *, dev_t *, int, int, cred_t *);
151 static int ipsecah_close(queue_t *);
152 static void ipsecah_wput(queue_t *, mblk_t *);
153 static void ah_send_acquire(ipsacq_t *, mblk_t *, netstack_t *);
154 static boolean_t ah_register_out(uint32_t, uint32_t, uint_t, ipsecah_stack_t *,
155 cred_t *);
156 static void *ipsecah_stack_init(netstackid_t stackid, netstack_t *ns);
157 static void ipsecah_stack_fini(netstackid_t stackid, void *arg);
158
159 /* Setable in /etc/system */
160 uint32_t ah_hash_size = IPSEC_DEFAULT_HASH_SIZE;
161
162 static taskq_t *ah_taskq;
163
164 static struct module_info info = {
165 5136, "ipsecah", 0, INFPSZ, 65536, 1024
166 };
167
168 static struct qinit rinit = {
169 (pfi_t)putnext, NULL, ipsecah_open, ipsecah_close, NULL, &info,
170 NULL
171 };
172
173 static struct qinit winit = {
174 (pfi_t)ipsecah_wput, NULL, ipsecah_open, ipsecah_close, NULL, &info,
175 NULL
176 };
177
178 struct streamtab ipsecahinfo = {
179 &rinit, &winit, NULL, NULL
180 };
181
182 static int ah_kstat_update(kstat_t *, int);
183
184 uint64_t ipsacq_maxpackets = IPSACQ_MAXPACKETS;
185
186 static boolean_t
187 ah_kstat_init(ipsecah_stack_t *ahstack, netstackid_t stackid)
188 {
189 ipsec_stack_t *ipss = ahstack->ipsecah_netstack->netstack_ipsec;
190
191 ahstack->ah_ksp = kstat_create_netstack("ipsecah", 0, "ah_stat", "net",
192 KSTAT_TYPE_NAMED, sizeof (ah_kstats_t) / sizeof (kstat_named_t),
193 KSTAT_FLAG_PERSISTENT, stackid);
194
195 if (ahstack->ah_ksp == NULL || ahstack->ah_ksp->ks_data == NULL)
196 return (B_FALSE);
197
198 ahstack->ah_kstats = ahstack->ah_ksp->ks_data;
199
200 ahstack->ah_ksp->ks_update = ah_kstat_update;
201 ahstack->ah_ksp->ks_private = (void *)(uintptr_t)stackid;
202
203 #define K64 KSTAT_DATA_UINT64
204 #define KI(x) kstat_named_init(&(ahstack->ah_kstats->ah_stat_##x), #x, K64)
205
206 KI(num_aalgs);
207 KI(good_auth);
208 KI(bad_auth);
209 KI(replay_failures);
210 KI(replay_early_failures);
211 KI(keysock_in);
212 KI(out_requests);
213 KI(acquire_requests);
214 KI(bytes_expired);
215 KI(out_discards);
216 KI(crypto_sync);
217 KI(crypto_async);
218 KI(crypto_failures);
219
220 #undef KI
221 #undef K64
222
223 kstat_install(ahstack->ah_ksp);
224 IP_ACQUIRE_STAT(ipss, maxpackets, ipsacq_maxpackets);
225 return (B_TRUE);
226 }
227
228 static int
229 ah_kstat_update(kstat_t *kp, int rw)
230 {
231 ah_kstats_t *ekp;
232 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private;
233 netstack_t *ns;
234 ipsec_stack_t *ipss;
235
236 if ((kp == NULL) || (kp->ks_data == NULL))
237 return (EIO);
238
239 if (rw == KSTAT_WRITE)
240 return (EACCES);
241
242 ns = netstack_find_by_stackid(stackid);
243 if (ns == NULL)
244 return (-1);
245 ipss = ns->netstack_ipsec;
246 if (ipss == NULL) {
247 netstack_rele(ns);
248 return (-1);
249 }
250 ekp = (ah_kstats_t *)kp->ks_data;
251
252 mutex_enter(&ipss->ipsec_alg_lock);
253 ekp->ah_stat_num_aalgs.value.ui64 = ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
254 mutex_exit(&ipss->ipsec_alg_lock);
255
256 netstack_rele(ns);
257 return (0);
258 }
259
260 /*
261 * Don't have to lock ipsec_age_interval, as only one thread will access it at
262 * a time, because I control the one function that does a qtimeout() on
263 * ah_pfkey_q.
264 */
265 static void
266 ah_ager(void *arg)
267 {
268 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)arg;
269 netstack_t *ns = ahstack->ipsecah_netstack;
270 hrtime_t begin = gethrtime();
271
272 sadb_ager(&ahstack->ah_sadb.s_v4, ahstack->ah_pfkey_q,
273 ahstack->ipsecah_reap_delay, ns);
274 sadb_ager(&ahstack->ah_sadb.s_v6, ahstack->ah_pfkey_q,
275 ahstack->ipsecah_reap_delay, ns);
276
277 ahstack->ah_event = sadb_retimeout(begin, ahstack->ah_pfkey_q,
278 ah_ager, ahstack,
279 &ahstack->ipsecah_age_interval, ahstack->ipsecah_age_int_max,
280 info.mi_idnum);
281 }
282
283 /*
284 * Get an AH NDD parameter.
285 */
286 /* ARGSUSED */
287 static int
288 ipsecah_param_get(q, mp, cp, cr)
289 queue_t *q;
290 mblk_t *mp;
291 caddr_t cp;
292 cred_t *cr;
293 {
294 ipsecahparam_t *ipsecahpa = (ipsecahparam_t *)cp;
295 uint_t value;
296 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)q->q_ptr;
297
298 mutex_enter(&ahstack->ipsecah_param_lock);
299 value = ipsecahpa->ipsecah_param_value;
300 mutex_exit(&ahstack->ipsecah_param_lock);
301
302 (void) mi_mpprintf(mp, "%u", value);
303 return (0);
304 }
305
306 /*
307 * This routine sets an NDD variable in a ipsecahparam_t structure.
308 */
309 /* ARGSUSED */
310 static int
311 ipsecah_param_set(q, mp, value, cp, cr)
312 queue_t *q;
313 mblk_t *mp;
314 char *value;
315 caddr_t cp;
316 cred_t *cr;
317 {
318 ulong_t new_value;
319 ipsecahparam_t *ipsecahpa = (ipsecahparam_t *)cp;
320 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)q->q_ptr;
321
322 /*
323 * Fail the request if the new value does not lie within the
324 * required bounds.
325 */
326 if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
327 new_value < ipsecahpa->ipsecah_param_min ||
328 new_value > ipsecahpa->ipsecah_param_max) {
329 return (EINVAL);
330 }
331
332 /* Set the new value */
333 mutex_enter(&ahstack->ipsecah_param_lock);
334 ipsecahpa->ipsecah_param_value = new_value;
335 mutex_exit(&ahstack->ipsecah_param_lock);
336 return (0);
337 }
338
339 /*
340 * Using lifetime NDD variables, fill in an extended combination's
341 * lifetime information.
342 */
343 void
344 ipsecah_fill_defs(sadb_x_ecomb_t *ecomb, netstack_t *ns)
345 {
346 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
347
348 ecomb->sadb_x_ecomb_soft_bytes = ahstack->ipsecah_default_soft_bytes;
349 ecomb->sadb_x_ecomb_hard_bytes = ahstack->ipsecah_default_hard_bytes;
350 ecomb->sadb_x_ecomb_soft_addtime =
351 ahstack->ipsecah_default_soft_addtime;
352 ecomb->sadb_x_ecomb_hard_addtime =
353 ahstack->ipsecah_default_hard_addtime;
354 ecomb->sadb_x_ecomb_soft_usetime =
355 ahstack->ipsecah_default_soft_usetime;
356 ecomb->sadb_x_ecomb_hard_usetime =
357 ahstack->ipsecah_default_hard_usetime;
358 }
359
360 /*
361 * Initialize things for AH at module load time.
362 */
363 boolean_t
364 ipsecah_ddi_init(void)
365 {
366 ah_taskq = taskq_create("ah_taskq", 1, minclsyspri,
367 IPSEC_TASKQ_MIN, IPSEC_TASKQ_MAX, 0);
368
369 /*
370 * We want to be informed each time a stack is created or
371 * destroyed in the kernel, so we can maintain the
372 * set of ipsecah_stack_t's.
373 */
374 netstack_register(NS_IPSECAH, ipsecah_stack_init, NULL,
375 ipsecah_stack_fini);
376
377 return (B_TRUE);
378 }
379
380 /*
381 * Walk through the param array specified registering each element with the
382 * named dispatch handler.
383 */
384 static boolean_t
385 ipsecah_param_register(IDP *ndp, ipsecahparam_t *ahp, int cnt)
386 {
387 for (; cnt-- > 0; ahp++) {
388 if (ahp->ipsecah_param_name != NULL &&
389 ahp->ipsecah_param_name[0]) {
390 if (!nd_load(ndp,
391 ahp->ipsecah_param_name,
392 ipsecah_param_get, ipsecah_param_set,
393 (caddr_t)ahp)) {
394 nd_free(ndp);
395 return (B_FALSE);
396 }
397 }
398 }
399 return (B_TRUE);
400 }
401
402 /*
403 * Initialize things for AH for each stack instance
404 */
405 static void *
406 ipsecah_stack_init(netstackid_t stackid, netstack_t *ns)
407 {
408 ipsecah_stack_t *ahstack;
409 ipsecahparam_t *ahp;
410
411 ahstack = (ipsecah_stack_t *)kmem_zalloc(sizeof (*ahstack), KM_SLEEP);
412 ahstack->ipsecah_netstack = ns;
413
414 ahp = (ipsecahparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
415 ahstack->ipsecah_params = ahp;
416 bcopy(lcl_param_arr, ahp, sizeof (lcl_param_arr));
417
418 (void) ipsecah_param_register(&ahstack->ipsecah_g_nd, ahp,
419 A_CNT(lcl_param_arr));
420
421 (void) ah_kstat_init(ahstack, stackid);
422
423 ahstack->ah_sadb.s_acquire_timeout = &ahstack->ipsecah_acquire_timeout;
424 ahstack->ah_sadb.s_acqfn = ah_send_acquire;
425 sadbp_init("AH", &ahstack->ah_sadb, SADB_SATYPE_AH, ah_hash_size,
426 ahstack->ipsecah_netstack);
427
428 mutex_init(&ahstack->ipsecah_param_lock, NULL, MUTEX_DEFAULT, 0);
429
430 ip_drop_register(&ahstack->ah_dropper, "IPsec AH");
431 return (ahstack);
432 }
433
434 /*
435 * Destroy things for AH at module unload time.
436 */
437 void
438 ipsecah_ddi_destroy(void)
439 {
440 netstack_unregister(NS_IPSECAH);
441 taskq_destroy(ah_taskq);
442 }
443
444 /*
445 * Destroy things for AH for one stack... Never called?
446 */
447 static void
448 ipsecah_stack_fini(netstackid_t stackid, void *arg)
449 {
450 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)arg;
451
452 if (ahstack->ah_pfkey_q != NULL) {
453 (void) quntimeout(ahstack->ah_pfkey_q, ahstack->ah_event);
454 }
455 ahstack->ah_sadb.s_acqfn = NULL;
456 ahstack->ah_sadb.s_acquire_timeout = NULL;
457 sadbp_destroy(&ahstack->ah_sadb, ahstack->ipsecah_netstack);
458 ip_drop_unregister(&ahstack->ah_dropper);
459 mutex_destroy(&ahstack->ipsecah_param_lock);
460 nd_free(&ahstack->ipsecah_g_nd);
461
462 kmem_free(ahstack->ipsecah_params, sizeof (lcl_param_arr));
463 ahstack->ipsecah_params = NULL;
464 kstat_delete_netstack(ahstack->ah_ksp, stackid);
465 ahstack->ah_ksp = NULL;
466 ahstack->ah_kstats = NULL;
467
468 kmem_free(ahstack, sizeof (*ahstack));
469 }
470
471 /*
472 * AH module open routine, which is here for keysock plumbing.
473 * Keysock is pushed over {AH,ESP} which is an artifact from the Bad Old
474 * Days of export control, and fears that ESP would not be allowed
475 * to be shipped at all by default. Eventually, keysock should
476 * either access AH and ESP via modstubs or krtld dependencies, or
477 * perhaps be folded in with AH and ESP into a single IPsec/netsec
478 * module ("netsec" if PF_KEY provides more than AH/ESP keying tables).
479 */
480 /* ARGSUSED */
481 static int
482 ipsecah_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
483 {
484 netstack_t *ns;
485 ipsecah_stack_t *ahstack;
486
487 if (secpolicy_ip_config(credp, B_FALSE) != 0)
488 return (EPERM);
489
490 if (q->q_ptr != NULL)
491 return (0); /* Re-open of an already open instance. */
492
493 if (sflag != MODOPEN)
494 return (EINVAL);
495
496 ns = netstack_find_by_cred(credp);
497 ASSERT(ns != NULL);
498 ahstack = ns->netstack_ipsecah;
499 ASSERT(ahstack != NULL);
500
501 q->q_ptr = ahstack;
502 WR(q)->q_ptr = q->q_ptr;
503
504 qprocson(q);
505 return (0);
506 }
507
508 /*
509 * AH module close routine.
510 */
511 static int
512 ipsecah_close(queue_t *q)
513 {
514 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)q->q_ptr;
515
516 /*
517 * Clean up q_ptr, if needed.
518 */
519 qprocsoff(q);
520
521 /* Keysock queue check is safe, because of OCEXCL perimeter. */
522
523 if (q == ahstack->ah_pfkey_q) {
524 ah1dbg(ahstack,
525 ("ipsecah_close: Ummm... keysock is closing AH.\n"));
526 ahstack->ah_pfkey_q = NULL;
527 /* Detach qtimeouts. */
528 (void) quntimeout(q, ahstack->ah_event);
529 }
530
531 netstack_rele(ahstack->ipsecah_netstack);
532 return (0);
533 }
534
535 /*
536 * Construct an SADB_REGISTER message with the current algorithms.
537 */
538 static boolean_t
539 ah_register_out(uint32_t sequence, uint32_t pid, uint_t serial,
540 ipsecah_stack_t *ahstack, cred_t *cr)
541 {
542 mblk_t *mp;
543 boolean_t rc = B_TRUE;
544 sadb_msg_t *samsg;
545 sadb_supported_t *sasupp;
546 sadb_alg_t *saalg;
547 uint_t allocsize = sizeof (*samsg);
548 uint_t i, numalgs_snap;
549 ipsec_alginfo_t **authalgs;
550 uint_t num_aalgs;
551 ipsec_stack_t *ipss = ahstack->ipsecah_netstack->netstack_ipsec;
552 sadb_sens_t *sens;
553 size_t sens_len = 0;
554 sadb_ext_t *nextext;
555 ts_label_t *sens_tsl = NULL;
556
557 /* Allocate the KEYSOCK_OUT. */
558 mp = sadb_keysock_out(serial);
559 if (mp == NULL) {
560 ah0dbg(("ah_register_out: couldn't allocate mblk.\n"));
561 return (B_FALSE);
562 }
563
564 if (is_system_labeled() && (cr != NULL)) {
565 sens_tsl = crgetlabel(cr);
566 if (sens_tsl != NULL) {
567 sens_len = sadb_sens_len_from_label(sens_tsl);
568 allocsize += sens_len;
569 }
570 }
571
572 /*
573 * Allocate the PF_KEY message that follows KEYSOCK_OUT.
574 * The alg reader lock needs to be held while allocating
575 * the variable part (i.e. the algorithms) of the message.
576 */
577
578 mutex_enter(&ipss->ipsec_alg_lock);
579
580 /*
581 * Return only valid algorithms, so the number of algorithms
582 * to send up may be less than the number of algorithm entries
583 * in the table.
584 */
585 authalgs = ipss->ipsec_alglists[IPSEC_ALG_AUTH];
586 for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++)
587 if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
588 num_aalgs++;
589
590 /*
591 * Fill SADB_REGISTER message's algorithm descriptors. Hold
592 * down the lock while filling it.
593 */
594 if (num_aalgs != 0) {
595 allocsize += (num_aalgs * sizeof (*saalg));
596 allocsize += sizeof (*sasupp);
597 }
598 mp->b_cont = allocb(allocsize, BPRI_HI);
599 if (mp->b_cont == NULL) {
600 mutex_exit(&ipss->ipsec_alg_lock);
601 freemsg(mp);
602 return (B_FALSE);
603 }
604
605 mp->b_cont->b_wptr += allocsize;
606 nextext = (sadb_ext_t *)(mp->b_cont->b_rptr + sizeof (*samsg));
607
608 if (num_aalgs != 0) {
609
610 saalg = (sadb_alg_t *)(((uint8_t *)nextext) + sizeof (*sasupp));
611 ASSERT(((ulong_t)saalg & 0x7) == 0);
612
613 numalgs_snap = 0;
614 for (i = 0;
615 ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs));
616 i++) {
617 if (authalgs[i] == NULL || !ALG_VALID(authalgs[i]))
618 continue;
619
620 saalg->sadb_alg_id = authalgs[i]->alg_id;
621 saalg->sadb_alg_ivlen = 0;
622 saalg->sadb_alg_minbits = authalgs[i]->alg_ef_minbits;
623 saalg->sadb_alg_maxbits = authalgs[i]->alg_ef_maxbits;
624 saalg->sadb_x_alg_increment =
625 authalgs[i]->alg_increment;
626 /* For now, salt is meaningless in AH. */
627 ASSERT(authalgs[i]->alg_saltlen == 0);
628 saalg->sadb_x_alg_saltbits =
629 SADB_8TO1(authalgs[i]->alg_saltlen);
630 numalgs_snap++;
631 saalg++;
632 }
633 ASSERT(numalgs_snap == num_aalgs);
634 #ifdef DEBUG
635 /*
636 * Reality check to make sure I snagged all of the
637 * algorithms.
638 */
639 for (; i < IPSEC_MAX_ALGS; i++)
640 if (authalgs[i] != NULL && ALG_VALID(authalgs[i]))
641 cmn_err(CE_PANIC,
642 "ah_register_out()! Missed #%d.\n", i);
643 #endif /* DEBUG */
644 nextext = (sadb_ext_t *)saalg;
645 }
646
647 mutex_exit(&ipss->ipsec_alg_lock);
648
649 if (sens_tsl != NULL) {
650 sens = (sadb_sens_t *)nextext;
651 sadb_sens_from_label(sens, SADB_EXT_SENSITIVITY,
652 sens_tsl, sens_len);
653
654 nextext = (sadb_ext_t *)(((uint8_t *)sens) + sens_len);
655 }
656
657 /* Now fill the restof the SADB_REGISTER message. */
658
659 samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
660 samsg->sadb_msg_version = PF_KEY_V2;
661 samsg->sadb_msg_type = SADB_REGISTER;
662 samsg->sadb_msg_errno = 0;
663 samsg->sadb_msg_satype = SADB_SATYPE_AH;
664 samsg->sadb_msg_len = SADB_8TO64(allocsize);
665 samsg->sadb_msg_reserved = 0;
666 /*
667 * Assume caller has sufficient sequence/pid number info. If it's one
668 * from me over a new alg., I could give two hoots about sequence.
669 */
670 samsg->sadb_msg_seq = sequence;
671 samsg->sadb_msg_pid = pid;
672
673 if (num_aalgs != 0) {
674 sasupp = (sadb_supported_t *)(samsg + 1);
675 sasupp->sadb_supported_len = SADB_8TO64(
676 sizeof (*sasupp) + sizeof (*saalg) * num_aalgs);
677 sasupp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
678 sasupp->sadb_supported_reserved = 0;
679 }
680
681 if (ahstack->ah_pfkey_q != NULL)
682 putnext(ahstack->ah_pfkey_q, mp);
683 else {
684 rc = B_FALSE;
685 freemsg(mp);
686 }
687
688 return (rc);
689 }
690
691 /*
692 * Invoked when the algorithm table changes. Causes SADB_REGISTER
693 * messages continaining the current list of algorithms to be
694 * sent up to the AH listeners.
695 */
696 void
697 ipsecah_algs_changed(netstack_t *ns)
698 {
699 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
700
701 /*
702 * Time to send a PF_KEY SADB_REGISTER message to AH listeners
703 * everywhere. (The function itself checks for NULL ah_pfkey_q.)
704 */
705 (void) ah_register_out(0, 0, 0, ahstack, NULL);
706 }
707
708 /*
709 * Stub function that taskq_dispatch() invokes to take the mblk (in arg)
710 * and send it into AH and IP again.
711 */
712 static void
713 inbound_task(void *arg)
714 {
715 mblk_t *mp = (mblk_t *)arg;
716 mblk_t *async_mp;
717 ip_recv_attr_t iras;
718
719 async_mp = mp;
720 mp = async_mp->b_cont;
721 async_mp->b_cont = NULL;
722 if (!ip_recv_attr_from_mblk(async_mp, &iras)) {
723 /* The ill or ip_stack_t disappeared on us */
724 ip_drop_input("ip_recv_attr_from_mblk", mp, NULL);
725 freemsg(mp);
726 goto done;
727 }
728
729 ah_inbound_restart(mp, &iras);
730 done:
731 ira_cleanup(&iras, B_TRUE);
732 }
733
734 /*
735 * Restart ESP after the SA has been added.
736 */
737 static void
738 ah_inbound_restart(mblk_t *mp, ip_recv_attr_t *ira)
739 {
740 ah_t *ah;
741 netstack_t *ns;
742 ipsecah_stack_t *ahstack;
743
744 ns = ira->ira_ill->ill_ipst->ips_netstack;
745 ahstack = ns->netstack_ipsecah;
746
747 ASSERT(ahstack != NULL);
748 mp = ipsec_inbound_ah_sa(mp, ira, &ah);
749 if (mp == NULL)
750 return;
751
752 ASSERT(ah != NULL);
753 ASSERT(ira->ira_flags & IRAF_IPSEC_SECURE);
754 ASSERT(ira->ira_ipsec_ah_sa != NULL);
755
756 mp = ira->ira_ipsec_ah_sa->ipsa_input_func(mp, ah, ira);
757 if (mp == NULL) {
758 /*
759 * Either it failed or is pending. In the former case
760 * ipIfStatsInDiscards was increased.
761 */
762 return;
763 }
764 ip_input_post_ipsec(mp, ira);
765 }
766
767 /*
768 * Now that weak-key passed, actually ADD the security association, and
769 * send back a reply ADD message.
770 */
771 static int
772 ah_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi,
773 int *diagnostic, ipsecah_stack_t *ahstack)
774 {
775 isaf_t *primary = NULL, *secondary;
776 boolean_t clone = B_FALSE, is_inbound = B_FALSE;
777 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
778 ipsa_t *larval;
779 ipsacq_t *acqrec;
780 iacqf_t *acq_bucket;
781 mblk_t *acq_msgs = NULL;
782 mblk_t *lpkt;
783 int rc;
784 ipsa_query_t sq;
785 int error;
786 netstack_t *ns = ahstack->ipsecah_netstack;
787 ipsec_stack_t *ipss = ns->netstack_ipsec;
788
789 /*
790 * Locate the appropriate table(s).
791 */
792
793 sq.spp = &ahstack->ah_sadb;
794 error = sadb_form_query(ksi, IPSA_Q_SA|IPSA_Q_DST,
795 IPSA_Q_SA|IPSA_Q_DST|IPSA_Q_INBOUND|IPSA_Q_OUTBOUND,
796 &sq, diagnostic);
797 if (error)
798 return (error);
799
800 /*
801 * Use the direction flags provided by the KMD to determine
802 * if the inbound or outbound table should be the primary
803 * for this SA. If these flags were absent then make this
804 * decision based on the addresses.
805 */
806 if (assoc->sadb_sa_flags & IPSA_F_INBOUND) {
807 primary = sq.inbound;
808 secondary = sq.outbound;
809 is_inbound = B_TRUE;
810 if (assoc->sadb_sa_flags & IPSA_F_OUTBOUND)
811 clone = B_TRUE;
812 } else {
813 if (assoc->sadb_sa_flags & IPSA_F_OUTBOUND) {
814 primary = sq.outbound;
815 secondary = sq.inbound;
816 }
817 }
818 if (primary == NULL) {
819 /*
820 * The KMD did not set a direction flag, determine which
821 * table to insert the SA into based on addresses.
822 */
823 switch (ksi->ks_in_dsttype) {
824 case KS_IN_ADDR_MBCAST:
825 clone = B_TRUE; /* All mcast SAs can be bidirectional */
826 assoc->sadb_sa_flags |= IPSA_F_OUTBOUND;
827 /* FALLTHRU */
828 /*
829 * If the source address is either one of mine, or unspecified
830 * (which is best summed up by saying "not 'not mine'"),
831 * then the association is potentially bi-directional,
832 * in that it can be used for inbound traffic and outbound
833 * traffic. The best example of such and SA is a multicast
834 * SA (which allows me to receive the outbound traffic).
835 */
836 case KS_IN_ADDR_ME:
837 assoc->sadb_sa_flags |= IPSA_F_INBOUND;
838 primary = sq.inbound;
839 secondary = sq.outbound;
840 if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME)
841 clone = B_TRUE;
842 is_inbound = B_TRUE;
843 break;
844
845 /*
846 * If the source address literally not mine (either
847 * unspecified or not mine), then this SA may have an
848 * address that WILL be mine after some configuration.
849 * We pay the price for this by making it a bi-directional
850 * SA.
851 */
852 case KS_IN_ADDR_NOTME:
853 assoc->sadb_sa_flags |= IPSA_F_OUTBOUND;
854 primary = sq.outbound;
855 secondary = sq.inbound;
856 if (ksi->ks_in_srctype != KS_IN_ADDR_ME) {
857 assoc->sadb_sa_flags |= IPSA_F_INBOUND;
858 clone = B_TRUE;
859 }
860 break;
861 default:
862 *diagnostic = SADB_X_DIAGNOSTIC_BAD_DST;
863 return (EINVAL);
864 }
865 }
866
867 /*
868 * Find a ACQUIRE list entry if possible. If we've added an SA that
869 * suits the needs of an ACQUIRE list entry, we can eliminate the
870 * ACQUIRE list entry and transmit the enqueued packets. Use the
871 * high-bit of the sequence number to queue it. Key off destination
872 * addr, and change acqrec's state.
873 */
874
875 if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) {
876 acq_bucket = &(sq.sp->sdb_acq[sq.outhash]);
877 mutex_enter(&acq_bucket->iacqf_lock);
878 for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL;
879 acqrec = acqrec->ipsacq_next) {
880 mutex_enter(&acqrec->ipsacq_lock);
881 /*
882 * Q: I only check sequence. Should I check dst?
883 * A: Yes, check dest because those are the packets
884 * that are queued up.
885 */
886 if (acqrec->ipsacq_seq == samsg->sadb_msg_seq &&
887 IPSA_ARE_ADDR_EQUAL(sq.dstaddr,
888 acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam))
889 break;
890 mutex_exit(&acqrec->ipsacq_lock);
891 }
892 if (acqrec != NULL) {
893 /*
894 * AHA! I found an ACQUIRE record for this SA.
895 * Grab the msg list, and free the acquire record.
896 * I already am holding the lock for this record,
897 * so all I have to do is free it.
898 */
899 acq_msgs = acqrec->ipsacq_mp;
900 acqrec->ipsacq_mp = NULL;
901 mutex_exit(&acqrec->ipsacq_lock);
902 sadb_destroy_acquire(acqrec, ns);
903 }
904 mutex_exit(&acq_bucket->iacqf_lock);
905 }
906
907 /*
908 * Find PF_KEY message, and see if I'm an update. If so, find entry
909 * in larval list (if there).
910 */
911
912 larval = NULL;
913
914 if (samsg->sadb_msg_type == SADB_UPDATE) {
915 mutex_enter(&sq.inbound->isaf_lock);
916 larval = ipsec_getassocbyspi(sq.inbound, sq.assoc->sadb_sa_spi,
917 ALL_ZEROES_PTR, sq.dstaddr, sq.dst->sin_family);
918 mutex_exit(&sq.inbound->isaf_lock);
919
920 if ((larval == NULL) ||
921 (larval->ipsa_state != IPSA_STATE_LARVAL)) {
922 *diagnostic = SADB_X_DIAGNOSTIC_SA_NOTFOUND;
923 if (larval != NULL) {
924 IPSA_REFRELE(larval);
925 }
926 ah0dbg(("Larval update, but larval disappeared.\n"));
927 return (ESRCH);
928 } /* Else sadb_common_add unlinks it for me! */
929 }
930
931 if (larval != NULL) {
932 /*
933 * Hold again, because sadb_common_add() consumes a reference,
934 * and we don't want to clear_lpkt() without a reference.
935 */
936 IPSA_REFHOLD(larval);
937 }
938
939 rc = sadb_common_add(ahstack->ah_pfkey_q, mp,
940 samsg, ksi, primary, secondary, larval, clone, is_inbound,
941 diagnostic, ns, &ahstack->ah_sadb);
942
943 if (larval != NULL) {
944 if (rc == 0) {
945 lpkt = sadb_clear_lpkt(larval);
946 if (lpkt != NULL) {
947 rc = !taskq_dispatch(ah_taskq, inbound_task,
948 lpkt, TQ_NOSLEEP);
949 }
950 }
951 IPSA_REFRELE(larval);
952 }
953
954 /*
955 * How much more stack will I create with all of these
956 * ah_outbound_*() calls?
957 */
958
959 /* Handle the packets queued waiting for the SA */
960 while (acq_msgs != NULL) {
961 mblk_t *asyncmp;
962 mblk_t *data_mp;
963 ip_xmit_attr_t ixas;
964 ill_t *ill;
965
966 asyncmp = acq_msgs;
967 acq_msgs = acq_msgs->b_next;
968 asyncmp->b_next = NULL;
969
970 /*
971 * Extract the ip_xmit_attr_t from the first mblk.
972 * Verifies that the netstack and ill is still around; could
973 * have vanished while iked was doing its work.
974 * On succesful return we have a nce_t and the ill/ipst can't
975 * disappear until we do the nce_refrele in ixa_cleanup.
976 */
977 data_mp = asyncmp->b_cont;
978 asyncmp->b_cont = NULL;
979 if (!ip_xmit_attr_from_mblk(asyncmp, &ixas)) {
980 AH_BUMP_STAT(ahstack, out_discards);
981 ip_drop_packet(data_mp, B_FALSE, NULL,
982 DROPPER(ipss, ipds_sadb_acquire_timeout),
983 &ahstack->ah_dropper);
984 } else if (rc != 0) {
985 ill = ixas.ixa_nce->nce_ill;
986 AH_BUMP_STAT(ahstack, out_discards);
987 ip_drop_packet(data_mp, B_FALSE, ill,
988 DROPPER(ipss, ipds_sadb_acquire_timeout),
989 &ahstack->ah_dropper);
990 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
991 } else {
992 ah_outbound_finish(data_mp, &ixas);
993 }
994 ixa_cleanup(&ixas);
995 }
996
997 return (rc);
998 }
999
1000
1001 /*
1002 * Process one of the queued messages (from ipsacq_mp) once the SA
1003 * has been added.
1004 */
1005 static void
1006 ah_outbound_finish(mblk_t *data_mp, ip_xmit_attr_t *ixa)
1007 {
1008 netstack_t *ns = ixa->ixa_ipst->ips_netstack;
1009 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1010 ipsec_stack_t *ipss = ns->netstack_ipsec;
1011 ill_t *ill = ixa->ixa_nce->nce_ill;
1012
1013 if (!ipsec_outbound_sa(data_mp, ixa, IPPROTO_AH)) {
1014 AH_BUMP_STAT(ahstack, out_discards);
1015 ip_drop_packet(data_mp, B_FALSE, ill,
1016 DROPPER(ipss, ipds_sadb_acquire_timeout),
1017 &ahstack->ah_dropper);
1018 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
1019 return;
1020 }
1021
1022 data_mp = ah_outbound(data_mp, ixa);
1023 if (data_mp == NULL)
1024 return;
1025
1026 (void) ip_output_post_ipsec(data_mp, ixa);
1027 }
1028
1029 /*
1030 * Add new AH security association. This may become a generic AH/ESP
1031 * routine eventually.
1032 */
1033 static int
1034 ah_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic, netstack_t *ns)
1035 {
1036 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
1037 sadb_address_t *srcext =
1038 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
1039 sadb_address_t *dstext =
1040 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
1041 sadb_address_t *isrcext =
1042 (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC];
1043 sadb_address_t *idstext =
1044 (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_DST];
1045 sadb_key_t *key = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH];
1046 struct sockaddr_in *src, *dst;
1047 /* We don't need sockaddr_in6 for now. */
1048 sadb_lifetime_t *soft =
1049 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT];
1050 sadb_lifetime_t *hard =
1051 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD];
1052 sadb_lifetime_t *idle =
1053 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_X_EXT_LIFETIME_IDLE];
1054 ipsec_alginfo_t *aalg;
1055 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1056 ipsec_stack_t *ipss = ns->netstack_ipsec;
1057
1058 /* I need certain extensions present for an ADD message. */
1059 if (srcext == NULL) {
1060 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC;
1061 return (EINVAL);
1062 }
1063 if (dstext == NULL) {
1064 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
1065 return (EINVAL);
1066 }
1067 if (isrcext == NULL && idstext != NULL) {
1068 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC;
1069 return (EINVAL);
1070 }
1071 if (isrcext != NULL && idstext == NULL) {
1072 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST;
1073 return (EINVAL);
1074 }
1075 if (assoc == NULL) {
1076 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
1077 return (EINVAL);
1078 }
1079 if (key == NULL) {
1080 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_AKEY;
1081 return (EINVAL);
1082 }
1083
1084 src = (struct sockaddr_in *)(srcext + 1);
1085 dst = (struct sockaddr_in *)(dstext + 1);
1086
1087 /* Sundry ADD-specific reality checks. */
1088 /* XXX STATS : Logging/stats here? */
1089
1090 if ((assoc->sadb_sa_state != SADB_SASTATE_MATURE) &&
1091 (assoc->sadb_sa_state != SADB_X_SASTATE_ACTIVE_ELSEWHERE)) {
1092 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE;
1093 return (EINVAL);
1094 }
1095 if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) {
1096 *diagnostic = SADB_X_DIAGNOSTIC_ENCR_NOTSUPP;
1097 return (EINVAL);
1098 }
1099 if (assoc->sadb_sa_flags & ~ahstack->ah_sadb.s_addflags) {
1100 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS;
1101 return (EINVAL);
1102 }
1103 if ((*diagnostic = sadb_hardsoftchk(hard, soft, idle)) != 0)
1104 return (EINVAL);
1105
1106 ASSERT(src->sin_family == dst->sin_family);
1107
1108 /* Stuff I don't support, for now. XXX Diagnostic? */
1109 if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL)
1110 return (EOPNOTSUPP);
1111
1112 if (ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL) {
1113 if (!is_system_labeled())
1114 return (EOPNOTSUPP);
1115 }
1116
1117 if (ksi->ks_in_extv[SADB_X_EXT_OUTER_SENS] != NULL) {
1118 if (!is_system_labeled())
1119 return (EOPNOTSUPP);
1120 }
1121 /*
1122 * XXX Policy : I'm not checking identities at this time, but
1123 * if I did, I'd do them here, before I sent the weak key
1124 * check up to the algorithm.
1125 */
1126
1127 /* verify that there is a mapping for the specified algorithm */
1128 mutex_enter(&ipss->ipsec_alg_lock);
1129 aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH][assoc->sadb_sa_auth];
1130 if (aalg == NULL || !ALG_VALID(aalg)) {
1131 mutex_exit(&ipss->ipsec_alg_lock);
1132 ah1dbg(ahstack, ("Couldn't find auth alg #%d.\n",
1133 assoc->sadb_sa_auth));
1134 *diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG;
1135 return (EINVAL);
1136 }
1137 ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID);
1138
1139 /* sanity check key sizes */
1140 if (!ipsec_valid_key_size(key->sadb_key_bits, aalg)) {
1141 mutex_exit(&ipss->ipsec_alg_lock);
1142 *diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS;
1143 return (EINVAL);
1144 }
1145
1146 /* check key and fix parity if needed */
1147 if (ipsec_check_key(aalg->alg_mech_type, key, B_TRUE,
1148 diagnostic) != 0) {
1149 mutex_exit(&ipss->ipsec_alg_lock);
1150 return (EINVAL);
1151 }
1152
1153 mutex_exit(&ipss->ipsec_alg_lock);
1154
1155 return (ah_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi,
1156 diagnostic, ahstack));
1157 }
1158
1159 /* Refactor me */
1160 /*
1161 * Update a security association. Updates come in two varieties. The first
1162 * is an update of lifetimes on a non-larval SA. The second is an update of
1163 * a larval SA, which ends up looking a lot more like an add.
1164 */
1165 static int
1166 ah_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
1167 ipsecah_stack_t *ahstack, uint8_t sadb_msg_type)
1168 {
1169 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
1170 sadb_address_t *dstext =
1171 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
1172 mblk_t *buf_pkt;
1173 int rcode;
1174
1175 if (dstext == NULL) {
1176 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST;
1177 return (EINVAL);
1178 }
1179
1180 rcode = sadb_update_sa(mp, ksi, &buf_pkt, &ahstack->ah_sadb,
1181 diagnostic, ahstack->ah_pfkey_q, ah_add_sa,
1182 ahstack->ipsecah_netstack, sadb_msg_type);
1183
1184 if ((assoc->sadb_sa_state != SADB_X_SASTATE_ACTIVE) ||
1185 (rcode != 0)) {
1186 return (rcode);
1187 }
1188
1189 HANDLE_BUF_PKT(ah_taskq, ahstack->ipsecah_netstack->netstack_ipsec,
1190 ahstack->ah_dropper, buf_pkt);
1191
1192 return (rcode);
1193 }
1194
1195 /* Refactor me */
1196 /*
1197 * Delete a security association. This is REALLY likely to be code common to
1198 * both AH and ESP. Find the association, then unlink it.
1199 */
1200 static int
1201 ah_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic,
1202 ipsecah_stack_t *ahstack, uint8_t sadb_msg_type)
1203 {
1204 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA];
1205 sadb_address_t *dstext =
1206 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST];
1207 sadb_address_t *srcext =
1208 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC];
1209 struct sockaddr_in *sin;
1210
1211 if (assoc == NULL) {
1212 if (dstext != NULL)
1213 sin = (struct sockaddr_in *)(dstext + 1);
1214 else if (srcext != NULL)
1215 sin = (struct sockaddr_in *)(srcext + 1);
1216 else {
1217 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA;
1218 return (EINVAL);
1219 }
1220 return (sadb_purge_sa(mp, ksi,
1221 (sin->sin_family == AF_INET6) ? &ahstack->ah_sadb.s_v6 :
1222 &ahstack->ah_sadb.s_v4, diagnostic, ahstack->ah_pfkey_q));
1223 }
1224
1225 return (sadb_delget_sa(mp, ksi, &ahstack->ah_sadb, diagnostic,
1226 ahstack->ah_pfkey_q, sadb_msg_type));
1227 }
1228
1229 /* Refactor me */
1230 /*
1231 * Convert the entire contents of all of AH's SA tables into PF_KEY SADB_DUMP
1232 * messages.
1233 */
1234 static void
1235 ah_dump(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
1236 {
1237 int error;
1238 sadb_msg_t *samsg;
1239
1240 /*
1241 * Dump each fanout, bailing if error is non-zero.
1242 */
1243
1244 error = sadb_dump(ahstack->ah_pfkey_q, mp, ksi, &ahstack->ah_sadb.s_v4);
1245 if (error != 0)
1246 goto bail;
1247
1248 error = sadb_dump(ahstack->ah_pfkey_q, mp, ksi, &ahstack->ah_sadb.s_v6);
1249 bail:
1250 ASSERT(mp->b_cont != NULL);
1251 samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
1252 samsg->sadb_msg_errno = (uint8_t)error;
1253 sadb_pfkey_echo(ahstack->ah_pfkey_q, mp,
1254 (sadb_msg_t *)mp->b_cont->b_rptr, ksi, NULL);
1255 }
1256
1257 /*
1258 * First-cut reality check for an inbound PF_KEY message.
1259 */
1260 static boolean_t
1261 ah_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi,
1262 ipsecah_stack_t *ahstack)
1263 {
1264 int diagnostic;
1265
1266 if (mp->b_cont == NULL) {
1267 freemsg(mp);
1268 return (B_TRUE);
1269 }
1270
1271 if (ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT] != NULL) {
1272 diagnostic = SADB_X_DIAGNOSTIC_EKEY_PRESENT;
1273 goto badmsg;
1274 }
1275 if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) {
1276 diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT;
1277 goto badmsg;
1278 }
1279 if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL ||
1280 ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) {
1281 diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT;
1282 goto badmsg;
1283 }
1284 return (B_FALSE); /* False ==> no failures */
1285
1286 badmsg:
1287 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL,
1288 diagnostic, ksi->ks_in_serial);
1289 return (B_TRUE); /* True ==> failures */
1290 }
1291
1292 /*
1293 * AH parsing of PF_KEY messages. Keysock did most of the really silly
1294 * error cases. What I receive is a fully-formed, syntactically legal
1295 * PF_KEY message. I then need to check semantics...
1296 *
1297 * This code may become common to AH and ESP. Stay tuned.
1298 *
1299 * I also make the assumption that db_ref's are cool. If this assumption
1300 * is wrong, this means that someone other than keysock or me has been
1301 * mucking with PF_KEY messages.
1302 */
1303 static void
1304 ah_parse_pfkey(mblk_t *mp, ipsecah_stack_t *ahstack)
1305 {
1306 mblk_t *msg = mp->b_cont;
1307 sadb_msg_t *samsg;
1308 keysock_in_t *ksi;
1309 int error;
1310 int diagnostic = SADB_X_DIAGNOSTIC_NONE;
1311
1312 ASSERT(msg != NULL);
1313
1314 samsg = (sadb_msg_t *)msg->b_rptr;
1315 ksi = (keysock_in_t *)mp->b_rptr;
1316
1317 /*
1318 * If applicable, convert unspecified AF_INET6 to unspecified
1319 * AF_INET.
1320 */
1321 if (!sadb_addrfix(ksi, ahstack->ah_pfkey_q, mp,
1322 ahstack->ipsecah_netstack) ||
1323 ah_pfkey_reality_failures(mp, ksi, ahstack)) {
1324 return;
1325 }
1326
1327 switch (samsg->sadb_msg_type) {
1328 case SADB_ADD:
1329 error = ah_add_sa(mp, ksi, &diagnostic,
1330 ahstack->ipsecah_netstack);
1331 if (error != 0) {
1332 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1333 diagnostic, ksi->ks_in_serial);
1334 }
1335 /* else ah_add_sa() took care of things. */
1336 break;
1337 case SADB_DELETE:
1338 case SADB_X_DELPAIR:
1339 case SADB_X_DELPAIR_STATE:
1340 error = ah_del_sa(mp, ksi, &diagnostic, ahstack,
1341 samsg->sadb_msg_type);
1342 if (error != 0) {
1343 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1344 diagnostic, ksi->ks_in_serial);
1345 }
1346 /* Else ah_del_sa() took care of things. */
1347 break;
1348 case SADB_GET:
1349 error = sadb_delget_sa(mp, ksi, &ahstack->ah_sadb, &diagnostic,
1350 ahstack->ah_pfkey_q, samsg->sadb_msg_type);
1351 if (error != 0) {
1352 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1353 diagnostic, ksi->ks_in_serial);
1354 }
1355 /* Else sadb_get_sa() took care of things. */
1356 break;
1357 case SADB_FLUSH:
1358 sadbp_flush(&ahstack->ah_sadb, ahstack->ipsecah_netstack);
1359 sadb_pfkey_echo(ahstack->ah_pfkey_q, mp, samsg, ksi, NULL);
1360 break;
1361 case SADB_REGISTER:
1362 /*
1363 * Hmmm, let's do it! Check for extensions (there should
1364 * be none), extract the fields, call ah_register_out(),
1365 * then either free or report an error.
1366 *
1367 * Keysock takes care of the PF_KEY bookkeeping for this.
1368 */
1369 if (ah_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid,
1370 ksi->ks_in_serial, ahstack, msg_getcred(mp, NULL))) {
1371 freemsg(mp);
1372 } else {
1373 /*
1374 * Only way this path hits is if there is a memory
1375 * failure. It will not return B_FALSE because of
1376 * lack of ah_pfkey_q if I am in wput().
1377 */
1378 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, ENOMEM,
1379 diagnostic, ksi->ks_in_serial);
1380 }
1381 break;
1382 case SADB_UPDATE:
1383 case SADB_X_UPDATEPAIR:
1384 /*
1385 * Find a larval, if not there, find a full one and get
1386 * strict.
1387 */
1388 error = ah_update_sa(mp, ksi, &diagnostic, ahstack,
1389 samsg->sadb_msg_type);
1390 if (error != 0) {
1391 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, error,
1392 diagnostic, ksi->ks_in_serial);
1393 }
1394 /* else ah_update_sa() took care of things. */
1395 break;
1396 case SADB_GETSPI:
1397 /*
1398 * Reserve a new larval entry.
1399 */
1400 ah_getspi(mp, ksi, ahstack);
1401 break;
1402 case SADB_ACQUIRE:
1403 /*
1404 * Find larval and/or ACQUIRE record and kill it (them), I'm
1405 * most likely an error. Inbound ACQUIRE messages should only
1406 * have the base header.
1407 */
1408 sadb_in_acquire(samsg, &ahstack->ah_sadb, ahstack->ah_pfkey_q,
1409 ahstack->ipsecah_netstack);
1410 freemsg(mp);
1411 break;
1412 case SADB_DUMP:
1413 /*
1414 * Dump all entries.
1415 */
1416 ah_dump(mp, ksi, ahstack);
1417 /* ah_dump will take care of the return message, etc. */
1418 break;
1419 case SADB_EXPIRE:
1420 /* Should never reach me. */
1421 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EOPNOTSUPP,
1422 diagnostic, ksi->ks_in_serial);
1423 break;
1424 default:
1425 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL,
1426 SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial);
1427 break;
1428 }
1429 }
1430
1431 /*
1432 * Handle case where PF_KEY says it can't find a keysock for one of my
1433 * ACQUIRE messages.
1434 */
1435 static void
1436 ah_keysock_no_socket(mblk_t *mp, ipsecah_stack_t *ahstack)
1437 {
1438 sadb_msg_t *samsg;
1439 keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr;
1440
1441 if (mp->b_cont == NULL) {
1442 freemsg(mp);
1443 return;
1444 }
1445 samsg = (sadb_msg_t *)mp->b_cont->b_rptr;
1446
1447 /*
1448 * If keysock can't find any registered, delete the acquire record
1449 * immediately, and handle errors.
1450 */
1451 if (samsg->sadb_msg_type == SADB_ACQUIRE) {
1452 samsg->sadb_msg_errno = kse->ks_err_errno;
1453 samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg));
1454 /*
1455 * Use the write-side of the ah_pfkey_q
1456 */
1457 sadb_in_acquire(samsg, &ahstack->ah_sadb,
1458 WR(ahstack->ah_pfkey_q), ahstack->ipsecah_netstack);
1459 }
1460
1461 freemsg(mp);
1462 }
1463
1464 /*
1465 * AH module write put routine.
1466 */
1467 static void
1468 ipsecah_wput(queue_t *q, mblk_t *mp)
1469 {
1470 ipsec_info_t *ii;
1471 struct iocblk *iocp;
1472 ipsecah_stack_t *ahstack = (ipsecah_stack_t *)q->q_ptr;
1473
1474 ah3dbg(ahstack, ("In ah_wput().\n"));
1475
1476 /* NOTE: Each case must take care of freeing or passing mp. */
1477 switch (mp->b_datap->db_type) {
1478 case M_CTL:
1479 if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) {
1480 /* Not big enough message. */
1481 freemsg(mp);
1482 break;
1483 }
1484 ii = (ipsec_info_t *)mp->b_rptr;
1485
1486 switch (ii->ipsec_info_type) {
1487 case KEYSOCK_OUT_ERR:
1488 ah1dbg(ahstack, ("Got KEYSOCK_OUT_ERR message.\n"));
1489 ah_keysock_no_socket(mp, ahstack);
1490 break;
1491 case KEYSOCK_IN:
1492 AH_BUMP_STAT(ahstack, keysock_in);
1493 ah3dbg(ahstack, ("Got KEYSOCK_IN message.\n"));
1494
1495 /* Parse the message. */
1496 ah_parse_pfkey(mp, ahstack);
1497 break;
1498 case KEYSOCK_HELLO:
1499 sadb_keysock_hello(&ahstack->ah_pfkey_q, q, mp,
1500 ah_ager, (void *)ahstack, &ahstack->ah_event,
1501 SADB_SATYPE_AH);
1502 break;
1503 default:
1504 ah1dbg(ahstack, ("Got M_CTL from above of 0x%x.\n",
1505 ii->ipsec_info_type));
1506 freemsg(mp);
1507 break;
1508 }
1509 break;
1510 case M_IOCTL:
1511 iocp = (struct iocblk *)mp->b_rptr;
1512 switch (iocp->ioc_cmd) {
1513 case ND_SET:
1514 case ND_GET:
1515 if (nd_getset(q, ahstack->ipsecah_g_nd, mp)) {
1516 qreply(q, mp);
1517 return;
1518 } else {
1519 iocp->ioc_error = ENOENT;
1520 }
1521 /* FALLTHRU */
1522 default:
1523 /* We really don't support any other ioctls, do we? */
1524
1525 /* Return EINVAL */
1526 if (iocp->ioc_error != ENOENT)
1527 iocp->ioc_error = EINVAL;
1528 iocp->ioc_count = 0;
1529 mp->b_datap->db_type = M_IOCACK;
1530 qreply(q, mp);
1531 return;
1532 }
1533 default:
1534 ah3dbg(ahstack,
1535 ("Got default message, type %d, passing to IP.\n",
1536 mp->b_datap->db_type));
1537 putnext(q, mp);
1538 }
1539 }
1540
1541 /* Refactor me */
1542 /*
1543 * Updating use times can be tricky business if the ipsa_haspeer flag is
1544 * set. This function is called once in an SA's lifetime.
1545 *
1546 * Caller has to REFRELE "assoc" which is passed in. This function has
1547 * to REFRELE any peer SA that is obtained.
1548 */
1549 static void
1550 ah_set_usetime(ipsa_t *assoc, boolean_t inbound)
1551 {
1552 ipsa_t *inassoc, *outassoc;
1553 isaf_t *bucket;
1554 sadb_t *sp;
1555 int outhash;
1556 boolean_t isv6;
1557 netstack_t *ns = assoc->ipsa_netstack;
1558 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1559
1560 /* No peer? No problem! */
1561 if (!assoc->ipsa_haspeer) {
1562 sadb_set_usetime(assoc);
1563 return;
1564 }
1565
1566 /*
1567 * Otherwise, we want to grab both the original assoc and its peer.
1568 * There might be a race for this, but if it's a real race, the times
1569 * will be out-of-synch by at most a second, and since our time
1570 * granularity is a second, this won't be a problem.
1571 *
1572 * If we need tight synchronization on the peer SA, then we need to
1573 * reconsider.
1574 */
1575
1576 /* Use address family to select IPv6/IPv4 */
1577 isv6 = (assoc->ipsa_addrfam == AF_INET6);
1578 if (isv6) {
1579 sp = &ahstack->ah_sadb.s_v6;
1580 } else {
1581 sp = &ahstack->ah_sadb.s_v4;
1582 ASSERT(assoc->ipsa_addrfam == AF_INET);
1583 }
1584 if (inbound) {
1585 inassoc = assoc;
1586 if (isv6)
1587 outhash = OUTBOUND_HASH_V6(sp,
1588 *((in6_addr_t *)&inassoc->ipsa_dstaddr));
1589 else
1590 outhash = OUTBOUND_HASH_V4(sp,
1591 *((ipaddr_t *)&inassoc->ipsa_dstaddr));
1592 bucket = &sp->sdb_of[outhash];
1593
1594 mutex_enter(&bucket->isaf_lock);
1595 outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
1596 inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
1597 inassoc->ipsa_addrfam);
1598 mutex_exit(&bucket->isaf_lock);
1599 if (outassoc == NULL) {
1600 /* Q: Do we wish to set haspeer == B_FALSE? */
1601 ah0dbg(("ah_set_usetime: "
1602 "can't find peer for inbound.\n"));
1603 sadb_set_usetime(inassoc);
1604 return;
1605 }
1606 } else {
1607 outassoc = assoc;
1608 bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
1609 mutex_enter(&bucket->isaf_lock);
1610 inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
1611 outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
1612 outassoc->ipsa_addrfam);
1613 mutex_exit(&bucket->isaf_lock);
1614 if (inassoc == NULL) {
1615 /* Q: Do we wish to set haspeer == B_FALSE? */
1616 ah0dbg(("ah_set_usetime: "
1617 "can't find peer for outbound.\n"));
1618 sadb_set_usetime(outassoc);
1619 return;
1620 }
1621 }
1622
1623 /* Update usetime on both. */
1624 sadb_set_usetime(inassoc);
1625 sadb_set_usetime(outassoc);
1626
1627 /*
1628 * REFRELE any peer SA.
1629 *
1630 * Because of the multi-line macro nature of IPSA_REFRELE, keep
1631 * them in { }.
1632 */
1633 if (inbound) {
1634 IPSA_REFRELE(outassoc);
1635 } else {
1636 IPSA_REFRELE(inassoc);
1637 }
1638 }
1639
1640 /* Refactor me */
1641 /*
1642 * Add a number of bytes to what the SA has protected so far. Return
1643 * B_TRUE if the SA can still protect that many bytes.
1644 *
1645 * Caller must REFRELE the passed-in assoc. This function must REFRELE
1646 * any obtained peer SA.
1647 */
1648 static boolean_t
1649 ah_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound)
1650 {
1651 ipsa_t *inassoc, *outassoc;
1652 isaf_t *bucket;
1653 boolean_t inrc, outrc, isv6;
1654 sadb_t *sp;
1655 int outhash;
1656 netstack_t *ns = assoc->ipsa_netstack;
1657 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1658
1659 /* No peer? No problem! */
1660 if (!assoc->ipsa_haspeer) {
1661 return (sadb_age_bytes(ahstack->ah_pfkey_q, assoc, bytes,
1662 B_TRUE));
1663 }
1664
1665 /*
1666 * Otherwise, we want to grab both the original assoc and its peer.
1667 * There might be a race for this, but if it's a real race, two
1668 * expire messages may occur. We limit this by only sending the
1669 * expire message on one of the peers, we'll pick the inbound
1670 * arbitrarily.
1671 *
1672 * If we need tight synchronization on the peer SA, then we need to
1673 * reconsider.
1674 */
1675
1676 /* Pick v4/v6 bucket based on addrfam. */
1677 isv6 = (assoc->ipsa_addrfam == AF_INET6);
1678 if (isv6) {
1679 sp = &ahstack->ah_sadb.s_v6;
1680 } else {
1681 sp = &ahstack->ah_sadb.s_v4;
1682 ASSERT(assoc->ipsa_addrfam == AF_INET);
1683 }
1684 if (inbound) {
1685 inassoc = assoc;
1686 if (isv6)
1687 outhash = OUTBOUND_HASH_V6(sp,
1688 *((in6_addr_t *)&inassoc->ipsa_dstaddr));
1689 else
1690 outhash = OUTBOUND_HASH_V4(sp,
1691 *((ipaddr_t *)&inassoc->ipsa_dstaddr));
1692 bucket = &sp->sdb_of[outhash];
1693 mutex_enter(&bucket->isaf_lock);
1694 outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi,
1695 inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr,
1696 inassoc->ipsa_addrfam);
1697 mutex_exit(&bucket->isaf_lock);
1698 if (outassoc == NULL) {
1699 /* Q: Do we wish to set haspeer == B_FALSE? */
1700 ah0dbg(("ah_age_bytes: "
1701 "can't find peer for inbound.\n"));
1702 return (sadb_age_bytes(ahstack->ah_pfkey_q, inassoc,
1703 bytes, B_TRUE));
1704 }
1705 } else {
1706 outassoc = assoc;
1707 bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi);
1708 mutex_enter(&bucket->isaf_lock);
1709 inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi,
1710 outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr,
1711 outassoc->ipsa_addrfam);
1712 mutex_exit(&bucket->isaf_lock);
1713 if (inassoc == NULL) {
1714 /* Q: Do we wish to set haspeer == B_FALSE? */
1715 ah0dbg(("ah_age_bytes: "
1716 "can't find peer for outbound.\n"));
1717 return (sadb_age_bytes(ahstack->ah_pfkey_q, outassoc,
1718 bytes, B_TRUE));
1719 }
1720 }
1721
1722 inrc = sadb_age_bytes(ahstack->ah_pfkey_q, inassoc, bytes, B_TRUE);
1723 outrc = sadb_age_bytes(ahstack->ah_pfkey_q, outassoc, bytes, B_FALSE);
1724
1725 /*
1726 * REFRELE any peer SA.
1727 *
1728 * Because of the multi-line macro nature of IPSA_REFRELE, keep
1729 * them in { }.
1730 */
1731 if (inbound) {
1732 IPSA_REFRELE(outassoc);
1733 } else {
1734 IPSA_REFRELE(inassoc);
1735 }
1736
1737 return (inrc && outrc);
1738 }
1739
1740 /*
1741 * Perform the really difficult work of inserting the proposed situation.
1742 * Called while holding the algorithm lock.
1743 */
1744 static void
1745 ah_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs,
1746 netstack_t *ns)
1747 {
1748 sadb_comb_t *comb = (sadb_comb_t *)(prop + 1);
1749 ipsec_action_t *ap;
1750 ipsec_prot_t *prot;
1751 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1752 ipsec_stack_t *ipss = ns->netstack_ipsec;
1753
1754 ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
1755
1756 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
1757 prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t));
1758 *(uint32_t *)(&prop->sadb_prop_replay) = 0; /* Quick zero-out! */
1759
1760 prop->sadb_prop_replay = ahstack->ipsecah_replay_size;
1761
1762 /*
1763 * Based upon algorithm properties, and what-not, prioritize a
1764 * proposal, based on the ordering of the AH algorithms in the
1765 * alternatives in the policy rule or socket that was placed
1766 * in the acquire record.
1767 */
1768
1769 for (ap = acqrec->ipsacq_act; ap != NULL;
1770 ap = ap->ipa_next) {
1771 ipsec_alginfo_t *aalg;
1772
1773 if ((ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY) ||
1774 (!ap->ipa_act.ipa_apply.ipp_use_ah))
1775 continue;
1776
1777 prot = &ap->ipa_act.ipa_apply;
1778
1779 ASSERT(prot->ipp_auth_alg > 0);
1780
1781 aalg = ipss->ipsec_alglists[IPSEC_ALG_AUTH]
1782 [prot->ipp_auth_alg];
1783 if (aalg == NULL || !ALG_VALID(aalg))
1784 continue;
1785
1786 /* XXX check aalg for duplicates??.. */
1787
1788 comb->sadb_comb_flags = 0;
1789 comb->sadb_comb_reserved = 0;
1790 comb->sadb_comb_encrypt = 0;
1791 comb->sadb_comb_encrypt_minbits = 0;
1792 comb->sadb_comb_encrypt_maxbits = 0;
1793
1794 comb->sadb_comb_auth = aalg->alg_id;
1795 comb->sadb_comb_auth_minbits =
1796 MAX(prot->ipp_ah_minbits, aalg->alg_ef_minbits);
1797 comb->sadb_comb_auth_maxbits =
1798 MIN(prot->ipp_ah_maxbits, aalg->alg_ef_maxbits);
1799
1800 /*
1801 * The following may be based on algorithm
1802 * properties, but in the meantime, we just pick
1803 * some good, sensible numbers. Key mgmt. can
1804 * (and perhaps should) be the place to finalize
1805 * such decisions.
1806 */
1807
1808 /*
1809 * No limits on allocations, since we really don't
1810 * support that concept currently.
1811 */
1812 comb->sadb_comb_soft_allocations = 0;
1813 comb->sadb_comb_hard_allocations = 0;
1814
1815 /*
1816 * These may want to come from policy rule..
1817 */
1818 comb->sadb_comb_soft_bytes =
1819 ahstack->ipsecah_default_soft_bytes;
1820 comb->sadb_comb_hard_bytes =
1821 ahstack->ipsecah_default_hard_bytes;
1822 comb->sadb_comb_soft_addtime =
1823 ahstack->ipsecah_default_soft_addtime;
1824 comb->sadb_comb_hard_addtime =
1825 ahstack->ipsecah_default_hard_addtime;
1826 comb->sadb_comb_soft_usetime =
1827 ahstack->ipsecah_default_soft_usetime;
1828 comb->sadb_comb_hard_usetime =
1829 ahstack->ipsecah_default_hard_usetime;
1830
1831 prop->sadb_prop_len += SADB_8TO64(sizeof (*comb));
1832 if (--combs == 0)
1833 return; /* out of space.. */
1834 comb++;
1835 }
1836 }
1837
1838 /*
1839 * Prepare and actually send the SADB_ACQUIRE message to PF_KEY.
1840 */
1841 static void
1842 ah_send_acquire(ipsacq_t *acqrec, mblk_t *extended, netstack_t *ns)
1843 {
1844 uint_t combs;
1845 sadb_msg_t *samsg;
1846 sadb_prop_t *prop;
1847 mblk_t *pfkeymp, *msgmp;
1848 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
1849 ipsec_stack_t *ipss = ns->netstack_ipsec;
1850
1851 AH_BUMP_STAT(ahstack, acquire_requests);
1852
1853 if (ahstack->ah_pfkey_q == NULL) {
1854 mutex_exit(&acqrec->ipsacq_lock);
1855 return;
1856 }
1857
1858 /* Set up ACQUIRE. */
1859 pfkeymp = sadb_setup_acquire(acqrec, SADB_SATYPE_AH,
1860 ns->netstack_ipsec);
1861 if (pfkeymp == NULL) {
1862 ah0dbg(("sadb_setup_acquire failed.\n"));
1863 mutex_exit(&acqrec->ipsacq_lock);
1864 return;
1865 }
1866 ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock));
1867 combs = ipss->ipsec_nalgs[IPSEC_ALG_AUTH];
1868 msgmp = pfkeymp->b_cont;
1869 samsg = (sadb_msg_t *)(msgmp->b_rptr);
1870
1871 /* Insert proposal here. */
1872
1873 prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len);
1874 ah_insert_prop(prop, acqrec, combs, ns);
1875 samsg->sadb_msg_len += prop->sadb_prop_len;
1876 msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len);
1877
1878 mutex_exit(&ipss->ipsec_alg_lock);
1879
1880 /*
1881 * Must mutex_exit() before sending PF_KEY message up, in
1882 * order to avoid recursive mutex_enter() if there are no registered
1883 * listeners.
1884 *
1885 * Once I've sent the message, I'm cool anyway.
1886 */
1887 mutex_exit(&acqrec->ipsacq_lock);
1888 if (extended != NULL) {
1889 putnext(ahstack->ah_pfkey_q, extended);
1890 }
1891 putnext(ahstack->ah_pfkey_q, pfkeymp);
1892 }
1893
1894 /* Refactor me */
1895 /*
1896 * Handle the SADB_GETSPI message. Create a larval SA.
1897 */
1898 static void
1899 ah_getspi(mblk_t *mp, keysock_in_t *ksi, ipsecah_stack_t *ahstack)
1900 {
1901 ipsa_t *newbie, *target;
1902 isaf_t *outbound, *inbound;
1903 int rc, diagnostic;
1904 sadb_sa_t *assoc;
1905 keysock_out_t *kso;
1906 uint32_t newspi;
1907
1908 /*
1909 * Randomly generate a proposed SPI value.
1910 */
1911 (void) random_get_pseudo_bytes((uint8_t *)&newspi, sizeof (uint32_t));
1912 newbie = sadb_getspi(ksi, newspi, &diagnostic,
1913 ahstack->ipsecah_netstack, IPPROTO_AH);
1914
1915 if (newbie == NULL) {
1916 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, ENOMEM, diagnostic,
1917 ksi->ks_in_serial);
1918 return;
1919 } else if (newbie == (ipsa_t *)-1) {
1920 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, EINVAL, diagnostic,
1921 ksi->ks_in_serial);
1922 return;
1923 }
1924
1925 /*
1926 * XXX - We may randomly collide. We really should recover from this.
1927 * Unfortunately, that could require spending way-too-much-time
1928 * in here. For now, let the user retry.
1929 */
1930
1931 if (newbie->ipsa_addrfam == AF_INET6) {
1932 outbound = OUTBOUND_BUCKET_V6(&ahstack->ah_sadb.s_v6,
1933 *(uint32_t *)(newbie->ipsa_dstaddr));
1934 inbound = INBOUND_BUCKET(&ahstack->ah_sadb.s_v6,
1935 newbie->ipsa_spi);
1936 } else {
1937 outbound = OUTBOUND_BUCKET_V4(&ahstack->ah_sadb.s_v4,
1938 *(uint32_t *)(newbie->ipsa_dstaddr));
1939 inbound = INBOUND_BUCKET(&ahstack->ah_sadb.s_v4,
1940 newbie->ipsa_spi);
1941 }
1942
1943 mutex_enter(&outbound->isaf_lock);
1944 mutex_enter(&inbound->isaf_lock);
1945
1946 /*
1947 * Check for collisions (i.e. did sadb_getspi() return with something
1948 * that already exists?).
1949 *
1950 * Try outbound first. Even though SADB_GETSPI is traditionally
1951 * for inbound SAs, you never know what a user might do.
1952 */
1953 target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi,
1954 newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam);
1955 if (target == NULL) {
1956 target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi,
1957 newbie->ipsa_srcaddr, newbie->ipsa_dstaddr,
1958 newbie->ipsa_addrfam);
1959 }
1960
1961 /*
1962 * I don't have collisions elsewhere!
1963 * (Nor will I because I'm still holding inbound/outbound locks.)
1964 */
1965
1966 if (target != NULL) {
1967 rc = EEXIST;
1968 IPSA_REFRELE(target);
1969 } else {
1970 /*
1971 * sadb_insertassoc() also checks for collisions, so
1972 * if there's a colliding larval entry, rc will be set
1973 * to EEXIST.
1974 */
1975 rc = sadb_insertassoc(newbie, inbound);
1976 newbie->ipsa_hardexpiretime = gethrestime_sec();
1977 newbie->ipsa_hardexpiretime += ahstack->ipsecah_larval_timeout;
1978 }
1979
1980 /*
1981 * Can exit outbound mutex. Hold inbound until we're done with
1982 * newbie.
1983 */
1984 mutex_exit(&outbound->isaf_lock);
1985
1986 if (rc != 0) {
1987 mutex_exit(&inbound->isaf_lock);
1988 IPSA_REFRELE(newbie);
1989 sadb_pfkey_error(ahstack->ah_pfkey_q, mp, rc,
1990 SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial);
1991 return;
1992 }
1993
1994 /* Can write here because I'm still holding the bucket lock. */
1995 newbie->ipsa_type = SADB_SATYPE_AH;
1996
1997 /*
1998 * Construct successful return message. We have one thing going
1999 * for us in PF_KEY v2. That's the fact that
2000 * sizeof (sadb_spirange_t) == sizeof (sadb_sa_t)
2001 */
2002 assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE];
2003 assoc->sadb_sa_exttype = SADB_EXT_SA;
2004 assoc->sadb_sa_spi = newbie->ipsa_spi;
2005 *((uint64_t *)(&assoc->sadb_sa_replay)) = 0;
2006 mutex_exit(&inbound->isaf_lock);
2007
2008 /* Convert KEYSOCK_IN to KEYSOCK_OUT. */
2009 kso = (keysock_out_t *)ksi;
2010 kso->ks_out_len = sizeof (*kso);
2011 kso->ks_out_serial = ksi->ks_in_serial;
2012 kso->ks_out_type = KEYSOCK_OUT;
2013
2014 /*
2015 * Can safely putnext() to ah_pfkey_q, because this is a turnaround
2016 * from the ah_pfkey_q.
2017 */
2018 putnext(ahstack->ah_pfkey_q, mp);
2019 }
2020
2021 /*
2022 * IPv6 sends up the ICMP errors for validation and the removal of the AH
2023 * header.
2024 * If succesful, the mp has been modified to not include the AH header so
2025 * that the caller can fanout to the ULP's icmp error handler.
2026 */
2027 static mblk_t *
2028 ah_icmp_error_v6(mblk_t *mp, ip_recv_attr_t *ira, ipsecah_stack_t *ahstack)
2029 {
2030 ip6_t *ip6h, *oip6h;
2031 uint16_t hdr_length, ah_length;
2032 uint8_t *nexthdrp;
2033 ah_t *ah;
2034 icmp6_t *icmp6;
2035 isaf_t *isaf;
2036 ipsa_t *assoc;
2037 uint8_t *post_ah_ptr;
2038 ipsec_stack_t *ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2039
2040 /*
2041 * Eat the cost of a pullupmsg() for now. It makes the rest of this
2042 * code far less convoluted.
2043 */
2044 if (!pullupmsg(mp, -1) ||
2045 !ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, &hdr_length,
2046 &nexthdrp) ||
2047 mp->b_rptr + hdr_length + sizeof (icmp6_t) + sizeof (ip6_t) +
2048 sizeof (ah_t) > mp->b_wptr) {
2049 IP_AH_BUMP_STAT(ipss, in_discards);
2050 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2051 DROPPER(ipss, ipds_ah_nomem),
2052 &ahstack->ah_dropper);
2053 return (NULL);
2054 }
2055
2056 oip6h = (ip6_t *)mp->b_rptr;
2057 icmp6 = (icmp6_t *)((uint8_t *)oip6h + hdr_length);
2058 ip6h = (ip6_t *)(icmp6 + 1);
2059 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_length, &nexthdrp)) {
2060 IP_AH_BUMP_STAT(ipss, in_discards);
2061 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2062 DROPPER(ipss, ipds_ah_bad_v6_hdrs),
2063 &ahstack->ah_dropper);
2064 return (NULL);
2065 }
2066 ah = (ah_t *)((uint8_t *)ip6h + hdr_length);
2067
2068 isaf = OUTBOUND_BUCKET_V6(&ahstack->ah_sadb.s_v6, ip6h->ip6_dst);
2069 mutex_enter(&isaf->isaf_lock);
2070 assoc = ipsec_getassocbyspi(isaf, ah->ah_spi,
2071 (uint32_t *)&ip6h->ip6_src, (uint32_t *)&ip6h->ip6_dst, AF_INET6);
2072 mutex_exit(&isaf->isaf_lock);
2073
2074 if (assoc == NULL) {
2075 IP_AH_BUMP_STAT(ipss, lookup_failure);
2076 IP_AH_BUMP_STAT(ipss, in_discards);
2077 if (ahstack->ipsecah_log_unknown_spi) {
2078 ipsec_assocfailure(info.mi_idnum, 0, 0,
2079 SL_CONSOLE | SL_WARN | SL_ERROR,
2080 "Bad ICMP message - No association for the "
2081 "attached AH header whose spi is 0x%x, "
2082 "sender is 0x%x\n",
2083 ah->ah_spi, &oip6h->ip6_src, AF_INET6,
2084 ahstack->ipsecah_netstack);
2085 }
2086 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2087 DROPPER(ipss, ipds_ah_no_sa),
2088 &ahstack->ah_dropper);
2089 return (NULL);
2090 }
2091
2092 IPSA_REFRELE(assoc);
2093
2094 /*
2095 * There seems to be a valid association. If there is enough of AH
2096 * header remove it, otherwise bail. One could check whether it has
2097 * complete AH header plus 8 bytes but it does not make sense if an
2098 * icmp error is returned for ICMP messages e.g ICMP time exceeded,
2099 * that are being sent up. Let the caller figure out.
2100 *
2101 * NOTE: ah_length is the number of 32 bit words minus 2.
2102 */
2103 ah_length = (ah->ah_length << 2) + 8;
2104 post_ah_ptr = (uint8_t *)ah + ah_length;
2105
2106 if (post_ah_ptr > mp->b_wptr) {
2107 IP_AH_BUMP_STAT(ipss, in_discards);
2108 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2109 DROPPER(ipss, ipds_ah_bad_length),
2110 &ahstack->ah_dropper);
2111 return (NULL);
2112 }
2113
2114 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - ah_length);
2115 *nexthdrp = ah->ah_nexthdr;
2116 ovbcopy(post_ah_ptr, ah,
2117 (size_t)((uintptr_t)mp->b_wptr - (uintptr_t)post_ah_ptr));
2118 mp->b_wptr -= ah_length;
2119
2120 return (mp);
2121 }
2122
2123 /*
2124 * IP sends up the ICMP errors for validation and the removal of
2125 * the AH header.
2126 * If succesful, the mp has been modified to not include the AH header so
2127 * that the caller can fanout to the ULP's icmp error handler.
2128 */
2129 static mblk_t *
2130 ah_icmp_error_v4(mblk_t *mp, ip_recv_attr_t *ira, ipsecah_stack_t *ahstack)
2131 {
2132 mblk_t *mp1;
2133 icmph_t *icmph;
2134 int iph_hdr_length;
2135 int hdr_length;
2136 isaf_t *hptr;
2137 ipsa_t *assoc;
2138 int ah_length;
2139 ipha_t *ipha;
2140 ipha_t *oipha;
2141 ah_t *ah;
2142 uint32_t length;
2143 int alloc_size;
2144 uint8_t nexthdr;
2145 ipsec_stack_t *ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2146
2147 oipha = ipha = (ipha_t *)mp->b_rptr;
2148 iph_hdr_length = IPH_HDR_LENGTH(ipha);
2149 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2150
2151 ipha = (ipha_t *)&icmph[1];
2152 hdr_length = IPH_HDR_LENGTH(ipha);
2153
2154 /*
2155 * See if we have enough to locate the SPI
2156 */
2157 if ((uchar_t *)ipha + hdr_length + 8 > mp->b_wptr) {
2158 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 8 -
2159 mp->b_rptr)) {
2160 ipsec_rl_strlog(ahstack->ipsecah_netstack,
2161 info.mi_idnum, 0, 0,
2162 SL_WARN | SL_ERROR,
2163 "ICMP error: Small AH header\n");
2164 IP_AH_BUMP_STAT(ipss, in_discards);
2165 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2166 DROPPER(ipss, ipds_ah_bad_length),
2167 &ahstack->ah_dropper);
2168 return (NULL);
2169 }
2170 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2171 ipha = (ipha_t *)&icmph[1];
2172 }
2173
2174 ah = (ah_t *)((uint8_t *)ipha + hdr_length);
2175 nexthdr = ah->ah_nexthdr;
2176
2177 hptr = OUTBOUND_BUCKET_V4(&ahstack->ah_sadb.s_v4, ipha->ipha_dst);
2178 mutex_enter(&hptr->isaf_lock);
2179 assoc = ipsec_getassocbyspi(hptr, ah->ah_spi,
2180 (uint32_t *)&ipha->ipha_src, (uint32_t *)&ipha->ipha_dst, AF_INET);
2181 mutex_exit(&hptr->isaf_lock);
2182
2183 if (assoc == NULL) {
2184 IP_AH_BUMP_STAT(ipss, lookup_failure);
2185 IP_AH_BUMP_STAT(ipss, in_discards);
2186 if (ahstack->ipsecah_log_unknown_spi) {
2187 ipsec_assocfailure(info.mi_idnum, 0, 0,
2188 SL_CONSOLE | SL_WARN | SL_ERROR,
2189 "Bad ICMP message - No association for the "
2190 "attached AH header whose spi is 0x%x, "
2191 "sender is 0x%x\n",
2192 ah->ah_spi, &oipha->ipha_src, AF_INET,
2193 ahstack->ipsecah_netstack);
2194 }
2195 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2196 DROPPER(ipss, ipds_ah_no_sa),
2197 &ahstack->ah_dropper);
2198 return (NULL);
2199 }
2200
2201 IPSA_REFRELE(assoc);
2202 /*
2203 * There seems to be a valid association. If there
2204 * is enough of AH header remove it, otherwise remove
2205 * as much as possible and send it back. One could check
2206 * whether it has complete AH header plus 8 bytes but it
2207 * does not make sense if an icmp error is returned for
2208 * ICMP messages e.g ICMP time exceeded, that are being
2209 * sent up. Let the caller figure out.
2210 *
2211 * NOTE: ah_length is the number of 32 bit words minus 2.
2212 */
2213 ah_length = (ah->ah_length << 2) + 8;
2214
2215 if ((uchar_t *)ipha + hdr_length + ah_length > mp->b_wptr) {
2216 if (mp->b_cont == NULL) {
2217 /*
2218 * There is nothing to pullup. Just remove as
2219 * much as possible. This is a common case for
2220 * IPV4.
2221 */
2222 ah_length = (mp->b_wptr - ((uchar_t *)ipha +
2223 hdr_length));
2224 goto done;
2225 }
2226 /* Pullup the full ah header */
2227 if (!pullupmsg(mp, (uchar_t *)ah + ah_length - mp->b_rptr)) {
2228 /*
2229 * pullupmsg could have failed if there was not
2230 * enough to pullup or memory allocation failed.
2231 * We tried hard, give up now.
2232 */
2233 IP_AH_BUMP_STAT(ipss, in_discards);
2234 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2235 DROPPER(ipss, ipds_ah_nomem),
2236 &ahstack->ah_dropper);
2237 return (NULL);
2238 }
2239 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2240 ipha = (ipha_t *)&icmph[1];
2241 }
2242 done:
2243 /*
2244 * Remove the AH header and change the protocol.
2245 * Don't update the spi fields in the ip_recv_attr_t
2246 * as we are called just to validate the
2247 * message attached to the ICMP message.
2248 *
2249 * If we never pulled up since all of the message
2250 * is in one single mblk, we can't remove the AH header
2251 * by just setting the b_wptr to the beginning of the
2252 * AH header. We need to allocate a mblk that can hold
2253 * up until the inner IP header and copy them.
2254 */
2255 alloc_size = iph_hdr_length + sizeof (icmph_t) + hdr_length;
2256
2257 if ((mp1 = allocb(alloc_size, BPRI_LO)) == NULL) {
2258 IP_AH_BUMP_STAT(ipss, in_discards);
2259 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2260 DROPPER(ipss, ipds_ah_nomem),
2261 &ahstack->ah_dropper);
2262 return (NULL);
2263 }
2264 bcopy(mp->b_rptr, mp1->b_rptr, alloc_size);
2265 mp1->b_wptr += alloc_size;
2266
2267 /*
2268 * Skip whatever we have copied and as much of AH header
2269 * possible. If we still have something left in the original
2270 * message, tag on.
2271 */
2272 mp->b_rptr = (uchar_t *)ipha + hdr_length + ah_length;
2273
2274 if (mp->b_rptr != mp->b_wptr) {
2275 mp1->b_cont = mp;
2276 } else {
2277 if (mp->b_cont != NULL)
2278 mp1->b_cont = mp->b_cont;
2279 freeb(mp);
2280 }
2281
2282 ipha = (ipha_t *)(mp1->b_rptr + iph_hdr_length + sizeof (icmph_t));
2283 ipha->ipha_protocol = nexthdr;
2284 length = ntohs(ipha->ipha_length);
2285 length -= ah_length;
2286 ipha->ipha_length = htons((uint16_t)length);
2287 ipha->ipha_hdr_checksum = 0;
2288 ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
2289
2290 return (mp1);
2291 }
2292
2293 /*
2294 * IP calls this to validate the ICMP errors that
2295 * we got from the network.
2296 */
2297 mblk_t *
2298 ipsecah_icmp_error(mblk_t *data_mp, ip_recv_attr_t *ira)
2299 {
2300 netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
2301 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
2302
2303 if (ira->ira_flags & IRAF_IS_IPV4)
2304 return (ah_icmp_error_v4(data_mp, ira, ahstack));
2305 else
2306 return (ah_icmp_error_v6(data_mp, ira, ahstack));
2307 }
2308
2309 static int
2310 ah_fix_tlv_options_v6(uint8_t *oi_opt, uint8_t *pi_opt, uint_t ehdrlen,
2311 uint8_t hdr_type, boolean_t copy_always)
2312 {
2313 uint8_t opt_type;
2314 uint_t optlen;
2315
2316 ASSERT(hdr_type == IPPROTO_DSTOPTS || hdr_type == IPPROTO_HOPOPTS);
2317
2318 /*
2319 * Copy the next header and hdr ext. len of the HOP-by-HOP
2320 * and Destination option.
2321 */
2322 *pi_opt++ = *oi_opt++;
2323 *pi_opt++ = *oi_opt++;
2324 ehdrlen -= 2;
2325
2326 /*
2327 * Now handle all the TLV encoded options.
2328 */
2329 while (ehdrlen != 0) {
2330 opt_type = *oi_opt;
2331
2332 if (opt_type == IP6OPT_PAD1) {
2333 optlen = 1;
2334 } else {
2335 if (ehdrlen < 2)
2336 goto bad_opt;
2337 optlen = 2 + oi_opt[1];
2338 if (optlen > ehdrlen)
2339 goto bad_opt;
2340 }
2341 if (copy_always || !(opt_type & IP6OPT_MUTABLE)) {
2342 bcopy(oi_opt, pi_opt, optlen);
2343 } else {
2344 if (optlen == 1) {
2345 *pi_opt = 0;
2346 } else {
2347 /*
2348 * Copy the type and data length fields.
2349 * Zero the option data by skipping
2350 * option type and option data len
2351 * fields.
2352 */
2353 *pi_opt = *oi_opt;
2354 *(pi_opt + 1) = *(oi_opt + 1);
2355 bzero(pi_opt + 2, optlen - 2);
2356 }
2357 }
2358 ehdrlen -= optlen;
2359 oi_opt += optlen;
2360 pi_opt += optlen;
2361 }
2362 return (0);
2363 bad_opt:
2364 return (-1);
2365 }
2366
2367 /*
2368 * Construct a pseudo header for AH, processing all the options.
2369 *
2370 * oip6h is the IPv6 header of the incoming or outgoing packet.
2371 * ip6h is the pointer to the pseudo headers IPV6 header. All
2372 * the space needed for the options have been allocated including
2373 * the AH header.
2374 *
2375 * If copy_always is set, all the options that appear before AH are copied
2376 * blindly without checking for IP6OPT_MUTABLE. This is used by
2377 * ah_auth_out_done(). Please refer to that function for details.
2378 *
2379 * NOTE :
2380 *
2381 * * AH header is never copied in this function even if copy_always
2382 * is set. It just returns the ah_offset - offset of the AH header
2383 * and the caller needs to do the copying. This is done so that we
2384 * don't have pass extra arguments e.g. SA etc. and also,
2385 * it is not needed when ah_auth_out_done is calling this function.
2386 */
2387 static uint_t
2388 ah_fix_phdr_v6(ip6_t *ip6h, ip6_t *oip6h, boolean_t outbound,
2389 boolean_t copy_always)
2390 {
2391 uint8_t *oi_opt;
2392 uint8_t *pi_opt;
2393 uint8_t nexthdr;
2394 uint8_t *prev_nexthdr;
2395 ip6_hbh_t *hbhhdr;
2396 ip6_dest_t *dsthdr = NULL;
2397 ip6_rthdr0_t *rthdr;
2398 int ehdrlen;
2399 ah_t *ah;
2400 int ret;
2401
2402 /*
2403 * In the outbound case for source route, ULP has already moved
2404 * the first hop, which is now in ip6_dst. We need to re-arrange
2405 * the header to make it look like how it would appear in the
2406 * receiver i.e
2407 *
2408 * Because of ip_massage_options_v6 the header looks like
2409 * this :
2410 *
2411 * ip6_src = S, ip6_dst = I1. followed by I2,I3,D.
2412 *
2413 * When it reaches the receiver, it would look like
2414 *
2415 * ip6_src = S, ip6_dst = D. followed by I1,I2,I3.
2416 *
2417 * NOTE : We assume that there are no problems with the options
2418 * as IP should have already checked this.
2419 */
2420
2421 oi_opt = (uchar_t *)&oip6h[1];
2422 pi_opt = (uchar_t *)&ip6h[1];
2423
2424 /*
2425 * We set the prev_nexthdr properly in the pseudo header.
2426 * After we finish authentication and come back from the
2427 * algorithm module, pseudo header will become the real
2428 * IP header.
2429 */
2430 prev_nexthdr = (uint8_t *)&ip6h->ip6_nxt;
2431 nexthdr = oip6h->ip6_nxt;
2432 /* Assume IP has already stripped it */
2433 ASSERT(nexthdr != IPPROTO_FRAGMENT);
2434 ah = NULL;
2435 dsthdr = NULL;
2436 for (;;) {
2437 switch (nexthdr) {
2438 case IPPROTO_HOPOPTS:
2439 hbhhdr = (ip6_hbh_t *)oi_opt;
2440 nexthdr = hbhhdr->ip6h_nxt;
2441 ehdrlen = 8 * (hbhhdr->ip6h_len + 1);
2442 ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
2443 IPPROTO_HOPOPTS, copy_always);
2444 /*
2445 * Return a zero offset indicating error if there
2446 * was error.
2447 */
2448 if (ret == -1)
2449 return (0);
2450 hbhhdr = (ip6_hbh_t *)pi_opt;
2451 prev_nexthdr = (uint8_t *)&hbhhdr->ip6h_nxt;
2452 break;
2453 case IPPROTO_ROUTING:
2454 rthdr = (ip6_rthdr0_t *)oi_opt;
2455 nexthdr = rthdr->ip6r0_nxt;
2456 ehdrlen = 8 * (rthdr->ip6r0_len + 1);
2457 if (!copy_always && outbound) {
2458 int i, left;
2459 ip6_rthdr0_t *prthdr;
2460 in6_addr_t *ap, *pap;
2461
2462 left = rthdr->ip6r0_segleft;
2463 prthdr = (ip6_rthdr0_t *)pi_opt;
2464 pap = (in6_addr_t *)(prthdr + 1);
2465 ap = (in6_addr_t *)(rthdr + 1);
2466 /*
2467 * First eight bytes except seg_left
2468 * does not change en route.
2469 */
2470 bcopy(oi_opt, pi_opt, 8);
2471 prthdr->ip6r0_segleft = 0;
2472 /*
2473 * First address has been moved to
2474 * the destination address of the
2475 * ip header by ip_massage_options_v6.
2476 * And the real destination address is
2477 * in the last address part of the
2478 * option.
2479 */
2480 *pap = oip6h->ip6_dst;
2481 for (i = 1; i < left - 1; i++)
2482 pap[i] = ap[i - 1];
2483 ip6h->ip6_dst = *(ap + left - 1);
2484 } else {
2485 bcopy(oi_opt, pi_opt, ehdrlen);
2486 }
2487 rthdr = (ip6_rthdr0_t *)pi_opt;
2488 prev_nexthdr = (uint8_t *)&rthdr->ip6r0_nxt;
2489 break;
2490 case IPPROTO_DSTOPTS:
2491 /*
2492 * Destination options are tricky. If there is
2493 * a terminal (e.g. non-IPv6-extension) header
2494 * following the destination options, don't
2495 * reset prev_nexthdr or advance the AH insertion
2496 * point and just treat this as a terminal header.
2497 *
2498 * If this is an inbound packet, just deal with
2499 * it as is.
2500 */
2501 dsthdr = (ip6_dest_t *)oi_opt;
2502 /*
2503 * XXX I hope common-subexpression elimination
2504 * saves us the double-evaluate.
2505 */
2506 if (outbound && dsthdr->ip6d_nxt != IPPROTO_ROUTING &&
2507 dsthdr->ip6d_nxt != IPPROTO_HOPOPTS)
2508 goto terminal_hdr;
2509 nexthdr = dsthdr->ip6d_nxt;
2510 ehdrlen = 8 * (dsthdr->ip6d_len + 1);
2511 ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen,
2512 IPPROTO_DSTOPTS, copy_always);
2513 /*
2514 * Return a zero offset indicating error if there
2515 * was error.
2516 */
2517 if (ret == -1)
2518 return (0);
2519 break;
2520 case IPPROTO_AH:
2521 /*
2522 * Be conservative in what you send. We shouldn't
2523 * see two same-scoped AH's in one packet.
2524 * (Inner-IP-scoped AH will be hit by terminal
2525 * header of IP or IPv6.)
2526 */
2527 ASSERT(!outbound);
2528 return ((uint_t)(pi_opt - (uint8_t *)ip6h));
2529 default:
2530 ASSERT(outbound);
2531 terminal_hdr:
2532 *prev_nexthdr = IPPROTO_AH;
2533 ah = (ah_t *)pi_opt;
2534 ah->ah_nexthdr = nexthdr;
2535 return ((uint_t)(pi_opt - (uint8_t *)ip6h));
2536 }
2537 pi_opt += ehdrlen;
2538 oi_opt += ehdrlen;
2539 }
2540 /* NOTREACHED */
2541 }
2542
2543 static boolean_t
2544 ah_finish_up(ah_t *phdr_ah, ah_t *inbound_ah, ipsa_t *assoc,
2545 int ah_data_sz, int ah_align_sz, ipsecah_stack_t *ahstack)
2546 {
2547 int i;
2548
2549 /*
2550 * Padding :
2551 *
2552 * 1) Authentication data may have to be padded
2553 * before ICV calculation if ICV is not a multiple
2554 * of 64 bits. This padding is arbitrary and transmitted
2555 * with the packet at the end of the authentication data.
2556 * Payload length should include the padding bytes.
2557 *
2558 * 2) Explicit padding of the whole datagram may be
2559 * required by the algorithm which need not be
2560 * transmitted. It is assumed that this will be taken
2561 * care by the algorithm module.
2562 */
2563 bzero(phdr_ah + 1, ah_data_sz); /* Zero out ICV for pseudo-hdr. */
2564
2565 if (inbound_ah == NULL) {
2566 /* Outbound AH datagram. */
2567
2568 phdr_ah->ah_length = (ah_align_sz >> 2) + 1;
2569 phdr_ah->ah_reserved = 0;
2570 phdr_ah->ah_spi = assoc->ipsa_spi;
2571
2572 phdr_ah->ah_replay =
2573 htonl(atomic_inc_32_nv(&assoc->ipsa_replay));
2574 if (phdr_ah->ah_replay == 0 && assoc->ipsa_replay_wsize != 0) {
2575 /*
2576 * XXX We have replay counter wrapping. We probably
2577 * want to nuke this SA (and its peer).
2578 */
2579 ipsec_assocfailure(info.mi_idnum, 0, 0,
2580 SL_ERROR | SL_CONSOLE | SL_WARN,
2581 "Outbound AH SA (0x%x), dst %s has wrapped "
2582 "sequence.\n", phdr_ah->ah_spi,
2583 assoc->ipsa_dstaddr, assoc->ipsa_addrfam,
2584 ahstack->ipsecah_netstack);
2585
2586 sadb_replay_delete(assoc);
2587 /* Caller will free phdr_mp and return NULL. */
2588 return (B_FALSE);
2589 }
2590
2591 if (ah_data_sz != ah_align_sz) {
2592 uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
2593 ah_data_sz);
2594
2595 for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2596 pad[i] = (uchar_t)i; /* Fill the padding */
2597 }
2598 }
2599 } else {
2600 /* Inbound AH datagram. */
2601 phdr_ah->ah_nexthdr = inbound_ah->ah_nexthdr;
2602 phdr_ah->ah_length = inbound_ah->ah_length;
2603 phdr_ah->ah_reserved = 0;
2604 ASSERT(inbound_ah->ah_spi == assoc->ipsa_spi);
2605 phdr_ah->ah_spi = inbound_ah->ah_spi;
2606 phdr_ah->ah_replay = inbound_ah->ah_replay;
2607
2608 if (ah_data_sz != ah_align_sz) {
2609 uchar_t *opad = ((uchar_t *)inbound_ah +
2610 sizeof (ah_t) + ah_data_sz);
2611 uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) +
2612 ah_data_sz);
2613
2614 for (i = 0; i < (ah_align_sz - ah_data_sz); i++) {
2615 pad[i] = opad[i]; /* Copy the padding */
2616 }
2617 }
2618 }
2619
2620 return (B_TRUE);
2621 }
2622
2623 /*
2624 * Called upon failing the inbound ICV check. The message passed as
2625 * argument is freed.
2626 */
2627 static void
2628 ah_log_bad_auth(mblk_t *mp, ip_recv_attr_t *ira, ipsec_crypto_t *ic)
2629 {
2630 boolean_t isv4 = (ira->ira_flags & IRAF_IS_IPV4);
2631 ipsa_t *assoc = ira->ira_ipsec_ah_sa;
2632 int af;
2633 void *addr;
2634 netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
2635 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
2636 ipsec_stack_t *ipss = ns->netstack_ipsec;
2637
2638 ASSERT(mp->b_datap->db_type == M_DATA);
2639
2640 mp->b_rptr -= ic->ic_skip_len;
2641
2642 if (isv4) {
2643 ipha_t *ipha = (ipha_t *)mp->b_rptr;
2644 addr = &ipha->ipha_dst;
2645 af = AF_INET;
2646 } else {
2647 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
2648 addr = &ip6h->ip6_dst;
2649 af = AF_INET6;
2650 }
2651
2652 /*
2653 * Log the event. Don't print to the console, block
2654 * potential denial-of-service attack.
2655 */
2656 AH_BUMP_STAT(ahstack, bad_auth);
2657
2658 ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
2659 "AH Authentication failed spi %x, dst_addr %s",
2660 assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
2661
2662 IP_AH_BUMP_STAT(ipss, in_discards);
2663 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2664 DROPPER(ipss, ipds_ah_bad_auth),
2665 &ahstack->ah_dropper);
2666 }
2667
2668 /*
2669 * Kernel crypto framework callback invoked after completion of async
2670 * crypto requests for outbound packets.
2671 */
2672 static void
2673 ah_kcf_callback_outbound(void *arg, int status)
2674 {
2675 mblk_t *mp = (mblk_t *)arg;
2676 mblk_t *async_mp;
2677 netstack_t *ns;
2678 ipsec_stack_t *ipss;
2679 ipsecah_stack_t *ahstack;
2680 mblk_t *data_mp;
2681 ip_xmit_attr_t ixas;
2682 ipsec_crypto_t *ic;
2683 ill_t *ill;
2684
2685 /*
2686 * First remove the ipsec_crypto_t mblk
2687 * Note that we need to ipsec_free_crypto_data(mp) once done with ic.
2688 */
2689 async_mp = ipsec_remove_crypto_data(mp, &ic);
2690 ASSERT(async_mp != NULL);
2691
2692 /*
2693 * Extract the ip_xmit_attr_t from the first mblk.
2694 * Verifies that the netstack and ill is still around; could
2695 * have vanished while kEf was doing its work.
2696 * On succesful return we have a nce_t and the ill/ipst can't
2697 * disappear until we do the nce_refrele in ixa_cleanup.
2698 */
2699 data_mp = async_mp->b_cont;
2700 async_mp->b_cont = NULL;
2701 if (!ip_xmit_attr_from_mblk(async_mp, &ixas)) {
2702 /* Disappeared on us - no ill/ipst for MIB */
2703 if (ixas.ixa_nce != NULL) {
2704 ill = ixas.ixa_nce->nce_ill;
2705 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
2706 ip_drop_output("ipIfStatsOutDiscards", data_mp, ill);
2707 }
2708 freemsg(data_mp);
2709 goto done;
2710 }
2711 ns = ixas.ixa_ipst->ips_netstack;
2712 ahstack = ns->netstack_ipsecah;
2713 ipss = ns->netstack_ipsec;
2714 ill = ixas.ixa_nce->nce_ill;
2715
2716 if (status == CRYPTO_SUCCESS) {
2717 data_mp = ah_auth_out_done(data_mp, &ixas, ic);
2718 if (data_mp == NULL)
2719 goto done;
2720
2721 (void) ip_output_post_ipsec(data_mp, &ixas);
2722 } else {
2723 /* Outbound shouldn't see invalid MAC */
2724 ASSERT(status != CRYPTO_INVALID_MAC);
2725
2726 ah1dbg(ahstack,
2727 ("ah_kcf_callback_outbound: crypto failed with 0x%x\n",
2728 status));
2729 AH_BUMP_STAT(ahstack, crypto_failures);
2730 AH_BUMP_STAT(ahstack, out_discards);
2731
2732 ip_drop_packet(data_mp, B_FALSE, ill,
2733 DROPPER(ipss, ipds_ah_crypto_failed),
2734 &ahstack->ah_dropper);
2735 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
2736 }
2737 done:
2738 ixa_cleanup(&ixas);
2739 (void) ipsec_free_crypto_data(mp);
2740 }
2741
2742 /*
2743 * Kernel crypto framework callback invoked after completion of async
2744 * crypto requests for inbound packets.
2745 */
2746 static void
2747 ah_kcf_callback_inbound(void *arg, int status)
2748 {
2749 mblk_t *mp = (mblk_t *)arg;
2750 mblk_t *async_mp;
2751 netstack_t *ns;
2752 ipsec_stack_t *ipss;
2753 ipsecah_stack_t *ahstack;
2754 mblk_t *data_mp;
2755 ip_recv_attr_t iras;
2756 ipsec_crypto_t *ic;
2757
2758 /*
2759 * First remove the ipsec_crypto_t mblk
2760 * Note that we need to ipsec_free_crypto_data(mp) once done with ic.
2761 */
2762 async_mp = ipsec_remove_crypto_data(mp, &ic);
2763 ASSERT(async_mp != NULL);
2764
2765 /*
2766 * Extract the ip_xmit_attr_t from the first mblk.
2767 * Verifies that the netstack and ill is still around; could
2768 * have vanished while kEf was doing its work.
2769 */
2770 data_mp = async_mp->b_cont;
2771 async_mp->b_cont = NULL;
2772 if (!ip_recv_attr_from_mblk(async_mp, &iras)) {
2773 /* The ill or ip_stack_t disappeared on us */
2774 ip_drop_input("ip_recv_attr_from_mblk", data_mp, NULL);
2775 freemsg(data_mp);
2776 goto done;
2777 }
2778 ns = iras.ira_ill->ill_ipst->ips_netstack;
2779 ahstack = ns->netstack_ipsecah;
2780 ipss = ns->netstack_ipsec;
2781
2782 if (status == CRYPTO_SUCCESS) {
2783 data_mp = ah_auth_in_done(data_mp, &iras, ic);
2784 if (data_mp == NULL)
2785 goto done;
2786
2787 /* finish IPsec processing */
2788 ip_input_post_ipsec(data_mp, &iras);
2789
2790 } else if (status == CRYPTO_INVALID_MAC) {
2791 ah_log_bad_auth(data_mp, &iras, ic);
2792 } else {
2793 ah1dbg(ahstack,
2794 ("ah_kcf_callback_inbound: crypto failed with 0x%x\n",
2795 status));
2796 AH_BUMP_STAT(ahstack, crypto_failures);
2797 IP_AH_BUMP_STAT(ipss, in_discards);
2798 ip_drop_packet(data_mp, B_TRUE, iras.ira_ill,
2799 DROPPER(ipss, ipds_ah_crypto_failed),
2800 &ahstack->ah_dropper);
2801 BUMP_MIB(iras.ira_ill->ill_ip_mib, ipIfStatsInDiscards);
2802 }
2803 done:
2804 ira_cleanup(&iras, B_TRUE);
2805 (void) ipsec_free_crypto_data(mp);
2806 }
2807
2808 /*
2809 * Invoked on kernel crypto failure during inbound and outbound processing.
2810 */
2811 static void
2812 ah_crypto_failed(mblk_t *data_mp, boolean_t is_inbound, int kef_rc,
2813 ill_t *ill, ipsecah_stack_t *ahstack)
2814 {
2815 ipsec_stack_t *ipss = ahstack->ipsecah_netstack->netstack_ipsec;
2816
2817 ah1dbg(ahstack, ("crypto failed for %s AH with 0x%x\n",
2818 is_inbound ? "inbound" : "outbound", kef_rc));
2819 ip_drop_packet(data_mp, is_inbound, ill,
2820 DROPPER(ipss, ipds_ah_crypto_failed),
2821 &ahstack->ah_dropper);
2822 AH_BUMP_STAT(ahstack, crypto_failures);
2823 if (is_inbound)
2824 IP_AH_BUMP_STAT(ipss, in_discards);
2825 else
2826 AH_BUMP_STAT(ahstack, out_discards);
2827 }
2828
2829 /*
2830 * Helper macros for the ah_submit_req_{inbound,outbound}() functions.
2831 */
2832
2833 /*
2834 * A statement-equivalent macro, _cr MUST point to a modifiable
2835 * crypto_call_req_t.
2836 */
2837 #define AH_INIT_CALLREQ(_cr, _mp, _callback) \
2838 (_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_ALWAYS_QUEUE; \
2839 (_cr)->cr_callback_arg = (_mp); \
2840 (_cr)->cr_callback_func = (_callback)
2841
2842 #define AH_INIT_CRYPTO_DATA(data, msglen, mblk) { \
2843 (data)->cd_format = CRYPTO_DATA_MBLK; \
2844 (data)->cd_mp = mblk; \
2845 (data)->cd_offset = 0; \
2846 (data)->cd_length = msglen; \
2847 }
2848
2849 #define AH_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) { \
2850 (mac)->cd_format = CRYPTO_DATA_RAW; \
2851 (mac)->cd_offset = 0; \
2852 (mac)->cd_length = icvlen; \
2853 (mac)->cd_raw.iov_base = icvbuf; \
2854 (mac)->cd_raw.iov_len = icvlen; \
2855 }
2856
2857 /*
2858 * Submit an inbound packet for processing by the crypto framework.
2859 */
2860 static mblk_t *
2861 ah_submit_req_inbound(mblk_t *phdr_mp, ip_recv_attr_t *ira,
2862 size_t skip_len, uint32_t ah_offset, ipsa_t *assoc)
2863 {
2864 int kef_rc;
2865 mblk_t *mp;
2866 crypto_call_req_t call_req, *callrp;
2867 uint_t icv_len = assoc->ipsa_mac_len;
2868 crypto_ctx_template_t ctx_tmpl;
2869 ipsecah_stack_t *ahstack;
2870 ipsec_crypto_t *ic, icstack;
2871 boolean_t force = (assoc->ipsa_flags & IPSA_F_ASYNC);
2872
2873 ahstack = ira->ira_ill->ill_ipst->ips_netstack->netstack_ipsecah;
2874
2875 ASSERT(phdr_mp != NULL);
2876 ASSERT(phdr_mp->b_datap->db_type == M_DATA);
2877
2878 if (force) {
2879 /* We are doing asynch; allocate mblks to hold state */
2880 if ((mp = ip_recv_attr_to_mblk(ira)) == NULL ||
2881 (mp = ipsec_add_crypto_data(mp, &ic)) == NULL) {
2882 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
2883 ip_drop_input("ipIfStatsInDiscards", phdr_mp,
2884 ira->ira_ill);
2885 freemsg(phdr_mp);
2886 return (NULL);
2887 }
2888
2889 linkb(mp, phdr_mp);
2890 callrp = &call_req;
2891 AH_INIT_CALLREQ(callrp, mp, ah_kcf_callback_inbound);
2892 } else {
2893 /*
2894 * If we know we are going to do sync then ipsec_crypto_t
2895 * should be on the stack.
2896 */
2897 ic = &icstack;
2898 bzero(ic, sizeof (*ic));
2899 callrp = NULL;
2900 }
2901
2902 /* init arguments for the crypto framework */
2903 AH_INIT_CRYPTO_DATA(&ic->ic_crypto_data, AH_MSGSIZE(phdr_mp),
2904 phdr_mp);
2905
2906 AH_INIT_CRYPTO_MAC(&ic->ic_crypto_mac, icv_len,
2907 (char *)phdr_mp->b_cont->b_rptr - skip_len + ah_offset +
2908 sizeof (ah_t));
2909
2910 ic->ic_skip_len = skip_len;
2911
2912 IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH, ctx_tmpl);
2913
2914 /* call KEF to do the MAC operation */
2915 kef_rc = crypto_mac_verify(&assoc->ipsa_amech,
2916 &ic->ic_crypto_data, &assoc->ipsa_kcfauthkey, ctx_tmpl,
2917 &ic->ic_crypto_mac, callrp);
2918
2919 switch (kef_rc) {
2920 case CRYPTO_SUCCESS:
2921 AH_BUMP_STAT(ahstack, crypto_sync);
2922 phdr_mp = ah_auth_in_done(phdr_mp, ira, ic);
2923 if (force) {
2924 /* Free mp after we are done with ic */
2925 mp = ipsec_free_crypto_data(mp);
2926 (void) ip_recv_attr_free_mblk(mp);
2927 }
2928 return (phdr_mp);
2929 case CRYPTO_QUEUED:
2930 /* ah_kcf_callback_inbound() will be invoked on completion */
2931 AH_BUMP_STAT(ahstack, crypto_async);
2932 return (NULL);
2933 case CRYPTO_INVALID_MAC:
2934 /* Free mp after we are done with ic */
2935 AH_BUMP_STAT(ahstack, crypto_sync);
2936 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
2937 ah_log_bad_auth(phdr_mp, ira, ic);
2938 /* phdr_mp was passed to ip_drop_packet */
2939 if (force) {
2940 mp = ipsec_free_crypto_data(mp);
2941 (void) ip_recv_attr_free_mblk(mp);
2942 }
2943 return (NULL);
2944 }
2945
2946 if (force) {
2947 mp = ipsec_free_crypto_data(mp);
2948 phdr_mp = ip_recv_attr_free_mblk(mp);
2949 }
2950 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
2951 ah_crypto_failed(phdr_mp, B_TRUE, kef_rc, ira->ira_ill, ahstack);
2952 /* phdr_mp was passed to ip_drop_packet */
2953 return (NULL);
2954 }
2955
2956 /*
2957 * Submit an outbound packet for processing by the crypto framework.
2958 */
2959 static mblk_t *
2960 ah_submit_req_outbound(mblk_t *phdr_mp, ip_xmit_attr_t *ixa,
2961 size_t skip_len, ipsa_t *assoc)
2962 {
2963 int kef_rc;
2964 mblk_t *mp;
2965 crypto_call_req_t call_req, *callrp;
2966 uint_t icv_len = assoc->ipsa_mac_len;
2967 ipsecah_stack_t *ahstack;
2968 ipsec_crypto_t *ic, icstack;
2969 ill_t *ill = ixa->ixa_nce->nce_ill;
2970 boolean_t force = (assoc->ipsa_flags & IPSA_F_ASYNC);
2971
2972 ahstack = ill->ill_ipst->ips_netstack->netstack_ipsecah;
2973
2974 ASSERT(phdr_mp != NULL);
2975 ASSERT(phdr_mp->b_datap->db_type == M_DATA);
2976
2977 if (force) {
2978 /* We are doing asynch; allocate mblks to hold state */
2979 if ((mp = ip_xmit_attr_to_mblk(ixa)) == NULL ||
2980 (mp = ipsec_add_crypto_data(mp, &ic)) == NULL) {
2981 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
2982 ip_drop_output("ipIfStatsOutDiscards", phdr_mp, ill);
2983 freemsg(phdr_mp);
2984 return (NULL);
2985 }
2986 linkb(mp, phdr_mp);
2987 callrp = &call_req;
2988 AH_INIT_CALLREQ(callrp, mp, ah_kcf_callback_outbound);
2989 } else {
2990 /*
2991 * If we know we are going to do sync then ipsec_crypto_t
2992 * should be on the stack.
2993 */
2994 ic = &icstack;
2995 bzero(ic, sizeof (*ic));
2996 callrp = NULL;
2997 }
2998
2999 /* init arguments for the crypto framework */
3000 AH_INIT_CRYPTO_DATA(&ic->ic_crypto_data, AH_MSGSIZE(phdr_mp),
3001 phdr_mp);
3002
3003 AH_INIT_CRYPTO_MAC(&ic->ic_crypto_mac, icv_len,
3004 (char *)phdr_mp->b_wptr);
3005
3006 ic->ic_skip_len = skip_len;
3007
3008 ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
3009
3010 /* call KEF to do the MAC operation */
3011 kef_rc = crypto_mac(&assoc->ipsa_amech, &ic->ic_crypto_data,
3012 &assoc->ipsa_kcfauthkey, assoc->ipsa_authtmpl,
3013 &ic->ic_crypto_mac, callrp);
3014
3015 switch (kef_rc) {
3016 case CRYPTO_SUCCESS:
3017 AH_BUMP_STAT(ahstack, crypto_sync);
3018 phdr_mp = ah_auth_out_done(phdr_mp, ixa, ic);
3019 if (force) {
3020 /* Free mp after we are done with ic */
3021 mp = ipsec_free_crypto_data(mp);
3022 (void) ip_xmit_attr_free_mblk(mp);
3023 }
3024 return (phdr_mp);
3025 case CRYPTO_QUEUED:
3026 /* ah_kcf_callback_outbound() will be invoked on completion */
3027 AH_BUMP_STAT(ahstack, crypto_async);
3028 return (NULL);
3029 }
3030
3031 if (force) {
3032 mp = ipsec_free_crypto_data(mp);
3033 phdr_mp = ip_xmit_attr_free_mblk(mp);
3034 }
3035 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
3036 ah_crypto_failed(phdr_mp, B_FALSE, kef_rc, NULL, ahstack);
3037 /* phdr_mp was passed to ip_drop_packet */
3038 return (NULL);
3039 }
3040
3041 /*
3042 * This function constructs a pseudo header by looking at the IP header
3043 * and options if any. This is called for both outbound and inbound,
3044 * before computing the ICV.
3045 */
3046 static mblk_t *
3047 ah_process_ip_options_v6(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
3048 uint_t ah_data_sz, boolean_t outbound, ipsecah_stack_t *ahstack)
3049 {
3050 ip6_t *ip6h;
3051 ip6_t *oip6h;
3052 mblk_t *phdr_mp;
3053 int option_length;
3054 uint_t ah_align_sz;
3055 uint_t ah_offset;
3056 int hdr_size;
3057
3058 /*
3059 * Allocate space for the authentication data also. It is
3060 * useful both during the ICV calculation where we need to
3061 * feed in zeroes and while sending the datagram back to IP
3062 * where we will be using the same space.
3063 *
3064 * We need to allocate space for padding bytes if it is not
3065 * a multiple of IPV6_PADDING_ALIGN.
3066 *
3067 * In addition, we allocate space for the ICV computed by
3068 * the kernel crypto framework, saving us a separate kmem
3069 * allocation down the road.
3070 */
3071
3072 ah_align_sz = P2ALIGN(ah_data_sz + IPV6_PADDING_ALIGN - 1,
3073 IPV6_PADDING_ALIGN);
3074
3075 ASSERT(ah_align_sz >= ah_data_sz);
3076
3077 hdr_size = ipsec_ah_get_hdr_size_v6(mp, B_FALSE);
3078 option_length = hdr_size - IPV6_HDR_LEN;
3079
3080 /* This was not included in ipsec_ah_get_hdr_size_v6() */
3081 hdr_size += (sizeof (ah_t) + ah_align_sz);
3082
3083 if (!outbound && (MBLKL(mp) < hdr_size)) {
3084 /*
3085 * We have post-AH header options in a separate mblk,
3086 * a pullup is required.
3087 */
3088 if (!pullupmsg(mp, hdr_size))
3089 return (NULL);
3090 }
3091
3092 if ((phdr_mp = allocb_tmpl(hdr_size + ah_data_sz, mp)) == NULL) {
3093 return (NULL);
3094 }
3095
3096 oip6h = (ip6_t *)mp->b_rptr;
3097
3098 /*
3099 * Form the basic IP header first. Zero out the header
3100 * so that the mutable fields are zeroed out.
3101 */
3102 ip6h = (ip6_t *)phdr_mp->b_rptr;
3103 bzero(ip6h, sizeof (ip6_t));
3104 ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
3105
3106 if (outbound) {
3107 /*
3108 * Include the size of AH and authentication data.
3109 * This is how our recipient would compute the
3110 * authentication data. Look at what we do in the
3111 * inbound case below.
3112 */
3113 ip6h->ip6_plen = htons(ntohs(oip6h->ip6_plen) +
3114 sizeof (ah_t) + ah_align_sz);
3115 } else {
3116 ip6h->ip6_plen = oip6h->ip6_plen;
3117 }
3118
3119 ip6h->ip6_src = oip6h->ip6_src;
3120 ip6h->ip6_dst = oip6h->ip6_dst;
3121
3122 *length_to_skip = IPV6_HDR_LEN;
3123 if (option_length == 0) {
3124 /* Form the AH header */
3125 ip6h->ip6_nxt = IPPROTO_AH;
3126 ((ah_t *)(ip6h + 1))->ah_nexthdr = oip6h->ip6_nxt;
3127 ah_offset = *length_to_skip;
3128 } else {
3129 ip6h->ip6_nxt = oip6h->ip6_nxt;
3130 /* option_length does not include the AH header's size */
3131 *length_to_skip += option_length;
3132
3133 ah_offset = ah_fix_phdr_v6(ip6h, oip6h, outbound, B_FALSE);
3134 if (ah_offset == 0) {
3135 return (NULL);
3136 }
3137 }
3138
3139 if (!ah_finish_up(((ah_t *)((uint8_t *)ip6h + ah_offset)),
3140 (outbound ? NULL : ((ah_t *)((uint8_t *)oip6h + ah_offset))),
3141 assoc, ah_data_sz, ah_align_sz, ahstack)) {
3142 freeb(phdr_mp);
3143 /*
3144 * Returning NULL will tell the caller to
3145 * IPSA_REFELE(), free the memory, etc.
3146 */
3147 return (NULL);
3148 }
3149
3150 phdr_mp->b_wptr = ((uint8_t *)ip6h + ah_offset + sizeof (ah_t) +
3151 ah_align_sz);
3152 if (!outbound)
3153 *length_to_skip += sizeof (ah_t) + ah_align_sz;
3154 return (phdr_mp);
3155 }
3156
3157 /*
3158 * This function constructs a pseudo header by looking at the IP header
3159 * and options if any. This is called for both outbound and inbound,
3160 * before computing the ICV.
3161 */
3162 static mblk_t *
3163 ah_process_ip_options_v4(mblk_t *mp, ipsa_t *assoc, int *length_to_skip,
3164 uint_t ah_data_sz, boolean_t outbound, ipsecah_stack_t *ahstack)
3165 {
3166 ipoptp_t opts;
3167 uint32_t option_length;
3168 ipha_t *ipha;
3169 ipha_t *oipha;
3170 mblk_t *phdr_mp;
3171 int size;
3172 uchar_t *optptr;
3173 uint8_t optval;
3174 uint8_t optlen;
3175 ipaddr_t dst;
3176 uint32_t v_hlen_tos_len;
3177 int ip_hdr_length;
3178 uint_t ah_align_sz;
3179 uint32_t off;
3180
3181 #ifdef _BIG_ENDIAN
3182 #define V_HLEN (v_hlen_tos_len >> 24)
3183 #else
3184 #define V_HLEN (v_hlen_tos_len & 0xFF)
3185 #endif
3186
3187 oipha = (ipha_t *)mp->b_rptr;
3188 v_hlen_tos_len = ((uint32_t *)oipha)[0];
3189
3190 /*
3191 * Allocate space for the authentication data also. It is
3192 * useful both during the ICV calculation where we need to
3193 * feed in zeroes and while sending the datagram back to IP
3194 * where we will be using the same space.
3195 *
3196 * We need to allocate space for padding bytes if it is not
3197 * a multiple of IPV4_PADDING_ALIGN.
3198 *
3199 * In addition, we allocate space for the ICV computed by
3200 * the kernel crypto framework, saving us a separate kmem
3201 * allocation down the road.
3202 */
3203
3204 ah_align_sz = P2ALIGN(ah_data_sz + IPV4_PADDING_ALIGN - 1,
3205 IPV4_PADDING_ALIGN);
3206
3207 ASSERT(ah_align_sz >= ah_data_sz);
3208
3209 size = IP_SIMPLE_HDR_LENGTH + sizeof (ah_t) + ah_align_sz +
3210 ah_data_sz;
3211
3212 if (V_HLEN != IP_SIMPLE_HDR_VERSION) {
3213 option_length = oipha->ipha_version_and_hdr_length -
3214 (uint8_t)((IP_VERSION << 4) +
3215 IP_SIMPLE_HDR_LENGTH_IN_WORDS);
3216 option_length <<= 2;
3217 size += option_length;
3218 }
3219
3220 if ((phdr_mp = allocb_tmpl(size, mp)) == NULL) {
3221 return (NULL);
3222 }
3223
3224 /*
3225 * Form the basic IP header first.
3226 */
3227 ipha = (ipha_t *)phdr_mp->b_rptr;
3228 ipha->ipha_version_and_hdr_length = oipha->ipha_version_and_hdr_length;
3229 ipha->ipha_type_of_service = 0;
3230
3231 if (outbound) {
3232 /*
3233 * Include the size of AH and authentication data.
3234 * This is how our recipient would compute the
3235 * authentication data. Look at what we do in the
3236 * inbound case below.
3237 */
3238 ipha->ipha_length = ntohs(htons(oipha->ipha_length) +
3239 sizeof (ah_t) + ah_align_sz);
3240 } else {
3241 ipha->ipha_length = oipha->ipha_length;
3242 }
3243
3244 ipha->ipha_ident = oipha->ipha_ident;
3245 ipha->ipha_fragment_offset_and_flags = 0;
3246 ipha->ipha_ttl = 0;
3247 ipha->ipha_protocol = IPPROTO_AH;
3248 ipha->ipha_hdr_checksum = 0;
3249 ipha->ipha_src = oipha->ipha_src;
3250 ipha->ipha_dst = dst = oipha->ipha_dst;
3251
3252 /*
3253 * If there is no option to process return now.
3254 */
3255 ip_hdr_length = IP_SIMPLE_HDR_LENGTH;
3256
3257 if (V_HLEN == IP_SIMPLE_HDR_VERSION) {
3258 /* Form the AH header */
3259 goto ah_hdr;
3260 }
3261
3262 ip_hdr_length += option_length;
3263
3264 /*
3265 * We have options. In the outbound case for source route,
3266 * ULP has already moved the first hop, which is now in
3267 * ipha_dst. We need the final destination for the calculation
3268 * of authentication data. And also make sure that mutable
3269 * and experimental fields are zeroed out in the IP options.
3270 */
3271
3272 bcopy(&oipha[1], &ipha[1], option_length);
3273
3274 for (optval = ipoptp_first(&opts, ipha);
3275 optval != IPOPT_EOL;
3276 optval = ipoptp_next(&opts)) {
3277 optptr = opts.ipoptp_cur;
3278 optlen = opts.ipoptp_len;
3279 switch (optval) {
3280 case IPOPT_EXTSEC:
3281 case IPOPT_COMSEC:
3282 case IPOPT_RA:
3283 case IPOPT_SDMDD:
3284 case IPOPT_SECURITY:
3285 /*
3286 * These options are Immutable, leave them as-is.
3287 * Note that IPOPT_NOP is also Immutable, but it
3288 * was skipped by ipoptp_next() and thus remains
3289 * intact in the header.
3290 */
3291 break;
3292 case IPOPT_SSRR:
3293 case IPOPT_LSRR:
3294 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0)
3295 goto bad_ipv4opt;
3296 /*
3297 * These two are mutable and will be zeroed, but
3298 * first get the final destination.
3299 */
3300 off = optptr[IPOPT_OFFSET];
3301 /*
3302 * If one of the conditions is true, it means
3303 * end of options and dst already has the right
3304 * value. So, just fall through.
3305 */
3306 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
3307 off = optlen - IP_ADDR_LEN;
3308 bcopy(&optptr[off], &dst, IP_ADDR_LEN);
3309 }
3310 /* FALLTHRU */
3311 case IPOPT_RR:
3312 case IPOPT_TS:
3313 case IPOPT_SATID:
3314 default:
3315 /*
3316 * optlen should include from the beginning of an
3317 * option.
3318 * NOTE : Stream Identifier Option (SID): RFC 791
3319 * shows the bit pattern of optlen as 2 and documents
3320 * the length as 4. We assume it to be 2 here.
3321 */
3322 bzero(optptr, optlen);
3323 break;
3324 }
3325 }
3326
3327 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
3328 bad_ipv4opt:
3329 ah1dbg(ahstack, ("AH : bad IPv4 option"));
3330 freeb(phdr_mp);
3331 return (NULL);
3332 }
3333
3334 /*
3335 * Don't change ipha_dst for an inbound datagram as it points
3336 * to the right value. Only for the outbound with LSRR/SSRR,
3337 * because of ip_massage_options called by the ULP, ipha_dst
3338 * points to the first hop and we need to use the final
3339 * destination for computing the ICV.
3340 */
3341
3342 if (outbound)
3343 ipha->ipha_dst = dst;
3344 ah_hdr:
3345 ((ah_t *)((uint8_t *)ipha + ip_hdr_length))->ah_nexthdr =
3346 oipha->ipha_protocol;
3347 if (!ah_finish_up(((ah_t *)((uint8_t *)ipha + ip_hdr_length)),
3348 (outbound ? NULL : ((ah_t *)((uint8_t *)oipha + ip_hdr_length))),
3349 assoc, ah_data_sz, ah_align_sz, ahstack)) {
3350 freeb(phdr_mp);
3351 /*
3352 * Returning NULL will tell the caller to IPSA_REFELE(), free
3353 * the memory, etc.
3354 */
3355 return (NULL);
3356 }
3357
3358 phdr_mp->b_wptr = ((uchar_t *)ipha + ip_hdr_length +
3359 sizeof (ah_t) + ah_align_sz);
3360
3361 ASSERT(phdr_mp->b_wptr <= phdr_mp->b_datap->db_lim);
3362 if (outbound)
3363 *length_to_skip = ip_hdr_length;
3364 else
3365 *length_to_skip = ip_hdr_length + sizeof (ah_t) + ah_align_sz;
3366 return (phdr_mp);
3367 }
3368
3369 /*
3370 * Authenticate an outbound datagram. This function is called
3371 * whenever IP sends an outbound datagram that needs authentication.
3372 * Returns a modified packet if done. Returns NULL if error or queued.
3373 * If error return then ipIfStatsOutDiscards has been increased.
3374 */
3375 static mblk_t *
3376 ah_outbound(mblk_t *data_mp, ip_xmit_attr_t *ixa)
3377 {
3378 mblk_t *phdr_mp;
3379 ipsa_t *assoc;
3380 int length_to_skip;
3381 uint_t ah_align_sz;
3382 uint_t age_bytes;
3383 netstack_t *ns = ixa->ixa_ipst->ips_netstack;
3384 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
3385 ipsec_stack_t *ipss = ns->netstack_ipsec;
3386 ill_t *ill = ixa->ixa_nce->nce_ill;
3387 boolean_t need_refrele = B_FALSE;
3388
3389 /*
3390 * Construct the chain of mblks
3391 *
3392 * PSEUDO_HDR->DATA
3393 *
3394 * one by one.
3395 */
3396
3397 AH_BUMP_STAT(ahstack, out_requests);
3398
3399 ASSERT(data_mp->b_datap->db_type == M_DATA);
3400
3401 assoc = ixa->ixa_ipsec_ah_sa;
3402 ASSERT(assoc != NULL);
3403
3404
3405 /*
3406 * Get the outer IP header in shape to escape this system..
3407 */
3408 if (is_system_labeled() && (assoc->ipsa_otsl != NULL)) {
3409 /*
3410 * Need to update packet with any CIPSO option and update
3411 * ixa_tsl to capture the new label.
3412 * We allocate a separate ixa for that purpose.
3413 */
3414 ixa = ip_xmit_attr_duplicate(ixa);
3415 if (ixa == NULL) {
3416 ip_drop_packet(data_mp, B_FALSE, ill,
3417 DROPPER(ipss, ipds_ah_nomem),
3418 &ahstack->ah_dropper);
3419 return (NULL);
3420 }
3421 need_refrele = B_TRUE;
3422
3423 label_hold(assoc->ipsa_otsl);
3424 ip_xmit_attr_replace_tsl(ixa, assoc->ipsa_otsl);
3425
3426 data_mp = sadb_whack_label(data_mp, assoc, ixa,
3427 DROPPER(ipss, ipds_ah_nomem), &ahstack->ah_dropper);
3428 if (data_mp == NULL) {
3429 /* Packet dropped by sadb_whack_label */
3430 ixa_refrele(ixa);
3431 return (NULL);
3432 }
3433 }
3434
3435 /*
3436 * Age SA according to number of bytes that will be sent after
3437 * adding the AH header, ICV, and padding to the packet.
3438 */
3439
3440 if (ixa->ixa_flags & IXAF_IS_IPV4) {
3441 ipha_t *ipha = (ipha_t *)data_mp->b_rptr;
3442 ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
3443 IPV4_PADDING_ALIGN - 1, IPV4_PADDING_ALIGN);
3444 age_bytes = ntohs(ipha->ipha_length) + sizeof (ah_t) +
3445 ah_align_sz;
3446 } else {
3447 ip6_t *ip6h = (ip6_t *)data_mp->b_rptr;
3448 ah_align_sz = P2ALIGN(assoc->ipsa_mac_len +
3449 IPV6_PADDING_ALIGN - 1, IPV6_PADDING_ALIGN);
3450 age_bytes = sizeof (ip6_t) + ntohs(ip6h->ip6_plen) +
3451 sizeof (ah_t) + ah_align_sz;
3452 }
3453
3454 if (!ah_age_bytes(assoc, age_bytes, B_FALSE)) {
3455 /* rig things as if ipsec_getassocbyconn() failed */
3456 ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN,
3457 "AH association 0x%x, dst %s had bytes expire.\n",
3458 ntohl(assoc->ipsa_spi), assoc->ipsa_dstaddr, AF_INET,
3459 ahstack->ipsecah_netstack);
3460 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
3461 ip_drop_output("ipIfStatsOutDiscards", data_mp, ill);
3462 freemsg(data_mp);
3463 if (need_refrele)
3464 ixa_refrele(ixa);
3465 return (NULL);
3466 }
3467
3468 /*
3469 * XXX We need to have fixed up the outer label before we get here.
3470 * (AH is computing the checksum over the outer label).
3471 */
3472
3473 /*
3474 * Insert pseudo header:
3475 * [IP, ULP] => [IP, AH, ICV] -> ULP
3476 */
3477
3478 if (ixa->ixa_flags & IXAF_IS_IPV4) {
3479 phdr_mp = ah_process_ip_options_v4(data_mp, assoc,
3480 &length_to_skip, assoc->ipsa_mac_len, B_TRUE, ahstack);
3481 } else {
3482 phdr_mp = ah_process_ip_options_v6(data_mp, assoc,
3483 &length_to_skip, assoc->ipsa_mac_len, B_TRUE, ahstack);
3484 }
3485
3486 if (phdr_mp == NULL) {
3487 AH_BUMP_STAT(ahstack, out_discards);
3488 ip_drop_packet(data_mp, B_FALSE, ixa->ixa_nce->nce_ill,
3489 DROPPER(ipss, ipds_ah_bad_v4_opts),
3490 &ahstack->ah_dropper);
3491 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
3492 if (need_refrele)
3493 ixa_refrele(ixa);
3494 return (NULL);
3495 }
3496
3497 phdr_mp->b_cont = data_mp;
3498 data_mp->b_rptr += length_to_skip;
3499 data_mp = phdr_mp;
3500
3501 /*
3502 * At this point data_mp points to
3503 * an mblk containing the pseudo header (IP header,
3504 * AH header, and ICV with mutable fields zero'ed out).
3505 * mp points to the mblk containing the ULP data. The original
3506 * IP header is kept before the ULP data in data_mp.
3507 */
3508
3509 /* submit MAC request to KCF */
3510 data_mp = ah_submit_req_outbound(data_mp, ixa, length_to_skip, assoc);
3511 if (need_refrele)
3512 ixa_refrele(ixa);
3513 return (data_mp);
3514 }
3515
3516 static mblk_t *
3517 ah_inbound(mblk_t *data_mp, void *arg, ip_recv_attr_t *ira)
3518 {
3519 ah_t *ah = (ah_t *)arg;
3520 ipsa_t *assoc = ira->ira_ipsec_ah_sa;
3521 int length_to_skip;
3522 int ah_length;
3523 mblk_t *phdr_mp;
3524 uint32_t ah_offset;
3525 netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
3526 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
3527 ipsec_stack_t *ipss = ns->netstack_ipsec;
3528
3529 ASSERT(assoc != NULL);
3530
3531 /*
3532 * We may wish to check replay in-range-only here as an optimization.
3533 * Include the reality check of ipsa->ipsa_replay >
3534 * ipsa->ipsa_replay_wsize for times when it's the first N packets,
3535 * where N == ipsa->ipsa_replay_wsize.
3536 *
3537 * Another check that may come here later is the "collision" check.
3538 * If legitimate packets flow quickly enough, this won't be a problem,
3539 * but collisions may cause authentication algorithm crunching to
3540 * take place when it doesn't need to.
3541 */
3542 if (!sadb_replay_peek(assoc, ah->ah_replay)) {
3543 AH_BUMP_STAT(ahstack, replay_early_failures);
3544 IP_AH_BUMP_STAT(ipss, in_discards);
3545 ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
3546 DROPPER(ipss, ipds_ah_early_replay),
3547 &ahstack->ah_dropper);
3548 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3549 return (NULL);
3550 }
3551
3552 /*
3553 * The offset of the AH header can be computed from its pointer
3554 * within the data mblk, which was pulled up until the AH header
3555 * by ipsec_inbound_ah_sa() during SA selection.
3556 */
3557 ah_offset = (uchar_t *)ah - data_mp->b_rptr;
3558
3559 /*
3560 * We need to pullup until the ICV before we call
3561 * ah_process_ip_options_v6.
3562 */
3563 ah_length = (ah->ah_length << 2) + 8;
3564
3565 /*
3566 * NOTE : If we want to use any field of IP/AH header, you need
3567 * to re-assign following the pullup.
3568 */
3569 if (((uchar_t *)ah + ah_length) > data_mp->b_wptr) {
3570 if (!pullupmsg(data_mp, (uchar_t *)ah + ah_length -
3571 data_mp->b_rptr)) {
3572 (void) ipsec_rl_strlog(ns, info.mi_idnum, 0, 0,
3573 SL_WARN | SL_ERROR,
3574 "ah_inbound: Small AH header\n");
3575 IP_AH_BUMP_STAT(ipss, in_discards);
3576 ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
3577 DROPPER(ipss, ipds_ah_nomem),
3578 &ahstack->ah_dropper);
3579 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3580 return (NULL);
3581 }
3582 }
3583
3584 /*
3585 * Insert pseudo header:
3586 * [IP, ULP] => [IP, AH, ICV] -> ULP
3587 */
3588 if (ira->ira_flags & IRAF_IS_IPV4) {
3589 phdr_mp = ah_process_ip_options_v4(data_mp, assoc,
3590 &length_to_skip, assoc->ipsa_mac_len, B_FALSE, ahstack);
3591 } else {
3592 phdr_mp = ah_process_ip_options_v6(data_mp, assoc,
3593 &length_to_skip, assoc->ipsa_mac_len, B_FALSE, ahstack);
3594 }
3595
3596 if (phdr_mp == NULL) {
3597 IP_AH_BUMP_STAT(ipss, in_discards);
3598 ip_drop_packet(data_mp, B_TRUE, ira->ira_ill,
3599 ((ira->ira_flags & IRAF_IS_IPV4) ?
3600 DROPPER(ipss, ipds_ah_bad_v4_opts) :
3601 DROPPER(ipss, ipds_ah_bad_v6_hdrs)),
3602 &ahstack->ah_dropper);
3603 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3604 return (NULL);
3605 }
3606
3607 phdr_mp->b_cont = data_mp;
3608 data_mp->b_rptr += length_to_skip;
3609 data_mp = phdr_mp;
3610
3611 /* submit request to KCF */
3612 return (ah_submit_req_inbound(data_mp, ira, length_to_skip, ah_offset,
3613 assoc));
3614 }
3615
3616 /*
3617 * Invoked after processing of an inbound packet by the
3618 * kernel crypto framework. Called by ah_submit_req() for a sync request,
3619 * or by the kcf callback for an async request.
3620 * Returns NULL if the mblk chain is consumed.
3621 */
3622 static mblk_t *
3623 ah_auth_in_done(mblk_t *phdr_mp, ip_recv_attr_t *ira, ipsec_crypto_t *ic)
3624 {
3625 ipha_t *ipha;
3626 uint_t ah_offset = 0;
3627 mblk_t *mp;
3628 int align_len, newpos;
3629 ah_t *ah;
3630 uint32_t length;
3631 uint32_t *dest32;
3632 uint8_t *dest;
3633 boolean_t isv4;
3634 ip6_t *ip6h;
3635 uint_t icv_len;
3636 ipsa_t *assoc;
3637 kstat_named_t *counter;
3638 netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
3639 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
3640 ipsec_stack_t *ipss = ns->netstack_ipsec;
3641
3642 isv4 = (ira->ira_flags & IRAF_IS_IPV4);
3643 assoc = ira->ira_ipsec_ah_sa;
3644 icv_len = (uint_t)ic->ic_crypto_mac.cd_raw.iov_len;
3645
3646 if (phdr_mp == NULL) {
3647 ip_drop_packet(phdr_mp, B_TRUE, ira->ira_ill,
3648 DROPPER(ipss, ipds_ah_nomem),
3649 &ahstack->ah_dropper);
3650 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3651 return (NULL);
3652 }
3653
3654 mp = phdr_mp->b_cont;
3655 if (mp == NULL) {
3656 ip_drop_packet(phdr_mp, B_TRUE, ira->ira_ill,
3657 DROPPER(ipss, ipds_ah_nomem),
3658 &ahstack->ah_dropper);
3659 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3660 return (NULL);
3661 }
3662 mp->b_rptr -= ic->ic_skip_len;
3663
3664 ah_set_usetime(assoc, B_TRUE);
3665
3666 if (isv4) {
3667 ipha = (ipha_t *)mp->b_rptr;
3668 ah_offset = ipha->ipha_version_and_hdr_length -
3669 (uint8_t)((IP_VERSION << 4));
3670 ah_offset <<= 2;
3671 align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
3672 IPV4_PADDING_ALIGN);
3673 } else {
3674 ip6h = (ip6_t *)mp->b_rptr;
3675 ah_offset = ipsec_ah_get_hdr_size_v6(mp, B_TRUE);
3676 ASSERT((mp->b_wptr - mp->b_rptr) >= ah_offset);
3677 align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1,
3678 IPV6_PADDING_ALIGN);
3679 }
3680
3681 ah = (ah_t *)(mp->b_rptr + ah_offset);
3682 newpos = sizeof (ah_t) + align_len;
3683
3684 /*
3685 * We get here only when authentication passed.
3686 */
3687
3688 ah3dbg(ahstack, ("AH succeeded, checking replay\n"));
3689 AH_BUMP_STAT(ahstack, good_auth);
3690
3691 if (!sadb_replay_check(assoc, ah->ah_replay)) {
3692 int af;
3693 void *addr;
3694
3695 if (isv4) {
3696 addr = &ipha->ipha_dst;
3697 af = AF_INET;
3698 } else {
3699 addr = &ip6h->ip6_dst;
3700 af = AF_INET6;
3701 }
3702
3703 /*
3704 * Log the event. As of now we print out an event.
3705 * Do not print the replay failure number, or else
3706 * syslog cannot collate the error messages. Printing
3707 * the replay number that failed (or printing to the
3708 * console) opens a denial-of-service attack.
3709 */
3710 AH_BUMP_STAT(ahstack, replay_failures);
3711 ipsec_assocfailure(info.mi_idnum, 0, 0,
3712 SL_ERROR | SL_WARN,
3713 "Replay failed for AH spi %x, dst_addr %s",
3714 assoc->ipsa_spi, addr, af, ahstack->ipsecah_netstack);
3715 counter = DROPPER(ipss, ipds_ah_replay);
3716 goto ah_in_discard;
3717 }
3718
3719 /*
3720 * We need to remove the AH header from the original
3721 * datagram. Best way to do this is to move the pre-AH headers
3722 * forward in the (relatively simple) IPv4 case. In IPv6, it's
3723 * a bit more complicated because of IPv6's next-header chaining,
3724 * but it's doable.
3725 */
3726 if (isv4) {
3727 /*
3728 * Assign the right protocol, adjust the length as we
3729 * are removing the AH header and adjust the checksum to
3730 * account for the protocol and length.
3731 */
3732 length = ntohs(ipha->ipha_length);
3733 if (!ah_age_bytes(assoc, length, B_TRUE)) {
3734 /* The ipsa has hit hard expiration, LOG and AUDIT. */
3735 ipsec_assocfailure(info.mi_idnum, 0, 0,
3736 SL_ERROR | SL_WARN,
3737 "AH Association 0x%x, dst %s had bytes expire.\n",
3738 assoc->ipsa_spi, assoc->ipsa_dstaddr,
3739 AF_INET, ahstack->ipsecah_netstack);
3740 AH_BUMP_STAT(ahstack, bytes_expired);
3741 counter = DROPPER(ipss, ipds_ah_bytes_expire);
3742 goto ah_in_discard;
3743 }
3744 ipha->ipha_protocol = ah->ah_nexthdr;
3745 length -= newpos;
3746
3747 ipha->ipha_length = htons((uint16_t)length);
3748 ipha->ipha_hdr_checksum = 0;
3749 ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
3750 } else {
3751 uchar_t *whereptr;
3752 int hdrlen;
3753 uint8_t *nexthdr;
3754 ip6_hbh_t *hbhhdr;
3755 ip6_dest_t *dsthdr;
3756 ip6_rthdr0_t *rthdr;
3757
3758 /*
3759 * Make phdr_mp hold until the AH header and make
3760 * mp hold everything past AH header.
3761 */
3762 length = ntohs(ip6h->ip6_plen);
3763 if (!ah_age_bytes(assoc, length + sizeof (ip6_t), B_TRUE)) {
3764 /* The ipsa has hit hard expiration, LOG and AUDIT. */
3765 ipsec_assocfailure(info.mi_idnum, 0, 0,
3766 SL_ERROR | SL_WARN,
3767 "AH Association 0x%x, dst %s had bytes "
3768 "expire.\n", assoc->ipsa_spi, &ip6h->ip6_dst,
3769 AF_INET6, ahstack->ipsecah_netstack);
3770 AH_BUMP_STAT(ahstack, bytes_expired);
3771 counter = DROPPER(ipss, ipds_ah_bytes_expire);
3772 goto ah_in_discard;
3773 }
3774
3775 /*
3776 * Update the next header field of the header preceding
3777 * AH with the next header field of AH. Start with the
3778 * IPv6 header and proceed with the extension headers
3779 * until we find what we're looking for.
3780 */
3781 nexthdr = &ip6h->ip6_nxt;
3782 whereptr = (uchar_t *)ip6h;
3783 hdrlen = sizeof (ip6_t);
3784
3785 while (*nexthdr != IPPROTO_AH) {
3786 whereptr += hdrlen;
3787 /* Assume IP has already stripped it */
3788 ASSERT(*nexthdr != IPPROTO_FRAGMENT);
3789 switch (*nexthdr) {
3790 case IPPROTO_HOPOPTS:
3791 hbhhdr = (ip6_hbh_t *)whereptr;
3792 nexthdr = &hbhhdr->ip6h_nxt;
3793 hdrlen = 8 * (hbhhdr->ip6h_len + 1);
3794 break;
3795 case IPPROTO_DSTOPTS:
3796 dsthdr = (ip6_dest_t *)whereptr;
3797 nexthdr = &dsthdr->ip6d_nxt;
3798 hdrlen = 8 * (dsthdr->ip6d_len + 1);
3799 break;
3800 case IPPROTO_ROUTING:
3801 rthdr = (ip6_rthdr0_t *)whereptr;
3802 nexthdr = &rthdr->ip6r0_nxt;
3803 hdrlen = 8 * (rthdr->ip6r0_len + 1);
3804 break;
3805 }
3806 }
3807 *nexthdr = ah->ah_nexthdr;
3808 length -= newpos;
3809 ip6h->ip6_plen = htons((uint16_t)length);
3810 }
3811
3812 /* Now that we've fixed the IP header, move it forward. */
3813 mp->b_rptr += newpos;
3814 if (IS_P2ALIGNED(mp->b_rptr, sizeof (uint32_t))) {
3815 dest32 = (uint32_t *)(mp->b_rptr + ah_offset);
3816 while (--dest32 >= (uint32_t *)mp->b_rptr)
3817 *dest32 = *(dest32 - (newpos >> 2));
3818 } else {
3819 dest = mp->b_rptr + ah_offset;
3820 while (--dest >= mp->b_rptr)
3821 *dest = *(dest - newpos);
3822 }
3823 freeb(phdr_mp);
3824
3825 /*
3826 * If SA is labelled, use its label, else inherit the label
3827 */
3828 if (is_system_labeled() && (assoc->ipsa_tsl != NULL)) {
3829 if (!ip_recv_attr_replace_label(ira, assoc->ipsa_tsl)) {
3830 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
3831 DROPPER(ipss, ipds_ah_nomem), &ahstack->ah_dropper);
3832 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3833 return (NULL);
3834 }
3835 }
3836
3837 if (assoc->ipsa_state == IPSA_STATE_IDLE) {
3838 /*
3839 * Cluster buffering case. Tell caller that we're
3840 * handling the packet.
3841 */
3842 sadb_buf_pkt(assoc, mp, ira);
3843 return (NULL);
3844 }
3845
3846 return (mp);
3847
3848 ah_in_discard:
3849 IP_AH_BUMP_STAT(ipss, in_discards);
3850 ip_drop_packet(phdr_mp, B_TRUE, ira->ira_ill, counter,
3851 &ahstack->ah_dropper);
3852 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
3853 return (NULL);
3854 }
3855
3856 /*
3857 * Invoked after processing of an outbound packet by the
3858 * kernel crypto framework, either by ah_submit_req() for a request
3859 * executed syncrhonously, or by the KEF callback for a request
3860 * executed asynchronously.
3861 */
3862 static mblk_t *
3863 ah_auth_out_done(mblk_t *phdr_mp, ip_xmit_attr_t *ixa, ipsec_crypto_t *ic)
3864 {
3865 mblk_t *mp;
3866 int align_len;
3867 uint32_t hdrs_length;
3868 uchar_t *ptr;
3869 uint32_t length;
3870 boolean_t isv4;
3871 size_t icv_len;
3872 netstack_t *ns = ixa->ixa_ipst->ips_netstack;
3873 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
3874 ipsec_stack_t *ipss = ns->netstack_ipsec;
3875 ill_t *ill = ixa->ixa_nce->nce_ill;
3876
3877 isv4 = (ixa->ixa_flags & IXAF_IS_IPV4);
3878 icv_len = ic->ic_crypto_mac.cd_raw.iov_len;
3879
3880 mp = phdr_mp->b_cont;
3881 if (mp == NULL) {
3882 ip_drop_packet(phdr_mp, B_FALSE, ill,
3883 DROPPER(ipss, ipds_ah_nomem),
3884 &ahstack->ah_dropper);
3885 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
3886 return (NULL);
3887 }
3888 mp->b_rptr -= ic->ic_skip_len;
3889
3890 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
3891 ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
3892 ah_set_usetime(ixa->ixa_ipsec_ah_sa, B_FALSE);
3893
3894 if (isv4) {
3895 ipha_t *ipha;
3896 ipha_t *nipha;
3897
3898 ipha = (ipha_t *)mp->b_rptr;
3899 hdrs_length = ipha->ipha_version_and_hdr_length -
3900 (uint8_t)((IP_VERSION << 4));
3901 hdrs_length <<= 2;
3902 align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1,
3903 IPV4_PADDING_ALIGN);
3904 /*
3905 * phdr_mp must have the right amount of space for the
3906 * combined IP and AH header. Copy the IP header and
3907 * the ack_data onto AH. Note that the AH header was
3908 * already formed before the ICV calculation and hence
3909 * you don't have to copy it here.
3910 */
3911 bcopy(mp->b_rptr, phdr_mp->b_rptr, hdrs_length);
3912
3913 ptr = phdr_mp->b_rptr + hdrs_length + sizeof (ah_t);
3914 bcopy(phdr_mp->b_wptr, ptr, icv_len);
3915
3916 /*
3917 * Compute the new header checksum as we are assigning
3918 * IPPROTO_AH and adjusting the length here.
3919 */
3920 nipha = (ipha_t *)phdr_mp->b_rptr;
3921
3922 nipha->ipha_protocol = IPPROTO_AH;
3923 length = ntohs(nipha->ipha_length);
3924 length += (sizeof (ah_t) + align_len);
3925 nipha->ipha_length = htons((uint16_t)length);
3926 nipha->ipha_hdr_checksum = 0;
3927 nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha);
3928 } else {
3929 ip6_t *ip6h;
3930 ip6_t *nip6h;
3931 uint_t ah_offset;
3932
3933 ip6h = (ip6_t *)mp->b_rptr;
3934 nip6h = (ip6_t *)phdr_mp->b_rptr;
3935 align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1,
3936 IPV6_PADDING_ALIGN);
3937 /*
3938 * phdr_mp must have the right amount of space for the
3939 * combined IP and AH header. Copy the IP header with
3940 * options into the pseudo header. When we constructed
3941 * a pseudo header, we did not copy some of the mutable
3942 * fields. We do it now by calling ah_fix_phdr_v6()
3943 * with the last argument B_TRUE. It returns the
3944 * ah_offset into the pseudo header.
3945 */
3946
3947 bcopy(ip6h, nip6h, IPV6_HDR_LEN);
3948 ah_offset = ah_fix_phdr_v6(nip6h, ip6h, B_TRUE, B_TRUE);
3949 ASSERT(ah_offset != 0);
3950 /*
3951 * phdr_mp can hold exactly the whole IP header with options
3952 * plus the AH header also. Thus subtracting the AH header's
3953 * size should give exactly how much of the original header
3954 * should be skipped.
3955 */
3956 hdrs_length = (phdr_mp->b_wptr - phdr_mp->b_rptr) -
3957 sizeof (ah_t) - icv_len;
3958 bcopy(phdr_mp->b_wptr, ((uint8_t *)nip6h + ah_offset +
3959 sizeof (ah_t)), icv_len);
3960 length = ntohs(nip6h->ip6_plen);
3961 length += (sizeof (ah_t) + align_len);
3962 nip6h->ip6_plen = htons((uint16_t)length);
3963 }
3964
3965 /* Skip the original IP header */
3966 mp->b_rptr += hdrs_length;
3967 if (mp->b_rptr == mp->b_wptr) {
3968 phdr_mp->b_cont = mp->b_cont;
3969 freeb(mp);
3970 }
3971
3972 return (phdr_mp);
3973 }
3974
3975 /* Refactor me */
3976 /*
3977 * Wrapper to allow IP to trigger an AH association failure message
3978 * during SA inbound selection.
3979 */
3980 void
3981 ipsecah_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt,
3982 uint32_t spi, void *addr, int af, ip_recv_attr_t *ira)
3983 {
3984 netstack_t *ns = ira->ira_ill->ill_ipst->ips_netstack;
3985 ipsecah_stack_t *ahstack = ns->netstack_ipsecah;
3986 ipsec_stack_t *ipss = ns->netstack_ipsec;
3987
3988 if (ahstack->ipsecah_log_unknown_spi) {
3989 ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi,
3990 addr, af, ahstack->ipsecah_netstack);
3991 }
3992
3993 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
3994 DROPPER(ipss, ipds_ah_no_sa),
3995 &ahstack->ah_dropper);
3996 }
3997
3998 /*
3999 * Initialize the AH input and output processing functions.
4000 */
4001 void
4002 ipsecah_init_funcs(ipsa_t *sa)
4003 {
4004 if (sa->ipsa_output_func == NULL)
4005 sa->ipsa_output_func = ah_outbound;
4006 if (sa->ipsa_input_func == NULL)
4007 sa->ipsa_input_func = ah_inbound;
4008 }