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