1 /* $NetBSD: xform_ah.c,v 1.25 2009/03/18 17:06:52 cegger Exp $ */
2 /* $FreeBSD: src/sys/netipsec/xform_ah.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $ */
3 /* $OpenBSD: ip_ah.c,v 1.63 2001/06/26 06:18:58 angelos Exp $ */
5 * The authors of this code are John Ioannidis (ji@tla.org),
6 * Angelos D. Keromytis (kermit@csd.uch.gr) and
7 * Niels Provos (provos@physnet.uni-hamburg.de).
9 * The original version of this code was written by John Ioannidis
10 * for BSD/OS in Athens, Greece, in November 1995.
12 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
13 * by Angelos D. Keromytis.
15 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
18 * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist.
20 * Copyright (c) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
21 * Angelos D. Keromytis and Niels Provos.
22 * Copyright (c) 1999 Niklas Hallqvist.
23 * Copyright (c) 2001 Angelos D. Keromytis.
25 * Permission to use, copy, and modify this software with or without fee
26 * is hereby granted, provided that this entire notice is included in
27 * all copies of any software which is or includes a copy or
28 * modification of this software.
29 * You may use this code under the GNU public license if you so wish. Please
30 * contribute changes back to the authors under this freer than GPL license
31 * so that we may further the use of strong encryption without limitations to
34 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
35 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
36 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
37 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: xform_ah.c,v 1.25 2009/03/18 17:06:52 cegger Exp $");
46 #include "opt_inet6.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
52 #include <sys/socket.h>
53 #include <sys/syslog.h>
54 #include <sys/kernel.h>
55 #include <sys/sysctl.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_ecn.h>
63 #include <netinet/ip6.h>
65 #include <net/route.h>
66 #include <netipsec/ipsec.h>
67 #include <netipsec/ipsec_private.h>
68 #include <netipsec/ah.h>
69 #include <netipsec/ah_var.h>
70 #include <netipsec/xform.h>
73 #include <netinet6/ip6_var.h>
74 #include <netipsec/ipsec6.h>
76 # include <netinet6/ip6_ecn.h>
80 #include <netipsec/key.h>
81 #include <netipsec/key_debug.h>
82 #include <netipsec/ipsec_osdep.h>
84 #include <opencrypto/cryptodev.h>
87 * Return header size in bytes. The old protocol did not support
88 * the replay counter; the new protocol always includes the counter.
90 #define HDRSIZE(sav) \
91 (((sav)->flags & SADB_X_EXT_OLD) ? \
92 sizeof (struct ah) : sizeof (struct ah) + sizeof (u_int32_t))
94 * Return authenticator size in bytes. The old protocol is known
95 * to use a fixed 16-byte authenticator. The new algorithm gets
96 * this size from the xform but is (currently) always 12.
98 #define AUTHSIZE(sav) \
99 ((sav->flags & SADB_X_EXT_OLD) ? 16 : (sav)->tdb_authalgxform->authsize)
101 percpu_t
*ahstat_percpu
;
103 int ah_enable
= 1; /* control flow of packets with AH */
104 int ip4_ah_cleartos
= 1; /* clear ip_tos when doing AH calc */
107 SYSCTL_DECL(_net_inet_ah
);
108 SYSCTL_INT(_net_inet_ah
, OID_AUTO
,
109 ah_enable
, CTLFLAG_RW
, &ah_enable
, 0, "");
110 SYSCTL_INT(_net_inet_ah
, OID_AUTO
,
111 ah_cleartos
, CTLFLAG_RW
, &ip4_ah_cleartos
, 0, "");
112 SYSCTL_STRUCT(_net_inet_ah
, IPSECCTL_STATS
,
113 stats
, CTLFLAG_RD
, &ahstat
, ahstat
, "");
115 #endif /* __FreeBSD__ */
117 static unsigned char ipseczeroes
[256]; /* larger than an ip6 extension hdr */
119 static int ah_input_cb(struct cryptop
*);
120 static int ah_output_cb(struct cryptop
*);
123 * NB: this is public for use by the PF_KEY support.
126 ah_algorithm_lookup(int alg
)
128 if (alg
>= AH_ALG_MAX
)
131 case SADB_X_AALG_NULL
:
132 return &auth_hash_null
;
133 case SADB_AALG_MD5HMAC
:
134 return &auth_hash_hmac_md5_96
;
135 case SADB_AALG_SHA1HMAC
:
136 return &auth_hash_hmac_sha1_96
;
137 case SADB_X_AALG_RIPEMD160HMAC
:
138 return &auth_hash_hmac_ripemd_160_96
;
139 case SADB_X_AALG_MD5
:
140 return &auth_hash_key_md5
;
141 case SADB_X_AALG_SHA
:
142 return &auth_hash_key_sha1
;
143 case SADB_X_AALG_SHA2_256
:
144 return &auth_hash_hmac_sha2_256
;
145 case SADB_X_AALG_SHA2_384
:
146 return &auth_hash_hmac_sha2_384
;
147 case SADB_X_AALG_SHA2_512
:
148 return &auth_hash_hmac_sha2_512
;
154 ah_hdrsiz(struct secasvar
*sav
)
160 IPSEC_ASSERT(sav
->tdb_authalgxform
!= NULL
,
161 ("ah_hdrsiz: null xform"));
162 /*XXX not right for null algorithm--does it matter??*/
163 authsize
= AUTHSIZE(sav
);
164 size
= roundup(authsize
, sizeof (u_int32_t
)) + HDRSIZE(sav
);
167 size
= sizeof (struct ah
) + sizeof (u_int32_t
) + 16;
173 * NB: public for use by esp_init.
176 ah_init0(struct secasvar
*sav
, struct xformsw
*xsp
, struct cryptoini
*cria
)
178 struct auth_hash
*thash
;
181 thash
= ah_algorithm_lookup(sav
->alg_auth
);
183 DPRINTF(("ah_init: unsupported authentication algorithm %u\n",
188 * Verify the replay state block allocation is consistent with
189 * the protocol type. We check here so we can make assumptions
190 * later during protocol processing.
192 /* NB: replay state is setup elsewhere (sigh) */
193 if (((sav
->flags
&SADB_X_EXT_OLD
) == 0) ^ (sav
->replay
!= NULL
)) {
194 DPRINTF(("ah_init: replay state block inconsistency, "
195 "%s algorithm %s replay state\n",
196 (sav
->flags
& SADB_X_EXT_OLD
) ? "old" : "new",
197 sav
->replay
== NULL
? "without" : "with"));
200 if (sav
->key_auth
== NULL
) {
201 DPRINTF(("ah_init: no authentication key for %s "
202 "algorithm\n", thash
->name
));
205 keylen
= _KEYLEN(sav
->key_auth
);
206 if (keylen
!= thash
->keysize
&& thash
->keysize
!= 0) {
207 DPRINTF(("ah_init: invalid keylength %d, algorithm "
208 "%s requires keysize %d\n",
209 keylen
, thash
->name
, thash
->keysize
));
213 sav
->tdb_xform
= xsp
;
214 sav
->tdb_authalgxform
= thash
;
216 /* Initialize crypto session. */
217 memset(cria
, 0, sizeof (*cria
));
218 cria
->cri_alg
= sav
->tdb_authalgxform
->type
;
219 cria
->cri_klen
= _KEYBITS(sav
->key_auth
);
220 cria
->cri_key
= _KEYBUF(sav
->key_auth
);
226 * ah_init() is called when an SPI is being set up.
229 ah_init(struct secasvar
*sav
, struct xformsw
*xsp
)
231 struct cryptoini cria
;
234 error
= ah_init0(sav
, xsp
, &cria
);
236 mutex_spin_enter(&crypto_mtx
);
237 error
= crypto_newsession(&sav
->tdb_cryptoid
,
238 &cria
, crypto_support
);
239 mutex_spin_exit(&crypto_mtx
);
247 * NB: public for use by esp_zeroize (XXX).
250 ah_zeroize(struct secasvar
*sav
)
255 memset(_KEYBUF(sav
->key_auth
), 0, _KEYLEN(sav
->key_auth
));
257 mutex_spin_enter(&crypto_mtx
);
258 err
= crypto_freesession(sav
->tdb_cryptoid
);
259 mutex_spin_exit(&crypto_mtx
);
260 sav
->tdb_cryptoid
= 0;
261 sav
->tdb_authalgxform
= NULL
;
262 sav
->tdb_xform
= NULL
;
267 * Massage IPv4/IPv6 headers for AH processing.
270 ah_massage_headers(struct mbuf
**m0
, int proto
, int skip
, int alg
, int out
)
272 struct mbuf
*m
= *m0
;
281 struct ip6_ext
*ip6e
;
290 * This is the least painful way of dealing with IPv4 header
291 * and option processing -- just make sure they're in
294 *m0
= m
= m_pullup(m
, skip
);
296 DPRINTF(("ah_massage_headers: m_pullup failed\n"));
300 /* Fix the IP header */
301 ip
= mtod(m
, struct ip
*);
306 ip
->ip_off
= htons(ntohs(ip
->ip_off
) & ip4_ah_offsetmask
);
309 * On FreeBSD, ip_off and ip_len assumed in host endian;
310 * they are converted (if necessary) by ip_input().
311 * On NetBSD, ip_off and ip_len are in network byte order.
312 * They must be massaged back to network byte order
313 * before verifying the HMAC. Moreover, on FreeBSD,
314 * we should add `skip' back into the massaged ip_len
315 * (presumably ip_input() deducted it before we got here?)
316 * whereas on NetBSD, we should not.
319 #define TOHOST(x) (x)
321 #define TOHOST(x) (ntohs(x))
324 u_int16_t inlen
= TOHOST(ip
->ip_len
);
327 ip
->ip_len
= htons(inlen
+ skip
);
328 #else /*!__FreeBSD__ */
329 ip
->ip_len
= htons(inlen
);
330 #endif /*!__FreeBSD__ */
331 DPRINTF(("ip len: skip %d, "
332 "in %d host %d: new: raw %d host %d\n",
334 inlen
, TOHOST(inlen
),
335 ip
->ip_len
, ntohs(ip
->ip_len
)));
338 if (alg
== CRYPTO_MD5_KPDK
|| alg
== CRYPTO_SHA1_KPDK
)
339 ip
->ip_off
&= IP_OFF_CONVERT(IP_DF
);
343 if (alg
== CRYPTO_MD5_KPDK
|| alg
== CRYPTO_SHA1_KPDK
)
344 ip
->ip_off
&= IP_OFF_CONVERT(IP_DF
);
349 ptr
= mtod(m
, unsigned char *) + sizeof(struct ip
);
351 /* IPv4 option processing */
352 for (off
= sizeof(struct ip
); off
< skip
;) {
353 if (ptr
[off
] == IPOPT_EOL
|| ptr
[off
] == IPOPT_NOP
||
357 DPRINTF(("ah_massage_headers: illegal IPv4 "
358 "option length for option %d\n",
367 off
= skip
; /* End the loop. */
374 case IPOPT_SECURITY
: /* 0x82 */
375 case 0x85: /* Extended security. */
376 case 0x86: /* Commercial security. */
377 case 0x94: /* Router alert */
378 case 0x95: /* RFC1770 */
379 /* Sanity check for option length. */
380 if (ptr
[off
+ 1] < 2) {
381 DPRINTF(("ah_massage_headers: "
382 "illegal IPv4 option length for "
383 "option %d\n", ptr
[off
]));
394 /* Sanity check for option length. */
395 if (ptr
[off
+ 1] < 2) {
396 DPRINTF(("ah_massage_headers: "
397 "illegal IPv4 option length for "
398 "option %d\n", ptr
[off
]));
405 * On output, if we have either of the
406 * source routing options, we should
407 * swap the destination address of the
408 * IP header with the last address
409 * specified in the option, as that is
410 * what the destination's IP header
414 bcopy(ptr
+ off
+ ptr
[off
+ 1] -
415 sizeof(struct in_addr
),
416 &(ip
->ip_dst
), sizeof(struct in_addr
));
420 /* Sanity check for option length. */
421 if (ptr
[off
+ 1] < 2) {
422 DPRINTF(("ah_massage_headers: "
423 "illegal IPv4 option length for "
424 "option %d\n", ptr
[off
]));
429 /* Zeroize all other options. */
430 count
= ptr
[off
+ 1];
431 memcpy(ptr
, ipseczeroes
, count
);
438 DPRINTF(("ah_massage_headers(): malformed "
439 "IPv4 options header\n"));
450 case AF_INET6
: /* Ugly... */
451 /* Copy and "cook" the IPv6 header. */
452 m_copydata(m
, 0, sizeof(ip6
), &ip6
);
454 /* We don't do IPv6 Jumbograms. */
455 if (ip6
.ip6_plen
== 0) {
456 DPRINTF(("ah_massage_headers: unsupported IPv6 jumbogram\n"));
463 ip6
.ip6_vfc
&= ~IPV6_VERSION_MASK
;
464 ip6
.ip6_vfc
|= IPV6_VERSION
;
466 /* Scoped address handling. */
467 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
.ip6_src
))
468 ip6
.ip6_src
.s6_addr16
[1] = 0;
469 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
.ip6_dst
))
470 ip6
.ip6_dst
.s6_addr16
[1] = 0;
472 /* Done with IPv6 header. */
473 m_copyback(m
, 0, sizeof(struct ip6_hdr
), &ip6
);
475 /* Let's deal with the remaining headers (if any). */
476 if (skip
- sizeof(struct ip6_hdr
) > 0) {
477 if (m
->m_len
<= skip
) {
478 ptr
= (unsigned char *) malloc(
479 skip
- sizeof(struct ip6_hdr
),
482 DPRINTF(("ah_massage_headers: failed "
483 "to allocate memory for IPv6 "
490 * Copy all the protocol headers after
493 m_copydata(m
, sizeof(struct ip6_hdr
),
494 skip
- sizeof(struct ip6_hdr
), ptr
);
497 /* No need to allocate memory. */
498 ptr
= mtod(m
, unsigned char *) +
499 sizeof(struct ip6_hdr
);
505 off
= ip6
.ip6_nxt
& 0xff; /* Next header type. */
507 for (len
= 0; len
< skip
- sizeof(struct ip6_hdr
);)
509 case IPPROTO_HOPOPTS
:
510 case IPPROTO_DSTOPTS
:
511 ip6e
= (struct ip6_ext
*) (ptr
+ len
);
514 * Process the mutable/immutable
515 * options -- borrows heavily from the
518 for (count
= len
+ sizeof(struct ip6_ext
);
519 count
< len
+ ((ip6e
->ip6e_len
+ 1) << 3);) {
520 if (ptr
[count
] == IP6OPT_PAD1
) {
522 continue; /* Skip padding. */
527 ((ip6e
->ip6e_len
+ 1) << 3)) {
530 /* Free, if we allocated. */
538 /* If mutable option, zeroize. */
539 if (ptr
[count
] & IP6OPT_MUTABLE
)
540 memcpy(ptr
+ count
, ipseczeroes
,
547 skip
- sizeof(struct ip6_hdr
)) {
550 /* Free, if we allocated. */
558 len
+= ((ip6e
->ip6e_len
+ 1) << 3);
559 off
= ip6e
->ip6e_nxt
;
562 case IPPROTO_ROUTING
:
564 * Always include routing headers in
567 ip6e
= (struct ip6_ext
*) (ptr
+ len
);
568 len
+= ((ip6e
->ip6e_len
+ 1) << 3);
569 off
= ip6e
->ip6e_nxt
;
573 DPRINTF(("ah_massage_headers: unexpected "
574 "IPv6 header type %d", off
));
581 /* Copyback and free, if we allocated. */
583 m_copyback(m
, sizeof(struct ip6_hdr
),
584 skip
- sizeof(struct ip6_hdr
), ptr
);
596 * ah_input() gets called to verify that an input packet
597 * passes authentication.
600 ah_input(struct mbuf
*m
, struct secasvar
*sav
, int skip
, int protoff
)
602 struct auth_hash
*ahx
;
603 struct tdb_ident
*tdbi
;
604 struct tdb_crypto
*tc
;
607 int hl
, rplen
, authsize
;
609 struct cryptodesc
*crda
;
612 IPSEC_SPLASSERT_SOFTNET("ah_input");
614 IPSEC_ASSERT(sav
!= NULL
, ("ah_input: null SA"));
615 IPSEC_ASSERT(sav
->key_auth
!= NULL
,
616 ("ah_input: null authentication key"));
617 IPSEC_ASSERT(sav
->tdb_authalgxform
!= NULL
,
618 ("ah_input: null authentication xform"));
620 /* Figure out header size. */
621 rplen
= HDRSIZE(sav
);
623 /* XXX don't pullup, just copy header */
624 IP6_EXTHDR_GET(ah
, struct newah
*, m
, skip
, rplen
);
626 DPRINTF(("ah_input: cannot pullup header\n"));
627 AH_STATINC(AH_STAT_HDROPS
); /*XXX*/
632 /* Check replay window, if applicable. */
633 if (sav
->replay
&& !ipsec_chkreplay(ntohl(ah
->ah_seq
), sav
)) {
634 AH_STATINC(AH_STAT_REPLAY
);
635 DPRINTF(("ah_input: packet replay failure: %s\n",
636 ipsec_logsastr(sav
)));
641 /* Verify AH header length. */
642 hl
= ah
->ah_len
* sizeof (u_int32_t
);
643 ahx
= sav
->tdb_authalgxform
;
644 authsize
= AUTHSIZE(sav
);
645 if (hl
!= authsize
+ rplen
- sizeof (struct ah
)) {
646 DPRINTF(("ah_input: bad authenticator length %u (expecting %lu)"
647 " for packet in SA %s/%08lx\n",
648 hl
, (u_long
) (authsize
+ rplen
- sizeof (struct ah
)),
649 ipsec_address(&sav
->sah
->saidx
.dst
),
650 (u_long
) ntohl(sav
->spi
)));
651 AH_STATINC(AH_STAT_BADAUTHL
);
655 AH_STATADD(AH_STAT_IBYTES
, m
->m_pkthdr
.len
- skip
- hl
);
656 DPRINTF(("ah_input skip %d poff %d\n"
657 "len: hl %d authsize %d rpl %d expect %ld\n",
660 (long)(authsize
+ rplen
- sizeof(struct ah
))));
662 /* Get crypto descriptors. */
663 crp
= crypto_getreq(1);
665 DPRINTF(("ah_input: failed to acquire crypto descriptor\n"));
666 AH_STATINC(AH_STAT_CRYPTO
);
671 crda
= crp
->crp_desc
;
672 IPSEC_ASSERT(crda
!= NULL
, ("ah_input: null crypto descriptor"));
675 crda
->crd_len
= m
->m_pkthdr
.len
;
676 crda
->crd_inject
= skip
+ rplen
;
678 /* Authentication operation. */
679 crda
->crd_alg
= ahx
->type
;
680 crda
->crd_key
= _KEYBUF(sav
->key_auth
);
681 crda
->crd_klen
= _KEYBITS(sav
->key_auth
);
683 /* Find out if we've already done crypto. */
684 for (mtag
= m_tag_find(m
, PACKET_TAG_IPSEC_IN_CRYPTO_DONE
, NULL
);
686 mtag
= m_tag_find(m
, PACKET_TAG_IPSEC_IN_CRYPTO_DONE
, mtag
)) {
687 tdbi
= (struct tdb_ident
*) (mtag
+ 1);
688 if (tdbi
->proto
== sav
->sah
->saidx
.proto
&&
689 tdbi
->spi
== sav
->spi
&&
690 !memcmp(&tdbi
->dst
, &sav
->sah
->saidx
.dst
,
691 sizeof (union sockaddr_union
)))
695 /* Allocate IPsec-specific opaque crypto info. */
697 tc
= (struct tdb_crypto
*) malloc(sizeof (struct tdb_crypto
) +
698 skip
+ rplen
+ authsize
, M_XDATA
, M_NOWAIT
|M_ZERO
);
700 /* Hash verification has already been done successfully. */
701 tc
= (struct tdb_crypto
*) malloc(sizeof (struct tdb_crypto
),
702 M_XDATA
, M_NOWAIT
|M_ZERO
);
705 DPRINTF(("ah_input: failed to allocate tdb_crypto\n"));
706 AH_STATINC(AH_STAT_CRYPTO
);
712 /* Only save information if crypto processing is needed. */
717 * Save the authenticator, the skipped portion of the packet,
720 m_copydata(m
, 0, skip
+ rplen
+ authsize
, (char *)(tc
+1));
723 u_int8_t
*pppp
= ((char *)(tc
+1))+skip
+rplen
;
724 DPRINTF(("ah_input: zeroing %d bytes of authent " \
725 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
727 pppp
[0], pppp
[1], pppp
[2], pppp
[3],
728 pppp
[4], pppp
[5], pppp
[6], pppp
[7],
729 pppp
[8], pppp
[9], pppp
[10], pppp
[11]));
732 /* Zeroize the authenticator on the packet. */
733 m_copyback(m
, skip
+ rplen
, authsize
, ipseczeroes
);
735 /* "Massage" the packet headers for crypto processing. */
736 error
= ah_massage_headers(&m
, sav
->sah
->saidx
.dst
.sa
.sa_family
,
739 /* NB: mbuf is free'd by ah_massage_headers */
740 AH_STATINC(AH_STAT_HDROPS
);
747 /* Crypto operation descriptor. */
748 crp
->crp_ilen
= m
->m_pkthdr
.len
; /* Total input length. */
749 crp
->crp_flags
= CRYPTO_F_IMBUF
;
751 crp
->crp_callback
= ah_input_cb
;
752 crp
->crp_sid
= sav
->tdb_cryptoid
;
753 crp
->crp_opaque
= tc
;
755 /* These are passed as-is to the callback. */
756 tc
->tc_spi
= sav
->spi
;
757 tc
->tc_dst
= sav
->sah
->saidx
.dst
;
758 tc
->tc_proto
= sav
->sah
->saidx
.proto
;
759 tc
->tc_nxt
= ah
->ah_nxt
;
760 tc
->tc_protoff
= protoff
;
762 tc
->tc_ptr
= mtag
; /* Save the mtag we've identified. */
764 DPRINTF(("ah: hash over %d bytes, skip %d: "
765 "crda len %d skip %d inject %d\n",
766 crp
->crp_ilen
, tc
->tc_skip
,
767 crda
->crd_len
, crda
->crd_skip
, crda
->crd_inject
));
770 return crypto_dispatch(crp
);
772 return ah_input_cb(crp
);
776 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
777 if (saidx->dst.sa.sa_family == AF_INET6) { \
778 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
780 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
784 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
785 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
789 * AH input callback from the crypto driver.
792 ah_input_cb(struct cryptop
*crp
)
794 int rplen
, error
, skip
, protoff
;
795 unsigned char calc
[AH_ALEN_MAX
];
797 struct cryptodesc
*crd
;
798 struct auth_hash
*ahx
;
799 struct tdb_crypto
*tc
;
801 struct secasvar
*sav
;
802 struct secasindex
*saidx
;
809 struct m_tag
* tag
= NULL
;
814 tc
= (struct tdb_crypto
*) crp
->crp_opaque
;
815 IPSEC_ASSERT(tc
!= NULL
, ("ah_input_cb: null opaque crypto data area!"));
818 protoff
= tc
->tc_protoff
;
819 mtag
= (struct m_tag
*) tc
->tc_ptr
;
820 m
= (struct mbuf
*) crp
->crp_buf
;
824 /* find the source port for NAT-T */
825 if ((tag
= m_tag_find(m
, PACKET_TAG_IPSEC_NAT_T_PORTS
, NULL
))) {
826 sport
= ((u_int16_t
*)(tag
+ 1))[0];
827 dport
= ((u_int16_t
*)(tag
+ 1))[1];
833 sav
= KEY_ALLOCSA(&tc
->tc_dst
, tc
->tc_proto
, tc
->tc_spi
, sport
, dport
);
835 AH_STATINC(AH_STAT_NOTDB
);
836 DPRINTF(("ah_input_cb: SA expired while in crypto\n"));
837 error
= ENOBUFS
; /*XXX*/
841 saidx
= &sav
->sah
->saidx
;
842 IPSEC_ASSERT(saidx
->dst
.sa
.sa_family
== AF_INET
||
843 saidx
->dst
.sa
.sa_family
== AF_INET6
,
844 ("ah_input_cb: unexpected protocol family %u",
845 saidx
->dst
.sa
.sa_family
));
847 ahx
= (struct auth_hash
*) sav
->tdb_authalgxform
;
849 /* Check for crypto errors. */
850 if (crp
->crp_etype
) {
851 if (sav
->tdb_cryptoid
!= 0)
852 sav
->tdb_cryptoid
= crp
->crp_sid
;
854 if (crp
->crp_etype
== EAGAIN
)
855 return crypto_dispatch(crp
);
857 AH_STATINC(AH_STAT_NOXFORM
);
858 DPRINTF(("ah_input_cb: crypto error %d\n", crp
->crp_etype
));
859 error
= crp
->crp_etype
;
862 AH_STATINC(AH_STAT_HIST
+ sav
->alg_auth
);
863 crypto_freereq(crp
); /* No longer needed. */
867 /* Shouldn't happen... */
869 AH_STATINC(AH_STAT_CRYPTO
);
870 DPRINTF(("ah_input_cb: bogus returned buffer from crypto\n"));
875 /* Figure out header size. */
876 rplen
= HDRSIZE(sav
);
877 authsize
= AUTHSIZE(sav
);
880 memset(calc
, 0, sizeof(calc
));
882 /* Copy authenticator off the packet. */
883 m_copydata(m
, skip
+ rplen
, authsize
, calc
);
886 * If we have an mtag, we don't need to verify the authenticator --
887 * it has been verified by an IPsec-aware NIC.
890 ptr
= (char *) (tc
+ 1);
892 /* Verify authenticator. */
893 if (memcmp(ptr
+ skip
+ rplen
, calc
, authsize
)) {
894 u_int8_t
*pppp
= ptr
+ skip
+rplen
;
895 DPRINTF(("ah_input: authentication hash mismatch " \
897 "for packet in SA %s/%08lx:\n" \
898 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, " \
899 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
901 ipsec_address(&saidx
->dst
),
902 (u_long
) ntohl(sav
->spi
),
903 calc
[0], calc
[1], calc
[2], calc
[3],
904 calc
[4], calc
[5], calc
[6], calc
[7],
905 calc
[8], calc
[9], calc
[10], calc
[11],
906 pppp
[0], pppp
[1], pppp
[2], pppp
[3],
907 pppp
[4], pppp
[5], pppp
[6], pppp
[7],
908 pppp
[8], pppp
[9], pppp
[10], pppp
[11]
910 AH_STATINC(AH_STAT_BADAUTH
);
915 /* Fix the Next Protocol field. */
916 ((u_int8_t
*) ptr
)[protoff
] = nxt
;
918 /* Copyback the saved (uncooked) network headers. */
919 m_copyback(m
, 0, skip
, ptr
);
921 /* Fix the Next Protocol field. */
922 m_copyback(m
, protoff
, sizeof(u_int8_t
), &nxt
);
925 free(tc
, M_XDATA
), tc
= NULL
; /* No longer needed */
928 * Header is now authenticated.
930 m
->m_flags
|= M_AUTHIPHDR
|M_AUTHIPDGM
;
933 * Update replay sequence number, if appropriate.
938 m_copydata(m
, skip
+ offsetof(struct newah
, ah_seq
),
940 if (ipsec_updatereplay(ntohl(seq
), sav
)) {
941 AH_STATINC(AH_STAT_REPLAY
);
942 error
= ENOBUFS
; /*XXX as above*/
948 * Remove the AH header and authenticator from the mbuf.
950 error
= m_striphdr(m
, skip
, rplen
+ authsize
);
952 DPRINTF(("ah_input_cb: mangled mbuf chain for SA %s/%08lx\n",
953 ipsec_address(&saidx
->dst
), (u_long
) ntohl(sav
->spi
)));
955 AH_STATINC(AH_STAT_HDROPS
);
959 IPSEC_COMMON_INPUT_CB(m
, sav
, skip
, protoff
, mtag
);
978 * AH output routine, called by ipsec[46]_process_packet().
983 struct ipsecrequest
*isr
,
989 struct secasvar
*sav
;
990 struct auth_hash
*ahx
;
991 struct cryptodesc
*crda
;
992 struct tdb_crypto
*tc
;
996 int error
, rplen
, authsize
, maxpacketsize
, roff
;
1000 IPSEC_SPLASSERT_SOFTNET("ah_output");
1003 IPSEC_ASSERT(sav
!= NULL
, ("ah_output: null SA"));
1004 ahx
= sav
->tdb_authalgxform
;
1005 IPSEC_ASSERT(ahx
!= NULL
, ("ah_output: null authentication xform"));
1007 AH_STATINC(AH_STAT_OUTPUT
);
1009 /* Figure out header size. */
1010 rplen
= HDRSIZE(sav
);
1012 /* Check for maximum packet size violations. */
1013 switch (sav
->sah
->saidx
.dst
.sa
.sa_family
) {
1016 maxpacketsize
= IP_MAXPACKET
;
1021 maxpacketsize
= IPV6_MAXPACKET
;
1025 DPRINTF(("ah_output: unknown/unsupported protocol "
1026 "family %u, SA %s/%08lx\n",
1027 sav
->sah
->saidx
.dst
.sa
.sa_family
,
1028 ipsec_address(&sav
->sah
->saidx
.dst
),
1029 (u_long
) ntohl(sav
->spi
)));
1030 AH_STATINC(AH_STAT_NOPF
);
1031 error
= EPFNOSUPPORT
;
1034 authsize
= AUTHSIZE(sav
);
1035 if (rplen
+ authsize
+ m
->m_pkthdr
.len
> maxpacketsize
) {
1036 DPRINTF(("ah_output: packet in SA %s/%08lx got too big "
1037 "(len %u, max len %u)\n",
1038 ipsec_address(&sav
->sah
->saidx
.dst
),
1039 (u_long
) ntohl(sav
->spi
),
1040 rplen
+ authsize
+ m
->m_pkthdr
.len
, maxpacketsize
));
1041 AH_STATINC(AH_STAT_TOOBIG
);
1046 /* Update the counters. */
1047 AH_STATADD(AH_STAT_OBYTES
, m
->m_pkthdr
.len
- skip
);
1051 DPRINTF(("ah_output: cannot clone mbuf chain, SA %s/%08lx\n",
1052 ipsec_address(&sav
->sah
->saidx
.dst
),
1053 (u_long
) ntohl(sav
->spi
)));
1054 AH_STATINC(AH_STAT_HDROPS
);
1059 /* Inject AH header. */
1060 mi
= m_makespace(m
, skip
, rplen
+ authsize
, &roff
);
1062 DPRINTF(("ah_output: failed to inject %u byte AH header for SA "
1065 ipsec_address(&sav
->sah
->saidx
.dst
),
1066 (u_long
) ntohl(sav
->spi
)));
1067 AH_STATINC(AH_STAT_HDROPS
); /*XXX differs from openbsd */
1073 * The AH header is guaranteed by m_makespace() to be in
1074 * contiguous memory, at roff bytes offset into the returned mbuf.
1076 ah
= (struct newah
*)(mtod(mi
, char *) + roff
);
1078 /* Initialize the AH header. */
1079 m_copydata(m
, protoff
, sizeof(u_int8_t
), (char *) &ah
->ah_nxt
);
1080 ah
->ah_len
= (rplen
+ authsize
- sizeof(struct ah
)) / sizeof(u_int32_t
);
1082 ah
->ah_spi
= sav
->spi
;
1084 /* Zeroize authenticator. */
1085 m_copyback(m
, skip
+ rplen
, authsize
, ipseczeroes
);
1087 /* Insert packet replay counter, as requested. */
1089 if (sav
->replay
->count
== ~0 &&
1090 (sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
1091 DPRINTF(("ah_output: replay counter wrapped for SA "
1093 ipsec_address(&sav
->sah
->saidx
.dst
),
1094 (u_long
) ntohl(sav
->spi
)));
1095 AH_STATINC(AH_STAT_WRAP
);
1100 /* Emulate replay attack when ipsec_replay is TRUE. */
1103 sav
->replay
->count
++;
1104 ah
->ah_seq
= htonl(sav
->replay
->count
);
1107 /* Get crypto descriptors. */
1108 crp
= crypto_getreq(1);
1110 DPRINTF(("ah_output: failed to acquire crypto descriptors\n"));
1111 AH_STATINC(AH_STAT_CRYPTO
);
1116 crda
= crp
->crp_desc
;
1119 crda
->crd_inject
= skip
+ rplen
;
1120 crda
->crd_len
= m
->m_pkthdr
.len
;
1122 /* Authentication operation. */
1123 crda
->crd_alg
= ahx
->type
;
1124 crda
->crd_key
= _KEYBUF(sav
->key_auth
);
1125 crda
->crd_klen
= _KEYBITS(sav
->key_auth
);
1127 /* Allocate IPsec-specific opaque crypto info. */
1128 tc
= (struct tdb_crypto
*) malloc(
1129 sizeof(struct tdb_crypto
) + skip
, M_XDATA
, M_NOWAIT
|M_ZERO
);
1131 crypto_freereq(crp
);
1132 DPRINTF(("ah_output: failed to allocate tdb_crypto\n"));
1133 AH_STATINC(AH_STAT_CRYPTO
);
1138 /* Save the skipped portion of the packet. */
1139 m_copydata(m
, 0, skip
, (tc
+ 1));
1142 * Fix IP header length on the header used for
1143 * authentication. We don't need to fix the original
1144 * header length as it will be fixed by our caller.
1146 switch (sav
->sah
->saidx
.dst
.sa
.sa_family
) {
1149 bcopy(((char *)(tc
+ 1)) +
1150 offsetof(struct ip
, ip_len
),
1151 &iplen
, sizeof(u_int16_t
));
1152 iplen
= htons(ntohs(iplen
) + rplen
+ authsize
);
1153 m_copyback(m
, offsetof(struct ip
, ip_len
),
1154 sizeof(u_int16_t
), &iplen
);
1160 bcopy(((char *)(tc
+ 1)) +
1161 offsetof(struct ip6_hdr
, ip6_plen
),
1162 &iplen
, sizeof(u_int16_t
));
1163 iplen
= htons(ntohs(iplen
) + rplen
+ authsize
);
1164 m_copyback(m
, offsetof(struct ip6_hdr
, ip6_plen
),
1165 sizeof(u_int16_t
), &iplen
);
1170 /* Fix the Next Header field in saved header. */
1171 ((u_int8_t
*) (tc
+ 1))[protoff
] = IPPROTO_AH
;
1173 /* Update the Next Protocol field in the IP header. */
1175 m_copyback(m
, protoff
, sizeof(u_int8_t
), &prot
);
1177 /* "Massage" the packet headers for crypto processing. */
1178 error
= ah_massage_headers(&m
, sav
->sah
->saidx
.dst
.sa
.sa_family
,
1179 skip
, ahx
->type
, 1);
1181 m
= NULL
; /* mbuf was free'd by ah_massage_headers. */
1183 crypto_freereq(crp
);
1187 /* Crypto operation descriptor. */
1188 crp
->crp_ilen
= m
->m_pkthdr
.len
; /* Total input length. */
1189 crp
->crp_flags
= CRYPTO_F_IMBUF
;
1191 crp
->crp_callback
= ah_output_cb
;
1192 crp
->crp_sid
= sav
->tdb_cryptoid
;
1193 crp
->crp_opaque
= tc
;
1195 /* These are passed as-is to the callback. */
1197 tc
->tc_spi
= sav
->spi
;
1198 tc
->tc_dst
= sav
->sah
->saidx
.dst
;
1199 tc
->tc_proto
= sav
->sah
->saidx
.proto
;
1201 tc
->tc_protoff
= protoff
;
1203 return crypto_dispatch(crp
);
1211 * AH output callback from the crypto driver.
1214 ah_output_cb(struct cryptop
*crp
)
1216 int skip
, protoff
, error
;
1217 struct tdb_crypto
*tc
;
1218 struct ipsecrequest
*isr
;
1219 struct secasvar
*sav
;
1224 tc
= (struct tdb_crypto
*) crp
->crp_opaque
;
1225 IPSEC_ASSERT(tc
!= NULL
, ("ah_output_cb: null opaque data area!"));
1227 protoff
= tc
->tc_protoff
;
1229 m
= (struct mbuf
*) crp
->crp_buf
;
1234 sav
= KEY_ALLOCSA(&tc
->tc_dst
, tc
->tc_proto
, tc
->tc_spi
, 0, 0);
1236 AH_STATINC(AH_STAT_NOTDB
);
1237 DPRINTF(("ah_output_cb: SA expired while in crypto\n"));
1238 error
= ENOBUFS
; /*XXX*/
1241 IPSEC_ASSERT(isr
->sav
== sav
, ("ah_output_cb: SA changed\n"));
1243 /* Check for crypto errors. */
1244 if (crp
->crp_etype
) {
1245 if (sav
->tdb_cryptoid
!= 0)
1246 sav
->tdb_cryptoid
= crp
->crp_sid
;
1248 if (crp
->crp_etype
== EAGAIN
) {
1251 return crypto_dispatch(crp
);
1254 AH_STATINC(AH_STAT_NOXFORM
);
1255 DPRINTF(("ah_output_cb: crypto error %d\n", crp
->crp_etype
));
1256 error
= crp
->crp_etype
;
1260 /* Shouldn't happen... */
1262 AH_STATINC(AH_STAT_CRYPTO
);
1263 DPRINTF(("ah_output_cb: bogus returned buffer from crypto\n"));
1267 AH_STATINC(AH_STAT_HIST
+ sav
->alg_auth
);
1270 * Copy original headers (with the new protocol number) back
1273 m_copyback(m
, 0, skip
, ptr
);
1275 /* No longer needed. */
1277 crypto_freereq(crp
);
1280 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
1281 if (ipsec_integrity
) {
1285 * Corrupt HMAC if we want to test integrity verification of
1288 alen
= AUTHSIZE(sav
);
1289 m_copyback(m
, m
->m_pkthdr
.len
- alen
, alen
, ipseczeroes
);
1293 /* NB: m is reclaimed by ipsec_process_done. */
1294 err
= ipsec_process_done(m
, isr
);
1305 crypto_freereq(crp
);
1309 static struct xformsw ah_xformsw
= {
1310 XF_AH
, XFT_AUTH
, "IPsec AH",
1311 ah_init
, ah_zeroize
, ah_input
, ah_output
,
1318 ahstat_percpu
= percpu_alloc(sizeof(uint64_t) * AH_NSTATS
);
1319 xform_register(&ah_xformsw
);
1323 SYSINIT(ah_xform_init
, SI_SUB_PROTO_DOMAIN
, SI_ORDER_MIDDLE
, ah_attach
, NULL
);