MFC numerous features from HEAD.
[dragonfly.git] / sys / netgraph7 / ng_vjc.c
blob2cfc86732464fca97976c2b4daf0b4e8549dfcb1
2 /*
3 * ng_vjc.c
4 */
6 /*-
7 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * All rights reserved.
9 *
10 * Subject to the following obligations and disclaimer of warranty, use and
11 * redistribution of this software, in source or object code forms, with or
12 * without modifications are expressly permitted by Whistle Communications;
13 * provided, however, that:
14 * 1. Any and all reproductions of the source or object code must include the
15 * copyright notice above and the following disclaimer of warranties; and
16 * 2. No rights are granted, in any manner or form, to use Whistle
17 * Communications, Inc. trademarks, including the mark "WHISTLE
18 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
19 * such appears in the above copyright notice or in the software.
21 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
22 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
23 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
24 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
26 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
27 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
28 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
29 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
30 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
31 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
32 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * OF SUCH DAMAGE.
39 * Author: Archie Cobbs <archie@freebsd.org>
41 * $FreeBSD: src/sys/netgraph/ng_vjc.c,v 1.26 2005/12/04 00:25:03 ru Exp $
42 * $DragonFly: src/sys/netgraph7/ng_vjc.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
43 * $Whistle: ng_vjc.c,v 1.17 1999/11/01 09:24:52 julian Exp $
47 * This node performs Van Jacobson IP header (de)compression.
48 * You must have included net/slcompress.c in your kernel compilation.
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/errno.h>
54 #include <sys/kernel.h>
55 #include <sys/mbuf.h>
56 #include <sys/malloc.h>
57 #include <sys/errno.h>
59 #include "ng_message.h"
60 #include "netgraph.h"
61 #include "ng_parse.h"
62 #include "ng_vjc.h"
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/tcp.h>
69 #include <net/slcompress.h>
71 /* Check agreement with slcompress.c */
72 #if MAX_STATES != NG_VJC_MAX_CHANNELS
73 #error NG_VJC_MAX_CHANNELS must be the same as MAX_STATES
74 #endif
76 /* Maximum length of a compressed TCP VJ header */
77 #define MAX_VJHEADER 19
79 /* Node private data */
80 struct ng_vjc_private {
81 struct ngm_vjc_config conf;
82 struct slcompress slc;
83 hook_p ip;
84 hook_p vjcomp;
85 hook_p vjuncomp;
86 hook_p vjip;
88 typedef struct ng_vjc_private *priv_p;
90 #define ERROUT(x) do { error = (x); goto done; } while (0)
92 /* Netgraph node methods */
93 static ng_constructor_t ng_vjc_constructor;
94 static ng_rcvmsg_t ng_vjc_rcvmsg;
95 static ng_shutdown_t ng_vjc_shutdown;
96 static ng_newhook_t ng_vjc_newhook;
97 static ng_rcvdata_t ng_vjc_rcvdata;
98 static ng_disconnect_t ng_vjc_disconnect;
100 /* Helper stuff */
101 static struct mbuf *ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP);
103 /* Parse type for struct ngm_vjc_config */
104 static const struct ng_parse_struct_field ng_vjc_config_type_fields[]
105 = NG_VJC_CONFIG_TYPE_INFO;
106 static const struct ng_parse_type ng_vjc_config_type = {
107 &ng_parse_struct_type,
108 &ng_vjc_config_type_fields
111 /* Parse type for the 'last_cs' and 'cs_next' fields in struct slcompress,
112 which are pointers converted to integer indices, so parse them that way. */
113 #ifndef __LP64__
114 #define NG_VJC_TSTATE_PTR_TYPE &ng_parse_uint32_type
115 #else
116 #define NG_VJC_TSTATE_PTR_TYPE &ng_parse_uint64_type
117 #endif
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,
123 MAX_HDR
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 },
137 { NULL }
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 = {
146 &ng_vjc_cstate_type,
147 MAX_STATES
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 },
161 #ifndef SL_NO_STATS
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 },
170 #endif
171 { "tstate", &ng_vjc_cstatearray_type },
172 { "rstate", &ng_vjc_cstatearray_type },
173 { NULL }
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[] = {
183 NGM_VJC_COOKIE,
184 NGM_VJC_SET_CONFIG,
185 "setconfig",
186 &ng_vjc_config_type,
187 NULL
190 NGM_VJC_COOKIE,
191 NGM_VJC_GET_CONFIG,
192 "getconfig",
193 NULL,
194 &ng_vjc_config_type,
197 NGM_VJC_COOKIE,
198 NGM_VJC_GET_STATE,
199 "getstate",
200 NULL,
201 &ng_vjc_slcompress_type,
204 NGM_VJC_COOKIE,
205 NGM_VJC_CLR_STATS,
206 "clrstats",
207 NULL,
208 NULL,
211 NGM_VJC_COOKIE,
212 NGM_VJC_RECV_ERROR,
213 "recverror",
214 NULL,
215 NULL,
217 { 0 }
220 /* Node type descriptor */
221 static struct ng_type ng_vjc_typestruct = {
222 .version = NG_ABI_VERSION,
223 .name = NG_VJC_NODE_TYPE,
224 .constructor = ng_vjc_constructor,
225 .rcvmsg = ng_vjc_rcvmsg,
226 .shutdown = ng_vjc_shutdown,
227 .newhook = ng_vjc_newhook,
228 .rcvdata = ng_vjc_rcvdata,
229 .disconnect = ng_vjc_disconnect,
230 .cmdlist = ng_vjc_cmds,
232 NETGRAPH_INIT(vjc, &ng_vjc_typestruct);
234 /************************************************************************
235 NETGRAPH NODE METHODS
236 ************************************************************************/
239 * Create a new node
241 static int
242 ng_vjc_constructor(node_p node)
244 priv_p priv;
246 /* Allocate private structure */
247 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
248 if (priv == NULL)
249 return (ENOMEM);
251 NG_NODE_SET_PRIVATE(node, priv);
253 /* Done */
254 return (0);
258 * Add a new hook
260 static int
261 ng_vjc_newhook(node_p node, hook_p hook, const char *name)
263 const priv_p priv = NG_NODE_PRIVATE(node);
264 hook_p *hookp;
266 /* Get hook */
267 if (strcmp(name, NG_VJC_HOOK_IP) == 0)
268 hookp = &priv->ip;
269 else if (strcmp(name, NG_VJC_HOOK_VJCOMP) == 0)
270 hookp = &priv->vjcomp;
271 else if (strcmp(name, NG_VJC_HOOK_VJUNCOMP) == 0)
272 hookp = &priv->vjuncomp;
273 else if (strcmp(name, NG_VJC_HOOK_VJIP) == 0)
274 hookp = &priv->vjip;
275 else
276 return (EINVAL);
278 /* See if already connected */
279 if (*hookp)
280 return (EISCONN);
282 /* OK */
283 *hookp = hook;
284 return (0);
288 * Receive a control message
290 static int
291 ng_vjc_rcvmsg(node_p node, item_p item, hook_p lasthook)
293 const priv_p priv = NG_NODE_PRIVATE(node);
294 struct ng_mesg *resp = NULL;
295 int error = 0;
296 struct ng_mesg *msg;
298 NGI_GET_MSG(item, msg);
299 /* Check type cookie */
300 switch (msg->header.typecookie) {
301 case NGM_VJC_COOKIE:
302 switch (msg->header.cmd) {
303 case NGM_VJC_SET_CONFIG:
305 struct ngm_vjc_config *const c =
306 (struct ngm_vjc_config *) msg->data;
308 if (msg->header.arglen != sizeof(*c))
309 ERROUT(EINVAL);
310 if ((priv->conf.enableComp || priv->conf.enableDecomp)
311 && (c->enableComp || c->enableDecomp))
312 ERROUT(EALREADY);
313 if (c->enableComp) {
314 if (c->maxChannel > NG_VJC_MAX_CHANNELS - 1
315 || c->maxChannel < NG_VJC_MIN_CHANNELS - 1)
316 ERROUT(EINVAL);
317 } else
318 c->maxChannel = NG_VJC_MAX_CHANNELS - 1;
319 if (c->enableComp != 0 || c->enableDecomp != 0) {
320 bzero(&priv->slc, sizeof(priv->slc));
321 sl_compress_init(&priv->slc, c->maxChannel);
323 priv->conf = *c;
324 break;
326 case NGM_VJC_GET_CONFIG:
328 struct ngm_vjc_config *conf;
330 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_WAITOK | M_NULLOK);
331 if (resp == NULL)
332 ERROUT(ENOMEM);
333 conf = (struct ngm_vjc_config *)resp->data;
334 *conf = priv->conf;
335 break;
337 case NGM_VJC_GET_STATE:
339 const struct slcompress *const sl0 = &priv->slc;
340 struct slcompress *sl;
341 u_int16_t index;
342 int i;
344 /* Get response structure */
345 NG_MKRESPONSE(resp, msg, sizeof(*sl), M_WAITOK | M_NULLOK);
346 if (resp == NULL)
347 ERROUT(ENOMEM);
348 sl = (struct slcompress *)resp->data;
349 *sl = *sl0;
351 /* Replace pointers with integer indicies */
352 if (sl->last_cs != NULL) {
353 index = sl0->last_cs - sl0->tstate;
354 bzero(&sl->last_cs, sizeof(sl->last_cs));
355 *((u_int16_t *)&sl->last_cs) = index;
357 for (i = 0; i < MAX_STATES; i++) {
358 struct cstate *const cs = &sl->tstate[i];
360 index = sl0->tstate[i].cs_next - sl0->tstate;
361 bzero(&cs->cs_next, sizeof(cs->cs_next));
362 *((u_int16_t *)&cs->cs_next) = index;
364 break;
366 case NGM_VJC_CLR_STATS:
367 priv->slc.sls_packets = 0;
368 priv->slc.sls_compressed = 0;
369 priv->slc.sls_searches = 0;
370 priv->slc.sls_misses = 0;
371 priv->slc.sls_uncompressedin = 0;
372 priv->slc.sls_compressedin = 0;
373 priv->slc.sls_errorin = 0;
374 priv->slc.sls_tossed = 0;
375 break;
376 case NGM_VJC_RECV_ERROR:
377 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &priv->slc);
378 break;
379 default:
380 error = EINVAL;
381 break;
383 break;
384 default:
385 error = EINVAL;
386 break;
388 done:
389 NG_RESPOND_MSG(error, node, item, resp);
390 NG_FREE_MSG(msg);
391 return (error);
395 * Receive data
397 static int
398 ng_vjc_rcvdata(hook_p hook, item_p item)
400 const node_p node = NG_HOOK_NODE(hook);
401 const priv_p priv = NG_NODE_PRIVATE(node);
402 int error = 0;
403 struct mbuf *m;
405 NGI_GET_M(item, m);
406 if (hook == priv->ip) { /* outgoing packet */
407 u_int type = TYPE_IP;
409 /* Compress packet if enabled and proto is TCP */
410 if (priv->conf.enableComp) {
411 struct ip *ip;
413 if ((m = ng_vjc_pulluphdrs(m, 0)) == NULL) {
414 NG_FREE_ITEM(item);
415 return (ENOBUFS);
417 ip = mtod(m, struct ip *);
418 if (ip->ip_p == IPPROTO_TCP) {
419 const int origLen = m->m_len;
421 type = sl_compress_tcp(m, ip,
422 &priv->slc, priv->conf.compressCID);
423 m->m_pkthdr.len += m->m_len - origLen;
427 /* Dispatch to the appropriate outgoing hook */
428 switch (type) {
429 case TYPE_IP:
430 hook = priv->vjip;
431 break;
432 case TYPE_UNCOMPRESSED_TCP:
433 hook = priv->vjuncomp;
434 break;
435 case TYPE_COMPRESSED_TCP:
436 hook = priv->vjcomp;
437 break;
438 default:
439 panic("%s: type=%d", __func__, type);
441 } else if (hook == priv->vjcomp) { /* incoming compressed packet */
442 int vjlen, need2pullup;
443 struct mbuf *hm;
444 u_char *hdr;
445 u_int hlen;
447 /* Are we decompressing? */
448 if (!priv->conf.enableDecomp) {
449 NG_FREE_M(m);
450 NG_FREE_ITEM(item);
451 return (ENXIO);
454 /* Pull up the necessary amount from the mbuf */
455 need2pullup = MAX_VJHEADER;
456 if (need2pullup > m->m_pkthdr.len)
457 need2pullup = m->m_pkthdr.len;
458 if (m->m_len < need2pullup
459 && (m = m_pullup(m, need2pullup)) == NULL) {
460 priv->slc.sls_errorin++;
461 NG_FREE_ITEM(item);
462 return (ENOBUFS);
465 /* Uncompress packet to reconstruct TCP/IP header */
466 vjlen = sl_uncompress_tcp_core(mtod(m, u_char *),
467 m->m_len, m->m_pkthdr.len, TYPE_COMPRESSED_TCP,
468 &priv->slc, &hdr, &hlen);
469 if (vjlen <= 0) {
470 NG_FREE_M(m);
471 NG_FREE_ITEM(item);
472 return (EINVAL);
474 m_adj(m, vjlen);
476 /* Copy the reconstructed TCP/IP headers into a new mbuf */
477 MGETHDR(hm, MB_DONTWAIT, MT_DATA);
478 if (hm == NULL) {
479 priv->slc.sls_errorin++;
480 NG_FREE_M(m);
481 NG_FREE_ITEM(item);
482 return (ENOBUFS);
484 hm->m_len = 0;
485 hm->m_pkthdr.rcvif = NULL;
486 if (hlen > MHLEN) { /* unlikely, but can happen */
487 MCLGET(hm, MB_DONTWAIT);
488 if ((hm->m_flags & M_EXT) == 0) {
489 m_freem(hm);
490 priv->slc.sls_errorin++;
491 NG_FREE_M(m);
492 NG_FREE_ITEM(item);
493 return (ENOBUFS);
496 bcopy(hdr, mtod(hm, u_char *), hlen);
497 hm->m_len = hlen;
499 /* Glue TCP/IP headers and rest of packet together */
500 hm->m_next = m;
501 hm->m_pkthdr.len = hlen + m->m_pkthdr.len;
502 m = hm;
503 hook = priv->ip;
504 } else if (hook == priv->vjuncomp) { /* incoming uncompressed pkt */
505 u_char *hdr;
506 u_int hlen;
508 /* Are we decompressing? */
509 if (!priv->conf.enableDecomp) {
510 NG_FREE_M(m);
511 NG_FREE_ITEM(item);
512 return (ENXIO);
515 /* Pull up IP+TCP headers */
516 if ((m = ng_vjc_pulluphdrs(m, 1)) == NULL) {
517 NG_FREE_ITEM(item);
518 return (ENOBUFS);
521 /* Run packet through uncompressor */
522 if (sl_uncompress_tcp_core(mtod(m, u_char *),
523 m->m_len, m->m_pkthdr.len, TYPE_UNCOMPRESSED_TCP,
524 &priv->slc, &hdr, &hlen) < 0) {
525 NG_FREE_M(m);
526 NG_FREE_ITEM(item);
527 return (EINVAL);
529 hook = priv->ip;
530 } else if (hook == priv->vjip) /* incoming regular packet (bypass) */
531 hook = priv->ip;
532 else
533 panic("%s: unknown hook", __func__);
535 /* Send result back out */
536 NG_FWD_NEW_DATA(error, item, hook, m);
537 return (error);
541 * Shutdown node
543 static int
544 ng_vjc_shutdown(node_p node)
546 const priv_p priv = NG_NODE_PRIVATE(node);
548 bzero(priv, sizeof(*priv));
549 FREE(priv, M_NETGRAPH);
550 NG_NODE_SET_PRIVATE(node, NULL);
551 NG_NODE_UNREF(node);
552 return (0);
556 * Hook disconnection
558 static int
559 ng_vjc_disconnect(hook_p hook)
561 const node_p node = NG_HOOK_NODE(hook);
562 const priv_p priv = NG_NODE_PRIVATE(node);
564 /* Zero out hook pointer */
565 if (hook == priv->ip)
566 priv->ip = NULL;
567 else if (hook == priv->vjcomp)
568 priv->vjcomp = NULL;
569 else if (hook == priv->vjuncomp)
570 priv->vjuncomp = NULL;
571 else if (hook == priv->vjip)
572 priv->vjip = NULL;
573 else
574 panic("%s: unknown hook", __func__);
576 /* Go away if no hooks left */
577 if ((NG_NODE_NUMHOOKS(node) == 0)
578 && (NG_NODE_IS_VALID(node)))
579 ng_rmnode_self(node);
580 return (0);
583 /************************************************************************
584 HELPER STUFF
585 ************************************************************************/
588 * Pull up the full IP and TCP headers of a packet. If packet is not
589 * a TCP packet, just pull up the IP header.
591 static struct mbuf *
592 ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP)
594 struct ip *ip;
595 struct tcphdr *tcp;
596 int ihlen, thlen;
598 if (m->m_len < sizeof(*ip) && (m = m_pullup(m, sizeof(*ip))) == NULL)
599 return (NULL);
600 ip = mtod(m, struct ip *);
601 if (!knownTCP && ip->ip_p != IPPROTO_TCP)
602 return (m);
603 ihlen = ip->ip_hl << 2;
604 if (m->m_len < ihlen + sizeof(*tcp)) {
605 if ((m = m_pullup(m, ihlen + sizeof(*tcp))) == NULL)
606 return (NULL);
607 ip = mtod(m, struct ip *);
609 tcp = (struct tcphdr *)((u_char *)ip + ihlen);
610 thlen = tcp->th_off << 2;
611 if (m->m_len < ihlen + thlen)
612 m = m_pullup(m, ihlen + thlen);
613 return (m);