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