Per-CPU VFS Namecache Effectiveness Statistics:
[dragonfly.git] / sys / netinet6 / ipcomp_core.c
1 /*      $FreeBSD: src/sys/netinet6/ipcomp_core.c,v 1.1.2.5 2003/01/11 19:10:59 ume Exp $        */
2 /*      $DragonFly: src/sys/netinet6/ipcomp_core.c,v 1.3 2003/08/23 11:02:45 rob Exp $  */
3 /*      $KAME: ipcomp_core.c,v 1.25 2001/07/26 06:53:17 jinmei Exp $    */
4
5 /*
6  * Copyright (C) 1999 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * RFC2393 IP payload compression protocol (IPComp).
36  */
37
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
49 #include <sys/time.h>
50 #include <sys/syslog.h>
51 #include <sys/queue.h>
52
53 #include <net/if.h>
54 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <net/netisr.h>
57 #include <net/zlib.h>
58 #include <machine/cpu.h>
59
60 #include <netinet6/ipcomp.h>
61 #ifdef INET6
62 #include <netinet6/ipcomp6.h>
63 #endif
64 #include <netinet6/ipsec.h>
65 #ifdef INET6
66 #include <netinet6/ipsec6.h>
67 #endif
68
69 #include <machine/stdarg.h>
70
71 #include <net/net_osdep.h>
72
73 static void *deflate_alloc (void *, u_int, u_int);
74 static void deflate_free (void *, void *);
75 static int deflate_common (struct mbuf *, struct mbuf *, size_t *, int);
76 static int deflate_compress (struct mbuf *, struct mbuf *, size_t *);
77 static int deflate_decompress (struct mbuf *, struct mbuf *, size_t *);
78
79 /*
80  * We need to use default window size (2^15 = 32Kbytes as of writing) for
81  * inbound case.  Otherwise we get interop problem.
82  * Use negative value to avoid Adler32 checksum.  This is an undocumented
83  * feature in zlib (see ipsec wg mailing list archive in January 2000).
84  */
85 static int deflate_policy = Z_DEFAULT_COMPRESSION;
86 static int deflate_window_out = -12;
87 static const int deflate_window_in = -1 * MAX_WBITS;    /* don't change it */
88 static int deflate_memlevel = MAX_MEM_LEVEL;
89
90 static const struct ipcomp_algorithm ipcomp_algorithms[] = {
91         { deflate_compress, deflate_decompress, 90 },
92 };
93
94 const struct ipcomp_algorithm *
95 ipcomp_algorithm_lookup(idx)
96         int idx;
97 {
98
99         if (idx == SADB_X_CALG_DEFLATE)
100                 return &ipcomp_algorithms[0];
101         return NULL;
102 }
103
104 static void *
105 deflate_alloc(aux, items, siz)
106         void *aux;
107         u_int items;
108         u_int siz;
109 {
110         void *ptr;
111         ptr = malloc(items * siz, M_TEMP, M_NOWAIT);
112         return ptr;
113 }
114
115 static void
116 deflate_free(aux, ptr)
117         void *aux;
118         void *ptr;
119 {
120         free(ptr, M_TEMP);
121 }
122
123 static int
124 deflate_common(m, md, lenp, mode)
125         struct mbuf *m;
126         struct mbuf *md;
127         size_t *lenp;
128         int mode;       /* 0: compress 1: decompress */
129 {
130         struct mbuf *mprev;
131         struct mbuf *p;
132         struct mbuf *n = NULL, *n0 = NULL, **np;
133         z_stream zs;
134         int error = 0;
135         int zerror;
136         size_t offset;
137
138 #define MOREBLOCK() \
139 do { \
140         /* keep the reply buffer into our chain */              \
141         if (n) {                                                \
142                 n->m_len = zs.total_out - offset;               \
143                 offset = zs.total_out;                          \
144                 *np = n;                                        \
145                 np = &n->m_next;                                \
146                 n = NULL;                                       \
147         }                                                       \
148                                                                 \
149         /* get a fresh reply buffer */                          \
150         MGET(n, M_DONTWAIT, MT_DATA);                           \
151         if (n) {                                                \
152                 MCLGET(n, M_DONTWAIT);                          \
153         }                                                       \
154         if (!n) {                                               \
155                 error = ENOBUFS;                                \
156                 goto fail;                                      \
157         }                                                       \
158         n->m_len = 0;                                           \
159         n->m_len = M_TRAILINGSPACE(n);                          \
160         n->m_next = NULL;                                       \
161         /*                                                      \
162          * if this is the first reply buffer, reserve           \
163          * region for ipcomp header.                            \
164          */                                                     \
165         if (*np == NULL) {                                      \
166                 n->m_len -= sizeof(struct ipcomp);              \
167                 n->m_data += sizeof(struct ipcomp);             \
168         }                                                       \
169                                                                 \
170         zs.next_out = mtod(n, u_int8_t *);                      \
171         zs.avail_out = n->m_len;                                \
172 } while (0)
173
174         for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
175                 ;
176         if (!mprev)
177                 panic("md is not in m in deflate_common");
178
179         bzero(&zs, sizeof(zs));
180         zs.zalloc = deflate_alloc;
181         zs.zfree = deflate_free;
182
183         zerror = mode ? inflateInit2(&zs, deflate_window_in)
184                       : deflateInit2(&zs, deflate_policy, Z_DEFLATED,
185                                 deflate_window_out, deflate_memlevel,
186                                 Z_DEFAULT_STRATEGY);
187         if (zerror != Z_OK) {
188                 error = ENOBUFS;
189                 goto fail;
190         }
191
192         n0 = n = NULL;
193         np = &n0;
194         offset = 0;
195         zerror = 0;
196         p = md;
197         while (p && p->m_len == 0) {
198                 p = p->m_next;
199         }
200
201         /* input stream and output stream are available */
202         while (p && zs.avail_in == 0) {
203                 /* get input buffer */
204                 if (p && zs.avail_in == 0) {
205                         zs.next_in = mtod(p, u_int8_t *);
206                         zs.avail_in = p->m_len;
207                         p = p->m_next;
208                         while (p && p->m_len == 0) {
209                                 p = p->m_next;
210                         }
211                 }
212
213                 /* get output buffer */
214                 if (zs.next_out == NULL || zs.avail_out == 0) {
215                         MOREBLOCK();
216                 }
217
218                 zerror = mode ? inflate(&zs, Z_NO_FLUSH)
219                               : deflate(&zs, Z_NO_FLUSH);
220
221                 if (zerror == Z_STREAM_END)
222                         ; /* once more. */
223                 else if (zerror == Z_OK) {
224                         /* inflate: Z_OK can indicate the end of decode */
225                         if (mode && !p && zs.avail_out != 0)
226                                 goto terminate;
227                         else
228                                 ; /* once more. */
229                 } else {
230                         if (zs.msg) {
231                                 ipseclog((LOG_ERR, "ipcomp_%scompress: "
232                                     "%sflate(Z_NO_FLUSH): %s\n",
233                                     mode ? "de" : "", mode ? "in" : "de",
234                                     zs.msg));
235                         } else {
236                                 ipseclog((LOG_ERR, "ipcomp_%scompress: "
237                                     "%sflate(Z_NO_FLUSH): unknown error (%d)\n",
238                                     mode ? "de" : "", mode ? "in" : "de",
239                                     zerror));
240                         }
241                         mode ? inflateEnd(&zs) : deflateEnd(&zs);
242                         error = EINVAL;
243                         goto fail;
244                 }
245         }
246
247         if (zerror == Z_STREAM_END)
248                 goto terminate;
249
250         /* termination */
251         while (1) {
252                 /* get output buffer */
253                 if (zs.next_out == NULL || zs.avail_out == 0) {
254                         MOREBLOCK();
255                 }
256
257                 zerror = mode ? inflate(&zs, Z_SYNC_FLUSH)
258                               : deflate(&zs, Z_FINISH);
259
260                 if (zerror == Z_STREAM_END)
261                         break;
262                 else if (zerror == Z_OK) {
263                         if (mode && zs.avail_out != 0)
264                                 goto terminate;
265                         else
266                                 ; /* once more. */
267                 } else {
268                         if (zs.msg) {
269                                 ipseclog((LOG_ERR, "ipcomp_%scompress: "
270                                     "%sflate(Z_FINISH): %s\n",
271                                     mode ? "de" : "", mode ? "in" : "de",
272                                     zs.msg));
273                         } else {
274                                 ipseclog((LOG_ERR, "ipcomp_%scompress: "
275                                     "%sflate(Z_FINISH): unknown error (%d)\n",
276                                     mode ? "de" : "", mode ? "in" : "de",
277                                     zerror));
278                         }
279                         mode ? inflateEnd(&zs) : deflateEnd(&zs);
280                         error = EINVAL;
281                         goto fail;
282                 }
283         }
284
285 terminate:
286         zerror = mode ? inflateEnd(&zs) : deflateEnd(&zs);
287         if (zerror != Z_OK) {
288                 if (zs.msg) {
289                         ipseclog((LOG_ERR, "ipcomp_%scompress: "
290                             "%sflateEnd: %s\n",
291                             mode ? "de" : "", mode ? "in" : "de",
292                             zs.msg));
293                 } else {
294                         ipseclog((LOG_ERR, "ipcomp_%scompress: "
295                             "%sflateEnd: unknown error (%d)\n",
296                             mode ? "de" : "", mode ? "in" : "de",
297                             zerror));
298                 }
299                 error = EINVAL;
300                 goto fail;
301         }
302         /* keep the final reply buffer into our chain */
303         if (n) {
304                 n->m_len = zs.total_out - offset;
305                 offset = zs.total_out;
306                 *np = n;
307                 np = &n->m_next;
308                 n = NULL;
309         }
310
311         /* switch the mbuf to the new one */
312         mprev->m_next = n0;
313         m_freem(md);
314         *lenp = zs.total_out;
315
316         return 0;
317
318 fail:
319         if (m)
320                 m_freem(m);
321         if (n)
322                 m_freem(n);
323         if (n0)
324                 m_freem(n0);
325         return error;
326 #undef MOREBLOCK
327 }
328
329 static int
330 deflate_compress(m, md, lenp)
331         struct mbuf *m;
332         struct mbuf *md;
333         size_t *lenp;
334 {
335         if (!m)
336                 panic("m == NULL in deflate_compress");
337         if (!md)
338                 panic("md == NULL in deflate_compress");
339         if (!lenp)
340                 panic("lenp == NULL in deflate_compress");
341
342         return deflate_common(m, md, lenp, 0);
343 }
344
345 static int
346 deflate_decompress(m, md, lenp)
347         struct mbuf *m;
348         struct mbuf *md;
349         size_t *lenp;
350 {
351         if (!m)
352                 panic("m == NULL in deflate_decompress");
353         if (!md)
354                 panic("md == NULL in deflate_decompress");
355         if (!lenp)
356                 panic("lenp == NULL in deflate_decompress");
357
358         return deflate_common(m, md, lenp, 1);
359 }