1165619Sglebius/*-
2165619Sglebius * Copyright (c) 2006 Alexander Motin <mav@alkar.net>
3165619Sglebius * All rights reserved.
4165619Sglebius *
5165619Sglebius * Redistribution and use in source and binary forms, with or without
6165619Sglebius * modification, are permitted provided that the following conditions
7165619Sglebius * are met:
8165619Sglebius * 1. Redistributions of source code must retain the above copyright
9165619Sglebius *    notice unmodified, this list of conditions, and the following
10165619Sglebius *    disclaimer.
11165619Sglebius * 2. Redistributions in binary form must reproduce the above copyright
12165619Sglebius *    notice, this list of conditions and the following disclaimer in the
13165619Sglebius *    documentation and/or other materials provided with the distribution.
14165619Sglebius *
15165619Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16165619Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17165619Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18165619Sglebius * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19165619Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20165619Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21165619Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22165619Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23165619Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24165619Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25165619Sglebius * SUCH DAMAGE.
26165619Sglebius *
27165619Sglebius * $FreeBSD$
28165619Sglebius */
29165619Sglebius
30165619Sglebius/*
31165619Sglebius * Predictor-1 PPP compression netgraph node type.
32165619Sglebius */
33165619Sglebius
34165619Sglebius#include <sys/param.h>
35165619Sglebius#include <sys/systm.h>
36165619Sglebius#include <sys/kernel.h>
37165619Sglebius#include <sys/mbuf.h>
38165619Sglebius#include <sys/malloc.h>
39165619Sglebius#include <sys/errno.h>
40165619Sglebius#include <sys/syslog.h>
41165619Sglebius
42165619Sglebius#include <netgraph/ng_message.h>
43165619Sglebius#include <netgraph/netgraph.h>
44165619Sglebius#include <netgraph/ng_parse.h>
45165619Sglebius#include <netgraph/ng_pred1.h>
46165619Sglebius
47165619Sglebius#include "opt_netgraph.h"
48165619Sglebius
49227293Sedstatic MALLOC_DEFINE(M_NETGRAPH_PRED1, "netgraph_pred1", "netgraph pred1 node");
50165619Sglebius
51165619Sglebius/* PRED1 header length */
52165619Sglebius#define PRED1_HDRLEN		2
53165619Sglebius
54165619Sglebius#define PRED1_TABLE_SIZE	0x10000
55165619Sglebius#define PRED1_BUF_SIZE		4096
56165619Sglebius#define PPP_INITFCS		0xffff  /* Initial FCS value */
57165619Sglebius#define PPP_GOODFCS		0xf0b8  /* Good final FCS value */
58165619Sglebius
59165619Sglebius/*
60165619Sglebius * The following hash code is the heart of the algorithm:
61165619Sglebius * it builds a sliding hash sum of the previous 3-and-a-bit
62165619Sglebius * characters which will be used to index the guess table.
63165619Sglebius * A better hash function would result in additional compression,
64165619Sglebius * at the expense of time.
65165619Sglebius */
66165619Sglebius
67165619Sglebius#define HASH(x) priv->Hash = (priv->Hash << 4) ^ (x)
68165619Sglebius
69165619Sglebius/* Node private data */
70165619Sglebiusstruct ng_pred1_private {
71165619Sglebius	struct ng_pred1_config cfg;		/* configuration */
72165619Sglebius	u_char		GuessTable[PRED1_TABLE_SIZE];	/* dictionary */
73165619Sglebius	u_char		inbuf[PRED1_BUF_SIZE];	/* input buffer */
74165619Sglebius	u_char		outbuf[PRED1_BUF_SIZE];	/* output buffer */
75165619Sglebius	struct ng_pred1_stats stats;		/* statistics */
76165619Sglebius	uint16_t	Hash;
77165619Sglebius	ng_ID_t		ctrlnode;		/* path to controlling node */
78165619Sglebius	uint16_t	seqnum;			/* sequence number */
79165619Sglebius	u_char		compress;		/* compress/decompress flag */
80165619Sglebius};
81165619Sglebiustypedef struct ng_pred1_private *priv_p;
82165619Sglebius
83165619Sglebius/* Netgraph node methods */
84165619Sglebiusstatic ng_constructor_t	ng_pred1_constructor;
85165619Sglebiusstatic ng_rcvmsg_t	ng_pred1_rcvmsg;
86165619Sglebiusstatic ng_shutdown_t	ng_pred1_shutdown;
87165619Sglebiusstatic ng_newhook_t	ng_pred1_newhook;
88165619Sglebiusstatic ng_rcvdata_t	ng_pred1_rcvdata;
89165619Sglebiusstatic ng_disconnect_t	ng_pred1_disconnect;
90165619Sglebius
91165619Sglebius/* Helper functions */
92165619Sglebiusstatic int	ng_pred1_compress(node_p node, struct mbuf *m,
93165619Sglebius		    struct mbuf **resultp);
94165619Sglebiusstatic int	ng_pred1_decompress(node_p node, struct mbuf *m,
95165619Sglebius		    struct mbuf **resultp);
96165619Sglebiusstatic void	Pred1Init(node_p node);
97165619Sglebiusstatic int	Pred1Compress(node_p node, u_char *source, u_char *dest,
98165619Sglebius		    int len);
99165619Sglebiusstatic int	Pred1Decompress(node_p node, u_char *source, u_char *dest,
100165619Sglebius		    int slen, int dlen);
101165619Sglebiusstatic void	Pred1SyncTable(node_p node, u_char *source, int len);
102165619Sglebiusstatic uint16_t	Crc16(uint16_t fcs, u_char *cp, int len);
103165619Sglebius
104165619Sglebiusstatic const uint16_t	Crc16Table[];
105165619Sglebius
106165619Sglebius/* Parse type for struct ng_pred1_config. */
107165619Sglebiusstatic const struct ng_parse_struct_field ng_pred1_config_type_fields[]
108165619Sglebius	= NG_PRED1_CONFIG_INFO;
109165619Sglebiusstatic const struct ng_parse_type ng_pred1_config_type = {
110165619Sglebius	&ng_parse_struct_type,
111165619Sglebius	ng_pred1_config_type_fields
112165619Sglebius};
113165619Sglebius
114165619Sglebius/* Parse type for struct ng_pred1_stat. */
115165619Sglebiusstatic const struct ng_parse_struct_field ng_pred1_stats_type_fields[]
116165619Sglebius	= NG_PRED1_STATS_INFO;
117165619Sglebiusstatic const struct ng_parse_type ng_pred1_stat_type = {
118165619Sglebius	&ng_parse_struct_type,
119165619Sglebius	ng_pred1_stats_type_fields
120165619Sglebius};
121165619Sglebius
122165619Sglebius/* List of commands and how to convert arguments to/from ASCII. */
123165619Sglebiusstatic const struct ng_cmdlist ng_pred1_cmds[] = {
124165619Sglebius	{
125165619Sglebius	  NGM_PRED1_COOKIE,
126165619Sglebius	  NGM_PRED1_CONFIG,
127165619Sglebius	  "config",
128165619Sglebius	  &ng_pred1_config_type,
129165619Sglebius	  NULL
130165619Sglebius	},
131165619Sglebius	{
132165619Sglebius	  NGM_PRED1_COOKIE,
133165619Sglebius	  NGM_PRED1_RESETREQ,
134165619Sglebius	  "resetreq",
135165619Sglebius	  NULL,
136165619Sglebius	  NULL
137165619Sglebius	},
138165619Sglebius	{
139165619Sglebius	  NGM_PRED1_COOKIE,
140165619Sglebius	  NGM_PRED1_GET_STATS,
141165619Sglebius	  "getstats",
142165619Sglebius	  NULL,
143165619Sglebius	  &ng_pred1_stat_type
144165619Sglebius	},
145165619Sglebius	{
146165619Sglebius	  NGM_PRED1_COOKIE,
147165619Sglebius	  NGM_PRED1_CLR_STATS,
148165619Sglebius	  "clrstats",
149165619Sglebius	  NULL,
150165619Sglebius	  NULL
151165619Sglebius	},
152165619Sglebius	{
153165619Sglebius	  NGM_PRED1_COOKIE,
154165619Sglebius	  NGM_PRED1_GETCLR_STATS,
155165619Sglebius	  "getclrstats",
156165619Sglebius	  NULL,
157165619Sglebius	  &ng_pred1_stat_type
158165619Sglebius	},
159165619Sglebius	{ 0 }
160165619Sglebius};
161165619Sglebius
162165619Sglebius/* Node type descriptor */
163165619Sglebiusstatic struct ng_type ng_pred1_typestruct = {
164165619Sglebius	.version =	NG_ABI_VERSION,
165165619Sglebius	.name =		NG_PRED1_NODE_TYPE,
166165619Sglebius	.constructor =	ng_pred1_constructor,
167165619Sglebius	.rcvmsg =	ng_pred1_rcvmsg,
168165619Sglebius	.shutdown =	ng_pred1_shutdown,
169165619Sglebius	.newhook =	ng_pred1_newhook,
170165619Sglebius	.rcvdata =	ng_pred1_rcvdata,
171165619Sglebius	.disconnect =	ng_pred1_disconnect,
172165619Sglebius	.cmdlist =	ng_pred1_cmds,
173165619Sglebius};
174165619SglebiusNETGRAPH_INIT(pred1, &ng_pred1_typestruct);
175165619Sglebius
176165619Sglebius#define ERROUT(x)	do { error = (x); goto done; } while (0)
177165619Sglebius
178165619Sglebius/************************************************************************
179165619Sglebius			NETGRAPH NODE STUFF
180165619Sglebius ************************************************************************/
181165619Sglebius
182165619Sglebius/*
183165619Sglebius * Node type constructor
184165619Sglebius */
185165619Sglebiusstatic int
186165619Sglebiusng_pred1_constructor(node_p node)
187165619Sglebius{
188165619Sglebius	priv_p priv;
189165619Sglebius
190165619Sglebius	/* Allocate private structure. */
191165619Sglebius	priv = malloc(sizeof(*priv), M_NETGRAPH_PRED1, M_WAITOK | M_ZERO);
192165619Sglebius
193165619Sglebius	NG_NODE_SET_PRIVATE(node, priv);
194165619Sglebius
195165619Sglebius	/* This node is not thread safe. */
196165619Sglebius	NG_NODE_FORCE_WRITER(node);
197165619Sglebius
198165619Sglebius	/* Done */
199165619Sglebius	return (0);
200165619Sglebius}
201165619Sglebius
202165619Sglebius/*
203165619Sglebius * Give our OK for a hook to be added.
204165619Sglebius */
205165619Sglebiusstatic int
206165619Sglebiusng_pred1_newhook(node_p node, hook_p hook, const char *name)
207165619Sglebius{
208165619Sglebius	const priv_p priv = NG_NODE_PRIVATE(node);
209165619Sglebius
210165619Sglebius	if (NG_NODE_NUMHOOKS(node) > 0)
211165619Sglebius		return (EINVAL);
212165619Sglebius
213165619Sglebius	if (strcmp(name, NG_PRED1_HOOK_COMP) == 0)
214165619Sglebius		priv->compress = 1;
215165619Sglebius	else if (strcmp(name, NG_PRED1_HOOK_DECOMP) == 0)
216165619Sglebius		priv->compress = 0;
217165619Sglebius	else
218165619Sglebius		return (EINVAL);
219165619Sglebius
220165619Sglebius	return (0);
221165619Sglebius}
222165619Sglebius
223165619Sglebius/*
224165619Sglebius * Receive a control message.
225165619Sglebius */
226165619Sglebiusstatic int
227165619Sglebiusng_pred1_rcvmsg(node_p node, item_p item, hook_p lasthook)
228165619Sglebius{
229165619Sglebius	const priv_p priv = NG_NODE_PRIVATE(node);
230165619Sglebius	struct ng_mesg *resp = NULL;
231165619Sglebius	int error = 0;
232165619Sglebius	struct ng_mesg *msg;
233165619Sglebius
234165619Sglebius	NGI_GET_MSG(item, msg);
235165619Sglebius
236165619Sglebius	if (msg->header.typecookie != NGM_PRED1_COOKIE)
237165619Sglebius		ERROUT(EINVAL);
238166019Sglebius
239165619Sglebius	switch (msg->header.cmd) {
240165619Sglebius	case NGM_PRED1_CONFIG:
241165619Sglebius	    {
242165619Sglebius		struct ng_pred1_config *const cfg =
243165619Sglebius		    (struct ng_pred1_config *)msg->data;
244165619Sglebius
245165619Sglebius		/* Check configuration. */
246165619Sglebius		if (msg->header.arglen != sizeof(*cfg))
247165619Sglebius			ERROUT(EINVAL);
248165619Sglebius
249165619Sglebius		/* Configuration is OK, reset to it. */
250165619Sglebius		priv->cfg = *cfg;
251165619Sglebius
252165619Sglebius		/* Save return address so we can send reset-req's. */
253165619Sglebius		priv->ctrlnode = NGI_RETADDR(item);
254165619Sglebius
255165619Sglebius		/* Clear our state. */
256165619Sglebius		Pred1Init(node);
257165619Sglebius
258165619Sglebius		break;
259165619Sglebius	    }
260165619Sglebius	case NGM_PRED1_RESETREQ:
261165619Sglebius		Pred1Init(node);
262165619Sglebius		break;
263165619Sglebius
264165619Sglebius	case NGM_PRED1_GET_STATS:
265165619Sglebius	case NGM_PRED1_CLR_STATS:
266165619Sglebius	case NGM_PRED1_GETCLR_STATS:
267165619Sglebius	    {
268165619Sglebius		/* Create response. */
269165619Sglebius		if (msg->header.cmd != NGM_PRED1_CLR_STATS) {
270165619Sglebius			NG_MKRESPONSE(resp, msg,
271165619Sglebius			    sizeof(struct ng_pred1_stats), M_NOWAIT);
272165619Sglebius			if (resp == NULL)
273165619Sglebius				ERROUT(ENOMEM);
274165619Sglebius			bcopy(&priv->stats, resp->data,
275166019Sglebius			    sizeof(struct ng_pred1_stats));
276165619Sglebius		}
277165619Sglebius
278165619Sglebius		if (msg->header.cmd != NGM_PRED1_GET_STATS)
279165619Sglebius			bzero(&priv->stats, sizeof(struct ng_pred1_stats));
280165619Sglebius		break;
281165619Sglebius	    }
282165619Sglebius
283165619Sglebius	default:
284165619Sglebius		error = EINVAL;
285165619Sglebius		break;
286165619Sglebius	}
287165619Sglebiusdone:
288165619Sglebius	NG_RESPOND_MSG(error, node, item, resp);
289165619Sglebius	NG_FREE_MSG(msg);
290165619Sglebius	return (error);
291165619Sglebius}
292165619Sglebius
293165619Sglebius/*
294165619Sglebius * Receive incoming data on our hook.
295165619Sglebius */
296165619Sglebiusstatic int
297165619Sglebiusng_pred1_rcvdata(hook_p hook, item_p item)
298165619Sglebius{
299165619Sglebius	const node_p node = NG_HOOK_NODE(hook);
300165619Sglebius	const priv_p priv = NG_NODE_PRIVATE(node);
301165619Sglebius	struct mbuf *m, *out;
302165619Sglebius	int error;
303165619Sglebius
304165619Sglebius	if (!priv->cfg.enable) {
305165619Sglebius		NG_FREE_ITEM(item);
306165619Sglebius		return (ENXIO);
307165619Sglebius	}
308166019Sglebius
309165619Sglebius	NGI_GET_M(item, m);
310165619Sglebius	/* Compress. */
311165619Sglebius	if (priv->compress) {
312165619Sglebius		if ((error = ng_pred1_compress(node, m, &out)) != 0) {
313165619Sglebius			NG_FREE_ITEM(item);
314165619Sglebius			log(LOG_NOTICE, "%s: error: %d\n", __func__, error);
315165619Sglebius			return (error);
316165619Sglebius		}
317165619Sglebius
318165619Sglebius	} else { /* Decompress. */
319165619Sglebius		if ((error = ng_pred1_decompress(node, m, &out)) != 0) {
320165619Sglebius			NG_FREE_ITEM(item);
321165619Sglebius			log(LOG_NOTICE, "%s: error: %d\n", __func__, error);
322165619Sglebius			if (priv->ctrlnode != 0) {
323165619Sglebius				struct ng_mesg *msg;
324165619Sglebius
325165619Sglebius				/* Need to send a reset-request. */
326165619Sglebius				NG_MKMESSAGE(msg, NGM_PRED1_COOKIE,
327165619Sglebius				    NGM_PRED1_RESETREQ, 0, M_NOWAIT);
328165619Sglebius				if (msg == NULL)
329165619Sglebius					return (error);
330165619Sglebius				NG_SEND_MSG_ID(error, node, msg,
331165619Sglebius				    priv->ctrlnode, 0);
332165619Sglebius			}
333165619Sglebius			return (error);
334165619Sglebius		}
335165619Sglebius	}
336165619Sglebius
337165619Sglebius	NG_FWD_NEW_DATA(error, item, hook, out);
338165619Sglebius	return (error);
339165619Sglebius}
340165619Sglebius
341165619Sglebius/*
342165619Sglebius * Destroy node.
343165619Sglebius */
344165619Sglebiusstatic int
345165619Sglebiusng_pred1_shutdown(node_p node)
346165619Sglebius{
347165619Sglebius	const priv_p priv = NG_NODE_PRIVATE(node);
348165619Sglebius
349165619Sglebius	free(priv, M_NETGRAPH_PRED1);
350165619Sglebius	NG_NODE_SET_PRIVATE(node, NULL);
351165619Sglebius	NG_NODE_UNREF(node);		/* Let the node escape. */
352165619Sglebius	return (0);
353165619Sglebius}
354165619Sglebius
355165619Sglebius/*
356165619Sglebius * Hook disconnection
357165619Sglebius */
358165619Sglebiusstatic int
359165619Sglebiusng_pred1_disconnect(hook_p hook)
360165619Sglebius{
361165619Sglebius	const node_p node = NG_HOOK_NODE(hook);
362165619Sglebius
363165619Sglebius	Pred1Init(node);
364165619Sglebius
365165619Sglebius	/* Go away if no longer connected. */
366165619Sglebius	if ((NG_NODE_NUMHOOKS(node) == 0) && NG_NODE_IS_VALID(node))
367165619Sglebius		ng_rmnode_self(node);
368165619Sglebius	return (0);
369165619Sglebius}
370165619Sglebius
371165619Sglebius/************************************************************************
372165619Sglebius			HELPER STUFF
373165619Sglebius ************************************************************************/
374165619Sglebius
375165619Sglebius/*
376165619Sglebius * Compress/encrypt a packet and put the result in a new mbuf at *resultp.
377165619Sglebius * The original mbuf is not free'd.
378165619Sglebius */
379165619Sglebiusstatic int
380165619Sglebiusng_pred1_compress(node_p node, struct mbuf *m, struct mbuf **resultp)
381165619Sglebius{
382165619Sglebius	const priv_p 	priv = NG_NODE_PRIVATE(node);
383165619Sglebius	int 		outlen, inlen;
384165619Sglebius	u_char		*out;
385165619Sglebius	uint16_t	fcs, lenn;
386165619Sglebius	int		len;
387165619Sglebius
388165619Sglebius	/* Initialize. */
389165619Sglebius	*resultp = NULL;
390165619Sglebius
391165619Sglebius	inlen = m->m_pkthdr.len;
392165619Sglebius
393165619Sglebius	priv->stats.FramesPlain++;
394165619Sglebius	priv->stats.InOctets += inlen;
395165619Sglebius
396165619Sglebius	/* Reserve space for expansion. */
397165619Sglebius	if (inlen > (PRED1_BUF_SIZE*8/9 + 1 + 4)) {
398165619Sglebius		priv->stats.Errors++;
399165619Sglebius		NG_FREE_M(m);
400165619Sglebius		return (ENOMEM);
401165619Sglebius	}
402166019Sglebius
403187405Smav	/* We must own the mbuf chain exclusively to modify it. */
404243882Sglebius	m = m_unshare(m, M_NOWAIT);
405187405Smav	if (m == NULL) {
406187405Smav		priv->stats.Errors++;
407187405Smav		return (ENOMEM);
408187405Smav	}
409187405Smav
410165619Sglebius	/* Work with contiguous regions of memory. */
411165619Sglebius	m_copydata(m, 0, inlen, (caddr_t)(priv->inbuf + 2));
412165619Sglebius
413165619Sglebius	lenn = htons(inlen & 0x7FFF);
414165619Sglebius
415165619Sglebius	/* Compute FCS. */
416165619Sglebius	fcs = Crc16(PPP_INITFCS, (u_char *)&lenn, 2);
417165619Sglebius	fcs = Crc16(fcs, priv->inbuf + 2, inlen);
418165619Sglebius	fcs = ~fcs;
419165619Sglebius
420165619Sglebius	/* Compress data. */
421165619Sglebius	len = Pred1Compress(node, priv->inbuf + 2, priv->outbuf + 2, inlen);
422165619Sglebius
423165619Sglebius	/* What happened? */
424165619Sglebius	if (len < inlen) {
425165619Sglebius		out = priv->outbuf;
426165619Sglebius		outlen = 2 + len;
427165619Sglebius		*(uint16_t *)out = lenn;
428165619Sglebius		*out |= 0x80;
429165619Sglebius		priv->stats.FramesComp++;
430165619Sglebius	} else {
431165619Sglebius		out = priv->inbuf;
432165619Sglebius		outlen = 2 + inlen;
433165619Sglebius		*(uint16_t *)out = lenn;
434165619Sglebius		priv->stats.FramesUncomp++;
435165619Sglebius	}
436165619Sglebius
437165619Sglebius	/* Add FCS. */
438165619Sglebius	(out + outlen)[0] = fcs & 0xFF;
439165619Sglebius	(out + outlen)[1] = fcs >> 8;
440165619Sglebius
441165619Sglebius	/* Calculate resulting size. */
442165619Sglebius	outlen += 2;
443165619Sglebius
444165619Sglebius	/* Return packet in an mbuf. */
445187405Smav	m_copyback(m, 0, outlen, (caddr_t)out);
446187405Smav	if (m->m_pkthdr.len < outlen) {
447187405Smav		m_freem(m);
448187405Smav		priv->stats.Errors++;
449187405Smav		return (ENOMEM);
450187405Smav	} else if (outlen < m->m_pkthdr.len)
451187405Smav		m_adj(m, outlen - m->m_pkthdr.len);
452187405Smav	*resultp = m;
453165619Sglebius	priv->stats.OutOctets += outlen;
454165619Sglebius
455165619Sglebius	return (0);
456165619Sglebius}
457165619Sglebius
458165619Sglebius/*
459165619Sglebius * Decompress/decrypt packet and put the result in a new mbuf at *resultp.
460165619Sglebius * The original mbuf is not free'd.
461165619Sglebius */
462165619Sglebiusstatic int
463165619Sglebiusng_pred1_decompress(node_p node, struct mbuf *m, struct mbuf **resultp)
464165619Sglebius{
465165619Sglebius	const priv_p 	priv = NG_NODE_PRIVATE(node);
466165619Sglebius	int 		inlen;
467165619Sglebius	uint16_t	len, len1, cf, lenn;
468165619Sglebius	uint16_t	fcs;
469165619Sglebius
470165619Sglebius	/* Initialize. */
471165619Sglebius	*resultp = NULL;
472165619Sglebius
473165619Sglebius	inlen = m->m_pkthdr.len;
474166019Sglebius
475165619Sglebius	if (inlen > PRED1_BUF_SIZE) {
476165619Sglebius		priv->stats.Errors++;
477165619Sglebius		NG_FREE_M(m);
478165619Sglebius		return (ENOMEM);
479165619Sglebius	}
480165619Sglebius
481187405Smav	/* We must own the mbuf chain exclusively to modify it. */
482243882Sglebius	m = m_unshare(m, M_NOWAIT);
483187405Smav	if (m == NULL) {
484187405Smav		priv->stats.Errors++;
485187405Smav		return (ENOMEM);
486187405Smav	}
487187405Smav
488165619Sglebius	/* Work with contiguous regions of memory. */
489165619Sglebius	m_copydata(m, 0, inlen, (caddr_t)priv->inbuf);
490165619Sglebius
491165619Sglebius	priv->stats.InOctets += inlen;
492166019Sglebius
493165619Sglebius	/* Get initial length value. */
494165619Sglebius	len = priv->inbuf[0] << 8;
495165619Sglebius	len += priv->inbuf[1];
496165619Sglebius
497165619Sglebius	cf = (len & 0x8000);
498165619Sglebius	len &= 0x7fff;
499165619Sglebius
500165619Sglebius	/* Is data compressed or not really? */
501165619Sglebius	if (cf) {
502165619Sglebius		priv->stats.FramesComp++;
503165619Sglebius		len1 = Pred1Decompress(node, priv->inbuf + 2, priv->outbuf,
504165619Sglebius		    inlen - 4, PRED1_BUF_SIZE);
505165619Sglebius		if (len != len1) {
506165619Sglebius			/* Error is detected. Send reset request */
507187405Smav			m_freem(m);
508165619Sglebius			priv->stats.Errors++;
509165619Sglebius			log(LOG_NOTICE, "ng_pred1: Comp length error (%d) "
510165619Sglebius			    "--> len (%d)\n", len, len1);
511165619Sglebius			return (EIO);
512165619Sglebius		}
513165619Sglebius
514165619Sglebius		/*
515165619Sglebius		 * CRC check on receive is defined in RFC. It is surely required
516165619Sglebius		 * for compressed frames to signal dictionary corruption,
517165619Sglebius		 * but it is actually useless for uncompressed frames because
518165619Sglebius		 * the same check has already done by HDLC and/or other layer.
519165619Sglebius		 */
520165619Sglebius		lenn = htons(len);
521165619Sglebius		fcs = Crc16(PPP_INITFCS, (u_char *)&lenn, 2);
522165619Sglebius		fcs = Crc16(fcs, priv->outbuf, len);
523165619Sglebius		fcs = Crc16(fcs, priv->inbuf + inlen - 2, 2);
524165619Sglebius
525165619Sglebius		if (fcs != PPP_GOODFCS) {
526187405Smav			m_freem(m);
527165619Sglebius			priv->stats.Errors++;
528165619Sglebius	    		log(LOG_NOTICE, "ng_pred1: Pred1: Bad CRC-16\n");
529165619Sglebius			return (EIO);
530165619Sglebius		}
531165619Sglebius
532165619Sglebius		/* Return packet in an mbuf. */
533187405Smav		m_copyback(m, 0, len, (caddr_t)priv->outbuf);
534187405Smav		if (m->m_pkthdr.len < len) {
535187405Smav			m_freem(m);
536165619Sglebius			priv->stats.Errors++;
537165619Sglebius			return (ENOMEM);
538187405Smav		} else if (len < m->m_pkthdr.len)
539187405Smav			m_adj(m, len - m->m_pkthdr.len);
540187405Smav		*resultp = m;
541165619Sglebius
542165619Sglebius	} else {
543165619Sglebius		priv->stats.FramesUncomp++;
544165619Sglebius		if (len != (inlen - 4)) {
545165619Sglebius			/* Wrong length. Send reset request */
546165619Sglebius			priv->stats.Errors++;
547165619Sglebius			log(LOG_NOTICE, "ng_pred1: Uncomp length error (%d) "
548165619Sglebius			    "--> len (%d)\n", len, inlen - 4);
549165619Sglebius			NG_FREE_M(m);
550165619Sglebius			return (EIO);
551165619Sglebius		}
552165619Sglebius		Pred1SyncTable(node, priv->inbuf + 2, len);
553165619Sglebius		m_adj(m, 2);	/* Strip length. */
554165619Sglebius		m_adj(m, -2);	/* Strip fcs. */
555165619Sglebius		*resultp = m;
556165619Sglebius	}
557165619Sglebius
558165619Sglebius	priv->stats.FramesPlain++;
559165619Sglebius	priv->stats.OutOctets += len;
560165619Sglebius
561165619Sglebius	return (0);
562165619Sglebius}
563165619Sglebius
564165619Sglebius/*
565165619Sglebius * Pred1Init()
566165619Sglebius */
567165619Sglebius
568165619Sglebiusstatic void
569165619SglebiusPred1Init(node_p node)
570165619Sglebius{
571165619Sglebius	const priv_p priv = NG_NODE_PRIVATE(node);
572165619Sglebius
573165619Sglebius	priv->Hash = 0;
574165619Sglebius	memset(priv->GuessTable, 0, PRED1_TABLE_SIZE);
575165619Sglebius}
576165619Sglebius
577165619Sglebius/*
578165619Sglebius * Pred1Compress()
579165619Sglebius */
580165619Sglebius
581165619Sglebiusstatic int
582165619SglebiusPred1Compress(node_p node, u_char *source, u_char *dest, int len)
583165619Sglebius{
584165619Sglebius	const priv_p 	priv = NG_NODE_PRIVATE(node);
585175706Smav	int		i;
586165619Sglebius	u_char		flags;
587165619Sglebius	u_char		*flagdest, *orgdest;
588165619Sglebius
589165619Sglebius	orgdest = dest;
590165619Sglebius	while (len) {
591165619Sglebius		flagdest = dest++;
592165619Sglebius		flags = 0;	/* All guesses are wrong initially. */
593175706Smav		for (i = 0; i < 8 && len; i++) {
594165619Sglebius    			if (priv->GuessTable[priv->Hash] == *source)
595165619Sglebius				/* Guess was right - don't output. */
596175706Smav				flags |= (1 << i);
597165619Sglebius    			else {
598165619Sglebius				/* Guess wrong, output char. */
599165619Sglebius				priv->GuessTable[priv->Hash] = *source;
600165619Sglebius				*dest++ = *source;
601165619Sglebius    			}
602165619Sglebius    			HASH(*source++);
603165619Sglebius    			len--;
604165619Sglebius		}
605165619Sglebius		*flagdest = flags;
606165619Sglebius	}
607165619Sglebius	return (dest - orgdest);
608165619Sglebius}
609165619Sglebius
610165619Sglebius/*
611165619Sglebius * Pred1Decompress()
612165619Sglebius *
613165619Sglebius * Returns decompressed size, or -1 if we ran out of space.
614165619Sglebius */
615165619Sglebius
616165619Sglebiusstatic int
617165619SglebiusPred1Decompress(node_p node, u_char *source, u_char *dest, int slen, int dlen)
618165619Sglebius{
619165619Sglebius	const priv_p 	priv = NG_NODE_PRIVATE(node);
620175706Smav	int		i;
621165619Sglebius	u_char		flags, *orgdest;
622165619Sglebius
623165619Sglebius	orgdest = dest;
624165619Sglebius	while (slen) {
625165619Sglebius		flags = *source++;
626165619Sglebius		slen--;
627175706Smav		for (i = 0; i < 8; i++, flags >>= 1) {
628165619Sglebius			if (dlen <= 0)
629165619Sglebius				return(-1);
630175706Smav			if (flags & 0x01)
631165619Sglebius				/* Guess correct */
632165619Sglebius				*dest = priv->GuessTable[priv->Hash];
633165619Sglebius			else {
634165619Sglebius				if (!slen)
635165619Sglebius					/* We seem to be really done -- cabo. */
636165619Sglebius					break;
637165619Sglebius
638165619Sglebius				/* Guess wrong. */
639165619Sglebius				priv->GuessTable[priv->Hash] = *source;
640165619Sglebius				/* Read from source. */
641165619Sglebius				*dest = *source++;
642165619Sglebius				slen--;
643165619Sglebius			}
644165619Sglebius			HASH(*dest++);
645165619Sglebius			dlen--;
646165619Sglebius		}
647165619Sglebius	}
648165619Sglebius	return (dest - orgdest);
649165619Sglebius}
650165619Sglebius
651165619Sglebius/*
652165619Sglebius * Pred1SyncTable()
653165619Sglebius */
654165619Sglebius
655165619Sglebiusstatic void
656165619SglebiusPred1SyncTable(node_p node, u_char *source, int len)
657165619Sglebius{
658165619Sglebius	const priv_p priv = NG_NODE_PRIVATE(node);
659165619Sglebius
660165619Sglebius	while (len--) {
661165619Sglebius		priv->GuessTable[priv->Hash] = *source;
662165619Sglebius		HASH(*source++);
663165619Sglebius	}
664165619Sglebius}
665165619Sglebius
666165619Sglebius/*
667165619Sglebius * Crc16()
668165619Sglebius *
669165619Sglebius * Compute the 16 bit frame check value, per RFC 1171 Appendix B,
670165619Sglebius * on an array of bytes.
671165619Sglebius */
672165619Sglebius
673165619Sglebiusstatic uint16_t
674165619SglebiusCrc16(uint16_t crc, u_char *cp, int len)
675165619Sglebius{
676165619Sglebius	while (len--)
677165619Sglebius		crc = (crc >> 8) ^ Crc16Table[(crc ^ *cp++) & 0xff];
678165619Sglebius	return (crc);
679165619Sglebius}
680165619Sglebius
681165619Sglebiusstatic const uint16_t Crc16Table[256] = {
682165619Sglebius/* 00 */    0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
683165619Sglebius/* 08 */    0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
684165619Sglebius/* 10 */    0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
685165619Sglebius/* 18 */    0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
686165619Sglebius/* 20 */    0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
687165619Sglebius/* 28 */    0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
688165619Sglebius/* 30 */    0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
689165619Sglebius/* 38 */    0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
690165619Sglebius/* 40 */    0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
691165619Sglebius/* 48 */    0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
692165619Sglebius/* 50 */    0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
693165619Sglebius/* 58 */    0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
694165619Sglebius/* 60 */    0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
695165619Sglebius/* 68 */    0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
696165619Sglebius/* 70 */    0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
697165619Sglebius/* 78 */    0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
698165619Sglebius/* 80 */    0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
699165619Sglebius/* 88 */    0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
700165619Sglebius/* 90 */    0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
701165619Sglebius/* 98 */    0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
702165619Sglebius/* a0 */    0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
703165619Sglebius/* a8 */    0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
704165619Sglebius/* b0 */    0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
705165619Sglebius/* b8 */    0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
706165619Sglebius/* c0 */    0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
707165619Sglebius/* c8 */    0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
708165619Sglebius/* d0 */    0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
709165619Sglebius/* d8 */    0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
710165619Sglebius/* e0 */    0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
711165619Sglebius/* e8 */    0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
712165619Sglebius/* f0 */    0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
713165619Sglebius/* f8 */    0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
714165619Sglebius};
715165619Sglebius
716