5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Archie Cobbs <archie@whistle.com>
40 * $Whistle: ng_vjc.c,v 1.17 1999/11/01 09:24:52 julian Exp $
44 * This node performs Van Jacobsen IP header (de)compression.
45 * You must have included net/slcompress.c in your kernel compilation.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/errno.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/errno.h>
56 #include <netgraph/ng_message.h>
57 #include <netgraph/netgraph.h>
58 #include <netgraph/ng_vjc.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/tcp.h>
65 #include <net/slcompress.h>
67 /* Check agreement with slcompress.c */
68 #if MAX_STATES != NG_VJC_MAX_CHANNELS
69 #error NG_VJC_MAX_CHANNELS must be the same as MAX_STATES
72 /* Maximum length of a compressed TCP VJ header */
73 #define MAX_VJHEADER 19
75 /* Node private data */
76 struct ng_vjc_private {
77 struct ngm_vjc_config conf;
78 struct slcompress slc;
84 typedef struct ng_vjc_private *priv_p;
86 #define ERROUT(x) do { error = (x); goto done; } while (0)
88 /* Netgraph node methods */
89 static ng_constructor_t ng_vjc_constructor;
90 static ng_rcvmsg_t ng_vjc_rcvmsg;
91 static ng_shutdown_t ng_vjc_rmnode;
92 static ng_newhook_t ng_vjc_newhook;
93 static ng_rcvdata_t ng_vjc_rcvdata;
94 static ng_disconnect_t ng_vjc_disconnect;
97 static struct mbuf *ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP);
99 /* Node type descriptor */
100 static struct ng_type typestruct = {
115 NETGRAPH_INIT(vjc, &typestruct);
117 /************************************************************************
118 NETGRAPH NODE METHODS
119 ************************************************************************/
125 ng_vjc_constructor(node_p *nodep)
130 /* Allocate private structure */
131 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK);
134 bzero(priv, sizeof(*priv));
136 /* Call generic node constructor */
137 if ((error = ng_make_node_common(&typestruct, nodep))) {
138 FREE(priv, M_NETGRAPH);
141 (*nodep)->private = priv;
151 ng_vjc_newhook(node_p node, hook_p hook, const char *name)
153 const priv_p priv = (priv_p) node->private;
157 if (strcmp(name, NG_VJC_HOOK_IP) == 0)
159 else if (strcmp(name, NG_VJC_HOOK_VJCOMP) == 0)
160 hookp = &priv->vjcomp;
161 else if (strcmp(name, NG_VJC_HOOK_VJUNCOMP) == 0)
162 hookp = &priv->vjuncomp;
163 else if (strcmp(name, NG_VJC_HOOK_VJIP) == 0)
168 /* See if already connected */
178 * Receive a control message
181 ng_vjc_rcvmsg(node_p node, struct ng_mesg *msg,
182 const char *raddr, struct ng_mesg **rptr)
184 const priv_p priv = (priv_p) node->private;
185 struct ng_mesg *resp = NULL;
188 /* Check type cookie */
189 switch (msg->header.typecookie) {
191 switch (msg->header.cmd) {
192 case NGM_VJC_SET_CONFIG:
194 struct ngm_vjc_config *const c =
195 (struct ngm_vjc_config *) msg->data;
197 if (msg->header.arglen != sizeof(*c))
199 if ((priv->conf.enableComp || priv->conf.enableDecomp)
200 && (c->enableComp || c->enableDecomp))
203 if (c->maxChannel > NG_VJC_MAX_CHANNELS - 1
204 || c->maxChannel < NG_VJC_MIN_CHANNELS - 1)
207 c->maxChannel = NG_VJC_MAX_CHANNELS - 1;
208 if (c->enableComp != 0 || c->enableDecomp != 0) {
209 bzero(&priv->slc, sizeof(priv->slc));
210 sl_compress_init(&priv->slc, c->maxChannel);
215 case NGM_VJC_GET_STATE:
216 NG_MKRESPONSE(resp, msg, sizeof(priv->slc), M_NOWAIT);
219 *((struct slcompress *) resp->data) = priv->slc;
221 case NGM_VJC_CLR_STATS:
222 priv->slc.sls_packets = 0;
223 priv->slc.sls_compressed = 0;
224 priv->slc.sls_searches = 0;
225 priv->slc.sls_misses = 0;
226 priv->slc.sls_uncompressedin = 0;
227 priv->slc.sls_compressedin = 0;
228 priv->slc.sls_errorin = 0;
229 priv->slc.sls_tossed = 0;
231 case NGM_VJC_RECV_ERROR:
232 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &priv->slc);
246 FREE(resp, M_NETGRAPH);
249 FREE(msg, M_NETGRAPH);
257 ng_vjc_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
259 const node_p node = hook->node;
260 const priv_p priv = (priv_p) node->private;
263 if (hook == priv->ip) { /* outgoing packet */
264 u_int type = TYPE_IP;
266 /* Compress packet if enabled and proto is TCP */
267 if (priv->conf.enableComp) {
270 if ((m = ng_vjc_pulluphdrs(m, 0)) == NULL) {
274 ip = mtod(m, struct ip *);
275 if (ip->ip_p == IPPROTO_TCP) {
276 const int origLen = m->m_len;
278 type = sl_compress_tcp(m, ip,
279 &priv->slc, priv->conf.compressCID);
280 m->m_pkthdr.len += m->m_len - origLen;
284 /* Dispatch to the appropriate outgoing hook */
289 case TYPE_UNCOMPRESSED_TCP:
290 hook = priv->vjuncomp;
292 case TYPE_COMPRESSED_TCP:
296 panic("%s: type=%d", __FUNCTION__, type);
298 } else if (hook == priv->vjcomp) { /* incoming compressed packet */
299 int vjlen, need2pullup;
304 /* Are we decompressing? */
305 if (!priv->conf.enableDecomp) {
306 NG_FREE_DATA(m, meta);
310 /* Pull up the necessary amount from the mbuf */
311 need2pullup = MAX_VJHEADER;
312 if (need2pullup > m->m_pkthdr.len)
313 need2pullup = m->m_pkthdr.len;
314 if (m->m_len < need2pullup
315 && (m = m_pullup(m, need2pullup)) == NULL) {
316 priv->slc.sls_errorin++;
321 /* Uncompress packet to reconstruct TCP/IP header */
322 vjlen = sl_uncompress_tcp_core(mtod(m, u_char *),
323 m->m_len, m->m_pkthdr.len, TYPE_COMPRESSED_TCP,
324 &priv->slc, &hdr, &hlen);
326 NG_FREE_DATA(m, meta);
331 /* Copy the reconstructed TCP/IP headers into a new mbuf */
332 MGETHDR(hm, M_DONTWAIT, MT_DATA);
334 priv->slc.sls_errorin++;
335 NG_FREE_DATA(m, meta);
339 hm->m_pkthdr.rcvif = NULL;
340 if (hlen > MHLEN) { /* unlikely, but can happen */
341 MCLGET(hm, M_DONTWAIT);
342 if ((hm->m_flags & M_EXT) == 0) {
344 priv->slc.sls_errorin++;
345 NG_FREE_DATA(m, meta);
349 bcopy(hdr, mtod(hm, u_char *), hlen);
352 /* Glue TCP/IP headers and rest of packet together */
354 hm->m_pkthdr.len = hlen + m->m_pkthdr.len;
357 } else if (hook == priv->vjuncomp) { /* incoming uncompressed pkt */
361 /* Are we decompressing? */
362 if (!priv->conf.enableDecomp) {
363 NG_FREE_DATA(m, meta);
367 /* Pull up IP+TCP headers */
368 if ((m = ng_vjc_pulluphdrs(m, 1)) == NULL) {
373 /* Run packet through uncompressor */
374 if (sl_uncompress_tcp_core(mtod(m, u_char *),
375 m->m_len, m->m_pkthdr.len, TYPE_UNCOMPRESSED_TCP,
376 &priv->slc, &hdr, &hlen) < 0) {
377 NG_FREE_DATA(m, meta);
381 } else if (hook == priv->vjip) /* incoming regular packet (bypass) */
384 panic("%s: unknown hook", __FUNCTION__);
386 /* Send result back out */
387 NG_SEND_DATA(error, hook, m, meta);
395 ng_vjc_rmnode(node_p node)
397 const priv_p priv = (priv_p) node->private;
399 node->flags |= NG_INVALID;
402 bzero(priv, sizeof(*priv));
403 FREE(priv, M_NETGRAPH);
404 node->private = NULL;
413 ng_vjc_disconnect(hook_p hook)
415 const node_p node = hook->node;
416 const priv_p priv = node->private;
418 /* Zero out hook pointer */
419 if (hook == priv->ip)
421 else if (hook == priv->vjcomp)
423 else if (hook == priv->vjuncomp)
424 priv->vjuncomp = NULL;
425 else if (hook == priv->vjip)
428 panic("%s: unknown hook", __FUNCTION__);
430 /* Go away if no hooks left */
431 if (node->numhooks == 0)
436 /************************************************************************
438 ************************************************************************/
441 * Pull up the full IP and TCP headers of a packet. If packet is not
442 * a TCP packet, just pull up the IP header.
445 ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP)
451 if (m->m_len < sizeof(*ip) && (m = m_pullup(m, sizeof(*ip))) == NULL)
453 ip = mtod(m, struct ip *);
454 if (!knownTCP && ip->ip_p != IPPROTO_TCP)
456 ihlen = ip->ip_hl << 2;
457 if (m->m_len < ihlen + sizeof(*tcp)) {
458 if ((m = m_pullup(m, ihlen + sizeof(*tcp))) == NULL)
460 ip = mtod(m, struct ip *);
462 tcp = (struct tcphdr *)((u_char *)ip + ihlen);
463 thlen = tcp->th_off << 2;
464 if (m->m_len < ihlen + thlen)
465 m = m_pullup(m, ihlen + thlen);