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