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