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 */
111 static struct timeval md5warn, ripewarn, kpdkmd5warn, kpdksha1warn;
112 static struct timeval warninterval = { .tv_sec = 1, .tv_usec = 0 };
114 static int ah_input_cb(struct cryptop*);
115 static int ah_output_cb(struct cryptop*);
118 xform_ah_authsize(const struct auth_hash *esph)
125 switch (esph->type) {
126 case CRYPTO_SHA2_256_HMAC:
127 case CRYPTO_SHA2_384_HMAC:
128 case CRYPTO_SHA2_512_HMAC:
129 alen = esph->hashsize / 2; /* RFC4868 2.3 */
132 case CRYPTO_AES_128_NIST_GMAC:
133 case CRYPTO_AES_192_NIST_GMAC:
134 case CRYPTO_AES_256_NIST_GMAC:
135 alen = esph->hashsize;
139 alen = AH_HMAC_HASHLEN;
147 ah_hdrsiz(struct secasvar *sav)
152 int authsize, rplen, align;
154 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, ("null xform"));
155 /*XXX not right for null algorithm--does it matter??*/
157 /* RFC4302: use the correct alignment. */
158 align = sizeof(uint32_t);
160 if (sav->sah->saidx.dst.sa.sa_family == AF_INET6) {
161 align = sizeof(uint64_t);
164 rplen = HDRSIZE(sav);
165 authsize = AUTHSIZE(sav);
166 size = roundup(rplen + authsize, align);
169 size = sizeof (struct ah) + sizeof (u_int32_t) + 16;
175 * NB: public for use by esp_init.
178 ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria)
180 const struct auth_hash *thash;
183 thash = auth_algorithm_lookup(sav->alg_auth);
185 DPRINTF(("%s: unsupported authentication algorithm %u\n",
186 __func__, sav->alg_auth));
190 switch (sav->alg_auth) {
191 case SADB_AALG_MD5HMAC:
192 if (ratecheck(&md5warn, &warninterval))
193 gone_in(13, "MD5-HMAC authenticator for IPsec");
195 case SADB_X_AALG_RIPEMD160HMAC:
196 if (ratecheck(&ripewarn, &warninterval))
197 gone_in(13, "RIPEMD160-HMAC authenticator for IPsec");
199 case SADB_X_AALG_MD5:
200 if (ratecheck(&kpdkmd5warn, &warninterval))
201 gone_in(13, "Keyed-MD5 authenticator for IPsec");
203 case SADB_X_AALG_SHA:
204 if (ratecheck(&kpdksha1warn, &warninterval))
205 gone_in(13, "Keyed-SHA1 authenticator for IPsec");
210 * Verify the replay state block allocation is consistent with
211 * the protocol type. We check here so we can make assumptions
212 * later during protocol processing.
214 /* NB: replay state is setup elsewhere (sigh) */
215 if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) {
216 DPRINTF(("%s: replay state block inconsistency, "
217 "%s algorithm %s replay state\n", __func__,
218 (sav->flags & SADB_X_EXT_OLD) ? "old" : "new",
219 sav->replay == NULL ? "without" : "with"));
222 if (sav->key_auth == NULL) {
223 DPRINTF(("%s: no authentication key for %s algorithm\n",
224 __func__, thash->name));
227 keylen = _KEYLEN(sav->key_auth);
228 if (keylen > thash->keysize && thash->keysize != 0) {
229 DPRINTF(("%s: invalid keylength %d, algorithm %s requires "
230 "keysize less than %d\n", __func__,
231 keylen, thash->name, thash->keysize));
235 sav->tdb_xform = xsp;
236 sav->tdb_authalgxform = thash;
238 /* Initialize crypto session. */
239 bzero(cria, sizeof (*cria));
240 cria->cri_alg = sav->tdb_authalgxform->type;
241 cria->cri_klen = _KEYBITS(sav->key_auth);
242 cria->cri_key = sav->key_auth->key_data;
243 cria->cri_mlen = AUTHSIZE(sav);
249 * ah_init() is called when an SPI is being set up.
252 ah_init(struct secasvar *sav, struct xformsw *xsp)
254 struct cryptoini cria;
257 error = ah_init0(sav, xsp, &cria);
258 return error ? error :
259 crypto_newsession(&sav->tdb_cryptoid, &cria, V_crypto_support);
265 * NB: public for use by esp_zeroize (XXX).
268 ah_zeroize(struct secasvar *sav)
272 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
274 crypto_freesession(sav->tdb_cryptoid);
275 sav->tdb_cryptoid = NULL;
276 sav->tdb_authalgxform = NULL;
277 sav->tdb_xform = NULL;
282 * Massage IPv4/IPv6 headers for AH processing.
285 ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out)
287 struct mbuf *m = *m0;
296 struct ip6_ext *ip6e;
298 int ad, alloc, nxt, noff;
305 * This is the least painful way of dealing with IPv4 header
306 * and option processing -- just make sure they're in
309 *m0 = m = m_pullup(m, skip);
311 DPRINTF(("%s: m_pullup failed\n", __func__));
315 /* Fix the IP header */
316 ip = mtod(m, struct ip *);
322 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
323 ip->ip_off &= htons(IP_DF);
325 ip->ip_off = htons(0);
327 ptr = mtod(m, unsigned char *);
329 /* IPv4 option processing */
330 for (off = sizeof(struct ip); off < skip;) {
331 if (ptr[off] == IPOPT_EOL || ptr[off] == IPOPT_NOP ||
335 DPRINTF(("%s: illegal IPv4 option length for "
336 "option %d\n", __func__, ptr[off]));
344 off = skip; /* End the loop. */
351 case IPOPT_SECURITY: /* 0x82 */
352 case 0x85: /* Extended security. */
353 case 0x86: /* Commercial security. */
354 case 0x94: /* Router alert */
355 case 0x95: /* RFC1770 */
356 /* Sanity check for option length. */
357 if (ptr[off + 1] < 2) {
358 DPRINTF(("%s: illegal IPv4 option "
359 "length for option %d\n",
360 __func__, ptr[off]));
371 /* Sanity check for option length. */
372 if (ptr[off + 1] < 2) {
373 DPRINTF(("%s: illegal IPv4 option "
374 "length for option %d\n",
375 __func__, ptr[off]));
382 * On output, if we have either of the
383 * source routing options, we should
384 * swap the destination address of the
385 * IP header with the last address
386 * specified in the option, as that is
387 * what the destination's IP header
391 bcopy(ptr + off + ptr[off + 1] -
392 sizeof(struct in_addr),
393 &(ip->ip_dst), sizeof(struct in_addr));
397 /* Sanity check for option length. */
398 if (ptr[off + 1] < 2) {
399 DPRINTF(("%s: illegal IPv4 option "
400 "length for option %d\n",
401 __func__, ptr[off]));
406 /* Zeroize all other options. */
407 count = ptr[off + 1];
408 bcopy(ipseczeroes, ptr + off, count);
415 DPRINTF(("%s: malformed IPv4 options header\n",
427 case AF_INET6: /* Ugly... */
428 /* Copy and "cook" the IPv6 header. */
429 m_copydata(m, 0, sizeof(ip6), (caddr_t) &ip6);
431 /* We don't do IPv6 Jumbograms. */
432 if (ip6.ip6_plen == 0) {
433 DPRINTF(("%s: unsupported IPv6 jumbogram\n", __func__));
440 ip6.ip6_vfc &= ~IPV6_VERSION_MASK;
441 ip6.ip6_vfc |= IPV6_VERSION;
443 /* Scoped address handling. */
444 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src))
445 ip6.ip6_src.s6_addr16[1] = 0;
446 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst))
447 ip6.ip6_dst.s6_addr16[1] = 0;
449 /* Done with IPv6 header. */
450 m_copyback(m, 0, sizeof(struct ip6_hdr), (caddr_t) &ip6);
452 /* Let's deal with the remaining headers (if any). */
453 if (skip - sizeof(struct ip6_hdr) > 0) {
454 if (m->m_len <= skip) {
455 ptr = (unsigned char *) malloc(
456 skip - sizeof(struct ip6_hdr),
459 DPRINTF(("%s: failed to allocate memory"
460 "for IPv6 headers\n",__func__));
466 * Copy all the protocol headers after
469 m_copydata(m, sizeof(struct ip6_hdr),
470 skip - sizeof(struct ip6_hdr), ptr);
473 /* No need to allocate memory. */
474 ptr = mtod(m, unsigned char *) +
475 sizeof(struct ip6_hdr);
481 nxt = ip6.ip6_nxt & 0xff; /* Next header type. */
483 for (off = 0; off < skip - sizeof(struct ip6_hdr);)
485 case IPPROTO_HOPOPTS:
486 case IPPROTO_DSTOPTS:
487 ip6e = (struct ip6_ext *)(ptr + off);
488 noff = off + ((ip6e->ip6e_len + 1) << 3);
491 if (noff > skip - sizeof(struct ip6_hdr))
495 * Zero out mutable options.
497 for (count = off + sizeof(struct ip6_ext);
499 if (ptr[count] == IP6OPT_PAD1) {
501 continue; /* Skip padding. */
504 ad = ptr[count + 1] + 2;
505 if (count + ad > noff)
508 if (ptr[count] & IP6OPT_MUTABLE)
509 memset(ptr + count, 0, ad);
517 off += ((ip6e->ip6e_len + 1) << 3);
518 nxt = ip6e->ip6e_nxt;
521 case IPPROTO_ROUTING:
523 * Always include routing headers in
526 ip6e = (struct ip6_ext *) (ptr + off);
527 off += ((ip6e->ip6e_len + 1) << 3);
528 nxt = ip6e->ip6e_nxt;
532 DPRINTF(("%s: unexpected IPv6 header type %d",
541 /* Copyback and free, if we allocated. */
543 m_copyback(m, sizeof(struct ip6_hdr),
544 skip - sizeof(struct ip6_hdr), ptr);
556 * ah_input() gets called to verify that an input packet
557 * passes authentication.
560 ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
562 IPSEC_DEBUG_DECLARE(char buf[128]);
563 const struct auth_hash *ahx;
564 struct cryptodesc *crda;
566 struct xform_data *xd;
568 crypto_session_t cryptoid;
569 int hl, rplen, authsize, ahsize, error;
571 IPSEC_ASSERT(sav != NULL, ("null SA"));
572 IPSEC_ASSERT(sav->key_auth != NULL, ("null authentication key"));
573 IPSEC_ASSERT(sav->tdb_authalgxform != NULL,
574 ("null authentication xform"));
576 /* Figure out header size. */
577 rplen = HDRSIZE(sav);
579 /* XXX don't pullup, just copy header */
580 IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen);
582 DPRINTF(("ah_input: cannot pullup header\n"));
583 AHSTAT_INC(ahs_hdrops); /*XXX*/
588 /* Check replay window, if applicable. */
590 if (sav->replay != NULL && sav->replay->wsize != 0 &&
591 ipsec_chkreplay(ntohl(ah->ah_seq), sav) == 0) {
592 SECASVAR_UNLOCK(sav);
593 AHSTAT_INC(ahs_replay);
594 DPRINTF(("%s: packet replay failure: %s\n", __func__,
595 ipsec_sa2str(sav, buf, sizeof(buf))));
599 cryptoid = sav->tdb_cryptoid;
600 SECASVAR_UNLOCK(sav);
602 /* Verify AH header length. */
603 hl = sizeof(struct ah) + (ah->ah_len * sizeof (u_int32_t));
604 ahx = sav->tdb_authalgxform;
605 authsize = AUTHSIZE(sav);
606 ahsize = ah_hdrsiz(sav);
608 DPRINTF(("%s: bad authenticator length %u (expecting %lu)"
609 " for packet in SA %s/%08lx\n", __func__, hl,
611 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
612 (u_long) ntohl(sav->spi)));
613 AHSTAT_INC(ahs_badauthl);
617 if (skip + ahsize > m->m_pkthdr.len) {
618 DPRINTF(("%s: bad mbuf length %u (expecting %lu)"
619 " for packet in SA %s/%08lx\n", __func__,
620 m->m_pkthdr.len, (u_long)(skip + ahsize),
621 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
622 (u_long) ntohl(sav->spi)));
623 AHSTAT_INC(ahs_badauthl);
627 AHSTAT_ADD(ahs_ibytes, m->m_pkthdr.len - skip - hl);
629 /* Get crypto descriptors. */
630 crp = crypto_getreq(1);
632 DPRINTF(("%s: failed to acquire crypto descriptor\n",
634 AHSTAT_INC(ahs_crypto);
639 crda = crp->crp_desc;
640 IPSEC_ASSERT(crda != NULL, ("null crypto descriptor"));
643 crda->crd_len = m->m_pkthdr.len;
644 crda->crd_inject = skip + rplen;
646 /* Authentication operation. */
647 crda->crd_alg = ahx->type;
648 crda->crd_klen = _KEYBITS(sav->key_auth);
649 crda->crd_key = sav->key_auth->key_data;
651 /* Allocate IPsec-specific opaque crypto info. */
652 xd = malloc(sizeof(*xd) + skip + rplen + authsize, M_XDATA,
655 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
656 AHSTAT_INC(ahs_crypto);
663 * Save the authenticator, the skipped portion of the packet,
666 m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(xd + 1));
668 /* Zeroize the authenticator on the packet. */
669 m_copyback(m, skip + rplen, authsize, ipseczeroes);
671 /* Save ah_nxt, since ah pointer can become invalid after "massage" */
674 /* "Massage" the packet headers for crypto processing. */
675 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
678 /* NB: mbuf is free'd by ah_massage_headers */
679 AHSTAT_INC(ahs_hdrops);
686 /* Crypto operation descriptor. */
687 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
688 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
690 crp->crp_flags |= CRYPTO_F_ASYNC | CRYPTO_F_ASYNC_KEEPORDER;
691 crp->crp_buf = (caddr_t) m;
692 crp->crp_callback = ah_input_cb;
693 crp->crp_session = cryptoid;
694 crp->crp_opaque = (caddr_t) xd;
696 /* These are passed as-is to the callback. */
699 xd->protoff = protoff;
701 xd->cryptoid = cryptoid;
703 return (crypto_dispatch(crp));
711 * AH input callback from the crypto driver.
714 ah_input_cb(struct cryptop *crp)
716 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
717 unsigned char calc[AH_ALEN_MAX];
719 struct xform_data *xd;
720 struct secasvar *sav;
721 struct secasindex *saidx;
723 crypto_session_t cryptoid;
724 int authsize, rplen, ahsize, error, skip, protoff;
727 m = (struct mbuf *) crp->crp_buf;
728 xd = (struct xform_data *) crp->crp_opaque;
729 CURVNET_SET(xd->vnet);
733 protoff = xd->protoff;
734 cryptoid = xd->cryptoid;
735 saidx = &sav->sah->saidx;
736 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
737 saidx->dst.sa.sa_family == AF_INET6,
738 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
740 /* Check for crypto errors. */
741 if (crp->crp_etype) {
742 if (crp->crp_etype == EAGAIN) {
743 /* Reset the session ID */
744 if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
745 crypto_freesession(cryptoid);
746 xd->cryptoid = crp->crp_session;
748 return (crypto_dispatch(crp));
750 AHSTAT_INC(ahs_noxform);
751 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
752 error = crp->crp_etype;
755 AHSTAT_INC(ahs_hist[sav->alg_auth]);
756 crypto_freereq(crp); /* No longer needed. */
760 /* Shouldn't happen... */
762 AHSTAT_INC(ahs_crypto);
763 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
768 /* Figure out header size. */
769 rplen = HDRSIZE(sav);
770 authsize = AUTHSIZE(sav);
771 ahsize = ah_hdrsiz(sav);
773 /* Copy authenticator off the packet. */
774 m_copydata(m, skip + rplen, authsize, calc);
776 /* Verify authenticator. */
777 ptr = (caddr_t) (xd + 1);
778 if (timingsafe_bcmp(ptr + skip + rplen, calc, authsize)) {
779 DPRINTF(("%s: authentication hash mismatch for packet "
780 "in SA %s/%08lx\n", __func__,
781 ipsec_address(&saidx->dst, buf, sizeof(buf)),
782 (u_long) ntohl(sav->spi)));
783 AHSTAT_INC(ahs_badauth);
787 /* Fix the Next Protocol field. */
788 ((uint8_t *) ptr)[protoff] = nxt;
790 /* Copyback the saved (uncooked) network headers. */
791 m_copyback(m, 0, skip, ptr);
792 free(xd, M_XDATA), xd = NULL; /* No longer needed */
795 * Header is now authenticated.
797 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM;
800 * Update replay sequence number, if appropriate.
805 m_copydata(m, skip + offsetof(struct newah, ah_seq),
806 sizeof (seq), (caddr_t) &seq);
808 if (ipsec_updatereplay(ntohl(seq), sav)) {
809 SECASVAR_UNLOCK(sav);
810 AHSTAT_INC(ahs_replay);
814 SECASVAR_UNLOCK(sav);
818 * Remove the AH header and authenticator from the mbuf.
820 error = m_striphdr(m, skip, ahsize);
822 DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__,
823 ipsec_address(&saidx->dst, buf, sizeof(buf)),
824 (u_long) ntohl(sav->spi)));
825 AHSTAT_INC(ahs_hdrops);
829 switch (saidx->dst.sa.sa_family) {
832 error = ipsec6_common_input_cb(m, sav, skip, protoff);
837 error = ipsec4_common_input_cb(m, sav, skip, protoff);
841 panic("%s: Unexpected address family: %d saidx=%p", __func__,
842 saidx->dst.sa.sa_family, saidx);
860 * AH output routine, called by ipsec[46]_perform_request().
863 ah_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
864 u_int idx, int skip, int protoff)
866 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
867 const struct auth_hash *ahx;
868 struct cryptodesc *crda;
869 struct xform_data *xd;
873 crypto_session_t cryptoid;
875 int error, rplen, authsize, ahsize, maxpacketsize, roff;
878 IPSEC_ASSERT(sav != NULL, ("null SA"));
879 ahx = sav->tdb_authalgxform;
880 IPSEC_ASSERT(ahx != NULL, ("null authentication xform"));
882 AHSTAT_INC(ahs_output);
884 /* Figure out header size. */
885 rplen = HDRSIZE(sav);
886 authsize = AUTHSIZE(sav);
887 ahsize = ah_hdrsiz(sav);
889 /* Check for maximum packet size violations. */
890 switch (sav->sah->saidx.dst.sa.sa_family) {
893 maxpacketsize = IP_MAXPACKET;
898 maxpacketsize = IPV6_MAXPACKET;
902 DPRINTF(("%s: unknown/unsupported protocol family %u, "
903 "SA %s/%08lx\n", __func__,
904 sav->sah->saidx.dst.sa.sa_family,
905 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
906 (u_long) ntohl(sav->spi)));
907 AHSTAT_INC(ahs_nopf);
908 error = EPFNOSUPPORT;
911 if (ahsize + m->m_pkthdr.len > maxpacketsize) {
912 DPRINTF(("%s: packet in SA %s/%08lx got too big "
913 "(len %u, max len %u)\n", __func__,
914 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
915 (u_long) ntohl(sav->spi),
916 ahsize + m->m_pkthdr.len, maxpacketsize));
917 AHSTAT_INC(ahs_toobig);
922 /* Update the counters. */
923 AHSTAT_ADD(ahs_obytes, m->m_pkthdr.len - skip);
925 m = m_unshare(m, M_NOWAIT);
927 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
928 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
929 (u_long) ntohl(sav->spi)));
930 AHSTAT_INC(ahs_hdrops);
935 /* Inject AH header. */
936 mi = m_makespace(m, skip, ahsize, &roff);
938 DPRINTF(("%s: failed to inject %u byte AH header for SA "
939 "%s/%08lx\n", __func__, ahsize,
940 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
941 (u_long) ntohl(sav->spi)));
942 AHSTAT_INC(ahs_hdrops); /*XXX differs from openbsd */
948 * The AH header is guaranteed by m_makespace() to be in
949 * contiguous memory, at roff bytes offset into the returned mbuf.
951 ah = (struct newah *)(mtod(mi, caddr_t) + roff);
953 /* Initialize the AH header. */
954 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt);
955 ah->ah_len = (ahsize - sizeof(struct ah)) / sizeof(u_int32_t);
957 ah->ah_spi = sav->spi;
959 /* Zeroize authenticator. */
960 m_copyback(m, skip + rplen, authsize, ipseczeroes);
962 /* Zeroize padding */
963 m_copyback(m, skip + rplen + authsize, ahsize - (rplen + authsize),
966 /* Insert packet replay counter, as requested. */
969 if (sav->replay->count == ~0 &&
970 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
971 SECASVAR_UNLOCK(sav);
972 DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n",
973 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
974 sizeof(buf)), (u_long) ntohl(sav->spi)));
975 AHSTAT_INC(ahs_wrap);
980 /* Emulate replay attack when ipsec_replay is TRUE. */
983 sav->replay->count++;
984 ah->ah_seq = htonl(sav->replay->count);
986 cryptoid = sav->tdb_cryptoid;
987 SECASVAR_UNLOCK(sav);
989 /* Get crypto descriptors. */
990 crp = crypto_getreq(1);
992 DPRINTF(("%s: failed to acquire crypto descriptors\n",
994 AHSTAT_INC(ahs_crypto);
999 crda = crp->crp_desc;
1001 crda->crd_inject = skip + rplen;
1002 crda->crd_len = m->m_pkthdr.len;
1004 /* Authentication operation. */
1005 crda->crd_alg = ahx->type;
1006 crda->crd_key = sav->key_auth->key_data;
1007 crda->crd_klen = _KEYBITS(sav->key_auth);
1009 /* Allocate IPsec-specific opaque crypto info. */
1010 xd = malloc(sizeof(struct xform_data) + skip, M_XDATA,
1013 crypto_freereq(crp);
1014 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
1015 AHSTAT_INC(ahs_crypto);
1020 /* Save the skipped portion of the packet. */
1021 m_copydata(m, 0, skip, (caddr_t) (xd + 1));
1024 * Fix IP header length on the header used for
1025 * authentication. We don't need to fix the original
1026 * header length as it will be fixed by our caller.
1028 switch (sav->sah->saidx.dst.sa.sa_family) {
1031 bcopy(((caddr_t)(xd + 1)) +
1032 offsetof(struct ip, ip_len),
1033 (caddr_t) &iplen, sizeof(u_int16_t));
1034 iplen = htons(ntohs(iplen) + ahsize);
1035 m_copyback(m, offsetof(struct ip, ip_len),
1036 sizeof(u_int16_t), (caddr_t) &iplen);
1042 bcopy(((caddr_t)(xd + 1)) +
1043 offsetof(struct ip6_hdr, ip6_plen),
1044 (caddr_t) &iplen, sizeof(uint16_t));
1045 iplen = htons(ntohs(iplen) + ahsize);
1046 m_copyback(m, offsetof(struct ip6_hdr, ip6_plen),
1047 sizeof(uint16_t), (caddr_t) &iplen);
1052 /* Fix the Next Header field in saved header. */
1053 ((uint8_t *) (xd + 1))[protoff] = IPPROTO_AH;
1055 /* Update the Next Protocol field in the IP header. */
1057 m_copyback(m, protoff, sizeof(uint8_t), (caddr_t) &prot);
1059 /* "Massage" the packet headers for crypto processing. */
1060 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
1061 skip, ahx->type, 1);
1063 m = NULL; /* mbuf was free'd by ah_massage_headers. */
1065 crypto_freereq(crp);
1069 /* Crypto operation descriptor. */
1070 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
1071 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
1073 crp->crp_flags |= CRYPTO_F_ASYNC | CRYPTO_F_ASYNC_KEEPORDER;
1074 crp->crp_buf = (caddr_t) m;
1075 crp->crp_callback = ah_output_cb;
1076 crp->crp_session = cryptoid;
1077 crp->crp_opaque = (caddr_t) xd;
1079 /* These are passed as-is to the callback. */
1084 xd->cryptoid = cryptoid;
1087 return crypto_dispatch(crp);
1097 * AH output callback from the crypto driver.
1100 ah_output_cb(struct cryptop *crp)
1102 struct xform_data *xd;
1103 struct secpolicy *sp;
1104 struct secasvar *sav;
1106 crypto_session_t cryptoid;
1111 m = (struct mbuf *) crp->crp_buf;
1112 xd = (struct xform_data *) crp->crp_opaque;
1113 CURVNET_SET(xd->vnet);
1118 cryptoid = xd->cryptoid;
1119 ptr = (caddr_t) (xd + 1);
1121 /* Check for crypto errors. */
1122 if (crp->crp_etype) {
1123 if (crp->crp_etype == EAGAIN) {
1124 /* Reset the session ID */
1125 if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
1126 crypto_freesession(cryptoid);
1127 xd->cryptoid = crp->crp_session;
1129 return (crypto_dispatch(crp));
1131 AHSTAT_INC(ahs_noxform);
1132 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
1133 error = crp->crp_etype;
1138 /* Shouldn't happen... */
1140 AHSTAT_INC(ahs_crypto);
1141 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
1146 * Copy original headers (with the new protocol number) back
1149 m_copyback(m, 0, skip, ptr);
1152 crypto_freereq(crp);
1153 AHSTAT_INC(ahs_hist[sav->alg_auth]);
1155 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
1156 if (V_ipsec_integrity) {
1160 * Corrupt HMAC if we want to test integrity verification of
1163 alen = AUTHSIZE(sav);
1164 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes);
1168 /* NB: m is reclaimed by ipsec_process_done. */
1169 error = ipsec_process_done(m, sp, sav, idx);
1175 crypto_freereq(crp);
1181 static struct xformsw ah_xformsw = {
1183 .xf_name = "IPsec AH",
1185 .xf_zeroize = ah_zeroize,
1186 .xf_input = ah_input,
1187 .xf_output = ah_output,
1190 SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
1191 xform_attach, &ah_xformsw);
1192 SYSUNINIT(ah_xform_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
1193 xform_detach, &ah_xformsw);