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