4 * Deflate algorithm (RFC 1951), implemented here primarily for use
5 * by IPCOMP (RFC 3173 & RFC 2394).
7 * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
14 * FIXME: deflate transforms will require up to a total of about 436k of kernel
15 * memory on i386 (390k for compression, the rest for decompression), as the
16 * current zlib kernel code uses a worst case pre-allocation system by default.
17 * This needs to be fixed so that the amount of memory required is properly
18 * related to the winbits and memlevel parameters.
20 * The default winbits of 11 should suit most packets, and it may be something
21 * to configure on a per-tfm basis in the future.
23 * Currently, compression history is not maintained between tfm calls, as
24 * it is not needed for IPCOMP and keeps the code simpler. It can be
25 * implemented if someone wants it.
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/crypto.h>
30 #include <linux/zlib.h>
31 #include <linux/vmalloc.h>
32 #include <linux/interrupt.h>
34 #include <linux/net.h>
35 #include <linux/slab.h>
37 #define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION
38 #define DEFLATE_DEF_WINBITS 11
39 #define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
43 int decomp_initialized;
44 struct z_stream_s comp_stream;
45 struct z_stream_s decomp_stream;
48 static inline int deflate_gfp(void)
50 return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
53 static int deflate_init(void *ctx)
58 static void deflate_exit(void *ctx)
60 struct deflate_ctx *dctx = ctx;
62 if (dctx->comp_initialized)
63 vfree(dctx->comp_stream.workspace);
64 if (dctx->decomp_initialized)
65 kfree(dctx->decomp_stream.workspace);
69 * Lazy initialization to make interface simple without allocating
70 * un-needed workspaces. Thus can be called in softirq context.
72 static int deflate_comp_init(struct deflate_ctx *ctx)
75 struct z_stream_s *stream = &ctx->comp_stream;
77 stream->workspace = __vmalloc(zlib_deflate_workspacesize(),
78 deflate_gfp()|__GFP_HIGHMEM,
80 if (!stream->workspace ) {
84 memset(stream->workspace, 0, zlib_deflate_workspacesize());
85 ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
86 -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
92 ctx->comp_initialized = 1;
96 vfree(stream->workspace);
100 static int deflate_decomp_init(struct deflate_ctx *ctx)
103 struct z_stream_s *stream = &ctx->decomp_stream;
105 stream->workspace = kmalloc(zlib_inflate_workspacesize(),
107 if (!stream->workspace ) {
111 memset(stream->workspace, 0, zlib_inflate_workspacesize());
112 ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
117 ctx->decomp_initialized = 1;
121 kfree(stream->workspace);
125 static int deflate_compress(void *ctx, const u8 *src, unsigned int slen,
126 u8 *dst, unsigned int *dlen)
129 struct deflate_ctx *dctx = ctx;
130 struct z_stream_s *stream = &dctx->comp_stream;
132 if (!dctx->comp_initialized) {
133 ret = deflate_comp_init(dctx);
138 ret = zlib_deflateReset(stream);
144 stream->next_in = (u8 *)src;
145 stream->avail_in = slen;
146 stream->next_out = (u8 *)dst;
147 stream->avail_out = *dlen;
149 ret = zlib_deflate(stream, Z_FINISH);
150 if (ret != Z_STREAM_END) {
155 *dlen = stream->total_out;
160 static int deflate_decompress(void *ctx, const u8 *src, unsigned int slen,
161 u8 *dst, unsigned int *dlen)
165 struct deflate_ctx *dctx = ctx;
166 struct z_stream_s *stream = &dctx->decomp_stream;
168 if (!dctx->decomp_initialized) {
169 ret = deflate_decomp_init(dctx);
174 ret = zlib_inflateReset(stream);
180 stream->next_in = (u8 *)src;
181 stream->avail_in = slen;
182 stream->next_out = (u8 *)dst;
183 stream->avail_out = *dlen;
185 ret = zlib_inflate(stream, Z_SYNC_FLUSH);
187 * Work around a bug in zlib, which sometimes wants to taste an extra
188 * byte when being used in the (undocumented) raw deflate mode.
191 if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
193 stream->next_in = &zerostuff;
194 stream->avail_in = 1;
195 ret = zlib_inflate(stream, Z_FINISH);
197 if (ret != Z_STREAM_END) {
202 *dlen = stream->total_out;
207 static struct crypto_alg alg = {
208 .cra_name = "deflate",
209 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
210 .cra_ctxsize = sizeof(struct deflate_ctx),
211 .cra_module = THIS_MODULE,
212 .cra_list = LIST_HEAD_INIT(alg.cra_list),
213 .cra_u = { .compress = {
214 .coa_init = deflate_init,
215 .coa_exit = deflate_exit,
216 .coa_compress = deflate_compress,
217 .coa_decompress = deflate_decompress } }
220 static int __init init(void)
222 return crypto_register_alg(&alg);
225 static void __exit fini(void)
227 crypto_unregister_alg(&alg);
233 MODULE_LICENSE("GPL");
234 MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
235 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");