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