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