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