The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netipsec/xform_ah.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD: releng/8.4/sys/netipsec/xform_ah.c 221525 2011-05-06 13:24:10Z fabient $      */
    2 /*      $OpenBSD: ip_ah.c,v 1.63 2001/06/26 06:18:58 angelos Exp $ */
    3 /*-
    4  * The authors of this code are John Ioannidis (ji@tla.org),
    5  * Angelos D. Keromytis (kermit@csd.uch.gr) and
    6  * Niels Provos (provos@physnet.uni-hamburg.de).
    7  *
    8  * The original version of this code was written by John Ioannidis
    9  * for BSD/OS in Athens, Greece, in November 1995.
   10  *
   11  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
   12  * by Angelos D. Keromytis.
   13  *
   14  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
   15  * and Niels Provos.
   16  *
   17  * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist.
   18  *
   19  * Copyright (c) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
   20  * Angelos D. Keromytis and Niels Provos.
   21  * Copyright (c) 1999 Niklas Hallqvist.
   22  * Copyright (c) 2001 Angelos D. Keromytis.
   23  *
   24  * Permission to use, copy, and modify this software with or without fee
   25  * is hereby granted, provided that this entire notice is included in
   26  * all copies of any software which is or includes a copy or
   27  * modification of this software.
   28  * You may use this code under the GNU public license if you so wish. Please
   29  * contribute changes back to the authors under this freer than GPL license
   30  * so that we may further the use of strong encryption without limitations to
   31  * all.
   32  *
   33  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
   34  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
   35  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
   36  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
   37  * PURPOSE.
   38  */
   39 #include "opt_inet.h"
   40 #include "opt_inet6.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/mbuf.h>
   45 #include <sys/socket.h>
   46 #include <sys/syslog.h>
   47 #include <sys/kernel.h>
   48 #include <sys/sysctl.h>
   49 
   50 #include <net/if.h>
   51 #include <net/vnet.h>
   52 
   53 #include <netinet/in.h>
   54 #include <netinet/in_systm.h>
   55 #include <netinet/ip.h>
   56 #include <netinet/ip_ecn.h>
   57 #include <netinet/ip6.h>
   58 
   59 #include <net/route.h>
   60 #include <netipsec/ipsec.h>
   61 #include <netipsec/ah.h>
   62 #include <netipsec/ah_var.h>
   63 #include <netipsec/xform.h>
   64 
   65 #ifdef INET6
   66 #include <netinet6/ip6_var.h>
   67 #include <netipsec/ipsec6.h>
   68 #include <netinet6/ip6_ecn.h>
   69 #endif
   70 
   71 #include <netipsec/key.h>
   72 #include <netipsec/key_debug.h>
   73 
   74 #include <opencrypto/cryptodev.h>
   75 
   76 /*
   77  * Return header size in bytes.  The old protocol did not support
   78  * the replay counter; the new protocol always includes the counter.
   79  */
   80 #define HDRSIZE(sav) \
   81         (((sav)->flags & SADB_X_EXT_OLD) ? \
   82                 sizeof (struct ah) : sizeof (struct ah) + sizeof (u_int32_t))
   83 /* 
   84  * Return authenticator size in bytes.  The old protocol is known
   85  * to use a fixed 16-byte authenticator.  The new algorithm use 12-byte
   86  * authenticator.
   87  */
   88 #define AUTHSIZE(sav)   ah_authsize(sav)
   89 
   90 VNET_DEFINE(int, ah_enable) = 1;        /* control flow of packets with AH */
   91 VNET_DEFINE(int, ah_cleartos) = 1;      /* clear ip_tos when doing AH calc */
   92 VNET_DEFINE(struct ahstat, ahstat);
   93 
   94 SYSCTL_DECL(_net_inet_ah);
   95 SYSCTL_VNET_INT(_net_inet_ah, OID_AUTO,
   96         ah_enable,      CTLFLAG_RW,     &VNET_NAME(ah_enable),  0, "");
   97 SYSCTL_VNET_INT(_net_inet_ah, OID_AUTO,
   98         ah_cleartos,    CTLFLAG_RW,     &VNET_NAME(ah_cleartos), 0, "");
   99 SYSCTL_VNET_STRUCT(_net_inet_ah, IPSECCTL_STATS,
  100         stats,          CTLFLAG_RD,     &VNET_NAME(ahstat), ahstat, "");
  101 
  102 static unsigned char ipseczeroes[256];  /* larger than an ip6 extension hdr */
  103 
  104 static int ah_input_cb(struct cryptop*);
  105 static int ah_output_cb(struct cryptop*);
  106 
  107 static int
  108 ah_authsize(struct secasvar *sav)
  109 {
  110 
  111         IPSEC_ASSERT(sav != NULL, ("%s: sav == NULL", __func__));
  112 
  113         if (sav->flags & SADB_X_EXT_OLD)
  114                 return 16;
  115 
  116         switch (sav->alg_auth) {
  117         case SADB_X_AALG_SHA2_256:
  118                 return 16;
  119         case SADB_X_AALG_SHA2_384:
  120                 return 24;
  121         case SADB_X_AALG_SHA2_512:
  122                 return 32;
  123         default:
  124                 return AH_HMAC_HASHLEN;
  125         }
  126         /* NOTREACHED */
  127 }
  128 /*
  129  * NB: this is public for use by the PF_KEY support.
  130  */
  131 struct auth_hash *
  132 ah_algorithm_lookup(int alg)
  133 {
  134         if (alg > SADB_AALG_MAX)
  135                 return NULL;
  136         switch (alg) {
  137         case SADB_X_AALG_NULL:
  138                 return &auth_hash_null;
  139         case SADB_AALG_MD5HMAC:
  140                 return &auth_hash_hmac_md5;
  141         case SADB_AALG_SHA1HMAC:
  142                 return &auth_hash_hmac_sha1;
  143         case SADB_X_AALG_RIPEMD160HMAC:
  144                 return &auth_hash_hmac_ripemd_160;
  145         case SADB_X_AALG_MD5:
  146                 return &auth_hash_key_md5;
  147         case SADB_X_AALG_SHA:
  148                 return &auth_hash_key_sha1;
  149         case SADB_X_AALG_SHA2_256:
  150                 return &auth_hash_hmac_sha2_256;
  151         case SADB_X_AALG_SHA2_384:
  152                 return &auth_hash_hmac_sha2_384;
  153         case SADB_X_AALG_SHA2_512:
  154                 return &auth_hash_hmac_sha2_512;
  155         }
  156         return NULL;
  157 }
  158 
  159 size_t
  160 ah_hdrsiz(struct secasvar *sav)
  161 {
  162         size_t size;
  163 
  164         if (sav != NULL) {
  165                 int authsize;
  166                 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, ("null xform"));
  167                 /*XXX not right for null algorithm--does it matter??*/
  168                 authsize = AUTHSIZE(sav);
  169                 size = roundup(authsize, sizeof (u_int32_t)) + HDRSIZE(sav);
  170         } else {
  171                 /* default guess */
  172                 size = sizeof (struct ah) + sizeof (u_int32_t) + 16;
  173         }
  174         return size;
  175 }
  176 
  177 /*
  178  * NB: public for use by esp_init.
  179  */
  180 int
  181 ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria)
  182 {
  183         struct auth_hash *thash;
  184         int keylen;
  185 
  186         thash = ah_algorithm_lookup(sav->alg_auth);
  187         if (thash == NULL) {
  188                 DPRINTF(("%s: unsupported authentication algorithm %u\n",
  189                         __func__, sav->alg_auth));
  190                 return EINVAL;
  191         }
  192         /*
  193          * Verify the replay state block allocation is consistent with
  194          * the protocol type.  We check here so we can make assumptions
  195          * later during protocol processing.
  196          */
  197         /* NB: replay state is setup elsewhere (sigh) */
  198         if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) {
  199                 DPRINTF(("%s: replay state block inconsistency, "
  200                         "%s algorithm %s replay state\n", __func__,
  201                         (sav->flags & SADB_X_EXT_OLD) ? "old" : "new",
  202                         sav->replay == NULL ? "without" : "with"));
  203                 return EINVAL;
  204         }
  205         if (sav->key_auth == NULL) {
  206                 DPRINTF(("%s: no authentication key for %s algorithm\n",
  207                         __func__, thash->name));
  208                 return EINVAL;
  209         }
  210         keylen = _KEYLEN(sav->key_auth);
  211         if (keylen != thash->keysize && thash->keysize != 0) {
  212                 DPRINTF(("%s: invalid keylength %d, algorithm %s requires "
  213                         "keysize %d\n", __func__,
  214                          keylen, thash->name, thash->keysize));
  215                 return EINVAL;
  216         }
  217 
  218         sav->tdb_xform = xsp;
  219         sav->tdb_authalgxform = thash;
  220 
  221         /* Initialize crypto session. */
  222         bzero(cria, sizeof (*cria));
  223         cria->cri_alg = sav->tdb_authalgxform->type;
  224         cria->cri_klen = _KEYBITS(sav->key_auth);
  225         cria->cri_key = sav->key_auth->key_data;
  226         cria->cri_mlen = AUTHSIZE(sav);
  227 
  228         return 0;
  229 }
  230 
  231 /*
  232  * ah_init() is called when an SPI is being set up.
  233  */
  234 static int
  235 ah_init(struct secasvar *sav, struct xformsw *xsp)
  236 {
  237         struct cryptoini cria;
  238         int error;
  239 
  240         error = ah_init0(sav, xsp, &cria);
  241         return error ? error :
  242                  crypto_newsession(&sav->tdb_cryptoid, &cria, V_crypto_support);
  243 }
  244 
  245 /*
  246  * Paranoia.
  247  *
  248  * NB: public for use by esp_zeroize (XXX).
  249  */
  250 int
  251 ah_zeroize(struct secasvar *sav)
  252 {
  253         int err;
  254 
  255         if (sav->key_auth)
  256                 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
  257 
  258         err = crypto_freesession(sav->tdb_cryptoid);
  259         sav->tdb_cryptoid = 0;
  260         sav->tdb_authalgxform = NULL;
  261         sav->tdb_xform = NULL;
  262         return err;
  263 }
  264 
  265 /*
  266  * Massage IPv4/IPv6 headers for AH processing.
  267  */
  268 static int
  269 ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out)
  270 {
  271         struct mbuf *m = *m0;
  272         unsigned char *ptr;
  273         int off, count;
  274 
  275 #ifdef INET
  276         struct ip *ip;
  277 #endif /* INET */
  278 
  279 #ifdef INET6
  280         struct ip6_ext *ip6e;
  281         struct ip6_hdr ip6;
  282         int alloc, len, ad;
  283 #endif /* INET6 */
  284 
  285         switch (proto) {
  286 #ifdef INET
  287         case AF_INET:
  288                 /*
  289                  * This is the least painful way of dealing with IPv4 header
  290                  * and option processing -- just make sure they're in
  291                  * contiguous memory.
  292                  */
  293                 *m0 = m = m_pullup(m, skip);
  294                 if (m == NULL) {
  295                         DPRINTF(("%s: m_pullup failed\n", __func__));
  296                         return ENOBUFS;
  297                 }
  298 
  299                 /* Fix the IP header */
  300                 ip = mtod(m, struct ip *);
  301                 if (V_ah_cleartos)
  302                         ip->ip_tos = 0;
  303                 ip->ip_ttl = 0;
  304                 ip->ip_sum = 0;
  305 
  306                 /*
  307                  * On input, fix ip_len which has been byte-swapped
  308                  * at ip_input().
  309                  */
  310                 if (!out) {
  311                         ip->ip_len = htons(ip->ip_len + skip);
  312 
  313                         if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
  314                                 ip->ip_off = htons(ip->ip_off & IP_DF);
  315                         else
  316                                 ip->ip_off = 0;
  317                 } else {
  318                         if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
  319                                 ip->ip_off = htons(ntohs(ip->ip_off) & IP_DF);
  320                         else
  321                                 ip->ip_off = 0;
  322                 }
  323 
  324                 ptr = mtod(m, unsigned char *) + sizeof(struct ip);
  325 
  326                 /* IPv4 option processing */
  327                 for (off = sizeof(struct ip); off < skip;) {
  328                         if (ptr[off] == IPOPT_EOL || ptr[off] == IPOPT_NOP ||
  329                             off + 1 < skip)
  330                                 ;
  331                         else {
  332                                 DPRINTF(("%s: illegal IPv4 option length for "
  333                                         "option %d\n", __func__, ptr[off]));
  334 
  335                                 m_freem(m);
  336                                 return EINVAL;
  337                         }
  338 
  339                         switch (ptr[off]) {
  340                         case IPOPT_EOL:
  341                                 off = skip;  /* End the loop. */
  342                                 break;
  343 
  344                         case IPOPT_NOP:
  345                                 off++;
  346                                 break;
  347 
  348                         case IPOPT_SECURITY:    /* 0x82 */
  349                         case 0x85:      /* Extended security. */
  350                         case 0x86:      /* Commercial security. */
  351                         case 0x94:      /* Router alert */
  352                         case 0x95:      /* RFC1770 */
  353                                 /* Sanity check for option length. */
  354                                 if (ptr[off + 1] < 2) {
  355                                         DPRINTF(("%s: illegal IPv4 option "
  356                                                 "length for option %d\n",
  357                                                 __func__, ptr[off]));
  358 
  359                                         m_freem(m);
  360                                         return EINVAL;
  361                                 }
  362 
  363                                 off += ptr[off + 1];
  364                                 break;
  365 
  366                         case IPOPT_LSRR:
  367                         case IPOPT_SSRR:
  368                                 /* Sanity check for option length. */
  369                                 if (ptr[off + 1] < 2) {
  370                                         DPRINTF(("%s: illegal IPv4 option "
  371                                                 "length for option %d\n",
  372                                                 __func__, ptr[off]));
  373 
  374                                         m_freem(m);
  375                                         return EINVAL;
  376                                 }
  377 
  378                                 /*
  379                                  * On output, if we have either of the
  380                                  * source routing options, we should
  381                                  * swap the destination address of the
  382                                  * IP header with the last address
  383                                  * specified in the option, as that is
  384                                  * what the destination's IP header
  385                                  * will look like.
  386                                  */
  387                                 if (out)
  388                                         bcopy(ptr + off + ptr[off + 1] -
  389                                             sizeof(struct in_addr),
  390                                             &(ip->ip_dst), sizeof(struct in_addr));
  391 
  392                                 /* Fall through */
  393                         default:
  394                                 /* Sanity check for option length. */
  395                                 if (ptr[off + 1] < 2) {
  396                                         DPRINTF(("%s: illegal IPv4 option "
  397                                                 "length for option %d\n",
  398                                                 __func__, ptr[off]));
  399                                         m_freem(m);
  400                                         return EINVAL;
  401                                 }
  402 
  403                                 /* Zeroize all other options. */
  404                                 count = ptr[off + 1];
  405                                 bcopy(ipseczeroes, ptr, count);
  406                                 off += count;
  407                                 break;
  408                         }
  409 
  410                         /* Sanity check. */
  411                         if (off > skip) {
  412                                 DPRINTF(("%s: malformed IPv4 options header\n",
  413                                         __func__));
  414 
  415                                 m_freem(m);
  416                                 return EINVAL;
  417                         }
  418                 }
  419 
  420                 break;
  421 #endif /* INET */
  422 
  423 #ifdef INET6
  424         case AF_INET6:  /* Ugly... */
  425                 /* Copy and "cook" the IPv6 header. */
  426                 m_copydata(m, 0, sizeof(ip6), (caddr_t) &ip6);
  427 
  428                 /* We don't do IPv6 Jumbograms. */
  429                 if (ip6.ip6_plen == 0) {
  430                         DPRINTF(("%s: unsupported IPv6 jumbogram\n", __func__));
  431                         m_freem(m);
  432                         return EMSGSIZE;
  433                 }
  434 
  435                 ip6.ip6_flow = 0;
  436                 ip6.ip6_hlim = 0;
  437                 ip6.ip6_vfc &= ~IPV6_VERSION_MASK;
  438                 ip6.ip6_vfc |= IPV6_VERSION;
  439 
  440                 /* Scoped address handling. */
  441                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src))
  442                         ip6.ip6_src.s6_addr16[1] = 0;
  443                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst))
  444                         ip6.ip6_dst.s6_addr16[1] = 0;
  445 
  446                 /* Done with IPv6 header. */
  447                 m_copyback(m, 0, sizeof(struct ip6_hdr), (caddr_t) &ip6);
  448 
  449                 /* Let's deal with the remaining headers (if any). */
  450                 if (skip - sizeof(struct ip6_hdr) > 0) {
  451                         if (m->m_len <= skip) {
  452                                 ptr = (unsigned char *) malloc(
  453                                     skip - sizeof(struct ip6_hdr),
  454                                     M_XDATA, M_NOWAIT);
  455                                 if (ptr == NULL) {
  456                                         DPRINTF(("%s: failed to allocate memory"
  457                                                 "for IPv6 headers\n",__func__));
  458                                         m_freem(m);
  459                                         return ENOBUFS;
  460                                 }
  461 
  462                                 /*
  463                                  * Copy all the protocol headers after
  464                                  * the IPv6 header.
  465                                  */
  466                                 m_copydata(m, sizeof(struct ip6_hdr),
  467                                     skip - sizeof(struct ip6_hdr), ptr);
  468                                 alloc = 1;
  469                         } else {
  470                                 /* No need to allocate memory. */
  471                                 ptr = mtod(m, unsigned char *) +
  472                                     sizeof(struct ip6_hdr);
  473                                 alloc = 0;
  474                         }
  475                 } else
  476                         break;
  477 
  478                 off = ip6.ip6_nxt & 0xff; /* Next header type. */
  479 
  480                 for (len = 0; len < skip - sizeof(struct ip6_hdr);)
  481                         switch (off) {
  482                         case IPPROTO_HOPOPTS:
  483                         case IPPROTO_DSTOPTS:
  484                                 ip6e = (struct ip6_ext *) (ptr + len);
  485 
  486                                 /*
  487                                  * Process the mutable/immutable
  488                                  * options -- borrows heavily from the
  489                                  * KAME code.
  490                                  */
  491                                 for (count = len + sizeof(struct ip6_ext);
  492                                      count < len + ((ip6e->ip6e_len + 1) << 3);) {
  493                                         if (ptr[count] == IP6OPT_PAD1) {
  494                                                 count++;
  495                                                 continue; /* Skip padding. */
  496                                         }
  497 
  498                                         /* Sanity check. */
  499                                         if (count > len +
  500                                             ((ip6e->ip6e_len + 1) << 3)) {
  501                                                 m_freem(m);
  502 
  503                                                 /* Free, if we allocated. */
  504                                                 if (alloc)
  505                                                         free(ptr, M_XDATA);
  506                                                 return EINVAL;
  507                                         }
  508 
  509                                         ad = ptr[count + 1];
  510 
  511                                         /* If mutable option, zeroize. */
  512                                         if (ptr[count] & IP6OPT_MUTABLE)
  513                                                 bcopy(ipseczeroes, ptr + count,
  514                                                     ptr[count + 1]);
  515 
  516                                         count += ad;
  517 
  518                                         /* Sanity check. */
  519                                         if (count >
  520                                             skip - sizeof(struct ip6_hdr)) {
  521                                                 m_freem(m);
  522 
  523                                                 /* Free, if we allocated. */
  524                                                 if (alloc)
  525                                                         free(ptr, M_XDATA);
  526                                                 return EINVAL;
  527                                         }
  528                                 }
  529 
  530                                 /* Advance. */
  531                                 len += ((ip6e->ip6e_len + 1) << 3);
  532                                 off = ip6e->ip6e_nxt;
  533                                 break;
  534 
  535                         case IPPROTO_ROUTING:
  536                                 /*
  537                                  * Always include routing headers in
  538                                  * computation.
  539                                  */
  540                                 ip6e = (struct ip6_ext *) (ptr + len);
  541                                 len += ((ip6e->ip6e_len + 1) << 3);
  542                                 off = ip6e->ip6e_nxt;
  543                                 break;
  544 
  545                         default:
  546                                 DPRINTF(("%s: unexpected IPv6 header type %d",
  547                                         __func__, off));
  548                                 if (alloc)
  549                                         free(ptr, M_XDATA);
  550                                 m_freem(m);
  551                                 return EINVAL;
  552                         }
  553 
  554                 /* Copyback and free, if we allocated. */
  555                 if (alloc) {
  556                         m_copyback(m, sizeof(struct ip6_hdr),
  557                             skip - sizeof(struct ip6_hdr), ptr);
  558                         free(ptr, M_XDATA);
  559                 }
  560 
  561                 break;
  562 #endif /* INET6 */
  563         }
  564 
  565         return 0;
  566 }
  567 
  568 /*
  569  * ah_input() gets called to verify that an input packet
  570  * passes authentication.
  571  */
  572 static int
  573 ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
  574 {
  575         struct auth_hash *ahx;
  576         struct tdb_ident *tdbi;
  577         struct tdb_crypto *tc;
  578         struct m_tag *mtag;
  579         struct newah *ah;
  580         int hl, rplen, authsize;
  581 
  582         struct cryptodesc *crda;
  583         struct cryptop *crp;
  584 
  585         IPSEC_ASSERT(sav != NULL, ("null SA"));
  586         IPSEC_ASSERT(sav->key_auth != NULL, ("null authentication key"));
  587         IPSEC_ASSERT(sav->tdb_authalgxform != NULL,
  588                 ("null authentication xform"));
  589 
  590         /* Figure out header size. */
  591         rplen = HDRSIZE(sav);
  592 
  593         /* XXX don't pullup, just copy header */
  594         IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen);
  595         if (ah == NULL) {
  596                 DPRINTF(("ah_input: cannot pullup header\n"));
  597                 V_ahstat.ahs_hdrops++;          /*XXX*/
  598                 m_freem(m);
  599                 return ENOBUFS;
  600         }
  601 
  602         /* Check replay window, if applicable. */
  603         if (sav->replay && !ipsec_chkreplay(ntohl(ah->ah_seq), sav)) {
  604                 V_ahstat.ahs_replay++;
  605                 DPRINTF(("%s: packet replay failure: %s\n", __func__,
  606                           ipsec_logsastr(sav)));
  607                 m_freem(m);
  608                 return ENOBUFS;
  609         }
  610 
  611         /* Verify AH header length. */
  612         hl = ah->ah_len * sizeof (u_int32_t);
  613         ahx = sav->tdb_authalgxform;
  614         authsize = AUTHSIZE(sav);
  615         if (hl != authsize + rplen - sizeof (struct ah)) {
  616                 DPRINTF(("%s: bad authenticator length %u (expecting %lu)"
  617                         " for packet in SA %s/%08lx\n", __func__,
  618                         hl, (u_long) (authsize + rplen - sizeof (struct ah)),
  619                         ipsec_address(&sav->sah->saidx.dst),
  620                         (u_long) ntohl(sav->spi)));
  621                 V_ahstat.ahs_badauthl++;
  622                 m_freem(m);
  623                 return EACCES;
  624         }
  625         V_ahstat.ahs_ibytes += m->m_pkthdr.len - skip - hl;
  626 
  627         /* Get crypto descriptors. */
  628         crp = crypto_getreq(1);
  629         if (crp == NULL) {
  630                 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
  631                 V_ahstat.ahs_crypto++;
  632                 m_freem(m);
  633                 return ENOBUFS;
  634         }
  635 
  636         crda = crp->crp_desc;
  637         IPSEC_ASSERT(crda != NULL, ("null crypto descriptor"));
  638 
  639         crda->crd_skip = 0;
  640         crda->crd_len = m->m_pkthdr.len;
  641         crda->crd_inject = skip + rplen;
  642 
  643         /* Authentication operation. */
  644         crda->crd_alg = ahx->type;
  645         crda->crd_klen = _KEYBITS(sav->key_auth);
  646         crda->crd_key = sav->key_auth->key_data;
  647 
  648         /* Find out if we've already done crypto. */
  649         for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL);
  650              mtag != NULL;
  651              mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) {
  652                 tdbi = (struct tdb_ident *) (mtag + 1);
  653                 if (tdbi->proto == sav->sah->saidx.proto &&
  654                     tdbi->spi == sav->spi &&
  655                     !bcmp(&tdbi->dst, &sav->sah->saidx.dst,
  656                           sizeof (union sockaddr_union)))
  657                         break;
  658         }
  659 
  660         /* Allocate IPsec-specific opaque crypto info. */
  661         if (mtag == NULL) {
  662                 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto) +
  663                         skip + rplen + authsize, M_XDATA, M_NOWAIT|M_ZERO);
  664         } else {
  665                 /* Hash verification has already been done successfully. */
  666                 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto),
  667                                                     M_XDATA, M_NOWAIT|M_ZERO);
  668         }
  669         if (tc == NULL) {
  670                 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
  671                 V_ahstat.ahs_crypto++;
  672                 crypto_freereq(crp);
  673                 m_freem(m);
  674                 return ENOBUFS;
  675         }
  676 
  677         /* Only save information if crypto processing is needed. */
  678         if (mtag == NULL) {
  679                 int error;
  680 
  681                 /*
  682                  * Save the authenticator, the skipped portion of the packet,
  683                  * and the AH header.
  684                  */
  685                 m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(tc+1));
  686 
  687                 /* Zeroize the authenticator on the packet. */
  688                 m_copyback(m, skip + rplen, authsize, ipseczeroes);
  689 
  690                 /* "Massage" the packet headers for crypto processing. */
  691                 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
  692                     skip, ahx->type, 0);
  693                 if (error != 0) {
  694                         /* NB: mbuf is free'd by ah_massage_headers */
  695                         V_ahstat.ahs_hdrops++;
  696                         free(tc, M_XDATA);
  697                         crypto_freereq(crp);
  698                         return error;
  699                 }
  700         }
  701 
  702         /* Crypto operation descriptor. */
  703         crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
  704         crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
  705         crp->crp_buf = (caddr_t) m;
  706         crp->crp_callback = ah_input_cb;
  707         crp->crp_sid = sav->tdb_cryptoid;
  708         crp->crp_opaque = (caddr_t) tc;
  709 
  710         /* These are passed as-is to the callback. */
  711         tc->tc_spi = sav->spi;
  712         tc->tc_dst = sav->sah->saidx.dst;
  713         tc->tc_proto = sav->sah->saidx.proto;
  714         tc->tc_nxt = ah->ah_nxt;
  715         tc->tc_protoff = protoff;
  716         tc->tc_skip = skip;
  717         tc->tc_ptr = (caddr_t) mtag; /* Save the mtag we've identified. */
  718         KEY_ADDREFSA(sav);
  719         tc->tc_sav = sav;
  720 
  721         if (mtag == NULL)
  722                 return crypto_dispatch(crp);
  723         else
  724                 return ah_input_cb(crp);
  725 }
  726 
  727 #ifdef INET6
  728 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do {              \
  729         if (saidx->dst.sa.sa_family == AF_INET6) {                           \
  730                 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
  731         } else {                                                             \
  732                 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
  733         }                                                                    \
  734 } while (0)
  735 #else
  736 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag)                   \
  737         (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
  738 #endif
  739 
  740 /*
  741  * AH input callback from the crypto driver.
  742  */
  743 static int
  744 ah_input_cb(struct cryptop *crp)
  745 {
  746         int rplen, error, skip, protoff;
  747         unsigned char calc[AH_ALEN_MAX];
  748         struct mbuf *m;
  749         struct cryptodesc *crd;
  750         struct auth_hash *ahx;
  751         struct tdb_crypto *tc;
  752         struct m_tag *mtag;
  753         struct secasvar *sav;
  754         struct secasindex *saidx;
  755         u_int8_t nxt;
  756         caddr_t ptr;
  757         int authsize;
  758 
  759         crd = crp->crp_desc;
  760 
  761         tc = (struct tdb_crypto *) crp->crp_opaque;
  762         IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
  763         skip = tc->tc_skip;
  764         nxt = tc->tc_nxt;
  765         protoff = tc->tc_protoff;
  766         mtag = (struct m_tag *) tc->tc_ptr;
  767         m = (struct mbuf *) crp->crp_buf;
  768 
  769         sav = tc->tc_sav;
  770         IPSEC_ASSERT(sav != NULL, ("null SA!"));
  771 
  772         saidx = &sav->sah->saidx;
  773         IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
  774                 saidx->dst.sa.sa_family == AF_INET6,
  775                 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
  776 
  777         ahx = (struct auth_hash *) sav->tdb_authalgxform;
  778 
  779         /* Check for crypto errors. */
  780         if (crp->crp_etype) {
  781                 if (sav->tdb_cryptoid != 0)
  782                         sav->tdb_cryptoid = crp->crp_sid;
  783 
  784                 if (crp->crp_etype == EAGAIN) {
  785                         error = crypto_dispatch(crp);
  786                         return error;
  787                 }
  788 
  789                 V_ahstat.ahs_noxform++;
  790                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
  791                 error = crp->crp_etype;
  792                 goto bad;
  793         } else {
  794                 V_ahstat.ahs_hist[sav->alg_auth]++;
  795                 crypto_freereq(crp);            /* No longer needed. */
  796                 crp = NULL;
  797         }
  798 
  799         /* Shouldn't happen... */
  800         if (m == NULL) {
  801                 V_ahstat.ahs_crypto++;
  802                 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
  803                 error = EINVAL;
  804                 goto bad;
  805         }
  806 
  807         /* Figure out header size. */
  808         rplen = HDRSIZE(sav);
  809         authsize = AUTHSIZE(sav);
  810 
  811         /* Copy authenticator off the packet. */
  812         m_copydata(m, skip + rplen, authsize, calc);
  813 
  814         /*
  815          * If we have an mtag, we don't need to verify the authenticator --
  816          * it has been verified by an IPsec-aware NIC.
  817          */
  818         if (mtag == NULL) {
  819                 ptr = (caddr_t) (tc + 1);
  820 
  821                 /* Verify authenticator. */
  822                 if (bcmp(ptr + skip + rplen, calc, authsize)) {
  823                         DPRINTF(("%s: authentication hash mismatch for packet "
  824                             "in SA %s/%08lx\n", __func__,
  825                             ipsec_address(&saidx->dst),
  826                             (u_long) ntohl(sav->spi)));
  827                         V_ahstat.ahs_badauth++;
  828                         error = EACCES;
  829                         goto bad;
  830                 }
  831 
  832                 /* Fix the Next Protocol field. */
  833                 ((u_int8_t *) ptr)[protoff] = nxt;
  834 
  835                 /* Copyback the saved (uncooked) network headers. */
  836                 m_copyback(m, 0, skip, ptr);
  837         } else {
  838                 /* Fix the Next Protocol field. */
  839                 m_copyback(m, protoff, sizeof(u_int8_t), &nxt);
  840         }
  841 
  842         free(tc, M_XDATA), tc = NULL;                   /* No longer needed */
  843 
  844         /*
  845          * Header is now authenticated.
  846          */
  847         m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM;
  848 
  849         /*
  850          * Update replay sequence number, if appropriate.
  851          */
  852         if (sav->replay) {
  853                 u_int32_t seq;
  854 
  855                 m_copydata(m, skip + offsetof(struct newah, ah_seq),
  856                            sizeof (seq), (caddr_t) &seq);
  857                 if (ipsec_updatereplay(ntohl(seq), sav)) {
  858                         V_ahstat.ahs_replay++;
  859                         error = ENOBUFS;                        /*XXX as above*/
  860                         goto bad;
  861                 }
  862         }
  863 
  864         /*
  865          * Remove the AH header and authenticator from the mbuf.
  866          */
  867         error = m_striphdr(m, skip, rplen + authsize);
  868         if (error) {
  869                 DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__,
  870                     ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
  871 
  872                 V_ahstat.ahs_hdrops++;
  873                 goto bad;
  874         }
  875 
  876         IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag);
  877 
  878         KEY_FREESAV(&sav);
  879         return error;
  880 bad:
  881         if (sav)
  882                 KEY_FREESAV(&sav);
  883         if (m != NULL)
  884                 m_freem(m);
  885         if (tc != NULL)
  886                 free(tc, M_XDATA);
  887         if (crp != NULL)
  888                 crypto_freereq(crp);
  889         return error;
  890 }
  891 
  892 /*
  893  * AH output routine, called by ipsec[46]_process_packet().
  894  */
  895 static int
  896 ah_output(
  897         struct mbuf *m,
  898         struct ipsecrequest *isr,
  899         struct mbuf **mp,
  900         int skip,
  901         int protoff)
  902 {
  903         struct secasvar *sav;
  904         struct auth_hash *ahx;
  905         struct cryptodesc *crda;
  906         struct tdb_crypto *tc;
  907         struct mbuf *mi;
  908         struct cryptop *crp;
  909         u_int16_t iplen;
  910         int error, rplen, authsize, maxpacketsize, roff;
  911         u_int8_t prot;
  912         struct newah *ah;
  913 
  914         sav = isr->sav;
  915         IPSEC_ASSERT(sav != NULL, ("null SA"));
  916         ahx = sav->tdb_authalgxform;
  917         IPSEC_ASSERT(ahx != NULL, ("null authentication xform"));
  918 
  919         V_ahstat.ahs_output++;
  920 
  921         /* Figure out header size. */
  922         rplen = HDRSIZE(sav);
  923 
  924         /* Check for maximum packet size violations. */
  925         switch (sav->sah->saidx.dst.sa.sa_family) {
  926 #ifdef INET
  927         case AF_INET:
  928                 maxpacketsize = IP_MAXPACKET;
  929                 break;
  930 #endif /* INET */
  931 #ifdef INET6
  932         case AF_INET6:
  933                 maxpacketsize = IPV6_MAXPACKET;
  934                 break;
  935 #endif /* INET6 */
  936         default:
  937                 DPRINTF(("%s: unknown/unsupported protocol family %u, "
  938                     "SA %s/%08lx\n", __func__,
  939                     sav->sah->saidx.dst.sa.sa_family,
  940                     ipsec_address(&sav->sah->saidx.dst),
  941                     (u_long) ntohl(sav->spi)));
  942                 V_ahstat.ahs_nopf++;
  943                 error = EPFNOSUPPORT;
  944                 goto bad;
  945         }
  946         authsize = AUTHSIZE(sav);
  947         if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) {
  948                 DPRINTF(("%s: packet in SA %s/%08lx got too big "
  949                     "(len %u, max len %u)\n", __func__,
  950                     ipsec_address(&sav->sah->saidx.dst),
  951                     (u_long) ntohl(sav->spi),
  952                     rplen + authsize + m->m_pkthdr.len, maxpacketsize));
  953                 V_ahstat.ahs_toobig++;
  954                 error = EMSGSIZE;
  955                 goto bad;
  956         }
  957 
  958         /* Update the counters. */
  959         V_ahstat.ahs_obytes += m->m_pkthdr.len - skip;
  960 
  961         m = m_unshare(m, M_NOWAIT);
  962         if (m == NULL) {
  963                 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
  964                     ipsec_address(&sav->sah->saidx.dst),
  965                     (u_long) ntohl(sav->spi)));
  966                 V_ahstat.ahs_hdrops++;
  967                 error = ENOBUFS;
  968                 goto bad;
  969         }
  970 
  971         /* Inject AH header. */
  972         mi = m_makespace(m, skip, rplen + authsize, &roff);
  973         if (mi == NULL) {
  974                 DPRINTF(("%s: failed to inject %u byte AH header for SA "
  975                     "%s/%08lx\n", __func__,
  976                     rplen + authsize,
  977                     ipsec_address(&sav->sah->saidx.dst),
  978                     (u_long) ntohl(sav->spi)));
  979                 V_ahstat.ahs_hdrops++;          /*XXX differs from openbsd */
  980                 error = ENOBUFS;
  981                 goto bad;
  982         }
  983 
  984         /*
  985          * The AH header is guaranteed by m_makespace() to be in
  986          * contiguous memory, at roff bytes offset into the returned mbuf.
  987          */
  988         ah = (struct newah *)(mtod(mi, caddr_t) + roff);
  989 
  990         /* Initialize the AH header. */
  991         m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt);
  992         ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(u_int32_t);
  993         ah->ah_reserve = 0;
  994         ah->ah_spi = sav->spi;
  995 
  996         /* Zeroize authenticator. */
  997         m_copyback(m, skip + rplen, authsize, ipseczeroes);
  998 
  999         /* Insert packet replay counter, as requested.  */
 1000         if (sav->replay) {
 1001                 if (sav->replay->count == ~0 &&
 1002                     (sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
 1003                         DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n",
 1004                                 __func__,
 1005                                 ipsec_address(&sav->sah->saidx.dst),
 1006                                 (u_long) ntohl(sav->spi)));
 1007                         V_ahstat.ahs_wrap++;
 1008                         error = EINVAL;
 1009                         goto bad;
 1010                 }
 1011 #ifdef REGRESSION
 1012                 /* Emulate replay attack when ipsec_replay is TRUE. */
 1013                 if (!V_ipsec_replay)
 1014 #endif
 1015                         sav->replay->count++;
 1016                 ah->ah_seq = htonl(sav->replay->count);
 1017         }
 1018 
 1019         /* Get crypto descriptors. */
 1020         crp = crypto_getreq(1);
 1021         if (crp == NULL) {
 1022                 DPRINTF(("%s: failed to acquire crypto descriptors\n",
 1023                         __func__));
 1024                 V_ahstat.ahs_crypto++;
 1025                 error = ENOBUFS;
 1026                 goto bad;
 1027         }
 1028 
 1029         crda = crp->crp_desc;
 1030 
 1031         crda->crd_skip = 0;
 1032         crda->crd_inject = skip + rplen;
 1033         crda->crd_len = m->m_pkthdr.len;
 1034 
 1035         /* Authentication operation. */
 1036         crda->crd_alg = ahx->type;
 1037         crda->crd_key = sav->key_auth->key_data;
 1038         crda->crd_klen = _KEYBITS(sav->key_auth);
 1039 
 1040         /* Allocate IPsec-specific opaque crypto info. */
 1041         tc = (struct tdb_crypto *) malloc(
 1042                 sizeof(struct tdb_crypto) + skip, M_XDATA, M_NOWAIT|M_ZERO);
 1043         if (tc == NULL) {
 1044                 crypto_freereq(crp);
 1045                 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
 1046                 V_ahstat.ahs_crypto++;
 1047                 error = ENOBUFS;
 1048                 goto bad;
 1049         }
 1050 
 1051         /* Save the skipped portion of the packet. */
 1052         m_copydata(m, 0, skip, (caddr_t) (tc + 1));
 1053 
 1054         /*
 1055          * Fix IP header length on the header used for
 1056          * authentication. We don't need to fix the original
 1057          * header length as it will be fixed by our caller.
 1058          */
 1059         switch (sav->sah->saidx.dst.sa.sa_family) {
 1060 #ifdef INET
 1061         case AF_INET:
 1062                 bcopy(((caddr_t)(tc + 1)) +
 1063                     offsetof(struct ip, ip_len),
 1064                     (caddr_t) &iplen, sizeof(u_int16_t));
 1065                 iplen = htons(ntohs(iplen) + rplen + authsize);
 1066                 m_copyback(m, offsetof(struct ip, ip_len),
 1067                     sizeof(u_int16_t), (caddr_t) &iplen);
 1068                 break;
 1069 #endif /* INET */
 1070 
 1071 #ifdef INET6
 1072         case AF_INET6:
 1073                 bcopy(((caddr_t)(tc + 1)) +
 1074                     offsetof(struct ip6_hdr, ip6_plen),
 1075                     (caddr_t) &iplen, sizeof(u_int16_t));
 1076                 iplen = htons(ntohs(iplen) + rplen + authsize);
 1077                 m_copyback(m, offsetof(struct ip6_hdr, ip6_plen),
 1078                     sizeof(u_int16_t), (caddr_t) &iplen);
 1079                 break;
 1080 #endif /* INET6 */
 1081         }
 1082 
 1083         /* Fix the Next Header field in saved header. */
 1084         ((u_int8_t *) (tc + 1))[protoff] = IPPROTO_AH;
 1085 
 1086         /* Update the Next Protocol field in the IP header. */
 1087         prot = IPPROTO_AH;
 1088         m_copyback(m, protoff, sizeof(u_int8_t), (caddr_t) &prot);
 1089 
 1090         /* "Massage" the packet headers for crypto processing. */
 1091         error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
 1092                         skip, ahx->type, 1);
 1093         if (error != 0) {
 1094                 m = NULL;       /* mbuf was free'd by ah_massage_headers. */
 1095                 free(tc, M_XDATA);
 1096                 crypto_freereq(crp);
 1097                 goto bad;
 1098         }
 1099 
 1100         /* Crypto operation descriptor. */
 1101         crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
 1102         crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
 1103         crp->crp_buf = (caddr_t) m;
 1104         crp->crp_callback = ah_output_cb;
 1105         crp->crp_sid = sav->tdb_cryptoid;
 1106         crp->crp_opaque = (caddr_t) tc;
 1107 
 1108         /* These are passed as-is to the callback. */
 1109         tc->tc_isr = isr;
 1110         KEY_ADDREFSA(sav);
 1111         tc->tc_sav = sav;
 1112         tc->tc_spi = sav->spi;
 1113         tc->tc_dst = sav->sah->saidx.dst;
 1114         tc->tc_proto = sav->sah->saidx.proto;
 1115         tc->tc_skip = skip;
 1116         tc->tc_protoff = protoff;
 1117 
 1118         return crypto_dispatch(crp);
 1119 bad:
 1120         if (m)
 1121                 m_freem(m);
 1122         return (error);
 1123 }
 1124 
 1125 /*
 1126  * AH output callback from the crypto driver.
 1127  */
 1128 static int
 1129 ah_output_cb(struct cryptop *crp)
 1130 {
 1131         int skip, protoff, error;
 1132         struct tdb_crypto *tc;
 1133         struct ipsecrequest *isr;
 1134         struct secasvar *sav;
 1135         struct mbuf *m;
 1136         caddr_t ptr;
 1137         int err;
 1138 
 1139         tc = (struct tdb_crypto *) crp->crp_opaque;
 1140         IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
 1141         skip = tc->tc_skip;
 1142         protoff = tc->tc_protoff;
 1143         ptr = (caddr_t) (tc + 1);
 1144         m = (struct mbuf *) crp->crp_buf;
 1145 
 1146         isr = tc->tc_isr;
 1147         IPSECREQUEST_LOCK(isr);
 1148         sav = tc->tc_sav;
 1149         /* With the isr lock released SA pointer can be updated. */
 1150         if (sav != isr->sav) {
 1151                 V_ahstat.ahs_notdb++;
 1152                 DPRINTF(("%s: SA expired while in crypto\n", __func__));
 1153                 error = ENOBUFS;                /*XXX*/
 1154                 goto bad;
 1155         }
 1156 
 1157         /* Check for crypto errors. */
 1158         if (crp->crp_etype) {
 1159                 if (sav->tdb_cryptoid != 0)
 1160                         sav->tdb_cryptoid = crp->crp_sid;
 1161 
 1162                 if (crp->crp_etype == EAGAIN) {
 1163                         IPSECREQUEST_UNLOCK(isr);
 1164                         error = crypto_dispatch(crp);
 1165                         return error;
 1166                 }
 1167 
 1168                 V_ahstat.ahs_noxform++;
 1169                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
 1170                 error = crp->crp_etype;
 1171                 goto bad;
 1172         }
 1173 
 1174         /* Shouldn't happen... */
 1175         if (m == NULL) {
 1176                 V_ahstat.ahs_crypto++;
 1177                 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
 1178                 error = EINVAL;
 1179                 goto bad;
 1180         }
 1181         V_ahstat.ahs_hist[sav->alg_auth]++;
 1182 
 1183         /*
 1184          * Copy original headers (with the new protocol number) back
 1185          * in place.
 1186          */
 1187         m_copyback(m, 0, skip, ptr);
 1188 
 1189         /* No longer needed. */
 1190         free(tc, M_XDATA);
 1191         crypto_freereq(crp);
 1192 
 1193 #ifdef REGRESSION
 1194         /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
 1195         if (V_ipsec_integrity) {
 1196                 int alen;
 1197 
 1198                 /*
 1199                  * Corrupt HMAC if we want to test integrity verification of
 1200                  * the other side.
 1201                  */
 1202                 alen = AUTHSIZE(sav);
 1203                 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes);
 1204         }
 1205 #endif
 1206 
 1207         /* NB: m is reclaimed by ipsec_process_done. */
 1208         err = ipsec_process_done(m, isr);
 1209         KEY_FREESAV(&sav);
 1210         IPSECREQUEST_UNLOCK(isr);
 1211         return err;
 1212 bad:
 1213         if (sav)
 1214                 KEY_FREESAV(&sav);
 1215         IPSECREQUEST_UNLOCK(isr);
 1216         if (m)
 1217                 m_freem(m);
 1218         free(tc, M_XDATA);
 1219         crypto_freereq(crp);
 1220         return error;
 1221 }
 1222 
 1223 static struct xformsw ah_xformsw = {
 1224         XF_AH,          XFT_AUTH,       "IPsec AH",
 1225         ah_init,        ah_zeroize,     ah_input,       ah_output,
 1226 };
 1227 
 1228 static void
 1229 ah_attach(void)
 1230 {
 1231 
 1232         xform_register(&ah_xformsw);
 1233 }
 1234 
 1235 SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ah_attach, NULL);

Cache object: 8f77c274b5967556516f3b91e327eeac


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.