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