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