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