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