3 @@ -602,5 +602,20 @@ int crypto_has_alg(const char *name, u32
5 EXPORT_SYMBOL_GPL(crypto_has_alg);
7 +int crypto_alg_available(const char *name, u32 flags)
10 + struct crypto_alg *alg = crypto_alg_mod_lookup(name, 0,
14 + crypto_mod_put(alg);
20 +EXPORT_SYMBOL_GPL(crypto_alg_available);
22 MODULE_DESCRIPTION("Cryptographic core API");
23 MODULE_LICENSE("GPL");
24 --- a/drivers/net/Kconfig
25 +++ b/drivers/net/Kconfig
26 @@ -3349,19 +3349,31 @@ config PPP_BSDCOMP
27 module; it is called bsd_comp and will show up in the directory
28 modules once you have said "make modules". If unsure, say N.
31 - tristate "PPP MPPE compression (encryption) (EXPERIMENTAL)"
32 - depends on PPP && EXPERIMENTAL
38 - Support for the MPPE Encryption protocol, as employed by the
39 - Microsoft Point-to-Point Tunneling Protocol.
41 + tristate "Microsoft PPP compression/encryption (MPPC/MPPE)"
46 + Support for the Microsoft Point-To-Point Compression (RFC2118) and
47 + Microsoft Point-To-Point Encryption (RFC3078). These protocols are
48 + supported by Microsoft Windows and wide range of "hardware" access
49 + servers. MPPE is common protocol in Virtual Private Networks. According
50 + to RFC3078, MPPE supports 40, 56 and 128-bit key lengths. Depending on
51 + PPP daemon configuration on both ends of the link, following scenarios
53 + - only compression (MPPC) is used,
54 + - only encryption (MPPE) is used,
55 + - compression and encryption (MPPC+MPPE) are used.
57 - See http://pptpclient.sourceforge.net/ for information on
58 - configuring PPTP clients and servers to utilize this method.
59 + Please note that Hi/Fn (http://www.hifn.com) holds patent on MPPC so
60 + you should check if this patent is valid in your country in order to
61 + avoid legal problems.
63 + For more information please visit http://mppe-mppc.alphacron.de
65 + To compile this driver as a module, choose M here. The module will
66 + be called ppp_mppe_mppc.ko.
69 tristate "PPP over Ethernet (EXPERIMENTAL)"
70 --- a/drivers/net/Makefile
71 +++ b/drivers/net/Makefile
72 @@ -162,7 +162,7 @@ obj-$(CONFIG_PPP_ASYNC) += ppp_async.o
73 obj-$(CONFIG_PPP_SYNC_TTY) += ppp_synctty.o
74 obj-$(CONFIG_PPP_DEFLATE) += ppp_deflate.o
75 obj-$(CONFIG_PPP_BSDCOMP) += bsd_comp.o
76 -obj-$(CONFIG_PPP_MPPE) += ppp_mppe.o
77 +obj-$(CONFIG_PPP_MPPE_MPPC) += ppp_mppe_mppc.o
78 obj-$(CONFIG_PPPOE) += pppox.o pppoe.o
79 obj-$(CONFIG_PPPOL2TP) += pppox.o
80 obj-$(CONFIG_PPTP) += pppox.o pptp.o
81 --- a/drivers/net/ppp_generic.c
82 +++ b/drivers/net/ppp_generic.c
85 #include <linux/tcp.h>
86 #include <linux/spinlock.h>
87 +#include <linux/smp_lock.h>
88 #include <linux/rwsem.h>
89 #include <linux/stddef.h>
90 #include <linux/device.h>
91 @@ -87,10 +88,11 @@ struct ppp_file {
92 int dead; /* unit/channel has been shut down */
95 -#define PF_TO_X(pf, X) container_of(pf, X, file)
96 +#define PF_TO_X(pf, X) ((X *)((char *)(pf) - offsetof(X, file)))
98 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
99 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
100 +#define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
103 * Data structure describing one ppp unit.
104 @@ -106,6 +108,7 @@ struct ppp {
105 spinlock_t rlock; /* lock for receive side 58 */
106 spinlock_t wlock; /* lock for transmit side 5c */
107 int mru; /* max receive unit 60 */
108 + int mru_alloc; /* MAX(1500,MRU) for dev_alloc_skb() */
109 unsigned int flags; /* control bits 64 */
110 unsigned int xstate; /* transmit state bits 68 */
111 unsigned int rstate; /* receive state bits 6c */
112 @@ -139,14 +142,13 @@ struct ppp {
115 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
116 - * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
118 + * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
119 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
120 * Bits in xstate: SC_COMP_RUN
122 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
123 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
124 - |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
125 + |SC_COMP_TCP|SC_REJ_COMP_TCP)
128 * Private data structure for each channel.
129 @@ -639,7 +641,9 @@ static long ppp_ioctl(struct file *file,
131 if (get_user(val, p))
134 + ppp->mru_alloc = ppp->mru = val;
135 + if (ppp->mru_alloc < PPP_MRU)
136 + ppp->mru_alloc = PPP_MRU; /* increase for broken peers */
140 @@ -1066,56 +1070,6 @@ ppp_xmit_process(struct ppp *ppp)
141 ppp_xmit_unlock(ppp);
144 -static inline struct sk_buff *
145 -pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
147 - struct sk_buff *new_skb;
149 - int new_skb_size = ppp->dev->mtu +
150 - ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
151 - int compressor_skb_size = ppp->dev->mtu +
152 - ppp->xcomp->comp_extra + PPP_HDRLEN;
153 - new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
155 - if (net_ratelimit())
156 - printk(KERN_ERR "PPP: no memory (comp pkt)\n");
159 - if (ppp->dev->hard_header_len > PPP_HDRLEN)
160 - skb_reserve(new_skb,
161 - ppp->dev->hard_header_len - PPP_HDRLEN);
163 - /* compressor still expects A/C bytes in hdr */
164 - len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
165 - new_skb->data, skb->len + 2,
166 - compressor_skb_size);
167 - if (len > 0 && (ppp->flags & SC_CCP_UP)) {
171 - skb_pull(skb, 2); /* pull off A/C bytes */
172 - } else if (len == 0) {
173 - /* didn't compress, or CCP not up yet */
174 - kfree_skb(new_skb);
179 - * MPPE requires that we do not send unencrypted
180 - * frames. The compressor will return -1 if we
181 - * should drop the frame. We cannot simply test
182 - * the compress_proto because MPPE and MPPC share
185 - if (net_ratelimit())
186 - printk(KERN_ERR "ppp: compressor dropped pkt\n");
188 - kfree_skb(new_skb);
195 * Compress and send a frame.
196 * The caller should have locked the xmit path,
197 @@ -1196,20 +1150,62 @@ ppp_send_frame(struct ppp *ppp, struct s
199 /* peek at outbound CCP frames */
200 ppp_ccp_peek(ppp, skb, 0);
201 + if (CCP_CODE(skb->data+2) == CCP_RESETACK
202 + && (ppp->xcomp->compress_proto == CI_MPPE
203 + || ppp->xcomp->compress_proto == CI_LZS)) {
204 + --ppp->dev->stats.tx_packets;
205 + ppp->dev->stats.tx_bytes -= skb->len - 2;
212 /* try to do packet compression */
213 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
214 proto != PPP_LCP && proto != PPP_CCP) {
215 - if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
216 - if (net_ratelimit())
217 - printk(KERN_ERR "ppp: compression required but down - pkt dropped.\n");
220 + * because of possible data expansion when MPPC or LZS
221 + * is used, allocate compressor's buffer 12.5% bigger
224 + if (ppp->xcomp->compress_proto == CI_MPPE)
225 + comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + MPPE_OVHD;
226 + else if (ppp->xcomp->compress_proto == CI_LZS)
227 + comp_ovhd = ((ppp->dev->mtu * 9) / 8) + 1 + LZS_OVHD;
228 + new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len
229 + + comp_ovhd, GFP_ATOMIC);
230 + if (new_skb == 0) {
231 + printk(KERN_ERR "PPP: no memory (comp pkt)\n");
234 - skb = pad_compress_skb(ppp, skb);
236 + if (ppp->dev->hard_header_len > PPP_HDRLEN)
237 + skb_reserve(new_skb,
238 + ppp->dev->hard_header_len - PPP_HDRLEN);
240 + /* compressor still expects A/C bytes in hdr */
241 + len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
242 + new_skb->data, skb->len + 2,
243 + ppp->dev->mtu + PPP_HDRLEN);
244 + if (len > 0 && (ppp->flags & SC_CCP_UP)) {
248 + skb_pull(skb, 2); /* pull off A/C bytes */
249 + } else if (len == 0) {
252 + * MPPE requires that we do not send unencrypted
253 + * frames. The compressor will return -1 if we
254 + * should drop the frame. We cannot simply test
255 + * the compress_proto because MPPE and MPPC share
258 + printk(KERN_ERR "ppp: compressor dropped pkt\n");
259 + kfree_skb(new_skb);
265 @@ -1670,9 +1666,6 @@ ppp_receive_nonmp_frame(struct ppp *ppp,
266 (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
267 skb = ppp_decompress_frame(ppp, skb);
269 - if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
272 proto = PPP_PROTO(skb);
275 @@ -1852,7 +1845,14 @@ ppp_decompress_frame(struct ppp *ppp, st
279 - ppp->rstate |= SC_DC_ERROR;
280 + if (ppp->rcomp->compress_proto != CI_MPPE
281 + && ppp->rcomp->compress_proto != CI_LZS) {
283 + * If decompression protocol isn't MPPE/MPPC or LZS, we set
284 + * SC_DC_ERROR flag and wait for CCP_RESETACK
286 + ppp->rstate |= SC_DC_ERROR;
288 ppp_receive_error(ppp);
291 @@ -2566,6 +2566,7 @@ ppp_create_interface(struct net *net, in
292 ppp = netdev_priv(dev);
295 + ppp->mru_alloc = PPP_MRU;
296 init_ppp_file(&ppp->file, INTERFACE);
297 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
298 for (i = 0; i < NUM_NP; ++i)
300 +++ b/drivers/net/ppp_mppe_mppc.c
303 + * ppp_mppe_mppc.c - MPPC/MPPE "compressor/decompressor" module.
305 + * Copyright (c) 1994 Árpád Magosányi <mag@bunuel.tii.matav.hu>
306 + * Copyright (c) 1999 Tim Hockin, Cobalt Networks Inc. <thockin@cobaltnet.com>
307 + * Copyright (c) 2002-2004 Jan Dubiec <jdx@slackware.pl>
309 + * Permission to use, copy, modify, and distribute this software and its
310 + * documentation is hereby granted, provided that the above copyright
311 + * notice appears in all copies. This software is provided without any
312 + * warranty, express or implied.
314 + * The code is based on MPPE kernel module written by Árpád Magosányi and
315 + * Tim Hockin which can be found on http://planetmirror.com/pub/mppe/.
316 + * I have added MPPC and 56 bit session keys support in MPPE.
318 + * WARNING! Although this is open source code, its usage in some countries
319 + * (in particular in the USA) may violate Stac Inc. patent for MPPC.
321 + * ==FILEVERSION 20041123==
325 +#include <linux/init.h>
326 +#include <linux/module.h>
327 +#include <linux/mm.h>
328 +#include <linux/slab.h>
329 +#include <asm/scatterlist.h>
330 +#include <linux/vmalloc.h>
331 +#include <linux/crypto.h>
333 +#include <linux/ppp_defs.h>
334 +#include <linux/ppp-comp.h>
337 + * State for a mppc/mppe "(de)compressor".
339 +struct ppp_mppe_state {
340 + struct crypto_tfm *arc4_tfm;
341 + struct crypto_hash *sha1_tfm;
343 + u8 master_key[MPPE_MAX_KEY_LEN];
344 + u8 session_key[MPPE_MAX_KEY_LEN];
345 + u8 mppc; /* do we use compression (MPPC)? */
346 + u8 mppe; /* do we use encryption (MPPE)? */
347 + u8 keylen; /* key length in bytes */
348 + u8 bitkeylen; /* key length in bits */
349 + u16 ccount; /* coherency counter */
350 + u16 bits; /* MPPC/MPPE control bits */
351 + u8 stateless; /* do we use stateless mode? */
352 + u8 nextflushed; /* set A bit in the next outgoing packet;
353 + used only by compressor*/
354 + u8 flushexpected; /* drop packets until A bit is received;
355 + used only by decompressor*/
356 + u8 *hist; /* MPPC history */
357 + u16 *hash; /* Hash table; used only by compressor */
358 + u16 histptr; /* history "cursor" */
362 + struct compstat stats;
365 +#define MPPE_HIST_LEN 8192 /* MPPC history size */
366 +#define MPPE_MAX_CCOUNT 0x0FFF /* max. coherency counter value */
368 +#define MPPE_BIT_FLUSHED 0x80 /* bit A */
369 +#define MPPE_BIT_RESET 0x40 /* bit B */
370 +#define MPPE_BIT_COMP 0x20 /* bit C */
371 +#define MPPE_BIT_ENCRYPTED 0x10 /* bit D */
373 +#define MPPE_SALT0 0xD1 /* values used in MPPE key derivation */
374 +#define MPPE_SALT1 0x26 /* according to RFC3079 */
375 +#define MPPE_SALT2 0x9E
377 +#define MPPE_CCOUNT(x) ((((x)[4] & 0x0f) << 8) + (x)[5])
378 +#define MPPE_BITS(x) ((x)[4] & 0xf0)
379 +#define MPPE_CTRLHI(x) ((((x)->ccount & 0xf00)>>8)|((x)->bits))
380 +#define MPPE_CTRLLO(x) ((x)->ccount & 0xff)
383 + * Kernel Crypto API needs its arguments to be in kmalloc'd memory, not in the
384 + * module static data area. That means sha_pad needs to be kmalloc'd. It is done
385 + * in mppe_module_init(). This has been pointed out on 30th July 2004 by Oleg
386 + * Makarenko on pptpclient-devel mailing list.
388 +#define SHA1_PAD_SIZE 40
390 + unsigned char sha_pad1[SHA1_PAD_SIZE];
391 + unsigned char sha_pad2[SHA1_PAD_SIZE];
393 +static struct sha_pad *sha_pad;
396 +setup_sg(struct scatterlist *sg, const void *address, unsigned int length)
398 + sg[0].page_link = virt_to_page(address);
399 + sg[0].offset = offset_in_page(address);
400 + sg[0].length = length;
405 +arc4_setkey(struct ppp_mppe_state *state, const unsigned char *key,
406 + const unsigned int keylen)
408 + crypto_cipher_setkey(state->arc4_tfm, key, keylen);
412 +arc4_encrypt(struct ppp_mppe_state *state, const unsigned char *in,
413 + const unsigned int len, unsigned char *out)
416 + for (i = 0; i < len; i++)
418 + crypto_cipher_encrypt_one(state->arc4_tfm, out+i, in+i);
423 +#define arc4_decrypt arc4_encrypt
426 + * Key Derivation, from RFC 3078, RFC 3079.
427 + * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
430 +get_new_key_from_sha(struct ppp_mppe_state *state, unsigned char *interim_key)
432 + struct hash_desc desc;
433 + struct scatterlist sg[4];
434 + unsigned int nbytes;
437 + nbytes = setup_sg(&sg[0], state->master_key, state->keylen);
438 + nbytes += setup_sg(&sg[1], sha_pad->sha_pad1,
439 + sizeof(sha_pad->sha_pad1));
440 + nbytes += setup_sg(&sg[2], state->session_key, state->keylen);
441 + nbytes += setup_sg(&sg[3], sha_pad->sha_pad2,
442 + sizeof(sha_pad->sha_pad2));
444 + desc.tfm = state->sha1_tfm;
447 + crypto_hash_digest(&desc, sg, nbytes, state->sha1_digest);
448 + memcpy(interim_key, state->sha1_digest, state->keylen);
452 +mppe_change_key(struct ppp_mppe_state *state, int initialize)
454 + unsigned char interim_key[MPPE_MAX_KEY_LEN];
456 + get_new_key_from_sha(state, interim_key);
458 + memcpy(state->session_key, interim_key, state->keylen);
460 + arc4_setkey(state, interim_key, state->keylen);
461 + arc4_encrypt(state, interim_key, state->keylen, state->session_key);
463 + if (state->keylen == 8) {
464 + if (state->bitkeylen == 40) {
465 + state->session_key[0] = MPPE_SALT0;
466 + state->session_key[1] = MPPE_SALT1;
467 + state->session_key[2] = MPPE_SALT2;
469 + state->session_key[0] = MPPE_SALT0;
472 + arc4_setkey(state, state->session_key, state->keylen);
475 +/* increase 12-bit coherency counter */
477 +mppe_increase_ccount(struct ppp_mppe_state *state)
479 + state->ccount = (state->ccount + 1) & MPPE_MAX_CCOUNT;
481 + if (state->stateless) {
482 + mppe_change_key(state, 0);
483 + state->nextflushed = 1;
485 + if ((state->ccount & 0xff) == 0xff) {
486 + mppe_change_key(state, 0);
492 +/* allocate space for a MPPE/MPPC (de)compressor. */
493 +/* comp != 0 -> init compressor */
494 +/* comp = 0 -> init decompressor */
496 +mppe_alloc(unsigned char *options, int opt_len, int comp)
498 + struct ppp_mppe_state *state;
499 + unsigned int digestsize;
502 + fname = comp ? "mppe_comp_alloc" : "mppe_decomp_alloc";
505 + * Hack warning - additionally to the standard MPPC/MPPE configuration
506 + * options, pppd passes to the (de)copressor 8 or 16 byte session key.
507 + * Therefore options[1] contains MPPC/MPPE configuration option length
508 + * (CILEN_MPPE = 6), but the real options length, depending on the key
509 + * length, is 6+8 or 6+16.
511 + if (opt_len < CILEN_MPPE) {
512 + printk(KERN_WARNING "%s: wrong options length: %u\n", fname, opt_len);
516 + if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
517 + (options[2] & ~MPPE_STATELESS) != 0 ||
518 + options[3] != 0 || options[4] != 0 ||
519 + (options[5] & ~(MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
520 + (options[5] & (MPPE_128BIT|MPPE_56BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
521 + printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
522 + "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname, options[0],
523 + options[1], options[2], options[3], options[4], options[5]);
527 + state = (struct ppp_mppe_state *)kmalloc(sizeof(*state), GFP_KERNEL);
528 + if (state == NULL) {
529 + printk(KERN_ERR "%s: cannot allocate space for %scompressor\n", fname,
533 + memset(state, 0, sizeof(struct ppp_mppe_state));
535 + state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */
536 + state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
537 + MPPE_40BIT); /* Do we use MPPE? */
540 + /* allocate MPPC history */
541 + state->hist = (u8*)vmalloc(2*MPPE_HIST_LEN*sizeof(u8));
542 + if (state->hist == NULL) {
544 + printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
549 + /* allocate hashtable for MPPC compressor */
551 + state->hash = (u16*)vmalloc(MPPE_HIST_LEN*sizeof(u16));
552 + if (state->hash == NULL) {
553 + vfree(state->hist);
555 + printk(KERN_ERR "%s: cannot allocate space for MPPC history\n",
562 + if (state->mppe) { /* specific for MPPE */
563 + /* Load ARC4 algorithm */
564 + state->arc4_tfm = crypto_alloc_base("arc4", 0, 0);
565 + if (state->arc4_tfm == NULL) {
567 + vfree(state->hash);
569 + vfree(state->hist);
572 + printk(KERN_ERR "%s: cannot load ARC4 module\n", fname);
576 + /* Load SHA1 algorithm */
577 + state->sha1_tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
578 + if (state->sha1_tfm == NULL) {
579 + crypto_free_tfm(state->arc4_tfm);
581 + vfree(state->hash);
583 + vfree(state->hist);
586 + printk(KERN_ERR "%s: cannot load SHA1 module\n", fname);
590 + digestsize = crypto_hash_digestsize(state->sha1_tfm);
591 + if (digestsize < MPPE_MAX_KEY_LEN) {
592 + crypto_free_hash(state->sha1_tfm);
593 + crypto_free_tfm(state->arc4_tfm);
595 + vfree(state->hash);
597 + vfree(state->hist);
600 + printk(KERN_ERR "%s: CryptoAPI SHA1 digest size too small\n", fname);
603 + state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
604 + if (!state->sha1_digest) {
605 + crypto_free_hash(state->sha1_tfm);
606 + crypto_free_tfm(state->arc4_tfm);
608 + vfree(state->hash);
610 + vfree(state->hist);
613 + printk(KERN_ERR "%s: cannot allocate space for SHA1 digest\n", fname);
616 + memcpy(state->master_key, options+CILEN_MPPE, MPPE_MAX_KEY_LEN);
617 + memcpy(state->session_key, state->master_key, MPPE_MAX_KEY_LEN);
618 + /* initial key generation is done in mppe_init() */
621 + return (void *) state;
625 +mppe_comp_alloc(unsigned char *options, int opt_len)
627 + return mppe_alloc(options, opt_len, 1);
631 +mppe_decomp_alloc(unsigned char *options, int opt_len)
633 + return mppe_alloc(options, opt_len, 0);
636 +/* cleanup the (de)compressor */
638 +mppe_comp_free(void *arg)
640 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
642 + if (state != NULL) {
644 + if (state->sha1_digest != NULL)
645 + kfree(state->sha1_digest);
646 + if (state->sha1_tfm != NULL)
647 + crypto_free_hash(state->sha1_tfm);
648 + if (state->arc4_tfm != NULL)
649 + crypto_free_tfm(state->arc4_tfm);
651 + if (state->hist != NULL)
652 + vfree(state->hist);
653 + if (state->hash != NULL)
654 + vfree(state->hash);
659 +/* init MPPC/MPPE (de)compresor */
660 +/* comp != 0 -> init compressor */
661 +/* comp = 0 -> init decompressor */
663 +mppe_init(void *arg, unsigned char *options, int opt_len, int unit,
664 + int hdrlen, int mru, int debug, int comp)
666 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
669 + fname = comp ? "mppe_comp_init" : "mppe_decomp_init";
671 + if (opt_len < CILEN_MPPE) {
673 + printk(KERN_WARNING "%s: wrong options length: %u\n",
678 + if (options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
679 + (options[2] & ~MPPE_STATELESS) != 0 ||
680 + options[3] != 0 || options[4] != 0 ||
681 + (options[5] & ~(MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) != 0 ||
682 + (options[5] & (MPPE_56BIT|MPPE_128BIT|MPPE_40BIT|MPPE_MPPC)) == 0) {
684 + printk(KERN_WARNING "%s: options rejected: o[0]=%02x, o[1]=%02x, "
685 + "o[2]=%02x, o[3]=%02x, o[4]=%02x, o[5]=%02x\n", fname,
686 + options[0], options[1], options[2], options[3], options[4],
691 + if ((options[5] & ~MPPE_MPPC) != MPPE_128BIT &&
692 + (options[5] & ~MPPE_MPPC) != MPPE_56BIT &&
693 + (options[5] & ~MPPE_MPPC) != MPPE_40BIT &&
694 + (options[5] & MPPE_MPPC) != MPPE_MPPC) {
696 + printk(KERN_WARNING "%s: don't know what to do: o[5]=%02x\n",
697 + fname, options[5]);
701 + state->mppc = options[5] & MPPE_MPPC; /* Do we use MPPC? */
702 + state->mppe = options[5] & (MPPE_128BIT | MPPE_56BIT |
703 + MPPE_40BIT); /* Do we use MPPE? */
704 + state->stateless = options[2] & MPPE_STATELESS; /* Do we use stateless mode? */
706 + switch (state->mppe) {
707 + case MPPE_40BIT: /* 40 bit key */
709 + state->bitkeylen = 40;
711 + case MPPE_56BIT: /* 56 bit key */
713 + state->bitkeylen = 56;
715 + case MPPE_128BIT: /* 128 bit key */
716 + state->keylen = 16;
717 + state->bitkeylen = 128;
721 + state->bitkeylen = 0;
724 + state->ccount = MPPE_MAX_CCOUNT;
726 + state->unit = unit;
727 + state->debug = debug;
728 + state->histptr = MPPE_HIST_LEN;
729 + if (state->mppc) { /* reset history if MPPC was negotiated */
730 + memset(state->hist, 0, 2*MPPE_HIST_LEN*sizeof(u8));
733 + if (state->mppe) { /* generate initial session keys */
734 + mppe_change_key(state, 1);
737 + if (comp) { /* specific for compressor */
738 + state->nextflushed = 1;
739 + } else { /* specific for decompressor */
741 + state->flushexpected = 1;
748 +mppe_comp_init(void *arg, unsigned char *options, int opt_len, int unit,
749 + int hdrlen, int debug)
751 + return mppe_init(arg, options, opt_len, unit, hdrlen, 0, debug, 1);
756 +mppe_decomp_init(void *arg, unsigned char *options, int opt_len, int unit,
757 + int hdrlen, int mru, int debug)
759 + return mppe_init(arg, options, opt_len, unit, hdrlen, mru, debug, 0);
763 +mppe_comp_reset(void *arg)
765 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
768 + printk(KERN_DEBUG "%s%d: resetting MPPC/MPPE compressor\n",
769 + __FUNCTION__, state->unit);
771 + state->nextflushed = 1;
773 + arc4_setkey(state, state->session_key, state->keylen);
777 +mppe_decomp_reset(void *arg)
779 + /* When MPPC/MPPE is in use, we shouldn't receive any CCP Reset-Ack.
780 + But when we receive such a packet, we just ignore it. */
785 +mppe_stats(void *arg, struct compstat *stats)
787 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
789 + *stats = state->stats;
792 +/***************************/
793 +/**** Compression stuff ****/
794 +/***************************/
795 +/* inserts 1 to 8 bits into the output buffer */
796 +static inline void putbits8(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
802 + *buf = *buf | (val & 0xff);
812 + *buf = *buf | ((val >> 8) & 0xff);
813 + *(++buf) = val & 0xff;
817 +/* inserts 9 to 16 bits into the output buffer */
818 +static inline void putbits16(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
825 + *buf = *buf | ((val >> 8) & 0xff);
826 + *(++buf) = val & 0xff;
834 + *l = 16 - n + (*l);
836 + *buf = *buf | ((val >> 16) & 0xff);
837 + *(++buf) = (val >> 8) & 0xff;
838 + *(++buf) = val & 0xff;
842 +/* inserts 17 to 24 bits into the output buffer */
843 +static inline void putbits24(u8 *buf, u32 val, const u32 n, u32 *i, u32 *l)
846 + if (*l >= n - 16) {
848 + *l = 16 - n + (*l);
850 + *buf = *buf | ((val >> 16) & 0xff);
851 + *(++buf) = (val >> 8) & 0xff;
852 + *(++buf) = val & 0xff;
859 + (*i)++; (*i)++; (*i)++;
860 + *l = 24 - n + (*l);
862 + *buf = *buf | ((val >> 24) & 0xff);
863 + *(++buf) = (val >> 16) & 0xff;
864 + *(++buf) = (val >> 8) & 0xff;
865 + *(++buf) = val & 0xff;
870 +mppc_compress(struct ppp_mppe_state *state, unsigned char *ibuf,
871 + unsigned char *obuf, int isize, int osize)
873 + u32 olen, off, len, idx, i, l;
874 + u8 *hist, *sbuf, *p, *q, *r, *s;
877 + At this point, to avoid possible buffer overflow caused by packet
878 + expansion during/after compression, we should make sure that
879 + osize >= (((isize*9)/8)+1)+2, but we don't do that because in
880 + ppp_generic.c we simply allocate bigger obuf.
882 + Maximum MPPC packet expansion is 12.5%. This is the worst case when
883 + all octets in the input buffer are >= 0x80 and we cannot find any
884 + repeated tokens. Additionally we have to reserve 2 bytes for MPPE/MPPC
885 + status bits and coherency counter.
888 + hist = state->hist + MPPE_HIST_LEN;
889 + /* check if there is enough room at the end of the history */
890 + if (state->histptr + isize >= 2*MPPE_HIST_LEN) {
891 + state->bits |= MPPE_BIT_RESET;
892 + state->histptr = MPPE_HIST_LEN;
893 + memcpy(state->hist, hist, MPPE_HIST_LEN);
895 + /* add packet to the history; isize must be <= MPPE_HIST_LEN */
896 + sbuf = state->hist + state->histptr;
897 + memcpy(sbuf, ibuf, isize);
898 + state->histptr += isize;
900 + /* compress data */
902 + *obuf = olen = i = 0;
904 + while (i < isize - 2) {
906 + idx = ((40543*((((s[0]<<4)^s[1])<<4)^s[2]))>>4) & 0x1fff;
907 + p = hist + state->hash[idx];
908 + state->hash[idx] = (u16) (s - hist);
910 + if (off > MPPE_HIST_LEN - 1 || off < 1 || *p++ != *s++ || *p++ != *s++ ||
912 + /* no match found; encode literal byte */
913 + if (ibuf[i] < 0x80) { /* literal byte < 0x80 */
914 + putbits8(obuf, (u32) ibuf[i], 8, &olen, &l);
915 + } else { /* literal byte >= 0x80 */
916 + putbits16(obuf, (u32) (0x100|(ibuf[i]&0x7f)), 9, &olen, &l);
922 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
923 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
924 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
925 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
926 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
927 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
928 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
929 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
930 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
931 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
932 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
933 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
934 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
935 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
936 + *p++ != *s++ || *p++ != *s++ || *p++ != *s++ || *p++ != *s++ ||
940 + while((*p++ == *s++) && (s < r) && (p < q));
943 + while((*p++ == *s++) && (s < r) && (p < q));
948 + /* at least 3 character match found; code data */
949 + /* encode offset */
950 + if (off < 64) { /* 10-bit offset; 0 <= offset < 64 */
951 + putbits16(obuf, 0x3c0|off, 10, &olen, &l);
952 + } else if (off < 320) { /* 12-bit offset; 64 <= offset < 320 */
953 + putbits16(obuf, 0xe00|(off-64), 12, &olen, &l);
954 + } else if (off < 8192) { /* 16-bit offset; 320 <= offset < 8192 */
955 + putbits16(obuf, 0xc000|(off-320), 16, &olen, &l);
957 + /* This shouldn't happen; we return 0 what means "packet expands",
958 + and we send packet uncompressed. */
960 + printk(KERN_DEBUG "%s%d: wrong offset value: %d\n",
961 + __FUNCTION__, state->unit, off);
964 + /* encode length of match */
965 + if (len < 4) { /* length = 3 */
966 + putbits8(obuf, 0, 1, &olen, &l);
967 + } else if (len < 8) { /* 4 <= length < 8 */
968 + putbits8(obuf, 0x08|(len&0x03), 4, &olen, &l);
969 + } else if (len < 16) { /* 8 <= length < 16 */
970 + putbits8(obuf, 0x30|(len&0x07), 6, &olen, &l);
971 + } else if (len < 32) { /* 16 <= length < 32 */
972 + putbits8(obuf, 0xe0|(len&0x0f), 8, &olen, &l);
973 + } else if (len < 64) { /* 32 <= length < 64 */
974 + putbits16(obuf, 0x3c0|(len&0x1f), 10, &olen, &l);
975 + } else if (len < 128) { /* 64 <= length < 128 */
976 + putbits16(obuf, 0xf80|(len&0x3f), 12, &olen, &l);
977 + } else if (len < 256) { /* 128 <= length < 256 */
978 + putbits16(obuf, 0x3f00|(len&0x7f), 14, &olen, &l);
979 + } else if (len < 512) { /* 256 <= length < 512 */
980 + putbits16(obuf, 0xfe00|(len&0xff), 16, &olen, &l);
981 + } else if (len < 1024) { /* 512 <= length < 1024 */
982 + putbits24(obuf, 0x3fc00|(len&0x1ff), 18, &olen, &l);
983 + } else if (len < 2048) { /* 1024 <= length < 2048 */
984 + putbits24(obuf, 0xff800|(len&0x3ff), 20, &olen, &l);
985 + } else if (len < 4096) { /* 2048 <= length < 4096 */
986 + putbits24(obuf, 0x3ff000|(len&0x7ff), 22, &olen, &l);
987 + } else if (len < 8192) { /* 4096 <= length < 8192 */
988 + putbits24(obuf, 0xffe000|(len&0xfff), 24, &olen, &l);
990 + /* This shouldn't happen; we return 0 what means "packet expands",
991 + and send packet uncompressed. */
993 + printk(KERN_DEBUG "%s%d: wrong length of match value: %d\n",
994 + __FUNCTION__, state->unit, len);
999 + /* Add remaining octets to the output */
1000 + while(isize - i > 0) {
1001 + if (ibuf[i] < 0x80) { /* literal byte < 0x80 */
1002 + putbits8(obuf, (u32) ibuf[i++], 8, &olen, &l);
1003 + } else { /* literal byte >= 0x80 */
1004 + putbits16(obuf, (u32) (0x100|(ibuf[i++]&0x7f)), 9, &olen, &l);
1007 + /* Reset unused bits of the last output octet */
1008 + if ((l != 0) && (l != 8)) {
1009 + putbits8(obuf, 0, l, &olen, &l);
1012 + return (int) olen;
1016 +mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
1017 + int isize, int osize)
1019 + struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
1020 + int proto, olen, complen, off;
1021 + unsigned char *wptr;
1023 + /* Check that the protocol is in the range we handle. */
1024 + proto = PPP_PROTOCOL(ibuf);
1025 + if (proto < 0x0021 || proto > 0x00fa)
1029 + /* Copy over the PPP header */
1030 + wptr[0] = PPP_ADDRESS(ibuf);
1031 + wptr[1] = PPP_CONTROL(ibuf);
1032 + wptr[2] = PPP_COMP >> 8;
1033 + wptr[3] = PPP_COMP;
1034 + wptr += PPP_HDRLEN + (MPPE_OVHD / 2); /* Leave two octets for MPPE/MPPC bits */
1037 + * In ver. 0.99 protocol field was compressed. Deflate and BSD compress
1038 + * do PFC before actual compression, RCF2118 and RFC3078 are not precise
1039 + * on this topic so I decided to do PFC. Unfortunately this change caused
1040 + * incompatibility with older/other MPPE/MPPC modules. I have received
1041 + * a lot of complaints from unexperienced users so I have decided to revert
1042 + * to previous state, i.e. the protocol field is sent uncompressed now.
1043 + * Although this may be changed in the future.
1045 + * Receiving side (mppe_decompress()) still accepts packets with compressed
1046 + * and uncompressed protocol field so you shouldn't get "Unsupported protocol
1047 + * 0x2145 received" messages anymore.
1049 + //off = (proto > 0xff) ? 2 : 3; /* PFC - skip first protocol byte if 0 */
1054 + mppe_increase_ccount(state);
1056 + if (state->nextflushed) {
1057 + state->bits |= MPPE_BIT_FLUSHED;
1058 + state->nextflushed = 0;
1059 + if (state->mppe && !state->stateless) {
1061 + * If this is the flag packet, the key has been already changed in
1062 + * mppe_increase_ccount() so we dont't do it once again.
1064 + if ((state->ccount & 0xff) != 0xff) {
1065 + arc4_setkey(state, state->session_key, state->keylen);
1068 + if (state->mppc) { /* reset history */
1069 + state->bits |= MPPE_BIT_RESET;
1070 + state->histptr = MPPE_HIST_LEN;
1071 + memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
1075 + if (state->mppc && !state->mppe) { /* Do only compression */
1076 + complen = mppc_compress(state, ibuf, wptr, isize - off,
1077 + osize - PPP_HDRLEN - (MPPE_OVHD / 2));
1079 + * TODO: Implement an heuristics to handle packet expansion in a smart
1080 + * way. Now, when a packet expands, we send it as uncompressed and
1081 + * when next packet is sent we have to reset compressor's history.
1082 + * Maybe it would be better to send such packet as compressed in order
1083 + * to keep history's continuity.
1085 + if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
1087 + /* packet expands */
1088 + state->nextflushed = 1;
1089 + memcpy(wptr, ibuf, isize - off);
1090 + olen = isize - (off - 2) + MPPE_OVHD;
1091 + (state->stats).inc_bytes += olen;
1092 + (state->stats).inc_packets++;
1094 + state->bits |= MPPE_BIT_COMP;
1095 + olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
1096 + (state->stats).comp_bytes += olen;
1097 + (state->stats).comp_packets++;
1099 + } else { /* Do encryption with or without compression */
1100 + state->bits |= MPPE_BIT_ENCRYPTED;
1101 + if (!state->mppc && state->mppe) { /* Do only encryption */
1102 + /* read from ibuf, write to wptr, adjust for PPP_HDRLEN */
1103 + arc4_encrypt(state, ibuf, isize - off, wptr);
1104 + olen = isize - (off - 2) + MPPE_OVHD;
1105 + (state->stats).inc_bytes += olen;
1106 + (state->stats).inc_packets++;
1107 + } else { /* Do compression and then encryption - RFC3078 */
1108 + complen = mppc_compress(state, ibuf, wptr, isize - off,
1109 + osize - PPP_HDRLEN - (MPPE_OVHD / 2));
1111 + * TODO: Implement an heuristics to handle packet expansion in a smart
1112 + * way. Now, when a packet expands, we send it as uncompressed and
1113 + * when next packet is sent we have to reset compressor's history.
1114 + * Maybe it would be good to send such packet as compressed in order
1115 + * to keep history's continuity.
1117 + if ((complen > isize) || (complen > osize - PPP_HDRLEN) ||
1119 + /* packet expands */
1120 + state->nextflushed = 1;
1121 + arc4_encrypt(state, ibuf, isize - off, wptr);
1122 + olen = isize - (off - 2) + MPPE_OVHD;
1123 + (state->stats).inc_bytes += olen;
1124 + (state->stats).inc_packets++;
1126 + state->bits |= MPPE_BIT_COMP;
1127 + /* Hack warning !!! RC4 implementation which we use does
1128 + encryption "in place" - it means that input and output
1129 + buffers can be *the same* memory area. Therefore we don't
1130 + need to use a temporary buffer. But be careful - other
1131 + implementations don't have to be so nice.
1132 + I used to use ibuf as temporary buffer here, but it led
1133 + packet sniffers into error. Thanks to Wilfried Weissmann
1134 + for pointing that. */
1135 + arc4_encrypt(state, wptr, complen, wptr);
1136 + olen = complen + PPP_HDRLEN + (MPPE_OVHD / 2);
1137 + (state->stats).comp_bytes += olen;
1138 + (state->stats).comp_packets++;
1143 + /* write status bits and coherency counter into the output buffer */
1144 + wptr = obuf + PPP_HDRLEN;
1145 + wptr[0] = MPPE_CTRLHI(state);
1146 + wptr[1] = MPPE_CTRLLO(state);
1150 + (state->stats).unc_bytes += isize;
1151 + (state->stats).unc_packets++;
1156 +/***************************/
1157 +/*** Decompression stuff ***/
1158 +/***************************/
1159 +static inline u32 getbits(const u8 *buf, const u32 n, u32 *i, u32 *l)
1161 + static const u32 m[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1167 + res = (buf[*i] & m[ol]) >> (*l);
1173 + *l = 8 - n + (*l);
1174 + res = (buf[(*i)++] & m[ol]) << 8;
1175 + res = (res | buf[*i]) >> (*l);
1181 +static inline u32 getbyte(const u8 *buf, const u32 i, const u32 l)
1186 + return (((buf[i] << 8) | buf[i+1]) >> l) & 0xff;
1190 +static inline void lamecopy(u8 *dst, u8 *src, u32 len)
1197 +mppc_decompress(struct ppp_mppe_state *state, unsigned char *ibuf,
1198 + unsigned char *obuf, int isize, int osize)
1200 + u32 olen, off, len, bits, val, sig, i, l;
1203 + history = state->hist + state->histptr;
1204 + olen = len = i = 0;
1207 + while (bits >= 8) {
1208 + val = getbyte(ibuf, i++, l);
1209 + if (val < 0x80) { /* literal byte < 0x80 */
1210 + if (state->histptr < 2*MPPE_HIST_LEN) {
1211 + /* copy uncompressed byte to the history */
1212 + (state->hist)[(state->histptr)++] = (u8) val;
1214 + /* buffer overflow; drop packet */
1216 + printk(KERN_ERR "%s%d: trying to write outside history "
1217 + "buffer\n", __FUNCTION__, state->unit);
1218 + return DECOMP_ERROR;
1226 + if (sig == 0x80) { /* literal byte >= 0x80 */
1227 + if (state->histptr < 2*MPPE_HIST_LEN) {
1228 + /* copy uncompressed byte to the history */
1229 + (state->hist)[(state->histptr)++] =
1230 + (u8) (0x80|((val&0x3f)<<1)|getbits(ibuf, 1 , &i ,&l));
1232 + /* buffer overflow; drop packet */
1234 + printk(KERN_ERR "%s%d: trying to write outside history "
1235 + "buffer\n", __FUNCTION__, state->unit);
1236 + return DECOMP_ERROR;
1243 + /* Not a literal byte so it must be an (offset,length) pair */
1244 + /* decode offset */
1246 + if (sig == 0xf0) { /* 10-bit offset; 0 <= offset < 64 */
1247 + off = (((val&0x0f)<<2)|getbits(ibuf, 2 , &i ,&l));
1250 + if (sig == 0xe0) { /* 12-bit offset; 64 <= offset < 320 */
1251 + off = ((((val&0x0f)<<4)|getbits(ibuf, 4 , &i ,&l))+64);
1254 + if ((sig&0xe0) == 0xc0) {/* 16-bit offset; 320 <= offset < 8192 */
1255 + off = ((((val&0x1f)<<8)|getbyte(ibuf, i++, l))+320);
1257 + if (off > MPPE_HIST_LEN - 1) {
1259 + printk(KERN_DEBUG "%s%d: too big offset value: %d\n",
1260 + __FUNCTION__, state->unit, off);
1261 + return DECOMP_ERROR;
1263 + } else { /* this shouldn't happen */
1265 + printk(KERN_DEBUG "%s%d: cannot decode offset value\n",
1266 + __FUNCTION__, state->unit);
1267 + return DECOMP_ERROR;
1271 + /* decode length of match */
1272 + val = getbyte(ibuf, i, l);
1273 + if ((val & 0x80) == 0x00) { /* len = 3 */
1276 + getbits(ibuf, 1 , &i ,&l);
1277 + } else if ((val & 0xc0) == 0x80) { /* 4 <= len < 8 */
1278 + len = 0x04 | ((val>>4) & 0x03);
1280 + getbits(ibuf, 4 , &i ,&l);
1281 + } else if ((val & 0xe0) == 0xc0) { /* 8 <= len < 16 */
1282 + len = 0x08 | ((val>>2) & 0x07);
1284 + getbits(ibuf, 6 , &i ,&l);
1285 + } else if ((val & 0xf0) == 0xe0) { /* 16 <= len < 32 */
1286 + len = 0x10 | (val & 0x0f);
1291 + val = (val << 8) | getbyte(ibuf, ++i, l);
1292 + if ((val & 0xf800) == 0xf000) { /* 32 <= len < 64 */
1293 + len = 0x0020 | ((val >> 6) & 0x001f);
1295 + getbits(ibuf, 2 , &i ,&l);
1296 + } else if ((val & 0xfc00) == 0xf800) { /* 64 <= len < 128 */
1297 + len = 0x0040 | ((val >> 4) & 0x003f);
1299 + getbits(ibuf, 4 , &i ,&l);
1300 + } else if ((val & 0xfe00) == 0xfc00) { /* 128 <= len < 256 */
1301 + len = 0x0080 | ((val >> 2) & 0x007f);
1303 + getbits(ibuf, 6 , &i ,&l);
1304 + } else if ((val & 0xff00) == 0xfe00) { /* 256 <= len < 512 */
1305 + len = 0x0100 | (val & 0x00ff);
1310 + val = (val << 8) | getbyte(ibuf, ++i, l);
1311 + if ((val & 0xff8000) == 0xff0000) { /* 512 <= len < 1024 */
1312 + len = 0x000200 | ((val >> 6) & 0x0001ff);
1314 + getbits(ibuf, 2 , &i ,&l);
1315 + } else if ((val & 0xffc000) == 0xff8000) {/* 1024 <= len < 2048 */
1316 + len = 0x000400 | ((val >> 4) & 0x0003ff);
1318 + getbits(ibuf, 4 , &i ,&l);
1319 + } else if ((val & 0xffe000) == 0xffc000) {/* 2048 <= len < 4096 */
1320 + len = 0x000800 | ((val >> 2) & 0x0007ff);
1322 + getbits(ibuf, 6 , &i ,&l);
1323 + } else if ((val & 0xfff000) == 0xffe000) {/* 4096 <= len < 8192 */
1324 + len = 0x001000 | (val & 0x000fff);
1327 + } else { /* this shouldn't happen */
1329 + printk(KERN_DEBUG "%s%d: wrong length code: 0x%X\n",
1330 + __FUNCTION__, state->unit, val);
1331 + return DECOMP_ERROR;
1335 + s = state->hist + state->histptr;
1336 + state->histptr += len;
1338 + if (state->histptr < 2*MPPE_HIST_LEN) {
1339 + /* copy uncompressed bytes to the history */
1341 + /* In some cases len may be greater than off. It means that memory
1342 + * areas pointed by s and s-off overlap. I had used memmove() here
1343 + * because I thought that it acts as libc's version. Unfortunately,
1344 + * I was wrong. :-) I got strange errors sometimes. Wilfried suggested
1345 + * using of byte by byte copying here and strange errors disappeared.
1347 + lamecopy(s, s - off, len);
1349 + /* buffer overflow; drop packet */
1351 + printk(KERN_ERR "%s%d: trying to write outside history "
1352 + "buffer\n", __FUNCTION__, state->unit);
1353 + return DECOMP_ERROR;
1357 + /* Do PFC decompression */
1359 + if ((history[0] & 0x01) != 0) {
1365 + if (len <= osize) {
1366 + /* copy uncompressed packet to the output buffer */
1367 + memcpy(obuf, history, olen);
1369 + /* buffer overflow; drop packet */
1371 + printk(KERN_ERR "%s%d: too big uncompressed packet: %d\n",
1372 + __FUNCTION__, state->unit, len + (PPP_HDRLEN / 2));
1373 + return DECOMP_ERROR;
1380 +mppe_decompress(void *arg, unsigned char *ibuf, int isize,
1381 + unsigned char *obuf, int osize)
1383 + struct ppp_mppe_state *state = (struct ppp_mppe_state *)arg;
1384 + int seq, bits, uncomplen;
1386 + if (isize <= PPP_HDRLEN + MPPE_OVHD) {
1387 + if (state->debug) {
1388 + printk(KERN_DEBUG "%s%d: short packet (len=%d)\n", __FUNCTION__,
1389 + state->unit, isize);
1391 + return DECOMP_ERROR;
1394 + /* Get coherency counter and control bits from input buffer */
1395 + seq = MPPE_CCOUNT(ibuf);
1396 + bits = MPPE_BITS(ibuf);
1398 + if (state->stateless) {
1399 + /* RFC 3078, sec 8.1. */
1400 + mppe_increase_ccount(state);
1401 + if ((seq != state->ccount) && state->debug)
1402 + printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1403 + __FUNCTION__, state->unit, seq, state->ccount);
1404 + while (seq != state->ccount)
1405 + mppe_increase_ccount(state);
1407 + /* RFC 3078, sec 8.2. */
1408 + if (state->flushexpected) { /* discard state */
1409 + if ((bits & MPPE_BIT_FLUSHED)) { /* we received expected FLUSH bit */
1410 + while (seq != state->ccount)
1411 + mppe_increase_ccount(state);
1412 + state->flushexpected = 0;
1413 + } else /* drop packet*/
1414 + return DECOMP_ERROR;
1415 + } else { /* normal state */
1416 + mppe_increase_ccount(state);
1417 + if (seq != state->ccount) {
1418 + /* Packet loss detected, enter the discard state. */
1420 + printk(KERN_DEBUG "%s%d: bad sequence number: %d, expected: %d\n",
1421 + __FUNCTION__, state->unit, seq, state->ccount);
1422 + state->flushexpected = 1;
1423 + return DECOMP_ERROR;
1426 + if (state->mppe && (bits & MPPE_BIT_FLUSHED)) {
1427 + arc4_setkey(state, state->session_key, state->keylen);
1431 + if (state->mppc && (bits & (MPPE_BIT_FLUSHED | MPPE_BIT_RESET))) {
1432 + state->histptr = MPPE_HIST_LEN;
1433 + if ((bits & MPPE_BIT_FLUSHED)) {
1434 + memset(state->hist + MPPE_HIST_LEN, 0, MPPE_HIST_LEN*sizeof(u8));
1436 + if ((bits & MPPE_BIT_RESET)) {
1437 + memcpy(state->hist, state->hist + MPPE_HIST_LEN, MPPE_HIST_LEN);
1441 + /* Fill in the first part of the PPP header. The protocol field
1442 + comes from the decompressed data. */
1443 + obuf[0] = PPP_ADDRESS(ibuf);
1444 + obuf[1] = PPP_CONTROL(ibuf);
1445 + obuf += PPP_HDRLEN / 2;
1447 + if (state->mppe) { /* process encrypted packet */
1448 + if ((bits & MPPE_BIT_ENCRYPTED)) {
1449 + /* OK, packet encrypted, so decrypt it */
1450 + if (state->mppc && (bits & MPPE_BIT_COMP)) {
1451 + /* Hack warning !!! RC4 implementation which we use does
1452 + decryption "in place" - it means that input and output
1453 + buffers can be *the same* memory area. Therefore we don't
1454 + need to use a temporary buffer. But be careful - other
1455 + implementations don't have to be so nice. */
1456 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1457 + PPP_HDRLEN - (MPPE_OVHD / 2), ibuf + PPP_HDRLEN +
1459 + uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1460 + (MPPE_OVHD / 2), obuf, isize -
1461 + PPP_HDRLEN - (MPPE_OVHD / 2),
1462 + osize - (PPP_HDRLEN / 2));
1463 + if (uncomplen == DECOMP_ERROR) {
1464 + state->flushexpected = 1;
1465 + return DECOMP_ERROR;
1467 + uncomplen += PPP_HDRLEN / 2;
1468 + (state->stats).comp_bytes += isize;
1469 + (state->stats).comp_packets++;
1471 + uncomplen = isize - MPPE_OVHD;
1472 + /* Decrypt the first byte in order to check if it is
1473 + compressed or uncompressed protocol field */
1474 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), 1, obuf);
1475 + /* Do PFC decompression */
1476 + if ((obuf[0] & 0x01) != 0) {
1477 + obuf[1] = obuf[0];
1482 + /* And finally, decrypt the rest of the frame. */
1483 + arc4_decrypt(state, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2) + 1,
1484 + isize - PPP_HDRLEN - (MPPE_OVHD / 2) - 1, obuf + 1);
1485 + (state->stats).inc_bytes += isize;
1486 + (state->stats).inc_packets++;
1488 + } else { /* this shouldn't happen */
1490 + printk(KERN_ERR "%s%d: encryption negotiated but not an "
1491 + "encrypted packet received\n", __FUNCTION__, state->unit);
1492 + mppe_change_key(state, 0);
1493 + state->flushexpected = 1;
1494 + return DECOMP_ERROR;
1497 + if (state->mppc) { /* no MPPE, only MPPC */
1498 + if ((bits & MPPE_BIT_COMP)) {
1499 + uncomplen = mppc_decompress(state, ibuf + PPP_HDRLEN +
1500 + (MPPE_OVHD / 2), obuf, isize -
1501 + PPP_HDRLEN - (MPPE_OVHD / 2),
1502 + osize - (PPP_HDRLEN / 2));
1503 + if (uncomplen == DECOMP_ERROR) {
1504 + state->flushexpected = 1;
1505 + return DECOMP_ERROR;
1507 + uncomplen += PPP_HDRLEN / 2;
1508 + (state->stats).comp_bytes += isize;
1509 + (state->stats).comp_packets++;
1511 + memcpy(obuf, ibuf + PPP_HDRLEN + (MPPE_OVHD / 2), isize -
1512 + PPP_HDRLEN - (MPPE_OVHD / 2));
1513 + uncomplen = isize - MPPE_OVHD;
1514 + (state->stats).inc_bytes += isize;
1515 + (state->stats).inc_packets++;
1517 + } else { /* this shouldn't happen */
1519 + printk(KERN_ERR "%s%d: error - not an MPPC or MPPE frame "
1520 + "received\n", __FUNCTION__, state->unit);
1521 + state->flushexpected = 1;
1522 + return DECOMP_ERROR;
1526 + (state->stats).unc_bytes += uncomplen;
1527 + (state->stats).unc_packets++;
1533 +/************************************************************
1534 + * Module interface table
1535 + ************************************************************/
1537 +/* These are in ppp_generic.c */
1538 +extern int ppp_register_compressor (struct compressor *cp);
1539 +extern void ppp_unregister_compressor (struct compressor *cp);
1542 + * Functions exported to ppp_generic.c.
1544 + * In case of MPPC/MPPE there is no need to process incompressible data
1545 + * because such a data is sent in MPPC/MPPE frame. Therefore the (*incomp)
1546 + * callback function isn't needed.
1548 +struct compressor ppp_mppe = {
1549 + .compress_proto = CI_MPPE,
1550 + .comp_alloc = mppe_comp_alloc,
1551 + .comp_free = mppe_comp_free,
1552 + .comp_init = mppe_comp_init,
1553 + .comp_reset = mppe_comp_reset,
1554 + .compress = mppe_compress,
1555 + .comp_stat = mppe_stats,
1556 + .decomp_alloc = mppe_decomp_alloc,
1557 + .decomp_free = mppe_comp_free,
1558 + .decomp_init = mppe_decomp_init,
1559 + .decomp_reset = mppe_decomp_reset,
1560 + .decompress = mppe_decompress,
1562 + .decomp_stat = mppe_stats,
1563 + .owner = THIS_MODULE
1566 +/************************************************************
1567 + * Module support routines
1568 + ************************************************************/
1570 +int __init mppe_module_init(void)
1574 + if (!(crypto_alg_available("arc4", 0) && crypto_alg_available("sha1", 0))) {
1575 + printk(KERN_ERR "Kernel doesn't provide ARC4 and/or SHA1 algorithms "
1576 + "required by MPPE/MPPC. Check CryptoAPI configuration.\n");
1580 + /* Allocate space for SHAPad1, SHAPad2 and ... */
1581 + sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
1582 + if (sha_pad == NULL)
1584 + /* ... initialize them */
1585 + memset(sha_pad->sha_pad1, 0x00, sizeof(sha_pad->sha_pad1));
1586 + memset(sha_pad->sha_pad2, 0xf2, sizeof(sha_pad->sha_pad2));
1588 + answer = ppp_register_compressor(&ppp_mppe);
1589 + if (answer == 0) {
1590 + printk(KERN_INFO "MPPE/MPPC encryption/compression module registered\n");
1595 +void __exit mppe_module_cleanup(void)
1598 + ppp_unregister_compressor(&ppp_mppe);
1599 + printk(KERN_INFO "MPPE/MPPC encryption/compression module unregistered\n");
1602 +module_init(mppe_module_init);
1603 +module_exit(mppe_module_cleanup);
1605 +MODULE_AUTHOR("Jan Dubiec <jdx@slackware.pl>");
1606 +MODULE_DESCRIPTION("MPPE/MPPC encryption/compression module for Linux");
1607 +MODULE_VERSION("1.3");
1608 +MODULE_LICENSE("Dual BSD/GPL");
1609 +MODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
1610 --- a/include/linux/crypto.h
1611 +++ b/include/linux/crypto.h
1612 @@ -309,7 +309,18 @@ int crypto_unregister_alg(struct crypto_
1614 * Algorithm query interface.
1616 +#ifdef CONFIG_CRYPTO
1617 +int crypto_alg_available(const char *name, u32 flags)
1618 + __deprecated_for_modules;
1619 int crypto_has_alg(const char *name, u32 type, u32 mask);
1621 +static int crypto_alg_available(const char *name, u32 flags)
1622 + __deprecated_for_modules;
1623 +static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
1630 * Transforms: user-instantiated objects which encapsulate algorithms
1631 @@ -1101,6 +1112,16 @@ static inline void crypto_cipher_decrypt
1635 +void crypto_digest_init(struct crypto_tfm *tfm) __deprecated_for_modules;
1636 +void crypto_digest_update(struct crypto_tfm *tfm,
1637 + struct scatterlist *sg, unsigned int nsg)
1638 + __deprecated_for_modules;
1639 +void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
1640 + __deprecated_for_modules;
1641 +void crypto_digest_digest(struct crypto_tfm *tfm,
1642 + struct scatterlist *sg, unsigned int nsg, u8 *out)
1643 + __deprecated_for_modules;
1645 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1647 return (struct crypto_hash *)tfm;
1648 @@ -1113,6 +1134,14 @@ static inline struct crypto_hash *crypto
1649 return __crypto_hash_cast(tfm);
1652 +static int crypto_digest_setkey(struct crypto_tfm *tfm, const u8 *key,
1653 + unsigned int keylen) __deprecated;
1654 +static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
1655 + const u8 *key, unsigned int keylen)
1657 + return tfm->crt_hash.setkey(crypto_hash_cast(tfm), key, keylen);
1660 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1663 --- a/include/linux/ppp-comp.h
1664 +++ b/include/linux/ppp-comp.h
1669 - * ==FILEVERSION 980319==
1670 + * ==FILEVERSION 20040509==
1672 * NOTE TO MAINTAINERS:
1673 * If you modify this file at all, please set the above date.
1674 @@ -78,7 +78,7 @@ struct compressor {
1676 /* Compress a packet */
1677 int (*compress) (void *state, unsigned char *rptr,
1678 - unsigned char *obuf, int isize, int osize);
1679 + unsigned char *obuf, int isize, int osize);
1681 /* Return compression statistics */
1682 void (*comp_stat) (void *state, struct compstat *stats);
1683 @@ -99,7 +99,7 @@ struct compressor {
1685 /* Decompress a packet. */
1686 int (*decompress) (void *state, unsigned char *ibuf, int isize,
1687 - unsigned char *obuf, int osize);
1688 + unsigned char *obuf, int osize);
1690 /* Update state for an incompressible packet received */
1691 void (*incomp) (void *state, unsigned char *ibuf, int icnt);
1692 @@ -109,8 +109,6 @@ struct compressor {
1694 /* Used in locking compressor modules */
1695 struct module *owner;
1696 - /* Extra skb space needed by the compressor algorithm */
1697 - unsigned int comp_extra;
1701 @@ -191,11 +189,40 @@ struct compressor {
1702 #define DEFLATE_CHK_SEQUENCE 0
1705 - * Definitions for MPPE.
1706 + * Definitions for MPPE/MPPC.
1709 -#define CI_MPPE 18 /* config option for MPPE */
1710 -#define CILEN_MPPE 6 /* length of config option */
1711 +#define CI_MPPE 18 /* config option for MPPE */
1712 +#define CILEN_MPPE 6 /* length of config option */
1714 +#define MPPE_OVHD 4 /* MPPE overhead */
1715 +#define MPPE_MAX_KEY_LEN 16 /* largest key length (128-bit) */
1717 +#define MPPE_STATELESS 0x01 /* configuration bit H */
1718 +#define MPPE_40BIT 0x20 /* configuration bit L */
1719 +#define MPPE_56BIT 0x80 /* configuration bit M */
1720 +#define MPPE_128BIT 0x40 /* configuration bit S */
1721 +#define MPPE_MPPC 0x01 /* configuration bit C */
1724 + * Definitions for Stac LZS.
1727 +#define CI_LZS 17 /* config option for Stac LZS */
1728 +#define CILEN_LZS 5 /* length of config option */
1730 +#define LZS_OVHD 4 /* max. LZS overhead */
1731 +#define LZS_HIST_LEN 2048 /* LZS history size */
1732 +#define LZS_MAX_CCOUNT 0x0FFF /* max. coherency counter value */
1734 +#define LZS_MODE_NONE 0
1735 +#define LZS_MODE_LCB 1
1736 +#define LZS_MODE_CRC 2
1737 +#define LZS_MODE_SEQ 3
1738 +#define LZS_MODE_EXT 4
1740 +#define LZS_EXT_BIT_FLUSHED 0x80 /* bit A */
1741 +#define LZS_EXT_BIT_COMP 0x20 /* bit C */
1744 * Definitions for other, as yet unsupported, compression methods.