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