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