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@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_vjc.c,v 1.9.2.5 2002/07/02 23:44:03 archie Exp $
40 * $DragonFly: src/sys/netgraph/vjc/ng_vjc.c,v 1.4 2004/06/02 14:43:01 eirikn Exp $
41 * $Whistle: ng_vjc.c,v 1.17 1999/11/01 09:24:52 julian Exp $
45 * This node performs Van Jacobson IP header (de)compression.
46 * You must have included net/slcompress.c in your kernel compilation.
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/errno.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/errno.h>
57 #include <netgraph/ng_message.h>
58 #include <netgraph/netgraph.h>
59 #include <netgraph/ng_parse.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/tcp.h>
67 #include <net/slcompress.h>
69 /* Check agreement with slcompress.c */
70 #if MAX_STATES != NG_VJC_MAX_CHANNELS
71 #error NG_VJC_MAX_CHANNELS must be the same as MAX_STATES
74 /* Maximum length of a compressed TCP VJ header */
75 #define MAX_VJHEADER 19
77 /* Node private data */
78 struct ng_vjc_private {
79 struct ngm_vjc_config conf;
80 struct slcompress slc;
86 typedef struct ng_vjc_private *priv_p;
88 #define ERROUT(x) do { error = (x); goto done; } while (0)
90 /* Netgraph node methods */
91 static ng_constructor_t ng_vjc_constructor;
92 static ng_rcvmsg_t ng_vjc_rcvmsg;
93 static ng_shutdown_t ng_vjc_rmnode;
94 static ng_newhook_t ng_vjc_newhook;
95 static ng_rcvdata_t ng_vjc_rcvdata;
96 static ng_disconnect_t ng_vjc_disconnect;
99 static struct mbuf *ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP);
101 /* Parse type for struct ngm_vjc_config */
102 static const struct ng_parse_struct_field ng_vjc_config_type_fields[]
103 = NG_VJC_CONFIG_TYPE_INFO;
104 static const struct ng_parse_type ng_vjc_config_type = {
105 &ng_parse_struct_type,
106 &ng_vjc_config_type_fields
109 /* Parse type for the 'last_cs' and 'cs_next' fields in struct slcompress,
110 which are pointers converted to integer indices, so parse them that way. */
111 #if _MACHINE_ARCH == i386
112 #define NG_VJC_TSTATE_PTR_TYPE &ng_parse_uint32_type
113 #elif _MACHINE_ARCH == alpha
114 #define NG_VJC_TSTATE_PTR_TYPE &ng_parse_uint64_type
116 #error Unspported _MACHINE_ARCH
119 /* Parse type for the 'cs_hdr' field in a struct cstate. Ideally we would
120 like to use a 'struct ip' type instead of a simple array of bytes. */
121 static const struct ng_parse_fixedarray_info ng_vjc_cs_hdr_type_info = {
122 &ng_parse_hint8_type,
125 static const struct ng_parse_type ng_vjc_cs_hdr_type = {
126 &ng_parse_fixedarray_type,
127 &ng_vjc_cs_hdr_type_info
130 /* Parse type for a struct cstate */
131 static const struct ng_parse_struct_field ng_vjc_cstate_type_fields[] = {
132 { "cs_next", NG_VJC_TSTATE_PTR_TYPE },
133 { "cs_hlen", &ng_parse_uint16_type },
134 { "cs_id", &ng_parse_uint8_type },
135 { "cs_filler", &ng_parse_uint8_type },
136 { "cs_hdr", &ng_vjc_cs_hdr_type },
139 static const struct ng_parse_type ng_vjc_cstate_type = {
140 &ng_parse_struct_type,
141 &ng_vjc_cstate_type_fields
144 /* Parse type for an array of MAX_STATES struct cstate's, ie, tstate & rstate */
145 static const struct ng_parse_fixedarray_info ng_vjc_cstatearray_type_info = {
149 static const struct ng_parse_type ng_vjc_cstatearray_type = {
150 &ng_parse_fixedarray_type,
151 &ng_vjc_cstatearray_type_info
154 /* Parse type for struct slcompress. Keep this in sync with the
155 definition of struct slcompress defined in <net/slcompress.h> */
156 static const struct ng_parse_struct_field ng_vjc_slcompress_type_fields[] = {
157 { "last_cs", NG_VJC_TSTATE_PTR_TYPE },
158 { "last_recv", &ng_parse_uint8_type },
159 { "last_xmit", &ng_parse_uint8_type },
160 { "flags", &ng_parse_hint16_type },
162 { "sls_packets", &ng_parse_uint32_type },
163 { "sls_compressed", &ng_parse_uint32_type },
164 { "sls_searches", &ng_parse_uint32_type },
165 { "sls_misses", &ng_parse_uint32_type },
166 { "sls_uncompressedin", &ng_parse_uint32_type },
167 { "sls_compressedin", &ng_parse_uint32_type },
168 { "sls_errorin", &ng_parse_uint32_type },
169 { "sls_tossed", &ng_parse_uint32_type },
171 { "tstate", &ng_vjc_cstatearray_type },
172 { "rstate", &ng_vjc_cstatearray_type },
175 static const struct ng_parse_type ng_vjc_slcompress_type = {
176 &ng_parse_struct_type,
177 &ng_vjc_slcompress_type_fields
180 /* List of commands and how to convert arguments to/from ASCII */
181 static const struct ng_cmdlist ng_vjc_cmds[] = {
201 &ng_vjc_slcompress_type,
220 /* Node type descriptor */
221 static struct ng_type ng_vjc_typestruct = {
236 NETGRAPH_INIT(vjc, &ng_vjc_typestruct);
238 /************************************************************************
239 NETGRAPH NODE METHODS
240 ************************************************************************/
246 ng_vjc_constructor(node_p *nodep)
251 /* Allocate private structure */
252 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
255 bzero(priv, sizeof(*priv));
257 /* Call generic node constructor */
258 if ((error = ng_make_node_common(&ng_vjc_typestruct, nodep))) {
259 FREE(priv, M_NETGRAPH);
262 (*nodep)->private = priv;
272 ng_vjc_newhook(node_p node, hook_p hook, const char *name)
274 const priv_p priv = (priv_p) node->private;
278 if (strcmp(name, NG_VJC_HOOK_IP) == 0)
280 else if (strcmp(name, NG_VJC_HOOK_VJCOMP) == 0)
281 hookp = &priv->vjcomp;
282 else if (strcmp(name, NG_VJC_HOOK_VJUNCOMP) == 0)
283 hookp = &priv->vjuncomp;
284 else if (strcmp(name, NG_VJC_HOOK_VJIP) == 0)
289 /* See if already connected */
299 * Receive a control message
302 ng_vjc_rcvmsg(node_p node, struct ng_mesg *msg,
303 const char *raddr, struct ng_mesg **rptr)
305 const priv_p priv = (priv_p) node->private;
306 struct ng_mesg *resp = NULL;
309 /* Check type cookie */
310 switch (msg->header.typecookie) {
312 switch (msg->header.cmd) {
313 case NGM_VJC_SET_CONFIG:
315 struct ngm_vjc_config *const c =
316 (struct ngm_vjc_config *) msg->data;
318 if (msg->header.arglen != sizeof(*c))
320 if ((priv->conf.enableComp || priv->conf.enableDecomp)
321 && (c->enableComp || c->enableDecomp))
324 if (c->maxChannel > NG_VJC_MAX_CHANNELS - 1
325 || c->maxChannel < NG_VJC_MIN_CHANNELS - 1)
328 c->maxChannel = NG_VJC_MAX_CHANNELS - 1;
329 if (c->enableComp != 0 || c->enableDecomp != 0) {
330 bzero(&priv->slc, sizeof(priv->slc));
331 sl_compress_init(&priv->slc, c->maxChannel);
336 case NGM_VJC_GET_CONFIG:
338 struct ngm_vjc_config *conf;
340 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
343 conf = (struct ngm_vjc_config *)resp->data;
347 case NGM_VJC_GET_STATE:
349 const struct slcompress *const sl0 = &priv->slc;
350 struct slcompress *sl;
354 /* Get response structure */
355 NG_MKRESPONSE(resp, msg, sizeof(*sl), M_NOWAIT);
358 sl = (struct slcompress *)resp->data;
361 /* Replace pointers with integer indicies */
362 if (sl->last_cs != NULL) {
363 index = sl0->last_cs - sl0->tstate;
364 bzero(&sl->last_cs, sizeof(sl->last_cs));
365 *((u_int16_t *)&sl->last_cs) = index;
367 for (i = 0; i < MAX_STATES; i++) {
368 struct cstate *const cs = &sl->tstate[i];
370 index = sl0->tstate[i].cs_next - sl0->tstate;
371 bzero(&cs->cs_next, sizeof(cs->cs_next));
372 *((u_int16_t *)&cs->cs_next) = index;
376 case NGM_VJC_CLR_STATS:
377 priv->slc.sls_packets = 0;
378 priv->slc.sls_compressed = 0;
379 priv->slc.sls_searches = 0;
380 priv->slc.sls_misses = 0;
381 priv->slc.sls_uncompressedin = 0;
382 priv->slc.sls_compressedin = 0;
383 priv->slc.sls_errorin = 0;
384 priv->slc.sls_tossed = 0;
386 case NGM_VJC_RECV_ERROR:
387 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &priv->slc);
401 FREE(resp, M_NETGRAPH);
404 FREE(msg, M_NETGRAPH);
412 ng_vjc_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
414 const node_p node = hook->node;
415 const priv_p priv = (priv_p) node->private;
418 if (hook == priv->ip) { /* outgoing packet */
419 u_int type = TYPE_IP;
421 /* Compress packet if enabled and proto is TCP */
422 if (priv->conf.enableComp) {
425 if ((m = ng_vjc_pulluphdrs(m, 0)) == NULL) {
429 ip = mtod(m, struct ip *);
430 if (ip->ip_p == IPPROTO_TCP) {
431 const int origLen = m->m_len;
433 type = sl_compress_tcp(m, ip,
434 &priv->slc, priv->conf.compressCID);
435 m->m_pkthdr.len += m->m_len - origLen;
439 /* Dispatch to the appropriate outgoing hook */
444 case TYPE_UNCOMPRESSED_TCP:
445 hook = priv->vjuncomp;
447 case TYPE_COMPRESSED_TCP:
451 panic("%s: type=%d", __FUNCTION__, type);
453 } else if (hook == priv->vjcomp) { /* incoming compressed packet */
454 int vjlen, need2pullup;
459 /* Are we decompressing? */
460 if (!priv->conf.enableDecomp) {
461 NG_FREE_DATA(m, meta);
465 /* Pull up the necessary amount from the mbuf */
466 need2pullup = MAX_VJHEADER;
467 if (need2pullup > m->m_pkthdr.len)
468 need2pullup = m->m_pkthdr.len;
469 if (m->m_len < need2pullup
470 && (m = m_pullup(m, need2pullup)) == NULL) {
471 priv->slc.sls_errorin++;
476 /* Uncompress packet to reconstruct TCP/IP header */
477 vjlen = sl_uncompress_tcp_core(mtod(m, u_char *),
478 m->m_len, m->m_pkthdr.len, TYPE_COMPRESSED_TCP,
479 &priv->slc, &hdr, &hlen);
481 NG_FREE_DATA(m, meta);
486 /* Copy the reconstructed TCP/IP headers into a new mbuf */
487 MGETHDR(hm, MB_DONTWAIT, MT_DATA);
489 priv->slc.sls_errorin++;
490 NG_FREE_DATA(m, meta);
494 hm->m_pkthdr.rcvif = NULL;
495 if (hlen > MHLEN) { /* unlikely, but can happen */
496 MCLGET(hm, MB_DONTWAIT);
497 if ((hm->m_flags & M_EXT) == 0) {
499 priv->slc.sls_errorin++;
500 NG_FREE_DATA(m, meta);
504 bcopy(hdr, mtod(hm, u_char *), hlen);
507 /* Glue TCP/IP headers and rest of packet together */
509 hm->m_pkthdr.len = hlen + m->m_pkthdr.len;
512 } else if (hook == priv->vjuncomp) { /* incoming uncompressed pkt */
516 /* Are we decompressing? */
517 if (!priv->conf.enableDecomp) {
518 NG_FREE_DATA(m, meta);
522 /* Pull up IP+TCP headers */
523 if ((m = ng_vjc_pulluphdrs(m, 1)) == NULL) {
528 /* Run packet through uncompressor */
529 if (sl_uncompress_tcp_core(mtod(m, u_char *),
530 m->m_len, m->m_pkthdr.len, TYPE_UNCOMPRESSED_TCP,
531 &priv->slc, &hdr, &hlen) < 0) {
532 NG_FREE_DATA(m, meta);
536 } else if (hook == priv->vjip) /* incoming regular packet (bypass) */
539 panic("%s: unknown hook", __FUNCTION__);
541 /* Send result back out */
542 NG_SEND_DATA(error, hook, m, meta);
550 ng_vjc_rmnode(node_p node)
552 const priv_p priv = (priv_p) node->private;
554 node->flags |= NG_INVALID;
557 bzero(priv, sizeof(*priv));
558 FREE(priv, M_NETGRAPH);
559 node->private = NULL;
568 ng_vjc_disconnect(hook_p hook)
570 const node_p node = hook->node;
571 const priv_p priv = node->private;
573 /* Zero out hook pointer */
574 if (hook == priv->ip)
576 else if (hook == priv->vjcomp)
578 else if (hook == priv->vjuncomp)
579 priv->vjuncomp = NULL;
580 else if (hook == priv->vjip)
583 panic("%s: unknown hook", __FUNCTION__);
585 /* Go away if no hooks left */
586 if (node->numhooks == 0)
591 /************************************************************************
593 ************************************************************************/
596 * Pull up the full IP and TCP headers of a packet. If packet is not
597 * a TCP packet, just pull up the IP header.
600 ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP)
606 if (m->m_len < sizeof(*ip) && (m = m_pullup(m, sizeof(*ip))) == NULL)
608 ip = mtod(m, struct ip *);
609 if (!knownTCP && ip->ip_p != IPPROTO_TCP)
611 ihlen = ip->ip_hl << 2;
612 if (m->m_len < ihlen + sizeof(*tcp)) {
613 if ((m = m_pullup(m, ihlen + sizeof(*tcp))) == NULL)
615 ip = mtod(m, struct ip *);
617 tcp = (struct tcphdr *)((u_char *)ip + ihlen);
618 thlen = tcp->th_off << 2;
619 if (m->m_len < ihlen + thlen)
620 m = m_pullup(m, ihlen + thlen);