2/* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */ 3 4/*- 5 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org) 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31/* IP payload compression protocol (IPComp), see RFC 2393 */ 32#include "opt_inet.h" 33#include "opt_inet6.h" 34 35#include <sys/param.h> 36#include <sys/systm.h> 37#include <sys/mbuf.h> 38#include <sys/lock.h> 39#include <sys/mutex.h> 40#include <sys/rwlock.h> 41#include <sys/socket.h> 42#include <sys/kernel.h> 43#include <sys/protosw.h> 44#include <sys/sysctl.h> 45 46#include <netinet/in.h> 47#include <netinet/in_systm.h> 48#include <netinet/ip.h> 49#include <netinet/ip_var.h> 50 51#include <net/vnet.h> 52 53#include <netipsec/ipsec.h> 54#include <netipsec/xform.h> 55 56#ifdef INET6 57#include <netinet/ip6.h> 58#include <netipsec/ipsec6.h> 59#endif 60 61#include <netipsec/ipcomp.h> 62#include <netipsec/ipcomp_var.h> 63 64#include <netipsec/key.h> 65#include <netipsec/key_debug.h> 66 67#include <opencrypto/cryptodev.h> 68#include <opencrypto/deflate.h> 69#include <opencrypto/xform.h> 70 71VNET_DEFINE(int, ipcomp_enable) = 1; 72VNET_PCPUSTAT_DEFINE(struct ipcompstat, ipcompstat); 73VNET_PCPUSTAT_SYSINIT(ipcompstat); 74 75#ifdef VIMAGE 76VNET_PCPUSTAT_SYSUNINIT(ipcompstat); 77#endif /* VIMAGE */ 78 79SYSCTL_DECL(_net_inet_ipcomp); 80SYSCTL_INT(_net_inet_ipcomp, OID_AUTO, ipcomp_enable, 81 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipcomp_enable), 0, ""); 82SYSCTL_VNET_PCPUSTAT(_net_inet_ipcomp, IPSECCTL_STATS, stats, 83 struct ipcompstat, ipcompstat, 84 "IPCOMP statistics (struct ipcompstat, netipsec/ipcomp_var.h"); 85 86static int ipcomp_input_cb(struct cryptop *crp); 87static int ipcomp_output_cb(struct cryptop *crp); 88 89struct comp_algo * 90ipcomp_algorithm_lookup(int alg) 91{ 92 if (alg >= IPCOMP_ALG_MAX) 93 return NULL; 94 switch (alg) { 95 case SADB_X_CALG_DEFLATE: 96 return &comp_algo_deflate; 97 } 98 return NULL; 99} 100 101/* 102 * ipcomp_init() is called when an CPI is being set up. 103 */ 104static int 105ipcomp_init(struct secasvar *sav, struct xformsw *xsp) 106{ 107 struct comp_algo *tcomp; 108 struct cryptoini cric; 109 110 /* NB: algorithm really comes in alg_enc and not alg_comp! */ 111 tcomp = ipcomp_algorithm_lookup(sav->alg_enc); 112 if (tcomp == NULL) { 113 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__, 114 sav->alg_comp)); 115 return EINVAL; 116 } 117 sav->alg_comp = sav->alg_enc; /* set for doing histogram */ 118 sav->tdb_xform = xsp; 119 sav->tdb_compalgxform = tcomp; 120 121 /* Initialize crypto session */ 122 bzero(&cric, sizeof (cric)); 123 cric.cri_alg = sav->tdb_compalgxform->type; 124 125 return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support); 126} 127 128/* 129 * ipcomp_zeroize() used when IPCA is deleted 130 */ 131static int 132ipcomp_zeroize(struct secasvar *sav) 133{ 134 int err; 135 136 err = crypto_freesession(sav->tdb_cryptoid); 137 sav->tdb_cryptoid = 0; 138 return err; 139} 140 141/* 142 * ipcomp_input() gets called to uncompress an input packet 143 */ 144static int 145ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff) 146{ 147 struct tdb_crypto *tc; 148 struct cryptodesc *crdc; 149 struct cryptop *crp; 150 struct ipcomp *ipcomp; 151 caddr_t addr; 152 int hlen = IPCOMP_HLENGTH; 153 154 /* 155 * Check that the next header of the IPComp is not IPComp again, before 156 * doing any real work. Given it is not possible to do double 157 * compression it means someone is playing tricks on us. 158 */ 159 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) { 160 IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/ 161 DPRINTF(("%s: m_pullup failed\n", __func__)); 162 return (ENOBUFS); 163 } 164 addr = (caddr_t) mtod(m, struct ip *) + skip; 165 ipcomp = (struct ipcomp *)addr; 166 if (ipcomp->comp_nxt == IPPROTO_IPCOMP) { 167 m_freem(m); 168 IPCOMPSTAT_INC(ipcomps_pdrops); /* XXX have our own stats? */ 169 DPRINTF(("%s: recursive compression detected\n", __func__)); 170 return (EINVAL); 171 } 172 173 /* Get crypto descriptors */ 174 crp = crypto_getreq(1); 175 if (crp == NULL) { 176 m_freem(m); 177 DPRINTF(("%s: no crypto descriptors\n", __func__)); 178 IPCOMPSTAT_INC(ipcomps_crypto); 179 return ENOBUFS; 180 } 181 /* Get IPsec-specific opaque pointer */ 182 tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO); 183 if (tc == NULL) { 184 m_freem(m); 185 crypto_freereq(crp); 186 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__)); 187 IPCOMPSTAT_INC(ipcomps_crypto); 188 return ENOBUFS; 189 } 190 crdc = crp->crp_desc; 191 192 crdc->crd_skip = skip + hlen; 193 crdc->crd_len = m->m_pkthdr.len - (skip + hlen); 194 crdc->crd_inject = skip; 195 196 tc->tc_ptr = 0; 197 198 /* Decompression operation */ 199 crdc->crd_alg = sav->tdb_compalgxform->type; 200 201 /* Crypto operation descriptor */ 202 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen); 203 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 204 crp->crp_buf = (caddr_t) m; 205 crp->crp_callback = ipcomp_input_cb; 206 crp->crp_sid = sav->tdb_cryptoid; 207 crp->crp_opaque = (caddr_t) tc; 208 209 /* These are passed as-is to the callback */ 210 tc->tc_spi = sav->spi; 211 tc->tc_dst = sav->sah->saidx.dst; 212 tc->tc_proto = sav->sah->saidx.proto; 213 tc->tc_protoff = protoff; 214 tc->tc_skip = skip; 215 KEY_ADDREFSA(sav); 216 tc->tc_sav = sav; 217 218 return crypto_dispatch(crp); 219} 220 221/* 222 * IPComp input callback from the crypto driver. 223 */ 224static int 225ipcomp_input_cb(struct cryptop *crp) 226{ 227 char buf[INET6_ADDRSTRLEN]; 228 struct cryptodesc *crd; 229 struct tdb_crypto *tc; 230 int skip, protoff; 231 struct mbuf *m; 232 struct secasvar *sav; 233 struct secasindex *saidx; 234 int hlen = IPCOMP_HLENGTH, error, clen; 235 u_int8_t nproto; 236 caddr_t addr; 237 238 crd = crp->crp_desc; 239 240 tc = (struct tdb_crypto *) crp->crp_opaque; 241 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!")); 242 skip = tc->tc_skip; 243 protoff = tc->tc_protoff; 244 m = (struct mbuf *) crp->crp_buf; 245 246 sav = tc->tc_sav; 247 IPSEC_ASSERT(sav != NULL, ("null SA!")); 248 249 saidx = &sav->sah->saidx; 250 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET || 251 saidx->dst.sa.sa_family == AF_INET6, 252 ("unexpected protocol family %u", saidx->dst.sa.sa_family)); 253 254 /* Check for crypto errors */ 255 if (crp->crp_etype) { 256 /* Reset the session ID */ 257 if (sav->tdb_cryptoid != 0) 258 sav->tdb_cryptoid = crp->crp_sid; 259 260 if (crp->crp_etype == EAGAIN) { 261 return crypto_dispatch(crp); 262 } 263 IPCOMPSTAT_INC(ipcomps_noxform); 264 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 265 error = crp->crp_etype; 266 goto bad; 267 } 268 /* Shouldn't happen... */ 269 if (m == NULL) { 270 IPCOMPSTAT_INC(ipcomps_crypto); 271 DPRINTF(("%s: null mbuf returned from crypto\n", __func__)); 272 error = EINVAL; 273 goto bad; 274 } 275 IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]); 276 277 clen = crp->crp_olen; /* Length of data after processing */ 278 279 /* Release the crypto descriptors */ 280 free(tc, M_XDATA), tc = NULL; 281 crypto_freereq(crp), crp = NULL; 282 283 /* In case it's not done already, adjust the size of the mbuf chain */ 284 m->m_pkthdr.len = clen + hlen + skip; 285 286 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) { 287 IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/ 288 DPRINTF(("%s: m_pullup failed\n", __func__)); 289 error = EINVAL; /*XXX*/ 290 goto bad; 291 } 292 293 /* Keep the next protocol field */ 294 addr = (caddr_t) mtod(m, struct ip *) + skip; 295 nproto = ((struct ipcomp *) addr)->comp_nxt; 296 297 /* Remove the IPCOMP header */ 298 error = m_striphdr(m, skip, hlen); 299 if (error) { 300 IPCOMPSTAT_INC(ipcomps_hdrops); 301 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__, 302 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 303 (u_long) ntohl(sav->spi))); 304 goto bad; 305 } 306 307 /* Restore the Next Protocol field */ 308 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto); 309 310 switch (saidx->dst.sa.sa_family) { 311#ifdef INET6 312 case AF_INET6: 313 error = ipsec6_common_input_cb(m, sav, skip, protoff); 314 break; 315#endif 316#ifdef INET 317 case AF_INET: 318 error = ipsec4_common_input_cb(m, sav, skip, protoff); 319 break; 320#endif 321 default: 322 panic("%s: Unexpected address family: %d saidx=%p", __func__, 323 saidx->dst.sa.sa_family, saidx); 324 } 325 326 KEY_FREESAV(&sav); 327 return error; 328bad: 329 if (sav) 330 KEY_FREESAV(&sav); 331 if (m) 332 m_freem(m); 333 if (tc != NULL) 334 free(tc, M_XDATA); 335 if (crp) 336 crypto_freereq(crp); 337 return error; 338} 339 340/* 341 * IPComp output routine, called by ipsec[46]_process_packet() 342 */ 343static int 344ipcomp_output(struct mbuf *m, struct ipsecrequest *isr, struct mbuf **mp, 345 int skip, int protoff) 346{ 347 char buf[INET6_ADDRSTRLEN]; 348 struct secasvar *sav; 349 struct comp_algo *ipcompx; 350 int error, ralen, maxpacketsize; 351 struct cryptodesc *crdc; 352 struct cryptop *crp; 353 struct tdb_crypto *tc; 354 355 sav = isr->sav; 356 IPSEC_ASSERT(sav != NULL, ("null SA")); 357 ipcompx = sav->tdb_compalgxform; 358 IPSEC_ASSERT(ipcompx != NULL, ("null compression xform")); 359 360 /* 361 * Do not touch the packet in case our payload to compress 362 * is lower than the minimal threshold of the compression 363 * alogrithm. We will just send out the data uncompressed. 364 * See RFC 3173, 2.2. Non-Expansion Policy. 365 */ 366 if (m->m_pkthdr.len <= ipcompx->minlen) { 367 IPCOMPSTAT_INC(ipcomps_threshold); 368 return ipsec_process_done(m, isr); 369 } 370 371 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */ 372 IPCOMPSTAT_INC(ipcomps_output); 373 374 /* Check for maximum packet size violations. */ 375 switch (sav->sah->saidx.dst.sa.sa_family) { 376#ifdef INET 377 case AF_INET: 378 maxpacketsize = IP_MAXPACKET; 379 break; 380#endif /* INET */ 381#ifdef INET6 382 case AF_INET6: 383 maxpacketsize = IPV6_MAXPACKET; 384 break; 385#endif /* INET6 */ 386 default: 387 IPCOMPSTAT_INC(ipcomps_nopf); 388 DPRINTF(("%s: unknown/unsupported protocol family %d, " 389 "IPCA %s/%08lx\n", __func__, 390 sav->sah->saidx.dst.sa.sa_family, 391 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 392 (u_long) ntohl(sav->spi))); 393 error = EPFNOSUPPORT; 394 goto bad; 395 } 396 if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) { 397 IPCOMPSTAT_INC(ipcomps_toobig); 398 DPRINTF(("%s: packet in IPCA %s/%08lx got too big " 399 "(len %u, max len %u)\n", __func__, 400 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 401 (u_long) ntohl(sav->spi), 402 ralen + skip + IPCOMP_HLENGTH, maxpacketsize)); 403 error = EMSGSIZE; 404 goto bad; 405 } 406 407 /* Update the counters */ 408 IPCOMPSTAT_ADD(ipcomps_obytes, m->m_pkthdr.len - skip); 409 410 m = m_unshare(m, M_NOWAIT); 411 if (m == NULL) { 412 IPCOMPSTAT_INC(ipcomps_hdrops); 413 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n", 414 __func__, ipsec_address(&sav->sah->saidx.dst, buf, 415 sizeof(buf)), (u_long) ntohl(sav->spi))); 416 error = ENOBUFS; 417 goto bad; 418 } 419 420 /* Ok now, we can pass to the crypto processing. */ 421 422 /* Get crypto descriptors */ 423 crp = crypto_getreq(1); 424 if (crp == NULL) { 425 IPCOMPSTAT_INC(ipcomps_crypto); 426 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__)); 427 error = ENOBUFS; 428 goto bad; 429 } 430 crdc = crp->crp_desc; 431 432 /* Compression descriptor */ 433 crdc->crd_skip = skip; 434 crdc->crd_len = ralen; 435 crdc->crd_flags = CRD_F_COMP; 436 crdc->crd_inject = skip; 437 438 /* Compression operation */ 439 crdc->crd_alg = ipcompx->type; 440 441 /* IPsec-specific opaque crypto info */ 442 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto), 443 M_XDATA, M_NOWAIT|M_ZERO); 444 if (tc == NULL) { 445 IPCOMPSTAT_INC(ipcomps_crypto); 446 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__)); 447 crypto_freereq(crp); 448 error = ENOBUFS; 449 goto bad; 450 } 451 452 tc->tc_isr = isr; 453 KEY_ADDREFSA(sav); 454 tc->tc_sav = sav; 455 tc->tc_spi = sav->spi; 456 tc->tc_dst = sav->sah->saidx.dst; 457 tc->tc_proto = sav->sah->saidx.proto; 458 tc->tc_protoff = protoff; 459 tc->tc_skip = skip; 460 461 /* Crypto operation descriptor */ 462 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */ 463 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 464 crp->crp_buf = (caddr_t) m; 465 crp->crp_callback = ipcomp_output_cb; 466 crp->crp_opaque = (caddr_t) tc; 467 crp->crp_sid = sav->tdb_cryptoid; 468 469 return crypto_dispatch(crp); 470bad: 471 if (m) 472 m_freem(m); 473 return (error); 474} 475 476/* 477 * IPComp output callback from the crypto driver. 478 */ 479static int 480ipcomp_output_cb(struct cryptop *crp) 481{ 482 char buf[INET6_ADDRSTRLEN]; 483 struct tdb_crypto *tc; 484 struct ipsecrequest *isr; 485 struct secasvar *sav; 486 struct mbuf *m; 487 int error, skip; 488 489 tc = (struct tdb_crypto *) crp->crp_opaque; 490 IPSEC_ASSERT(tc != NULL, ("null opaque data area!")); 491 m = (struct mbuf *) crp->crp_buf; 492 skip = tc->tc_skip; 493 494 isr = tc->tc_isr;
|
496 IPSECREQUEST_LOCK(isr); 497 sav = tc->tc_sav; 498 /* With the isr lock released SA pointer can be updated. */ 499 if (sav != isr->sav) { 500 IPCOMPSTAT_INC(ipcomps_notdb); 501 DPRINTF(("%s: SA expired while in crypto\n", __func__)); 502 error = ENOBUFS; /*XXX*/ 503 goto bad; 504 } 505 506 /* Check for crypto errors */ 507 if (crp->crp_etype) { 508 /* Reset the session ID */ 509 if (sav->tdb_cryptoid != 0) 510 sav->tdb_cryptoid = crp->crp_sid; 511 512 if (crp->crp_etype == EAGAIN) { 513 IPSECREQUEST_UNLOCK(isr); 514 return crypto_dispatch(crp); 515 } 516 IPCOMPSTAT_INC(ipcomps_noxform); 517 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 518 error = crp->crp_etype; 519 goto bad; 520 } 521 /* Shouldn't happen... */ 522 if (m == NULL) { 523 IPCOMPSTAT_INC(ipcomps_crypto); 524 DPRINTF(("%s: bogus return buffer from crypto\n", __func__)); 525 error = EINVAL; 526 goto bad; 527 } 528 IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]); 529 530 if (crp->crp_ilen - skip > crp->crp_olen) { 531 struct mbuf *mo; 532 struct ipcomp *ipcomp; 533 int roff; 534 uint8_t prot; 535 536 /* Compression helped, inject IPCOMP header. */ 537 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff); 538 if (mo == NULL) { 539 IPCOMPSTAT_INC(ipcomps_wrap); 540 DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n", 541 __func__, ipsec_address(&sav->sah->saidx.dst, buf, 542 sizeof(buf)), (u_long) ntohl(sav->spi))); 543 error = ENOBUFS; 544 goto bad; 545 } 546 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff); 547 548 /* Initialize the IPCOMP header */ 549 /* XXX alignment always correct? */ 550 switch (sav->sah->saidx.dst.sa.sa_family) { 551#ifdef INET 552 case AF_INET: 553 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p; 554 break; 555#endif /* INET */ 556#ifdef INET6 557 case AF_INET6: 558 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt; 559 break; 560#endif 561 } 562 ipcomp->comp_flags = 0; 563 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi)); 564 565 /* Fix Next Protocol in IPv4/IPv6 header */ 566 prot = IPPROTO_IPCOMP; 567 m_copyback(m, tc->tc_protoff, sizeof(u_int8_t), 568 (u_char *)&prot); 569 570 /* Adjust the length in the IP header */ 571 switch (sav->sah->saidx.dst.sa.sa_family) { 572#ifdef INET 573 case AF_INET: 574 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 575 break; 576#endif /* INET */ 577#ifdef INET6 578 case AF_INET6: 579 mtod(m, struct ip6_hdr *)->ip6_plen = 580 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr); 581 break; 582#endif /* INET6 */ 583 default: 584 IPCOMPSTAT_INC(ipcomps_nopf); 585 DPRINTF(("%s: unknown/unsupported protocol " 586 "family %d, IPCA %s/%08lx\n", __func__, 587 sav->sah->saidx.dst.sa.sa_family, 588 ipsec_address(&sav->sah->saidx.dst, buf, 589 sizeof(buf)), (u_long) ntohl(sav->spi))); 590 error = EPFNOSUPPORT; 591 goto bad; 592 } 593 } else { 594 /* Compression was useless, we have lost time. */ 595 IPCOMPSTAT_INC(ipcomps_uncompr); 596 DPRINTF(("%s: compressions was useless %d - %d <= %d\n", 597 __func__, crp->crp_ilen, skip, crp->crp_olen)); 598 /* XXX remember state to not compress the next couple 599 * of packets, RFC 3173, 2.2. Non-Expansion Policy */ 600 } 601 602 /* Release the crypto descriptor */ 603 free(tc, M_XDATA); 604 crypto_freereq(crp); 605 606 /* NB: m is reclaimed by ipsec_process_done. */ 607 error = ipsec_process_done(m, isr); 608 KEY_FREESAV(&sav); 609 IPSECREQUEST_UNLOCK(isr);
|