1 1.76 rin /* $NetBSD: xform_ipcomp.c,v 1.76 2024/07/05 04:31:54 rin Exp $ */ 2 1.62 maxv /* $FreeBSD: xform_ipcomp.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $ */ 3 1.1 jonathan /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */ 4 1.1 jonathan 5 1.1 jonathan /* 6 1.1 jonathan * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj (at) wabbitt.org) 7 1.1 jonathan * 8 1.1 jonathan * Redistribution and use in source and binary forms, with or without 9 1.1 jonathan * modification, are permitted provided that the following conditions 10 1.1 jonathan * are met: 11 1.1 jonathan * 12 1.1 jonathan * 1. Redistributions of source code must retain the above copyright 13 1.1 jonathan * notice, this list of conditions and the following disclaimer. 14 1.1 jonathan * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 jonathan * notice, this list of conditions and the following disclaimer in the 16 1.1 jonathan * documentation and/or other materials provided with the distribution. 17 1.1 jonathan * 3. The name of the author may not be used to endorse or promote products 18 1.1 jonathan * derived from this software without specific prior written permission. 19 1.1 jonathan * 20 1.1 jonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 1.1 jonathan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 1.1 jonathan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 1.1 jonathan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 1.1 jonathan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 1.1 jonathan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 1.1 jonathan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 1.1 jonathan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 1.1 jonathan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 1.1 jonathan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 1.1 jonathan */ 31 1.1 jonathan 32 1.1 jonathan #include <sys/cdefs.h> 33 1.76 rin __KERNEL_RCSID(0, "$NetBSD: xform_ipcomp.c,v 1.76 2024/07/05 04:31:54 rin Exp $"); 34 1.1 jonathan 35 1.1 jonathan /* IP payload compression protocol (IPComp), see RFC 2393 */ 36 1.32 ozaki #if defined(_KERNEL_OPT) 37 1.1 jonathan #include "opt_inet.h" 38 1.32 ozaki #endif 39 1.1 jonathan 40 1.1 jonathan #include <sys/param.h> 41 1.1 jonathan #include <sys/systm.h> 42 1.1 jonathan #include <sys/mbuf.h> 43 1.1 jonathan #include <sys/socket.h> 44 1.1 jonathan #include <sys/kernel.h> 45 1.1 jonathan #include <sys/protosw.h> 46 1.1 jonathan #include <sys/sysctl.h> 47 1.47 ozaki #include <sys/pool.h> 48 1.49 ozaki #include <sys/pserialize.h> 49 1.1 jonathan 50 1.1 jonathan #include <netinet/in.h> 51 1.1 jonathan #include <netinet/in_systm.h> 52 1.1 jonathan #include <netinet/ip.h> 53 1.1 jonathan #include <netinet/ip_var.h> 54 1.1 jonathan 55 1.1 jonathan #include <net/route.h> 56 1.1 jonathan #include <netipsec/ipsec.h> 57 1.18 thorpej #include <netipsec/ipsec_private.h> 58 1.1 jonathan #include <netipsec/xform.h> 59 1.1 jonathan 60 1.1 jonathan #ifdef INET6 61 1.1 jonathan #include <netinet/ip6.h> 62 1.1 jonathan #include <netipsec/ipsec6.h> 63 1.1 jonathan #endif 64 1.1 jonathan 65 1.1 jonathan #include <netipsec/ipcomp.h> 66 1.1 jonathan #include <netipsec/ipcomp_var.h> 67 1.1 jonathan 68 1.4 tls #include <netipsec/key.h> 69 1.4 tls #include <netipsec/key_debug.h> 70 1.1 jonathan 71 1.1 jonathan #include <opencrypto/cryptodev.h> 72 1.1 jonathan #include <opencrypto/deflate.h> 73 1.1 jonathan 74 1.18 thorpej percpu_t *ipcompstat_percpu; 75 1.18 thorpej 76 1.57 maxv int ipcomp_enable = 1; 77 1.1 jonathan 78 1.70 riastrad static void ipcomp_input_cb(struct cryptop *crp); 79 1.70 riastrad static void ipcomp_output_cb(struct cryptop *crp); 80 1.1 jonathan 81 1.33 christos const uint8_t ipcomp_stats[256] = { SADB_CALG_STATS_INIT }; 82 1.33 christos 83 1.52 ozaki static pool_cache_t ipcomp_tdb_crypto_pool_cache; 84 1.47 ozaki 85 1.24 drochner const struct comp_algo * 86 1.1 jonathan ipcomp_algorithm_lookup(int alg) 87 1.1 jonathan { 88 1.1 jonathan switch (alg) { 89 1.1 jonathan case SADB_X_CALG_DEFLATE: 90 1.25 drochner return &comp_algo_deflate_nogrow; 91 1.1 jonathan } 92 1.1 jonathan return NULL; 93 1.1 jonathan } 94 1.1 jonathan 95 1.1 jonathan /* 96 1.1 jonathan * ipcomp_init() is called when an CPI is being set up. 97 1.1 jonathan */ 98 1.1 jonathan static int 99 1.24 drochner ipcomp_init(struct secasvar *sav, const struct xformsw *xsp) 100 1.1 jonathan { 101 1.24 drochner const struct comp_algo *tcomp; 102 1.1 jonathan struct cryptoini cric; 103 1.17 tls int ses; 104 1.1 jonathan 105 1.1 jonathan /* NB: algorithm really comes in alg_enc and not alg_comp! */ 106 1.1 jonathan tcomp = ipcomp_algorithm_lookup(sav->alg_enc); 107 1.1 jonathan if (tcomp == NULL) { 108 1.68 christos DPRINTF("unsupported compression algorithm %d\n", 109 1.68 christos sav->alg_comp); 110 1.1 jonathan return EINVAL; 111 1.1 jonathan } 112 1.1 jonathan sav->alg_comp = sav->alg_enc; /* set for doing histogram */ 113 1.1 jonathan sav->tdb_xform = xsp; 114 1.1 jonathan sav->tdb_compalgxform = tcomp; 115 1.1 jonathan 116 1.1 jonathan /* Initialize crypto session */ 117 1.34 christos memset(&cric, 0, sizeof(cric)); 118 1.1 jonathan cric.cri_alg = sav->tdb_compalgxform->type; 119 1.1 jonathan 120 1.17 tls ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support); 121 1.17 tls return ses; 122 1.1 jonathan } 123 1.1 jonathan 124 1.1 jonathan /* 125 1.1 jonathan * ipcomp_zeroize() used when IPCA is deleted 126 1.1 jonathan */ 127 1.71 riastrad static void 128 1.1 jonathan ipcomp_zeroize(struct secasvar *sav) 129 1.1 jonathan { 130 1.1 jonathan 131 1.72 riastrad crypto_freesession(sav->tdb_cryptoid); 132 1.1 jonathan sav->tdb_cryptoid = 0; 133 1.1 jonathan } 134 1.1 jonathan 135 1.1 jonathan /* 136 1.1 jonathan * ipcomp_input() gets called to uncompress an input packet 137 1.1 jonathan */ 138 1.1 jonathan static int 139 1.42 ozaki ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff) 140 1.1 jonathan { 141 1.1 jonathan struct tdb_crypto *tc; 142 1.1 jonathan struct cryptodesc *crdc; 143 1.1 jonathan struct cryptop *crp; 144 1.55 ozaki int error, hlen = IPCOMP_HLENGTH, stat = IPCOMP_STAT_CRYPTO; 145 1.1 jonathan 146 1.61 maxv KASSERT(skip + hlen <= m->m_pkthdr.len); 147 1.1 jonathan 148 1.1 jonathan /* Get crypto descriptors */ 149 1.1 jonathan crp = crypto_getreq(1); 150 1.1 jonathan if (crp == NULL) { 151 1.68 christos DPRINTF("no crypto descriptors\n"); 152 1.55 ozaki error = ENOBUFS; 153 1.55 ozaki goto error_m; 154 1.1 jonathan } 155 1.1 jonathan /* Get IPsec-specific opaque pointer */ 156 1.52 ozaki tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT); 157 1.1 jonathan if (tc == NULL) { 158 1.68 christos DPRINTF("cannot allocate tdb_crypto\n"); 159 1.55 ozaki error = ENOBUFS; 160 1.55 ozaki goto error_crp; 161 1.1 jonathan } 162 1.29 drochner 163 1.29 drochner error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT); 164 1.29 drochner if (error) { 165 1.68 christos DPRINTF("m_makewritable failed\n"); 166 1.55 ozaki goto error_tc; 167 1.29 drochner } 168 1.29 drochner 169 1.51 ozaki { 170 1.51 ozaki int s = pserialize_read_enter(); 171 1.51 ozaki 172 1.51 ozaki /* 173 1.51 ozaki * Take another reference to the SA for opencrypto callback. 174 1.51 ozaki */ 175 1.51 ozaki if (__predict_false(sav->state == SADB_SASTATE_DEAD)) { 176 1.51 ozaki pserialize_read_exit(s); 177 1.55 ozaki stat = IPCOMP_STAT_NOTDB; 178 1.55 ozaki error = ENOENT; 179 1.55 ozaki goto error_tc; 180 1.51 ozaki } 181 1.51 ozaki KEY_SA_REF(sav); 182 1.51 ozaki pserialize_read_exit(s); 183 1.51 ozaki } 184 1.51 ozaki 185 1.1 jonathan crdc = crp->crp_desc; 186 1.1 jonathan 187 1.1 jonathan crdc->crd_skip = skip + hlen; 188 1.1 jonathan crdc->crd_len = m->m_pkthdr.len - (skip + hlen); 189 1.29 drochner crdc->crd_inject = 0; /* unused */ 190 1.1 jonathan 191 1.1 jonathan /* Decompression operation */ 192 1.1 jonathan crdc->crd_alg = sav->tdb_compalgxform->type; 193 1.1 jonathan 194 1.1 jonathan /* Crypto operation descriptor */ 195 1.1 jonathan crp->crp_ilen = m->m_pkthdr.len - (skip + hlen); 196 1.25 drochner crp->crp_olen = MCLBYTES; /* hint to decompression code */ 197 1.1 jonathan crp->crp_flags = CRYPTO_F_IMBUF; 198 1.13 degroote crp->crp_buf = m; 199 1.1 jonathan crp->crp_callback = ipcomp_input_cb; 200 1.1 jonathan crp->crp_sid = sav->tdb_cryptoid; 201 1.13 degroote crp->crp_opaque = tc; 202 1.1 jonathan 203 1.1 jonathan /* These are passed as-is to the callback */ 204 1.1 jonathan tc->tc_spi = sav->spi; 205 1.1 jonathan tc->tc_dst = sav->sah->saidx.dst; 206 1.1 jonathan tc->tc_proto = sav->sah->saidx.proto; 207 1.1 jonathan tc->tc_protoff = protoff; 208 1.1 jonathan tc->tc_skip = skip; 209 1.42 ozaki tc->tc_sav = sav; 210 1.1 jonathan 211 1.74 riastrad crypto_dispatch(crp); 212 1.74 riastrad return 0; 213 1.55 ozaki 214 1.55 ozaki error_tc: 215 1.55 ozaki pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc); 216 1.55 ozaki error_crp: 217 1.55 ozaki crypto_freereq(crp); 218 1.55 ozaki error_m: 219 1.55 ozaki m_freem(m); 220 1.55 ozaki IPCOMP_STATINC(stat); 221 1.55 ozaki return error; 222 1.1 jonathan } 223 1.1 jonathan 224 1.1 jonathan #ifdef INET6 225 1.40 ozaki #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do { \ 226 1.1 jonathan if (saidx->dst.sa.sa_family == AF_INET6) { \ 227 1.70 riastrad (void)ipsec6_common_input_cb(m, sav, skip, protoff); \ 228 1.1 jonathan } else { \ 229 1.70 riastrad (void)ipsec4_common_input_cb(m, sav, skip, protoff); \ 230 1.1 jonathan } \ 231 1.1 jonathan } while (0) 232 1.1 jonathan #else 233 1.40 ozaki #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) \ 234 1.70 riastrad ((void)ipsec4_common_input_cb(m, sav, skip, protoff)) 235 1.1 jonathan #endif 236 1.1 jonathan 237 1.1 jonathan /* 238 1.1 jonathan * IPComp input callback from the crypto driver. 239 1.1 jonathan */ 240 1.70 riastrad static void 241 1.1 jonathan ipcomp_input_cb(struct cryptop *crp) 242 1.1 jonathan { 243 1.38 ryo char buf[IPSEC_ADDRSTRLEN]; 244 1.1 jonathan struct tdb_crypto *tc; 245 1.1 jonathan int skip, protoff; 246 1.1 jonathan struct mbuf *m; 247 1.1 jonathan struct secasvar *sav; 248 1.31 mrg struct secasindex *saidx __diagused; 249 1.70 riastrad int hlen = IPCOMP_HLENGTH, clen; 250 1.34 christos uint8_t nproto; 251 1.57 maxv struct ipcomp *ipc; 252 1.48 ozaki IPSEC_DECLARE_LOCK_VARIABLE; 253 1.1 jonathan 254 1.36 ozaki KASSERT(crp->crp_opaque != NULL); 255 1.34 christos tc = crp->crp_opaque; 256 1.1 jonathan skip = tc->tc_skip; 257 1.1 jonathan protoff = tc->tc_protoff; 258 1.34 christos m = crp->crp_buf; 259 1.1 jonathan 260 1.48 ozaki IPSEC_ACQUIRE_GLOBAL_LOCKS(); 261 1.1 jonathan 262 1.42 ozaki sav = tc->tc_sav; 263 1.1 jonathan saidx = &sav->sah->saidx; 264 1.36 ozaki KASSERTMSG(saidx->dst.sa.sa_family == AF_INET || 265 1.36 ozaki saidx->dst.sa.sa_family == AF_INET6, 266 1.36 ozaki "unexpected protocol family %u", saidx->dst.sa.sa_family); 267 1.1 jonathan 268 1.1 jonathan /* Check for crypto errors */ 269 1.1 jonathan if (crp->crp_etype) { 270 1.1 jonathan /* Reset the session ID */ 271 1.1 jonathan if (sav->tdb_cryptoid != 0) 272 1.1 jonathan sav->tdb_cryptoid = crp->crp_sid; 273 1.1 jonathan 274 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_NOXFORM); 275 1.68 christos DPRINTF("crypto error %d\n", crp->crp_etype); 276 1.1 jonathan goto bad; 277 1.1 jonathan } 278 1.45 ozaki 279 1.33 christos IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]); 280 1.1 jonathan 281 1.20 degroote /* Update the counters */ 282 1.20 degroote IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen); 283 1.20 degroote 284 1.57 maxv /* Length of data after processing */ 285 1.57 maxv clen = crp->crp_olen; 286 1.1 jonathan 287 1.1 jonathan /* Release the crypto descriptors */ 288 1.52 ozaki pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc); 289 1.47 ozaki tc = NULL; 290 1.57 maxv crypto_freereq(crp); 291 1.57 maxv crp = NULL; 292 1.1 jonathan 293 1.1 jonathan /* In case it's not done already, adjust the size of the mbuf chain */ 294 1.1 jonathan m->m_pkthdr.len = clen + hlen + skip; 295 1.1 jonathan 296 1.61 maxv /* 297 1.61 maxv * Get the next protocol field. 298 1.61 maxv * 299 1.61 maxv * XXX: Really, we should use m_copydata instead of m_pullup. 300 1.61 maxv */ 301 1.1 jonathan if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) { 302 1.57 maxv IPCOMP_STATINC(IPCOMP_STAT_HDROPS); 303 1.68 christos DPRINTF("m_pullup failed\n"); 304 1.1 jonathan goto bad; 305 1.1 jonathan } 306 1.57 maxv ipc = (struct ipcomp *)(mtod(m, uint8_t *) + skip); 307 1.57 maxv nproto = ipc->comp_nxt; 308 1.61 maxv 309 1.34 christos switch (nproto) { 310 1.34 christos case IPPROTO_IPCOMP: 311 1.34 christos case IPPROTO_AH: 312 1.34 christos case IPPROTO_ESP: 313 1.26 spz IPCOMP_STATINC(IPCOMP_STAT_HDROPS); 314 1.68 christos DPRINTF("nested ipcomp, IPCA %s/%08lx\n", 315 1.38 ryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 316 1.68 christos (u_long) ntohl(sav->spi)); 317 1.26 spz goto bad; 318 1.34 christos default: 319 1.34 christos break; 320 1.26 spz } 321 1.1 jonathan 322 1.1 jonathan /* Remove the IPCOMP header */ 323 1.70 riastrad if (m_striphdr(m, skip, hlen) != 0) { 324 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_HDROPS); 325 1.68 christos DPRINTF("bad mbuf chain, IPCA %s/%08lx\n", 326 1.38 ryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 327 1.68 christos (u_long) ntohl(sav->spi)); 328 1.1 jonathan goto bad; 329 1.1 jonathan } 330 1.1 jonathan 331 1.1 jonathan /* Restore the Next Protocol field */ 332 1.61 maxv m_copyback(m, protoff, sizeof(nproto), &nproto); 333 1.1 jonathan 334 1.40 ozaki IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff); 335 1.1 jonathan 336 1.50 ozaki KEY_SA_UNREF(&sav); 337 1.48 ozaki IPSEC_RELEASE_GLOBAL_LOCKS(); 338 1.70 riastrad return; 339 1.57 maxv 340 1.1 jonathan bad: 341 1.1 jonathan if (sav) 342 1.50 ozaki KEY_SA_UNREF(&sav); 343 1.48 ozaki IPSEC_RELEASE_GLOBAL_LOCKS(); 344 1.76 rin m_freem(m); 345 1.1 jonathan if (tc != NULL) 346 1.52 ozaki pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc); 347 1.1 jonathan if (crp) 348 1.1 jonathan crypto_freereq(crp); 349 1.1 jonathan } 350 1.1 jonathan 351 1.1 jonathan /* 352 1.1 jonathan * IPComp output routine, called by ipsec[46]_process_packet() 353 1.1 jonathan */ 354 1.1 jonathan static int 355 1.57 maxv ipcomp_output(struct mbuf *m, const struct ipsecrequest *isr, 356 1.69 knakahar struct secasvar *sav, int skip, int protoff, int flags) 357 1.1 jonathan { 358 1.38 ryo char buf[IPSEC_ADDRSTRLEN]; 359 1.24 drochner const struct comp_algo *ipcompx; 360 1.9 degroote int error, ralen, hlen, maxpacketsize; 361 1.1 jonathan struct cryptodesc *crdc; 362 1.1 jonathan struct cryptop *crp; 363 1.1 jonathan struct tdb_crypto *tc; 364 1.1 jonathan 365 1.43 ozaki KASSERT(sav != NULL); 366 1.36 ozaki KASSERT(sav->tdb_compalgxform != NULL); 367 1.1 jonathan ipcompx = sav->tdb_compalgxform; 368 1.1 jonathan 369 1.57 maxv /* Raw payload length before comp. */ 370 1.57 maxv ralen = m->m_pkthdr.len - skip; 371 1.57 maxv 372 1.57 maxv /* Don't process the packet if it is too short */ 373 1.9 degroote if (ralen < ipcompx->minlen) { 374 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_MINLEN); 375 1.69 knakahar return ipsec_process_done(m, isr, sav, 0); 376 1.9 degroote } 377 1.9 degroote 378 1.1 jonathan hlen = IPCOMP_HLENGTH; 379 1.1 jonathan 380 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_OUTPUT); 381 1.1 jonathan 382 1.1 jonathan /* Check for maximum packet size violations. */ 383 1.1 jonathan switch (sav->sah->saidx.dst.sa.sa_family) { 384 1.1 jonathan #ifdef INET 385 1.1 jonathan case AF_INET: 386 1.57 maxv maxpacketsize = IP_MAXPACKET; 387 1.1 jonathan break; 388 1.57 maxv #endif 389 1.1 jonathan #ifdef INET6 390 1.1 jonathan case AF_INET6: 391 1.57 maxv maxpacketsize = IPV6_MAXPACKET; 392 1.1 jonathan break; 393 1.57 maxv #endif 394 1.1 jonathan default: 395 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_NOPF); 396 1.68 christos DPRINTF("unknown/unsupported protocol family %d" 397 1.68 christos ", IPCA %s/%08lx\n", 398 1.1 jonathan sav->sah->saidx.dst.sa.sa_family, 399 1.38 ryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 400 1.68 christos (u_long) ntohl(sav->spi)); 401 1.1 jonathan error = EPFNOSUPPORT; 402 1.1 jonathan goto bad; 403 1.1 jonathan } 404 1.1 jonathan if (skip + hlen + ralen > maxpacketsize) { 405 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_TOOBIG); 406 1.68 christos DPRINTF("packet in IPCA %s/%08lx got too big " 407 1.68 christos "(len %u, max len %u)\n", 408 1.38 ryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 409 1.1 jonathan (u_long) ntohl(sav->spi), 410 1.68 christos skip + hlen + ralen, maxpacketsize); 411 1.1 jonathan error = EMSGSIZE; 412 1.1 jonathan goto bad; 413 1.1 jonathan } 414 1.1 jonathan 415 1.1 jonathan /* Update the counters */ 416 1.18 thorpej IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip); 417 1.1 jonathan 418 1.1 jonathan m = m_clone(m); 419 1.1 jonathan if (m == NULL) { 420 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_HDROPS); 421 1.68 christos DPRINTF("cannot clone mbuf chain, IPCA %s/%08lx\n", 422 1.38 ryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)), 423 1.68 christos (u_long) ntohl(sav->spi)); 424 1.1 jonathan error = ENOBUFS; 425 1.1 jonathan goto bad; 426 1.1 jonathan } 427 1.1 jonathan 428 1.1 jonathan /* Ok now, we can pass to the crypto processing */ 429 1.1 jonathan 430 1.1 jonathan /* Get crypto descriptors */ 431 1.1 jonathan crp = crypto_getreq(1); 432 1.1 jonathan if (crp == NULL) { 433 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); 434 1.68 christos DPRINTF("failed to acquire crypto descriptor\n"); 435 1.1 jonathan error = ENOBUFS; 436 1.1 jonathan goto bad; 437 1.1 jonathan } 438 1.1 jonathan crdc = crp->crp_desc; 439 1.1 jonathan 440 1.1 jonathan /* Compression descriptor */ 441 1.9 degroote crdc->crd_skip = skip; 442 1.9 degroote crdc->crd_len = m->m_pkthdr.len - skip; 443 1.1 jonathan crdc->crd_flags = CRD_F_COMP; 444 1.9 degroote crdc->crd_inject = skip; 445 1.1 jonathan 446 1.1 jonathan /* Compression operation */ 447 1.1 jonathan crdc->crd_alg = ipcompx->type; 448 1.1 jonathan 449 1.1 jonathan /* IPsec-specific opaque crypto info */ 450 1.52 ozaki tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT); 451 1.1 jonathan if (tc == NULL) { 452 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); 453 1.68 christos DPRINTF("failed to allocate tdb_crypto\n"); 454 1.1 jonathan crypto_freereq(crp); 455 1.1 jonathan error = ENOBUFS; 456 1.1 jonathan goto bad; 457 1.1 jonathan } 458 1.1 jonathan 459 1.49 ozaki { 460 1.49 ozaki int s = pserialize_read_enter(); 461 1.49 ozaki 462 1.51 ozaki /* 463 1.51 ozaki * Take another reference to the SP and the SA for opencrypto callback. 464 1.51 ozaki */ 465 1.51 ozaki if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD || 466 1.51 ozaki sav->state == SADB_SASTATE_DEAD)) { 467 1.49 ozaki pserialize_read_exit(s); 468 1.52 ozaki pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc); 469 1.49 ozaki crypto_freereq(crp); 470 1.49 ozaki IPCOMP_STATINC(IPCOMP_STAT_NOTDB); 471 1.49 ozaki error = ENOENT; 472 1.49 ozaki goto bad; 473 1.49 ozaki } 474 1.49 ozaki KEY_SP_REF(isr->sp); 475 1.51 ozaki KEY_SA_REF(sav); 476 1.49 ozaki pserialize_read_exit(s); 477 1.49 ozaki } 478 1.49 ozaki 479 1.1 jonathan tc->tc_isr = isr; 480 1.1 jonathan tc->tc_spi = sav->spi; 481 1.1 jonathan tc->tc_dst = sav->sah->saidx.dst; 482 1.1 jonathan tc->tc_proto = sav->sah->saidx.proto; 483 1.10 degroote tc->tc_skip = skip; 484 1.10 degroote tc->tc_protoff = protoff; 485 1.69 knakahar tc->tc_flags = flags; 486 1.42 ozaki tc->tc_sav = sav; 487 1.1 jonathan 488 1.1 jonathan /* Crypto operation descriptor */ 489 1.1 jonathan crp->crp_ilen = m->m_pkthdr.len; /* Total input length */ 490 1.1 jonathan crp->crp_flags = CRYPTO_F_IMBUF; 491 1.13 degroote crp->crp_buf = m; 492 1.1 jonathan crp->crp_callback = ipcomp_output_cb; 493 1.13 degroote crp->crp_opaque = tc; 494 1.1 jonathan crp->crp_sid = sav->tdb_cryptoid; 495 1.1 jonathan 496 1.74 riastrad crypto_dispatch(crp); 497 1.74 riastrad return 0; 498 1.57 maxv 499 1.1 jonathan bad: 500 1.76 rin m_freem(m); 501 1.57 maxv return error; 502 1.1 jonathan } 503 1.1 jonathan 504 1.1 jonathan /* 505 1.1 jonathan * IPComp output callback from the crypto driver. 506 1.1 jonathan */ 507 1.70 riastrad static void 508 1.1 jonathan ipcomp_output_cb(struct cryptop *crp) 509 1.1 jonathan { 510 1.38 ryo char buf[IPSEC_ADDRSTRLEN]; 511 1.1 jonathan struct tdb_crypto *tc; 512 1.53 ozaki const struct ipsecrequest *isr; 513 1.1 jonathan struct secasvar *sav; 514 1.9 degroote struct mbuf *m, *mo; 515 1.70 riastrad int skip, rlen, roff, flags; 516 1.34 christos uint8_t prot; 517 1.34 christos uint16_t cpi; 518 1.9 degroote struct ipcomp * ipcomp; 519 1.48 ozaki IPSEC_DECLARE_LOCK_VARIABLE; 520 1.9 degroote 521 1.36 ozaki KASSERT(crp->crp_opaque != NULL); 522 1.34 christos tc = crp->crp_opaque; 523 1.34 christos m = crp->crp_buf; 524 1.1 jonathan skip = tc->tc_skip; 525 1.1 jonathan rlen = crp->crp_ilen - skip; 526 1.1 jonathan 527 1.48 ozaki IPSEC_ACQUIRE_GLOBAL_LOCKS(); 528 1.1 jonathan 529 1.1 jonathan isr = tc->tc_isr; 530 1.42 ozaki sav = tc->tc_sav; 531 1.1 jonathan 532 1.1 jonathan /* Check for crypto errors */ 533 1.1 jonathan if (crp->crp_etype) { 534 1.1 jonathan /* Reset session ID */ 535 1.1 jonathan if (sav->tdb_cryptoid != 0) 536 1.1 jonathan sav->tdb_cryptoid = crp->crp_sid; 537 1.1 jonathan 538 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_NOXFORM); 539 1.68 christos DPRINTF("crypto error %d\n", crp->crp_etype); 540 1.1 jonathan goto bad; 541 1.1 jonathan } 542 1.45 ozaki 543 1.33 christos IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]); 544 1.1 jonathan 545 1.1 jonathan if (rlen > crp->crp_olen) { 546 1.9 degroote /* Inject IPCOMP header */ 547 1.9 degroote mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff); 548 1.9 degroote if (mo == NULL) { 549 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_WRAP); 550 1.68 christos DPRINTF("failed to inject IPCOMP header for " 551 1.68 christos "IPCA %s/%08lx\n", 552 1.38 ryo ipsec_address(&sav->sah->saidx.dst, buf, 553 1.68 christos sizeof(buf)), (u_long) ntohl(sav->spi)); 554 1.9 degroote goto bad; 555 1.9 degroote } 556 1.12 degroote ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff); 557 1.9 degroote 558 1.9 degroote /* Initialize the IPCOMP header */ 559 1.9 degroote /* XXX alignment always correct? */ 560 1.9 degroote switch (sav->sah->saidx.dst.sa.sa_family) { 561 1.9 degroote #ifdef INET 562 1.9 degroote case AF_INET: 563 1.9 degroote ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p; 564 1.57 maxv break; 565 1.57 maxv #endif 566 1.9 degroote #ifdef INET6 567 1.9 degroote case AF_INET6: 568 1.9 degroote ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt; 569 1.57 maxv break; 570 1.9 degroote #endif 571 1.9 degroote } 572 1.9 degroote ipcomp->comp_flags = 0; 573 1.9 degroote 574 1.75 christos cpi = ntohl(sav->spi) & 0xffff; 575 1.9 degroote ipcomp->comp_cpi = htons(cpi); 576 1.9 degroote 577 1.9 degroote /* Fix Next Protocol in IPv4/IPv6 header */ 578 1.9 degroote prot = IPPROTO_IPCOMP; 579 1.61 maxv m_copyback(m, tc->tc_protoff, sizeof(prot), &prot); 580 1.9 degroote 581 1.1 jonathan /* Adjust the length in the IP header */ 582 1.1 jonathan switch (sav->sah->saidx.dst.sa.sa_family) { 583 1.1 jonathan #ifdef INET 584 1.1 jonathan case AF_INET: 585 1.1 jonathan mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); 586 1.1 jonathan break; 587 1.57 maxv #endif 588 1.1 jonathan #ifdef INET6 589 1.1 jonathan case AF_INET6: 590 1.1 jonathan mtod(m, struct ip6_hdr *)->ip6_plen = 591 1.60 maxv htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); 592 1.1 jonathan break; 593 1.57 maxv #endif 594 1.1 jonathan default: 595 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_NOPF); 596 1.68 christos DPRINTF("unknown/unsupported protocol " 597 1.5 perry "family %d, IPCA %s/%08lx\n", 598 1.1 jonathan sav->sah->saidx.dst.sa.sa_family, 599 1.38 ryo ipsec_address(&sav->sah->saidx.dst, buf, 600 1.68 christos sizeof(buf)), (u_long) ntohl(sav->spi)); 601 1.1 jonathan goto bad; 602 1.1 jonathan } 603 1.1 jonathan } else { 604 1.1 jonathan /* compression was useless, we have lost time */ 605 1.18 thorpej IPCOMP_STATINC(IPCOMP_STAT_USELESS); 606 1.68 christos DPRINTF("compression was useless: initial size was %d " 607 1.68 christos "and compressed size is %d\n", rlen, crp->crp_olen); 608 1.1 jonathan } 609 1.1 jonathan 610 1.69 knakahar flags = tc->tc_flags; 611 1.1 jonathan /* Release the crypto descriptor */ 612 1.52 ozaki pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc); 613 1.1 jonathan crypto_freereq(crp); 614 1.1 jonathan 615 1.1 jonathan /* NB: m is reclaimed by ipsec_process_done. */ 616 1.70 riastrad (void)ipsec_process_done(m, isr, sav, flags); 617 1.50 ozaki KEY_SA_UNREF(&sav); 618 1.49 ozaki KEY_SP_UNREF(&isr->sp); 619 1.48 ozaki IPSEC_RELEASE_GLOBAL_LOCKS(); 620 1.70 riastrad return; 621 1.57 maxv 622 1.1 jonathan bad: 623 1.1 jonathan if (sav) 624 1.50 ozaki KEY_SA_UNREF(&sav); 625 1.49 ozaki KEY_SP_UNREF(&isr->sp); 626 1.48 ozaki IPSEC_RELEASE_GLOBAL_LOCKS(); 627 1.76 rin m_freem(m); 628 1.52 ozaki pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc); 629 1.1 jonathan crypto_freereq(crp); 630 1.1 jonathan } 631 1.1 jonathan 632 1.1 jonathan static struct xformsw ipcomp_xformsw = { 633 1.39 ozaki .xf_type = XF_IPCOMP, 634 1.39 ozaki .xf_flags = XFT_COMP, 635 1.39 ozaki .xf_name = "IPcomp", 636 1.39 ozaki .xf_init = ipcomp_init, 637 1.39 ozaki .xf_zeroize = ipcomp_zeroize, 638 1.39 ozaki .xf_input = ipcomp_input, 639 1.39 ozaki .xf_output = ipcomp_output, 640 1.39 ozaki .xf_next = NULL, 641 1.1 jonathan }; 642 1.1 jonathan 643 1.37 ozaki void 644 1.1 jonathan ipcomp_attach(void) 645 1.1 jonathan { 646 1.18 thorpej ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS); 647 1.52 ozaki ipcomp_tdb_crypto_pool_cache = pool_cache_init(sizeof(struct tdb_crypto), 648 1.52 ozaki coherency_unit, 0, 0, "ipcomp_tdb_crypto", NULL, IPL_SOFTNET, 649 1.52 ozaki NULL, NULL, NULL); 650 1.1 jonathan xform_register(&ipcomp_xformsw); 651 1.1 jonathan } 652