Merge from vendor branch GCC:
[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.5 2004/06/02 14:43:01 eirikn 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(int idx)
96 {
97
98         if (idx == SADB_X_CALG_DEFLATE)
99                 return &ipcomp_algorithms[0];
100         return NULL;
101 }
102
103 static void *
104 deflate_alloc(void *aux, u_int items, u_int siz)
105 {
106         void *ptr;
107         ptr = malloc(items * siz, M_TEMP, M_NOWAIT);
108         return ptr;
109 }
110
111 static void
112 deflate_free(void *aux, void *ptr)
113 {
114         free(ptr, M_TEMP);
115 }
116
117 static int
118 deflate_common(struct mbuf *m, struct mbuf *md, size_t *lenp,
119                int mode)        /* 0: compress 1: decompress */
120 {
121         struct mbuf *mprev;
122         struct mbuf *p;
123         struct mbuf *n = NULL, *n0 = NULL, **np;
124         z_stream zs;
125         int error = 0;
126         int zerror;
127         size_t offset;
128
129 #define MOREBLOCK() \
130 do { \
131         /* keep the reply buffer into our chain */              \
132         if (n) {                                                \
133                 n->m_len = zs.total_out - offset;               \
134                 offset = zs.total_out;                          \
135                 *np = n;                                        \
136                 np = &n->m_next;                                \
137                 n = NULL;                                       \
138         }                                                       \
139                                                                 \
140         /* get a fresh reply buffer */                          \
141         MGET(n, MB_DONTWAIT, MT_DATA);                          \
142         if (n) {                                                \
143                 MCLGET(n, MB_DONTWAIT);                         \
144         }                                                       \
145         if (!n) {                                               \
146                 error = ENOBUFS;                                \
147                 goto fail;                                      \
148         }                                                       \
149         n->m_len = 0;                                           \
150         n->m_len = M_TRAILINGSPACE(n);                          \
151         n->m_next = NULL;                                       \
152         /*                                                      \
153          * if this is the first reply buffer, reserve           \
154          * region for ipcomp header.                            \
155          */                                                     \
156         if (*np == NULL) {                                      \
157                 n->m_len -= sizeof(struct ipcomp);              \
158                 n->m_data += sizeof(struct ipcomp);             \
159         }                                                       \
160                                                                 \
161         zs.next_out = mtod(n, u_int8_t *);                      \
162         zs.avail_out = n->m_len;                                \
163 } while (0)
164
165         for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
166                 ;
167         if (!mprev)
168                 panic("md is not in m in deflate_common");
169
170         bzero(&zs, sizeof(zs));
171         zs.zalloc = deflate_alloc;
172         zs.zfree = deflate_free;
173
174         zerror = mode ? inflateInit2(&zs, deflate_window_in)
175                       : deflateInit2(&zs, deflate_policy, Z_DEFLATED,
176                                 deflate_window_out, deflate_memlevel,
177                                 Z_DEFAULT_STRATEGY);
178         if (zerror != Z_OK) {
179                 error = ENOBUFS;
180                 goto fail;
181         }
182
183         n0 = n = NULL;
184         np = &n0;
185         offset = 0;
186         zerror = 0;
187         p = md;
188         while (p && p->m_len == 0) {
189                 p = p->m_next;
190         }
191
192         /* input stream and output stream are available */
193         while (p && zs.avail_in == 0) {
194                 /* get input buffer */
195                 if (p && zs.avail_in == 0) {
196                         zs.next_in = mtod(p, u_int8_t *);
197                         zs.avail_in = p->m_len;
198                         p = p->m_next;
199                         while (p && p->m_len == 0) {
200                                 p = p->m_next;
201                         }
202                 }
203
204                 /* get output buffer */
205                 if (zs.next_out == NULL || zs.avail_out == 0) {
206                         MOREBLOCK();
207                 }
208
209                 zerror = mode ? inflate(&zs, Z_NO_FLUSH)
210                               : deflate(&zs, Z_NO_FLUSH);
211
212                 if (zerror == Z_STREAM_END)
213                         ; /* once more. */
214                 else if (zerror == Z_OK) {
215                         /* inflate: Z_OK can indicate the end of decode */
216                         if (mode && !p && zs.avail_out != 0)
217                                 goto terminate;
218                         else
219                                 ; /* once more. */
220                 } else {
221                         if (zs.msg) {
222                                 ipseclog((LOG_ERR, "ipcomp_%scompress: "
223                                     "%sflate(Z_NO_FLUSH): %s\n",
224                                     mode ? "de" : "", mode ? "in" : "de",
225                                     zs.msg));
226                         } else {
227                                 ipseclog((LOG_ERR, "ipcomp_%scompress: "
228                                     "%sflate(Z_NO_FLUSH): unknown error (%d)\n",
229                                     mode ? "de" : "", mode ? "in" : "de",
230                                     zerror));
231                         }
232                         mode ? inflateEnd(&zs) : deflateEnd(&zs);
233                         error = EINVAL;
234                         goto fail;
235                 }
236         }
237
238         if (zerror == Z_STREAM_END)
239                 goto terminate;
240
241         /* termination */
242         while (1) {
243                 /* get output buffer */
244                 if (zs.next_out == NULL || zs.avail_out == 0) {
245                         MOREBLOCK();
246                 }
247
248                 zerror = mode ? inflate(&zs, Z_SYNC_FLUSH)
249                               : deflate(&zs, Z_FINISH);
250
251                 if (zerror == Z_STREAM_END)
252                         break;
253                 else if (zerror == Z_OK) {
254                         if (mode && zs.avail_out != 0)
255                                 goto terminate;
256                         else
257                                 ; /* once more. */
258                 } else {
259                         if (zs.msg) {
260                                 ipseclog((LOG_ERR, "ipcomp_%scompress: "
261                                     "%sflate(Z_FINISH): %s\n",
262                                     mode ? "de" : "", mode ? "in" : "de",
263                                     zs.msg));
264                         } else {
265                                 ipseclog((LOG_ERR, "ipcomp_%scompress: "
266                                     "%sflate(Z_FINISH): unknown error (%d)\n",
267                                     mode ? "de" : "", mode ? "in" : "de",
268                                     zerror));
269                         }
270                         mode ? inflateEnd(&zs) : deflateEnd(&zs);
271                         error = EINVAL;
272                         goto fail;
273                 }
274         }
275
276 terminate:
277         zerror = mode ? inflateEnd(&zs) : deflateEnd(&zs);
278         if (zerror != Z_OK) {
279                 if (zs.msg) {
280                         ipseclog((LOG_ERR, "ipcomp_%scompress: "
281                             "%sflateEnd: %s\n",
282                             mode ? "de" : "", mode ? "in" : "de",
283                             zs.msg));
284                 } else {
285                         ipseclog((LOG_ERR, "ipcomp_%scompress: "
286                             "%sflateEnd: unknown error (%d)\n",
287                             mode ? "de" : "", mode ? "in" : "de",
288                             zerror));
289                 }
290                 error = EINVAL;
291                 goto fail;
292         }
293         /* keep the final reply buffer into our chain */
294         if (n) {
295                 n->m_len = zs.total_out - offset;
296                 offset = zs.total_out;
297                 *np = n;
298                 np = &n->m_next;
299                 n = NULL;
300         }
301
302         /* switch the mbuf to the new one */
303         mprev->m_next = n0;
304         m_freem(md);
305         *lenp = zs.total_out;
306
307         return 0;
308
309 fail:
310         if (m)
311                 m_freem(m);
312         if (n)
313                 m_freem(n);
314         if (n0)
315                 m_freem(n0);
316         return error;
317 #undef MOREBLOCK
318 }
319
320 static int
321 deflate_compress(struct mbuf *m, struct mbuf *md, size_t *lenp)
322 {
323         if (!m)
324                 panic("m == NULL in deflate_compress");
325         if (!md)
326                 panic("md == NULL in deflate_compress");
327         if (!lenp)
328                 panic("lenp == NULL in deflate_compress");
329
330         return deflate_common(m, md, lenp, 0);
331 }
332
333 static int
334 deflate_decompress(struct mbuf *m, struct mbuf *md, size_t *lenp)
335 {
336         if (!m)
337                 panic("m == NULL in deflate_decompress");
338         if (!md)
339                 panic("md == NULL in deflate_decompress");
340         if (!lenp)
341                 panic("lenp == NULL in deflate_decompress");
342
343         return deflate_common(m, md, lenp, 1);
344 }