xform_ipcomp.c revision 190787
1/* $FreeBSD: head/sys/netipsec/xform_ipcomp.c 190787 2009-04-06 22:29:41Z zec $ */ 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/socket.h> 41#include <sys/kernel.h> 42#include <sys/protosw.h> 43#include <sys/sysctl.h> 44#include <sys/vimage.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/route.h> 52#include <netipsec/ipsec.h> 53#include <netipsec/xform.h> 54 55#ifdef INET6 56#include <netinet/ip6.h> 57#include <netipsec/ipsec6.h> 58#endif 59 60#include <netipsec/ipcomp.h> 61#include <netipsec/ipcomp_var.h> 62 63#include <netipsec/key.h> 64#include <netipsec/key_debug.h> 65 66#include <opencrypto/cryptodev.h> 67#include <opencrypto/deflate.h> 68#include <opencrypto/xform.h> 69 70#ifdef VIMAGE_GLOBALS 71int ipcomp_enable; 72struct ipcompstat ipcompstat; 73#endif 74 75SYSCTL_DECL(_net_inet_ipcomp); 76SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipcomp, OID_AUTO, 77 ipcomp_enable, CTLFLAG_RW, ipcomp_enable, 0, ""); 78SYSCTL_V_STRUCT(V_NET, vnet_ipsec, _net_inet_ipcomp, IPSECCTL_STATS, 79 stats, CTLFLAG_RD, ipcompstat, ipcompstat, ""); 80 81static int ipcomp_input_cb(struct cryptop *crp); 82static int ipcomp_output_cb(struct cryptop *crp); 83static int ipcomp_iattach(const void *); 84 85struct comp_algo * 86ipcomp_algorithm_lookup(int alg) 87{ 88 if (alg >= IPCOMP_ALG_MAX) 89 return NULL; 90 switch (alg) { 91 case SADB_X_CALG_DEFLATE: 92 return &comp_algo_deflate; 93 } 94 return NULL; 95} 96 97/* 98 * ipcomp_init() is called when an CPI is being set up. 99 */ 100static int 101ipcomp_init(struct secasvar *sav, struct xformsw *xsp) 102{ 103 INIT_VNET_IPSEC(curvnet); 104 struct comp_algo *tcomp; 105 struct cryptoini cric; 106 107 /* NB: algorithm really comes in alg_enc and not alg_comp! */ 108 tcomp = ipcomp_algorithm_lookup(sav->alg_enc); 109 if (tcomp == NULL) { 110 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__, 111 sav->alg_comp)); 112 return EINVAL; 113 } 114 sav->alg_comp = sav->alg_enc; /* set for doing histogram */ 115 sav->tdb_xform = xsp; 116 sav->tdb_compalgxform = tcomp; 117 118 /* Initialize crypto session */ 119 bzero(&cric, sizeof (cric)); 120 cric.cri_alg = sav->tdb_compalgxform->type; 121 122 return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support); 123} 124 125/* 126 * ipcomp_zeroize() used when IPCA is deleted 127 */ 128static int 129ipcomp_zeroize(struct secasvar *sav) 130{ 131 int err; 132 133 err = crypto_freesession(sav->tdb_cryptoid); 134 sav->tdb_cryptoid = 0; 135 return err; 136} 137 138/* 139 * ipcomp_input() gets called to uncompress an input packet 140 */ 141static int 142ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff) 143{ 144 INIT_VNET_IPSEC(curvnet); 145 struct tdb_crypto *tc; 146 struct cryptodesc *crdc; 147 struct cryptop *crp; 148 int hlen = IPCOMP_HLENGTH; 149 150 /* Get crypto descriptors */ 151 crp = crypto_getreq(1); 152 if (crp == NULL) { 153 m_freem(m); 154 DPRINTF(("%s: no crypto descriptors\n", __func__)); 155 V_ipcompstat.ipcomps_crypto++; 156 return ENOBUFS; 157 } 158 /* Get IPsec-specific opaque pointer */ 159 tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO); 160 if (tc == NULL) { 161 m_freem(m); 162 crypto_freereq(crp); 163 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__)); 164 V_ipcompstat.ipcomps_crypto++; 165 return ENOBUFS; 166 } 167 crdc = crp->crp_desc; 168 169 crdc->crd_skip = skip + hlen; 170 crdc->crd_len = m->m_pkthdr.len - (skip + hlen); 171 crdc->crd_inject = skip; 172 173 tc->tc_ptr = 0; 174 175 /* Decompression operation */ 176 crdc->crd_alg = sav->tdb_compalgxform->type; 177 178 /* Crypto operation descriptor */ 179 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen); 180 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 181 crp->crp_buf = (caddr_t) m; 182 crp->crp_callback = ipcomp_input_cb; 183 crp->crp_sid = sav->tdb_cryptoid; 184 crp->crp_opaque = (caddr_t) tc; 185 186 /* These are passed as-is to the callback */ 187 tc->tc_spi = sav->spi; 188 tc->tc_dst = sav->sah->saidx.dst; 189 tc->tc_proto = sav->sah->saidx.proto; 190 tc->tc_protoff = protoff; 191 tc->tc_skip = skip; 192 193 return crypto_dispatch(crp); 194} 195 196#ifdef INET6 197#define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \ 198 if (saidx->dst.sa.sa_family == AF_INET6) { \ 199 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \ 200 } else { \ 201 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \ 202 } \ 203} while (0) 204#else 205#define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \ 206 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag)) 207#endif 208 209/* 210 * IPComp input callback from the crypto driver. 211 */ 212static int 213ipcomp_input_cb(struct cryptop *crp) 214{ 215 INIT_VNET_IPSEC(curvnet); 216 struct cryptodesc *crd; 217 struct tdb_crypto *tc; 218 int skip, protoff; 219 struct mtag *mtag; 220 struct mbuf *m; 221 struct secasvar *sav; 222 struct secasindex *saidx; 223 int hlen = IPCOMP_HLENGTH, error, clen; 224 u_int8_t nproto; 225 caddr_t addr; 226 227 crd = crp->crp_desc; 228 229 tc = (struct tdb_crypto *) crp->crp_opaque; 230 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!")); 231 skip = tc->tc_skip; 232 protoff = tc->tc_protoff; 233 mtag = (struct mtag *) tc->tc_ptr; 234 m = (struct mbuf *) crp->crp_buf; 235 236 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi); 237 if (sav == NULL) { 238 V_ipcompstat.ipcomps_notdb++; 239 DPRINTF(("%s: SA expired while in crypto\n", __func__)); 240 error = ENOBUFS; /*XXX*/ 241 goto bad; 242 } 243 244 saidx = &sav->sah->saidx; 245 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET || 246 saidx->dst.sa.sa_family == AF_INET6, 247 ("unexpected protocol family %u", saidx->dst.sa.sa_family)); 248 249 /* Check for crypto errors */ 250 if (crp->crp_etype) { 251 /* Reset the session ID */ 252 if (sav->tdb_cryptoid != 0) 253 sav->tdb_cryptoid = crp->crp_sid; 254 255 if (crp->crp_etype == EAGAIN) { 256 KEY_FREESAV(&sav); 257 error = crypto_dispatch(crp); 258 return error; 259 } 260 261 V_ipcompstat.ipcomps_noxform++; 262 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 263 error = crp->crp_etype; 264 goto bad; 265 } 266 /* Shouldn't happen... */ 267 if (m == NULL) { 268 V_ipcompstat.ipcomps_crypto++; 269 DPRINTF(("%s: null mbuf returned from crypto\n", __func__)); 270 error = EINVAL; 271 goto bad; 272 } 273 V_ipcompstat.ipcomps_hist[sav->alg_comp]++; 274 275 clen = crp->crp_olen; /* Length of data after processing */ 276 277 /* Release the crypto descriptors */ 278 free(tc, M_XDATA), tc = NULL; 279 crypto_freereq(crp), crp = NULL; 280 281 /* In case it's not done already, adjust the size of the mbuf chain */ 282 m->m_pkthdr.len = clen + hlen + skip; 283 284 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) { 285 V_ipcompstat.ipcomps_hdrops++; /*XXX*/ 286 DPRINTF(("%s: m_pullup failed\n", __func__)); 287 error = EINVAL; /*XXX*/ 288 goto bad; 289 } 290 291 /* Keep the next protocol field */ 292 addr = (caddr_t) mtod(m, struct ip *) + skip; 293 nproto = ((struct ipcomp *) addr)->comp_nxt; 294 295 /* Remove the IPCOMP header */ 296 error = m_striphdr(m, skip, hlen); 297 if (error) { 298 V_ipcompstat.ipcomps_hdrops++; 299 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__, 300 ipsec_address(&sav->sah->saidx.dst), 301 (u_long) ntohl(sav->spi))); 302 goto bad; 303 } 304 305 /* Restore the Next Protocol field */ 306 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto); 307 308 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL); 309 310 KEY_FREESAV(&sav); 311 return error; 312bad: 313 if (sav) 314 KEY_FREESAV(&sav); 315 if (m) 316 m_freem(m); 317 if (tc != NULL) 318 free(tc, M_XDATA); 319 if (crp) 320 crypto_freereq(crp); 321 return error; 322} 323 324/* 325 * IPComp output routine, called by ipsec[46]_process_packet() 326 */ 327static int 328ipcomp_output( 329 struct mbuf *m, 330 struct ipsecrequest *isr, 331 struct mbuf **mp, 332 int skip, 333 int protoff 334) 335{ 336 INIT_VNET_IPSEC(curvnet); 337 struct secasvar *sav; 338 struct comp_algo *ipcompx; 339 int error, ralen, hlen, maxpacketsize, roff; 340 u_int8_t prot; 341 struct cryptodesc *crdc; 342 struct cryptop *crp; 343 struct tdb_crypto *tc; 344 struct mbuf *mo; 345 struct ipcomp *ipcomp; 346 347 sav = isr->sav; 348 IPSEC_ASSERT(sav != NULL, ("null SA")); 349 ipcompx = sav->tdb_compalgxform; 350 IPSEC_ASSERT(ipcompx != NULL, ("null compression xform")); 351 352 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */ 353 hlen = IPCOMP_HLENGTH; 354 355 V_ipcompstat.ipcomps_output++; 356 357 /* Check for maximum packet size violations. */ 358 switch (sav->sah->saidx.dst.sa.sa_family) { 359#ifdef INET 360 case AF_INET: 361 maxpacketsize = IP_MAXPACKET; 362 break; 363#endif /* INET */ 364#ifdef INET6 365 case AF_INET6: 366 maxpacketsize = IPV6_MAXPACKET; 367 break; 368#endif /* INET6 */ 369 default: 370 V_ipcompstat.ipcomps_nopf++; 371 DPRINTF(("%s: unknown/unsupported protocol family %d, " 372 "IPCA %s/%08lx\n", __func__, 373 sav->sah->saidx.dst.sa.sa_family, 374 ipsec_address(&sav->sah->saidx.dst), 375 (u_long) ntohl(sav->spi))); 376 error = EPFNOSUPPORT; 377 goto bad; 378 } 379 if (skip + hlen + ralen > maxpacketsize) { 380 V_ipcompstat.ipcomps_toobig++; 381 DPRINTF(("%s: packet in IPCA %s/%08lx got too big " 382 "(len %u, max len %u)\n", __func__, 383 ipsec_address(&sav->sah->saidx.dst), 384 (u_long) ntohl(sav->spi), 385 skip + hlen + ralen, maxpacketsize)); 386 error = EMSGSIZE; 387 goto bad; 388 } 389 390 /* Update the counters */ 391 V_ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip; 392 393 m = m_unshare(m, M_NOWAIT); 394 if (m == NULL) { 395 V_ipcompstat.ipcomps_hdrops++; 396 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n", 397 __func__, ipsec_address(&sav->sah->saidx.dst), 398 (u_long) ntohl(sav->spi))); 399 error = ENOBUFS; 400 goto bad; 401 } 402 403 /* Inject IPCOMP header */ 404 mo = m_makespace(m, skip, hlen, &roff); 405 if (mo == NULL) { 406 V_ipcompstat.ipcomps_wrap++; 407 DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n", 408 __func__, ipsec_address(&sav->sah->saidx.dst), 409 (u_long) ntohl(sav->spi))); 410 error = ENOBUFS; 411 goto bad; 412 } 413 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff); 414 415 /* Initialize the IPCOMP header */ 416 /* XXX alignment always correct? */ 417 switch (sav->sah->saidx.dst.sa.sa_family) { 418#ifdef INET 419 case AF_INET: 420 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p; 421 break; 422#endif /* INET */ 423#ifdef INET6 424 case AF_INET6: 425 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt; 426 break; 427#endif 428 } 429 ipcomp->comp_flags = 0; 430 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi)); 431 432 /* Fix Next Protocol in IPv4/IPv6 header */ 433 prot = IPPROTO_IPCOMP; 434 m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot); 435 436 /* Ok now, we can pass to the crypto processing */ 437 438 /* Get crypto descriptors */ 439 crp = crypto_getreq(1); 440 if (crp == NULL) { 441 V_ipcompstat.ipcomps_crypto++; 442 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__)); 443 error = ENOBUFS; 444 goto bad; 445 } 446 crdc = crp->crp_desc; 447 448 /* Compression descriptor */ 449 crdc->crd_skip = skip + hlen; 450 crdc->crd_len = m->m_pkthdr.len - (skip + hlen); 451 crdc->crd_flags = CRD_F_COMP; 452 crdc->crd_inject = skip + hlen; 453 454 /* Compression operation */ 455 crdc->crd_alg = ipcompx->type; 456 457 /* IPsec-specific opaque crypto info */ 458 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto), 459 M_XDATA, M_NOWAIT|M_ZERO); 460 if (tc == NULL) { 461 V_ipcompstat.ipcomps_crypto++; 462 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__)); 463 crypto_freereq(crp); 464 error = ENOBUFS; 465 goto bad; 466 } 467 468 tc->tc_isr = isr; 469 tc->tc_spi = sav->spi; 470 tc->tc_dst = sav->sah->saidx.dst; 471 tc->tc_proto = sav->sah->saidx.proto; 472 tc->tc_skip = skip + hlen; 473 474 /* Crypto operation descriptor */ 475 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */ 476 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 477 crp->crp_buf = (caddr_t) m; 478 crp->crp_callback = ipcomp_output_cb; 479 crp->crp_opaque = (caddr_t) tc; 480 crp->crp_sid = sav->tdb_cryptoid; 481 482 return crypto_dispatch(crp); 483bad: 484 if (m) 485 m_freem(m); 486 return (error); 487} 488 489/* 490 * IPComp output callback from the crypto driver. 491 */ 492static int 493ipcomp_output_cb(struct cryptop *crp) 494{ 495 INIT_VNET_IPSEC(curvnet); 496 struct tdb_crypto *tc; 497 struct ipsecrequest *isr; 498 struct secasvar *sav; 499 struct mbuf *m; 500 int error, skip, rlen; 501 502 tc = (struct tdb_crypto *) crp->crp_opaque; 503 IPSEC_ASSERT(tc != NULL, ("null opaque data area!")); 504 m = (struct mbuf *) crp->crp_buf; 505 skip = tc->tc_skip; 506 rlen = crp->crp_ilen - skip; 507 508 isr = tc->tc_isr; 509 IPSECREQUEST_LOCK(isr); 510 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi); 511 if (sav == NULL) { 512 V_ipcompstat.ipcomps_notdb++; 513 DPRINTF(("%s: SA expired while in crypto\n", __func__)); 514 error = ENOBUFS; /*XXX*/ 515 goto bad; 516 } 517 IPSEC_ASSERT(isr->sav == sav, ("SA changed\n")); 518 519 /* Check for crypto errors */ 520 if (crp->crp_etype) { 521 /* Reset session ID */ 522 if (sav->tdb_cryptoid != 0) 523 sav->tdb_cryptoid = crp->crp_sid; 524 525 if (crp->crp_etype == EAGAIN) { 526 KEY_FREESAV(&sav); 527 IPSECREQUEST_UNLOCK(isr); 528 error = crypto_dispatch(crp); 529 return error; 530 } 531 V_ipcompstat.ipcomps_noxform++; 532 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 533 error = crp->crp_etype; 534 goto bad; 535 } 536 /* Shouldn't happen... */ 537 if (m == NULL) { 538 V_ipcompstat.ipcomps_crypto++; 539 DPRINTF(("%s: bogus return buffer from crypto\n", __func__)); 540 error = EINVAL; 541 goto bad; 542 } 543 V_ipcompstat.ipcomps_hist[sav->alg_comp]++; 544 545 if (rlen > crp->crp_olen) { 546 /* Adjust the length in the IP header */ 547 switch (sav->sah->saidx.dst.sa.sa_family) { 548#ifdef INET 549 case AF_INET: 550 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 551 break; 552#endif /* INET */ 553#ifdef INET6 554 case AF_INET6: 555 mtod(m, struct ip6_hdr *)->ip6_plen = 556 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr); 557 break; 558#endif /* INET6 */ 559 default: 560 V_ipcompstat.ipcomps_nopf++; 561 DPRINTF(("%s: unknown/unsupported protocol " 562 "family %d, IPCA %s/%08lx\n", __func__, 563 sav->sah->saidx.dst.sa.sa_family, 564 ipsec_address(&sav->sah->saidx.dst), 565 (u_long) ntohl(sav->spi))); 566 error = EPFNOSUPPORT; 567 goto bad; 568 } 569 } else { 570 /* compression was useless, we have lost time */ 571 /* XXX add statistic */ 572 } 573 574 /* Release the crypto descriptor */ 575 free(tc, M_XDATA); 576 crypto_freereq(crp); 577 578 /* NB: m is reclaimed by ipsec_process_done. */ 579 error = ipsec_process_done(m, isr); 580 KEY_FREESAV(&sav); 581 IPSECREQUEST_UNLOCK(isr); 582 return error; 583bad: 584 if (sav) 585 KEY_FREESAV(&sav); 586 IPSECREQUEST_UNLOCK(isr); 587 if (m) 588 m_freem(m); 589 free(tc, M_XDATA); 590 crypto_freereq(crp); 591 return error; 592} 593 594static struct xformsw ipcomp_xformsw = { 595 XF_IPCOMP, XFT_COMP, "IPcomp", 596 ipcomp_init, ipcomp_zeroize, ipcomp_input, 597 ipcomp_output 598}; 599 600static void 601ipcomp_attach(void) 602{ 603 604 xform_register(&ipcomp_xformsw); 605 ipcomp_iattach(NULL); 606} 607 608static int 609ipcomp_iattach(const void *unused __unused) 610{ 611 INIT_VNET_IPSEC(curvnet); 612 613 V_ipcomp_enable = 0; 614 return (0); 615} 616SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL); 617