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