1 /* $FreeBSD$ */ 2 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */ 3 4 /*- 5 * SPDX-License-Identifier: BSD-3-Clause 6 * 7 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org) 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* IP payload compression protocol (IPComp), see RFC 2393 */ 34 #include "opt_inet.h" 35 #include "opt_inet6.h" 36 #include "opt_ipsec.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/lock.h> 43 #include <sys/mutex.h> 44 #include <sys/socket.h> 45 #include <sys/kernel.h> 46 #include <sys/protosw.h> 47 #include <sys/sysctl.h> 48 49 #include <netinet/in.h> 50 #include <netinet/in_systm.h> 51 #include <netinet/ip.h> 52 #include <netinet/ip_var.h> 53 #include <netinet/ip_encap.h> 54 55 #include <net/netisr.h> 56 #include <net/vnet.h> 57 #include <net/if.h> /* XXXGL: net_epoch should move out there */ 58 #include <net/if_var.h> /* XXXGL: net_epoch should move out there */ 59 60 #include <netipsec/ipsec.h> 61 #include <netipsec/xform.h> 62 63 #ifdef INET6 64 #include <netinet/ip6.h> 65 #include <netinet6/ip6_var.h> 66 #include <netipsec/ipsec6.h> 67 #endif 68 69 #include <netipsec/ipcomp.h> 70 #include <netipsec/ipcomp_var.h> 71 72 #include <netipsec/key.h> 73 #include <netipsec/key_debug.h> 74 75 #include <opencrypto/cryptodev.h> 76 #include <opencrypto/deflate.h> 77 #include <opencrypto/xform.h> 78 79 VNET_DEFINE(int, ipcomp_enable) = 1; 80 VNET_PCPUSTAT_DEFINE(struct ipcompstat, ipcompstat); 81 VNET_PCPUSTAT_SYSINIT(ipcompstat); 82 83 #ifdef VIMAGE 84 VNET_PCPUSTAT_SYSUNINIT(ipcompstat); 85 #endif /* VIMAGE */ 86 87 SYSCTL_DECL(_net_inet_ipcomp); 88 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO, ipcomp_enable, 89 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipcomp_enable), 0, ""); 90 SYSCTL_VNET_PCPUSTAT(_net_inet_ipcomp, IPSECCTL_STATS, stats, 91 struct ipcompstat, ipcompstat, 92 "IPCOMP statistics (struct ipcompstat, netipsec/ipcomp_var.h"); 93 94 static MALLOC_DEFINE(M_IPCOMP, "ipcomp", "IPCOMP"); 95 96 static int ipcomp_input_cb(struct cryptop *crp); 97 static int ipcomp_output_cb(struct cryptop *crp); 98 99 /* 100 * RFC 3173 p 2.2. Non-Expansion Policy: 101 * If the total size of a compressed payload and the IPComp header, as 102 * defined in section 3, is not smaller than the size of the original 103 * payload, the IP datagram MUST be sent in the original non-compressed 104 * form. 105 * 106 * When we use IPComp in tunnel mode, for small packets we will receive 107 * encapsulated IP-IP datagrams without any compression and without IPComp 108 * header. 109 */ 110 static int 111 ipcomp_encapcheck(union sockaddr_union *src, union sockaddr_union *dst) 112 { 113 struct secasvar *sav; 114 115 sav = key_allocsa_tunnel(src, dst, IPPROTO_IPCOMP); 116 if (sav == NULL) 117 return (0); 118 key_freesav(&sav); 119 120 if (src->sa.sa_family == AF_INET) 121 return (sizeof(struct in_addr) << 4); 122 else 123 return (sizeof(struct in6_addr) << 4); 124 } 125 126 static int 127 ipcomp_nonexp_input(struct mbuf *m, int off, int proto, void *arg __unused) 128 { 129 int isr; 130 131 NET_EPOCH_ASSERT(); 132 133 switch (proto) { 134 #ifdef INET 135 case IPPROTO_IPV4: 136 isr = NETISR_IP; 137 break; 138 #endif 139 #ifdef INET6 140 case IPPROTO_IPV6: 141 isr = NETISR_IPV6; 142 break; 143 #endif 144 default: 145 IPCOMPSTAT_INC(ipcomps_nopf); 146 m_freem(m); 147 return (IPPROTO_DONE); 148 } 149 m_adj(m, off); 150 IPCOMPSTAT_ADD(ipcomps_ibytes, m->m_pkthdr.len); 151 IPCOMPSTAT_INC(ipcomps_input); 152 netisr_dispatch(isr, m); 153 return (IPPROTO_DONE); 154 } 155 156 /* 157 * ipcomp_init() is called when an CPI is being set up. 158 */ 159 static int 160 ipcomp_init(struct secasvar *sav, struct xformsw *xsp) 161 { 162 const struct comp_algo *tcomp; 163 struct crypto_session_params csp; 164 165 /* NB: algorithm really comes in alg_enc and not alg_comp! */ 166 tcomp = comp_algorithm_lookup(sav->alg_enc); 167 if (tcomp == NULL) { 168 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__, 169 sav->alg_comp)); 170 return EINVAL; 171 } 172 sav->alg_comp = sav->alg_enc; /* set for doing histogram */ 173 sav->tdb_xform = xsp; 174 sav->tdb_compalgxform = tcomp; 175 176 /* Initialize crypto session */ 177 memset(&csp, 0, sizeof(csp)); 178 csp.csp_mode = CSP_MODE_COMPRESS; 179 csp.csp_cipher_alg = sav->tdb_compalgxform->type; 180 181 return crypto_newsession(&sav->tdb_cryptoid, &csp, V_crypto_support); 182 } 183 184 /* 185 * ipcomp_cleanup() used when IPCA is deleted 186 */ 187 static void 188 ipcomp_cleanup(struct secasvar *sav) 189 { 190 191 crypto_freesession(sav->tdb_cryptoid); 192 sav->tdb_cryptoid = NULL; 193 } 194 195 /* 196 * ipcomp_input() gets called to uncompress an input packet 197 */ 198 static int 199 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff) 200 { 201 struct xform_data *xd; 202 struct cryptop *crp; 203 struct ipcomp *ipcomp; 204 crypto_session_t cryptoid; 205 caddr_t addr; 206 int error, hlen = IPCOMP_HLENGTH; 207 208 /* 209 * Check that the next header of the IPComp is not IPComp again, before 210 * doing any real work. Given it is not possible to do double 211 * compression it means someone is playing tricks on us. 212 */ 213 error = ENOBUFS; 214 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) { 215 IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/ 216 DPRINTF(("%s: m_pullup failed\n", __func__)); 217 key_freesav(&sav); 218 return (error); 219 } 220 addr = (caddr_t) mtod(m, struct ip *) + skip; 221 ipcomp = (struct ipcomp *)addr; 222 if (ipcomp->comp_nxt == IPPROTO_IPCOMP) { 223 IPCOMPSTAT_INC(ipcomps_pdrops); /* XXX have our own stats? */ 224 DPRINTF(("%s: recursive compression detected\n", __func__)); 225 error = EINVAL; 226 goto bad; 227 } 228 229 SECASVAR_LOCK(sav); 230 cryptoid = sav->tdb_cryptoid; 231 SECASVAR_UNLOCK(sav); 232 233 /* Get crypto descriptors */ 234 crp = crypto_getreq(cryptoid, M_NOWAIT); 235 if (crp == NULL) { 236 DPRINTF(("%s: no crypto descriptors\n", __func__)); 237 IPCOMPSTAT_INC(ipcomps_crypto); 238 goto bad; 239 } 240 /* Get IPsec-specific opaque pointer */ 241 xd = malloc(sizeof(*xd), M_IPCOMP, M_NOWAIT | M_ZERO); 242 if (xd == NULL) { 243 DPRINTF(("%s: cannot allocate xform_data\n", __func__)); 244 IPCOMPSTAT_INC(ipcomps_crypto); 245 crypto_freereq(crp); 246 goto bad; 247 } 248 249 /* Decompression operation */ 250 crp->crp_op = CRYPTO_OP_DECOMPRESS; 251 crp->crp_payload_start = skip + hlen; 252 crp->crp_payload_length = m->m_pkthdr.len - (skip + hlen); 253 254 /* Crypto operation descriptor */ 255 crp->crp_flags = CRYPTO_F_CBIFSYNC; 256 crypto_use_mbuf(crp, m); 257 crp->crp_callback = ipcomp_input_cb; 258 crp->crp_opaque = xd; 259 260 /* These are passed as-is to the callback */ 261 xd->sav = sav; 262 xd->protoff = protoff; 263 xd->skip = skip; 264 xd->vnet = curvnet; 265 xd->cryptoid = cryptoid; 266 267 SECASVAR_LOCK(sav); 268 crp->crp_session = xd->cryptoid = sav->tdb_cryptoid; 269 SECASVAR_UNLOCK(sav); 270 271 return crypto_dispatch(crp); 272 bad: 273 m_freem(m); 274 key_freesav(&sav); 275 return (error); 276 } 277 278 /* 279 * IPComp input callback from the crypto driver. 280 */ 281 static int 282 ipcomp_input_cb(struct cryptop *crp) 283 { 284 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]); 285 struct xform_data *xd; 286 struct mbuf *m; 287 struct secasvar *sav; 288 struct secasindex *saidx; 289 caddr_t addr; 290 crypto_session_t cryptoid; 291 int hlen = IPCOMP_HLENGTH, error, clen; 292 int skip, protoff; 293 uint8_t nproto; 294 295 m = crp->crp_buf.cb_mbuf; 296 xd = crp->crp_opaque; 297 CURVNET_SET(xd->vnet); 298 sav = xd->sav; 299 skip = xd->skip; 300 protoff = xd->protoff; 301 cryptoid = xd->cryptoid; 302 saidx = &sav->sah->saidx; 303 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET || 304 saidx->dst.sa.sa_family == AF_INET6, 305 ("unexpected protocol family %u", saidx->dst.sa.sa_family)); 306 307 /* Check for crypto errors */ 308 if (crp->crp_etype) { 309 if (crp->crp_etype == EAGAIN) { 310 /* Reset the session ID */ 311 if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0) 312 crypto_freesession(cryptoid); 313 xd->cryptoid = crp->crp_session; 314 CURVNET_RESTORE(); 315 return (crypto_dispatch(crp)); 316 } 317 IPCOMPSTAT_INC(ipcomps_noxform); 318 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 319 error = crp->crp_etype; 320 goto bad; 321 } 322 /* Shouldn't happen... */ 323 if (m == NULL) { 324 IPCOMPSTAT_INC(ipcomps_crypto); 325 DPRINTF(("%s: null mbuf returned from crypto\n", __func__)); 326 error = EINVAL; 327 goto bad; 328 } 329 IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]); 330 331 clen = crp->crp_olen; /* Length of data after processing */ 332 333 /* Release the crypto descriptors */ 334 free(xd, M_IPCOMP), xd = NULL; 335 crypto_freereq(crp), crp = NULL; 336 337 /* In case it's not done already, adjust the size of the mbuf chain */ 338 m->m_pkthdr.len = clen + hlen + skip; 339 340 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) { 341 IPCOMPSTAT_INC(ipcomps_hdrops); /*XXX*/ 342 DPRINTF(("%s: m_pullup failed\n", __func__)); 343 error = EINVAL; /*XXX*/ 344 goto bad; 345 } 346 347 /* Keep the next protocol field */ 348 addr = (caddr_t) mtod(m, struct ip *) + skip; 349 nproto = ((struct ipcomp *) addr)->comp_nxt; 350 351 /* Remove the IPCOMP header */ 352 error = m_striphdr(m, skip, hlen); 353 if (error) { 354 IPCOMPSTAT_INC(ipcomps_hdrops); 355 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__, 356 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 357 (u_long) ntohl(sav->spi))); 358 goto bad; 359 } 360 361 /* Restore the Next Protocol field */ 362 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto); 363 364 switch (saidx->dst.sa.sa_family) { 365 #ifdef INET6 366 case AF_INET6: 367 error = ipsec6_common_input_cb(m, sav, skip, protoff); 368 break; 369 #endif 370 #ifdef INET 371 case AF_INET: 372 error = ipsec4_common_input_cb(m, sav, skip, protoff); 373 break; 374 #endif 375 default: 376 panic("%s: Unexpected address family: %d saidx=%p", __func__, 377 saidx->dst.sa.sa_family, saidx); 378 } 379 CURVNET_RESTORE(); 380 return error; 381 bad: 382 CURVNET_RESTORE(); 383 if (sav != NULL) 384 key_freesav(&sav); 385 if (m != NULL) 386 m_freem(m); 387 if (xd != NULL) 388 free(xd, M_IPCOMP); 389 if (crp != NULL) 390 crypto_freereq(crp); 391 return error; 392 } 393 394 /* 395 * IPComp output routine, called by ipsec[46]_perform_request() 396 */ 397 static int 398 ipcomp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav, 399 u_int idx, int skip, int protoff) 400 { 401 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]); 402 const struct comp_algo *ipcompx; 403 struct cryptop *crp; 404 struct xform_data *xd; 405 crypto_session_t cryptoid; 406 int error, ralen, maxpacketsize; 407 408 IPSEC_ASSERT(sav != NULL, ("null SA")); 409 ipcompx = sav->tdb_compalgxform; 410 IPSEC_ASSERT(ipcompx != NULL, ("null compression xform")); 411 412 /* 413 * Do not touch the packet in case our payload to compress 414 * is lower than the minimal threshold of the compression 415 * alogrithm. We will just send out the data uncompressed. 416 * See RFC 3173, 2.2. Non-Expansion Policy. 417 */ 418 if (m->m_pkthdr.len <= ipcompx->minlen) { 419 IPCOMPSTAT_INC(ipcomps_threshold); 420 return ipsec_process_done(m, sp, sav, idx); 421 } 422 423 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */ 424 IPCOMPSTAT_INC(ipcomps_output); 425 426 /* Check for maximum packet size violations. */ 427 switch (sav->sah->saidx.dst.sa.sa_family) { 428 #ifdef INET 429 case AF_INET: 430 maxpacketsize = IP_MAXPACKET; 431 break; 432 #endif /* INET */ 433 #ifdef INET6 434 case AF_INET6: 435 maxpacketsize = IPV6_MAXPACKET; 436 break; 437 #endif /* INET6 */ 438 default: 439 IPCOMPSTAT_INC(ipcomps_nopf); 440 DPRINTF(("%s: unknown/unsupported protocol family %d, " 441 "IPCA %s/%08lx\n", __func__, 442 sav->sah->saidx.dst.sa.sa_family, 443 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 444 (u_long) ntohl(sav->spi))); 445 error = EPFNOSUPPORT; 446 goto bad; 447 } 448 if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) { 449 IPCOMPSTAT_INC(ipcomps_toobig); 450 DPRINTF(("%s: packet in IPCA %s/%08lx got too big " 451 "(len %u, max len %u)\n", __func__, 452 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 453 (u_long) ntohl(sav->spi), 454 ralen + skip + IPCOMP_HLENGTH, maxpacketsize)); 455 error = EMSGSIZE; 456 goto bad; 457 } 458 459 /* Update the counters */ 460 IPCOMPSTAT_ADD(ipcomps_obytes, m->m_pkthdr.len - skip); 461 462 m = m_unshare(m, M_NOWAIT); 463 if (m == NULL) { 464 IPCOMPSTAT_INC(ipcomps_hdrops); 465 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n", 466 __func__, ipsec_address(&sav->sah->saidx.dst, buf, 467 sizeof(buf)), (u_long) ntohl(sav->spi))); 468 error = ENOBUFS; 469 goto bad; 470 } 471 472 /* Ok now, we can pass to the crypto processing. */ 473 SECASVAR_LOCK(sav); 474 cryptoid = sav->tdb_cryptoid; 475 SECASVAR_UNLOCK(sav); 476 477 /* Get crypto descriptors */ 478 crp = crypto_getreq(cryptoid, M_NOWAIT); 479 if (crp == NULL) { 480 IPCOMPSTAT_INC(ipcomps_crypto); 481 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__)); 482 error = ENOBUFS; 483 goto bad; 484 } 485 486 /* Compression descriptor */ 487 crp->crp_op = CRYPTO_OP_COMPRESS; 488 crp->crp_payload_start = skip; 489 crp->crp_payload_length = ralen; 490 491 /* IPsec-specific opaque crypto info */ 492 xd = malloc(sizeof(struct xform_data), M_IPCOMP, M_NOWAIT | M_ZERO); 493 if (xd == NULL) { 494 IPCOMPSTAT_INC(ipcomps_crypto); 495 DPRINTF(("%s: failed to allocate xform_data\n", __func__)); 496 crypto_freereq(crp); 497 error = ENOBUFS; 498 goto bad; 499 } 500 501 xd->sp = sp; 502 xd->sav = sav; 503 xd->idx = idx; 504 xd->skip = skip; 505 xd->protoff = protoff; 506 xd->vnet = curvnet; 507 xd->cryptoid = cryptoid; 508 509 /* Crypto operation descriptor */ 510 crp->crp_flags = CRYPTO_F_CBIFSYNC; 511 crypto_use_mbuf(crp, m); 512 crp->crp_callback = ipcomp_output_cb; 513 crp->crp_opaque = xd; 514 515 return crypto_dispatch(crp); 516 bad: 517 if (m) 518 m_freem(m); 519 key_freesav(&sav); 520 key_freesp(&sp); 521 return (error); 522 } 523 524 /* 525 * IPComp output callback from the crypto driver. 526 */ 527 static int 528 ipcomp_output_cb(struct cryptop *crp) 529 { 530 IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]); 531 struct xform_data *xd; 532 struct secpolicy *sp; 533 struct secasvar *sav; 534 struct mbuf *m; 535 crypto_session_t cryptoid; 536 u_int idx; 537 int error, skip, protoff; 538 539 m = crp->crp_buf.cb_mbuf; 540 xd = crp->crp_opaque; 541 CURVNET_SET(xd->vnet); 542 idx = xd->idx; 543 sp = xd->sp; 544 sav = xd->sav; 545 skip = xd->skip; 546 protoff = xd->protoff; 547 cryptoid = xd->cryptoid; 548 549 /* Check for crypto errors */ 550 if (crp->crp_etype) { 551 if (crp->crp_etype == EAGAIN) { 552 /* Reset the session ID */ 553 if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0) 554 crypto_freesession(cryptoid); 555 xd->cryptoid = crp->crp_session; 556 CURVNET_RESTORE(); 557 return (crypto_dispatch(crp)); 558 } 559 IPCOMPSTAT_INC(ipcomps_noxform); 560 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 561 error = crp->crp_etype; 562 goto bad; 563 } 564 /* Shouldn't happen... */ 565 if (m == NULL) { 566 IPCOMPSTAT_INC(ipcomps_crypto); 567 DPRINTF(("%s: bogus return buffer from crypto\n", __func__)); 568 error = EINVAL; 569 goto bad; 570 } 571 IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]); 572 573 if (crp->crp_payload_length > crp->crp_olen) { 574 struct mbuf *mo; 575 struct ipcomp *ipcomp; 576 int roff; 577 uint8_t prot; 578 579 /* Compression helped, inject IPCOMP header. */ 580 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff); 581 if (mo == NULL) { 582 IPCOMPSTAT_INC(ipcomps_wrap); 583 DPRINTF(("%s: IPCOMP header inject failed " 584 "for IPCA %s/%08lx\n", 585 __func__, ipsec_address(&sav->sah->saidx.dst, buf, 586 sizeof(buf)), (u_long) ntohl(sav->spi))); 587 error = ENOBUFS; 588 goto bad; 589 } 590 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff); 591 592 /* Initialize the IPCOMP header */ 593 /* XXX alignment always correct? */ 594 switch (sav->sah->saidx.dst.sa.sa_family) { 595 #ifdef INET 596 case AF_INET: 597 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p; 598 break; 599 #endif /* INET */ 600 #ifdef INET6 601 case AF_INET6: 602 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt; 603 break; 604 #endif 605 } 606 ipcomp->comp_flags = 0; 607 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi)); 608 609 /* Fix Next Protocol in IPv4/IPv6 header */ 610 prot = IPPROTO_IPCOMP; 611 m_copyback(m, protoff, sizeof(u_int8_t), 612 (u_char *)&prot); 613 614 /* Adjust the length in the IP header */ 615 switch (sav->sah->saidx.dst.sa.sa_family) { 616 #ifdef INET 617 case AF_INET: 618 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 619 break; 620 #endif /* INET */ 621 #ifdef INET6 622 case AF_INET6: 623 mtod(m, struct ip6_hdr *)->ip6_plen = 624 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr); 625 break; 626 #endif /* INET6 */ 627 default: 628 IPCOMPSTAT_INC(ipcomps_nopf); 629 DPRINTF(("%s: unknown/unsupported protocol " 630 "family %d, IPCA %s/%08lx\n", __func__, 631 sav->sah->saidx.dst.sa.sa_family, 632 ipsec_address(&sav->sah->saidx.dst, buf, 633 sizeof(buf)), (u_long) ntohl(sav->spi))); 634 error = EPFNOSUPPORT; 635 goto bad; 636 } 637 } else { 638 /* Compression was useless, we have lost time. */ 639 IPCOMPSTAT_INC(ipcomps_uncompr); 640 DPRINTF(("%s: compressions was useless %d <= %d\n", 641 __func__, crp->crp_payload_length, crp->crp_olen)); 642 /* XXX remember state to not compress the next couple 643 * of packets, RFC 3173, 2.2. Non-Expansion Policy */ 644 } 645 646 /* Release the crypto descriptor */ 647 free(xd, M_IPCOMP); 648 crypto_freereq(crp); 649 650 /* NB: m is reclaimed by ipsec_process_done. */ 651 error = ipsec_process_done(m, sp, sav, idx); 652 CURVNET_RESTORE(); 653 return (error); 654 bad: 655 if (m) 656 m_freem(m); 657 CURVNET_RESTORE(); 658 free(xd, M_IPCOMP); 659 crypto_freereq(crp); 660 key_freesav(&sav); 661 key_freesp(&sp); 662 return (error); 663 } 664 665 #ifdef INET 666 static int 667 ipcomp4_nonexp_encapcheck(const struct mbuf *m, int off, int proto, 668 void *arg __unused) 669 { 670 union sockaddr_union src, dst; 671 const struct ip *ip; 672 673 if (V_ipcomp_enable == 0) 674 return (0); 675 if (proto != IPPROTO_IPV4 && proto != IPPROTO_IPV6) 676 return (0); 677 bzero(&src, sizeof(src)); 678 bzero(&dst, sizeof(dst)); 679 src.sa.sa_family = dst.sa.sa_family = AF_INET; 680 src.sin.sin_len = dst.sin.sin_len = sizeof(struct sockaddr_in); 681 ip = mtod(m, const struct ip *); 682 src.sin.sin_addr = ip->ip_src; 683 dst.sin.sin_addr = ip->ip_dst; 684 return (ipcomp_encapcheck(&src, &dst)); 685 } 686 687 static const struct encaptab *ipe4_cookie = NULL; 688 static const struct encap_config ipv4_encap_cfg = { 689 .proto = -1, 690 .min_length = sizeof(struct ip), 691 .exact_match = sizeof(in_addr_t) << 4, 692 .check = ipcomp4_nonexp_encapcheck, 693 .input = ipcomp_nonexp_input 694 }; 695 #endif 696 #ifdef INET6 697 static int 698 ipcomp6_nonexp_encapcheck(const struct mbuf *m, int off, int proto, 699 void *arg __unused) 700 { 701 union sockaddr_union src, dst; 702 const struct ip6_hdr *ip6; 703 704 if (V_ipcomp_enable == 0) 705 return (0); 706 if (proto != IPPROTO_IPV4 && proto != IPPROTO_IPV6) 707 return (0); 708 bzero(&src, sizeof(src)); 709 bzero(&dst, sizeof(dst)); 710 src.sa.sa_family = dst.sa.sa_family = AF_INET; 711 src.sin6.sin6_len = dst.sin6.sin6_len = sizeof(struct sockaddr_in6); 712 ip6 = mtod(m, const struct ip6_hdr *); 713 src.sin6.sin6_addr = ip6->ip6_src; 714 dst.sin6.sin6_addr = ip6->ip6_dst; 715 if (IN6_IS_SCOPE_LINKLOCAL(&src.sin6.sin6_addr)) { 716 /* XXX: sa6_recoverscope() */ 717 src.sin6.sin6_scope_id = 718 ntohs(src.sin6.sin6_addr.s6_addr16[1]); 719 src.sin6.sin6_addr.s6_addr16[1] = 0; 720 } 721 if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6.sin6_addr)) { 722 /* XXX: sa6_recoverscope() */ 723 dst.sin6.sin6_scope_id = 724 ntohs(dst.sin6.sin6_addr.s6_addr16[1]); 725 dst.sin6.sin6_addr.s6_addr16[1] = 0; 726 } 727 return (ipcomp_encapcheck(&src, &dst)); 728 } 729 730 static const struct encaptab *ipe6_cookie = NULL; 731 static const struct encap_config ipv6_encap_cfg = { 732 .proto = -1, 733 .min_length = sizeof(struct ip6_hdr), 734 .exact_match = sizeof(struct in6_addr) << 4, 735 .check = ipcomp6_nonexp_encapcheck, 736 .input = ipcomp_nonexp_input 737 }; 738 #endif 739 740 static struct xformsw ipcomp_xformsw = { 741 .xf_type = XF_IPCOMP, 742 .xf_name = "IPcomp", 743 .xf_init = ipcomp_init, 744 .xf_cleanup = ipcomp_cleanup, 745 .xf_input = ipcomp_input, 746 .xf_output = ipcomp_output, 747 }; 748 749 static void 750 ipcomp_attach(void) 751 { 752 753 #ifdef INET 754 ipe4_cookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK); 755 #endif 756 #ifdef INET6 757 ipe6_cookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK); 758 #endif 759 xform_attach(&ipcomp_xformsw); 760 } 761 762 static void 763 ipcomp_detach(void) 764 { 765 766 #ifdef INET 767 ip_encap_detach(ipe4_cookie); 768 #endif 769 #ifdef INET6 770 ip6_encap_detach(ipe6_cookie); 771 #endif 772 xform_detach(&ipcomp_xformsw); 773 } 774 775 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 776 ipcomp_attach, NULL); 777 SYSUNINIT(ipcomp_xform_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, 778 ipcomp_detach, NULL); 779