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