xref: /freebsd/sys/netipsec/xform_ah.c (revision 5861f9665471e98e544f6fa3ce73c4912229ff82)
1 /*	$FreeBSD$	*/
2 /*	$OpenBSD: ip_ah.c,v 1.63 2001/06/26 06:18:58 angelos Exp $ */
3 /*-
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).
7  *
8  * The original version of this code was written by John Ioannidis
9  * for BSD/OS in Athens, Greece, in November 1995.
10  *
11  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12  * by Angelos D. Keromytis.
13  *
14  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15  * and Niels Provos.
16  *
17  * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist.
18  *
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.
23  *
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
31  * all.
32  *
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
37  * PURPOSE.
38  */
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/syslog.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/vimage.h>
50 
51 #include <net/if.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_ecn.h>
57 #include <netinet/ip6.h>
58 
59 #include <net/route.h>
60 #include <netipsec/ipsec.h>
61 #include <netipsec/ah.h>
62 #include <netipsec/ah_var.h>
63 #include <netipsec/xform.h>
64 
65 #ifdef INET6
66 #include <netinet6/ip6_var.h>
67 #include <netipsec/ipsec6.h>
68 #include <netinet6/ip6_ecn.h>
69 #endif
70 
71 #include <netipsec/key.h>
72 #include <netipsec/key_debug.h>
73 
74 #include <opencrypto/cryptodev.h>
75 
76 static int ah_iattach(const void *);
77 
78 #ifndef VIMAGE_GLOBALS
79 static const vnet_modinfo_t vnet_ah_modinfo = {
80 	.vmi_id		= VNET_MOD_AH,
81 	.vmi_name	= "ipsec_ah",
82 	.vmi_dependson	= VNET_MOD_IPSEC,
83 	.vmi_iattach	= ah_iattach
84 };
85 #endif /* !VIMAGE_GLOBALS */
86 
87 /*
88  * Return header size in bytes.  The old protocol did not support
89  * the replay counter; the new protocol always includes the counter.
90  */
91 #define HDRSIZE(sav) \
92 	(((sav)->flags & SADB_X_EXT_OLD) ? \
93 		sizeof (struct ah) : sizeof (struct ah) + sizeof (u_int32_t))
94 /*
95  * Return authenticator size in bytes.  The old protocol is known
96  * to use a fixed 16-byte authenticator.  The new algorithm use 12-byte
97  * authenticator.
98  */
99 #define	AUTHSIZE(sav) \
100 	((sav->flags & SADB_X_EXT_OLD) ? 16 : AH_HMAC_HASHLEN)
101 
102 #ifdef VIMAGE_GLOBALS
103 int	ah_enable;
104 int	ah_cleartos;
105 struct	ahstat ahstat;
106 #endif
107 
108 SYSCTL_DECL(_net_inet_ah);
109 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ah, OID_AUTO,
110 	ah_enable,	CTLFLAG_RW,	ah_enable,	0, "");
111 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ah, OID_AUTO,
112 	ah_cleartos,	CTLFLAG_RW,	ah_cleartos,	0, "");
113 SYSCTL_V_STRUCT(V_NET, vnet_ipsec, _net_inet_ah, IPSECCTL_STATS,
114 	stats,		CTLFLAG_RD,	ahstat,	ahstat, "");
115 
116 static unsigned char ipseczeroes[256];	/* larger than an ip6 extension hdr */
117 
118 static int ah_input_cb(struct cryptop*);
119 static int ah_output_cb(struct cryptop*);
120 
121 /*
122  * NB: this is public for use by the PF_KEY support.
123  */
124 struct auth_hash *
125 ah_algorithm_lookup(int alg)
126 {
127 	if (alg > SADB_AALG_MAX)
128 		return NULL;
129 	switch (alg) {
130 	case SADB_X_AALG_NULL:
131 		return &auth_hash_null;
132 	case SADB_AALG_MD5HMAC:
133 		return &auth_hash_hmac_md5;
134 	case SADB_AALG_SHA1HMAC:
135 		return &auth_hash_hmac_sha1;
136 	case SADB_X_AALG_RIPEMD160HMAC:
137 		return &auth_hash_hmac_ripemd_160;
138 	case SADB_X_AALG_MD5:
139 		return &auth_hash_key_md5;
140 	case SADB_X_AALG_SHA:
141 		return &auth_hash_key_sha1;
142 	case SADB_X_AALG_SHA2_256:
143 		return &auth_hash_hmac_sha2_256;
144 	case SADB_X_AALG_SHA2_384:
145 		return &auth_hash_hmac_sha2_384;
146 	case SADB_X_AALG_SHA2_512:
147 		return &auth_hash_hmac_sha2_512;
148 	}
149 	return NULL;
150 }
151 
152 size_t
153 ah_hdrsiz(struct secasvar *sav)
154 {
155 	size_t size;
156 
157 	if (sav != NULL) {
158 		int authsize;
159 		IPSEC_ASSERT(sav->tdb_authalgxform != NULL, ("null xform"));
160 		/*XXX not right for null algorithm--does it matter??*/
161 		authsize = AUTHSIZE(sav);
162 		size = roundup(authsize, sizeof (u_int32_t)) + HDRSIZE(sav);
163 	} else {
164 		/* default guess */
165 		size = sizeof (struct ah) + sizeof (u_int32_t) + 16;
166 	}
167 	return size;
168 }
169 
170 /*
171  * NB: public for use by esp_init.
172  */
173 int
174 ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria)
175 {
176 	INIT_VNET_IPSEC(curvnet);
177 	struct auth_hash *thash;
178 	int keylen;
179 
180 	thash = ah_algorithm_lookup(sav->alg_auth);
181 	if (thash == NULL) {
182 		DPRINTF(("%s: unsupported authentication algorithm %u\n",
183 			__func__, sav->alg_auth));
184 		return EINVAL;
185 	}
186 	/*
187 	 * Verify the replay state block allocation is consistent with
188 	 * the protocol type.  We check here so we can make assumptions
189 	 * later during protocol processing.
190 	 */
191 	/* NB: replay state is setup elsewhere (sigh) */
192 	if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) {
193 		DPRINTF(("%s: replay state block inconsistency, "
194 			"%s algorithm %s replay state\n", __func__,
195 			(sav->flags & SADB_X_EXT_OLD) ? "old" : "new",
196 			sav->replay == NULL ? "without" : "with"));
197 		return EINVAL;
198 	}
199 	if (sav->key_auth == NULL) {
200 		DPRINTF(("%s: no authentication key for %s algorithm\n",
201 			__func__, thash->name));
202 		return EINVAL;
203 	}
204 	keylen = _KEYLEN(sav->key_auth);
205 	if (keylen != thash->keysize && thash->keysize != 0) {
206 		DPRINTF(("%s: invalid keylength %d, algorithm %s requires "
207 			"keysize %d\n", __func__,
208 			 keylen, thash->name, thash->keysize));
209 		return EINVAL;
210 	}
211 
212 	sav->tdb_xform = xsp;
213 	sav->tdb_authalgxform = thash;
214 
215 	/* Initialize crypto session. */
216 	bzero(cria, sizeof (*cria));
217 	cria->cri_alg = sav->tdb_authalgxform->type;
218 	cria->cri_klen = _KEYBITS(sav->key_auth);
219 	cria->cri_key = sav->key_auth->key_data;
220 	cria->cri_mlen = AUTHSIZE(sav);
221 
222 	return 0;
223 }
224 
225 /*
226  * ah_init() is called when an SPI is being set up.
227  */
228 static int
229 ah_init(struct secasvar *sav, struct xformsw *xsp)
230 {
231 	INIT_VNET_IPSEC(curvnet);
232 	struct cryptoini cria;
233 	int error;
234 
235 	error = ah_init0(sav, xsp, &cria);
236 	return error ? error :
237 		 crypto_newsession(&sav->tdb_cryptoid, &cria, V_crypto_support);
238 }
239 
240 /*
241  * Paranoia.
242  *
243  * NB: public for use by esp_zeroize (XXX).
244  */
245 int
246 ah_zeroize(struct secasvar *sav)
247 {
248 	int err;
249 
250 	if (sav->key_auth)
251 		bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth));
252 
253 	err = crypto_freesession(sav->tdb_cryptoid);
254 	sav->tdb_cryptoid = 0;
255 	sav->tdb_authalgxform = NULL;
256 	sav->tdb_xform = NULL;
257 	return err;
258 }
259 
260 /*
261  * Massage IPv4/IPv6 headers for AH processing.
262  */
263 static int
264 ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out)
265 {
266 	INIT_VNET_IPSEC(curvnet);
267 	struct mbuf *m = *m0;
268 	unsigned char *ptr;
269 	int off, count;
270 
271 #ifdef INET
272 	struct ip *ip;
273 #endif /* INET */
274 
275 #ifdef INET6
276 	struct ip6_ext *ip6e;
277 	struct ip6_hdr ip6;
278 	int alloc, len, ad;
279 #endif /* INET6 */
280 
281 	switch (proto) {
282 #ifdef INET
283 	case AF_INET:
284 		/*
285 		 * This is the least painful way of dealing with IPv4 header
286 		 * and option processing -- just make sure they're in
287 		 * contiguous memory.
288 		 */
289 		*m0 = m = m_pullup(m, skip);
290 		if (m == NULL) {
291 			DPRINTF(("%s: m_pullup failed\n", __func__));
292 			return ENOBUFS;
293 		}
294 
295 		/* Fix the IP header */
296 		ip = mtod(m, struct ip *);
297 		if (V_ah_cleartos)
298 			ip->ip_tos = 0;
299 		ip->ip_ttl = 0;
300 		ip->ip_sum = 0;
301 
302 		/*
303 		 * On input, fix ip_len which has been byte-swapped
304 		 * at ip_input().
305 		 */
306 		if (!out) {
307 			ip->ip_len = htons(ip->ip_len + skip);
308 
309 			if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
310 				ip->ip_off = htons(ip->ip_off & IP_DF);
311 			else
312 				ip->ip_off = 0;
313 		} else {
314 			if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK)
315 				ip->ip_off = htons(ntohs(ip->ip_off) & IP_DF);
316 			else
317 				ip->ip_off = 0;
318 		}
319 
320 		ptr = mtod(m, unsigned char *) + sizeof(struct ip);
321 
322 		/* IPv4 option processing */
323 		for (off = sizeof(struct ip); off < skip;) {
324 			if (ptr[off] == IPOPT_EOL || ptr[off] == IPOPT_NOP ||
325 			    off + 1 < skip)
326 				;
327 			else {
328 				DPRINTF(("%s: illegal IPv4 option length for "
329 					"option %d\n", __func__, ptr[off]));
330 
331 				m_freem(m);
332 				return EINVAL;
333 			}
334 
335 			switch (ptr[off]) {
336 			case IPOPT_EOL:
337 				off = skip;  /* End the loop. */
338 				break;
339 
340 			case IPOPT_NOP:
341 				off++;
342 				break;
343 
344 			case IPOPT_SECURITY:	/* 0x82 */
345 			case 0x85:	/* Extended security. */
346 			case 0x86:	/* Commercial security. */
347 			case 0x94:	/* Router alert */
348 			case 0x95:	/* RFC1770 */
349 				/* Sanity check for option length. */
350 				if (ptr[off + 1] < 2) {
351 					DPRINTF(("%s: illegal IPv4 option "
352 						"length for option %d\n",
353 						__func__, ptr[off]));
354 
355 					m_freem(m);
356 					return EINVAL;
357 				}
358 
359 				off += ptr[off + 1];
360 				break;
361 
362 			case IPOPT_LSRR:
363 			case IPOPT_SSRR:
364 				/* Sanity check for option length. */
365 				if (ptr[off + 1] < 2) {
366 					DPRINTF(("%s: illegal IPv4 option "
367 						"length for option %d\n",
368 						__func__, ptr[off]));
369 
370 					m_freem(m);
371 					return EINVAL;
372 				}
373 
374 				/*
375 				 * On output, if we have either of the
376 				 * source routing options, we should
377 				 * swap the destination address of the
378 				 * IP header with the last address
379 				 * specified in the option, as that is
380 				 * what the destination's IP header
381 				 * will look like.
382 				 */
383 				if (out)
384 					bcopy(ptr + off + ptr[off + 1] -
385 					    sizeof(struct in_addr),
386 					    &(ip->ip_dst), sizeof(struct in_addr));
387 
388 				/* Fall through */
389 			default:
390 				/* Sanity check for option length. */
391 				if (ptr[off + 1] < 2) {
392 					DPRINTF(("%s: illegal IPv4 option "
393 						"length for option %d\n",
394 						__func__, ptr[off]));
395 					m_freem(m);
396 					return EINVAL;
397 				}
398 
399 				/* Zeroize all other options. */
400 				count = ptr[off + 1];
401 				bcopy(ipseczeroes, ptr, count);
402 				off += count;
403 				break;
404 			}
405 
406 			/* Sanity check. */
407 			if (off > skip)	{
408 				DPRINTF(("%s: malformed IPv4 options header\n",
409 					__func__));
410 
411 				m_freem(m);
412 				return EINVAL;
413 			}
414 		}
415 
416 		break;
417 #endif /* INET */
418 
419 #ifdef INET6
420 	case AF_INET6:  /* Ugly... */
421 		/* Copy and "cook" the IPv6 header. */
422 		m_copydata(m, 0, sizeof(ip6), (caddr_t) &ip6);
423 
424 		/* We don't do IPv6 Jumbograms. */
425 		if (ip6.ip6_plen == 0) {
426 			DPRINTF(("%s: unsupported IPv6 jumbogram\n", __func__));
427 			m_freem(m);
428 			return EMSGSIZE;
429 		}
430 
431 		ip6.ip6_flow = 0;
432 		ip6.ip6_hlim = 0;
433 		ip6.ip6_vfc &= ~IPV6_VERSION_MASK;
434 		ip6.ip6_vfc |= IPV6_VERSION;
435 
436 		/* Scoped address handling. */
437 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src))
438 			ip6.ip6_src.s6_addr16[1] = 0;
439 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst))
440 			ip6.ip6_dst.s6_addr16[1] = 0;
441 
442 		/* Done with IPv6 header. */
443 		m_copyback(m, 0, sizeof(struct ip6_hdr), (caddr_t) &ip6);
444 
445 		/* Let's deal with the remaining headers (if any). */
446 		if (skip - sizeof(struct ip6_hdr) > 0) {
447 			if (m->m_len <= skip) {
448 				ptr = (unsigned char *) malloc(
449 				    skip - sizeof(struct ip6_hdr),
450 				    M_XDATA, M_NOWAIT);
451 				if (ptr == NULL) {
452 					DPRINTF(("%s: failed to allocate memory"
453 						"for IPv6 headers\n",__func__));
454 					m_freem(m);
455 					return ENOBUFS;
456 				}
457 
458 				/*
459 				 * Copy all the protocol headers after
460 				 * the IPv6 header.
461 				 */
462 				m_copydata(m, sizeof(struct ip6_hdr),
463 				    skip - sizeof(struct ip6_hdr), ptr);
464 				alloc = 1;
465 			} else {
466 				/* No need to allocate memory. */
467 				ptr = mtod(m, unsigned char *) +
468 				    sizeof(struct ip6_hdr);
469 				alloc = 0;
470 			}
471 		} else
472 			break;
473 
474 		off = ip6.ip6_nxt & 0xff; /* Next header type. */
475 
476 		for (len = 0; len < skip - sizeof(struct ip6_hdr);)
477 			switch (off) {
478 			case IPPROTO_HOPOPTS:
479 			case IPPROTO_DSTOPTS:
480 				ip6e = (struct ip6_ext *) (ptr + len);
481 
482 				/*
483 				 * Process the mutable/immutable
484 				 * options -- borrows heavily from the
485 				 * KAME code.
486 				 */
487 				for (count = len + sizeof(struct ip6_ext);
488 				     count < len + ((ip6e->ip6e_len + 1) << 3);) {
489 					if (ptr[count] == IP6OPT_PAD1) {
490 						count++;
491 						continue; /* Skip padding. */
492 					}
493 
494 					/* Sanity check. */
495 					if (count > len +
496 					    ((ip6e->ip6e_len + 1) << 3)) {
497 						m_freem(m);
498 
499 						/* Free, if we allocated. */
500 						if (alloc)
501 							free(ptr, M_XDATA);
502 						return EINVAL;
503 					}
504 
505 					ad = ptr[count + 1];
506 
507 					/* If mutable option, zeroize. */
508 					if (ptr[count] & IP6OPT_MUTABLE)
509 						bcopy(ipseczeroes, ptr + count,
510 						    ptr[count + 1]);
511 
512 					count += ad;
513 
514 					/* Sanity check. */
515 					if (count >
516 					    skip - sizeof(struct ip6_hdr)) {
517 						m_freem(m);
518 
519 						/* Free, if we allocated. */
520 						if (alloc)
521 							free(ptr, M_XDATA);
522 						return EINVAL;
523 					}
524 				}
525 
526 				/* Advance. */
527 				len += ((ip6e->ip6e_len + 1) << 3);
528 				off = ip6e->ip6e_nxt;
529 				break;
530 
531 			case IPPROTO_ROUTING:
532 				/*
533 				 * Always include routing headers in
534 				 * computation.
535 				 */
536 				ip6e = (struct ip6_ext *) (ptr + len);
537 				len += ((ip6e->ip6e_len + 1) << 3);
538 				off = ip6e->ip6e_nxt;
539 				break;
540 
541 			default:
542 				DPRINTF(("%s: unexpected IPv6 header type %d",
543 					__func__, off));
544 				if (alloc)
545 					free(ptr, M_XDATA);
546 				m_freem(m);
547 				return EINVAL;
548 			}
549 
550 		/* Copyback and free, if we allocated. */
551 		if (alloc) {
552 			m_copyback(m, sizeof(struct ip6_hdr),
553 			    skip - sizeof(struct ip6_hdr), ptr);
554 			free(ptr, M_XDATA);
555 		}
556 
557 		break;
558 #endif /* INET6 */
559 	}
560 
561 	return 0;
562 }
563 
564 /*
565  * ah_input() gets called to verify that an input packet
566  * passes authentication.
567  */
568 static int
569 ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
570 {
571 	INIT_VNET_IPSEC(curvnet);
572 	struct auth_hash *ahx;
573 	struct tdb_ident *tdbi;
574 	struct tdb_crypto *tc;
575 	struct m_tag *mtag;
576 	struct newah *ah;
577 	int hl, rplen, authsize;
578 
579 	struct cryptodesc *crda;
580 	struct cryptop *crp;
581 
582 	IPSEC_ASSERT(sav != NULL, ("null SA"));
583 	IPSEC_ASSERT(sav->key_auth != NULL, ("null authentication key"));
584 	IPSEC_ASSERT(sav->tdb_authalgxform != NULL,
585 		("null authentication xform"));
586 
587 	/* Figure out header size. */
588 	rplen = HDRSIZE(sav);
589 
590 	/* XXX don't pullup, just copy header */
591 	IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen);
592 	if (ah == NULL) {
593 		DPRINTF(("ah_input: cannot pullup header\n"));
594 		V_ahstat.ahs_hdrops++;		/*XXX*/
595 		m_freem(m);
596 		return ENOBUFS;
597 	}
598 
599 	/* Check replay window, if applicable. */
600 	if (sav->replay && !ipsec_chkreplay(ntohl(ah->ah_seq), sav)) {
601 		V_ahstat.ahs_replay++;
602 		DPRINTF(("%s: packet replay failure: %s\n", __func__,
603 			  ipsec_logsastr(sav)));
604 		m_freem(m);
605 		return ENOBUFS;
606 	}
607 
608 	/* Verify AH header length. */
609 	hl = ah->ah_len * sizeof (u_int32_t);
610 	ahx = sav->tdb_authalgxform;
611 	authsize = AUTHSIZE(sav);
612 	if (hl != authsize + rplen - sizeof (struct ah)) {
613 		DPRINTF(("%s: bad authenticator length %u (expecting %lu)"
614 			" for packet in SA %s/%08lx\n", __func__,
615 			hl, (u_long) (authsize + rplen - sizeof (struct ah)),
616 			ipsec_address(&sav->sah->saidx.dst),
617 			(u_long) ntohl(sav->spi)));
618 		V_ahstat.ahs_badauthl++;
619 		m_freem(m);
620 		return EACCES;
621 	}
622 	V_ahstat.ahs_ibytes += m->m_pkthdr.len - skip - hl;
623 
624 	/* Get crypto descriptors. */
625 	crp = crypto_getreq(1);
626 	if (crp == NULL) {
627 		DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
628 		V_ahstat.ahs_crypto++;
629 		m_freem(m);
630 		return ENOBUFS;
631 	}
632 
633 	crda = crp->crp_desc;
634 	IPSEC_ASSERT(crda != NULL, ("null crypto descriptor"));
635 
636 	crda->crd_skip = 0;
637 	crda->crd_len = m->m_pkthdr.len;
638 	crda->crd_inject = skip + rplen;
639 
640 	/* Authentication operation. */
641 	crda->crd_alg = ahx->type;
642 	crda->crd_klen = _KEYBITS(sav->key_auth);
643 	crda->crd_key = sav->key_auth->key_data;
644 
645 	/* Find out if we've already done crypto. */
646 	for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL);
647 	     mtag != NULL;
648 	     mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) {
649 		tdbi = (struct tdb_ident *) (mtag + 1);
650 		if (tdbi->proto == sav->sah->saidx.proto &&
651 		    tdbi->spi == sav->spi &&
652 		    !bcmp(&tdbi->dst, &sav->sah->saidx.dst,
653 			  sizeof (union sockaddr_union)))
654 			break;
655 	}
656 
657 	/* Allocate IPsec-specific opaque crypto info. */
658 	if (mtag == NULL) {
659 		tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto) +
660 			skip + rplen + authsize, M_XDATA, M_NOWAIT|M_ZERO);
661 	} else {
662 		/* Hash verification has already been done successfully. */
663 		tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto),
664 						    M_XDATA, M_NOWAIT|M_ZERO);
665 	}
666 	if (tc == NULL) {
667 		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
668 		V_ahstat.ahs_crypto++;
669 		crypto_freereq(crp);
670 		m_freem(m);
671 		return ENOBUFS;
672 	}
673 
674 	/* Only save information if crypto processing is needed. */
675 	if (mtag == NULL) {
676 		int error;
677 
678 		/*
679 		 * Save the authenticator, the skipped portion of the packet,
680 		 * and the AH header.
681 		 */
682 		m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(tc+1));
683 
684 		/* Zeroize the authenticator on the packet. */
685 		m_copyback(m, skip + rplen, authsize, ipseczeroes);
686 
687 		/* "Massage" the packet headers for crypto processing. */
688 		error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
689 		    skip, ahx->type, 0);
690 		if (error != 0) {
691 			/* NB: mbuf is free'd by ah_massage_headers */
692 			V_ahstat.ahs_hdrops++;
693 			free(tc, M_XDATA);
694 			crypto_freereq(crp);
695 			return error;
696 		}
697 	}
698 
699 	/* Crypto operation descriptor. */
700 	crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
701 	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
702 	crp->crp_buf = (caddr_t) m;
703 	crp->crp_callback = ah_input_cb;
704 	crp->crp_sid = sav->tdb_cryptoid;
705 	crp->crp_opaque = (caddr_t) tc;
706 
707 	/* These are passed as-is to the callback. */
708 	tc->tc_spi = sav->spi;
709 	tc->tc_dst = sav->sah->saidx.dst;
710 	tc->tc_proto = sav->sah->saidx.proto;
711 	tc->tc_nxt = ah->ah_nxt;
712 	tc->tc_protoff = protoff;
713 	tc->tc_skip = skip;
714 	tc->tc_ptr = (caddr_t) mtag; /* Save the mtag we've identified. */
715 
716 	if (mtag == NULL)
717 		return crypto_dispatch(crp);
718 	else
719 		return ah_input_cb(crp);
720 }
721 
722 #ifdef INET6
723 #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do {		     \
724 	if (saidx->dst.sa.sa_family == AF_INET6) {			     \
725 		error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
726 	} else {							     \
727 		error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
728 	}								     \
729 } while (0)
730 #else
731 #define	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag)		     \
732 	(error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
733 #endif
734 
735 /*
736  * AH input callback from the crypto driver.
737  */
738 static int
739 ah_input_cb(struct cryptop *crp)
740 {
741 	INIT_VNET_IPSEC(curvnet);
742 	int rplen, error, skip, protoff;
743 	unsigned char calc[AH_ALEN_MAX];
744 	struct mbuf *m;
745 	struct cryptodesc *crd;
746 	struct auth_hash *ahx;
747 	struct tdb_crypto *tc;
748 	struct m_tag *mtag;
749 	struct secasvar *sav;
750 	struct secasindex *saidx;
751 	u_int8_t nxt;
752 	caddr_t ptr;
753 	int authsize;
754 
755 	crd = crp->crp_desc;
756 
757 	tc = (struct tdb_crypto *) crp->crp_opaque;
758 	IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
759 	skip = tc->tc_skip;
760 	nxt = tc->tc_nxt;
761 	protoff = tc->tc_protoff;
762 	mtag = (struct m_tag *) tc->tc_ptr;
763 	m = (struct mbuf *) crp->crp_buf;
764 
765 	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
766 	if (sav == NULL) {
767 		V_ahstat.ahs_notdb++;
768 		DPRINTF(("%s: SA expired while in crypto\n", __func__));
769 		error = ENOBUFS;		/*XXX*/
770 		goto bad;
771 	}
772 
773 	saidx = &sav->sah->saidx;
774 	IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
775 		saidx->dst.sa.sa_family == AF_INET6,
776 		("unexpected protocol family %u", saidx->dst.sa.sa_family));
777 
778 	ahx = (struct auth_hash *) sav->tdb_authalgxform;
779 
780 	/* Check for crypto errors. */
781 	if (crp->crp_etype) {
782 		if (sav->tdb_cryptoid != 0)
783 			sav->tdb_cryptoid = crp->crp_sid;
784 
785 		if (crp->crp_etype == EAGAIN) {
786 			error = crypto_dispatch(crp);
787 			return error;
788 		}
789 
790 		V_ahstat.ahs_noxform++;
791 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
792 		error = crp->crp_etype;
793 		goto bad;
794 	} else {
795 		V_ahstat.ahs_hist[sav->alg_auth]++;
796 		crypto_freereq(crp);		/* No longer needed. */
797 		crp = NULL;
798 	}
799 
800 	/* Shouldn't happen... */
801 	if (m == NULL) {
802 		V_ahstat.ahs_crypto++;
803 		DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
804 		error = EINVAL;
805 		goto bad;
806 	}
807 
808 	/* Figure out header size. */
809 	rplen = HDRSIZE(sav);
810 	authsize = AUTHSIZE(sav);
811 
812 	/* Copy authenticator off the packet. */
813 	m_copydata(m, skip + rplen, authsize, calc);
814 
815 	/*
816 	 * If we have an mtag, we don't need to verify the authenticator --
817 	 * it has been verified by an IPsec-aware NIC.
818 	 */
819 	if (mtag == NULL) {
820 		ptr = (caddr_t) (tc + 1);
821 
822 		/* Verify authenticator. */
823 		if (bcmp(ptr + skip + rplen, calc, authsize)) {
824 			DPRINTF(("%s: authentication hash mismatch for packet "
825 			    "in SA %s/%08lx\n", __func__,
826 			    ipsec_address(&saidx->dst),
827 			    (u_long) ntohl(sav->spi)));
828 			V_ahstat.ahs_badauth++;
829 			error = EACCES;
830 			goto bad;
831 		}
832 
833 		/* Fix the Next Protocol field. */
834 		((u_int8_t *) ptr)[protoff] = nxt;
835 
836 		/* Copyback the saved (uncooked) network headers. */
837 		m_copyback(m, 0, skip, ptr);
838 	} else {
839 		/* Fix the Next Protocol field. */
840 		m_copyback(m, protoff, sizeof(u_int8_t), &nxt);
841 	}
842 
843 	free(tc, M_XDATA), tc = NULL;			/* No longer needed */
844 
845 	/*
846 	 * Header is now authenticated.
847 	 */
848 	m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM;
849 
850 	/*
851 	 * Update replay sequence number, if appropriate.
852 	 */
853 	if (sav->replay) {
854 		u_int32_t seq;
855 
856 		m_copydata(m, skip + offsetof(struct newah, ah_seq),
857 			   sizeof (seq), (caddr_t) &seq);
858 		if (ipsec_updatereplay(ntohl(seq), sav)) {
859 			V_ahstat.ahs_replay++;
860 			error = ENOBUFS;			/*XXX as above*/
861 			goto bad;
862 		}
863 	}
864 
865 	/*
866 	 * Remove the AH header and authenticator from the mbuf.
867 	 */
868 	error = m_striphdr(m, skip, rplen + authsize);
869 	if (error) {
870 		DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__,
871 		    ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
872 
873 		V_ahstat.ahs_hdrops++;
874 		goto bad;
875 	}
876 
877 	IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag);
878 
879 	KEY_FREESAV(&sav);
880 	return error;
881 bad:
882 	if (sav)
883 		KEY_FREESAV(&sav);
884 	if (m != NULL)
885 		m_freem(m);
886 	if (tc != NULL)
887 		free(tc, M_XDATA);
888 	if (crp != NULL)
889 		crypto_freereq(crp);
890 	return error;
891 }
892 
893 /*
894  * AH output routine, called by ipsec[46]_process_packet().
895  */
896 static int
897 ah_output(
898 	struct mbuf *m,
899 	struct ipsecrequest *isr,
900 	struct mbuf **mp,
901 	int skip,
902 	int protoff)
903 {
904 	INIT_VNET_IPSEC(curvnet);
905 	struct secasvar *sav;
906 	struct auth_hash *ahx;
907 	struct cryptodesc *crda;
908 	struct tdb_crypto *tc;
909 	struct mbuf *mi;
910 	struct cryptop *crp;
911 	u_int16_t iplen;
912 	int error, rplen, authsize, maxpacketsize, roff;
913 	u_int8_t prot;
914 	struct newah *ah;
915 
916 	sav = isr->sav;
917 	IPSEC_ASSERT(sav != NULL, ("null SA"));
918 	ahx = sav->tdb_authalgxform;
919 	IPSEC_ASSERT(ahx != NULL, ("null authentication xform"));
920 
921 	V_ahstat.ahs_output++;
922 
923 	/* Figure out header size. */
924 	rplen = HDRSIZE(sav);
925 
926 	/* Check for maximum packet size violations. */
927 	switch (sav->sah->saidx.dst.sa.sa_family) {
928 #ifdef INET
929 	case AF_INET:
930 		maxpacketsize = IP_MAXPACKET;
931 		break;
932 #endif /* INET */
933 #ifdef INET6
934 	case AF_INET6:
935 		maxpacketsize = IPV6_MAXPACKET;
936 		break;
937 #endif /* INET6 */
938 	default:
939 		DPRINTF(("%s: unknown/unsupported protocol family %u, "
940 		    "SA %s/%08lx\n", __func__,
941 		    sav->sah->saidx.dst.sa.sa_family,
942 		    ipsec_address(&sav->sah->saidx.dst),
943 		    (u_long) ntohl(sav->spi)));
944 		V_ahstat.ahs_nopf++;
945 		error = EPFNOSUPPORT;
946 		goto bad;
947 	}
948 	authsize = AUTHSIZE(sav);
949 	if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) {
950 		DPRINTF(("%s: packet in SA %s/%08lx got too big "
951 		    "(len %u, max len %u)\n", __func__,
952 		    ipsec_address(&sav->sah->saidx.dst),
953 		    (u_long) ntohl(sav->spi),
954 		    rplen + authsize + m->m_pkthdr.len, maxpacketsize));
955 		V_ahstat.ahs_toobig++;
956 		error = EMSGSIZE;
957 		goto bad;
958 	}
959 
960 	/* Update the counters. */
961 	V_ahstat.ahs_obytes += m->m_pkthdr.len - skip;
962 
963 	m = m_unshare(m, M_NOWAIT);
964 	if (m == NULL) {
965 		DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
966 		    ipsec_address(&sav->sah->saidx.dst),
967 		    (u_long) ntohl(sav->spi)));
968 		V_ahstat.ahs_hdrops++;
969 		error = ENOBUFS;
970 		goto bad;
971 	}
972 
973 	/* Inject AH header. */
974 	mi = m_makespace(m, skip, rplen + authsize, &roff);
975 	if (mi == NULL) {
976 		DPRINTF(("%s: failed to inject %u byte AH header for SA "
977 		    "%s/%08lx\n", __func__,
978 		    rplen + authsize,
979 		    ipsec_address(&sav->sah->saidx.dst),
980 		    (u_long) ntohl(sav->spi)));
981 		V_ahstat.ahs_hdrops++;		/*XXX differs from openbsd */
982 		error = ENOBUFS;
983 		goto bad;
984 	}
985 
986 	/*
987 	 * The AH header is guaranteed by m_makespace() to be in
988 	 * contiguous memory, at roff bytes offset into the returned mbuf.
989 	 */
990 	ah = (struct newah *)(mtod(mi, caddr_t) + roff);
991 
992 	/* Initialize the AH header. */
993 	m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt);
994 	ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(u_int32_t);
995 	ah->ah_reserve = 0;
996 	ah->ah_spi = sav->spi;
997 
998 	/* Zeroize authenticator. */
999 	m_copyback(m, skip + rplen, authsize, ipseczeroes);
1000 
1001 	/* Insert packet replay counter, as requested.  */
1002 	if (sav->replay) {
1003 		if (sav->replay->count == ~0 &&
1004 		    (sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
1005 			DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n",
1006 				__func__,
1007 				ipsec_address(&sav->sah->saidx.dst),
1008 				(u_long) ntohl(sav->spi)));
1009 			V_ahstat.ahs_wrap++;
1010 			error = EINVAL;
1011 			goto bad;
1012 		}
1013 #ifdef REGRESSION
1014 		/* Emulate replay attack when ipsec_replay is TRUE. */
1015 		if (!V_ipsec_replay)
1016 #endif
1017 			sav->replay->count++;
1018 		ah->ah_seq = htonl(sav->replay->count);
1019 	}
1020 
1021 	/* Get crypto descriptors. */
1022 	crp = crypto_getreq(1);
1023 	if (crp == NULL) {
1024 		DPRINTF(("%s: failed to acquire crypto descriptors\n",
1025 			__func__));
1026 		V_ahstat.ahs_crypto++;
1027 		error = ENOBUFS;
1028 		goto bad;
1029 	}
1030 
1031 	crda = crp->crp_desc;
1032 
1033 	crda->crd_skip = 0;
1034 	crda->crd_inject = skip + rplen;
1035 	crda->crd_len = m->m_pkthdr.len;
1036 
1037 	/* Authentication operation. */
1038 	crda->crd_alg = ahx->type;
1039 	crda->crd_key = sav->key_auth->key_data;
1040 	crda->crd_klen = _KEYBITS(sav->key_auth);
1041 
1042 	/* Allocate IPsec-specific opaque crypto info. */
1043 	tc = (struct tdb_crypto *) malloc(
1044 		sizeof(struct tdb_crypto) + skip, M_XDATA, M_NOWAIT|M_ZERO);
1045 	if (tc == NULL) {
1046 		crypto_freereq(crp);
1047 		DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
1048 		V_ahstat.ahs_crypto++;
1049 		error = ENOBUFS;
1050 		goto bad;
1051 	}
1052 
1053 	/* Save the skipped portion of the packet. */
1054 	m_copydata(m, 0, skip, (caddr_t) (tc + 1));
1055 
1056 	/*
1057 	 * Fix IP header length on the header used for
1058 	 * authentication. We don't need to fix the original
1059 	 * header length as it will be fixed by our caller.
1060 	 */
1061 	switch (sav->sah->saidx.dst.sa.sa_family) {
1062 #ifdef INET
1063 	case AF_INET:
1064 		bcopy(((caddr_t)(tc + 1)) +
1065 		    offsetof(struct ip, ip_len),
1066 		    (caddr_t) &iplen, sizeof(u_int16_t));
1067 		iplen = htons(ntohs(iplen) + rplen + authsize);
1068 		m_copyback(m, offsetof(struct ip, ip_len),
1069 		    sizeof(u_int16_t), (caddr_t) &iplen);
1070 		break;
1071 #endif /* INET */
1072 
1073 #ifdef INET6
1074 	case AF_INET6:
1075 		bcopy(((caddr_t)(tc + 1)) +
1076 		    offsetof(struct ip6_hdr, ip6_plen),
1077 		    (caddr_t) &iplen, sizeof(u_int16_t));
1078 		iplen = htons(ntohs(iplen) + rplen + authsize);
1079 		m_copyback(m, offsetof(struct ip6_hdr, ip6_plen),
1080 		    sizeof(u_int16_t), (caddr_t) &iplen);
1081 		break;
1082 #endif /* INET6 */
1083 	}
1084 
1085 	/* Fix the Next Header field in saved header. */
1086 	((u_int8_t *) (tc + 1))[protoff] = IPPROTO_AH;
1087 
1088 	/* Update the Next Protocol field in the IP header. */
1089 	prot = IPPROTO_AH;
1090 	m_copyback(m, protoff, sizeof(u_int8_t), (caddr_t) &prot);
1091 
1092 	/* "Massage" the packet headers for crypto processing. */
1093 	error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
1094 			skip, ahx->type, 1);
1095 	if (error != 0) {
1096 		m = NULL;	/* mbuf was free'd by ah_massage_headers. */
1097 		free(tc, M_XDATA);
1098 		crypto_freereq(crp);
1099 		goto bad;
1100 	}
1101 
1102 	/* Crypto operation descriptor. */
1103 	crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
1104 	crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
1105 	crp->crp_buf = (caddr_t) m;
1106 	crp->crp_callback = ah_output_cb;
1107 	crp->crp_sid = sav->tdb_cryptoid;
1108 	crp->crp_opaque = (caddr_t) tc;
1109 
1110 	/* These are passed as-is to the callback. */
1111 	tc->tc_isr = isr;
1112 	tc->tc_spi = sav->spi;
1113 	tc->tc_dst = sav->sah->saidx.dst;
1114 	tc->tc_proto = sav->sah->saidx.proto;
1115 	tc->tc_skip = skip;
1116 	tc->tc_protoff = protoff;
1117 
1118 	return crypto_dispatch(crp);
1119 bad:
1120 	if (m)
1121 		m_freem(m);
1122 	return (error);
1123 }
1124 
1125 /*
1126  * AH output callback from the crypto driver.
1127  */
1128 static int
1129 ah_output_cb(struct cryptop *crp)
1130 {
1131 	INIT_VNET_IPSEC(curvnet);
1132 	int skip, protoff, error;
1133 	struct tdb_crypto *tc;
1134 	struct ipsecrequest *isr;
1135 	struct secasvar *sav;
1136 	struct mbuf *m;
1137 	caddr_t ptr;
1138 	int err;
1139 
1140 	tc = (struct tdb_crypto *) crp->crp_opaque;
1141 	IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
1142 	skip = tc->tc_skip;
1143 	protoff = tc->tc_protoff;
1144 	ptr = (caddr_t) (tc + 1);
1145 	m = (struct mbuf *) crp->crp_buf;
1146 
1147 	isr = tc->tc_isr;
1148 	IPSECREQUEST_LOCK(isr);
1149 	sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
1150 	if (sav == NULL) {
1151 		V_ahstat.ahs_notdb++;
1152 		DPRINTF(("%s: SA expired while in crypto\n", __func__));
1153 		error = ENOBUFS;		/*XXX*/
1154 		goto bad;
1155 	}
1156 	IPSEC_ASSERT(isr->sav == sav, ("SA changed\n"));
1157 
1158 	/* Check for crypto errors. */
1159 	if (crp->crp_etype) {
1160 		if (sav->tdb_cryptoid != 0)
1161 			sav->tdb_cryptoid = crp->crp_sid;
1162 
1163 		if (crp->crp_etype == EAGAIN) {
1164 			KEY_FREESAV(&sav);
1165 			IPSECREQUEST_UNLOCK(isr);
1166 			error = crypto_dispatch(crp);
1167 			return error;
1168 		}
1169 
1170 		V_ahstat.ahs_noxform++;
1171 		DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
1172 		error = crp->crp_etype;
1173 		goto bad;
1174 	}
1175 
1176 	/* Shouldn't happen... */
1177 	if (m == NULL) {
1178 		V_ahstat.ahs_crypto++;
1179 		DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
1180 		error = EINVAL;
1181 		goto bad;
1182 	}
1183 	V_ahstat.ahs_hist[sav->alg_auth]++;
1184 
1185 	/*
1186 	 * Copy original headers (with the new protocol number) back
1187 	 * in place.
1188 	 */
1189 	m_copyback(m, 0, skip, ptr);
1190 
1191 	/* No longer needed. */
1192 	free(tc, M_XDATA);
1193 	crypto_freereq(crp);
1194 
1195 #ifdef REGRESSION
1196 	/* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
1197 	if (V_ipsec_integrity) {
1198 		int alen;
1199 
1200 		/*
1201 		 * Corrupt HMAC if we want to test integrity verification of
1202 		 * the other side.
1203 		 */
1204 		alen = AUTHSIZE(sav);
1205 		m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes);
1206 	}
1207 #endif
1208 
1209 	/* NB: m is reclaimed by ipsec_process_done. */
1210 	err = ipsec_process_done(m, isr);
1211 	KEY_FREESAV(&sav);
1212 	IPSECREQUEST_UNLOCK(isr);
1213 	return err;
1214 bad:
1215 	if (sav)
1216 		KEY_FREESAV(&sav);
1217 	IPSECREQUEST_UNLOCK(isr);
1218 	if (m)
1219 		m_freem(m);
1220 	free(tc, M_XDATA);
1221 	crypto_freereq(crp);
1222 	return error;
1223 }
1224 
1225 static struct xformsw ah_xformsw = {
1226 	XF_AH,		XFT_AUTH,	"IPsec AH",
1227 	ah_init,	ah_zeroize,	ah_input,	ah_output,
1228 };
1229 
1230 static void
1231 ah_attach(void)
1232 {
1233 
1234 	xform_register(&ah_xformsw);
1235 #ifndef VIMAGE_GLOBALS
1236 	vnet_mod_register(&vnet_ah_modinfo);
1237 #else
1238 	ah_iattach(NULL);
1239 #endif
1240 }
1241 
1242 static int
1243 ah_iattach(const void *unused __unused)
1244 {
1245 	INIT_VNET_IPSEC(curvnet);
1246 
1247 	V_ah_enable = 1;	/* control flow of packets with AH */
1248 	V_ah_cleartos = 1;	/* clear ip_tos when doing AH calc */
1249 
1250 	return (0);
1251 }
1252 SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ah_attach, NULL);
1253