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