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 * $Whistle: ng_vjc.c,v 1.17 1999/11/01 09:24:52 julian Exp $
44 * This node performs Van Jacobson 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_parse.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/tcp.h>
66 #include <net/slcompress.h>
68 /* Check agreement with slcompress.c */
69 #if MAX_STATES != NG_VJC_MAX_CHANNELS
70 #error NG_VJC_MAX_CHANNELS must be the same as MAX_STATES
73 /* Maximum length of a compressed TCP VJ header */
74 #define MAX_VJHEADER 19
76 /* Node private data */
77 struct ng_vjc_private {
78 struct ngm_vjc_config conf;
79 struct slcompress slc;
85 typedef struct ng_vjc_private *priv_p;
87 #define ERROUT(x) do { error = (x); goto done; } while (0)
89 /* Netgraph node methods */
90 static ng_constructor_t ng_vjc_constructor;
91 static ng_rcvmsg_t ng_vjc_rcvmsg;
92 static ng_shutdown_t ng_vjc_rmnode;
93 static ng_newhook_t ng_vjc_newhook;
94 static ng_rcvdata_t ng_vjc_rcvdata;
95 static ng_disconnect_t ng_vjc_disconnect;
98 static struct mbuf *ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP);
100 /* Parse type for struct ngm_vjc_config */
101 static const struct ng_parse_struct_field ng_vjc_config_type_fields[]
102 = NG_VJC_CONFIG_TYPE_INFO;
103 static const struct ng_parse_type ng_vjc_config_type = {
104 &ng_parse_struct_type,
105 &ng_vjc_config_type_fields
108 /* Parse type for the 'last_cs' and 'cs_next' fields in struct slcompress,
109 which are pointers converted to integer indices, so parse them that way. */
111 #define NG_VJC_TSTATE_PTR_TYPE &ng_parse_uint32_type
113 #define NG_VJC_TSTATE_PTR_TYPE &ng_parse_uint64_type
116 /* Parse type for the 'cs_hdr' field in a struct cstate. Ideally we would
117 like to use a 'struct ip' type instead of a simple array of bytes. */
118 static const struct ng_parse_fixedarray_info ng_vjc_cs_hdr_type_info = {
119 &ng_parse_hint8_type,
122 static const struct ng_parse_type ng_vjc_cs_hdr_type = {
123 &ng_parse_fixedarray_type,
124 &ng_vjc_cs_hdr_type_info
127 /* Parse type for a struct cstate */
128 static const struct ng_parse_struct_field ng_vjc_cstate_type_fields[] = {
129 { "cs_next", NG_VJC_TSTATE_PTR_TYPE },
130 { "cs_hlen", &ng_parse_uint16_type },
131 { "cs_id", &ng_parse_uint8_type },
132 { "cs_filler", &ng_parse_uint8_type },
133 { "cs_hdr", &ng_vjc_cs_hdr_type },
136 static const struct ng_parse_type ng_vjc_cstate_type = {
137 &ng_parse_struct_type,
138 &ng_vjc_cstate_type_fields
141 /* Parse type for an array of MAX_STATES struct cstate's, ie, tstate & rstate */
142 static const struct ng_parse_fixedarray_info ng_vjc_cstatearray_type_info = {
146 static const struct ng_parse_type ng_vjc_cstatearray_type = {
147 &ng_parse_fixedarray_type,
148 &ng_vjc_cstatearray_type_info
151 /* Parse type for struct slcompress. Keep this in sync with the
152 definition of struct slcompress defined in <net/slcompress.h> */
153 static const struct ng_parse_struct_field ng_vjc_slcompress_type_fields[] = {
154 { "last_cs", NG_VJC_TSTATE_PTR_TYPE },
155 { "last_recv", &ng_parse_uint8_type },
156 { "last_xmit", &ng_parse_uint8_type },
157 { "flags", &ng_parse_hint16_type },
159 { "sls_packets", &ng_parse_uint32_type },
160 { "sls_compressed", &ng_parse_uint32_type },
161 { "sls_searches", &ng_parse_uint32_type },
162 { "sls_misses", &ng_parse_uint32_type },
163 { "sls_uncompressedin", &ng_parse_uint32_type },
164 { "sls_compressedin", &ng_parse_uint32_type },
165 { "sls_errorin", &ng_parse_uint32_type },
166 { "sls_tossed", &ng_parse_uint32_type },
168 { "tstate", &ng_vjc_cstatearray_type },
169 { "rstate", &ng_vjc_cstatearray_type },
172 static const struct ng_parse_type ng_vjc_slcompress_type = {
173 &ng_parse_struct_type,
174 &ng_vjc_slcompress_type_fields
177 /* List of commands and how to convert arguments to/from ASCII */
178 static const struct ng_cmdlist ng_vjc_cmds[] = {
198 &ng_vjc_slcompress_type,
217 /* Node type descriptor */
218 static struct ng_type ng_vjc_typestruct = {
233 NETGRAPH_INIT(vjc, &ng_vjc_typestruct);
235 /************************************************************************
236 NETGRAPH NODE METHODS
237 ************************************************************************/
243 ng_vjc_constructor(node_p *nodep)
248 /* Allocate private structure */
249 priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
253 /* Call generic node constructor */
254 if ((error = ng_make_node_common(&ng_vjc_typestruct, nodep))) {
255 kfree(priv, M_NETGRAPH);
258 (*nodep)->private = priv;
268 ng_vjc_newhook(node_p node, hook_p hook, const char *name)
270 const priv_p priv = (priv_p) node->private;
274 if (strcmp(name, NG_VJC_HOOK_IP) == 0)
276 else if (strcmp(name, NG_VJC_HOOK_VJCOMP) == 0)
277 hookp = &priv->vjcomp;
278 else if (strcmp(name, NG_VJC_HOOK_VJUNCOMP) == 0)
279 hookp = &priv->vjuncomp;
280 else if (strcmp(name, NG_VJC_HOOK_VJIP) == 0)
285 /* See if already connected */
295 * Receive a control message
298 ng_vjc_rcvmsg(node_p node, struct ng_mesg *msg,
299 const char *raddr, struct ng_mesg **rptr)
301 const priv_p priv = (priv_p) node->private;
302 struct ng_mesg *resp = NULL;
305 /* Check type cookie */
306 switch (msg->header.typecookie) {
308 switch (msg->header.cmd) {
309 case NGM_VJC_SET_CONFIG:
311 struct ngm_vjc_config *const c =
312 (struct ngm_vjc_config *) msg->data;
314 if (msg->header.arglen != sizeof(*c))
316 if ((priv->conf.enableComp || priv->conf.enableDecomp)
317 && (c->enableComp || c->enableDecomp))
320 if (c->maxChannel > NG_VJC_MAX_CHANNELS - 1
321 || c->maxChannel < NG_VJC_MIN_CHANNELS - 1)
324 c->maxChannel = NG_VJC_MAX_CHANNELS - 1;
325 if (c->enableComp != 0 || c->enableDecomp != 0) {
326 bzero(&priv->slc, sizeof(priv->slc));
327 sl_compress_init(&priv->slc, c->maxChannel);
332 case NGM_VJC_GET_CONFIG:
334 struct ngm_vjc_config *conf;
336 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
339 conf = (struct ngm_vjc_config *)resp->data;
343 case NGM_VJC_GET_STATE:
345 const struct slcompress *const sl0 = &priv->slc;
346 struct slcompress *sl;
350 /* Get response structure */
351 NG_MKRESPONSE(resp, msg, sizeof(*sl), M_NOWAIT);
354 sl = (struct slcompress *)resp->data;
357 /* Replace pointers with integer indicies */
358 if (sl->last_cs != NULL) {
359 index = sl0->last_cs - sl0->tstate;
360 bzero(&sl->last_cs, sizeof(sl->last_cs));
361 *((u_int16_t *)&sl->last_cs) = index;
363 for (i = 0; i < MAX_STATES; i++) {
364 struct cstate *const cs = &sl->tstate[i];
366 index = sl0->tstate[i].cs_next - sl0->tstate;
367 bzero(&cs->cs_next, sizeof(cs->cs_next));
368 *((u_int16_t *)&cs->cs_next) = index;
372 case NGM_VJC_CLR_STATS:
373 priv->slc.sls_packets = 0;
374 priv->slc.sls_compressed = 0;
375 priv->slc.sls_searches = 0;
376 priv->slc.sls_misses = 0;
377 priv->slc.sls_uncompressedin = 0;
378 priv->slc.sls_compressedin = 0;
379 priv->slc.sls_errorin = 0;
380 priv->slc.sls_tossed = 0;
382 case NGM_VJC_RECV_ERROR:
383 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &priv->slc);
397 kfree(resp, M_NETGRAPH);
400 kfree(msg, M_NETGRAPH);
408 ng_vjc_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
410 const node_p node = hook->node;
411 const priv_p priv = (priv_p) node->private;
414 if (hook == priv->ip) { /* outgoing packet */
415 u_int type = TYPE_IP;
417 /* Compress packet if enabled and proto is TCP */
418 if (priv->conf.enableComp) {
421 if ((m = ng_vjc_pulluphdrs(m, 0)) == NULL) {
425 ip = mtod(m, struct ip *);
426 if (ip->ip_p == IPPROTO_TCP) {
427 const int origLen = m->m_len;
429 type = sl_compress_tcp(m, ip,
430 &priv->slc, priv->conf.compressCID);
431 m->m_pkthdr.len += m->m_len - origLen;
435 /* Dispatch to the appropriate outgoing hook */
440 case TYPE_UNCOMPRESSED_TCP:
441 hook = priv->vjuncomp;
443 case TYPE_COMPRESSED_TCP:
447 panic("%s: type=%d", __func__, type);
449 } else if (hook == priv->vjcomp) { /* incoming compressed packet */
450 int vjlen, need2pullup;
455 /* Are we decompressing? */
456 if (!priv->conf.enableDecomp) {
457 NG_FREE_DATA(m, meta);
461 /* Pull up the necessary amount from the mbuf */
462 need2pullup = MAX_VJHEADER;
463 if (need2pullup > m->m_pkthdr.len)
464 need2pullup = m->m_pkthdr.len;
465 if (m->m_len < need2pullup
466 && (m = m_pullup(m, need2pullup)) == NULL) {
467 priv->slc.sls_errorin++;
472 /* Uncompress packet to reconstruct TCP/IP header */
473 vjlen = sl_uncompress_tcp_core(mtod(m, u_char *),
474 m->m_len, m->m_pkthdr.len, TYPE_COMPRESSED_TCP,
475 &priv->slc, &hdr, &hlen);
477 NG_FREE_DATA(m, meta);
482 /* Copy the reconstructed TCP/IP headers into a new mbuf */
483 MGETHDR(hm, MB_DONTWAIT, MT_DATA);
485 priv->slc.sls_errorin++;
486 NG_FREE_DATA(m, meta);
490 hm->m_pkthdr.rcvif = NULL;
491 if (hlen > MHLEN) { /* unlikely, but can happen */
492 MCLGET(hm, MB_DONTWAIT);
493 if ((hm->m_flags & M_EXT) == 0) {
495 priv->slc.sls_errorin++;
496 NG_FREE_DATA(m, meta);
500 bcopy(hdr, mtod(hm, u_char *), hlen);
503 /* Glue TCP/IP headers and rest of packet together */
505 hm->m_pkthdr.len = hlen + m->m_pkthdr.len;
508 } else if (hook == priv->vjuncomp) { /* incoming uncompressed pkt */
512 /* Are we decompressing? */
513 if (!priv->conf.enableDecomp) {
514 NG_FREE_DATA(m, meta);
518 /* Pull up IP+TCP headers */
519 if ((m = ng_vjc_pulluphdrs(m, 1)) == NULL) {
524 /* Run packet through uncompressor */
525 if (sl_uncompress_tcp_core(mtod(m, u_char *),
526 m->m_len, m->m_pkthdr.len, TYPE_UNCOMPRESSED_TCP,
527 &priv->slc, &hdr, &hlen) < 0) {
528 NG_FREE_DATA(m, meta);
532 } else if (hook == priv->vjip) /* incoming regular packet (bypass) */
535 panic("%s: unknown hook", __func__);
537 /* Send result back out */
538 NG_SEND_DATA(error, hook, m, meta);
546 ng_vjc_rmnode(node_p node)
548 const priv_p priv = (priv_p) node->private;
550 node->flags |= NG_INVALID;
553 bzero(priv, sizeof(*priv));
554 kfree(priv, M_NETGRAPH);
555 node->private = NULL;
564 ng_vjc_disconnect(hook_p hook)
566 const node_p node = hook->node;
567 const priv_p priv = node->private;
569 /* Zero out hook pointer */
570 if (hook == priv->ip)
572 else if (hook == priv->vjcomp)
574 else if (hook == priv->vjuncomp)
575 priv->vjuncomp = NULL;
576 else if (hook == priv->vjip)
579 panic("%s: unknown hook", __func__);
581 /* Go away if no hooks left */
582 if (node->numhooks == 0)
587 /************************************************************************
589 ************************************************************************/
592 * Pull up the full IP and TCP headers of a packet. If packet is not
593 * a TCP packet, just pull up the IP header.
596 ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP)
602 if (m->m_len < sizeof(*ip) && (m = m_pullup(m, sizeof(*ip))) == NULL)
604 ip = mtod(m, struct ip *);
605 if (!knownTCP && ip->ip_p != IPPROTO_TCP)
607 ihlen = ip->ip_hl << 2;
608 if (m->m_len < ihlen + sizeof(*tcp)) {
609 if ((m = m_pullup(m, ihlen + sizeof(*tcp))) == NULL)
611 ip = mtod(m, struct ip *);
613 tcp = (struct tcphdr *)((u_char *)ip + ihlen);
614 thlen = tcp->th_off << 2;
615 if (m->m_len < ihlen + thlen)
616 m = m_pullup(m, ihlen + thlen);