1 /* $FreeBSD$ */ 2 /* $OpenBSD: ip_ah.c,v 1.63 2001/06/26 06:18:58 angelos Exp $ */ 3 /*- 4 * The authors of this code are John Ioannidis (ji@tla.org), 5 * Angelos D. Keromytis (kermit@csd.uch.gr) and 6 * Niels Provos (provos@physnet.uni-hamburg.de). 7 * 8 * The original version of this code was written by John Ioannidis 9 * for BSD/OS in Athens, Greece, in November 1995. 10 * 11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 12 * by Angelos D. Keromytis. 13 * 14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 15 * and Niels Provos. 16 * 17 * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist. 18 * 19 * Copyright (c) 1995, 1996, 1997, 1998, 1999 by John Ioannidis, 20 * Angelos D. Keromytis and Niels Provos. 21 * Copyright (c) 1999 Niklas Hallqvist. 22 * Copyright (c) 2001 Angelos D. Keromytis. 23 * 24 * Permission to use, copy, and modify this software with or without fee 25 * is hereby granted, provided that this entire notice is included in 26 * all copies of any software which is or includes a copy or 27 * modification of this software. 28 * You may use this code under the GNU public license if you so wish. Please 29 * contribute changes back to the authors under this freer than GPL license 30 * so that we may further the use of strong encryption without limitations to 31 * all. 32 * 33 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 34 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 35 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 36 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 37 * PURPOSE. 38 */ 39 #include "opt_inet.h" 40 #include "opt_inet6.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/mbuf.h> 45 #include <sys/socket.h> 46 #include <sys/syslog.h> 47 #include <sys/kernel.h> 48 #include <sys/sysctl.h> 49 #include <sys/vimage.h> 50 51 #include <net/if.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/ip.h> 56 #include <netinet/ip_ecn.h> 57 #include <netinet/ip6.h> 58 59 #include <net/route.h> 60 #include <netipsec/ipsec.h> 61 #include <netipsec/ah.h> 62 #include <netipsec/ah_var.h> 63 #include <netipsec/xform.h> 64 65 #ifdef INET6 66 #include <netinet6/ip6_var.h> 67 #include <netipsec/ipsec6.h> 68 #include <netinet6/ip6_ecn.h> 69 #endif 70 71 #include <netipsec/key.h> 72 #include <netipsec/key_debug.h> 73 74 #include <opencrypto/cryptodev.h> 75 76 /* 77 * Return header size in bytes. The old protocol did not support 78 * the replay counter; the new protocol always includes the counter. 79 */ 80 #define HDRSIZE(sav) \ 81 (((sav)->flags & SADB_X_EXT_OLD) ? \ 82 sizeof (struct ah) : sizeof (struct ah) + sizeof (u_int32_t)) 83 /* 84 * Return authenticator size in bytes. The old protocol is known 85 * to use a fixed 16-byte authenticator. The new algorithm use 12-byte 86 * authenticator. 87 */ 88 #define AUTHSIZE(sav) \ 89 ((sav->flags & SADB_X_EXT_OLD) ? 16 : AH_HMAC_HASHLEN) 90 91 #ifdef VIMAGE_GLOBALS 92 int ah_enable; 93 int ah_cleartos; 94 struct ahstat ahstat; 95 #endif 96 97 SYSCTL_DECL(_net_inet_ah); 98 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ah, OID_AUTO, 99 ah_enable, CTLFLAG_RW, ah_enable, 0, ""); 100 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ah, OID_AUTO, 101 ah_cleartos, CTLFLAG_RW, ah_cleartos, 0, ""); 102 SYSCTL_V_STRUCT(V_NET, vnet_ipsec, _net_inet_ah, IPSECCTL_STATS, 103 stats, CTLFLAG_RD, ahstat, ahstat, ""); 104 105 static unsigned char ipseczeroes[256]; /* larger than an ip6 extension hdr */ 106 107 static int ah_input_cb(struct cryptop*); 108 static int ah_output_cb(struct cryptop*); 109 110 /* 111 * NB: this is public for use by the PF_KEY support. 112 */ 113 struct auth_hash * 114 ah_algorithm_lookup(int alg) 115 { 116 if (alg > SADB_AALG_MAX) 117 return NULL; 118 switch (alg) { 119 case SADB_X_AALG_NULL: 120 return &auth_hash_null; 121 case SADB_AALG_MD5HMAC: 122 return &auth_hash_hmac_md5; 123 case SADB_AALG_SHA1HMAC: 124 return &auth_hash_hmac_sha1; 125 case SADB_X_AALG_RIPEMD160HMAC: 126 return &auth_hash_hmac_ripemd_160; 127 case SADB_X_AALG_MD5: 128 return &auth_hash_key_md5; 129 case SADB_X_AALG_SHA: 130 return &auth_hash_key_sha1; 131 case SADB_X_AALG_SHA2_256: 132 return &auth_hash_hmac_sha2_256; 133 case SADB_X_AALG_SHA2_384: 134 return &auth_hash_hmac_sha2_384; 135 case SADB_X_AALG_SHA2_512: 136 return &auth_hash_hmac_sha2_512; 137 } 138 return NULL; 139 } 140 141 size_t 142 ah_hdrsiz(struct secasvar *sav) 143 { 144 size_t size; 145 146 if (sav != NULL) { 147 int authsize; 148 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, ("null xform")); 149 /*XXX not right for null algorithm--does it matter??*/ 150 authsize = AUTHSIZE(sav); 151 size = roundup(authsize, sizeof (u_int32_t)) + HDRSIZE(sav); 152 } else { 153 /* default guess */ 154 size = sizeof (struct ah) + sizeof (u_int32_t) + 16; 155 } 156 return size; 157 } 158 159 /* 160 * NB: public for use by esp_init. 161 */ 162 int 163 ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria) 164 { 165 INIT_VNET_IPSEC(curvnet); 166 struct auth_hash *thash; 167 int keylen; 168 169 thash = ah_algorithm_lookup(sav->alg_auth); 170 if (thash == NULL) { 171 DPRINTF(("%s: unsupported authentication algorithm %u\n", 172 __func__, sav->alg_auth)); 173 return EINVAL; 174 } 175 /* 176 * Verify the replay state block allocation is consistent with 177 * the protocol type. We check here so we can make assumptions 178 * later during protocol processing. 179 */ 180 /* NB: replay state is setup elsewhere (sigh) */ 181 if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) { 182 DPRINTF(("%s: replay state block inconsistency, " 183 "%s algorithm %s replay state\n", __func__, 184 (sav->flags & SADB_X_EXT_OLD) ? "old" : "new", 185 sav->replay == NULL ? "without" : "with")); 186 return EINVAL; 187 } 188 if (sav->key_auth == NULL) { 189 DPRINTF(("%s: no authentication key for %s algorithm\n", 190 __func__, thash->name)); 191 return EINVAL; 192 } 193 keylen = _KEYLEN(sav->key_auth); 194 if (keylen != thash->keysize && thash->keysize != 0) { 195 DPRINTF(("%s: invalid keylength %d, algorithm %s requires " 196 "keysize %d\n", __func__, 197 keylen, thash->name, thash->keysize)); 198 return EINVAL; 199 } 200 201 sav->tdb_xform = xsp; 202 sav->tdb_authalgxform = thash; 203 204 /* Initialize crypto session. */ 205 bzero(cria, sizeof (*cria)); 206 cria->cri_alg = sav->tdb_authalgxform->type; 207 cria->cri_klen = _KEYBITS(sav->key_auth); 208 cria->cri_key = sav->key_auth->key_data; 209 cria->cri_mlen = AUTHSIZE(sav); 210 211 return 0; 212 } 213 214 /* 215 * ah_init() is called when an SPI is being set up. 216 */ 217 static int 218 ah_init(struct secasvar *sav, struct xformsw *xsp) 219 { 220 INIT_VNET_IPSEC(curvnet); 221 struct cryptoini cria; 222 int error; 223 224 error = ah_init0(sav, xsp, &cria); 225 return error ? error : 226 crypto_newsession(&sav->tdb_cryptoid, &cria, V_crypto_support); 227 } 228 229 /* 230 * Paranoia. 231 * 232 * NB: public for use by esp_zeroize (XXX). 233 */ 234 int 235 ah_zeroize(struct secasvar *sav) 236 { 237 int err; 238 239 if (sav->key_auth) 240 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth)); 241 242 err = crypto_freesession(sav->tdb_cryptoid); 243 sav->tdb_cryptoid = 0; 244 sav->tdb_authalgxform = NULL; 245 sav->tdb_xform = NULL; 246 return err; 247 } 248 249 /* 250 * Massage IPv4/IPv6 headers for AH processing. 251 */ 252 static int 253 ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out) 254 { 255 INIT_VNET_IPSEC(curvnet); 256 struct mbuf *m = *m0; 257 unsigned char *ptr; 258 int off, count; 259 260 #ifdef INET 261 struct ip *ip; 262 #endif /* INET */ 263 264 #ifdef INET6 265 struct ip6_ext *ip6e; 266 struct ip6_hdr ip6; 267 int alloc, len, ad; 268 #endif /* INET6 */ 269 270 switch (proto) { 271 #ifdef INET 272 case AF_INET: 273 /* 274 * This is the least painful way of dealing with IPv4 header 275 * and option processing -- just make sure they're in 276 * contiguous memory. 277 */ 278 *m0 = m = m_pullup(m, skip); 279 if (m == NULL) { 280 DPRINTF(("%s: m_pullup failed\n", __func__)); 281 return ENOBUFS; 282 } 283 284 /* Fix the IP header */ 285 ip = mtod(m, struct ip *); 286 if (V_ah_cleartos) 287 ip->ip_tos = 0; 288 ip->ip_ttl = 0; 289 ip->ip_sum = 0; 290 291 /* 292 * On input, fix ip_len which has been byte-swapped 293 * at ip_input(). 294 */ 295 if (!out) { 296 ip->ip_len = htons(ip->ip_len + skip); 297 298 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK) 299 ip->ip_off = htons(ip->ip_off & IP_DF); 300 else 301 ip->ip_off = 0; 302 } else { 303 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK) 304 ip->ip_off = htons(ntohs(ip->ip_off) & IP_DF); 305 else 306 ip->ip_off = 0; 307 } 308 309 ptr = mtod(m, unsigned char *) + sizeof(struct ip); 310 311 /* IPv4 option processing */ 312 for (off = sizeof(struct ip); off < skip;) { 313 if (ptr[off] == IPOPT_EOL || ptr[off] == IPOPT_NOP || 314 off + 1 < skip) 315 ; 316 else { 317 DPRINTF(("%s: illegal IPv4 option length for " 318 "option %d\n", __func__, ptr[off])); 319 320 m_freem(m); 321 return EINVAL; 322 } 323 324 switch (ptr[off]) { 325 case IPOPT_EOL: 326 off = skip; /* End the loop. */ 327 break; 328 329 case IPOPT_NOP: 330 off++; 331 break; 332 333 case IPOPT_SECURITY: /* 0x82 */ 334 case 0x85: /* Extended security. */ 335 case 0x86: /* Commercial security. */ 336 case 0x94: /* Router alert */ 337 case 0x95: /* RFC1770 */ 338 /* Sanity check for option length. */ 339 if (ptr[off + 1] < 2) { 340 DPRINTF(("%s: illegal IPv4 option " 341 "length for option %d\n", 342 __func__, ptr[off])); 343 344 m_freem(m); 345 return EINVAL; 346 } 347 348 off += ptr[off + 1]; 349 break; 350 351 case IPOPT_LSRR: 352 case IPOPT_SSRR: 353 /* Sanity check for option length. */ 354 if (ptr[off + 1] < 2) { 355 DPRINTF(("%s: illegal IPv4 option " 356 "length for option %d\n", 357 __func__, ptr[off])); 358 359 m_freem(m); 360 return EINVAL; 361 } 362 363 /* 364 * On output, if we have either of the 365 * source routing options, we should 366 * swap the destination address of the 367 * IP header with the last address 368 * specified in the option, as that is 369 * what the destination's IP header 370 * will look like. 371 */ 372 if (out) 373 bcopy(ptr + off + ptr[off + 1] - 374 sizeof(struct in_addr), 375 &(ip->ip_dst), sizeof(struct in_addr)); 376 377 /* Fall through */ 378 default: 379 /* Sanity check for option length. */ 380 if (ptr[off + 1] < 2) { 381 DPRINTF(("%s: illegal IPv4 option " 382 "length for option %d\n", 383 __func__, ptr[off])); 384 m_freem(m); 385 return EINVAL; 386 } 387 388 /* Zeroize all other options. */ 389 count = ptr[off + 1]; 390 bcopy(ipseczeroes, ptr, count); 391 off += count; 392 break; 393 } 394 395 /* Sanity check. */ 396 if (off > skip) { 397 DPRINTF(("%s: malformed IPv4 options header\n", 398 __func__)); 399 400 m_freem(m); 401 return EINVAL; 402 } 403 } 404 405 break; 406 #endif /* INET */ 407 408 #ifdef INET6 409 case AF_INET6: /* Ugly... */ 410 /* Copy and "cook" the IPv6 header. */ 411 m_copydata(m, 0, sizeof(ip6), (caddr_t) &ip6); 412 413 /* We don't do IPv6 Jumbograms. */ 414 if (ip6.ip6_plen == 0) { 415 DPRINTF(("%s: unsupported IPv6 jumbogram\n", __func__)); 416 m_freem(m); 417 return EMSGSIZE; 418 } 419 420 ip6.ip6_flow = 0; 421 ip6.ip6_hlim = 0; 422 ip6.ip6_vfc &= ~IPV6_VERSION_MASK; 423 ip6.ip6_vfc |= IPV6_VERSION; 424 425 /* Scoped address handling. */ 426 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src)) 427 ip6.ip6_src.s6_addr16[1] = 0; 428 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst)) 429 ip6.ip6_dst.s6_addr16[1] = 0; 430 431 /* Done with IPv6 header. */ 432 m_copyback(m, 0, sizeof(struct ip6_hdr), (caddr_t) &ip6); 433 434 /* Let's deal with the remaining headers (if any). */ 435 if (skip - sizeof(struct ip6_hdr) > 0) { 436 if (m->m_len <= skip) { 437 ptr = (unsigned char *) malloc( 438 skip - sizeof(struct ip6_hdr), 439 M_XDATA, M_NOWAIT); 440 if (ptr == NULL) { 441 DPRINTF(("%s: failed to allocate memory" 442 "for IPv6 headers\n",__func__)); 443 m_freem(m); 444 return ENOBUFS; 445 } 446 447 /* 448 * Copy all the protocol headers after 449 * the IPv6 header. 450 */ 451 m_copydata(m, sizeof(struct ip6_hdr), 452 skip - sizeof(struct ip6_hdr), ptr); 453 alloc = 1; 454 } else { 455 /* No need to allocate memory. */ 456 ptr = mtod(m, unsigned char *) + 457 sizeof(struct ip6_hdr); 458 alloc = 0; 459 } 460 } else 461 break; 462 463 off = ip6.ip6_nxt & 0xff; /* Next header type. */ 464 465 for (len = 0; len < skip - sizeof(struct ip6_hdr);) 466 switch (off) { 467 case IPPROTO_HOPOPTS: 468 case IPPROTO_DSTOPTS: 469 ip6e = (struct ip6_ext *) (ptr + len); 470 471 /* 472 * Process the mutable/immutable 473 * options -- borrows heavily from the 474 * KAME code. 475 */ 476 for (count = len + sizeof(struct ip6_ext); 477 count < len + ((ip6e->ip6e_len + 1) << 3);) { 478 if (ptr[count] == IP6OPT_PAD1) { 479 count++; 480 continue; /* Skip padding. */ 481 } 482 483 /* Sanity check. */ 484 if (count > len + 485 ((ip6e->ip6e_len + 1) << 3)) { 486 m_freem(m); 487 488 /* Free, if we allocated. */ 489 if (alloc) 490 free(ptr, M_XDATA); 491 return EINVAL; 492 } 493 494 ad = ptr[count + 1]; 495 496 /* If mutable option, zeroize. */ 497 if (ptr[count] & IP6OPT_MUTABLE) 498 bcopy(ipseczeroes, ptr + count, 499 ptr[count + 1]); 500 501 count += ad; 502 503 /* Sanity check. */ 504 if (count > 505 skip - sizeof(struct ip6_hdr)) { 506 m_freem(m); 507 508 /* Free, if we allocated. */ 509 if (alloc) 510 free(ptr, M_XDATA); 511 return EINVAL; 512 } 513 } 514 515 /* Advance. */ 516 len += ((ip6e->ip6e_len + 1) << 3); 517 off = ip6e->ip6e_nxt; 518 break; 519 520 case IPPROTO_ROUTING: 521 /* 522 * Always include routing headers in 523 * computation. 524 */ 525 ip6e = (struct ip6_ext *) (ptr + len); 526 len += ((ip6e->ip6e_len + 1) << 3); 527 off = ip6e->ip6e_nxt; 528 break; 529 530 default: 531 DPRINTF(("%s: unexpected IPv6 header type %d", 532 __func__, off)); 533 if (alloc) 534 free(ptr, M_XDATA); 535 m_freem(m); 536 return EINVAL; 537 } 538 539 /* Copyback and free, if we allocated. */ 540 if (alloc) { 541 m_copyback(m, sizeof(struct ip6_hdr), 542 skip - sizeof(struct ip6_hdr), ptr); 543 free(ptr, M_XDATA); 544 } 545 546 break; 547 #endif /* INET6 */ 548 } 549 550 return 0; 551 } 552 553 /* 554 * ah_input() gets called to verify that an input packet 555 * passes authentication. 556 */ 557 static int 558 ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff) 559 { 560 INIT_VNET_IPSEC(curvnet); 561 struct auth_hash *ahx; 562 struct tdb_ident *tdbi; 563 struct tdb_crypto *tc; 564 struct m_tag *mtag; 565 struct newah *ah; 566 int hl, rplen, authsize; 567 568 struct cryptodesc *crda; 569 struct cryptop *crp; 570 571 IPSEC_ASSERT(sav != NULL, ("null SA")); 572 IPSEC_ASSERT(sav->key_auth != NULL, ("null authentication key")); 573 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, 574 ("null authentication xform")); 575 576 /* Figure out header size. */ 577 rplen = HDRSIZE(sav); 578 579 /* XXX don't pullup, just copy header */ 580 IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen); 581 if (ah == NULL) { 582 DPRINTF(("ah_input: cannot pullup header\n")); 583 V_ahstat.ahs_hdrops++; /*XXX*/ 584 m_freem(m); 585 return ENOBUFS; 586 } 587 588 /* Check replay window, if applicable. */ 589 if (sav->replay && !ipsec_chkreplay(ntohl(ah->ah_seq), sav)) { 590 V_ahstat.ahs_replay++; 591 DPRINTF(("%s: packet replay failure: %s\n", __func__, 592 ipsec_logsastr(sav))); 593 m_freem(m); 594 return ENOBUFS; 595 } 596 597 /* Verify AH header length. */ 598 hl = ah->ah_len * sizeof (u_int32_t); 599 ahx = sav->tdb_authalgxform; 600 authsize = AUTHSIZE(sav); 601 if (hl != authsize + rplen - sizeof (struct ah)) { 602 DPRINTF(("%s: bad authenticator length %u (expecting %lu)" 603 " for packet in SA %s/%08lx\n", __func__, 604 hl, (u_long) (authsize + rplen - sizeof (struct ah)), 605 ipsec_address(&sav->sah->saidx.dst), 606 (u_long) ntohl(sav->spi))); 607 V_ahstat.ahs_badauthl++; 608 m_freem(m); 609 return EACCES; 610 } 611 V_ahstat.ahs_ibytes += m->m_pkthdr.len - skip - hl; 612 613 /* Get crypto descriptors. */ 614 crp = crypto_getreq(1); 615 if (crp == NULL) { 616 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__)); 617 V_ahstat.ahs_crypto++; 618 m_freem(m); 619 return ENOBUFS; 620 } 621 622 crda = crp->crp_desc; 623 IPSEC_ASSERT(crda != NULL, ("null crypto descriptor")); 624 625 crda->crd_skip = 0; 626 crda->crd_len = m->m_pkthdr.len; 627 crda->crd_inject = skip + rplen; 628 629 /* Authentication operation. */ 630 crda->crd_alg = ahx->type; 631 crda->crd_klen = _KEYBITS(sav->key_auth); 632 crda->crd_key = sav->key_auth->key_data; 633 634 /* Find out if we've already done crypto. */ 635 for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL); 636 mtag != NULL; 637 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) { 638 tdbi = (struct tdb_ident *) (mtag + 1); 639 if (tdbi->proto == sav->sah->saidx.proto && 640 tdbi->spi == sav->spi && 641 !bcmp(&tdbi->dst, &sav->sah->saidx.dst, 642 sizeof (union sockaddr_union))) 643 break; 644 } 645 646 /* Allocate IPsec-specific opaque crypto info. */ 647 if (mtag == NULL) { 648 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto) + 649 skip + rplen + authsize, M_XDATA, M_NOWAIT|M_ZERO); 650 } else { 651 /* Hash verification has already been done successfully. */ 652 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto), 653 M_XDATA, M_NOWAIT|M_ZERO); 654 } 655 if (tc == NULL) { 656 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__)); 657 V_ahstat.ahs_crypto++; 658 crypto_freereq(crp); 659 m_freem(m); 660 return ENOBUFS; 661 } 662 663 /* Only save information if crypto processing is needed. */ 664 if (mtag == NULL) { 665 int error; 666 667 /* 668 * Save the authenticator, the skipped portion of the packet, 669 * and the AH header. 670 */ 671 m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(tc+1)); 672 673 /* Zeroize the authenticator on the packet. */ 674 m_copyback(m, skip + rplen, authsize, ipseczeroes); 675 676 /* "Massage" the packet headers for crypto processing. */ 677 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family, 678 skip, ahx->type, 0); 679 if (error != 0) { 680 /* NB: mbuf is free'd by ah_massage_headers */ 681 V_ahstat.ahs_hdrops++; 682 free(tc, M_XDATA); 683 crypto_freereq(crp); 684 return error; 685 } 686 } 687 688 /* Crypto operation descriptor. */ 689 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */ 690 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 691 crp->crp_buf = (caddr_t) m; 692 crp->crp_callback = ah_input_cb; 693 crp->crp_sid = sav->tdb_cryptoid; 694 crp->crp_opaque = (caddr_t) tc; 695 696 /* These are passed as-is to the callback. */ 697 tc->tc_spi = sav->spi; 698 tc->tc_dst = sav->sah->saidx.dst; 699 tc->tc_proto = sav->sah->saidx.proto; 700 tc->tc_nxt = ah->ah_nxt; 701 tc->tc_protoff = protoff; 702 tc->tc_skip = skip; 703 tc->tc_ptr = (caddr_t) mtag; /* Save the mtag we've identified. */ 704 705 if (mtag == NULL) 706 return crypto_dispatch(crp); 707 else 708 return ah_input_cb(crp); 709 } 710 711 #ifdef INET6 712 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \ 713 if (saidx->dst.sa.sa_family == AF_INET6) { \ 714 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \ 715 } else { \ 716 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \ 717 } \ 718 } while (0) 719 #else 720 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \ 721 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag)) 722 #endif 723 724 /* 725 * AH input callback from the crypto driver. 726 */ 727 static int 728 ah_input_cb(struct cryptop *crp) 729 { 730 INIT_VNET_IPSEC(curvnet); 731 int rplen, error, skip, protoff; 732 unsigned char calc[AH_ALEN_MAX]; 733 struct mbuf *m; 734 struct cryptodesc *crd; 735 struct auth_hash *ahx; 736 struct tdb_crypto *tc; 737 struct m_tag *mtag; 738 struct secasvar *sav; 739 struct secasindex *saidx; 740 u_int8_t nxt; 741 caddr_t ptr; 742 int authsize; 743 744 crd = crp->crp_desc; 745 746 tc = (struct tdb_crypto *) crp->crp_opaque; 747 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!")); 748 skip = tc->tc_skip; 749 nxt = tc->tc_nxt; 750 protoff = tc->tc_protoff; 751 mtag = (struct m_tag *) tc->tc_ptr; 752 m = (struct mbuf *) crp->crp_buf; 753 754 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi); 755 if (sav == NULL) { 756 V_ahstat.ahs_notdb++; 757 DPRINTF(("%s: SA expired while in crypto\n", __func__)); 758 error = ENOBUFS; /*XXX*/ 759 goto bad; 760 } 761 762 saidx = &sav->sah->saidx; 763 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET || 764 saidx->dst.sa.sa_family == AF_INET6, 765 ("unexpected protocol family %u", saidx->dst.sa.sa_family)); 766 767 ahx = (struct auth_hash *) sav->tdb_authalgxform; 768 769 /* Check for crypto errors. */ 770 if (crp->crp_etype) { 771 if (sav->tdb_cryptoid != 0) 772 sav->tdb_cryptoid = crp->crp_sid; 773 774 if (crp->crp_etype == EAGAIN) { 775 error = crypto_dispatch(crp); 776 return error; 777 } 778 779 V_ahstat.ahs_noxform++; 780 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 781 error = crp->crp_etype; 782 goto bad; 783 } else { 784 V_ahstat.ahs_hist[sav->alg_auth]++; 785 crypto_freereq(crp); /* No longer needed. */ 786 crp = NULL; 787 } 788 789 /* Shouldn't happen... */ 790 if (m == NULL) { 791 V_ahstat.ahs_crypto++; 792 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__)); 793 error = EINVAL; 794 goto bad; 795 } 796 797 /* Figure out header size. */ 798 rplen = HDRSIZE(sav); 799 authsize = AUTHSIZE(sav); 800 801 /* Copy authenticator off the packet. */ 802 m_copydata(m, skip + rplen, authsize, calc); 803 804 /* 805 * If we have an mtag, we don't need to verify the authenticator -- 806 * it has been verified by an IPsec-aware NIC. 807 */ 808 if (mtag == NULL) { 809 ptr = (caddr_t) (tc + 1); 810 811 /* Verify authenticator. */ 812 if (bcmp(ptr + skip + rplen, calc, authsize)) { 813 DPRINTF(("%s: authentication hash mismatch for packet " 814 "in SA %s/%08lx\n", __func__, 815 ipsec_address(&saidx->dst), 816 (u_long) ntohl(sav->spi))); 817 V_ahstat.ahs_badauth++; 818 error = EACCES; 819 goto bad; 820 } 821 822 /* Fix the Next Protocol field. */ 823 ((u_int8_t *) ptr)[protoff] = nxt; 824 825 /* Copyback the saved (uncooked) network headers. */ 826 m_copyback(m, 0, skip, ptr); 827 } else { 828 /* Fix the Next Protocol field. */ 829 m_copyback(m, protoff, sizeof(u_int8_t), &nxt); 830 } 831 832 free(tc, M_XDATA), tc = NULL; /* No longer needed */ 833 834 /* 835 * Header is now authenticated. 836 */ 837 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM; 838 839 /* 840 * Update replay sequence number, if appropriate. 841 */ 842 if (sav->replay) { 843 u_int32_t seq; 844 845 m_copydata(m, skip + offsetof(struct newah, ah_seq), 846 sizeof (seq), (caddr_t) &seq); 847 if (ipsec_updatereplay(ntohl(seq), sav)) { 848 V_ahstat.ahs_replay++; 849 error = ENOBUFS; /*XXX as above*/ 850 goto bad; 851 } 852 } 853 854 /* 855 * Remove the AH header and authenticator from the mbuf. 856 */ 857 error = m_striphdr(m, skip, rplen + authsize); 858 if (error) { 859 DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__, 860 ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi))); 861 862 V_ahstat.ahs_hdrops++; 863 goto bad; 864 } 865 866 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag); 867 868 KEY_FREESAV(&sav); 869 return error; 870 bad: 871 if (sav) 872 KEY_FREESAV(&sav); 873 if (m != NULL) 874 m_freem(m); 875 if (tc != NULL) 876 free(tc, M_XDATA); 877 if (crp != NULL) 878 crypto_freereq(crp); 879 return error; 880 } 881 882 /* 883 * AH output routine, called by ipsec[46]_process_packet(). 884 */ 885 static int 886 ah_output( 887 struct mbuf *m, 888 struct ipsecrequest *isr, 889 struct mbuf **mp, 890 int skip, 891 int protoff) 892 { 893 INIT_VNET_IPSEC(curvnet); 894 struct secasvar *sav; 895 struct auth_hash *ahx; 896 struct cryptodesc *crda; 897 struct tdb_crypto *tc; 898 struct mbuf *mi; 899 struct cryptop *crp; 900 u_int16_t iplen; 901 int error, rplen, authsize, maxpacketsize, roff; 902 u_int8_t prot; 903 struct newah *ah; 904 905 sav = isr->sav; 906 IPSEC_ASSERT(sav != NULL, ("null SA")); 907 ahx = sav->tdb_authalgxform; 908 IPSEC_ASSERT(ahx != NULL, ("null authentication xform")); 909 910 V_ahstat.ahs_output++; 911 912 /* Figure out header size. */ 913 rplen = HDRSIZE(sav); 914 915 /* Check for maximum packet size violations. */ 916 switch (sav->sah->saidx.dst.sa.sa_family) { 917 #ifdef INET 918 case AF_INET: 919 maxpacketsize = IP_MAXPACKET; 920 break; 921 #endif /* INET */ 922 #ifdef INET6 923 case AF_INET6: 924 maxpacketsize = IPV6_MAXPACKET; 925 break; 926 #endif /* INET6 */ 927 default: 928 DPRINTF(("%s: unknown/unsupported protocol family %u, " 929 "SA %s/%08lx\n", __func__, 930 sav->sah->saidx.dst.sa.sa_family, 931 ipsec_address(&sav->sah->saidx.dst), 932 (u_long) ntohl(sav->spi))); 933 V_ahstat.ahs_nopf++; 934 error = EPFNOSUPPORT; 935 goto bad; 936 } 937 authsize = AUTHSIZE(sav); 938 if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) { 939 DPRINTF(("%s: packet in SA %s/%08lx got too big " 940 "(len %u, max len %u)\n", __func__, 941 ipsec_address(&sav->sah->saidx.dst), 942 (u_long) ntohl(sav->spi), 943 rplen + authsize + m->m_pkthdr.len, maxpacketsize)); 944 V_ahstat.ahs_toobig++; 945 error = EMSGSIZE; 946 goto bad; 947 } 948 949 /* Update the counters. */ 950 V_ahstat.ahs_obytes += m->m_pkthdr.len - skip; 951 952 m = m_unshare(m, M_NOWAIT); 953 if (m == NULL) { 954 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__, 955 ipsec_address(&sav->sah->saidx.dst), 956 (u_long) ntohl(sav->spi))); 957 V_ahstat.ahs_hdrops++; 958 error = ENOBUFS; 959 goto bad; 960 } 961 962 /* Inject AH header. */ 963 mi = m_makespace(m, skip, rplen + authsize, &roff); 964 if (mi == NULL) { 965 DPRINTF(("%s: failed to inject %u byte AH header for SA " 966 "%s/%08lx\n", __func__, 967 rplen + authsize, 968 ipsec_address(&sav->sah->saidx.dst), 969 (u_long) ntohl(sav->spi))); 970 V_ahstat.ahs_hdrops++; /*XXX differs from openbsd */ 971 error = ENOBUFS; 972 goto bad; 973 } 974 975 /* 976 * The AH header is guaranteed by m_makespace() to be in 977 * contiguous memory, at roff bytes offset into the returned mbuf. 978 */ 979 ah = (struct newah *)(mtod(mi, caddr_t) + roff); 980 981 /* Initialize the AH header. */ 982 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt); 983 ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(u_int32_t); 984 ah->ah_reserve = 0; 985 ah->ah_spi = sav->spi; 986 987 /* Zeroize authenticator. */ 988 m_copyback(m, skip + rplen, authsize, ipseczeroes); 989 990 /* Insert packet replay counter, as requested. */ 991 if (sav->replay) { 992 if (sav->replay->count == ~0 && 993 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) { 994 DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n", 995 __func__, 996 ipsec_address(&sav->sah->saidx.dst), 997 (u_long) ntohl(sav->spi))); 998 V_ahstat.ahs_wrap++; 999 error = EINVAL; 1000 goto bad; 1001 } 1002 #ifdef REGRESSION 1003 /* Emulate replay attack when ipsec_replay is TRUE. */ 1004 if (!V_ipsec_replay) 1005 #endif 1006 sav->replay->count++; 1007 ah->ah_seq = htonl(sav->replay->count); 1008 } 1009 1010 /* Get crypto descriptors. */ 1011 crp = crypto_getreq(1); 1012 if (crp == NULL) { 1013 DPRINTF(("%s: failed to acquire crypto descriptors\n", 1014 __func__)); 1015 V_ahstat.ahs_crypto++; 1016 error = ENOBUFS; 1017 goto bad; 1018 } 1019 1020 crda = crp->crp_desc; 1021 1022 crda->crd_skip = 0; 1023 crda->crd_inject = skip + rplen; 1024 crda->crd_len = m->m_pkthdr.len; 1025 1026 /* Authentication operation. */ 1027 crda->crd_alg = ahx->type; 1028 crda->crd_key = sav->key_auth->key_data; 1029 crda->crd_klen = _KEYBITS(sav->key_auth); 1030 1031 /* Allocate IPsec-specific opaque crypto info. */ 1032 tc = (struct tdb_crypto *) malloc( 1033 sizeof(struct tdb_crypto) + skip, M_XDATA, M_NOWAIT|M_ZERO); 1034 if (tc == NULL) { 1035 crypto_freereq(crp); 1036 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__)); 1037 V_ahstat.ahs_crypto++; 1038 error = ENOBUFS; 1039 goto bad; 1040 } 1041 1042 /* Save the skipped portion of the packet. */ 1043 m_copydata(m, 0, skip, (caddr_t) (tc + 1)); 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 switch (sav->sah->saidx.dst.sa.sa_family) { 1051 #ifdef INET 1052 case AF_INET: 1053 bcopy(((caddr_t)(tc + 1)) + 1054 offsetof(struct ip, ip_len), 1055 (caddr_t) &iplen, sizeof(u_int16_t)); 1056 iplen = htons(ntohs(iplen) + rplen + authsize); 1057 m_copyback(m, offsetof(struct ip, ip_len), 1058 sizeof(u_int16_t), (caddr_t) &iplen); 1059 break; 1060 #endif /* INET */ 1061 1062 #ifdef INET6 1063 case AF_INET6: 1064 bcopy(((caddr_t)(tc + 1)) + 1065 offsetof(struct ip6_hdr, ip6_plen), 1066 (caddr_t) &iplen, sizeof(u_int16_t)); 1067 iplen = htons(ntohs(iplen) + rplen + authsize); 1068 m_copyback(m, offsetof(struct ip6_hdr, ip6_plen), 1069 sizeof(u_int16_t), (caddr_t) &iplen); 1070 break; 1071 #endif /* INET6 */ 1072 } 1073 1074 /* Fix the Next Header field in saved header. */ 1075 ((u_int8_t *) (tc + 1))[protoff] = IPPROTO_AH; 1076 1077 /* Update the Next Protocol field in the IP header. */ 1078 prot = IPPROTO_AH; 1079 m_copyback(m, protoff, sizeof(u_int8_t), (caddr_t) &prot); 1080 1081 /* "Massage" the packet headers for crypto processing. */ 1082 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family, 1083 skip, ahx->type, 1); 1084 if (error != 0) { 1085 m = NULL; /* mbuf was free'd by ah_massage_headers. */ 1086 free(tc, M_XDATA); 1087 crypto_freereq(crp); 1088 goto bad; 1089 } 1090 1091 /* Crypto operation descriptor. */ 1092 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */ 1093 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 1094 crp->crp_buf = (caddr_t) m; 1095 crp->crp_callback = ah_output_cb; 1096 crp->crp_sid = sav->tdb_cryptoid; 1097 crp->crp_opaque = (caddr_t) tc; 1098 1099 /* These are passed as-is to the callback. */ 1100 tc->tc_isr = isr; 1101 tc->tc_spi = sav->spi; 1102 tc->tc_dst = sav->sah->saidx.dst; 1103 tc->tc_proto = sav->sah->saidx.proto; 1104 tc->tc_skip = skip; 1105 tc->tc_protoff = protoff; 1106 1107 return crypto_dispatch(crp); 1108 bad: 1109 if (m) 1110 m_freem(m); 1111 return (error); 1112 } 1113 1114 /* 1115 * AH output callback from the crypto driver. 1116 */ 1117 static int 1118 ah_output_cb(struct cryptop *crp) 1119 { 1120 INIT_VNET_IPSEC(curvnet); 1121 int skip, protoff, error; 1122 struct tdb_crypto *tc; 1123 struct ipsecrequest *isr; 1124 struct secasvar *sav; 1125 struct mbuf *m; 1126 caddr_t ptr; 1127 int err; 1128 1129 tc = (struct tdb_crypto *) crp->crp_opaque; 1130 IPSEC_ASSERT(tc != NULL, ("null opaque data area!")); 1131 skip = tc->tc_skip; 1132 protoff = tc->tc_protoff; 1133 ptr = (caddr_t) (tc + 1); 1134 m = (struct mbuf *) crp->crp_buf; 1135 1136 isr = tc->tc_isr; 1137 IPSECREQUEST_LOCK(isr); 1138 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi); 1139 if (sav == NULL) { 1140 V_ahstat.ahs_notdb++; 1141 DPRINTF(("%s: SA expired while in crypto\n", __func__)); 1142 error = ENOBUFS; /*XXX*/ 1143 goto bad; 1144 } 1145 IPSEC_ASSERT(isr->sav == sav, ("SA changed\n")); 1146 1147 /* Check for crypto errors. */ 1148 if (crp->crp_etype) { 1149 if (sav->tdb_cryptoid != 0) 1150 sav->tdb_cryptoid = crp->crp_sid; 1151 1152 if (crp->crp_etype == EAGAIN) { 1153 KEY_FREESAV(&sav); 1154 IPSECREQUEST_UNLOCK(isr); 1155 error = crypto_dispatch(crp); 1156 return error; 1157 } 1158 1159 V_ahstat.ahs_noxform++; 1160 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 1161 error = crp->crp_etype; 1162 goto bad; 1163 } 1164 1165 /* Shouldn't happen... */ 1166 if (m == NULL) { 1167 V_ahstat.ahs_crypto++; 1168 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__)); 1169 error = EINVAL; 1170 goto bad; 1171 } 1172 V_ahstat.ahs_hist[sav->alg_auth]++; 1173 1174 /* 1175 * Copy original headers (with the new protocol number) back 1176 * in place. 1177 */ 1178 m_copyback(m, 0, skip, ptr); 1179 1180 /* No longer needed. */ 1181 free(tc, M_XDATA); 1182 crypto_freereq(crp); 1183 1184 #ifdef REGRESSION 1185 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */ 1186 if (V_ipsec_integrity) { 1187 int alen; 1188 1189 /* 1190 * Corrupt HMAC if we want to test integrity verification of 1191 * the other side. 1192 */ 1193 alen = AUTHSIZE(sav); 1194 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes); 1195 } 1196 #endif 1197 1198 /* NB: m is reclaimed by ipsec_process_done. */ 1199 err = ipsec_process_done(m, isr); 1200 KEY_FREESAV(&sav); 1201 IPSECREQUEST_UNLOCK(isr); 1202 return err; 1203 bad: 1204 if (sav) 1205 KEY_FREESAV(&sav); 1206 IPSECREQUEST_UNLOCK(isr); 1207 if (m) 1208 m_freem(m); 1209 free(tc, M_XDATA); 1210 crypto_freereq(crp); 1211 return error; 1212 } 1213 1214 static struct xformsw ah_xformsw = { 1215 XF_AH, XFT_AUTH, "IPsec AH", 1216 ah_init, ah_zeroize, ah_input, ah_output, 1217 }; 1218 1219 static void 1220 ah_attach(void) 1221 { 1222 1223 V_ah_enable = 1; /* control flow of packets with AH */ 1224 V_ah_cleartos = 1; /* clear ip_tos when doing AH calc */ 1225 1226 xform_register(&ah_xformsw); 1227 } 1228 SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ah_attach, NULL); 1229