1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #ifndef _INET_SADB_H
  27 #define _INET_SADB_H
  28 
  29 #ifdef  __cplusplus
  30 extern "C" {
  31 #endif
  32 
  33 #include <inet/ipsec_info.h>
  34 #include <sys/crypto/common.h>
  35 #include <sys/crypto/api.h>
  36 #include <sys/note.h>
  37 
  38 #define IPSA_MAX_ADDRLEN 4      /* Max address len. (in 32-bits) for an SA. */
  39 
  40 #define MAXSALTSIZE 8
  41 
  42 /*
  43  * For combined mode ciphers, store the crypto_mechanism_t in the
  44  * per-packet ipsec_in_t/ipsec_out_t structures. This is because the PARAMS
  45  * and nonce values change for each packet. For non-combined mode
  46  * ciphers, these values are constant for the life of the SA.
  47  */
  48 typedef struct ipsa_cm_mech_s {
  49         crypto_mechanism_t combined_mech;
  50         union {
  51                 CK_AES_CCM_PARAMS paramu_ccm;
  52                 CK_AES_GCM_PARAMS paramu_gcm;
  53         } paramu;
  54         uint8_t nonce[MAXSALTSIZE + sizeof (uint64_t)];
  55 #define param_ulMACSize paramu.paramu_ccm.ulMACSize
  56 #define param_ulNonceSize paramu.paramu_ccm.ipsa_ulNonceSize
  57 #define param_ulAuthDataSize paramu.paramu_ccm.ipsa_ulAuthDataSize
  58 #define param_ulDataSize paramu.paramu_ccm.ipsa_ulDataSize
  59 #define param_nonce paramu.paramu_ccm.nonce
  60 #define param_authData paramu.paramu_ccm.authData
  61 #define param_pIv paramu.paramu_gcm.ipsa_pIv
  62 #define param_ulIvLen paramu.paramu_gcm.ulIvLen
  63 #define param_ulIvBits paramu.paramu_gcm.ulIvBits
  64 #define param_pAAD paramu.paramu_gcm.pAAD
  65 #define param_ulAADLen paramu.paramu_gcm.ulAADLen
  66 #define param_ulTagBits paramu.paramu_gcm.ulTagBits
  67 } ipsa_cm_mech_t;
  68 
  69 /*
  70  * The Initialization Vector (also known as IV or Nonce) used to
  71  * initialize the Block Cipher, is made up of a Counter and a Salt.
  72  * The Counter is fixed at 64 bits and is incremented for each packet.
  73  * The Salt value can be any whole byte value upto 64 bits. This is
  74  * algorithm mode specific and can be configured with ipsecalgs(1m).
  75  *
  76  * We only support whole byte salt lengths, this is because the salt is
  77  * stored in an array of uint8_t's. This is enforced by ipsecalgs(1m)
  78  * which configures the salt length as a number of bytes. Checks are
  79  * made to ensure the salt length defined in ipsecalgs(1m) fits in
  80  * the ipsec_nonce_t.
  81  *
  82  * The Salt value remains constant for the life of the SA, the Salt is
  83  * know to both peers, but NOT transmitted on the network. The Counter
  84  * portion of the nonce is transmitted over the network with each packet
  85  * and is confusingly described as the Initialization Vector by RFCs
  86  * 4309/4106.
  87  *
  88  * The maximum Initialization Vector length is 128 bits, if the actual
  89  * size is less, its padded internally by the algorithm.
  90  *
  91  * The nonce structure is defined like this in the SA (ipsa_t)to ensure
  92  * the Initilization Vector (counter) is 64 bit aligned, because it will
  93  * be incremented as an uint64_t. The nonce as used by the algorithms is
  94  * a straight uint8_t array.
  95  *
  96  *                     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  97  *                     | | | | |x|x|x|x|               |
  98  *                     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  99  * salt_offset         <------>
 100  * ipsa_saltlen                <------->
 101  * ipsa_nonce_buf------^
 102  * ipsa_salt-------------~~~~~~^
 103  * ipsa_nonce------------~~~~~~^
 104  * ipsa_iv-----------------------------^
 105  */
 106 typedef struct ipsec_nonce_s {
 107         uint8_t         salt[MAXSALTSIZE];
 108         uint64_t        iv;
 109 } ipsec_nonce_t;
 110 
 111 /*
 112  * IP security association.  Synchronization assumes 32-bit loads, so
 113  * the 64-bit quantities can't even be be read w/o locking it down!
 114  */
 115 
 116 /* keying info */
 117 typedef struct ipsa_key_s {
 118         uint8_t *sak_key;               /* Algorithm key. */
 119         uint_t sak_keylen;      /* Algorithm key length (in bytes). */
 120         uint_t sak_keybits;     /* Algorithm key length (in bits) */
 121         uint_t sak_algid;       /* Algorithm ID number. */
 122 } ipsa_key_t;
 123 
 124 typedef struct ipsa_s {
 125         struct ipsa_s *ipsa_next;       /* Next in hash bucket */
 126         struct ipsa_s **ipsa_ptpn;      /* Pointer to previous next pointer. */
 127         kmutex_t *ipsa_linklock;        /* Pointer to hash-chain lock. */
 128         void (*ipsa_freefunc)(struct ipsa_s *); /* freeassoc function */
 129         void (*ipsa_noncefunc)(struct ipsa_s *, uchar_t *,
 130             uint_t, uchar_t *, ipsa_cm_mech_t *, crypto_data_t *);
 131         /*
 132          * NOTE: I may need more pointers, depending on future SA
 133          * requirements.
 134          */
 135         ipsa_key_t ipsa_authkeydata;
 136 #define ipsa_authkey ipsa_authkeydata.sak_key
 137 #define ipsa_authkeylen ipsa_authkeydata.sak_keylen
 138 #define ipsa_authkeybits ipsa_authkeydata.sak_keybits
 139 #define ipsa_auth_alg ipsa_authkeydata.sak_algid
 140         ipsa_key_t ipsa_encrkeydata;
 141 #define ipsa_encrkey ipsa_encrkeydata.sak_key
 142 #define ipsa_encrkeylen ipsa_encrkeydata.sak_keylen
 143 #define ipsa_encrkeybits ipsa_encrkeydata.sak_keybits
 144 #define ipsa_encr_alg ipsa_encrkeydata.sak_algid
 145 
 146         struct ipsid_s *ipsa_src_cid;   /* Source certificate identity */
 147         struct ipsid_s *ipsa_dst_cid;   /* Destination certificate identity */
 148         mblk_t  *ipsa_lpkt;     /* Packet received while larval (CAS me) */
 149 
 150         /*
 151          * PF_KEYv2 supports a replay window size of 255.  Hence there is a
 152          * need a bit vector to support a replay window of 255.  256 is a nice
 153          * round number, so I support that.
 154          *
 155          * Use an array of uint64_t for best performance on 64-bit
 156          * processors.  (And hope that 32-bit compilers can handle things
 157          * okay.)  The " >> 6 " is to get the appropriate number of 64-bit
 158          * ints.
 159          */
 160 #define SADB_MAX_REPLAY 256     /* Must be 0 mod 64. */
 161         uint64_t ipsa_replay_arr[SADB_MAX_REPLAY >> 6];
 162 
 163         uint64_t ipsa_unique_id;        /* Non-zero for unique SAs */
 164         uint64_t ipsa_unique_mask;      /* mask value for unique_id */
 165 
 166         /*
 167          * Reference count semantics:
 168          *
 169          *      An SA has a reference count of 1 if something's pointing
 170          *      to it.  This includes being in a hash table.  So if an
 171          *      SA is in a hash table, it has a reference count of at least 1.
 172          *
 173          *      When a ptr. to an IPSA is assigned, you MUST REFHOLD after
 174          *      said assignment.  When a ptr. to an IPSA is released
 175          *      you MUST REFRELE.  When the refcount hits 0, REFRELE
 176          *      will free the IPSA.
 177          */
 178         kmutex_t ipsa_lock;     /* Locks non-linkage/refcnt fields. */
 179         /* Q:  Since I may be doing refcnts differently, will I need cv? */
 180         uint_t ipsa_refcnt;     /* Reference count. */
 181 
 182         /*
 183          * The following four time fields are the ones monitored by ah_ager()
 184          * and esp_ager() respectively.  They are all absolute wall-clock
 185          * times.  The times of creation (i.e. add time) and first use are
 186          * pretty straightforward.  The soft and hard expire times are
 187          * derived from the times of first use and creation, plus the minimum
 188          * expiration times in the fields that follow this.
 189          *
 190          * For example, if I had a hard add time of 30 seconds, and a hard
 191          * use time of 15, the ipsa_hardexpiretime would be time of add, plus
 192          * 30 seconds.  If I USE the SA such that time of first use plus 15
 193          * seconds would be earlier than the add time plus 30 seconds, then
 194          * ipsa_hardexpiretime would become this earlier time.
 195          */
 196         time_t ipsa_addtime;    /* Time I was added. */
 197         time_t ipsa_usetime;    /* Time of my first use. */
 198         time_t ipsa_lastuse;    /* Time of my last use. */
 199         time_t ipsa_idletime;   /* Seconds of idle time */
 200         time_t ipsa_last_nat_t_ka;      /* Time of my last NAT-T keepalive. */
 201         time_t ipsa_softexpiretime;     /* Time of my first soft expire. */
 202         time_t ipsa_hardexpiretime;     /* Time of my first hard expire. */
 203         time_t ipsa_idleexpiretime;     /* Time of my next idle expire time */
 204 
 205         struct ipsec_nonce_s *ipsa_nonce_buf;
 206         uint8_t *ipsa_nonce;
 207         uint_t ipsa_nonce_len;
 208         uint8_t *ipsa_salt;
 209         uint_t ipsa_saltbits;
 210         uint_t ipsa_saltlen;
 211         uint64_t *ipsa_iv;
 212 
 213         uint64_t ipsa_iv_hardexpire;
 214         uint64_t ipsa_iv_softexpire;
 215         /*
 216          * The following fields are directly reflected in PF_KEYv2 LIFETIME
 217          * extensions.  The time_ts are in number-of-seconds, and the bytes
 218          * are in... bytes.
 219          */
 220         time_t ipsa_softaddlt;  /* Seconds of soft lifetime after add. */
 221         time_t ipsa_softuselt;  /* Seconds of soft lifetime after first use. */
 222         time_t ipsa_hardaddlt;  /* Seconds of hard lifetime after add. */
 223         time_t ipsa_harduselt;  /* Seconds of hard lifetime after first use. */
 224         time_t ipsa_idleaddlt;  /* Seconds of idle time after add */
 225         time_t ipsa_idleuselt;  /* Seconds of idle time after first use */
 226         uint64_t ipsa_softbyteslt;      /* Bytes of soft lifetime. */
 227         uint64_t ipsa_hardbyteslt;      /* Bytes of hard lifetime. */
 228         uint64_t ipsa_bytes;    /* Bytes encrypted/authed by this SA. */
 229 
 230         /*
 231          * "Allocations" are a concept mentioned in PF_KEYv2.  We do not
 232          * support them, except to record them per the PF_KEYv2 spec.
 233          */
 234         uint_t ipsa_softalloc;  /* Allocations allowed (soft). */
 235         uint_t ipsa_hardalloc;  /* Allocations allowed (hard). */
 236         uint_t ipsa_alloc;      /* Allocations made. */
 237 
 238         uint_t ipsa_type;       /* Type of security association. (AH/etc.) */
 239         uint_t ipsa_state;      /* State of my association. */
 240         uint_t ipsa_replay_wsize; /* Size of replay window */
 241         uint32_t ipsa_flags;    /* Flags for security association. */
 242         uint32_t ipsa_spi;      /* Security parameters index. */
 243         uint32_t ipsa_replay;   /* Highest seen replay value for this SA. */
 244         uint32_t ipsa_kmp;      /* key management proto */
 245         uint32_t ipsa_kmc;      /* key management cookie */
 246 
 247         boolean_t ipsa_haspeer;         /* Has peer in another table. */
 248 
 249         /*
 250          * Address storage.
 251          * The source address can be INADDR_ANY, IN6ADDR_ANY, etc.
 252          *
 253          * Address families (per sys/socket.h) guide us.  We could have just
 254          * used sockaddr_storage
 255          */
 256         sa_family_t ipsa_addrfam;
 257         sa_family_t ipsa_innerfam;      /* Inner AF can be != src/dst AF. */
 258 
 259         uint32_t ipsa_srcaddr[IPSA_MAX_ADDRLEN];
 260         uint32_t ipsa_dstaddr[IPSA_MAX_ADDRLEN];
 261         uint32_t ipsa_innersrc[IPSA_MAX_ADDRLEN];
 262         uint32_t ipsa_innerdst[IPSA_MAX_ADDRLEN];
 263 
 264         uint8_t ipsa_innersrcpfx;
 265         uint8_t ipsa_innerdstpfx;
 266 
 267         uint16_t ipsa_inbound_cksum; /* cksum correction for inbound packets */
 268         uint16_t ipsa_local_nat_port;   /* Local NAT-T port.  (0 --> 4500) */
 269         uint16_t ipsa_remote_nat_port; /* The other port that isn't 4500 */
 270 
 271         /* these can only be v4 */
 272         uint32_t ipsa_natt_addr_loc;
 273         uint32_t ipsa_natt_addr_rem;
 274 
 275         /*
 276          * icmp type and code. *_end are to specify ranges. if only
 277          * a single value, * and *_end are the same value.
 278          */
 279         uint8_t ipsa_icmp_type;
 280         uint8_t ipsa_icmp_type_end;
 281         uint8_t ipsa_icmp_code;
 282         uint8_t ipsa_icmp_code_end;
 283 
 284         /*
 285          * For the kernel crypto framework.
 286          */
 287         crypto_key_t ipsa_kcfauthkey;           /* authentication key */
 288         crypto_key_t ipsa_kcfencrkey;           /* encryption key */
 289         crypto_ctx_template_t ipsa_authtmpl;    /* auth context template */
 290         crypto_ctx_template_t ipsa_encrtmpl;    /* encr context template */
 291         crypto_mechanism_t ipsa_amech;          /* auth mech type and ICV len */
 292         crypto_mechanism_t ipsa_emech;          /* encr mech type */
 293         size_t ipsa_mac_len;                    /* auth MAC/ICV length */
 294         size_t ipsa_iv_len;                     /* encr IV length */
 295         size_t ipsa_datalen;                    /* block length in bytes. */
 296 
 297         /*
 298          * Input and output processing functions called from IP.
 299          * The mblk_t is the data; the IPsec information is in the attributes
 300          * Returns NULL if the mblk is consumed which it is if there was
 301          * a failure or if pending. If failure then
 302          * the ipIfInDiscards/OutDiscards counters are increased.
 303          */
 304         mblk_t *(*ipsa_output_func)(mblk_t *, ip_xmit_attr_t *);
 305         mblk_t *(*ipsa_input_func)(mblk_t *, void *, ip_recv_attr_t *);
 306 
 307         /*
 308          * Soft reference to paired SA
 309          */
 310         uint32_t        ipsa_otherspi;
 311         netstack_t      *ipsa_netstack; /* Does not have a netstack_hold */
 312 
 313         ts_label_t *ipsa_tsl;                   /* MLS: label attributes */
 314         ts_label_t *ipsa_otsl;                  /* MLS: outer label */
 315         uint8_t ipsa_mac_exempt;                /* MLS: mac exempt flag */
 316         uchar_t ipsa_opt_storage[IP_MAX_OPT_LENGTH];
 317 } ipsa_t;
 318 
 319 /*
 320  * ipsa_t address handling macros.  We want these to be inlined, and deal
 321  * with 32-bit words to avoid bcmp/bcopy calls.
 322  *
 323  * Assume we only have AF_INET and AF_INET6 addresses for now.  Also assume
 324  * that we have 32-bit alignment on everything.
 325  */
 326 #define IPSA_IS_ADDR_UNSPEC(addr, fam) ((((uint32_t *)(addr))[0] == 0) && \
 327         (((fam) == AF_INET) || (((uint32_t *)(addr))[3] == 0 && \
 328         ((uint32_t *)(addr))[2] == 0 && ((uint32_t *)(addr))[1] == 0)))
 329 #define IPSA_ARE_ADDR_EQUAL(addr1, addr2, fam) \
 330         ((((uint32_t *)(addr1))[0] == ((uint32_t *)(addr2))[0]) && \
 331         (((fam) == AF_INET) || \
 332         (((uint32_t *)(addr1))[3] == ((uint32_t *)(addr2))[3] && \
 333         ((uint32_t *)(addr1))[2] == ((uint32_t *)(addr2))[2] && \
 334         ((uint32_t *)(addr1))[1] == ((uint32_t *)(addr2))[1])))
 335 #define IPSA_COPY_ADDR(dstaddr, srcaddr, fam) { \
 336         ((uint32_t *)(dstaddr))[0] = ((uint32_t *)(srcaddr))[0]; \
 337         if ((fam) == AF_INET6) {\
 338                 ((uint32_t *)(dstaddr))[1] = ((uint32_t *)(srcaddr))[1]; \
 339                 ((uint32_t *)(dstaddr))[2] = ((uint32_t *)(srcaddr))[2]; \
 340                 ((uint32_t *)(dstaddr))[3] = ((uint32_t *)(srcaddr))[3]; } }
 341 
 342 /*
 343  * ipsa_t reference hold/release macros.
 344  *
 345  * If you have a pointer, you REFHOLD.  If you are releasing a pointer, you
 346  * REFRELE.  An ipsa_t that is newly inserted into the table should have
 347  * a reference count of 1 (for the table's pointer), plus 1 more for every
 348  * pointer that is referencing the ipsa_t.
 349  */
 350 
 351 #define IPSA_REFHOLD(ipsa) {                    \
 352         atomic_inc_32(&(ipsa)->ipsa_refcnt);     \
 353         ASSERT((ipsa)->ipsa_refcnt != 0);    \
 354 }
 355 
 356 /*
 357  * Decrement the reference count on the SA.
 358  * In architectures e.g sun4u, where atomic_add_32_nv is just
 359  * a cas, we need to maintain the right memory barrier semantics
 360  * as that of mutex_exit i.e all the loads and stores should complete
 361  * before the cas is executed. membar_exit() does that here.
 362  */
 363 
 364 #define IPSA_REFRELE(ipsa) {                                    \
 365         ASSERT((ipsa)->ipsa_refcnt != 0);                    \
 366         membar_exit();                                          \
 367         if (atomic_dec_32_nv(&(ipsa)->ipsa_refcnt) == 0) \
 368                 ((ipsa)->ipsa_freefunc)(ipsa);                       \
 369 }
 370 
 371 /*
 372  * Security association hash macros and definitions.  For now, assume the
 373  * IPsec model, and hash outbounds on destination address, and inbounds on
 374  * SPI.
 375  */
 376 
 377 #define IPSEC_DEFAULT_HASH_SIZE 256
 378 
 379 #define INBOUND_HASH(sadb, spi) ((spi) % ((sadb)->sdb_hashsize))
 380 #define OUTBOUND_HASH_V4(sadb, v4addr) ((v4addr) % ((sadb)->sdb_hashsize))
 381 #define OUTBOUND_HASH_V6(sadb, v6addr) OUTBOUND_HASH_V4((sadb), \
 382         (*(uint32_t *)&(v6addr)) ^ (*(((uint32_t *)&(v6addr)) + 1)) ^ \
 383         (*(((uint32_t *)&(v6addr)) + 2)) ^ (*(((uint32_t *)&(v6addr)) + 3)))
 384 
 385 /*
 386  * Syntactic sugar to find the appropriate hash bucket directly.
 387  */
 388 
 389 #define INBOUND_BUCKET(sadb, spi) &(((sadb)->sdb_if)[INBOUND_HASH(sadb, spi)])
 390 #define OUTBOUND_BUCKET_V4(sadb, v4addr) \
 391         &(((sadb)->sdb_of)[OUTBOUND_HASH_V4(sadb, v4addr)])
 392 #define OUTBOUND_BUCKET_V6(sadb, v6addr) \
 393         &(((sadb)->sdb_of)[OUTBOUND_HASH_V6(sadb, v6addr)])
 394 
 395 #define IPSA_F_PFS      SADB_SAFLAGS_PFS        /* PFS in use for this SA? */
 396 #define IPSA_F_NOREPFLD SADB_SAFLAGS_NOREPLAY   /* No replay field, for */
 397                                                 /* backward compat. */
 398 #define IPSA_F_USED     SADB_X_SAFLAGS_USED     /* SA has been used. */
 399 #define IPSA_F_UNIQUE   SADB_X_SAFLAGS_UNIQUE   /* SA is unique */
 400 #define IPSA_F_AALG1    SADB_X_SAFLAGS_AALG1    /* Auth alg flag 1 */
 401 #define IPSA_F_AALG2    SADB_X_SAFLAGS_AALG2    /* Auth alg flag 2 */
 402 #define IPSA_F_EALG1    SADB_X_SAFLAGS_EALG1    /* Encrypt alg flag 1 */
 403 #define IPSA_F_EALG2    SADB_X_SAFLAGS_EALG2    /* Encrypt alg flag 2 */
 404 
 405 #define IPSA_F_ASYNC    0x200000                /* Call KCF asynchronously? */
 406 #define IPSA_F_NATT_LOC SADB_X_SAFLAGS_NATT_LOC
 407 #define IPSA_F_NATT_REM SADB_X_SAFLAGS_NATT_REM
 408 #define IPSA_F_BEHIND_NAT SADB_X_SAFLAGS_NATTED
 409 #define IPSA_F_NATT     (SADB_X_SAFLAGS_NATT_LOC | SADB_X_SAFLAGS_NATT_REM | \
 410         SADB_X_SAFLAGS_NATTED)
 411 #define IPSA_F_CINVALID 0x40000         /* SA shouldn't be cached */
 412 #define IPSA_F_PAIRED   SADB_X_SAFLAGS_PAIRED   /* SA is one of a pair */
 413 #define IPSA_F_OUTBOUND SADB_X_SAFLAGS_OUTBOUND /* SA direction bit */
 414 #define IPSA_F_INBOUND  SADB_X_SAFLAGS_INBOUND  /* SA direction bit */
 415 #define IPSA_F_TUNNEL   SADB_X_SAFLAGS_TUNNEL
 416 /*
 417  * These flags are only defined here to prevent a flag value collision.
 418  */
 419 #define IPSA_F_COMBINED SADB_X_SAFLAGS_EALG1    /* Defined in pfkeyv2.h */
 420 #define IPSA_F_COUNTERMODE SADB_X_SAFLAGS_EALG2 /* Defined in pfkeyv2.h */
 421 
 422 /*
 423  * Sets of flags that are allowed to by set or modified by PF_KEY apps.
 424  */
 425 #define AH_UPDATE_SETTABLE_FLAGS \
 426         (SADB_X_SAFLAGS_PAIRED | SADB_SAFLAGS_NOREPLAY | \
 427         SADB_X_SAFLAGS_OUTBOUND | SADB_X_SAFLAGS_INBOUND | \
 428         SADB_X_SAFLAGS_KM1 | SADB_X_SAFLAGS_KM2 | \
 429         SADB_X_SAFLAGS_KM3 | SADB_X_SAFLAGS_KM4)
 430 
 431 /* AH can't set NAT flags (or even use NAT).  Add NAT flags to the ESP set. */
 432 #define ESP_UPDATE_SETTABLE_FLAGS (AH_UPDATE_SETTABLE_FLAGS | IPSA_F_NATT)
 433 
 434 #define AH_ADD_SETTABLE_FLAGS \
 435         (AH_UPDATE_SETTABLE_FLAGS | SADB_X_SAFLAGS_AALG1 | \
 436         SADB_X_SAFLAGS_AALG2 | SADB_X_SAFLAGS_TUNNEL | \
 437         SADB_SAFLAGS_NOREPLAY)
 438 
 439 /* AH can't set NAT flags (or even use NAT).  Add NAT flags to the ESP set. */
 440 #define ESP_ADD_SETTABLE_FLAGS (AH_ADD_SETTABLE_FLAGS | IPSA_F_NATT | \
 441         SADB_X_SAFLAGS_EALG1 | SADB_X_SAFLAGS_EALG2)
 442 
 443 
 444 
 445 /* SA states are important for handling UPDATE PF_KEY messages. */
 446 #define IPSA_STATE_LARVAL               SADB_SASTATE_LARVAL
 447 #define IPSA_STATE_MATURE               SADB_SASTATE_MATURE
 448 #define IPSA_STATE_DYING                SADB_SASTATE_DYING
 449 #define IPSA_STATE_DEAD                 SADB_SASTATE_DEAD
 450 /* Deprecated */
 451 /* #define      IPSA_STATE_IDLE                 SADB_X_SASTATE_IDLE */
 452 /* #define      IPSA_STATE_ACTIVE_ELSEWHERE SADB_X_SASTATE_ACTIVE_ELSEWHERE */
 453 
 454 /*
 455  * NOTE:  If the document authors do things right in defining algorithms, we'll
 456  *        probably have flags for what all is here w.r.t. replay, ESP w/HMAC,
 457  *        etc.
 458  */
 459 
 460 #define IPSA_T_ACQUIRE  SEC_TYPE_NONE   /* If this typed returned, sa needed */
 461 #define IPSA_T_AH       SEC_TYPE_AH     /* IPsec AH association */
 462 #define IPSA_T_ESP      SEC_TYPE_ESP    /* IPsec ESP association */
 463 
 464 #define IPSA_AALG_NONE  SADB_AALG_NONE          /* No auth. algorithm */
 465 #define IPSA_AALG_MD5H  SADB_AALG_MD5HMAC       /* MD5-HMAC algorithm */
 466 #define IPSA_AALG_SHA1H SADB_AALG_SHA1HMAC      /* SHA1-HMAC algorithm */
 467 
 468 #define IPSA_EALG_NONE          SADB_EALG_NONE  /* No encryption algorithm */
 469 #define IPSA_EALG_DES_CBC       SADB_EALG_DESCBC
 470 #define IPSA_EALG_3DES          SADB_EALG_3DESCBC
 471 
 472 /*
 473  * Protect each ipsa_t bucket (and linkage) with a lock.
 474  */
 475 
 476 typedef struct isaf_s {
 477         ipsa_t *isaf_ipsa;
 478         kmutex_t isaf_lock;
 479         uint64_t isaf_gen;
 480 } isaf_t;
 481 
 482 /*
 483  * ACQUIRE record.  If AH/ESP/whatever cannot find an association for outbound
 484  * traffic, it sends up an SADB_ACQUIRE message and create an ACQUIRE record.
 485  */
 486 
 487 #define IPSACQ_MAXPACKETS 4     /* Number of packets that can be queued up */
 488                                 /* waiting for an ACQUIRE to finish. */
 489 
 490 typedef struct ipsacq_s {
 491         struct ipsacq_s *ipsacq_next;
 492         struct ipsacq_s **ipsacq_ptpn;
 493         kmutex_t *ipsacq_linklock;
 494         struct ipsec_policy_s  *ipsacq_policy;
 495         struct ipsec_action_s  *ipsacq_act;
 496 
 497         sa_family_t ipsacq_addrfam;     /* Address family. */
 498         sa_family_t ipsacq_inneraddrfam; /* Inner-packet address family. */
 499         int ipsacq_numpackets;          /* How many packets queued up so far. */
 500         uint32_t ipsacq_seq;            /* PF_KEY sequence number. */
 501         uint64_t ipsacq_unique_id;      /* Unique ID for SAs that need it. */
 502 
 503         kmutex_t ipsacq_lock;   /* Protects non-linkage fields. */
 504         time_t ipsacq_expire;   /* Wall-clock time when this record expires. */
 505         mblk_t *ipsacq_mp;      /* List of datagrams waiting for an SA. */
 506 
 507         /* These two point inside the last mblk inserted. */
 508         uint32_t *ipsacq_srcaddr;
 509         uint32_t *ipsacq_dstaddr;
 510 
 511         /* Cache these instead of point so we can mask off accordingly */
 512         uint32_t ipsacq_innersrc[IPSA_MAX_ADDRLEN];
 513         uint32_t ipsacq_innerdst[IPSA_MAX_ADDRLEN];
 514 
 515         /* These may change per-acquire. */
 516         uint16_t ipsacq_srcport;
 517         uint16_t ipsacq_dstport;
 518         uint8_t ipsacq_proto;
 519         uint8_t ipsacq_inner_proto;
 520         uint8_t ipsacq_innersrcpfx;
 521         uint8_t ipsacq_innerdstpfx;
 522 
 523         /* icmp type and code of triggering packet (if applicable) */
 524         uint8_t ipsacq_icmp_type;
 525         uint8_t ipsacq_icmp_code;
 526 
 527         /* label associated with triggering packet */
 528         ts_label_t      *ipsacq_tsl;
 529 } ipsacq_t;
 530 
 531 /*
 532  * Kernel-generated sequence numbers will be no less than 0x80000000 to
 533  * forestall any cretinous problems with manual keying accidentally updating
 534  * an ACQUIRE entry.
 535  */
 536 #define IACQF_LOWEST_SEQ 0x80000000
 537 
 538 #define SADB_AGE_INTERVAL_DEFAULT 8000
 539 
 540 /*
 541  * ACQUIRE fanout.  Protect each linkage with a lock.
 542  */
 543 
 544 typedef struct iacqf_s {
 545         ipsacq_t *iacqf_ipsacq;
 546         kmutex_t iacqf_lock;
 547 } iacqf_t;
 548 
 549 /*
 550  * A (network protocol, ipsec protocol) specific SADB.
 551  * (i.e., one each for {ah, esp} and {v4, v6}.
 552  *
 553  * Keep outbound assocs in a simple hash table for now.
 554  * One danger point, multiple SAs for a single dest will clog a bucket.
 555  * For the future, consider two-level hashing (2nd hash on IPC?), then probe.
 556  */
 557 
 558 typedef struct sadb_s
 559 {
 560         isaf_t  *sdb_of;
 561         isaf_t  *sdb_if;
 562         iacqf_t *sdb_acq;
 563         int     sdb_hashsize;
 564 } sadb_t;
 565 
 566 /*
 567  * A pair of SADB's (one for v4, one for v6), and related state (including
 568  * acquire callbacks).
 569  */
 570 
 571 typedef struct sadbp_s
 572 {
 573         uint32_t        s_satype;
 574         uint32_t        *s_acquire_timeout;
 575         void            (*s_acqfn)(ipsacq_t *, mblk_t *, netstack_t *);
 576         sadb_t          s_v4;
 577         sadb_t          s_v6;
 578         uint32_t        s_addflags;
 579         uint32_t        s_updateflags;
 580 } sadbp_t;
 581 
 582 /*
 583  * A pair of SA's for a single connection, the structure contains a
 584  * pointer to a SA and the SA its paired with (opposite direction) as well
 585  * as the SA's respective hash buckets.
 586  */
 587 typedef struct ipsap_s
 588 {
 589         boolean_t       in_inbound_table;
 590         isaf_t          *ipsap_bucket;
 591         ipsa_t          *ipsap_sa_ptr;
 592         isaf_t          *ipsap_pbucket;
 593         ipsa_t          *ipsap_psa_ptr;
 594 } ipsap_t;
 595 
 596 typedef struct templist_s
 597 {
 598         ipsa_t          *ipsa;
 599         struct templist_s       *next;
 600 } templist_t;
 601 
 602 /* Pointer to an all-zeroes IPv6 address. */
 603 #define ALL_ZEROES_PTR  ((uint32_t *)&ipv6_all_zeros)
 604 
 605 /*
 606  * Form unique id from ip_xmit_attr_t.
 607  */
 608 #define SA_FORM_UNIQUE_ID(ixa)                                  \
 609         SA_UNIQUE_ID((ixa)->ixa_ipsec_src_port, (ixa)->ixa_ipsec_dst_port, \
 610             (((ixa)->ixa_flags & IXAF_IPSEC_TUNNEL) ?                    \
 611             ((ixa)->ixa_ipsec_inaf == AF_INET6 ? \
 612             IPPROTO_IPV6 : IPPROTO_ENCAP) :                             \
 613             (ixa)->ixa_ipsec_proto),                                 \
 614             (((ixa)->ixa_flags & IXAF_IPSEC_TUNNEL) ? \
 615             (ixa)->ixa_ipsec_proto : 0))
 616 
 617 /*
 618  * This macro is used to generate unique ids (along with the addresses, both
 619  * inner and outer) for outbound datagrams that require unique SAs.
 620  *
 621  * N.B. casts and unsigned shift amounts discourage unwarranted
 622  * sign extension of dstport, proto, and iproto.
 623  *
 624  * Unique ID is 64-bits allocated as follows (pardon my big-endian bias):
 625  *
 626  *   6               4      43      33              11
 627  *   3               7      09      21              65              0
 628  *   +---------------*-------+-------+--------------+---------------+
 629  *   |  MUST-BE-ZERO |<iprot>|<proto>| <src port>   |  <dest port>  |
 630  *   +---------------*-------+-------+--------------+---------------+
 631  *
 632  * If there are inner addresses (tunnel mode) the ports come from the
 633  * inner addresses.  If there are no inner addresses, the ports come from
 634  * the outer addresses (transport mode).  Tunnel mode MUST have <proto>
 635  * set to either IPPROTO_ENCAP or IPPPROTO_IPV6.
 636  */
 637 #define SA_UNIQUE_ID(srcport, dstport, proto, iproto)   \
 638         ((srcport) | ((uint64_t)(dstport) << 16U) | \
 639         ((uint64_t)(proto) << 32U) | ((uint64_t)(iproto) << 40U))
 640 
 641 /*
 642  * SA_UNIQUE_MASK generates a mask value to use when comparing the unique value
 643  * from a packet to an SA.
 644  */
 645 
 646 #define SA_UNIQUE_MASK(srcport, dstport, proto, iproto)         \
 647         SA_UNIQUE_ID((srcport != 0) ? 0xffff : 0,               \
 648                     (dstport != 0) ? 0xffff : 0,                \
 649                     (proto != 0) ? 0xff : 0,                    \
 650                     (iproto != 0) ? 0xff : 0)
 651 
 652 /*
 653  * Decompose unique id back into its original fields.
 654  */
 655 #define SA_IPROTO(ipsa) ((ipsa)->ipsa_unique_id>>40)&0xff
 656 #define SA_PROTO(ipsa) ((ipsa)->ipsa_unique_id>>32)&0xff
 657 #define SA_SRCPORT(ipsa) ((ipsa)->ipsa_unique_id & 0xffff)
 658 #define SA_DSTPORT(ipsa) (((ipsa)->ipsa_unique_id >> 16) & 0xffff)
 659 
 660 typedef struct ipsa_query_s ipsa_query_t;
 661 
 662 typedef boolean_t (*ipsa_match_fn_t)(ipsa_query_t *, ipsa_t *);
 663 
 664 #define IPSA_NMATCH     10
 665 
 666 /*
 667  * SADB query structure.
 668  *
 669  * Provide a generalized mechanism for matching entries in the SADB;
 670  * one of these structures is initialized using sadb_form_query(),
 671  * and then can be used as a parameter to sadb_match_query() which returns
 672  * B_TRUE if the SA matches the query.
 673  *
 674  * Under the covers, sadb_form_query populates the matchers[] array with
 675  * functions which are called one at a time until one fails to match.
 676  */
 677 struct ipsa_query_s {
 678         uint32_t req, match;
 679         sadb_address_t *srcext, *dstext;
 680         sadb_ident_t *srcid, *dstid;
 681         sadb_x_kmc_t *kmcext;
 682         sadb_sa_t *assoc;
 683         uint32_t spi;
 684         struct sockaddr_in *src;
 685         struct sockaddr_in6 *src6;
 686         struct sockaddr_in *dst;
 687         struct sockaddr_in6 *dst6;
 688         sa_family_t af;
 689         uint32_t *srcaddr, *dstaddr;
 690         uint32_t ifindex;
 691         uint32_t kmc, kmp;
 692         char *didstr, *sidstr;
 693         uint16_t didtype, sidtype;
 694         sadbp_t *spp;
 695         sadb_t *sp;
 696         isaf_t  *inbound, *outbound;
 697         uint32_t outhash;
 698         uint32_t inhash;
 699         ipsa_match_fn_t matchers[IPSA_NMATCH];
 700 };
 701 
 702 #define IPSA_Q_SA               0x00000001
 703 #define IPSA_Q_DST              0x00000002
 704 #define IPSA_Q_SRC              0x00000004
 705 #define IPSA_Q_DSTID            0x00000008
 706 #define IPSA_Q_SRCID            0x00000010
 707 #define IPSA_Q_KMC              0x00000020
 708 #define IPSA_Q_INBOUND          0x00000040 /* fill in inbound isaf_t */
 709 #define IPSA_Q_OUTBOUND         0x00000080 /* fill in outbound isaf_t */
 710 
 711 int sadb_form_query(keysock_in_t *, uint32_t, uint32_t, ipsa_query_t *, int *);
 712 boolean_t sadb_match_query(ipsa_query_t *q, ipsa_t *sa);
 713 
 714 
 715 /*
 716  * All functions that return an ipsa_t will return it with IPSA_REFHOLD()
 717  * already called.
 718  */
 719 
 720 /* SA retrieval (inbound and outbound) */
 721 ipsa_t *ipsec_getassocbyspi(isaf_t *, uint32_t, uint32_t *, uint32_t *,
 722     sa_family_t);
 723 ipsa_t *ipsec_getassocbyconn(isaf_t *, ip_xmit_attr_t *, uint32_t *, uint32_t *,
 724     sa_family_t, uint8_t, ts_label_t *);
 725 
 726 /* SA insertion. */
 727 int sadb_insertassoc(ipsa_t *, isaf_t *);
 728 
 729 /* SA table construction and destruction. */
 730 void sadbp_init(const char *name, sadbp_t *, int, int, netstack_t *);
 731 void sadbp_flush(sadbp_t *, netstack_t *);
 732 void sadbp_destroy(sadbp_t *, netstack_t *);
 733 
 734 /* SA insertion and deletion. */
 735 int sadb_insertassoc(ipsa_t *, isaf_t *);
 736 void sadb_unlinkassoc(ipsa_t *);
 737 
 738 /* Support routines to interface a keysock consumer to PF_KEY. */
 739 mblk_t *sadb_keysock_out(minor_t);
 740 int sadb_hardsoftchk(sadb_lifetime_t *, sadb_lifetime_t *, sadb_lifetime_t *);
 741 int sadb_labelchk(struct keysock_in_s *);
 742 void sadb_pfkey_echo(queue_t *, mblk_t *, sadb_msg_t *, struct keysock_in_s *,
 743     ipsa_t *);
 744 void sadb_pfkey_error(queue_t *, mblk_t *, int, int, uint_t);
 745 void sadb_keysock_hello(queue_t **, queue_t *, mblk_t *, void (*)(void *),
 746     void *, timeout_id_t *, int);
 747 int sadb_addrcheck(queue_t *, mblk_t *, sadb_ext_t *, uint_t, netstack_t *);
 748 boolean_t sadb_addrfix(keysock_in_t *, queue_t *, mblk_t *, netstack_t *);
 749 int sadb_addrset(ire_t *);
 750 int sadb_delget_sa(mblk_t *, keysock_in_t *, sadbp_t *, int *, queue_t *,
 751     uint8_t);
 752 
 753 int sadb_purge_sa(mblk_t *, keysock_in_t *, sadb_t *, int *, queue_t *);
 754 int sadb_common_add(queue_t *, mblk_t *, sadb_msg_t *,
 755     keysock_in_t *, isaf_t *, isaf_t *, ipsa_t *, boolean_t, boolean_t, int *,
 756     netstack_t *, sadbp_t *);
 757 void sadb_set_usetime(ipsa_t *);
 758 boolean_t sadb_age_bytes(queue_t *, ipsa_t *, uint64_t, boolean_t);
 759 int sadb_update_sa(mblk_t *, keysock_in_t *, sadbp_t *,
 760     int *, queue_t *, int (*)(mblk_t *, keysock_in_t *, int *, netstack_t *),
 761     netstack_t *, uint8_t);
 762 void sadb_acquire(mblk_t *, ip_xmit_attr_t *, boolean_t, boolean_t);
 763 void gcm_params_init(ipsa_t *, uchar_t *, uint_t, uchar_t *, ipsa_cm_mech_t *,
 764     crypto_data_t *);
 765 void ccm_params_init(ipsa_t *, uchar_t *, uint_t, uchar_t *, ipsa_cm_mech_t *,
 766     crypto_data_t *);
 767 void cbc_params_init(ipsa_t *, uchar_t *, uint_t, uchar_t *, ipsa_cm_mech_t *,
 768     crypto_data_t *);
 769 
 770 void sadb_destroy_acquire(ipsacq_t *, netstack_t *);
 771 struct ipsec_stack;
 772 mblk_t *sadb_setup_acquire(ipsacq_t *, uint8_t, struct ipsec_stack *);
 773 ipsa_t *sadb_getspi(keysock_in_t *, uint32_t, int *, netstack_t *);
 774 void sadb_in_acquire(sadb_msg_t *, sadbp_t *, queue_t *, netstack_t *);
 775 boolean_t sadb_replay_check(ipsa_t *, uint32_t);
 776 boolean_t sadb_replay_peek(ipsa_t *, uint32_t);
 777 int sadb_dump(queue_t *, mblk_t *, keysock_in_t *, sadb_t *);
 778 void sadb_replay_delete(ipsa_t *);
 779 void sadb_ager(sadb_t *, queue_t *, int, netstack_t *);
 780 
 781 timeout_id_t sadb_retimeout(hrtime_t, queue_t *, void (*)(void *), void *,
 782     uint_t *, uint_t, short);
 783 void sadb_sa_refrele(void *target);
 784 mblk_t *sadb_set_lpkt(ipsa_t *, mblk_t *, ip_recv_attr_t *);
 785 mblk_t *sadb_clear_lpkt(ipsa_t *);
 786 
 787 /*
 788  * Two IPsec rate-limiting routines.
 789  */
 790 /*PRINTFLIKE6*/
 791 extern void ipsec_rl_strlog(netstack_t *, short, short, char,
 792     ushort_t, char *, ...)
 793     __KPRINTFLIKE(6);
 794 extern void ipsec_assocfailure(short, short, char, ushort_t, char *, uint32_t,
 795     void *, int, netstack_t *);
 796 
 797 /*
 798  * Algorithm types.
 799  */
 800 
 801 #define IPSEC_NALGTYPES         2
 802 
 803 typedef enum ipsec_algtype {
 804         IPSEC_ALG_AUTH = 0,
 805         IPSEC_ALG_ENCR = 1,
 806         IPSEC_ALG_ALL = 2
 807 } ipsec_algtype_t;
 808 
 809 /*
 810  * Definitions as per IPsec/ISAKMP DOI.
 811  */
 812 
 813 #define IPSEC_MAX_ALGS          256
 814 #define PROTO_IPSEC_AH          2
 815 #define PROTO_IPSEC_ESP         3
 816 
 817 /*
 818  * Common algorithm info.
 819  */
 820 typedef struct ipsec_alginfo
 821 {
 822         uint8_t         alg_id;
 823         uint8_t         alg_flags;
 824         uint16_t        *alg_key_sizes;
 825         uint16_t        *alg_block_sizes;
 826         uint16_t        *alg_params;
 827         uint16_t        alg_nkey_sizes;
 828         uint16_t        alg_ivlen;
 829         uint16_t        alg_icvlen;
 830         uint8_t         alg_saltlen;
 831         uint16_t        alg_nblock_sizes;
 832         uint16_t        alg_nparams;
 833         uint16_t        alg_minbits;
 834         uint16_t        alg_maxbits;
 835         uint16_t        alg_datalen;
 836         /*
 837          * increment: number of bits from keysize to keysize
 838          * default: # of increments from min to default key len
 839          */
 840         uint16_t        alg_increment;
 841         uint16_t        alg_default;
 842         uint16_t        alg_default_bits;
 843         /*
 844          * Min, max, and default key sizes effectively supported
 845          * by the encryption framework.
 846          */
 847         uint16_t        alg_ef_minbits;
 848         uint16_t        alg_ef_maxbits;
 849         uint16_t        alg_ef_default;
 850         uint16_t        alg_ef_default_bits;
 851 
 852         crypto_mech_type_t alg_mech_type;       /* KCF mechanism type */
 853         crypto_mech_name_t alg_mech_name;       /* KCF mechanism name */
 854 } ipsec_alginfo_t;
 855 
 856 #define alg_datalen alg_block_sizes[0]
 857 #define ALG_VALID(_alg) ((_alg)->alg_flags & ALG_FLAG_VALID)
 858 
 859 /*
 860  * Software crypto execution mode.
 861  */
 862 typedef enum {
 863         IPSEC_ALGS_EXEC_SYNC = 0,
 864         IPSEC_ALGS_EXEC_ASYNC = 1
 865 } ipsec_algs_exec_mode_t;
 866 
 867 extern void ipsec_alg_reg(ipsec_algtype_t, ipsec_alginfo_t *, netstack_t *);
 868 extern void ipsec_alg_unreg(ipsec_algtype_t, uint8_t, netstack_t *);
 869 extern void ipsec_alg_fix_min_max(ipsec_alginfo_t *, ipsec_algtype_t,
 870     netstack_t *ns);
 871 extern void alg_flag_check(ipsec_alginfo_t *);
 872 extern void ipsec_alg_free(ipsec_alginfo_t *);
 873 extern void ipsec_register_prov_update(void);
 874 extern void sadb_alg_update(ipsec_algtype_t, uint8_t, boolean_t, netstack_t *);
 875 
 876 extern int sadb_sens_len_from_label(ts_label_t *);
 877 extern void sadb_sens_from_label(sadb_sens_t *, int, ts_label_t *, int);
 878 
 879 /*
 880  * Context templates management.
 881  */
 882 
 883 #define IPSEC_CTX_TMPL_ALLOC ((crypto_ctx_template_t)-1)
 884 #define IPSEC_CTX_TMPL(_sa, _which, _type, _tmpl) {                     \
 885         if ((_tmpl = (_sa)->_which) == IPSEC_CTX_TMPL_ALLOC) {               \
 886                 mutex_enter(&assoc->ipsa_lock);                          \
 887                 if ((_sa)->_which == IPSEC_CTX_TMPL_ALLOC) {         \
 888                         ipsec_stack_t *ipss;                            \
 889                                                                         \
 890                         ipss = assoc->ipsa_netstack->netstack_ipsec;      \
 891                         mutex_enter(&ipss->ipsec_alg_lock);              \
 892                         (void) ipsec_create_ctx_tmpl(_sa, _type);       \
 893                         mutex_exit(&ipss->ipsec_alg_lock);               \
 894                 }                                                       \
 895                 mutex_exit(&assoc->ipsa_lock);                           \
 896                 if ((_tmpl = (_sa)->_which) == IPSEC_CTX_TMPL_ALLOC) \
 897                         _tmpl = NULL;                                   \
 898         }                                                               \
 899 }
 900 
 901 extern int ipsec_create_ctx_tmpl(ipsa_t *, ipsec_algtype_t);
 902 extern void ipsec_destroy_ctx_tmpl(ipsa_t *, ipsec_algtype_t);
 903 
 904 /* key checking */
 905 extern int ipsec_check_key(crypto_mech_type_t, sadb_key_t *, boolean_t, int *);
 906 
 907 typedef struct ipsec_kstats_s {
 908         kstat_named_t esp_stat_in_requests;
 909         kstat_named_t esp_stat_in_discards;
 910         kstat_named_t esp_stat_lookup_failure;
 911         kstat_named_t ah_stat_in_requests;
 912         kstat_named_t ah_stat_in_discards;
 913         kstat_named_t ah_stat_lookup_failure;
 914         kstat_named_t sadb_acquire_maxpackets;
 915         kstat_named_t sadb_acquire_qhiwater;
 916 } ipsec_kstats_t;
 917 
 918 /*
 919  * (ipss)->ipsec_kstats is equal to (ipss)->ipsec_ksp->ks_data if
 920  * kstat_create_netstack for (ipss)->ipsec_ksp succeeds, but when it
 921  * fails, it will be NULL. Note this is done for all stack instances,
 922  * so it *could* fail. hence a non-NULL checking is done for
 923  * IP_ESP_BUMP_STAT, IP_AH_BUMP_STAT and IP_ACQUIRE_STAT
 924  */
 925 #define IP_ESP_BUMP_STAT(ipss, x)                                       \
 926 do {                                                                    \
 927         if ((ipss)->ipsec_kstats != NULL)                            \
 928                 ((ipss)->ipsec_kstats->esp_stat_ ## x).value.ui64++;      \
 929 _NOTE(CONSTCOND)                                                        \
 930 } while (0)
 931 
 932 #define IP_AH_BUMP_STAT(ipss, x)                                        \
 933 do {                                                                    \
 934         if ((ipss)->ipsec_kstats != NULL)                            \
 935                 ((ipss)->ipsec_kstats->ah_stat_ ## x).value.ui64++;       \
 936 _NOTE(CONSTCOND)                                                        \
 937 } while (0)
 938 
 939 #define IP_ACQUIRE_STAT(ipss, val, new)                                 \
 940 do {                                                                    \
 941         if ((ipss)->ipsec_kstats != NULL &&                          \
 942             ((uint64_t)(new)) >                                              \
 943             ((ipss)->ipsec_kstats->sadb_acquire_ ## val).value.ui64)      \
 944                 ((ipss)->ipsec_kstats->sadb_acquire_ ## val).value.ui64 = \
 945                         ((uint64_t)(new));                              \
 946 _NOTE(CONSTCOND)                                                        \
 947 } while (0)
 948 
 949 
 950 #ifdef  __cplusplus
 951 }
 952 #endif
 953 
 954 #endif /* _INET_SADB_H */