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)
153 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, ("null xform"));
154 /*XXX not right for null algorithm--does it matter??*/
155 authsize = AUTHSIZE(sav);
156 size = roundup(authsize, sizeof (u_int32_t)) + HDRSIZE(sav);
159 size = sizeof (struct ah) + sizeof (u_int32_t) + 16;
165 * NB: public for use by esp_init.
168 ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria)
170 const struct auth_hash *thash;
173 thash = auth_algorithm_lookup(sav->alg_auth);
175 DPRINTF(("%s: unsupported authentication algorithm %u\n",
176 __func__, sav->alg_auth));
180 switch (sav->alg_auth) {
181 case SADB_AALG_MD5HMAC:
182 if (ratecheck(&md5warn, &warninterval))
183 gone_in(13, "MD5-HMAC authenticator for IPsec");
185 case SADB_X_AALG_RIPEMD160HMAC:
186 if (ratecheck(&ripewarn, &warninterval))
187 gone_in(13, "RIPEMD160-HMAC authenticator for IPsec");
189 case SADB_X_AALG_MD5:
190 if (ratecheck(&kpdkmd5warn, &warninterval))
191 gone_in(13, "Keyed-MD5 authenticator for IPsec");
193 case SADB_X_AALG_SHA:
194 if (ratecheck(&kpdksha1warn, &warninterval))
195 gone_in(13, "Keyed-SHA1 authenticator for IPsec");
200 * Verify the replay state block allocation is consistent with
201 * the protocol type. We check here so we can make assumptions
202 * later during protocol processing.
204 /* NB: replay state is setup elsewhere (sigh) */
205 if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) {
206 DPRINTF(("%s: replay state block inconsistency, "
207 "%s algorithm %s replay state\n", __func__,
208 (sav->flags & SADB_X_EXT_OLD) ? "old" : "new",
209 sav->replay == NULL ? "without" : "with"));
212 if (sav->key_auth == NULL) {
213 DPRINTF(("%s: no authentication key for %s algorithm\n",
214 __func__, thash->name));
217 keylen = _KEYLEN(sav->key_auth);
218 if (keylen != thash->keysize && thash->keysize != 0) {
219 DPRINTF(("%s: invalid keylength %d, algorithm %s requires "
220 "keysize %d\n", __func__,
221 keylen, thash->name, thash->keysize));
225 sav->tdb_xform = xsp;
226 sav->tdb_authalgxform = thash;
228 /* Initialize crypto session. */
229 bzero(cria, sizeof (*cria));
230 cria->cri_alg = sav->tdb_authalgxform->type;
231 cria->cri_klen = _KEYBITS(sav->key_auth);
232 cria->cri_key = sav->key_auth->key_data;
233 cria->cri_mlen = AUTHSIZE(sav);
239 * ah_init() is called when an SPI is being set up.
242 ah_init(struct secasvar *sav, struct xformsw *xsp)
244 struct cryptoini cria;
247 error = ah_init0(sav, xsp, &cria);
248 return error ? error :
249 crypto_newsession(&sav->tdb_cryptoid, &cria, V_crypto_support);
255 * NB: public for use by esp_zeroize (XXX).
258 ah_zeroize(struct secasvar *sav)
263 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
265 err = crypto_freesession(sav->tdb_cryptoid);
266 sav->tdb_cryptoid = 0;
267 sav->tdb_authalgxform = NULL;
268 sav->tdb_xform = NULL;
273 * Massage IPv4/IPv6 headers for AH processing.
276 ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out)
278 struct mbuf *m = *m0;
287 struct ip6_ext *ip6e;
289 int ad, alloc, nxt, noff;
296 * This is the least painful way of dealing with IPv4 header
297 * and option processing -- just make sure they're in
300 *m0 = m = m_pullup(m, skip);
302 DPRINTF(("%s: m_pullup failed\n", __func__));
306 /* Fix the IP header */
307 ip = mtod(m, struct ip *);
313 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
314 ip->ip_off &= htons(IP_DF);
316 ip->ip_off = htons(0);
318 ptr = mtod(m, unsigned char *);
320 /* IPv4 option processing */
321 for (off = sizeof(struct ip); off < skip;) {
322 if (ptr[off] == IPOPT_EOL || ptr[off] == IPOPT_NOP ||
326 DPRINTF(("%s: illegal IPv4 option length for "
327 "option %d\n", __func__, ptr[off]));
335 off = skip; /* End the loop. */
342 case IPOPT_SECURITY: /* 0x82 */
343 case 0x85: /* Extended security. */
344 case 0x86: /* Commercial security. */
345 case 0x94: /* Router alert */
346 case 0x95: /* RFC1770 */
347 /* Sanity check for option length. */
348 if (ptr[off + 1] < 2) {
349 DPRINTF(("%s: illegal IPv4 option "
350 "length for option %d\n",
351 __func__, ptr[off]));
362 /* Sanity check for option length. */
363 if (ptr[off + 1] < 2) {
364 DPRINTF(("%s: illegal IPv4 option "
365 "length for option %d\n",
366 __func__, ptr[off]));
373 * On output, if we have either of the
374 * source routing options, we should
375 * swap the destination address of the
376 * IP header with the last address
377 * specified in the option, as that is
378 * what the destination's IP header
382 bcopy(ptr + off + ptr[off + 1] -
383 sizeof(struct in_addr),
384 &(ip->ip_dst), sizeof(struct in_addr));
388 /* Sanity check for option length. */
389 if (ptr[off + 1] < 2) {
390 DPRINTF(("%s: illegal IPv4 option "
391 "length for option %d\n",
392 __func__, ptr[off]));
397 /* Zeroize all other options. */
398 count = ptr[off + 1];
399 bcopy(ipseczeroes, ptr + off, count);
406 DPRINTF(("%s: malformed IPv4 options header\n",
418 case AF_INET6: /* Ugly... */
419 /* Copy and "cook" the IPv6 header. */
420 m_copydata(m, 0, sizeof(ip6), (caddr_t) &ip6);
422 /* We don't do IPv6 Jumbograms. */
423 if (ip6.ip6_plen == 0) {
424 DPRINTF(("%s: unsupported IPv6 jumbogram\n", __func__));
431 ip6.ip6_vfc &= ~IPV6_VERSION_MASK;
432 ip6.ip6_vfc |= IPV6_VERSION;
434 /* Scoped address handling. */
435 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src))
436 ip6.ip6_src.s6_addr16[1] = 0;
437 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst))
438 ip6.ip6_dst.s6_addr16[1] = 0;
440 /* Done with IPv6 header. */
441 m_copyback(m, 0, sizeof(struct ip6_hdr), (caddr_t) &ip6);
443 /* Let's deal with the remaining headers (if any). */
444 if (skip - sizeof(struct ip6_hdr) > 0) {
445 if (m->m_len <= skip) {
446 ptr = (unsigned char *) malloc(
447 skip - sizeof(struct ip6_hdr),
450 DPRINTF(("%s: failed to allocate memory"
451 "for IPv6 headers\n",__func__));
457 * Copy all the protocol headers after
460 m_copydata(m, sizeof(struct ip6_hdr),
461 skip - sizeof(struct ip6_hdr), ptr);
464 /* No need to allocate memory. */
465 ptr = mtod(m, unsigned char *) +
466 sizeof(struct ip6_hdr);
472 nxt = ip6.ip6_nxt & 0xff; /* Next header type. */
474 for (off = 0; off < skip - sizeof(struct ip6_hdr);)
476 case IPPROTO_HOPOPTS:
477 case IPPROTO_DSTOPTS:
478 ip6e = (struct ip6_ext *)(ptr + off);
479 noff = off + ((ip6e->ip6e_len + 1) << 3);
482 if (noff > skip - sizeof(struct ip6_hdr))
486 * Zero out mutable options.
488 for (count = off + sizeof(struct ip6_ext);
490 if (ptr[count] == IP6OPT_PAD1) {
492 continue; /* Skip padding. */
495 ad = ptr[count + 1] + 2;
496 if (count + ad > noff)
499 if (ptr[count] & IP6OPT_MUTABLE)
500 memset(ptr + count, 0, ad);
508 off += ((ip6e->ip6e_len + 1) << 3);
509 nxt = ip6e->ip6e_nxt;
512 case IPPROTO_ROUTING:
514 * Always include routing headers in
517 ip6e = (struct ip6_ext *) (ptr + off);
518 off += ((ip6e->ip6e_len + 1) << 3);
519 nxt = ip6e->ip6e_nxt;
523 DPRINTF(("%s: unexpected IPv6 header type %d",
532 /* Copyback and free, if we allocated. */
534 m_copyback(m, sizeof(struct ip6_hdr),
535 skip - sizeof(struct ip6_hdr), ptr);
547 * ah_input() gets called to verify that an input packet
548 * passes authentication.
551 ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
553 IPSEC_DEBUG_DECLARE(char buf[128]);
554 const struct auth_hash *ahx;
555 struct cryptodesc *crda;
557 struct xform_data *xd;
560 int hl, rplen, authsize, error;
562 IPSEC_ASSERT(sav != NULL, ("null SA"));
563 IPSEC_ASSERT(sav->key_auth != NULL, ("null authentication key"));
564 IPSEC_ASSERT(sav->tdb_authalgxform != NULL,
565 ("null authentication xform"));
567 /* Figure out header size. */
568 rplen = HDRSIZE(sav);
570 /* XXX don't pullup, just copy header */
571 IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen);
573 DPRINTF(("ah_input: cannot pullup header\n"));
574 AHSTAT_INC(ahs_hdrops); /*XXX*/
579 /* Check replay window, if applicable. */
581 if (sav->replay != NULL && sav->replay->wsize != 0 &&
582 ipsec_chkreplay(ntohl(ah->ah_seq), sav) == 0) {
583 SECASVAR_UNLOCK(sav);
584 AHSTAT_INC(ahs_replay);
585 DPRINTF(("%s: packet replay failure: %s\n", __func__,
586 ipsec_sa2str(sav, buf, sizeof(buf))));
590 cryptoid = sav->tdb_cryptoid;
591 SECASVAR_UNLOCK(sav);
593 /* Verify AH header length. */
594 hl = ah->ah_len * sizeof (u_int32_t);
595 ahx = sav->tdb_authalgxform;
596 authsize = AUTHSIZE(sav);
597 if (hl != authsize + rplen - sizeof (struct ah)) {
598 DPRINTF(("%s: bad authenticator length %u (expecting %lu)"
599 " for packet in SA %s/%08lx\n", __func__, hl,
600 (u_long) (authsize + rplen - sizeof (struct ah)),
601 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
602 (u_long) ntohl(sav->spi)));
603 AHSTAT_INC(ahs_badauthl);
607 if (skip + authsize + rplen > m->m_pkthdr.len) {
608 DPRINTF(("%s: bad mbuf length %u (expecting %lu)"
609 " for packet in SA %s/%08lx\n", __func__,
610 m->m_pkthdr.len, (u_long) (skip + authsize + rplen),
611 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
612 (u_long) ntohl(sav->spi)));
613 AHSTAT_INC(ahs_badauthl);
617 AHSTAT_ADD(ahs_ibytes, m->m_pkthdr.len - skip - hl);
619 /* Get crypto descriptors. */
620 crp = crypto_getreq(1);
622 DPRINTF(("%s: failed to acquire crypto descriptor\n",
624 AHSTAT_INC(ahs_crypto);
629 crda = crp->crp_desc;
630 IPSEC_ASSERT(crda != NULL, ("null crypto descriptor"));
633 crda->crd_len = m->m_pkthdr.len;
634 crda->crd_inject = skip + rplen;
636 /* Authentication operation. */
637 crda->crd_alg = ahx->type;
638 crda->crd_klen = _KEYBITS(sav->key_auth);
639 crda->crd_key = sav->key_auth->key_data;
641 /* Allocate IPsec-specific opaque crypto info. */
642 xd = malloc(sizeof(*xd) + skip + rplen + authsize, M_XDATA,
645 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
646 AHSTAT_INC(ahs_crypto);
653 * Save the authenticator, the skipped portion of the packet,
656 m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(xd + 1));
658 /* Zeroize the authenticator on the packet. */
659 m_copyback(m, skip + rplen, authsize, ipseczeroes);
661 /* Save ah_nxt, since ah pointer can become invalid after "massage" */
664 /* "Massage" the packet headers for crypto processing. */
665 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
668 /* NB: mbuf is free'd by ah_massage_headers */
669 AHSTAT_INC(ahs_hdrops);
676 /* Crypto operation descriptor. */
677 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
678 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
679 crp->crp_buf = (caddr_t) m;
680 crp->crp_callback = ah_input_cb;
681 crp->crp_sid = cryptoid;
682 crp->crp_opaque = (caddr_t) xd;
684 /* These are passed as-is to the callback. */
687 xd->protoff = protoff;
689 xd->cryptoid = cryptoid;
691 return (crypto_dispatch(crp));
699 * AH input callback from the crypto driver.
702 ah_input_cb(struct cryptop *crp)
704 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
705 unsigned char calc[AH_ALEN_MAX];
706 const struct auth_hash *ahx;
708 struct cryptodesc *crd;
709 struct xform_data *xd;
710 struct secasvar *sav;
711 struct secasindex *saidx;
714 int authsize, rplen, error, skip, protoff;
718 m = (struct mbuf *) crp->crp_buf;
719 xd = (struct xform_data *) crp->crp_opaque;
720 CURVNET_SET(xd->vnet);
724 protoff = xd->protoff;
725 cryptoid = xd->cryptoid;
726 saidx = &sav->sah->saidx;
727 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
728 saidx->dst.sa.sa_family == AF_INET6,
729 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
731 ahx = sav->tdb_authalgxform;
733 /* Check for crypto errors. */
734 if (crp->crp_etype) {
735 if (crp->crp_etype == EAGAIN) {
736 /* Reset the session ID */
737 if (ipsec_updateid(sav, &crp->crp_sid, &cryptoid) != 0)
738 crypto_freesession(cryptoid);
739 xd->cryptoid = crp->crp_sid;
741 return (crypto_dispatch(crp));
743 AHSTAT_INC(ahs_noxform);
744 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
745 error = crp->crp_etype;
748 AHSTAT_INC(ahs_hist[sav->alg_auth]);
749 crypto_freereq(crp); /* No longer needed. */
753 /* Shouldn't happen... */
755 AHSTAT_INC(ahs_crypto);
756 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
761 /* Figure out header size. */
762 rplen = HDRSIZE(sav);
763 authsize = AUTHSIZE(sav);
765 /* Copy authenticator off the packet. */
766 m_copydata(m, skip + rplen, authsize, calc);
768 /* Verify authenticator. */
769 ptr = (caddr_t) (xd + 1);
770 if (timingsafe_bcmp(ptr + skip + rplen, calc, authsize)) {
771 DPRINTF(("%s: authentication hash mismatch for packet "
772 "in SA %s/%08lx\n", __func__,
773 ipsec_address(&saidx->dst, buf, sizeof(buf)),
774 (u_long) ntohl(sav->spi)));
775 AHSTAT_INC(ahs_badauth);
779 /* Fix the Next Protocol field. */
780 ((uint8_t *) ptr)[protoff] = nxt;
782 /* Copyback the saved (uncooked) network headers. */
783 m_copyback(m, 0, skip, ptr);
784 free(xd, M_XDATA), xd = NULL; /* No longer needed */
787 * Header is now authenticated.
789 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM;
792 * Update replay sequence number, if appropriate.
797 m_copydata(m, skip + offsetof(struct newah, ah_seq),
798 sizeof (seq), (caddr_t) &seq);
800 if (ipsec_updatereplay(ntohl(seq), sav)) {
801 SECASVAR_UNLOCK(sav);
802 AHSTAT_INC(ahs_replay);
806 SECASVAR_UNLOCK(sav);
810 * Remove the AH header and authenticator from the mbuf.
812 error = m_striphdr(m, skip, rplen + authsize);
814 DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__,
815 ipsec_address(&saidx->dst, buf, sizeof(buf)),
816 (u_long) ntohl(sav->spi)));
817 AHSTAT_INC(ahs_hdrops);
821 switch (saidx->dst.sa.sa_family) {
824 error = ipsec6_common_input_cb(m, sav, skip, protoff);
829 error = ipsec4_common_input_cb(m, sav, skip, protoff);
833 panic("%s: Unexpected address family: %d saidx=%p", __func__,
834 saidx->dst.sa.sa_family, saidx);
852 * AH output routine, called by ipsec[46]_perform_request().
855 ah_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
856 u_int idx, int skip, int protoff)
858 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
859 const struct auth_hash *ahx;
860 struct cryptodesc *crda;
861 struct xform_data *xd;
867 int error, rplen, authsize, maxpacketsize, roff;
870 IPSEC_ASSERT(sav != NULL, ("null SA"));
871 ahx = sav->tdb_authalgxform;
872 IPSEC_ASSERT(ahx != NULL, ("null authentication xform"));
874 AHSTAT_INC(ahs_output);
876 /* Figure out header size. */
877 rplen = HDRSIZE(sav);
879 /* Check for maximum packet size violations. */
880 switch (sav->sah->saidx.dst.sa.sa_family) {
883 maxpacketsize = IP_MAXPACKET;
888 maxpacketsize = IPV6_MAXPACKET;
892 DPRINTF(("%s: unknown/unsupported protocol family %u, "
893 "SA %s/%08lx\n", __func__,
894 sav->sah->saidx.dst.sa.sa_family,
895 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
896 (u_long) ntohl(sav->spi)));
897 AHSTAT_INC(ahs_nopf);
898 error = EPFNOSUPPORT;
901 authsize = AUTHSIZE(sav);
902 if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) {
903 DPRINTF(("%s: packet in SA %s/%08lx got too big "
904 "(len %u, max len %u)\n", __func__,
905 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
906 (u_long) ntohl(sav->spi),
907 rplen + authsize + m->m_pkthdr.len, maxpacketsize));
908 AHSTAT_INC(ahs_toobig);
913 /* Update the counters. */
914 AHSTAT_ADD(ahs_obytes, m->m_pkthdr.len - skip);
916 m = m_unshare(m, M_NOWAIT);
918 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
919 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
920 (u_long) ntohl(sav->spi)));
921 AHSTAT_INC(ahs_hdrops);
926 /* Inject AH header. */
927 mi = m_makespace(m, skip, rplen + authsize, &roff);
929 DPRINTF(("%s: failed to inject %u byte AH header for SA "
930 "%s/%08lx\n", __func__,
932 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
933 (u_long) ntohl(sav->spi)));
934 AHSTAT_INC(ahs_hdrops); /*XXX differs from openbsd */
940 * The AH header is guaranteed by m_makespace() to be in
941 * contiguous memory, at roff bytes offset into the returned mbuf.
943 ah = (struct newah *)(mtod(mi, caddr_t) + roff);
945 /* Initialize the AH header. */
946 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt);
947 ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(u_int32_t);
949 ah->ah_spi = sav->spi;
951 /* Zeroize authenticator. */
952 m_copyback(m, skip + rplen, authsize, ipseczeroes);
954 /* Insert packet replay counter, as requested. */
957 if (sav->replay->count == ~0 &&
958 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
959 SECASVAR_UNLOCK(sav);
960 DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n",
961 __func__, ipsec_address(&sav->sah->saidx.dst, buf,
962 sizeof(buf)), (u_long) ntohl(sav->spi)));
963 AHSTAT_INC(ahs_wrap);
968 /* Emulate replay attack when ipsec_replay is TRUE. */
971 sav->replay->count++;
972 ah->ah_seq = htonl(sav->replay->count);
974 cryptoid = sav->tdb_cryptoid;
975 SECASVAR_UNLOCK(sav);
977 /* Get crypto descriptors. */
978 crp = crypto_getreq(1);
980 DPRINTF(("%s: failed to acquire crypto descriptors\n",
982 AHSTAT_INC(ahs_crypto);
987 crda = crp->crp_desc;
989 crda->crd_inject = skip + rplen;
990 crda->crd_len = m->m_pkthdr.len;
992 /* Authentication operation. */
993 crda->crd_alg = ahx->type;
994 crda->crd_key = sav->key_auth->key_data;
995 crda->crd_klen = _KEYBITS(sav->key_auth);
997 /* Allocate IPsec-specific opaque crypto info. */
998 xd = malloc(sizeof(struct xform_data) + skip, M_XDATA,
1001 crypto_freereq(crp);
1002 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
1003 AHSTAT_INC(ahs_crypto);
1008 /* Save the skipped portion of the packet. */
1009 m_copydata(m, 0, skip, (caddr_t) (xd + 1));
1012 * Fix IP header length on the header used for
1013 * authentication. We don't need to fix the original
1014 * header length as it will be fixed by our caller.
1016 switch (sav->sah->saidx.dst.sa.sa_family) {
1019 bcopy(((caddr_t)(xd + 1)) +
1020 offsetof(struct ip, ip_len),
1021 (caddr_t) &iplen, sizeof(u_int16_t));
1022 iplen = htons(ntohs(iplen) + rplen + authsize);
1023 m_copyback(m, offsetof(struct ip, ip_len),
1024 sizeof(u_int16_t), (caddr_t) &iplen);
1030 bcopy(((caddr_t)(xd + 1)) +
1031 offsetof(struct ip6_hdr, ip6_plen),
1032 (caddr_t) &iplen, sizeof(uint16_t));
1033 iplen = htons(ntohs(iplen) + rplen + authsize);
1034 m_copyback(m, offsetof(struct ip6_hdr, ip6_plen),
1035 sizeof(uint16_t), (caddr_t) &iplen);
1040 /* Fix the Next Header field in saved header. */
1041 ((uint8_t *) (xd + 1))[protoff] = IPPROTO_AH;
1043 /* Update the Next Protocol field in the IP header. */
1045 m_copyback(m, protoff, sizeof(uint8_t), (caddr_t) &prot);
1047 /* "Massage" the packet headers for crypto processing. */
1048 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
1049 skip, ahx->type, 1);
1051 m = NULL; /* mbuf was free'd by ah_massage_headers. */
1053 crypto_freereq(crp);
1057 /* Crypto operation descriptor. */
1058 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
1059 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
1060 crp->crp_buf = (caddr_t) m;
1061 crp->crp_callback = ah_output_cb;
1062 crp->crp_sid = cryptoid;
1063 crp->crp_opaque = (caddr_t) xd;
1065 /* These are passed as-is to the callback. */
1070 xd->cryptoid = cryptoid;
1073 return crypto_dispatch(crp);
1083 * AH output callback from the crypto driver.
1086 ah_output_cb(struct cryptop *crp)
1088 struct xform_data *xd;
1089 struct secpolicy *sp;
1090 struct secasvar *sav;
1097 m = (struct mbuf *) crp->crp_buf;
1098 xd = (struct xform_data *) crp->crp_opaque;
1099 CURVNET_SET(xd->vnet);
1104 cryptoid = xd->cryptoid;
1105 ptr = (caddr_t) (xd + 1);
1107 /* Check for crypto errors. */
1108 if (crp->crp_etype) {
1109 if (crp->crp_etype == EAGAIN) {
1110 /* Reset the session ID */
1111 if (ipsec_updateid(sav, &crp->crp_sid, &cryptoid) != 0)
1112 crypto_freesession(cryptoid);
1113 xd->cryptoid = crp->crp_sid;
1115 return (crypto_dispatch(crp));
1117 AHSTAT_INC(ahs_noxform);
1118 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
1119 error = crp->crp_etype;
1124 /* Shouldn't happen... */
1126 AHSTAT_INC(ahs_crypto);
1127 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
1132 * Copy original headers (with the new protocol number) back
1135 m_copyback(m, 0, skip, ptr);
1138 crypto_freereq(crp);
1139 AHSTAT_INC(ahs_hist[sav->alg_auth]);
1141 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
1142 if (V_ipsec_integrity) {
1146 * Corrupt HMAC if we want to test integrity verification of
1149 alen = AUTHSIZE(sav);
1150 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes);
1154 /* NB: m is reclaimed by ipsec_process_done. */
1155 error = ipsec_process_done(m, sp, sav, idx);
1161 crypto_freereq(crp);
1167 static struct xformsw ah_xformsw = {
1169 .xf_name = "IPsec AH",
1171 .xf_zeroize = ah_zeroize,
1172 .xf_input = ah_input,
1173 .xf_output = ah_output,
1176 SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
1177 xform_attach, &ah_xformsw);
1178 SYSUNINIT(ah_xform_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
1179 xform_detach, &ah_xformsw);