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