1 /* $NetBSD: xform_ipcomp.c,v 1.26 2011/04/01 08:29:29 spz 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.26 2011/04/01 08:29:29 spz 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 mutex_spin_enter(&crypto_mtx); 130 ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support); 131 mutex_spin_exit(&crypto_mtx); 132 return ses; 133 } 134 135 /* 136 * ipcomp_zeroize() used when IPCA is deleted 137 */ 138 static int 139 ipcomp_zeroize(struct secasvar *sav) 140 { 141 int err; 142 143 mutex_spin_enter(&crypto_mtx); 144 err = crypto_freesession(sav->tdb_cryptoid); 145 mutex_spin_exit(&crypto_mtx); 146 sav->tdb_cryptoid = 0; 147 return err; 148 } 149 150 /* 151 * ipcomp_input() gets called to uncompress an input packet 152 */ 153 static int 154 ipcomp_input(struct mbuf *m, const struct secasvar *sav, int skip, int protoff) 155 { 156 struct tdb_crypto *tc; 157 struct cryptodesc *crdc; 158 struct cryptop *crp; 159 int hlen = IPCOMP_HLENGTH; 160 161 IPSEC_SPLASSERT_SOFTNET("ipcomp_input"); 162 163 /* Get crypto descriptors */ 164 crp = crypto_getreq(1); 165 if (crp == NULL) { 166 m_freem(m); 167 DPRINTF(("ipcomp_input: no crypto descriptors\n")); 168 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); 169 return ENOBUFS; 170 } 171 /* Get IPsec-specific opaque pointer */ 172 tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO); 173 if (tc == NULL) { 174 m_freem(m); 175 crypto_freereq(crp); 176 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n")); 177 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); 178 return ENOBUFS; 179 } 180 crdc = crp->crp_desc; 181 182 crdc->crd_skip = skip + hlen; 183 crdc->crd_len = m->m_pkthdr.len - (skip + hlen); 184 crdc->crd_inject = skip; 185 186 tc->tc_ptr = 0; 187 188 /* Decompression operation */ 189 crdc->crd_alg = sav->tdb_compalgxform->type; 190 191 /* Crypto operation descriptor */ 192 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen); 193 crp->crp_olen = MCLBYTES; /* hint to decompression code */ 194 crp->crp_flags = CRYPTO_F_IMBUF; 195 crp->crp_buf = m; 196 crp->crp_callback = ipcomp_input_cb; 197 crp->crp_sid = sav->tdb_cryptoid; 198 crp->crp_opaque = tc; 199 200 /* These are passed as-is to the callback */ 201 tc->tc_spi = sav->spi; 202 tc->tc_dst = sav->sah->saidx.dst; 203 tc->tc_proto = sav->sah->saidx.proto; 204 tc->tc_protoff = protoff; 205 tc->tc_skip = skip; 206 207 return crypto_dispatch(crp); 208 } 209 210 #ifdef INET6 211 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \ 212 if (saidx->dst.sa.sa_family == AF_INET6) { \ 213 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \ 214 } else { \ 215 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \ 216 } \ 217 } while (0) 218 #else 219 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \ 220 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag)) 221 #endif 222 223 /* 224 * IPComp input callback from the crypto driver. 225 */ 226 static int 227 ipcomp_input_cb(struct cryptop *crp) 228 { 229 struct cryptodesc *crd; 230 struct tdb_crypto *tc; 231 int skip, protoff; 232 struct mtag *mtag; 233 struct mbuf *m; 234 struct secasvar *sav; 235 struct secasindex *saidx; 236 int s, hlen = IPCOMP_HLENGTH, error, clen; 237 u_int8_t nproto; 238 void *addr; 239 u_int16_t dport = 0; 240 u_int16_t sport = 0; 241 #ifdef IPSEC_NAT_T 242 struct m_tag * tag = NULL; 243 #endif 244 245 crd = crp->crp_desc; 246 247 tc = (struct tdb_crypto *) crp->crp_opaque; 248 IPSEC_ASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!")); 249 skip = tc->tc_skip; 250 protoff = tc->tc_protoff; 251 mtag = (struct mtag *) tc->tc_ptr; 252 m = (struct mbuf *) crp->crp_buf; 253 254 #ifdef IPSEC_NAT_T 255 /* find the source port for NAT-T */ 256 if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL))) { 257 sport = ((u_int16_t *)(tag + 1))[0]; 258 dport = ((u_int16_t *)(tag + 1))[1]; 259 } 260 #endif 261 262 s = splsoftnet(); 263 mutex_enter(softnet_lock); 264 265 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, sport, dport); 266 if (sav == NULL) { 267 IPCOMP_STATINC(IPCOMP_STAT_NOTDB); 268 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n")); 269 error = ENOBUFS; /*XXX*/ 270 goto bad; 271 } 272 273 saidx = &sav->sah->saidx; 274 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET || 275 saidx->dst.sa.sa_family == AF_INET6, 276 ("ah_input_cb: unexpected protocol family %u", 277 saidx->dst.sa.sa_family)); 278 279 /* Check for crypto errors */ 280 if (crp->crp_etype) { 281 /* Reset the session ID */ 282 if (sav->tdb_cryptoid != 0) 283 sav->tdb_cryptoid = crp->crp_sid; 284 285 if (crp->crp_etype == EAGAIN) { 286 KEY_FREESAV(&sav); 287 mutex_exit(softnet_lock); 288 splx(s); 289 return crypto_dispatch(crp); 290 } 291 292 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM); 293 DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp->crp_etype)); 294 error = crp->crp_etype; 295 goto bad; 296 } 297 /* Shouldn't happen... */ 298 if (m == NULL) { 299 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); 300 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n")); 301 error = EINVAL; 302 goto bad; 303 } 304 IPCOMP_STATINC(IPCOMP_STAT_HIST + sav->alg_comp); 305 306 /* Update the counters */ 307 IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen); 308 309 310 clen = crp->crp_olen; /* Length of data after processing */ 311 312 /* Release the crypto descriptors */ 313 free(tc, M_XDATA), tc = NULL; 314 crypto_freereq(crp), crp = NULL; 315 316 /* In case it's not done already, adjust the size of the mbuf chain */ 317 m->m_pkthdr.len = clen + hlen + skip; 318 319 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) { 320 IPCOMP_STATINC(IPCOMP_STAT_HDROPS); /*XXX*/ 321 DPRINTF(("ipcomp_input_cb: m_pullup failed\n")); 322 error = EINVAL; /*XXX*/ 323 goto bad; 324 } 325 326 /* Keep the next protocol field */ 327 addr = (uint8_t*) mtod(m, struct ip *) + skip; 328 nproto = ((struct ipcomp *) addr)->comp_nxt; 329 if (nproto == IPPROTO_IPCOMP || nproto == IPPROTO_AH || nproto == IPPROTO_ESP) { 330 IPCOMP_STATINC(IPCOMP_STAT_HDROPS); 331 DPRINTF(("ipcomp_input_cb: nested ipcomp, IPCA %s/%08lx\n", 332 ipsec_address(&sav->sah->saidx.dst), 333 (u_long) ntohl(sav->spi))); 334 error = EINVAL; 335 goto bad; 336 } 337 338 /* Remove the IPCOMP header */ 339 error = m_striphdr(m, skip, hlen); 340 if (error) { 341 IPCOMP_STATINC(IPCOMP_STAT_HDROPS); 342 DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n", 343 ipsec_address(&sav->sah->saidx.dst), 344 (u_long) ntohl(sav->spi))); 345 goto bad; 346 } 347 348 /* Restore the Next Protocol field */ 349 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto); 350 351 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL); 352 353 KEY_FREESAV(&sav); 354 mutex_exit(softnet_lock); 355 splx(s); 356 return error; 357 bad: 358 if (sav) 359 KEY_FREESAV(&sav); 360 mutex_exit(softnet_lock); 361 splx(s); 362 if (m) 363 m_freem(m); 364 if (tc != NULL) 365 free(tc, M_XDATA); 366 if (crp) 367 crypto_freereq(crp); 368 return error; 369 } 370 371 /* 372 * IPComp output routine, called by ipsec[46]_process_packet() 373 */ 374 static int 375 ipcomp_output( 376 struct mbuf *m, 377 struct ipsecrequest *isr, 378 struct mbuf **mp, 379 int skip, 380 int protoff 381 ) 382 { 383 const struct secasvar *sav; 384 const struct comp_algo *ipcompx; 385 int error, ralen, hlen, maxpacketsize; 386 struct cryptodesc *crdc; 387 struct cryptop *crp; 388 struct tdb_crypto *tc; 389 390 IPSEC_SPLASSERT_SOFTNET("ipcomp_output"); 391 sav = isr->sav; 392 IPSEC_ASSERT(sav != NULL, ("ipcomp_output: null SA")); 393 ipcompx = sav->tdb_compalgxform; 394 IPSEC_ASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform")); 395 396 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */ 397 398 /* Don't process the packet if it is too short */ 399 if (ralen < ipcompx->minlen) { 400 IPCOMP_STATINC(IPCOMP_STAT_MINLEN); 401 return ipsec_process_done(m,isr); 402 } 403 404 hlen = IPCOMP_HLENGTH; 405 406 IPCOMP_STATINC(IPCOMP_STAT_OUTPUT); 407 408 /* Check for maximum packet size violations. */ 409 switch (sav->sah->saidx.dst.sa.sa_family) { 410 #ifdef INET 411 case AF_INET: 412 maxpacketsize = IP_MAXPACKET; 413 break; 414 #endif /* INET */ 415 #ifdef INET6 416 case AF_INET6: 417 maxpacketsize = IPV6_MAXPACKET; 418 break; 419 #endif /* INET6 */ 420 default: 421 IPCOMP_STATINC(IPCOMP_STAT_NOPF); 422 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d" 423 ", IPCA %s/%08lx\n", 424 sav->sah->saidx.dst.sa.sa_family, 425 ipsec_address(&sav->sah->saidx.dst), 426 (u_long) ntohl(sav->spi))); 427 error = EPFNOSUPPORT; 428 goto bad; 429 } 430 if (skip + hlen + ralen > maxpacketsize) { 431 IPCOMP_STATINC(IPCOMP_STAT_TOOBIG); 432 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big " 433 "(len %u, max len %u)\n", 434 ipsec_address(&sav->sah->saidx.dst), 435 (u_long) ntohl(sav->spi), 436 skip + hlen + ralen, maxpacketsize)); 437 error = EMSGSIZE; 438 goto bad; 439 } 440 441 /* Update the counters */ 442 IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip); 443 444 m = m_clone(m); 445 if (m == NULL) { 446 IPCOMP_STATINC(IPCOMP_STAT_HDROPS); 447 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n", 448 ipsec_address(&sav->sah->saidx.dst), 449 (u_long) ntohl(sav->spi))); 450 error = ENOBUFS; 451 goto bad; 452 } 453 454 /* Ok now, we can pass to the crypto processing */ 455 456 /* Get crypto descriptors */ 457 crp = crypto_getreq(1); 458 if (crp == NULL) { 459 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); 460 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n")); 461 error = ENOBUFS; 462 goto bad; 463 } 464 crdc = crp->crp_desc; 465 466 /* Compression descriptor */ 467 crdc->crd_skip = skip; 468 crdc->crd_len = m->m_pkthdr.len - skip; 469 crdc->crd_flags = CRD_F_COMP; 470 crdc->crd_inject = skip; 471 472 /* Compression operation */ 473 crdc->crd_alg = ipcompx->type; 474 475 /* IPsec-specific opaque crypto info */ 476 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto), 477 M_XDATA, M_NOWAIT|M_ZERO); 478 if (tc == NULL) { 479 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); 480 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n")); 481 crypto_freereq(crp); 482 error = ENOBUFS; 483 goto bad; 484 } 485 486 tc->tc_isr = isr; 487 tc->tc_spi = sav->spi; 488 tc->tc_dst = sav->sah->saidx.dst; 489 tc->tc_proto = sav->sah->saidx.proto; 490 tc->tc_skip = skip; 491 tc->tc_protoff = protoff; 492 493 /* Crypto operation descriptor */ 494 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */ 495 crp->crp_flags = CRYPTO_F_IMBUF; 496 crp->crp_buf = m; 497 crp->crp_callback = ipcomp_output_cb; 498 crp->crp_opaque = tc; 499 crp->crp_sid = sav->tdb_cryptoid; 500 501 return crypto_dispatch(crp); 502 bad: 503 if (m) 504 m_freem(m); 505 return (error); 506 } 507 508 /* 509 * IPComp output callback from the crypto driver. 510 */ 511 static int 512 ipcomp_output_cb(struct cryptop *crp) 513 { 514 struct tdb_crypto *tc; 515 struct ipsecrequest *isr; 516 struct secasvar *sav; 517 struct mbuf *m, *mo; 518 int s, error, skip, rlen, roff; 519 u_int8_t prot; 520 u_int16_t cpi; 521 struct ipcomp * ipcomp; 522 523 524 tc = (struct tdb_crypto *) crp->crp_opaque; 525 IPSEC_ASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!")); 526 m = (struct mbuf *) crp->crp_buf; 527 skip = tc->tc_skip; 528 rlen = crp->crp_ilen - skip; 529 530 s = splsoftnet(); 531 mutex_enter(softnet_lock); 532 533 isr = tc->tc_isr; 534 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0); 535 if (sav == NULL) { 536 IPCOMP_STATINC(IPCOMP_STAT_NOTDB); 537 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n")); 538 error = ENOBUFS; /*XXX*/ 539 goto bad; 540 } 541 IPSEC_ASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n")); 542 543 /* Check for crypto errors */ 544 if (crp->crp_etype) { 545 /* Reset session ID */ 546 if (sav->tdb_cryptoid != 0) 547 sav->tdb_cryptoid = crp->crp_sid; 548 549 if (crp->crp_etype == EAGAIN) { 550 KEY_FREESAV(&sav); 551 mutex_exit(softnet_lock); 552 splx(s); 553 return crypto_dispatch(crp); 554 } 555 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM); 556 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype)); 557 error = crp->crp_etype; 558 goto bad; 559 } 560 /* Shouldn't happen... */ 561 if (m == NULL) { 562 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); 563 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n")); 564 error = EINVAL; 565 goto bad; 566 } 567 IPCOMP_STATINC(IPCOMP_STAT_HIST + sav->alg_comp); 568 569 if (rlen > crp->crp_olen) { 570 /* Inject IPCOMP header */ 571 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff); 572 if (mo == NULL) { 573 IPCOMP_STATINC(IPCOMP_STAT_WRAP); 574 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for " 575 "IPCA %s/%08lx\n", 576 ipsec_address(&sav->sah->saidx.dst), 577 (u_long) ntohl(sav->spi))); 578 error = ENOBUFS; 579 goto bad; 580 } 581 ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff); 582 583 /* Initialize the IPCOMP header */ 584 /* XXX alignment always correct? */ 585 switch (sav->sah->saidx.dst.sa.sa_family) { 586 #ifdef INET 587 case AF_INET: 588 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p; 589 break; 590 #endif /* INET */ 591 #ifdef INET6 592 case AF_INET6: 593 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt; 594 break; 595 #endif 596 } 597 ipcomp->comp_flags = 0; 598 599 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0) 600 cpi = sav->alg_enc; 601 else 602 cpi = ntohl(sav->spi) & 0xffff; 603 ipcomp->comp_cpi = htons(cpi); 604 605 /* Fix Next Protocol in IPv4/IPv6 header */ 606 prot = IPPROTO_IPCOMP; 607 m_copyback(m, tc->tc_protoff, sizeof(u_int8_t), (u_char *)&prot); 608 609 /* Adjust the length in the IP header */ 610 switch (sav->sah->saidx.dst.sa.sa_family) { 611 #ifdef INET 612 case AF_INET: 613 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 614 break; 615 #endif /* INET */ 616 #ifdef INET6 617 case AF_INET6: 618 mtod(m, struct ip6_hdr *)->ip6_plen = 619 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr); 620 break; 621 #endif /* INET6 */ 622 default: 623 IPCOMP_STATINC(IPCOMP_STAT_NOPF); 624 DPRINTF(("ipcomp_output: unknown/unsupported protocol " 625 "family %d, IPCA %s/%08lx\n", 626 sav->sah->saidx.dst.sa.sa_family, 627 ipsec_address(&sav->sah->saidx.dst), 628 (u_long) ntohl(sav->spi))); 629 error = EPFNOSUPPORT; 630 goto bad; 631 } 632 } else { 633 /* compression was useless, we have lost time */ 634 IPCOMP_STATINC(IPCOMP_STAT_USELESS); 635 DPRINTF(("ipcomp_output_cb: compression was useless : initial size was %d" 636 "and compressed size is %d\n", rlen, crp->crp_olen)); 637 } 638 639 640 /* Release the crypto descriptor */ 641 free(tc, M_XDATA); 642 crypto_freereq(crp); 643 644 /* NB: m is reclaimed by ipsec_process_done. */ 645 error = ipsec_process_done(m, isr); 646 KEY_FREESAV(&sav); 647 mutex_exit(softnet_lock); 648 splx(s); 649 return error; 650 bad: 651 if (sav) 652 KEY_FREESAV(&sav); 653 mutex_exit(softnet_lock); 654 splx(s); 655 if (m) 656 m_freem(m); 657 free(tc, M_XDATA); 658 crypto_freereq(crp); 659 return error; 660 } 661 662 static struct xformsw ipcomp_xformsw = { 663 XF_IPCOMP, XFT_COMP, "IPcomp", 664 ipcomp_init, ipcomp_zeroize, ipcomp_input, 665 ipcomp_output, 666 NULL, 667 }; 668 669 INITFN void 670 ipcomp_attach(void) 671 { 672 ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS); 673 xform_register(&ipcomp_xformsw); 674 } 675 676 #ifdef __FreeBSD__ 677 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL) 678 #endif /* __FreeBSD__ */ 679