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