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