18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * ppp_mppe.c - interface MPPE to the PPP code.
38c2ecf20Sopenharmony_ci * This version is for use with Linux kernel 2.6.14+
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * By Frank Cusack <fcusack@fcusack.com>.
68c2ecf20Sopenharmony_ci * Copyright (c) 2002,2003,2004 Google, Inc.
78c2ecf20Sopenharmony_ci * All rights reserved.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * License:
108c2ecf20Sopenharmony_ci * Permission to use, copy, modify, and distribute this software and its
118c2ecf20Sopenharmony_ci * documentation is hereby granted, provided that the above copyright
128c2ecf20Sopenharmony_ci * notice appears in all copies.  This software is provided without any
138c2ecf20Sopenharmony_ci * warranty, express or implied.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * ALTERNATIVELY, provided that this notice is retained in full, this product
168c2ecf20Sopenharmony_ci * may be distributed under the terms of the GNU General Public License (GPL),
178c2ecf20Sopenharmony_ci * in which case the provisions of the GPL apply INSTEAD OF those given above.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci *   This program is free software; you can redistribute it and/or modify
208c2ecf20Sopenharmony_ci *   it under the terms of the GNU General Public License as published by
218c2ecf20Sopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
228c2ecf20Sopenharmony_ci *   (at your option) any later version.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci *   This program is distributed in the hope that it will be useful,
258c2ecf20Sopenharmony_ci *   but WITHOUT ANY WARRANTY; without even the implied warranty of
268c2ecf20Sopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
278c2ecf20Sopenharmony_ci *   GNU General Public License for more details.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci *   You should have received a copy of the GNU General Public License
308c2ecf20Sopenharmony_ci *   along with this program; if not, see <http://www.gnu.org/licenses/>.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * Changelog:
348c2ecf20Sopenharmony_ci *      08/12/05 - Matt Domsch <Matt_Domsch@dell.com>
358c2ecf20Sopenharmony_ci *                 Only need extra skb padding on transmit, not receive.
368c2ecf20Sopenharmony_ci *      06/18/04 - Matt Domsch <Matt_Domsch@dell.com>, Oleg Makarenko <mole@quadra.ru>
378c2ecf20Sopenharmony_ci *                 Use Linux kernel 2.6 arc4 and sha1 routines rather than
388c2ecf20Sopenharmony_ci *                 providing our own.
398c2ecf20Sopenharmony_ci *      2/15/04 - TS: added #include <version.h> and testing for Kernel
408c2ecf20Sopenharmony_ci *                    version before using
418c2ecf20Sopenharmony_ci *                    MOD_DEC_USAGE_COUNT/MOD_INC_USAGE_COUNT which are
428c2ecf20Sopenharmony_ci *                    deprecated in 2.6
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#include <crypto/arc4.h>
468c2ecf20Sopenharmony_ci#include <crypto/hash.h>
478c2ecf20Sopenharmony_ci#include <linux/err.h>
488c2ecf20Sopenharmony_ci#include <linux/fips.h>
498c2ecf20Sopenharmony_ci#include <linux/module.h>
508c2ecf20Sopenharmony_ci#include <linux/kernel.h>
518c2ecf20Sopenharmony_ci#include <linux/init.h>
528c2ecf20Sopenharmony_ci#include <linux/types.h>
538c2ecf20Sopenharmony_ci#include <linux/slab.h>
548c2ecf20Sopenharmony_ci#include <linux/string.h>
558c2ecf20Sopenharmony_ci#include <linux/mm.h>
568c2ecf20Sopenharmony_ci#include <linux/ppp_defs.h>
578c2ecf20Sopenharmony_ci#include <linux/ppp-comp.h>
588c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
598c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#include "ppp_mppe.h"
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciMODULE_AUTHOR("Frank Cusack <fcusack@fcusack.com>");
648c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Point-to-Point Protocol Microsoft Point-to-Point Encryption support");
658c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
668c2ecf20Sopenharmony_ciMODULE_ALIAS("ppp-compress-" __stringify(CI_MPPE));
678c2ecf20Sopenharmony_ciMODULE_VERSION("1.0.2");
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define SHA1_PAD_SIZE 40
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/*
728c2ecf20Sopenharmony_ci * kernel crypto API needs its arguments to be in kmalloc'd memory, not in the module
738c2ecf20Sopenharmony_ci * static data area.  That means sha_pad needs to be kmalloc'd.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistruct sha_pad {
778c2ecf20Sopenharmony_ci	unsigned char sha_pad1[SHA1_PAD_SIZE];
788c2ecf20Sopenharmony_ci	unsigned char sha_pad2[SHA1_PAD_SIZE];
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_cistatic struct sha_pad *sha_pad;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic inline void sha_pad_init(struct sha_pad *shapad)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	memset(shapad->sha_pad1, 0x00, sizeof(shapad->sha_pad1));
858c2ecf20Sopenharmony_ci	memset(shapad->sha_pad2, 0xF2, sizeof(shapad->sha_pad2));
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/*
898c2ecf20Sopenharmony_ci * State for an MPPE (de)compressor.
908c2ecf20Sopenharmony_ci */
918c2ecf20Sopenharmony_cistruct ppp_mppe_state {
928c2ecf20Sopenharmony_ci	struct arc4_ctx arc4;
938c2ecf20Sopenharmony_ci	struct shash_desc *sha1;
948c2ecf20Sopenharmony_ci	unsigned char *sha1_digest;
958c2ecf20Sopenharmony_ci	unsigned char master_key[MPPE_MAX_KEY_LEN];
968c2ecf20Sopenharmony_ci	unsigned char session_key[MPPE_MAX_KEY_LEN];
978c2ecf20Sopenharmony_ci	unsigned keylen;	/* key length in bytes             */
988c2ecf20Sopenharmony_ci	/* NB: 128-bit == 16, 40-bit == 8! */
998c2ecf20Sopenharmony_ci	/* If we want to support 56-bit,   */
1008c2ecf20Sopenharmony_ci	/* the unit has to change to bits  */
1018c2ecf20Sopenharmony_ci	unsigned char bits;	/* MPPE control bits */
1028c2ecf20Sopenharmony_ci	unsigned ccount;	/* 12-bit coherency count (seqno)  */
1038c2ecf20Sopenharmony_ci	unsigned stateful;	/* stateful mode flag */
1048c2ecf20Sopenharmony_ci	int discard;		/* stateful mode packet loss flag */
1058c2ecf20Sopenharmony_ci	int sanity_errors;	/* take down LCP if too many */
1068c2ecf20Sopenharmony_ci	int unit;
1078c2ecf20Sopenharmony_ci	int debug;
1088c2ecf20Sopenharmony_ci	struct compstat stats;
1098c2ecf20Sopenharmony_ci};
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/* struct ppp_mppe_state.bits definitions */
1128c2ecf20Sopenharmony_ci#define MPPE_BIT_A	0x80	/* Encryption table were (re)inititalized */
1138c2ecf20Sopenharmony_ci#define MPPE_BIT_B	0x40	/* MPPC only (not implemented) */
1148c2ecf20Sopenharmony_ci#define MPPE_BIT_C	0x20	/* MPPC only (not implemented) */
1158c2ecf20Sopenharmony_ci#define MPPE_BIT_D	0x10	/* This is an encrypted frame */
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define MPPE_BIT_FLUSHED	MPPE_BIT_A
1188c2ecf20Sopenharmony_ci#define MPPE_BIT_ENCRYPTED	MPPE_BIT_D
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define MPPE_BITS(p) ((p)[4] & 0xf0)
1218c2ecf20Sopenharmony_ci#define MPPE_CCOUNT(p) ((((p)[4] & 0x0f) << 8) + (p)[5])
1228c2ecf20Sopenharmony_ci#define MPPE_CCOUNT_SPACE 0x1000	/* The size of the ccount space */
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#define MPPE_OVHD	2	/* MPPE overhead/packet */
1258c2ecf20Sopenharmony_ci#define SANITY_MAX	1600	/* Max bogon factor we will tolerate */
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * Key Derivation, from RFC 3078, RFC 3079.
1298c2ecf20Sopenharmony_ci * Equivalent to Get_Key() for MS-CHAP as described in RFC 3079.
1308c2ecf20Sopenharmony_ci */
1318c2ecf20Sopenharmony_cistatic void get_new_key_from_sha(struct ppp_mppe_state * state)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	crypto_shash_init(state->sha1);
1348c2ecf20Sopenharmony_ci	crypto_shash_update(state->sha1, state->master_key,
1358c2ecf20Sopenharmony_ci			    state->keylen);
1368c2ecf20Sopenharmony_ci	crypto_shash_update(state->sha1, sha_pad->sha_pad1,
1378c2ecf20Sopenharmony_ci			    sizeof(sha_pad->sha_pad1));
1388c2ecf20Sopenharmony_ci	crypto_shash_update(state->sha1, state->session_key,
1398c2ecf20Sopenharmony_ci			    state->keylen);
1408c2ecf20Sopenharmony_ci	crypto_shash_update(state->sha1, sha_pad->sha_pad2,
1418c2ecf20Sopenharmony_ci			    sizeof(sha_pad->sha_pad2));
1428c2ecf20Sopenharmony_ci	crypto_shash_final(state->sha1, state->sha1_digest);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/*
1468c2ecf20Sopenharmony_ci * Perform the MPPE rekey algorithm, from RFC 3078, sec. 7.3.
1478c2ecf20Sopenharmony_ci * Well, not what's written there, but rather what they meant.
1488c2ecf20Sopenharmony_ci */
1498c2ecf20Sopenharmony_cistatic void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	get_new_key_from_sha(state);
1528c2ecf20Sopenharmony_ci	if (!initial_key) {
1538c2ecf20Sopenharmony_ci		arc4_setkey(&state->arc4, state->sha1_digest, state->keylen);
1548c2ecf20Sopenharmony_ci		arc4_crypt(&state->arc4, state->session_key, state->sha1_digest,
1558c2ecf20Sopenharmony_ci			   state->keylen);
1568c2ecf20Sopenharmony_ci	} else {
1578c2ecf20Sopenharmony_ci		memcpy(state->session_key, state->sha1_digest, state->keylen);
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci	if (state->keylen == 8) {
1608c2ecf20Sopenharmony_ci		/* See RFC 3078 */
1618c2ecf20Sopenharmony_ci		state->session_key[0] = 0xd1;
1628c2ecf20Sopenharmony_ci		state->session_key[1] = 0x26;
1638c2ecf20Sopenharmony_ci		state->session_key[2] = 0x9e;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci	arc4_setkey(&state->arc4, state->session_key, state->keylen);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/*
1698c2ecf20Sopenharmony_ci * Allocate space for a (de)compressor.
1708c2ecf20Sopenharmony_ci */
1718c2ecf20Sopenharmony_cistatic void *mppe_alloc(unsigned char *options, int optlen)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct ppp_mppe_state *state;
1748c2ecf20Sopenharmony_ci	struct crypto_shash *shash;
1758c2ecf20Sopenharmony_ci	unsigned int digestsize;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	if (optlen != CILEN_MPPE + sizeof(state->master_key) ||
1788c2ecf20Sopenharmony_ci	    options[0] != CI_MPPE || options[1] != CILEN_MPPE ||
1798c2ecf20Sopenharmony_ci	    fips_enabled)
1808c2ecf20Sopenharmony_ci		goto out;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	state = kzalloc(sizeof(*state), GFP_KERNEL);
1838c2ecf20Sopenharmony_ci	if (state == NULL)
1848c2ecf20Sopenharmony_ci		goto out;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	shash = crypto_alloc_shash("sha1", 0, 0);
1888c2ecf20Sopenharmony_ci	if (IS_ERR(shash))
1898c2ecf20Sopenharmony_ci		goto out_free;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	state->sha1 = kmalloc(sizeof(*state->sha1) +
1928c2ecf20Sopenharmony_ci				     crypto_shash_descsize(shash),
1938c2ecf20Sopenharmony_ci			      GFP_KERNEL);
1948c2ecf20Sopenharmony_ci	if (!state->sha1) {
1958c2ecf20Sopenharmony_ci		crypto_free_shash(shash);
1968c2ecf20Sopenharmony_ci		goto out_free;
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci	state->sha1->tfm = shash;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	digestsize = crypto_shash_digestsize(shash);
2018c2ecf20Sopenharmony_ci	if (digestsize < MPPE_MAX_KEY_LEN)
2028c2ecf20Sopenharmony_ci		goto out_free;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	state->sha1_digest = kmalloc(digestsize, GFP_KERNEL);
2058c2ecf20Sopenharmony_ci	if (!state->sha1_digest)
2068c2ecf20Sopenharmony_ci		goto out_free;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	/* Save keys. */
2098c2ecf20Sopenharmony_ci	memcpy(state->master_key, &options[CILEN_MPPE],
2108c2ecf20Sopenharmony_ci	       sizeof(state->master_key));
2118c2ecf20Sopenharmony_ci	memcpy(state->session_key, state->master_key,
2128c2ecf20Sopenharmony_ci	       sizeof(state->master_key));
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	/*
2158c2ecf20Sopenharmony_ci	 * We defer initial key generation until mppe_init(), as mppe_alloc()
2168c2ecf20Sopenharmony_ci	 * is called frequently during negotiation.
2178c2ecf20Sopenharmony_ci	 */
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	return (void *)state;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ciout_free:
2228c2ecf20Sopenharmony_ci	kfree(state->sha1_digest);
2238c2ecf20Sopenharmony_ci	if (state->sha1) {
2248c2ecf20Sopenharmony_ci		crypto_free_shash(state->sha1->tfm);
2258c2ecf20Sopenharmony_ci		kfree_sensitive(state->sha1);
2268c2ecf20Sopenharmony_ci	}
2278c2ecf20Sopenharmony_ci	kfree(state);
2288c2ecf20Sopenharmony_ciout:
2298c2ecf20Sopenharmony_ci	return NULL;
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci/*
2338c2ecf20Sopenharmony_ci * Deallocate space for a (de)compressor.
2348c2ecf20Sopenharmony_ci */
2358c2ecf20Sopenharmony_cistatic void mppe_free(void *arg)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
2388c2ecf20Sopenharmony_ci	if (state) {
2398c2ecf20Sopenharmony_ci		kfree(state->sha1_digest);
2408c2ecf20Sopenharmony_ci		crypto_free_shash(state->sha1->tfm);
2418c2ecf20Sopenharmony_ci		kfree_sensitive(state->sha1);
2428c2ecf20Sopenharmony_ci		kfree_sensitive(state);
2438c2ecf20Sopenharmony_ci	}
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/*
2478c2ecf20Sopenharmony_ci * Initialize (de)compressor state.
2488c2ecf20Sopenharmony_ci */
2498c2ecf20Sopenharmony_cistatic int
2508c2ecf20Sopenharmony_cimppe_init(void *arg, unsigned char *options, int optlen, int unit, int debug,
2518c2ecf20Sopenharmony_ci	  const char *debugstr)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
2548c2ecf20Sopenharmony_ci	unsigned char mppe_opts;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (optlen != CILEN_MPPE ||
2578c2ecf20Sopenharmony_ci	    options[0] != CI_MPPE || options[1] != CILEN_MPPE)
2588c2ecf20Sopenharmony_ci		return 0;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	MPPE_CI_TO_OPTS(&options[2], mppe_opts);
2618c2ecf20Sopenharmony_ci	if (mppe_opts & MPPE_OPT_128)
2628c2ecf20Sopenharmony_ci		state->keylen = 16;
2638c2ecf20Sopenharmony_ci	else if (mppe_opts & MPPE_OPT_40)
2648c2ecf20Sopenharmony_ci		state->keylen = 8;
2658c2ecf20Sopenharmony_ci	else {
2668c2ecf20Sopenharmony_ci		printk(KERN_WARNING "%s[%d]: unknown key length\n", debugstr,
2678c2ecf20Sopenharmony_ci		       unit);
2688c2ecf20Sopenharmony_ci		return 0;
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci	if (mppe_opts & MPPE_OPT_STATEFUL)
2718c2ecf20Sopenharmony_ci		state->stateful = 1;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	/* Generate the initial session key. */
2748c2ecf20Sopenharmony_ci	mppe_rekey(state, 1);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (debug) {
2778c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "%s[%d]: initialized with %d-bit %s mode\n",
2788c2ecf20Sopenharmony_ci		       debugstr, unit, (state->keylen == 16) ? 128 : 40,
2798c2ecf20Sopenharmony_ci		       (state->stateful) ? "stateful" : "stateless");
2808c2ecf20Sopenharmony_ci		printk(KERN_DEBUG
2818c2ecf20Sopenharmony_ci		       "%s[%d]: keys: master: %*phN initial session: %*phN\n",
2828c2ecf20Sopenharmony_ci		       debugstr, unit,
2838c2ecf20Sopenharmony_ci		       (int)sizeof(state->master_key), state->master_key,
2848c2ecf20Sopenharmony_ci		       (int)sizeof(state->session_key), state->session_key);
2858c2ecf20Sopenharmony_ci	}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/*
2888c2ecf20Sopenharmony_ci	 * Initialize the coherency count.  The initial value is not specified
2898c2ecf20Sopenharmony_ci	 * in RFC 3078, but we can make a reasonable assumption that it will
2908c2ecf20Sopenharmony_ci	 * start at 0.  Setting it to the max here makes the comp/decomp code
2918c2ecf20Sopenharmony_ci	 * do the right thing (determined through experiment).
2928c2ecf20Sopenharmony_ci	 */
2938c2ecf20Sopenharmony_ci	state->ccount = MPPE_CCOUNT_SPACE - 1;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	/*
2968c2ecf20Sopenharmony_ci	 * Note that even though we have initialized the key table, we don't
2978c2ecf20Sopenharmony_ci	 * set the FLUSHED bit.  This is contrary to RFC 3078, sec. 3.1.
2988c2ecf20Sopenharmony_ci	 */
2998c2ecf20Sopenharmony_ci	state->bits = MPPE_BIT_ENCRYPTED;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	state->unit = unit;
3028c2ecf20Sopenharmony_ci	state->debug = debug;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	return 1;
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic int
3088c2ecf20Sopenharmony_cimppe_comp_init(void *arg, unsigned char *options, int optlen, int unit,
3098c2ecf20Sopenharmony_ci	       int hdrlen, int debug)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	/* ARGSUSED */
3128c2ecf20Sopenharmony_ci	return mppe_init(arg, options, optlen, unit, debug, "mppe_comp_init");
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci/*
3168c2ecf20Sopenharmony_ci * We received a CCP Reset-Request (actually, we are sending a Reset-Ack),
3178c2ecf20Sopenharmony_ci * tell the compressor to rekey.  Note that we MUST NOT rekey for
3188c2ecf20Sopenharmony_ci * every CCP Reset-Request; we only rekey on the next xmit packet.
3198c2ecf20Sopenharmony_ci * We might get multiple CCP Reset-Requests if our CCP Reset-Ack is lost.
3208c2ecf20Sopenharmony_ci * So, rekeying for every CCP Reset-Request is broken as the peer will not
3218c2ecf20Sopenharmony_ci * know how many times we've rekeyed.  (If we rekey and THEN get another
3228c2ecf20Sopenharmony_ci * CCP Reset-Request, we must rekey again.)
3238c2ecf20Sopenharmony_ci */
3248c2ecf20Sopenharmony_cistatic void mppe_comp_reset(void *arg)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	state->bits |= MPPE_BIT_FLUSHED;
3298c2ecf20Sopenharmony_ci}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci/*
3328c2ecf20Sopenharmony_ci * Compress (encrypt) a packet.
3338c2ecf20Sopenharmony_ci * It's strange to call this a compressor, since the output is always
3348c2ecf20Sopenharmony_ci * MPPE_OVHD + 2 bytes larger than the input.
3358c2ecf20Sopenharmony_ci */
3368c2ecf20Sopenharmony_cistatic int
3378c2ecf20Sopenharmony_cimppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
3388c2ecf20Sopenharmony_ci	      int isize, int osize)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
3418c2ecf20Sopenharmony_ci	int proto;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	/*
3448c2ecf20Sopenharmony_ci	 * Check that the protocol is in the range we handle.
3458c2ecf20Sopenharmony_ci	 */
3468c2ecf20Sopenharmony_ci	proto = PPP_PROTOCOL(ibuf);
3478c2ecf20Sopenharmony_ci	if (proto < 0x0021 || proto > 0x00fa)
3488c2ecf20Sopenharmony_ci		return 0;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	/* Make sure we have enough room to generate an encrypted packet. */
3518c2ecf20Sopenharmony_ci	if (osize < isize + MPPE_OVHD + 2) {
3528c2ecf20Sopenharmony_ci		/* Drop the packet if we should encrypt it, but can't. */
3538c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "mppe_compress[%d]: osize too small! "
3548c2ecf20Sopenharmony_ci		       "(have: %d need: %d)\n", state->unit,
3558c2ecf20Sopenharmony_ci		       osize, osize + MPPE_OVHD + 2);
3568c2ecf20Sopenharmony_ci		return -1;
3578c2ecf20Sopenharmony_ci	}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	osize = isize + MPPE_OVHD + 2;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	/*
3628c2ecf20Sopenharmony_ci	 * Copy over the PPP header and set control bits.
3638c2ecf20Sopenharmony_ci	 */
3648c2ecf20Sopenharmony_ci	obuf[0] = PPP_ADDRESS(ibuf);
3658c2ecf20Sopenharmony_ci	obuf[1] = PPP_CONTROL(ibuf);
3668c2ecf20Sopenharmony_ci	put_unaligned_be16(PPP_COMP, obuf + 2);
3678c2ecf20Sopenharmony_ci	obuf += PPP_HDRLEN;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
3708c2ecf20Sopenharmony_ci	if (state->debug >= 7)
3718c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "mppe_compress[%d]: ccount %d\n", state->unit,
3728c2ecf20Sopenharmony_ci		       state->ccount);
3738c2ecf20Sopenharmony_ci	put_unaligned_be16(state->ccount, obuf);
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	if (!state->stateful ||	/* stateless mode     */
3768c2ecf20Sopenharmony_ci	    ((state->ccount & 0xff) == 0xff) ||	/* "flag" packet      */
3778c2ecf20Sopenharmony_ci	    (state->bits & MPPE_BIT_FLUSHED)) {	/* CCP Reset-Request  */
3788c2ecf20Sopenharmony_ci		/* We must rekey */
3798c2ecf20Sopenharmony_ci		if (state->debug && state->stateful)
3808c2ecf20Sopenharmony_ci			printk(KERN_DEBUG "mppe_compress[%d]: rekeying\n",
3818c2ecf20Sopenharmony_ci			       state->unit);
3828c2ecf20Sopenharmony_ci		mppe_rekey(state, 0);
3838c2ecf20Sopenharmony_ci		state->bits |= MPPE_BIT_FLUSHED;
3848c2ecf20Sopenharmony_ci	}
3858c2ecf20Sopenharmony_ci	obuf[0] |= state->bits;
3868c2ecf20Sopenharmony_ci	state->bits &= ~MPPE_BIT_FLUSHED;	/* reset for next xmit */
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	obuf += MPPE_OVHD;
3898c2ecf20Sopenharmony_ci	ibuf += 2;		/* skip to proto field */
3908c2ecf20Sopenharmony_ci	isize -= 2;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	arc4_crypt(&state->arc4, obuf, ibuf, isize);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	state->stats.unc_bytes += isize;
3958c2ecf20Sopenharmony_ci	state->stats.unc_packets++;
3968c2ecf20Sopenharmony_ci	state->stats.comp_bytes += osize;
3978c2ecf20Sopenharmony_ci	state->stats.comp_packets++;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	return osize;
4008c2ecf20Sopenharmony_ci}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci/*
4038c2ecf20Sopenharmony_ci * Since every frame grows by MPPE_OVHD + 2 bytes, this is always going
4048c2ecf20Sopenharmony_ci * to look bad ... and the longer the link is up the worse it will get.
4058c2ecf20Sopenharmony_ci */
4068c2ecf20Sopenharmony_cistatic void mppe_comp_stats(void *arg, struct compstat *stats)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	*stats = state->stats;
4118c2ecf20Sopenharmony_ci}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_cistatic int
4148c2ecf20Sopenharmony_cimppe_decomp_init(void *arg, unsigned char *options, int optlen, int unit,
4158c2ecf20Sopenharmony_ci		 int hdrlen, int mru, int debug)
4168c2ecf20Sopenharmony_ci{
4178c2ecf20Sopenharmony_ci	/* ARGSUSED */
4188c2ecf20Sopenharmony_ci	return mppe_init(arg, options, optlen, unit, debug, "mppe_decomp_init");
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci/*
4228c2ecf20Sopenharmony_ci * We received a CCP Reset-Ack.  Just ignore it.
4238c2ecf20Sopenharmony_ci */
4248c2ecf20Sopenharmony_cistatic void mppe_decomp_reset(void *arg)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	/* ARGSUSED */
4278c2ecf20Sopenharmony_ci	return;
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci/*
4318c2ecf20Sopenharmony_ci * Decompress (decrypt) an MPPE packet.
4328c2ecf20Sopenharmony_ci */
4338c2ecf20Sopenharmony_cistatic int
4348c2ecf20Sopenharmony_cimppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
4358c2ecf20Sopenharmony_ci		int osize)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
4388c2ecf20Sopenharmony_ci	unsigned ccount;
4398c2ecf20Sopenharmony_ci	int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	if (isize <= PPP_HDRLEN + MPPE_OVHD) {
4428c2ecf20Sopenharmony_ci		if (state->debug)
4438c2ecf20Sopenharmony_ci			printk(KERN_DEBUG
4448c2ecf20Sopenharmony_ci			       "mppe_decompress[%d]: short pkt (%d)\n",
4458c2ecf20Sopenharmony_ci			       state->unit, isize);
4468c2ecf20Sopenharmony_ci		return DECOMP_ERROR;
4478c2ecf20Sopenharmony_ci	}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	/*
4508c2ecf20Sopenharmony_ci	 * Make sure we have enough room to decrypt the packet.
4518c2ecf20Sopenharmony_ci	 * Note that for our test we only subtract 1 byte whereas in
4528c2ecf20Sopenharmony_ci	 * mppe_compress() we added 2 bytes (+MPPE_OVHD);
4538c2ecf20Sopenharmony_ci	 * this is to account for possible PFC.
4548c2ecf20Sopenharmony_ci	 */
4558c2ecf20Sopenharmony_ci	if (osize < isize - MPPE_OVHD - 1) {
4568c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "mppe_decompress[%d]: osize too small! "
4578c2ecf20Sopenharmony_ci		       "(have: %d need: %d)\n", state->unit,
4588c2ecf20Sopenharmony_ci		       osize, isize - MPPE_OVHD - 1);
4598c2ecf20Sopenharmony_ci		return DECOMP_ERROR;
4608c2ecf20Sopenharmony_ci	}
4618c2ecf20Sopenharmony_ci	osize = isize - MPPE_OVHD - 2;	/* assume no PFC */
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	ccount = MPPE_CCOUNT(ibuf);
4648c2ecf20Sopenharmony_ci	if (state->debug >= 7)
4658c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "mppe_decompress[%d]: ccount %d\n",
4668c2ecf20Sopenharmony_ci		       state->unit, ccount);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	/* sanity checks -- terminate with extreme prejudice */
4698c2ecf20Sopenharmony_ci	if (!(MPPE_BITS(ibuf) & MPPE_BIT_ENCRYPTED)) {
4708c2ecf20Sopenharmony_ci		printk(KERN_DEBUG
4718c2ecf20Sopenharmony_ci		       "mppe_decompress[%d]: ENCRYPTED bit not set!\n",
4728c2ecf20Sopenharmony_ci		       state->unit);
4738c2ecf20Sopenharmony_ci		state->sanity_errors += 100;
4748c2ecf20Sopenharmony_ci		goto sanity_error;
4758c2ecf20Sopenharmony_ci	}
4768c2ecf20Sopenharmony_ci	if (!state->stateful && !flushed) {
4778c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set in "
4788c2ecf20Sopenharmony_ci		       "stateless mode!\n", state->unit);
4798c2ecf20Sopenharmony_ci		state->sanity_errors += 100;
4808c2ecf20Sopenharmony_ci		goto sanity_error;
4818c2ecf20Sopenharmony_ci	}
4828c2ecf20Sopenharmony_ci	if (state->stateful && ((ccount & 0xff) == 0xff) && !flushed) {
4838c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "mppe_decompress[%d]: FLUSHED bit not set on "
4848c2ecf20Sopenharmony_ci		       "flag packet!\n", state->unit);
4858c2ecf20Sopenharmony_ci		state->sanity_errors += 100;
4868c2ecf20Sopenharmony_ci		goto sanity_error;
4878c2ecf20Sopenharmony_ci	}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	/*
4908c2ecf20Sopenharmony_ci	 * Check the coherency count.
4918c2ecf20Sopenharmony_ci	 */
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	if (!state->stateful) {
4948c2ecf20Sopenharmony_ci		/* Discard late packet */
4958c2ecf20Sopenharmony_ci		if ((ccount - state->ccount) % MPPE_CCOUNT_SPACE
4968c2ecf20Sopenharmony_ci						> MPPE_CCOUNT_SPACE / 2) {
4978c2ecf20Sopenharmony_ci			state->sanity_errors++;
4988c2ecf20Sopenharmony_ci			goto sanity_error;
4998c2ecf20Sopenharmony_ci		}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci		/* RFC 3078, sec 8.1.  Rekey for every packet. */
5028c2ecf20Sopenharmony_ci		while (state->ccount != ccount) {
5038c2ecf20Sopenharmony_ci			mppe_rekey(state, 0);
5048c2ecf20Sopenharmony_ci			state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
5058c2ecf20Sopenharmony_ci		}
5068c2ecf20Sopenharmony_ci	} else {
5078c2ecf20Sopenharmony_ci		/* RFC 3078, sec 8.2. */
5088c2ecf20Sopenharmony_ci		if (!state->discard) {
5098c2ecf20Sopenharmony_ci			/* normal state */
5108c2ecf20Sopenharmony_ci			state->ccount = (state->ccount + 1) % MPPE_CCOUNT_SPACE;
5118c2ecf20Sopenharmony_ci			if (ccount != state->ccount) {
5128c2ecf20Sopenharmony_ci				/*
5138c2ecf20Sopenharmony_ci				 * (ccount > state->ccount)
5148c2ecf20Sopenharmony_ci				 * Packet loss detected, enter the discard state.
5158c2ecf20Sopenharmony_ci				 * Signal the peer to rekey (by sending a CCP Reset-Request).
5168c2ecf20Sopenharmony_ci				 */
5178c2ecf20Sopenharmony_ci				state->discard = 1;
5188c2ecf20Sopenharmony_ci				return DECOMP_ERROR;
5198c2ecf20Sopenharmony_ci			}
5208c2ecf20Sopenharmony_ci		} else {
5218c2ecf20Sopenharmony_ci			/* discard state */
5228c2ecf20Sopenharmony_ci			if (!flushed) {
5238c2ecf20Sopenharmony_ci				/* ccp.c will be silent (no additional CCP Reset-Requests). */
5248c2ecf20Sopenharmony_ci				return DECOMP_ERROR;
5258c2ecf20Sopenharmony_ci			} else {
5268c2ecf20Sopenharmony_ci				/* Rekey for every missed "flag" packet. */
5278c2ecf20Sopenharmony_ci				while ((ccount & ~0xff) !=
5288c2ecf20Sopenharmony_ci				       (state->ccount & ~0xff)) {
5298c2ecf20Sopenharmony_ci					mppe_rekey(state, 0);
5308c2ecf20Sopenharmony_ci					state->ccount =
5318c2ecf20Sopenharmony_ci					    (state->ccount +
5328c2ecf20Sopenharmony_ci					     256) % MPPE_CCOUNT_SPACE;
5338c2ecf20Sopenharmony_ci				}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci				/* reset */
5368c2ecf20Sopenharmony_ci				state->discard = 0;
5378c2ecf20Sopenharmony_ci				state->ccount = ccount;
5388c2ecf20Sopenharmony_ci				/*
5398c2ecf20Sopenharmony_ci				 * Another problem with RFC 3078 here.  It implies that the
5408c2ecf20Sopenharmony_ci				 * peer need not send a Reset-Ack packet.  But RFC 1962
5418c2ecf20Sopenharmony_ci				 * requires it.  Hopefully, M$ does send a Reset-Ack; even
5428c2ecf20Sopenharmony_ci				 * though it isn't required for MPPE synchronization, it is
5438c2ecf20Sopenharmony_ci				 * required to reset CCP state.
5448c2ecf20Sopenharmony_ci				 */
5458c2ecf20Sopenharmony_ci			}
5468c2ecf20Sopenharmony_ci		}
5478c2ecf20Sopenharmony_ci		if (flushed)
5488c2ecf20Sopenharmony_ci			mppe_rekey(state, 0);
5498c2ecf20Sopenharmony_ci	}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	/*
5528c2ecf20Sopenharmony_ci	 * Fill in the first part of the PPP header.  The protocol field
5538c2ecf20Sopenharmony_ci	 * comes from the decrypted data.
5548c2ecf20Sopenharmony_ci	 */
5558c2ecf20Sopenharmony_ci	obuf[0] = PPP_ADDRESS(ibuf);	/* +1 */
5568c2ecf20Sopenharmony_ci	obuf[1] = PPP_CONTROL(ibuf);	/* +1 */
5578c2ecf20Sopenharmony_ci	obuf += 2;
5588c2ecf20Sopenharmony_ci	ibuf += PPP_HDRLEN + MPPE_OVHD;
5598c2ecf20Sopenharmony_ci	isize -= PPP_HDRLEN + MPPE_OVHD;	/* -6 */
5608c2ecf20Sopenharmony_ci	/* net osize: isize-4 */
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	/*
5638c2ecf20Sopenharmony_ci	 * Decrypt the first byte in order to check if it is
5648c2ecf20Sopenharmony_ci	 * a compressed or uncompressed protocol field.
5658c2ecf20Sopenharmony_ci	 */
5668c2ecf20Sopenharmony_ci	arc4_crypt(&state->arc4, obuf, ibuf, 1);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	/*
5698c2ecf20Sopenharmony_ci	 * Do PFC decompression.
5708c2ecf20Sopenharmony_ci	 * This would be nicer if we were given the actual sk_buff
5718c2ecf20Sopenharmony_ci	 * instead of a char *.
5728c2ecf20Sopenharmony_ci	 */
5738c2ecf20Sopenharmony_ci	if ((obuf[0] & 0x01) != 0) {
5748c2ecf20Sopenharmony_ci		obuf[1] = obuf[0];
5758c2ecf20Sopenharmony_ci		obuf[0] = 0;
5768c2ecf20Sopenharmony_ci		obuf++;
5778c2ecf20Sopenharmony_ci		osize++;
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	/* And finally, decrypt the rest of the packet. */
5818c2ecf20Sopenharmony_ci	arc4_crypt(&state->arc4, obuf + 1, ibuf + 1, isize - 1);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	state->stats.unc_bytes += osize;
5848c2ecf20Sopenharmony_ci	state->stats.unc_packets++;
5858c2ecf20Sopenharmony_ci	state->stats.comp_bytes += isize;
5868c2ecf20Sopenharmony_ci	state->stats.comp_packets++;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	/* good packet credit */
5898c2ecf20Sopenharmony_ci	state->sanity_errors >>= 1;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	return osize;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_cisanity_error:
5948c2ecf20Sopenharmony_ci	if (state->sanity_errors < SANITY_MAX)
5958c2ecf20Sopenharmony_ci		return DECOMP_ERROR;
5968c2ecf20Sopenharmony_ci	else
5978c2ecf20Sopenharmony_ci		/* Take LCP down if the peer is sending too many bogons.
5988c2ecf20Sopenharmony_ci		 * We don't want to do this for a single or just a few
5998c2ecf20Sopenharmony_ci		 * instances since it could just be due to packet corruption.
6008c2ecf20Sopenharmony_ci		 */
6018c2ecf20Sopenharmony_ci		return DECOMP_FATALERROR;
6028c2ecf20Sopenharmony_ci}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci/*
6058c2ecf20Sopenharmony_ci * Incompressible data has arrived (this should never happen!).
6068c2ecf20Sopenharmony_ci * We should probably drop the link if the protocol is in the range
6078c2ecf20Sopenharmony_ci * of what should be encrypted.  At the least, we should drop this
6088c2ecf20Sopenharmony_ci * packet.  (How to do this?)
6098c2ecf20Sopenharmony_ci */
6108c2ecf20Sopenharmony_cistatic void mppe_incomp(void *arg, unsigned char *ibuf, int icnt)
6118c2ecf20Sopenharmony_ci{
6128c2ecf20Sopenharmony_ci	struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (state->debug &&
6158c2ecf20Sopenharmony_ci	    (PPP_PROTOCOL(ibuf) >= 0x0021 && PPP_PROTOCOL(ibuf) <= 0x00fa))
6168c2ecf20Sopenharmony_ci		printk(KERN_DEBUG
6178c2ecf20Sopenharmony_ci		       "mppe_incomp[%d]: incompressible (unencrypted) data! "
6188c2ecf20Sopenharmony_ci		       "(proto %04x)\n", state->unit, PPP_PROTOCOL(ibuf));
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	state->stats.inc_bytes += icnt;
6218c2ecf20Sopenharmony_ci	state->stats.inc_packets++;
6228c2ecf20Sopenharmony_ci	state->stats.unc_bytes += icnt;
6238c2ecf20Sopenharmony_ci	state->stats.unc_packets++;
6248c2ecf20Sopenharmony_ci}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci/*************************************************************
6278c2ecf20Sopenharmony_ci * Module interface table
6288c2ecf20Sopenharmony_ci *************************************************************/
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci/*
6318c2ecf20Sopenharmony_ci * Procedures exported to if_ppp.c.
6328c2ecf20Sopenharmony_ci */
6338c2ecf20Sopenharmony_cistatic struct compressor ppp_mppe = {
6348c2ecf20Sopenharmony_ci	.compress_proto = CI_MPPE,
6358c2ecf20Sopenharmony_ci	.comp_alloc     = mppe_alloc,
6368c2ecf20Sopenharmony_ci	.comp_free      = mppe_free,
6378c2ecf20Sopenharmony_ci	.comp_init      = mppe_comp_init,
6388c2ecf20Sopenharmony_ci	.comp_reset     = mppe_comp_reset,
6398c2ecf20Sopenharmony_ci	.compress       = mppe_compress,
6408c2ecf20Sopenharmony_ci	.comp_stat      = mppe_comp_stats,
6418c2ecf20Sopenharmony_ci	.decomp_alloc   = mppe_alloc,
6428c2ecf20Sopenharmony_ci	.decomp_free    = mppe_free,
6438c2ecf20Sopenharmony_ci	.decomp_init    = mppe_decomp_init,
6448c2ecf20Sopenharmony_ci	.decomp_reset   = mppe_decomp_reset,
6458c2ecf20Sopenharmony_ci	.decompress     = mppe_decompress,
6468c2ecf20Sopenharmony_ci	.incomp         = mppe_incomp,
6478c2ecf20Sopenharmony_ci	.decomp_stat    = mppe_comp_stats,
6488c2ecf20Sopenharmony_ci	.owner          = THIS_MODULE,
6498c2ecf20Sopenharmony_ci	.comp_extra     = MPPE_PAD,
6508c2ecf20Sopenharmony_ci};
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci/*
6538c2ecf20Sopenharmony_ci * ppp_mppe_init()
6548c2ecf20Sopenharmony_ci *
6558c2ecf20Sopenharmony_ci * Prior to allowing load, try to load the arc4 and sha1 crypto
6568c2ecf20Sopenharmony_ci * libraries.  The actual use will be allocated later, but
6578c2ecf20Sopenharmony_ci * this way the module will fail to insmod if they aren't available.
6588c2ecf20Sopenharmony_ci */
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_cistatic int __init ppp_mppe_init(void)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	int answer;
6638c2ecf20Sopenharmony_ci	if (fips_enabled || !crypto_has_ahash("sha1", 0, CRYPTO_ALG_ASYNC))
6648c2ecf20Sopenharmony_ci		return -ENODEV;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL);
6678c2ecf20Sopenharmony_ci	if (!sha_pad)
6688c2ecf20Sopenharmony_ci		return -ENOMEM;
6698c2ecf20Sopenharmony_ci	sha_pad_init(sha_pad);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	answer = ppp_register_compressor(&ppp_mppe);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	if (answer == 0)
6748c2ecf20Sopenharmony_ci		printk(KERN_INFO "PPP MPPE Compression module registered\n");
6758c2ecf20Sopenharmony_ci	else
6768c2ecf20Sopenharmony_ci		kfree(sha_pad);
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	return answer;
6798c2ecf20Sopenharmony_ci}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_cistatic void __exit ppp_mppe_cleanup(void)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	ppp_unregister_compressor(&ppp_mppe);
6848c2ecf20Sopenharmony_ci	kfree(sha_pad);
6858c2ecf20Sopenharmony_ci}
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_cimodule_init(ppp_mppe_init);
6888c2ecf20Sopenharmony_cimodule_exit(ppp_mppe_cleanup);
689