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