xform_ah.c revision 1.111 1 /* $NetBSD: xform_ah.c,v 1.111 2022/05/22 11:39:08 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 (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.111 2022/05/22 11:39:08 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
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 const char ipseczeroes[256];
108
109 int ah_max_authsize; /* max authsize over all algorithms */
110
111 static void ah_input_cb(struct cryptop *);
112 static void 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, 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 */
195 int
196 ah_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 */
249 static int
250 ah_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 */
267 void
268 ah_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 (void)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 */
285 static int
286 ah_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:
499 error6:
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 */
524 static int
525 ah_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 return crypto_dispatch(crp);
695
696 bad:
697 if (tc != NULL) {
698 if (__predict_true(pool_used))
699 pool_cache_put(ah_tdb_crypto_pool_cache, tc);
700 else
701 kmem_intr_free(tc, size);
702 }
703 if (crp != NULL)
704 crypto_freereq(crp);
705 if (m != NULL)
706 m_freem(m);
707 AH_STATINC(stat);
708 return error;
709 }
710
711 #ifdef INET6
712 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do { \
713 if (saidx->dst.sa.sa_family == AF_INET6) { \
714 (void)ipsec6_common_input_cb(m, sav, skip, protoff); \
715 } else { \
716 (void)ipsec4_common_input_cb(m, sav, skip, protoff); \
717 } \
718 } while (0)
719 #else
720 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) \
721 ((void)ipsec4_common_input_cb(m, sav, skip, protoff))
722 #endif
723
724 /*
725 * AH input callback from the crypto driver.
726 */
727 static void
728 ah_input_cb(struct cryptop *crp)
729 {
730 char buf[IPSEC_ADDRSTRLEN];
731 int rplen, ahsize, skip, protoff;
732 unsigned char calc[AH_ALEN_MAX];
733 struct mbuf *m;
734 struct tdb_crypto *tc;
735 struct secasvar *sav;
736 struct secasindex *saidx;
737 uint8_t nxt;
738 char *ptr;
739 int authsize;
740 bool pool_used;
741 size_t size;
742 IPSEC_DECLARE_LOCK_VARIABLE;
743
744 KASSERT(crp->crp_opaque != NULL);
745 tc = crp->crp_opaque;
746 skip = tc->tc_skip;
747 nxt = tc->tc_nxt;
748 protoff = tc->tc_protoff;
749 m = crp->crp_buf;
750
751 IPSEC_ACQUIRE_GLOBAL_LOCKS();
752
753 sav = tc->tc_sav;
754 saidx = &sav->sah->saidx;
755 KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
756 saidx->dst.sa.sa_family == AF_INET6,
757 "unexpected protocol family %u", saidx->dst.sa.sa_family);
758
759 /* Figure out header size. */
760 rplen = HDRSIZE(sav);
761 authsize = AUTHSIZE(sav);
762 ahsize = ah_hdrsiz(sav);
763
764 size = sizeof(*tc) + skip + rplen + authsize;
765 if (__predict_true(size <= ah_pool_item_size))
766 pool_used = true;
767 else
768 pool_used = false;
769
770 /* Check for crypto errors. */
771 if (crp->crp_etype) {
772 if (sav->tdb_cryptoid != 0)
773 sav->tdb_cryptoid = crp->crp_sid;
774
775 if (crp->crp_etype == EAGAIN) {
776 IPSEC_RELEASE_GLOBAL_LOCKS();
777 (void)crypto_dispatch(crp);
778 return;
779 }
780
781 AH_STATINC(AH_STAT_NOXFORM);
782 DPRINTF("crypto error %d\n", crp->crp_etype);
783 goto bad;
784 } else {
785 AH_STATINC(AH_STAT_HIST + ah_stats[sav->alg_auth]);
786 crypto_freereq(crp); /* No longer needed. */
787 crp = NULL;
788 }
789
790 if (ipsec_debug)
791 memset(calc, 0, sizeof(calc));
792
793 /* Copy authenticator off the packet. */
794 m_copydata(m, skip + rplen, authsize, calc);
795
796 ptr = (char *)(tc + 1);
797 const uint8_t *pppp = ptr + skip + rplen;
798
799 /* Verify authenticator. */
800 if (!consttime_memequal(pppp, calc, authsize)) {
801 DPRINTF("authentication hash mismatch " \
802 "over %d bytes " \
803 "for packet in SA %s/%08lx:\n" \
804 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, " \
805 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
806 authsize, ipsec_address(&saidx->dst, buf, sizeof(buf)),
807 (u_long) ntohl(sav->spi),
808 calc[0], calc[1], calc[2], calc[3],
809 calc[4], calc[5], calc[6], calc[7],
810 calc[8], calc[9], calc[10], calc[11],
811 pppp[0], pppp[1], pppp[2], pppp[3],
812 pppp[4], pppp[5], pppp[6], pppp[7],
813 pppp[8], pppp[9], pppp[10], pppp[11]);
814 AH_STATINC(AH_STAT_BADAUTH);
815 goto bad;
816 }
817
818 /* Fix the Next Protocol field. */
819 ptr[protoff] = nxt;
820
821 /* Copyback the saved (uncooked) network headers. */
822 m_copyback(m, 0, skip, ptr);
823
824 if (__predict_true(pool_used))
825 pool_cache_put(ah_tdb_crypto_pool_cache, tc);
826 else
827 kmem_intr_free(tc, size);
828 tc = NULL;
829
830 /*
831 * Header is now authenticated.
832 */
833 m->m_flags |= M_AUTHIPHDR;
834
835 /*
836 * Update replay sequence number, if appropriate.
837 */
838 if (sav->replay) {
839 uint32_t seq;
840
841 m_copydata(m, skip + offsetof(struct newah, ah_seq),
842 sizeof(seq), &seq);
843 if (ipsec_updatereplay(ntohl(seq), sav)) {
844 AH_STATINC(AH_STAT_REPLAY);
845 goto bad;
846 }
847 }
848
849 /*
850 * Remove the AH header and authenticator from the mbuf.
851 */
852 if (m_striphdr(m, skip, ahsize) != 0) {
853 DPRINTF("mangled mbuf chain for SA %s/%08lx\n",
854 ipsec_address(&saidx->dst, buf, sizeof(buf)),
855 (u_long) ntohl(sav->spi));
856
857 AH_STATINC(AH_STAT_HDROPS);
858 goto bad;
859 }
860
861 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
862
863 KEY_SA_UNREF(&sav);
864 IPSEC_RELEASE_GLOBAL_LOCKS();
865 return;
866
867 bad:
868 if (sav)
869 KEY_SA_UNREF(&sav);
870 IPSEC_RELEASE_GLOBAL_LOCKS();
871 if (m != NULL)
872 m_freem(m);
873 if (tc != NULL) {
874 if (pool_used)
875 pool_cache_put(ah_tdb_crypto_pool_cache, tc);
876 else
877 kmem_intr_free(tc, size);
878 }
879 if (crp != NULL)
880 crypto_freereq(crp);
881 return;
882 }
883
884 /*
885 * AH output routine, called by ipsec[46]_process_packet().
886 */
887 static int
888 ah_output(struct mbuf *m, const struct ipsecrequest *isr, struct secasvar *sav,
889 int skip, int protoff, int flags)
890 {
891 char buf[IPSEC_ADDRSTRLEN];
892 const struct auth_hash *ahx;
893 struct cryptodesc *crda;
894 struct tdb_crypto *tc;
895 struct mbuf *mi;
896 struct cryptop *crp;
897 uint16_t iplen;
898 int error, rplen, authsize, ahsize, maxpacketsize, roff;
899 uint8_t prot;
900 struct newah *ah;
901 size_t ipoffs;
902 bool pool_used;
903
904 KASSERT(sav != NULL);
905 KASSERT(sav->tdb_authalgxform != NULL);
906 ahx = sav->tdb_authalgxform;
907
908 AH_STATINC(AH_STAT_OUTPUT);
909
910 /* Figure out header size. */
911 rplen = HDRSIZE(sav);
912 authsize = AUTHSIZE(sav);
913 ahsize = ah_hdrsiz(sav);
914
915 /* Check for maximum packet size violations. */
916 switch (sav->sah->saidx.dst.sa.sa_family) {
917 #ifdef INET
918 case AF_INET:
919 maxpacketsize = IP_MAXPACKET;
920 ipoffs = offsetof(struct ip, ip_len);
921 break;
922 #endif
923 #ifdef INET6
924 case AF_INET6:
925 maxpacketsize = IPV6_MAXPACKET;
926 ipoffs = offsetof(struct ip6_hdr, ip6_plen);
927 break;
928 #endif
929 default:
930 DPRINTF("unknown/unsupported protocol "
931 "family %u, SA %s/%08lx\n",
932 sav->sah->saidx.dst.sa.sa_family,
933 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
934 (u_long) ntohl(sav->spi));
935 AH_STATINC(AH_STAT_NOPF);
936 error = EPFNOSUPPORT;
937 goto bad;
938 }
939 if (ahsize + m->m_pkthdr.len > maxpacketsize) {
940 DPRINTF("packet in SA %s/%08lx got too big "
941 "(len %u, max len %u)\n",
942 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
943 (u_long) ntohl(sav->spi),
944 ahsize + m->m_pkthdr.len, maxpacketsize);
945 AH_STATINC(AH_STAT_TOOBIG);
946 error = EMSGSIZE;
947 goto bad;
948 }
949
950 /* Update the counters. */
951 AH_STATADD(AH_STAT_OBYTES, m->m_pkthdr.len - skip);
952
953 m = m_clone(m);
954 if (m == NULL) {
955 DPRINTF("cannot clone mbuf chain, SA %s/%08lx\n",
956 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
957 (u_long) ntohl(sav->spi));
958 AH_STATINC(AH_STAT_HDROPS);
959 error = ENOBUFS;
960 goto bad;
961 }
962
963 /* Inject AH header. */
964 mi = m_makespace(m, skip, ahsize, &roff);
965 if (mi == NULL) {
966 DPRINTF("failed to inject %u byte AH header for SA "
967 "%s/%08lx\n", ahsize,
968 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
969 (u_long) ntohl(sav->spi));
970 AH_STATINC(AH_STAT_HDROPS);
971 error = ENOBUFS;
972 goto bad;
973 }
974
975 /*
976 * The AH header is guaranteed by m_makespace() to be in
977 * contiguous memory, at roff bytes offset into the returned mbuf.
978 */
979 ah = (struct newah *)(mtod(mi, char *) + roff);
980
981 /* Initialize the AH header. */
982 m_copydata(m, protoff, sizeof(uint8_t), &ah->ah_nxt);
983 ah->ah_len = (ahsize - sizeof(struct ah)) / sizeof(uint32_t);
984 ah->ah_reserve = 0;
985 ah->ah_spi = sav->spi;
986
987 /* Zeroize authenticator. */
988 m_copyback(m, skip + rplen, authsize, ipseczeroes);
989
990 /* Zeroize padding. */
991 m_copyback(m, skip + rplen + authsize, ahsize - (rplen + authsize),
992 ipseczeroes);
993
994 /* Insert packet replay counter, as requested. */
995 if (sav->replay) {
996 if (sav->replay->count == ~0 &&
997 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
998 DPRINTF("replay counter wrapped for SA %s/%08lx\n",
999 ipsec_address(&sav->sah->saidx.dst, buf,
1000 sizeof(buf)), (u_long) ntohl(sav->spi));
1001 AH_STATINC(AH_STAT_WRAP);
1002 error = EINVAL;
1003 goto bad;
1004 }
1005 #ifdef IPSEC_DEBUG
1006 /* Emulate replay attack when ipsec_replay is TRUE. */
1007 if (!ipsec_replay)
1008 #endif
1009 sav->replay->count++;
1010 ah->ah_seq = htonl(sav->replay->count);
1011 }
1012
1013 /* Get crypto descriptors. */
1014 crp = crypto_getreq(1);
1015 if (crp == NULL) {
1016 DPRINTF("failed to acquire crypto descriptors\n");
1017 AH_STATINC(AH_STAT_CRYPTO);
1018 error = ENOBUFS;
1019 goto bad;
1020 }
1021
1022 crda = crp->crp_desc;
1023
1024 crda->crd_skip = 0;
1025 crda->crd_inject = skip + rplen;
1026 crda->crd_len = m->m_pkthdr.len;
1027
1028 /* Authentication operation. */
1029 crda->crd_alg = ahx->type;
1030 crda->crd_key = _KEYBUF(sav->key_auth);
1031 crda->crd_klen = _KEYBITS(sav->key_auth);
1032
1033 /* Allocate IPsec-specific opaque crypto info. */
1034 size_t size = sizeof(*tc) + skip;
1035
1036 if (__predict_true(size <= ah_pool_item_size)) {
1037 tc = pool_cache_get(ah_tdb_crypto_pool_cache, PR_NOWAIT);
1038 pool_used = true;
1039 } else {
1040 /* size can exceed on IPv6 packets with large options. */
1041 tc = kmem_intr_zalloc(size, KM_NOSLEEP);
1042 pool_used = false;
1043 }
1044 if (tc == NULL) {
1045 DPRINTF("failed to allocate tdb_crypto\n");
1046 AH_STATINC(AH_STAT_CRYPTO);
1047 error = ENOBUFS;
1048 goto bad_crp;
1049 }
1050
1051 uint8_t *pext = (char *)(tc + 1);
1052 /* Save the skipped portion of the packet. */
1053 m_copydata(m, 0, skip, pext);
1054
1055 /*
1056 * Fix IP header length on the header used for
1057 * authentication. We don't need to fix the original
1058 * header length as it will be fixed by our caller.
1059 */
1060 memcpy(&iplen, pext + ipoffs, sizeof(iplen));
1061 iplen = htons(ntohs(iplen) + ahsize);
1062 m_copyback(m, ipoffs, sizeof(iplen), &iplen);
1063
1064 /* Fix the Next Header field in saved header. */
1065 pext[protoff] = IPPROTO_AH;
1066
1067 /* Update the Next Protocol field in the IP header. */
1068 prot = IPPROTO_AH;
1069 m_copyback(m, protoff, sizeof(prot), &prot);
1070
1071 /* "Massage" the packet headers for crypto processing. */
1072 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family,
1073 skip, ahx->type, 1);
1074 if (error != 0) {
1075 m = NULL; /* mbuf was free'd by ah_massage_headers. */
1076 goto bad_tc;
1077 }
1078
1079 {
1080 int s = pserialize_read_enter();
1081
1082 /*
1083 * Take another reference to the SP and the SA for opencrypto callback.
1084 */
1085 if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD ||
1086 sav->state == SADB_SASTATE_DEAD)) {
1087 pserialize_read_exit(s);
1088 AH_STATINC(AH_STAT_NOTDB);
1089 error = ENOENT;
1090 goto bad_tc;
1091 }
1092 KEY_SP_REF(isr->sp);
1093 KEY_SA_REF(sav);
1094 pserialize_read_exit(s);
1095 }
1096
1097 /* Crypto operation descriptor. */
1098 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
1099 crp->crp_flags = CRYPTO_F_IMBUF;
1100 crp->crp_buf = m;
1101 crp->crp_callback = ah_output_cb;
1102 crp->crp_sid = sav->tdb_cryptoid;
1103 crp->crp_opaque = tc;
1104
1105 /* These are passed as-is to the callback. */
1106 tc->tc_isr = isr;
1107 tc->tc_spi = sav->spi;
1108 tc->tc_dst = sav->sah->saidx.dst;
1109 tc->tc_proto = sav->sah->saidx.proto;
1110 tc->tc_skip = skip;
1111 tc->tc_protoff = protoff;
1112 tc->tc_flags = flags;
1113 tc->tc_sav = sav;
1114
1115 return crypto_dispatch(crp);
1116
1117 bad_tc:
1118 if (__predict_true(pool_used))
1119 pool_cache_put(ah_tdb_crypto_pool_cache, tc);
1120 else
1121 kmem_intr_free(tc, size);
1122 bad_crp:
1123 crypto_freereq(crp);
1124 bad:
1125 if (m)
1126 m_freem(m);
1127 return error;
1128 }
1129
1130 /*
1131 * AH output callback from the crypto driver.
1132 */
1133 static void
1134 ah_output_cb(struct cryptop *crp)
1135 {
1136 int skip;
1137 struct tdb_crypto *tc;
1138 const struct ipsecrequest *isr;
1139 struct secasvar *sav;
1140 struct mbuf *m;
1141 void *ptr;
1142 int flags;
1143 size_t size;
1144 bool pool_used;
1145 IPSEC_DECLARE_LOCK_VARIABLE;
1146
1147 KASSERT(crp->crp_opaque != NULL);
1148 tc = crp->crp_opaque;
1149 skip = tc->tc_skip;
1150 ptr = (tc + 1);
1151 m = crp->crp_buf;
1152 size = sizeof(*tc) + skip;
1153 pool_used = size <= ah_pool_item_size;
1154
1155 IPSEC_ACQUIRE_GLOBAL_LOCKS();
1156
1157 isr = tc->tc_isr;
1158 sav = tc->tc_sav;
1159
1160 /* Check for crypto errors. */
1161 if (crp->crp_etype) {
1162 if (sav->tdb_cryptoid != 0)
1163 sav->tdb_cryptoid = crp->crp_sid;
1164
1165 if (crp->crp_etype == EAGAIN) {
1166 IPSEC_RELEASE_GLOBAL_LOCKS();
1167 (void)crypto_dispatch(crp);
1168 return;
1169 }
1170
1171 AH_STATINC(AH_STAT_NOXFORM);
1172 DPRINTF("crypto error %d\n", crp->crp_etype);
1173 goto bad;
1174 }
1175
1176 AH_STATINC(AH_STAT_HIST + ah_stats[sav->alg_auth]);
1177
1178 /*
1179 * Copy original headers (with the new protocol number) back
1180 * in place.
1181 */
1182 m_copyback(m, 0, skip, ptr);
1183
1184 flags = tc->tc_flags;
1185 /* No longer needed. */
1186 if (__predict_true(pool_used))
1187 pool_cache_put(ah_tdb_crypto_pool_cache, tc);
1188 else
1189 kmem_intr_free(tc, size);
1190 crypto_freereq(crp);
1191
1192 #ifdef IPSEC_DEBUG
1193 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
1194 if (ipsec_integrity) {
1195 int alen;
1196
1197 /*
1198 * Corrupt HMAC if we want to test integrity verification of
1199 * the other side.
1200 */
1201 alen = AUTHSIZE(sav);
1202 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes);
1203 }
1204 #endif
1205
1206 /* NB: m is reclaimed by ipsec_process_done. */
1207 (void)ipsec_process_done(m, isr, sav, flags);
1208 KEY_SA_UNREF(&sav);
1209 KEY_SP_UNREF(&isr->sp);
1210 IPSEC_RELEASE_GLOBAL_LOCKS();
1211 return;
1212 bad:
1213 if (sav)
1214 KEY_SA_UNREF(&sav);
1215 KEY_SP_UNREF(&isr->sp);
1216 IPSEC_RELEASE_GLOBAL_LOCKS();
1217 if (m)
1218 m_freem(m);
1219 if (__predict_true(pool_used))
1220 pool_cache_put(ah_tdb_crypto_pool_cache, tc);
1221 else
1222 kmem_intr_free(tc, size);
1223 crypto_freereq(crp);
1224 }
1225
1226 static struct xformsw ah_xformsw = {
1227 .xf_type = XF_AH,
1228 .xf_flags = XFT_AUTH,
1229 .xf_name = "IPsec AH",
1230 .xf_init = ah_init,
1231 .xf_zeroize = ah_zeroize,
1232 .xf_input = ah_input,
1233 .xf_output = ah_output,
1234 .xf_next = NULL,
1235 };
1236
1237 void
1238 ah_attach(void)
1239 {
1240 ahstat_percpu = percpu_alloc(sizeof(uint64_t) * AH_NSTATS);
1241
1242 #define MAXAUTHSIZE(name) \
1243 if ((auth_hash_ ## name).authsize > ah_max_authsize) \
1244 ah_max_authsize = (auth_hash_ ## name).authsize
1245
1246 ah_max_authsize = 0;
1247 MAXAUTHSIZE(null);
1248 MAXAUTHSIZE(md5);
1249 MAXAUTHSIZE(sha1);
1250 MAXAUTHSIZE(key_md5);
1251 MAXAUTHSIZE(key_sha1);
1252 MAXAUTHSIZE(hmac_md5);
1253 MAXAUTHSIZE(hmac_sha1);
1254 MAXAUTHSIZE(hmac_ripemd_160);
1255 MAXAUTHSIZE(hmac_md5_96);
1256 MAXAUTHSIZE(hmac_sha1_96);
1257 MAXAUTHSIZE(hmac_ripemd_160_96);
1258 MAXAUTHSIZE(hmac_sha2_256);
1259 MAXAUTHSIZE(hmac_sha2_384);
1260 MAXAUTHSIZE(hmac_sha2_512);
1261 MAXAUTHSIZE(aes_xcbc_mac_96);
1262 MAXAUTHSIZE(gmac_aes_128);
1263 MAXAUTHSIZE(gmac_aes_192);
1264 MAXAUTHSIZE(gmac_aes_256);
1265 IPSECLOG(LOG_DEBUG, "ah_max_authsize=%d\n", ah_max_authsize);
1266
1267 #undef MAXAUTHSIZE
1268
1269 ah_pool_item_size = sizeof(struct tdb_crypto) +
1270 sizeof(struct ip) + MAX_IPOPTLEN +
1271 sizeof(struct ah) + sizeof(uint32_t) + ah_max_authsize;
1272 ah_tdb_crypto_pool_cache = pool_cache_init(ah_pool_item_size,
1273 coherency_unit, 0, 0, "ah_tdb_crypto", NULL, IPL_SOFTNET,
1274 NULL, NULL, NULL);
1275
1276 xform_register(&ah_xformsw);
1277 }
1278