1 /* $FreeBSD: src/sys/netipsec/xform_ah.c,v 1.1.4.2 2003/02/26 00:14:05 sam Exp $ */
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>
48 #include <sys/sysctl.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_ecn.h>
56 #include <netinet/ip6.h>
58 #include <net/route.h>
59 #include <netproto/ipsec/ipsec.h>
60 #include <netproto/ipsec/ah.h>
61 #include <netproto/ipsec/ah_var.h>
62 #include <netproto/ipsec/xform.h>
65 #include <netinet6/ip6_var.h>
66 #include <netproto/ipsec/ipsec6.h>
67 #include <netinet6/ip6_ecn.h>
70 #include <netproto/ipsec/key.h>
71 #include <netproto/ipsec/key_debug.h>
73 #include <opencrypto/cryptodev.h>
76 * Return header size in bytes. The old protocol did not support
77 * the replay counter; the new protocol always includes the counter.
79 #define HDRSIZE(sav) \
80 (((sav)->flags & SADB_X_EXT_OLD) ? \
81 sizeof (struct ah) : sizeof (struct ah) + sizeof (u_int32_t))
83 * Return authenticator size in bytes. The old protocol is known
84 * to use a fixed 16-byte authenticator. The new algorithm gets
85 * this size from the xform but is (currently) always 12.
87 #define AUTHSIZE(sav) \
88 ((sav->flags & SADB_X_EXT_OLD) ? 16 : (sav)->tdb_authalgxform->authsize)
90 int ah_enable = 1; /* control flow of packets with AH */
91 int ah_cleartos = 1; /* clear ip_tos when doing AH calc */
94 SYSCTL_DECL(_net_inet_ah);
95 SYSCTL_INT(_net_inet_ah, OID_AUTO,
96 ah_enable, CTLFLAG_RW, &ah_enable, 0, "");
97 SYSCTL_INT(_net_inet_ah, OID_AUTO,
98 ah_cleartos, CTLFLAG_RW, &ah_cleartos, 0, "");
99 SYSCTL_STRUCT(_net_inet_ah, IPSECCTL_STATS,
100 stats, CTLFLAG_RD, &ahstat, ahstat, "");
102 static unsigned char ipseczeroes[256]; /* larger than an ip6 extension hdr */
104 static int ah_input_cb(struct cryptop*);
105 static int ah_output_cb(struct cryptop*);
108 * NB: this is public for use by the PF_KEY support.
111 ah_algorithm_lookup(int alg)
113 if (alg >= AH_ALG_MAX)
116 case SADB_X_AALG_NULL:
117 return &auth_hash_null;
118 case SADB_AALG_MD5HMAC:
119 return &auth_hash_hmac_md5_96;
120 case SADB_AALG_SHA1HMAC:
121 return &auth_hash_hmac_sha1_96;
122 case SADB_X_AALG_RIPEMD160HMAC:
123 return &auth_hash_hmac_ripemd_160_96;
124 case SADB_X_AALG_MD5:
125 return &auth_hash_key_md5;
126 case SADB_X_AALG_SHA:
127 return &auth_hash_key_sha1;
128 case SADB_X_AALG_SHA2_256:
129 return &auth_hash_hmac_sha2_256;
130 case SADB_X_AALG_SHA2_384:
131 return &auth_hash_hmac_sha2_384;
132 case SADB_X_AALG_SHA2_512:
133 return &auth_hash_hmac_sha2_512;
139 ah_hdrsiz(struct secasvar *sav)
145 KASSERT(sav->tdb_authalgxform != NULL,
146 ("ah_hdrsiz: null xform"));
147 /*XXX not right for null algorithm--does it matter??*/
148 authsize = AUTHSIZE(sav);
149 size = roundup(authsize, sizeof (u_int32_t)) + HDRSIZE(sav);
152 size = sizeof (struct ah) + sizeof (u_int32_t) + 16;
158 * NB: public for use by esp_init.
161 ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria)
163 struct auth_hash *thash;
166 thash = ah_algorithm_lookup(sav->alg_auth);
168 DPRINTF(("ah_init: unsupported authentication algorithm %u\n",
173 * Verify the replay state block allocation is consistent with
174 * the protocol type. We check here so we can make assumptions
175 * later during protocol processing.
177 /* NB: replay state is setup elsewhere (sigh) */
178 if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) {
179 DPRINTF(("ah_init: replay state block inconsistency, "
180 "%s algorithm %s replay state\n",
181 (sav->flags & SADB_X_EXT_OLD) ? "old" : "new",
182 sav->replay == NULL ? "without" : "with"));
185 if (sav->key_auth == NULL) {
186 DPRINTF(("ah_init: no authentication key for %s "
187 "algorithm\n", thash->name));
190 keylen = _KEYLEN(sav->key_auth);
191 if (keylen != thash->keysize && thash->keysize != 0) {
192 DPRINTF(("ah_init: invalid keylength %d, algorithm "
193 "%s requires keysize %d\n",
194 keylen, thash->name, thash->keysize));
198 sav->tdb_xform = xsp;
199 sav->tdb_authalgxform = thash;
201 /* Initialize crypto session. */
202 bzero(cria, sizeof (*cria));
203 cria->cri_alg = sav->tdb_authalgxform->type;
204 cria->cri_klen = _KEYBITS(sav->key_auth);
205 cria->cri_key = _KEYBUF(sav->key_auth);
211 * ah_init() is called when an SPI is being set up.
214 ah_init(struct secasvar *sav, struct xformsw *xsp)
216 struct cryptoini cria;
219 error = ah_init0(sav, xsp, &cria);
220 return error ? error :
221 crypto_newsession(&sav->tdb_cryptoid, &cria, crypto_support);
227 * NB: public for use by esp_zeroize (XXX).
230 ah_zeroize(struct secasvar *sav)
235 bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
237 err = crypto_freesession(sav->tdb_cryptoid);
238 sav->tdb_cryptoid = 0;
239 sav->tdb_authalgxform = NULL;
240 sav->tdb_xform = NULL;
245 * Massage IPv4/IPv6 headers for AH processing.
248 ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out)
250 struct mbuf *m = *m0;
259 struct ip6_ext *ip6e;
268 * This is the least painful way of dealing with IPv4 header
269 * and option processing -- just make sure they're in
272 *m0 = m = m_pullup(m, skip);
274 DPRINTF(("ah_massage_headers: m_pullup failed\n"));
278 /* Fix the IP header */
279 ip = mtod(m, struct ip *);
286 * On input, fix ip_len which has been byte-swapped
290 ip->ip_len = htons(ip->ip_len + skip);
292 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
293 ip->ip_off = htons(ip->ip_off & IP_DF);
297 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
298 ip->ip_off = htons(ntohs(ip->ip_off) & IP_DF);
303 ptr = mtod(m, unsigned char *) + sizeof(struct ip);
305 /* IPv4 option processing */
306 for (off = sizeof(struct ip); off < skip;) {
307 if (ptr[off] == IPOPT_EOL || ptr[off] == IPOPT_NOP ||
311 DPRINTF(("ah_massage_headers: illegal IPv4 "
312 "option length for option %d\n",
321 off = skip; /* End the loop. */
328 case IPOPT_SECURITY: /* 0x82 */
329 case 0x85: /* Extended security. */
330 case 0x86: /* Commercial security. */
331 case 0x94: /* Router alert */
332 case 0x95: /* RFC1770 */
333 /* Sanity check for option length. */
334 if (ptr[off + 1] < 2) {
335 DPRINTF(("ah_massage_headers: "
336 "illegal IPv4 option length for "
337 "option %d\n", ptr[off]));
348 /* Sanity check for option length. */
349 if (ptr[off + 1] < 2) {
350 DPRINTF(("ah_massage_headers: "
351 "illegal IPv4 option length for "
352 "option %d\n", ptr[off]));
359 * On output, if we have either of the
360 * source routing options, we should
361 * swap the destination address of the
362 * IP header with the last address
363 * specified in the option, as that is
364 * what the destination's IP header
368 bcopy(ptr + off + ptr[off + 1] -
369 sizeof(struct in_addr),
370 &(ip->ip_dst), sizeof(struct in_addr));
374 /* Sanity check for option length. */
375 if (ptr[off + 1] < 2) {
376 DPRINTF(("ah_massage_headers: "
377 "illegal IPv4 option length for "
378 "option %d\n", ptr[off]));
383 /* Zeroize all other options. */
384 count = ptr[off + 1];
385 bcopy(ipseczeroes, ptr, count);
392 DPRINTF(("ah_massage_headers(): malformed "
393 "IPv4 options header\n"));
404 case AF_INET6: /* Ugly... */
405 /* Copy and "cook" the IPv6 header. */
406 m_copydata(m, 0, sizeof(ip6), (caddr_t) &ip6);
408 /* We don't do IPv6 Jumbograms. */
409 if (ip6.ip6_plen == 0) {
410 DPRINTF(("ah_massage_headers: unsupported IPv6 jumbogram\n"));
417 ip6.ip6_vfc &= ~IPV6_VERSION_MASK;
418 ip6.ip6_vfc |= IPV6_VERSION;
420 /* Scoped address handling. */
421 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src))
422 ip6.ip6_src.s6_addr16[1] = 0;
423 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst))
424 ip6.ip6_dst.s6_addr16[1] = 0;
426 /* Done with IPv6 header. */
427 m_copyback(m, 0, sizeof(struct ip6_hdr), (caddr_t) &ip6);
429 /* Let's deal with the remaining headers (if any). */
430 if (skip - sizeof(struct ip6_hdr) > 0) {
431 if (m->m_len <= skip) {
432 ptr = kmalloc(skip - sizeof(struct ip6_hdr),
433 M_XDATA, M_INTWAIT | M_NULLOK);
435 DPRINTF(("ah_massage_headers: failed "
436 "to allocate memory for IPv6 "
443 * Copy all the protocol headers after
446 m_copydata(m, sizeof(struct ip6_hdr),
447 skip - sizeof(struct ip6_hdr), ptr);
450 /* No need to allocate memory. */
451 ptr = mtod(m, unsigned char *) +
452 sizeof(struct ip6_hdr);
458 off = ip6.ip6_nxt & 0xff; /* Next header type. */
460 for (len = 0; len < skip - sizeof(struct ip6_hdr);)
462 case IPPROTO_HOPOPTS:
463 case IPPROTO_DSTOPTS:
464 ip6e = (struct ip6_ext *) (ptr + len);
467 * Process the mutable/immutable
468 * options -- borrows heavily from the
471 for (count = len + sizeof(struct ip6_ext);
472 count < len + ((ip6e->ip6e_len + 1) << 3);) {
473 if (ptr[count] == IP6OPT_PAD1) {
475 continue; /* Skip padding. */
480 ((ip6e->ip6e_len + 1) << 3)) {
483 /* Free, if we allocated. */
491 /* If mutable option, zeroize. */
492 if (ptr[count] & IP6OPT_MUTABLE)
493 bcopy(ipseczeroes, ptr + count,
500 skip - sizeof(struct ip6_hdr)) {
503 /* Free, if we allocated. */
511 len += ((ip6e->ip6e_len + 1) << 3);
512 off = ip6e->ip6e_nxt;
515 case IPPROTO_ROUTING:
517 * Always include routing headers in
520 ip6e = (struct ip6_ext *) (ptr + len);
521 len += ((ip6e->ip6e_len + 1) << 3);
522 off = ip6e->ip6e_nxt;
526 DPRINTF(("ah_massage_headers: unexpected "
527 "IPv6 header type %d", off));
534 /* Copyback and free, if we allocated. */
536 m_copyback(m, sizeof(struct ip6_hdr),
537 skip - sizeof(struct ip6_hdr), ptr);
549 * ah_input() gets called to verify that an input packet
550 * passes authentication.
553 ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
555 struct auth_hash *ahx;
556 struct tdb_ident *tdbi;
557 struct tdb_crypto *tc;
560 int hl, rplen, authsize;
562 struct cryptodesc *crda;
565 KASSERT(sav != NULL, ("ah_input: null SA"));
566 KASSERT(sav->key_auth != NULL,
567 ("ah_input: null authentication key"));
568 KASSERT(sav->tdb_authalgxform != NULL,
569 ("ah_input: null authentication xform"));
571 /* Figure out header size. */
572 rplen = HDRSIZE(sav);
574 /* XXX don't pullup, just copy header */
575 IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen);
577 DPRINTF(("ah_input: cannot pullup header\n"));
578 ahstat.ahs_hdrops++; /*XXX*/
583 /* Check replay window, if applicable. */
584 if (sav->replay && !ipsec_chkreplay(ntohl(ah->ah_seq), sav)) {
586 DPRINTF(("ah_input: packet replay failure: %s\n",
587 ipsec_logsastr(sav)));
592 /* Verify AH header length. */
593 hl = ah->ah_len * sizeof (u_int32_t);
594 ahx = sav->tdb_authalgxform;
595 authsize = AUTHSIZE(sav);
596 if (hl != authsize + rplen - sizeof (struct ah)) {
597 DPRINTF(("ah_input: bad authenticator length %u (expecting %lu)"
598 " for packet in SA %s/%08lx\n",
599 hl, (u_long) (authsize + rplen - sizeof (struct ah)),
600 ipsec_address(&sav->sah->saidx.dst),
601 (u_long) ntohl(sav->spi)));
602 ahstat.ahs_badauthl++;
606 ahstat.ahs_ibytes += m->m_pkthdr.len - skip - hl;
608 /* Get crypto descriptors. */
609 crp = crypto_getreq(1);
611 DPRINTF(("ah_input: failed to acquire crypto descriptor\n"));
617 crda = crp->crp_desc;
618 KASSERT(crda != NULL, ("ah_input: null crypto descriptor"));
621 crda->crd_len = m->m_pkthdr.len;
622 crda->crd_inject = skip + rplen;
624 /* Authentication operation. */
625 crda->crd_alg = ahx->type;
626 crda->crd_key = _KEYBUF(sav->key_auth);
627 crda->crd_klen = _KEYBITS(sav->key_auth);
629 /* Find out if we've already done crypto. */
630 for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL);
632 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) {
633 tdbi = (struct tdb_ident *)m_tag_data(mtag);
634 if (tdbi->proto == sav->sah->saidx.proto &&
635 tdbi->spi == sav->spi &&
636 !bcmp(&tdbi->dst, &sav->sah->saidx.dst,
637 sizeof (union sockaddr_union)))
641 /* Allocate IPsec-specific opaque crypto info. */
643 tc = kmalloc(sizeof (struct tdb_crypto) +
644 skip + rplen + authsize, M_XDATA,
645 M_INTWAIT | M_ZERO | M_NULLOK);
647 /* Hash verification has already been done successfully. */
648 tc = kmalloc(sizeof (struct tdb_crypto),
649 M_XDATA, M_INTWAIT | M_ZERO | M_NULLOK);
652 DPRINTF(("ah_input: failed to allocate tdb_crypto\n"));
659 /* Only save information if crypto processing is needed. */
664 * Save the authenticator, the skipped portion of the packet,
667 m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(tc+1));
669 /* Zeroize the authenticator on the packet. */
670 m_copyback(m, skip + rplen, authsize, ipseczeroes);
672 /* "Massage" the packet headers for crypto processing. */
673 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
676 /* NB: mbuf is free'd by ah_massage_headers */
684 /* Crypto operation descriptor. */
685 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
686 crp->crp_flags = CRYPTO_F_IMBUF;
687 crp->crp_buf = (caddr_t) m;
688 crp->crp_callback = ah_input_cb;
689 crp->crp_sid = sav->tdb_cryptoid;
690 crp->crp_opaque = (caddr_t) tc;
692 /* These are passed as-is to the callback. */
693 tc->tc_spi = sav->spi;
694 tc->tc_dst = sav->sah->saidx.dst;
695 tc->tc_proto = sav->sah->saidx.proto;
696 tc->tc_nxt = ah->ah_nxt;
697 tc->tc_protoff = protoff;
699 tc->tc_ptr = (caddr_t) mtag; /* Save the mtag we've identified. */
702 return crypto_dispatch(crp);
704 return ah_input_cb(crp);
708 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
709 if (saidx->dst.sa.sa_family == AF_INET6) { \
710 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
712 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
716 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
717 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
721 * AH input callback from the crypto driver.
724 ah_input_cb(struct cryptop *crp)
726 int rplen, error, skip, protoff;
727 unsigned char calc[AH_ALEN_MAX];
729 struct cryptodesc *crd;
730 struct auth_hash *ahx;
731 struct tdb_crypto *tc;
733 struct secasvar *sav;
734 struct secasindex *saidx;
741 tc = (struct tdb_crypto *) crp->crp_opaque;
742 KASSERT(tc != NULL, ("ah_input_cb: null opaque crypto data area!"));
745 protoff = tc->tc_protoff;
746 mtag = (struct m_tag *) tc->tc_ptr;
747 m = (struct mbuf *) crp->crp_buf;
751 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
754 DPRINTF(("ah_input_cb: SA expired while in crypto\n"));
755 error = ENOBUFS; /*XXX*/
759 saidx = &sav->sah->saidx;
760 KASSERT(saidx->dst.sa.sa_family == AF_INET ||
761 saidx->dst.sa.sa_family == AF_INET6,
762 ("ah_input_cb: unexpected protocol family %u",
763 saidx->dst.sa.sa_family));
765 ahx = (struct auth_hash *) sav->tdb_authalgxform;
767 /* Check for crypto errors. */
768 if (crp->crp_etype) {
769 if (sav->tdb_cryptoid != 0)
770 sav->tdb_cryptoid = crp->crp_sid;
772 if (crp->crp_etype == EAGAIN)
773 return crypto_dispatch(crp);
775 ahstat.ahs_noxform++;
776 DPRINTF(("ah_input_cb: crypto error %d\n", crp->crp_etype));
777 error = crp->crp_etype;
780 ahstat.ahs_hist[sav->alg_auth]++;
781 crypto_freereq(crp); /* No longer needed. */
785 /* Shouldn't happen... */
788 DPRINTF(("ah_input_cb: bogus returned buffer from crypto\n"));
793 /* Figure out header size. */
794 rplen = HDRSIZE(sav);
795 authsize = AUTHSIZE(sav);
797 /* Copy authenticator off the packet. */
798 m_copydata(m, skip + rplen, authsize, calc);
801 * If we have an mtag, we don't need to verify the authenticator --
802 * it has been verified by an IPsec-aware NIC.
805 ptr = (caddr_t) (tc + 1);
807 /* Verify authenticator. */
808 if (bcmp(ptr + skip + rplen, calc, authsize)) {
809 DPRINTF(("ah_input: authentication hash mismatch "
810 "for packet in SA %s/%08lx\n",
811 ipsec_address(&saidx->dst),
812 (u_long) ntohl(sav->spi)));
813 ahstat.ahs_badauth++;
818 /* Fix the Next Protocol field. */
819 ((u_int8_t *) ptr)[protoff] = nxt;
821 /* Copyback the saved (uncooked) network headers. */
822 m_copyback(m, 0, skip, ptr);
824 /* Fix the Next Protocol field. */
825 m_copyback(m, protoff, sizeof(u_int8_t), &nxt);
828 kfree(tc, M_XDATA), tc = NULL; /* No longer needed */
831 * Header is now authenticated.
833 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM;
836 * Update replay sequence number, if appropriate.
841 m_copydata(m, skip + offsetof(struct newah, ah_seq),
842 sizeof (seq), (caddr_t) &seq);
843 if (ipsec_updatereplay(ntohl(seq), sav)) {
845 error = ENOBUFS; /*XXX as above*/
851 * Remove the AH header and authenticator from the mbuf.
853 error = m_striphdr(m, skip, rplen + authsize);
855 DPRINTF(("ah_input_cb: mangled mbuf chain for SA %s/%08lx\n",
856 ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
862 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag);
881 * AH output routine, called by ipsec[46]_process_packet().
886 struct ipsecrequest *isr,
891 struct secasvar *sav;
892 struct auth_hash *ahx;
893 struct cryptodesc *crda;
894 struct tdb_crypto *tc;
898 int error, rplen, authsize, maxpacketsize, roff;
903 KASSERT(sav != NULL, ("ah_output: null SA"));
904 ahx = sav->tdb_authalgxform;
905 KASSERT(ahx != NULL, ("ah_output: null authentication xform"));
909 /* Figure out header size. */
910 rplen = HDRSIZE(sav);
912 /* Check for maximum packet size violations. */
913 switch (sav->sah->saidx.dst.sa.sa_family) {
916 maxpacketsize = IP_MAXPACKET;
921 maxpacketsize = IPV6_MAXPACKET;
925 DPRINTF(("ah_output: unknown/unsupported protocol "
926 "family %u, SA %s/%08lx\n",
927 sav->sah->saidx.dst.sa.sa_family,
928 ipsec_address(&sav->sah->saidx.dst),
929 (u_long) ntohl(sav->spi)));
931 error = EPFNOSUPPORT;
934 authsize = AUTHSIZE(sav);
935 if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) {
936 DPRINTF(("ah_output: packet in SA %s/%08lx got too big "
937 "(len %u, max len %u)\n",
938 ipsec_address(&sav->sah->saidx.dst),
939 (u_long) ntohl(sav->spi),
940 rplen + authsize + m->m_pkthdr.len, maxpacketsize));
946 /* Update the counters. */
947 ahstat.ahs_obytes += m->m_pkthdr.len - skip;
951 DPRINTF(("ah_output: cannot clone mbuf chain, SA %s/%08lx\n",
952 ipsec_address(&sav->sah->saidx.dst),
953 (u_long) ntohl(sav->spi)));
959 /* Inject AH header. */
960 mi = m_makespace(m, skip, rplen + authsize, &roff);
962 DPRINTF(("ah_output: failed to inject %u byte AH header for SA "
965 ipsec_address(&sav->sah->saidx.dst),
966 (u_long) ntohl(sav->spi)));
967 ahstat.ahs_hdrops++; /*XXX differs from openbsd */
973 * The AH header is guaranteed by m_makespace() to be in
974 * contiguous memory, at roff bytes offset into the returned mbuf.
976 ah = (struct newah *)(mtod(mi, caddr_t) + roff);
978 /* Initialize the AH header. */
979 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt);
980 ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(u_int32_t);
982 ah->ah_spi = sav->spi;
984 /* Zeroize authenticator. */
985 m_copyback(m, skip + rplen, authsize, ipseczeroes);
987 /* Insert packet replay counter, as requested. */
989 if (sav->replay->count == ~0 &&
990 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
991 DPRINTF(("ah_output: replay counter wrapped for SA "
993 ipsec_address(&sav->sah->saidx.dst),
994 (u_long) ntohl(sav->spi)));
999 sav->replay->count++;
1000 ah->ah_seq = htonl(sav->replay->count);
1003 /* Get crypto descriptors. */
1004 crp = crypto_getreq(1);
1006 DPRINTF(("ah_output: failed to acquire crypto descriptors\n"));
1007 ahstat.ahs_crypto++;
1012 crda = crp->crp_desc;
1015 crda->crd_inject = skip + rplen;
1016 crda->crd_len = m->m_pkthdr.len;
1018 /* Authentication operation. */
1019 crda->crd_alg = ahx->type;
1020 crda->crd_key = _KEYBUF(sav->key_auth);
1021 crda->crd_klen = _KEYBITS(sav->key_auth);
1023 /* Allocate IPsec-specific opaque crypto info. */
1024 tc = kmalloc(sizeof(struct tdb_crypto) + skip, M_XDATA,
1025 M_INTWAIT | M_ZERO | M_NULLOK);
1027 crypto_freereq(crp);
1028 DPRINTF(("ah_output: failed to allocate tdb_crypto\n"));
1029 ahstat.ahs_crypto++;
1034 /* Save the skipped portion of the packet. */
1035 m_copydata(m, 0, skip, (caddr_t) (tc + 1));
1038 * Fix IP header length on the header used for
1039 * authentication. We don't need to fix the original
1040 * header length as it will be fixed by our caller.
1042 switch (sav->sah->saidx.dst.sa.sa_family) {
1045 bcopy(((caddr_t)(tc + 1)) +
1046 offsetof(struct ip, ip_len),
1047 (caddr_t) &iplen, sizeof(u_int16_t));
1048 iplen = htons(ntohs(iplen) + rplen + authsize);
1049 m_copyback(m, offsetof(struct ip, ip_len),
1050 sizeof(u_int16_t), (caddr_t) &iplen);
1056 bcopy(((caddr_t)(tc + 1)) +
1057 offsetof(struct ip6_hdr, ip6_plen),
1058 (caddr_t) &iplen, sizeof(u_int16_t));
1059 iplen = htons(ntohs(iplen) + rplen + authsize);
1060 m_copyback(m, offsetof(struct ip6_hdr, ip6_plen),
1061 sizeof(u_int16_t), (caddr_t) &iplen);
1066 /* Fix the Next Header field in saved header. */
1067 ((u_int8_t *) (tc + 1))[protoff] = IPPROTO_AH;
1069 /* Update the Next Protocol field in the IP header. */
1071 m_copyback(m, protoff, sizeof(u_int8_t), (caddr_t) &prot);
1073 /* "Massage" the packet headers for crypto processing. */
1074 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
1075 skip, ahx->type, 1);
1077 m = NULL; /* mbuf was free'd by ah_massage_headers. */
1079 crypto_freereq(crp);
1083 /* Crypto operation descriptor. */
1084 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
1085 crp->crp_flags = CRYPTO_F_IMBUF;
1086 crp->crp_buf = (caddr_t) m;
1087 crp->crp_callback = ah_output_cb;
1088 crp->crp_sid = sav->tdb_cryptoid;
1089 crp->crp_opaque = (caddr_t) tc;
1091 /* These are passed as-is to the callback. */
1093 tc->tc_spi = sav->spi;
1094 tc->tc_dst = sav->sah->saidx.dst;
1095 tc->tc_proto = sav->sah->saidx.proto;
1097 tc->tc_protoff = protoff;
1099 return crypto_dispatch(crp);
1107 * AH output callback from the crypto driver.
1110 ah_output_cb(struct cryptop *crp)
1112 int skip, protoff, error;
1113 struct tdb_crypto *tc;
1114 struct ipsecrequest *isr;
1115 struct secasvar *sav;
1120 tc = (struct tdb_crypto *) crp->crp_opaque;
1121 KASSERT(tc != NULL, ("ah_output_cb: null opaque data area!"));
1123 protoff = tc->tc_protoff;
1124 ptr = (caddr_t) (tc + 1);
1125 m = (struct mbuf *) crp->crp_buf;
1130 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
1133 DPRINTF(("ah_output_cb: SA expired while in crypto\n"));
1134 error = ENOBUFS; /*XXX*/
1137 KASSERT(isr->sav == sav, ("ah_output_cb: SA changed\n"));
1139 /* Check for crypto errors. */
1140 if (crp->crp_etype) {
1141 if (sav->tdb_cryptoid != 0)
1142 sav->tdb_cryptoid = crp->crp_sid;
1144 if (crp->crp_etype == EAGAIN) {
1147 return crypto_dispatch(crp);
1150 ahstat.ahs_noxform++;
1151 DPRINTF(("ah_output_cb: crypto error %d\n", crp->crp_etype));
1152 error = crp->crp_etype;
1156 /* Shouldn't happen... */
1158 ahstat.ahs_crypto++;
1159 DPRINTF(("ah_output_cb: bogus returned buffer from crypto\n"));
1163 ahstat.ahs_hist[sav->alg_auth]++;
1166 * Copy original headers (with the new protocol number) back
1169 m_copyback(m, 0, skip, ptr);
1171 /* No longer needed. */
1173 crypto_freereq(crp);
1175 /* NB: m is reclaimed by ipsec_process_done. */
1176 err = ipsec_process_done(m, isr);
1187 crypto_freereq(crp);
1191 static struct xformsw ah_xformsw = {
1192 XF_AH, XFT_AUTH, "IPsec AH",
1193 ah_init, ah_zeroize, ah_input, ah_output,
1199 xform_register(&ah_xformsw);
1201 SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ah_attach, NULL);