2 /* $OpenBSD: ip_ah.c,v 1.63 2001/06/26 06:18:58 angelos Exp $ */
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).
8 * The original version of this code was written by John Ioannidis
9 * for BSD/OS in Athens, Greece, in November 1995.
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17 * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist.
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.
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
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
40 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/socket.h>
46 #include <sys/syslog.h>
47 #include <sys/kernel.h>
49 #include <sys/mutex.h>
50 #include <sys/sysctl.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_ecn.h>
59 #include <netinet/ip6.h>
61 #include <netipsec/ipsec.h>
62 #include <netipsec/ah.h>
63 #include <netipsec/ah_var.h>
64 #include <netipsec/xform.h>
67 #include <netinet6/ip6_var.h>
68 #include <netipsec/ipsec6.h>
69 #include <netinet6/ip6_ecn.h>
72 #include <netipsec/key.h>
73 #include <netipsec/key_debug.h>
75 #include <opencrypto/cryptodev.h>
78 * Return header size in bytes. The old protocol did not support
79 * the replay counter; the new protocol always includes the counter.
81 #define HDRSIZE(sav) \
82 (((sav)->flags & SADB_X_EXT_OLD) ? \
83 sizeof (struct ah) : sizeof (struct ah) + sizeof (u_int32_t))
85 * Return authenticator size in bytes, based on a field in the
86 * algorithm descriptor.
88 #define AUTHSIZE(sav) ((sav->flags & SADB_X_EXT_OLD) ? 16 : \
89 xform_ah_authsize((sav)->tdb_authalgxform))
91 VNET_DEFINE(int, ah_enable) = 1; /* control flow of packets with AH */
92 VNET_DEFINE(int, ah_cleartos) = 1; /* clear ip_tos when doing AH calc */
93 VNET_PCPUSTAT_DEFINE(struct ahstat, ahstat);
94 VNET_PCPUSTAT_SYSINIT(ahstat);
97 VNET_PCPUSTAT_SYSUNINIT(ahstat);
101 SYSCTL_DECL(_net_inet_ah);
102 SYSCTL_INT(_net_inet_ah, OID_AUTO, ah_enable,
103 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ah_enable), 0, "");
104 SYSCTL_INT(_net_inet_ah, OID_AUTO, ah_cleartos,
105 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ah_cleartos), 0, "");
106 SYSCTL_VNET_PCPUSTAT(_net_inet_ah, IPSECCTL_STATS, stats, struct ahstat,
107 ahstat, "AH statistics (struct ahstat, netipsec/ah_var.h)");
110 static unsigned char ipseczeroes[256]; /* larger than an ip6 extension hdr */
112 static int ah_input_cb(struct cryptop*);
113 static int ah_output_cb(struct cryptop*);
116 xform_ah_authsize(const struct auth_hash *esph)
123 switch (esph->type) {
124 case CRYPTO_SHA2_256_HMAC:
125 case CRYPTO_SHA2_384_HMAC:
126 case CRYPTO_SHA2_512_HMAC:
127 alen = esph->hashsize / 2; /* RFC4868 2.3 */
130 case CRYPTO_AES_128_NIST_GMAC:
131 case CRYPTO_AES_192_NIST_GMAC:
132 case CRYPTO_AES_256_NIST_GMAC:
133 alen = esph->hashsize;
137 alen = AH_HMAC_HASHLEN;
145 ah_hdrsiz(struct secasvar *sav)
151 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, ("null xform"));
152 /*XXX not right for null algorithm--does it matter??*/
153 authsize = AUTHSIZE(sav);
154 size = roundup(authsize, sizeof (u_int32_t)) + HDRSIZE(sav);
157 size = sizeof (struct ah) + sizeof (u_int32_t) + 16;
163 * NB: public for use by esp_init.
166 ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria)
168 const struct auth_hash *thash;
171 thash = auth_algorithm_lookup(sav->alg_auth);
173 DPRINTF(("%s: unsupported authentication algorithm %u\n",
174 __func__, sav->alg_auth));
178 * Verify the replay state block allocation is consistent with
179 * the protocol type. We check here so we can make assumptions
180 * later during protocol processing.
182 /* NB: replay state is setup elsewhere (sigh) */
183 if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) {
184 DPRINTF(("%s: replay state block inconsistency, "
185 "%s algorithm %s replay state\n", __func__,
186 (sav->flags & SADB_X_EXT_OLD) ? "old" : "new",
187 sav->replay == NULL ? "without" : "with"));
190 if (sav->key_auth == NULL) {
191 DPRINTF(("%s: no authentication key for %s algorithm\n",
192 __func__, thash->name));
195 keylen = _KEYLEN(sav->key_auth);
196 if (keylen > thash->keysize && thash->keysize != 0) {
197 DPRINTF(("%s: invalid keylength %d, algorithm %s requires "
198 "keysize less than %d\n", __func__,
199 keylen, thash->name, thash->keysize));
203 sav->tdb_xform = xsp;
204 sav->tdb_authalgxform = thash;
206 /* Initialize crypto session. */
207 bzero(cria, sizeof (*cria));
208 cria->cri_alg = sav->tdb_authalgxform->type;
209 cria->cri_klen = _KEYBITS(sav->key_auth);
210 cria->cri_key = sav->key_auth->key_data;
211 cria->cri_mlen = AUTHSIZE(sav);
217 * ah_init() is called when an SPI is being set up.
220 ah_init(struct secasvar *sav, struct xformsw *xsp)
222 struct cryptoini cria;
225 error = ah_init0(sav, xsp, &cria);
226 return error ? error :
227 crypto_newsession(&sav->tdb_cryptoid, &cria, V_crypto_support);
233 * NB: public for use by esp_zeroize (XXX).
236 ah_zeroize(struct secasvar *sav)
241 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
243 err = crypto_freesession(sav->tdb_cryptoid);
244 sav->tdb_cryptoid = 0;
245 sav->tdb_authalgxform = NULL;
246 sav->tdb_xform = NULL;
251 * Massage IPv4/IPv6 headers for AH processing.
254 ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out)
256 struct mbuf *m = *m0;
265 struct ip6_ext *ip6e;
274 * This is the least painful way of dealing with IPv4 header
275 * and option processing -- just make sure they're in
278 *m0 = m = m_pullup(m, skip);
280 DPRINTF(("%s: m_pullup failed\n", __func__));
284 /* Fix the IP header */
285 ip = mtod(m, struct ip *);
291 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
292 ip->ip_off &= htons(IP_DF);
294 ip->ip_off = htons(0);
296 ptr = mtod(m, unsigned char *) + sizeof(struct ip);
298 /* IPv4 option processing */
299 for (off = sizeof(struct ip); off < skip;) {
300 if (ptr[off] == IPOPT_EOL || ptr[off] == IPOPT_NOP ||
304 DPRINTF(("%s: illegal IPv4 option length for "
305 "option %d\n", __func__, ptr[off]));
313 off = skip; /* End the loop. */
320 case IPOPT_SECURITY: /* 0x82 */
321 case 0x85: /* Extended security. */
322 case 0x86: /* Commercial security. */
323 case 0x94: /* Router alert */
324 case 0x95: /* RFC1770 */
325 /* Sanity check for option length. */
326 if (ptr[off + 1] < 2) {
327 DPRINTF(("%s: illegal IPv4 option "
328 "length for option %d\n",
329 __func__, ptr[off]));
340 /* Sanity check for option length. */
341 if (ptr[off + 1] < 2) {
342 DPRINTF(("%s: illegal IPv4 option "
343 "length for option %d\n",
344 __func__, ptr[off]));
351 * On output, if we have either of the
352 * source routing options, we should
353 * swap the destination address of the
354 * IP header with the last address
355 * specified in the option, as that is
356 * what the destination's IP header
360 bcopy(ptr + off + ptr[off + 1] -
361 sizeof(struct in_addr),
362 &(ip->ip_dst), sizeof(struct in_addr));
366 /* Sanity check for option length. */
367 if (ptr[off + 1] < 2) {
368 DPRINTF(("%s: illegal IPv4 option "
369 "length for option %d\n",
370 __func__, ptr[off]));
375 /* Zeroize all other options. */
376 count = ptr[off + 1];
377 bcopy(ipseczeroes, ptr, count);
384 DPRINTF(("%s: malformed IPv4 options header\n",
396 case AF_INET6: /* Ugly... */
397 /* Copy and "cook" the IPv6 header. */
398 m_copydata(m, 0, sizeof(ip6), (caddr_t) &ip6);
400 /* We don't do IPv6 Jumbograms. */
401 if (ip6.ip6_plen == 0) {
402 DPRINTF(("%s: unsupported IPv6 jumbogram\n", __func__));
409 ip6.ip6_vfc &= ~IPV6_VERSION_MASK;
410 ip6.ip6_vfc |= IPV6_VERSION;
412 /* Scoped address handling. */
413 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src))
414 ip6.ip6_src.s6_addr16[1] = 0;
415 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst))
416 ip6.ip6_dst.s6_addr16[1] = 0;
418 /* Done with IPv6 header. */
419 m_copyback(m, 0, sizeof(struct ip6_hdr), (caddr_t) &ip6);
421 /* Let's deal with the remaining headers (if any). */
422 if (skip - sizeof(struct ip6_hdr) > 0) {
423 if (m->m_len <= skip) {
424 ptr = (unsigned char *) malloc(
425 skip - sizeof(struct ip6_hdr),
428 DPRINTF(("%s: failed to allocate memory"
429 "for IPv6 headers\n",__func__));
435 * Copy all the protocol headers after
438 m_copydata(m, sizeof(struct ip6_hdr),
439 skip - sizeof(struct ip6_hdr), ptr);
442 /* No need to allocate memory. */
443 ptr = mtod(m, unsigned char *) +
444 sizeof(struct ip6_hdr);
450 off = ip6.ip6_nxt & 0xff; /* Next header type. */
452 for (len = 0; len < skip - sizeof(struct ip6_hdr);)
454 case IPPROTO_HOPOPTS:
455 case IPPROTO_DSTOPTS:
456 ip6e = (struct ip6_ext *) (ptr + len);
459 * Process the mutable/immutable
460 * options -- borrows heavily from the
463 for (count = len + sizeof(struct ip6_ext);
464 count < len + ((ip6e->ip6e_len + 1) << 3);) {
465 if (ptr[count] == IP6OPT_PAD1) {
467 continue; /* Skip padding. */
472 ((ip6e->ip6e_len + 1) << 3)) {
475 /* Free, if we allocated. */
483 /* If mutable option, zeroize. */
484 if (ptr[count] & IP6OPT_MUTABLE)
485 bcopy(ipseczeroes, ptr + count,
492 skip - sizeof(struct ip6_hdr)) {
495 /* Free, if we allocated. */
503 len += ((ip6e->ip6e_len + 1) << 3);
504 off = ip6e->ip6e_nxt;
507 case IPPROTO_ROUTING:
509 * Always include routing headers in
512 ip6e = (struct ip6_ext *) (ptr + len);
513 len += ((ip6e->ip6e_len + 1) << 3);
514 off = ip6e->ip6e_nxt;
518 DPRINTF(("%s: unexpected IPv6 header type %d",
526 /* Copyback and free, if we allocated. */
528 m_copyback(m, sizeof(struct ip6_hdr),
529 skip - sizeof(struct ip6_hdr), ptr);
541 * ah_input() gets called to verify that an input packet
542 * passes authentication.
545 ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
547 IPSEC_DEBUG_DECLARE(char buf[128]);
548 const struct auth_hash *ahx;
549 struct cryptodesc *crda;
551 struct xform_data *xd;
554 int hl, rplen, authsize, error;
556 IPSEC_ASSERT(sav != NULL, ("null SA"));
557 IPSEC_ASSERT(sav->key_auth != NULL, ("null authentication key"));
558 IPSEC_ASSERT(sav->tdb_authalgxform != NULL,
559 ("null authentication xform"));
561 /* Figure out header size. */
562 rplen = HDRSIZE(sav);
564 /* XXX don't pullup, just copy header */
565 IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen);
567 DPRINTF(("ah_input: cannot pullup header\n"));
568 AHSTAT_INC(ahs_hdrops); /*XXX*/
573 /* Check replay window, if applicable. */
575 if (sav->replay != NULL && sav->replay->wsize != 0 &&
576 ipsec_chkreplay(ntohl(ah->ah_seq), sav) == 0) {
577 SECASVAR_UNLOCK(sav);
578 AHSTAT_INC(ahs_replay);
579 DPRINTF(("%s: packet replay failure: %s\n", __func__,
580 ipsec_sa2str(sav, buf, sizeof(buf))));
584 cryptoid = sav->tdb_cryptoid;
585 SECASVAR_UNLOCK(sav);
587 /* Verify AH header length. */
588 hl = ah->ah_len * sizeof (u_int32_t);
589 ahx = sav->tdb_authalgxform;
590 authsize = AUTHSIZE(sav);
591 if (hl != authsize + rplen - sizeof (struct ah)) {
592 DPRINTF(("%s: bad authenticator length %u (expecting %lu)"
593 " for packet in SA %s/%08lx\n", __func__, hl,
594 (u_long) (authsize + rplen - sizeof (struct ah)),
595 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
596 (u_long) ntohl(sav->spi)));
597 AHSTAT_INC(ahs_badauthl);
601 AHSTAT_ADD(ahs_ibytes, m->m_pkthdr.len - skip - hl);
603 /* Get crypto descriptors. */
604 crp = crypto_getreq(1);
606 DPRINTF(("%s: failed to acquire crypto descriptor\n",
608 AHSTAT_INC(ahs_crypto);
613 crda = crp->crp_desc;
614 IPSEC_ASSERT(crda != NULL, ("null crypto descriptor"));
617 crda->crd_len = m->m_pkthdr.len;
618 crda->crd_inject = skip + rplen;
620 /* Authentication operation. */
621 crda->crd_alg = ahx->type;
622 crda->crd_klen = _KEYBITS(sav->key_auth);
623 crda->crd_key = sav->key_auth->key_data;
625 /* Allocate IPsec-specific opaque crypto info. */
626 xd = malloc(sizeof(*xd) + skip + rplen + authsize, M_XDATA,
629 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
630 AHSTAT_INC(ahs_crypto);
637 * Save the authenticator, the skipped portion of the packet,
640 m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(xd + 1));
642 /* Zeroize the authenticator on the packet. */
643 m_copyback(m, skip + rplen, authsize, ipseczeroes);
645 /* "Massage" the packet headers for crypto processing. */
646 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
649 /* NB: mbuf is free'd by ah_massage_headers */
650 AHSTAT_INC(ahs_hdrops);
657 /* Crypto operation descriptor. */
658 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
659 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
660 crp->crp_buf = (caddr_t) m;
661 crp->crp_callback = ah_input_cb;
662 crp->crp_sid = cryptoid;
663 crp->crp_opaque = (caddr_t) xd;
665 /* These are passed as-is to the callback. */
667 xd->nxt = ah->ah_nxt;
668 xd->protoff = protoff;
670 xd->cryptoid = cryptoid;
671 return (crypto_dispatch(crp));
679 * AH input callback from the crypto driver.
682 ah_input_cb(struct cryptop *crp)
684 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
685 unsigned char calc[AH_ALEN_MAX];
686 const struct auth_hash *ahx;
688 struct cryptodesc *crd;
689 struct xform_data *xd;
690 struct secasvar *sav;
691 struct secasindex *saidx;
694 int authsize, rplen, error, skip, protoff;
698 m = (struct mbuf *) crp->crp_buf;
699 xd = (struct xform_data *) crp->crp_opaque;
703 protoff = xd->protoff;
704 cryptoid = xd->cryptoid;
705 saidx = &sav->sah->saidx;
706 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
707 saidx->dst.sa.sa_family == AF_INET6,
708 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
710 ahx = sav->tdb_authalgxform;
712 /* Check for crypto errors. */
713 if (crp->crp_etype) {
714 if (crp->crp_etype == EAGAIN) {
715 /* Reset the session ID */
716 if (ipsec_updateid(sav, &crp->crp_sid, &cryptoid) != 0)
717 crypto_freesession(cryptoid);
718 xd->cryptoid = crp->crp_sid;
719 return (crypto_dispatch(crp));
721 AHSTAT_INC(ahs_noxform);
722 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
723 error = crp->crp_etype;
726 AHSTAT_INC(ahs_hist[sav->alg_auth]);
727 crypto_freereq(crp); /* No longer needed. */
731 /* Shouldn't happen... */
733 AHSTAT_INC(ahs_crypto);
734 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
739 /* Figure out header size. */
740 rplen = HDRSIZE(sav);
741 authsize = AUTHSIZE(sav);
743 /* Copy authenticator off the packet. */
744 m_copydata(m, skip + rplen, authsize, calc);
746 /* Verify authenticator. */
747 ptr = (caddr_t) (xd + 1);
748 if (timingsafe_bcmp(ptr + skip + rplen, calc, authsize)) {
749 DPRINTF(("%s: authentication hash mismatch for packet "
750 "in SA %s/%08lx\n", __func__,
751 ipsec_address(&saidx->dst, buf, sizeof(buf)),
752 (u_long) ntohl(sav->spi)));
753 AHSTAT_INC(ahs_badauth);
757 /* Fix the Next Protocol field. */
758 ((uint8_t *) ptr)[protoff] = nxt;
760 /* Copyback the saved (uncooked) network headers. */
761 m_copyback(m, 0, skip, ptr);
762 free(xd, M_XDATA), xd = NULL; /* No longer needed */
765 * Header is now authenticated.
767 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM;
770 * Update replay sequence number, if appropriate.
775 m_copydata(m, skip + offsetof(struct newah, ah_seq),
776 sizeof (seq), (caddr_t) &seq);
778 if (ipsec_updatereplay(ntohl(seq), sav)) {
779 SECASVAR_UNLOCK(sav);
780 AHSTAT_INC(ahs_replay);
784 SECASVAR_UNLOCK(sav);
788 * Remove the AH header and authenticator from the mbuf.
790 error = m_striphdr(m, skip, rplen + authsize);
792 DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__,
793 ipsec_address(&saidx->dst, buf, sizeof(buf)),
794 (u_long) ntohl(sav->spi)));
795 AHSTAT_INC(ahs_hdrops);
799 switch (saidx->dst.sa.sa_family) {
802 error = ipsec6_common_input_cb(m, sav, skip, protoff);
807 error = ipsec4_common_input_cb(m, sav, skip, protoff);
811 panic("%s: Unexpected address family: %d saidx=%p", __func__,
812 saidx->dst.sa.sa_family, saidx);
828 * AH output routine, called by ipsec[46]_perform_request().
831 ah_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
832 u_int idx, int skip, int protoff)
834 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
835 const struct auth_hash *ahx;
836 struct cryptodesc *crda;
837 struct xform_data *xd;
843 int error, rplen, authsize, maxpacketsize, roff;
846 IPSEC_ASSERT(sav != NULL, ("null SA"));
847 ahx = sav->tdb_authalgxform;
848 IPSEC_ASSERT(ahx != NULL, ("null authentication xform"));
850 AHSTAT_INC(ahs_output);
852 /* Figure out header size. */
853 rplen = HDRSIZE(sav);
855 /* Check for maximum packet size violations. */
856 switch (sav->sah->saidx.dst.sa.sa_family) {
859 maxpacketsize = IP_MAXPACKET;
864 maxpacketsize = IPV6_MAXPACKET;
868 DPRINTF(("%s: unknown/unsupported protocol family %u, "
869 "SA %s/%08lx\n", __func__,
870 sav->sah->saidx.dst.sa.sa_family,
871 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
872 (u_long) ntohl(sav->spi)));
873 AHSTAT_INC(ahs_nopf);
874 error = EPFNOSUPPORT;
877 authsize = AUTHSIZE(sav);
878 if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) {
879 DPRINTF(("%s: packet in SA %s/%08lx got too big "
880 "(len %u, max len %u)\n", __func__,
881 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
882 (u_long) ntohl(sav->spi),
883 rplen + authsize + m->m_pkthdr.len, maxpacketsize));
884 AHSTAT_INC(ahs_toobig);
889 /* Update the counters. */
890 AHSTAT_ADD(ahs_obytes, m->m_pkthdr.len - skip);
892 m = m_unshare(m, M_NOWAIT);
894 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
895 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
896 (u_long) ntohl(sav->spi)));
897 AHSTAT_INC(ahs_hdrops);
902 /* Inject AH header. */
903 mi = m_makespace(m, skip, rplen + authsize, &roff);
905 DPRINTF(("%s: failed to inject %u byte AH header for SA "
906 "%s/%08lx\n", __func__,
908 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
909 (u_long) ntohl(sav->spi)));
910 AHSTAT_INC(ahs_hdrops); /*XXX differs from openbsd */
916 * The AH header is guaranteed by m_makespace() to be in
917 * contiguous memory, at roff bytes offset into the returned mbuf.
919 ah = (struct newah *)(mtod(mi, caddr_t) + roff);
921 /* Initialize the AH header. */
922 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt);
923 ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(u_int32_t);
925 ah->ah_spi = sav->spi;
927 /* Zeroize authenticator. */
928 m_copyback(m, skip + rplen, authsize, ipseczeroes);
930 /* Insert packet replay counter, as requested. */
933 if (sav->replay->count == ~0 &&
934 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
935 SECASVAR_UNLOCK(sav);
936 DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n",
937 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
938 sizeof(buf)), (u_long) ntohl(sav->spi)));
939 AHSTAT_INC(ahs_wrap);
944 /* Emulate replay attack when ipsec_replay is TRUE. */
947 sav->replay->count++;
948 ah->ah_seq = htonl(sav->replay->count);
950 cryptoid = sav->tdb_cryptoid;
951 SECASVAR_UNLOCK(sav);
953 /* Get crypto descriptors. */
954 crp = crypto_getreq(1);
956 DPRINTF(("%s: failed to acquire crypto descriptors\n",
958 AHSTAT_INC(ahs_crypto);
963 crda = crp->crp_desc;
965 crda->crd_inject = skip + rplen;
966 crda->crd_len = m->m_pkthdr.len;
968 /* Authentication operation. */
969 crda->crd_alg = ahx->type;
970 crda->crd_key = sav->key_auth->key_data;
971 crda->crd_klen = _KEYBITS(sav->key_auth);
973 /* Allocate IPsec-specific opaque crypto info. */
974 xd = malloc(sizeof(struct xform_data) + skip, M_XDATA,
978 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
979 AHSTAT_INC(ahs_crypto);
984 /* Save the skipped portion of the packet. */
985 m_copydata(m, 0, skip, (caddr_t) (xd + 1));
988 * Fix IP header length on the header used for
989 * authentication. We don't need to fix the original
990 * header length as it will be fixed by our caller.
992 switch (sav->sah->saidx.dst.sa.sa_family) {
995 bcopy(((caddr_t)(xd + 1)) +
996 offsetof(struct ip, ip_len),
997 (caddr_t) &iplen, sizeof(u_int16_t));
998 iplen = htons(ntohs(iplen) + rplen + authsize);
999 m_copyback(m, offsetof(struct ip, ip_len),
1000 sizeof(u_int16_t), (caddr_t) &iplen);
1006 bcopy(((caddr_t)(xd + 1)) +
1007 offsetof(struct ip6_hdr, ip6_plen),
1008 (caddr_t) &iplen, sizeof(uint16_t));
1009 iplen = htons(ntohs(iplen) + rplen + authsize);
1010 m_copyback(m, offsetof(struct ip6_hdr, ip6_plen),
1011 sizeof(uint16_t), (caddr_t) &iplen);
1016 /* Fix the Next Header field in saved header. */
1017 ((uint8_t *) (xd + 1))[protoff] = IPPROTO_AH;
1019 /* Update the Next Protocol field in the IP header. */
1021 m_copyback(m, protoff, sizeof(uint8_t), (caddr_t) &prot);
1023 /* "Massage" the packet headers for crypto processing. */
1024 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
1025 skip, ahx->type, 1);
1027 m = NULL; /* mbuf was free'd by ah_massage_headers. */
1029 crypto_freereq(crp);
1033 /* Crypto operation descriptor. */
1034 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
1035 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
1036 crp->crp_buf = (caddr_t) m;
1037 crp->crp_callback = ah_output_cb;
1038 crp->crp_sid = cryptoid;
1039 crp->crp_opaque = (caddr_t) xd;
1041 /* These are passed as-is to the callback. */
1046 xd->cryptoid = cryptoid;
1048 return crypto_dispatch(crp);
1058 * AH output callback from the crypto driver.
1061 ah_output_cb(struct cryptop *crp)
1063 struct xform_data *xd;
1064 struct secpolicy *sp;
1065 struct secasvar *sav;
1072 m = (struct mbuf *) crp->crp_buf;
1073 xd = (struct xform_data *) crp->crp_opaque;
1078 cryptoid = xd->cryptoid;
1079 ptr = (caddr_t) (xd + 1);
1081 /* Check for crypto errors. */
1082 if (crp->crp_etype) {
1083 if (crp->crp_etype == EAGAIN) {
1084 /* Reset the session ID */
1085 if (ipsec_updateid(sav, &crp->crp_sid, &cryptoid) != 0)
1086 crypto_freesession(cryptoid);
1087 xd->cryptoid = crp->crp_sid;
1088 return (crypto_dispatch(crp));
1090 AHSTAT_INC(ahs_noxform);
1091 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
1092 error = crp->crp_etype;
1097 /* Shouldn't happen... */
1099 AHSTAT_INC(ahs_crypto);
1100 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
1105 * Copy original headers (with the new protocol number) back
1108 m_copyback(m, 0, skip, ptr);
1111 crypto_freereq(crp);
1112 AHSTAT_INC(ahs_hist[sav->alg_auth]);
1114 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
1115 if (V_ipsec_integrity) {
1119 * Corrupt HMAC if we want to test integrity verification of
1122 alen = AUTHSIZE(sav);
1123 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes);
1127 /* NB: m is reclaimed by ipsec_process_done. */
1128 error = ipsec_process_done(m, sp, sav, idx);
1132 crypto_freereq(crp);
1138 static struct xformsw ah_xformsw = {
1140 .xf_name = "IPsec AH",
1142 .xf_zeroize = ah_zeroize,
1143 .xf_input = ah_input,
1144 .xf_output = ah_output,
1147 SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
1148 xform_attach, &ah_xformsw);
1149 SYSUNINIT(ah_xform_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
1150 xform_detach, &ah_xformsw);