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