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