1/*
2 * ppp-comp.h - Definitions for doing PPP packet compression.
3 *
4 * Copyright (c) 1984 Paul Mackerras. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in
15 *    the documentation and/or other materials provided with the
16 *    distribution.
17 *
18 * 3. The name(s) of the authors of this software must not be used to
19 *    endorse or promote products derived from this software without
20 *    prior written permission.
21 *
22 * 4. Redistributions of any form whatsoever must retain the following
23 *    acknowledgment:
24 *    "This product includes software developed by Paul Mackerras
25 *     <paulus@samba.org>".
26 *
27 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 *
35 * $Id: ppp-comp.h,v 1.10 2002/12/06 09:49:15 paulus Exp $
36 */
37
38/*
39 *  ==FILEVERSION 20020319==
40 *
41 *  NOTE TO MAINTAINERS:
42 *     If you modify this file at all, please set the above date.
43 *     ppp-comp.h is shipped with a PPP distribution as well as with the kernel;
44 *     if everyone increases the FILEVERSION number above, then scripts
45 *     can do the right thing when deciding whether to install a new ppp-comp.h
46 *     file.  Don't change the format of that line otherwise, so the
47 *     installation script can recognize it.
48 */
49
50#ifndef _NET_PPP_COMP_H
51#define _NET_PPP_COMP_H
52
53/*
54 * The following symbols control whether we include code for
55 * various compression methods.
56 */
57
58#ifndef DO_BSD_COMPRESS
59#define DO_BSD_COMPRESS	1	/* by default, include BSD-Compress */
60#endif
61#ifndef DO_DEFLATE
62#define DO_DEFLATE	1	/* by default, include Deflate */
63#endif
64#define DO_PREDICTOR_1	0
65#define DO_PREDICTOR_2	0
66
67/*
68 * Structure giving methods for compression/decompression.
69 */
70
71struct compressor {
72	int	compress_proto;	/* CCP compression protocol number */
73
74	/* Allocate space for a compressor (transmit side) */
75	void	*(*comp_alloc) (unsigned char *options, int opt_len);
76
77	/* Free space used by a compressor */
78	void	(*comp_free) (void *state);
79
80	/* Initialize a compressor */
81	int	(*comp_init) (void *state, unsigned char *options,
82			      int opt_len, int unit, int opthdr, int debug);
83
84	/* Reset a compressor */
85	void	(*comp_reset) (void *state);
86
87	/* Compress a packet */
88	int     (*compress) (void *state, unsigned char *rptr,
89			      unsigned char *obuf, int isize, int osize);
90
91	/* Return compression statistics */
92	void	(*comp_stat) (void *state, struct compstat *stats);
93
94	/* Allocate space for a decompressor (receive side) */
95	void	*(*decomp_alloc) (unsigned char *options, int opt_len);
96
97	/* Free space used by a decompressor */
98	void	(*decomp_free) (void *state);
99
100	/* Initialize a decompressor */
101	int	(*decomp_init) (void *state, unsigned char *options,
102				int opt_len, int unit, int opthdr, int mru,
103				int debug);
104
105	/* Reset a decompressor */
106	void	(*decomp_reset) (void *state);
107
108	/* Decompress a packet. */
109	int	(*decompress) (void *state, unsigned char *ibuf, int isize,
110				unsigned char *obuf, int osize);
111
112	/* Update state for an incompressible packet received */
113	void	(*incomp) (void *state, unsigned char *ibuf, int icnt);
114
115	/* Return decompression statistics */
116	void	(*decomp_stat) (void *state, struct compstat *stats);
117};
118
119/*
120 * The return value from decompress routine is the length of the
121 * decompressed packet if successful, otherwise DECOMP_ERROR
122 * or DECOMP_FATALERROR if an error occurred.
123 *
124 * We need to make this distinction so that we can disable certain
125 * useful functionality, namely sending a CCP reset-request as a result
126 * of an error detected after decompression.  This is to avoid infringing
127 * a patent held by Motorola.
128 * Don't you just lurve software patents.
129 */
130
131#define DECOMP_ERROR		-1	/* error detected before decomp. */
132#define DECOMP_FATALERROR	-2	/* error detected after decomp. */
133
134/*
135 * CCP codes.
136 */
137
138#define CCP_CONFREQ	1
139#define CCP_CONFACK	2
140#define CCP_TERMREQ	5
141#define CCP_TERMACK	6
142#define CCP_RESETREQ	14
143#define CCP_RESETACK	15
144
145/*
146 * Max # bytes for a CCP option
147 */
148
149#define CCP_MAX_OPTION_LENGTH	32
150
151/*
152 * Parts of a CCP packet.
153 */
154
155#define CCP_CODE(dp)		((dp)[0])
156#define CCP_ID(dp)		((dp)[1])
157#define CCP_LENGTH(dp)		(((dp)[2] << 8) + (dp)[3])
158#define CCP_HDRLEN		4
159
160#define CCP_OPT_CODE(dp)	((dp)[0])
161#define CCP_OPT_LENGTH(dp)	((dp)[1])
162#define CCP_OPT_MINLEN		2
163
164/*
165 * Definitions for BSD-Compress.
166 */
167
168#define CI_BSD_COMPRESS		21	/* config. option for BSD-Compress */
169#define CILEN_BSD_COMPRESS	3	/* length of config. option */
170
171/* Macros for handling the 3rd byte of the BSD-Compress config option. */
172#define BSD_NBITS(x)		((x) & 0x1F)	/* number of bits requested */
173#define BSD_VERSION(x)		((x) >> 5)	/* version of option format */
174#define BSD_CURRENT_VERSION	1		/* current version number */
175#define BSD_MAKE_OPT(v, n)	(((v) << 5) | (n))
176
177#define BSD_MIN_BITS		9	/* smallest code size supported */
178#define BSD_MAX_BITS		15	/* largest code size supported */
179
180/*
181 * Definitions for Deflate.
182 */
183
184#define CI_DEFLATE		26	/* config option for Deflate */
185#define CI_DEFLATE_DRAFT	24	/* value used in original draft RFC */
186#define CILEN_DEFLATE		4	/* length of its config option */
187
188#define DEFLATE_MIN_SIZE	8
189#define DEFLATE_MAX_SIZE	15
190#define DEFLATE_METHOD_VAL	8
191#define DEFLATE_SIZE(x)		(((x) >> 4) + DEFLATE_MIN_SIZE)
192#define DEFLATE_METHOD(x)	((x) & 0x0F)
193#define DEFLATE_MAKE_OPT(w)	((((w) - DEFLATE_MIN_SIZE) << 4) \
194				 + DEFLATE_METHOD_VAL)
195#define DEFLATE_CHK_SEQUENCE	0
196
197/*
198 * Definitions for MPPE.
199 */
200
201#define CI_MPPE			18	/* config option for MPPE */
202#define CILEN_MPPE		6	/* length of config option */
203
204#define MPPE_PAD		4	/* MPPE growth per frame */
205#define MPPE_MAX_KEY_LEN	16	/* largest key length (128-bit) */
206
207/* option bits for ccp_options.mppe */
208#define MPPE_OPT_40		0x01	/* 40 bit */
209#define MPPE_OPT_128		0x02	/* 128 bit */
210#define MPPE_OPT_STATEFUL	0x04	/* stateful mode */
211/* unsupported opts */
212#define MPPE_OPT_56		0x08	/* 56 bit */
213#define MPPE_OPT_MPPC		0x10	/* MPPC compression */
214#define MPPE_OPT_D		0x20	/* Unknown */
215#define MPPE_OPT_UNSUPPORTED (MPPE_OPT_56|MPPE_OPT_MPPC|MPPE_OPT_D)
216#define MPPE_OPT_UNKNOWN	0x40	/* Bits !defined in RFC 3078 were set */
217
218/*
219 * This is not nice ... the alternative is a bitfield struct though.
220 * And unfortunately, we cannot share the same bits for the option
221 * names above since C and H are the same bit.  We could do a u_int32
222 * but then we have to do a htonl() all the time and/or we still need
223 * to know which octet is which.
224 */
225#define MPPE_C_BIT		0x01	/* MPPC */
226#define MPPE_D_BIT		0x10	/* Obsolete, usage unknown */
227#define MPPE_L_BIT		0x20	/* 40-bit */
228#define MPPE_S_BIT		0x40	/* 128-bit */
229#define MPPE_M_BIT		0x80	/* 56-bit, not supported */
230#define MPPE_H_BIT		0x01	/* Stateless (in a different byte) */
231
232/* Does not include H bit; used for least significant octet only. */
233#define MPPE_ALL_BITS (MPPE_D_BIT|MPPE_L_BIT|MPPE_S_BIT|MPPE_M_BIT|MPPE_H_BIT)
234
235/* Build a CI from mppe opts (see RFC 3078) */
236#define MPPE_OPTS_TO_CI(opts, ci)		\
237    do {					\
238	u_char *ptr = ci; /* u_char[4] */	\
239						\
240	/* H bit */				\
241	if (opts & MPPE_OPT_STATEFUL)		\
242	    *ptr++ = 0x0;			\
243	else					\
244	    *ptr++ = MPPE_H_BIT;		\
245	*ptr++ = 0;				\
246	*ptr++ = 0;				\
247						\
248	/* S,L bits */				\
249	*ptr = 0;				\
250	if (opts & MPPE_OPT_128)		\
251	    *ptr |= MPPE_S_BIT;			\
252	if (opts & MPPE_OPT_40)			\
253	    *ptr |= MPPE_L_BIT;			\
254	/* M,D,C bits not supported */		\
255    } while (/* CONSTCOND */ 0)
256
257/* The reverse of the above */
258#define MPPE_CI_TO_OPTS(ci, opts)		\
259    do {					\
260	u_char *ptr = ci; /* u_char[4] */	\
261						\
262	opts = 0;				\
263						\
264	/* H bit */				\
265	if (!(ptr[0] & MPPE_H_BIT))		\
266	    opts |= MPPE_OPT_STATEFUL;		\
267						\
268	/* S,L bits */				\
269	if (ptr[3] & MPPE_S_BIT)		\
270	    opts |= MPPE_OPT_128;		\
271	if (ptr[3] & MPPE_L_BIT)		\
272	    opts |= MPPE_OPT_40;		\
273						\
274	/* M,D,C bits */			\
275	if (ptr[3] & MPPE_M_BIT)		\
276	    opts |= MPPE_OPT_56;		\
277	if (ptr[3] & MPPE_D_BIT)		\
278	    opts |= MPPE_OPT_D;			\
279	if (ptr[3] & MPPE_C_BIT)		\
280	    opts |= MPPE_OPT_MPPC;		\
281						\
282	/* Other bits */			\
283	if (ptr[0] & ~MPPE_H_BIT)		\
284	    opts |= MPPE_OPT_UNKNOWN;		\
285	if (ptr[1] || ptr[2])			\
286	    opts |= MPPE_OPT_UNKNOWN;		\
287	if (ptr[3] & ~MPPE_ALL_BITS)		\
288	    opts |= MPPE_OPT_UNKNOWN;		\
289    } while (/* CONSTCOND */ 0)
290
291/*
292 * Definitions for other, as yet unsupported, compression methods.
293 */
294
295#define CI_PREDICTOR_1		1	/* config option for Predictor-1 */
296#define CILEN_PREDICTOR_1	2	/* length of its config option */
297#define CI_PREDICTOR_2		2	/* config option for Predictor-2 */
298#define CILEN_PREDICTOR_2	2	/* length of its config option */
299
300#ifdef __KERNEL__
301extern int ppp_register_compressor(struct compressor *);
302extern void ppp_unregister_compressor(struct compressor *);
303#endif /* __KERNEL__ */
304
305#endif /* _NET_PPP_COMP_H */
306