Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / netgraph / vjc / ng_vjc.c
1
2 /*
3  * ng_vjc.c
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  * 
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.
18  * 
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
35  * OF SUCH DAMAGE.
36  *
37  * Author: Archie Cobbs <archie@freebsd.org>
38  *
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 $
41  */
42
43 /*
44  * This node performs Van Jacobson IP header (de)compression.
45  * You must have included net/slcompress.c in your kernel compilation.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/errno.h>
51 #include <sys/kernel.h>
52 #include <sys/mbuf.h>
53 #include <sys/malloc.h>
54 #include <sys/errno.h>
55
56 #include <netgraph/ng_message.h>
57 #include <netgraph/netgraph.h>
58 #include <netgraph/ng_parse.h>
59 #include <netgraph/ng_vjc.h>
60
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/tcp.h>
65
66 #include <net/slcompress.h>
67
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
71 #endif
72
73 /* Maximum length of a compressed TCP VJ header */
74 #define MAX_VJHEADER            19
75
76 /* Node private data */
77 struct ng_vjc_private {
78         struct  ngm_vjc_config conf;
79         struct  slcompress slc;
80         hook_p  ip;
81         hook_p  vjcomp;
82         hook_p  vjuncomp;
83         hook_p  vjip;
84 };
85 typedef struct ng_vjc_private *priv_p;
86
87 #define ERROUT(x)       do { error = (x); goto done; } while (0)
88
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;
96
97 /* Helper stuff */
98 static struct mbuf *ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP);
99
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
106 };
107
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. */
110 #if _MACHINE_ARCH == i386
111 #define NG_VJC_TSTATE_PTR_TYPE  &ng_parse_uint32_type
112 #elif _MACHINE_ARCH == alpha
113 #define NG_VJC_TSTATE_PTR_TYPE  &ng_parse_uint64_type
114 #else
115 #error Unspported _MACHINE_ARCH
116 #endif
117
118 /* Parse type for the 'cs_hdr' field in a struct cstate. Ideally we would
119    like to use a 'struct ip' type instead of a simple array of bytes. */
120 static const struct ng_parse_fixedarray_info ng_vjc_cs_hdr_type_info = {
121         &ng_parse_hint8_type,
122         MAX_HDR
123 };
124 static const struct ng_parse_type ng_vjc_cs_hdr_type = {
125         &ng_parse_fixedarray_type,
126         &ng_vjc_cs_hdr_type_info
127 };
128
129 /* Parse type for a struct cstate */
130 static const struct ng_parse_struct_field ng_vjc_cstate_type_fields[] = {
131         { "cs_next",            NG_VJC_TSTATE_PTR_TYPE          },
132         { "cs_hlen",            &ng_parse_uint16_type           },
133         { "cs_id",              &ng_parse_uint8_type            },
134         { "cs_filler",          &ng_parse_uint8_type            },
135         { "cs_hdr",             &ng_vjc_cs_hdr_type             },
136         { NULL }
137 };
138 static const struct ng_parse_type ng_vjc_cstate_type = {
139         &ng_parse_struct_type,
140         &ng_vjc_cstate_type_fields
141 };
142
143 /* Parse type for an array of MAX_STATES struct cstate's, ie, tstate & rstate */
144 static const struct ng_parse_fixedarray_info ng_vjc_cstatearray_type_info = {
145         &ng_vjc_cstate_type,
146         MAX_STATES
147 };
148 static const struct ng_parse_type ng_vjc_cstatearray_type = {
149         &ng_parse_fixedarray_type,
150         &ng_vjc_cstatearray_type_info
151 };
152
153 /* Parse type for struct slcompress. Keep this in sync with the
154    definition of struct slcompress defined in <net/slcompress.h> */
155 static const struct ng_parse_struct_field ng_vjc_slcompress_type_fields[] = {
156         { "last_cs",            NG_VJC_TSTATE_PTR_TYPE          },
157         { "last_recv",          &ng_parse_uint8_type            },
158         { "last_xmit",          &ng_parse_uint8_type            },
159         { "flags",              &ng_parse_hint16_type           },
160 #ifndef SL_NO_STATS
161         { "sls_packets",        &ng_parse_uint32_type           },
162         { "sls_compressed",     &ng_parse_uint32_type           },
163         { "sls_searches",       &ng_parse_uint32_type           },
164         { "sls_misses",         &ng_parse_uint32_type           },
165         { "sls_uncompressedin", &ng_parse_uint32_type           },
166         { "sls_compressedin",   &ng_parse_uint32_type           },
167         { "sls_errorin",        &ng_parse_uint32_type           },
168         { "sls_tossed",         &ng_parse_uint32_type           },
169 #endif
170         { "tstate",             &ng_vjc_cstatearray_type        },
171         { "rstate",             &ng_vjc_cstatearray_type        },
172         { NULL }
173 };
174 static const struct ng_parse_type ng_vjc_slcompress_type = {
175         &ng_parse_struct_type,
176         &ng_vjc_slcompress_type_fields
177 };
178
179 /* List of commands and how to convert arguments to/from ASCII */
180 static const struct ng_cmdlist ng_vjc_cmds[] = {
181         {
182           NGM_VJC_COOKIE,
183           NGM_VJC_SET_CONFIG,
184           "setconfig",
185           &ng_vjc_config_type,
186           NULL
187         },
188         {
189           NGM_VJC_COOKIE,
190           NGM_VJC_GET_CONFIG,
191           "getconfig",
192           NULL,
193           &ng_vjc_config_type,
194         },
195         {
196           NGM_VJC_COOKIE,
197           NGM_VJC_GET_STATE,
198           "getstate",
199           NULL,
200           &ng_vjc_slcompress_type,
201         },
202         {
203           NGM_VJC_COOKIE,
204           NGM_VJC_CLR_STATS,
205           "clrstats",
206           NULL,
207           NULL,
208         },
209         {
210           NGM_VJC_COOKIE,
211           NGM_VJC_RECV_ERROR,
212           "recverror",
213           NULL,
214           NULL,
215         },
216         { 0 }
217 };
218
219 /* Node type descriptor */
220 static struct ng_type ng_vjc_typestruct = {
221         NG_VERSION,
222         NG_VJC_NODE_TYPE,
223         NULL,
224         ng_vjc_constructor,
225         ng_vjc_rcvmsg,
226         ng_vjc_rmnode,
227         ng_vjc_newhook,
228         NULL,
229         NULL,
230         ng_vjc_rcvdata,
231         ng_vjc_rcvdata,
232         ng_vjc_disconnect,
233         ng_vjc_cmds
234 };
235 NETGRAPH_INIT(vjc, &ng_vjc_typestruct);
236
237 /************************************************************************
238                         NETGRAPH NODE METHODS
239  ************************************************************************/
240
241 /*
242  * Create a new node
243  */
244 static int
245 ng_vjc_constructor(node_p *nodep)
246 {
247         priv_p priv;
248         int error;
249
250         /* Allocate private structure */
251         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
252         if (priv == NULL)
253                 return (ENOMEM);
254         bzero(priv, sizeof(*priv));
255
256         /* Call generic node constructor */
257         if ((error = ng_make_node_common(&ng_vjc_typestruct, nodep))) {
258                 FREE(priv, M_NETGRAPH);
259                 return (error);
260         }
261         (*nodep)->private = priv;
262
263         /* Done */
264         return (0);
265 }
266
267 /*
268  * Add a new hook
269  */
270 static int
271 ng_vjc_newhook(node_p node, hook_p hook, const char *name)
272 {
273         const priv_p priv = (priv_p) node->private;
274         hook_p *hookp;
275
276         /* Get hook */
277         if (strcmp(name, NG_VJC_HOOK_IP) == 0)
278                 hookp = &priv->ip;
279         else if (strcmp(name, NG_VJC_HOOK_VJCOMP) == 0)
280                 hookp = &priv->vjcomp;
281         else if (strcmp(name, NG_VJC_HOOK_VJUNCOMP) == 0)
282                 hookp = &priv->vjuncomp;
283         else if (strcmp(name, NG_VJC_HOOK_VJIP) == 0)
284                 hookp = &priv->vjip;
285         else
286                 return (EINVAL);
287
288         /* See if already connected */
289         if (*hookp)
290                 return (EISCONN);
291
292         /* OK */
293         *hookp = hook;
294         return (0);
295 }
296
297 /*
298  * Receive a control message
299  */
300 static int
301 ng_vjc_rcvmsg(node_p node, struct ng_mesg *msg,
302               const char *raddr, struct ng_mesg **rptr)
303 {
304         const priv_p priv = (priv_p) node->private;
305         struct ng_mesg *resp = NULL;
306         int error = 0;
307
308         /* Check type cookie */
309         switch (msg->header.typecookie) {
310         case NGM_VJC_COOKIE:
311                 switch (msg->header.cmd) {
312                 case NGM_VJC_SET_CONFIG:
313                     {
314                         struct ngm_vjc_config *const c =
315                                 (struct ngm_vjc_config *) msg->data;
316
317                         if (msg->header.arglen != sizeof(*c))
318                                 ERROUT(EINVAL);
319                         if ((priv->conf.enableComp || priv->conf.enableDecomp)
320                             && (c->enableComp || c->enableDecomp))
321                                 ERROUT(EALREADY);
322                         if (c->enableComp) {
323                                 if (c->maxChannel > NG_VJC_MAX_CHANNELS - 1
324                                     || c->maxChannel < NG_VJC_MIN_CHANNELS - 1)
325                                         ERROUT(EINVAL);
326                         } else
327                                 c->maxChannel = NG_VJC_MAX_CHANNELS - 1;
328                         if (c->enableComp != 0 || c->enableDecomp != 0) {
329                                 bzero(&priv->slc, sizeof(priv->slc));
330                                 sl_compress_init(&priv->slc, c->maxChannel);
331                         }
332                         priv->conf = *c;
333                         break;
334                     }
335                 case NGM_VJC_GET_CONFIG:
336                     {
337                         struct ngm_vjc_config *conf;
338
339                         NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
340                         if (resp == NULL)
341                                 ERROUT(ENOMEM);
342                         conf = (struct ngm_vjc_config *)resp->data;
343                         *conf = priv->conf;
344                         break;
345                     }
346                 case NGM_VJC_GET_STATE:
347                     {
348                         const struct slcompress *const sl0 = &priv->slc;
349                         struct slcompress *sl;
350                         u_int16_t index;
351                         int i;
352
353                         /* Get response structure */
354                         NG_MKRESPONSE(resp, msg, sizeof(*sl), M_NOWAIT);
355                         if (resp == NULL)
356                                 ERROUT(ENOMEM);
357                         sl = (struct slcompress *)resp->data;
358                         *sl = *sl0;
359
360                         /* Replace pointers with integer indicies */
361                         if (sl->last_cs != NULL) {
362                                 index = sl0->last_cs - sl0->tstate;
363                                 bzero(&sl->last_cs, sizeof(sl->last_cs));
364                                 *((u_int16_t *)&sl->last_cs) = index;
365                         }
366                         for (i = 0; i < MAX_STATES; i++) {
367                                 struct cstate *const cs = &sl->tstate[i];
368
369                                 index = sl0->tstate[i].cs_next - sl0->tstate;
370                                 bzero(&cs->cs_next, sizeof(cs->cs_next));
371                                 *((u_int16_t *)&cs->cs_next) = index;
372                         }
373                         break;
374                     }
375                 case NGM_VJC_CLR_STATS:
376                         priv->slc.sls_packets = 0;
377                         priv->slc.sls_compressed = 0;
378                         priv->slc.sls_searches = 0;
379                         priv->slc.sls_misses = 0;
380                         priv->slc.sls_uncompressedin = 0;
381                         priv->slc.sls_compressedin = 0;
382                         priv->slc.sls_errorin = 0;
383                         priv->slc.sls_tossed = 0;
384                         break;
385                 case NGM_VJC_RECV_ERROR:
386                         sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &priv->slc);
387                         break;
388                 default:
389                         error = EINVAL;
390                         break;
391                 }
392                 break;
393         default:
394                 error = EINVAL;
395                 break;
396         }
397         if (rptr)
398                 *rptr = resp;
399         else if (resp)
400                 FREE(resp, M_NETGRAPH);
401
402 done:
403         FREE(msg, M_NETGRAPH);
404         return (error);
405 }
406
407 /*
408  * Receive data
409  */
410 static int
411 ng_vjc_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
412 {
413         const node_p node = hook->node;
414         const priv_p priv = (priv_p) node->private;
415         int error = 0;
416
417         if (hook == priv->ip) {                 /* outgoing packet */
418                 u_int type = TYPE_IP;
419
420                 /* Compress packet if enabled and proto is TCP */
421                 if (priv->conf.enableComp) {
422                         struct ip *ip;
423
424                         if ((m = ng_vjc_pulluphdrs(m, 0)) == NULL) {
425                                 NG_FREE_META(meta);
426                                 return (ENOBUFS);
427                         }
428                         ip = mtod(m, struct ip *);
429                         if (ip->ip_p == IPPROTO_TCP) {
430                                 const int origLen = m->m_len;
431
432                                 type = sl_compress_tcp(m, ip,
433                                     &priv->slc, priv->conf.compressCID);
434                                 m->m_pkthdr.len += m->m_len - origLen;
435                         }
436                 }
437
438                 /* Dispatch to the appropriate outgoing hook */
439                 switch (type) {
440                 case TYPE_IP:
441                         hook = priv->vjip;
442                         break;
443                 case TYPE_UNCOMPRESSED_TCP:
444                         hook = priv->vjuncomp;
445                         break;
446                 case TYPE_COMPRESSED_TCP:
447                         hook = priv->vjcomp;
448                         break;
449                 default:
450                         panic("%s: type=%d", __FUNCTION__, type);
451                 }
452         } else if (hook == priv->vjcomp) {      /* incoming compressed packet */
453                 int vjlen, need2pullup;
454                 struct mbuf *hm;
455                 u_char *hdr;
456                 u_int hlen;
457
458                 /* Are we decompressing? */
459                 if (!priv->conf.enableDecomp) {
460                         NG_FREE_DATA(m, meta);
461                         return (ENXIO);
462                 }
463
464                 /* Pull up the necessary amount from the mbuf */
465                 need2pullup = MAX_VJHEADER;
466                 if (need2pullup > m->m_pkthdr.len)
467                         need2pullup = m->m_pkthdr.len;
468                 if (m->m_len < need2pullup
469                     && (m = m_pullup(m, need2pullup)) == NULL) {
470                         priv->slc.sls_errorin++;
471                         NG_FREE_META(meta);
472                         return (ENOBUFS);
473                 }
474
475                 /* Uncompress packet to reconstruct TCP/IP header */
476                 vjlen = sl_uncompress_tcp_core(mtod(m, u_char *),
477                     m->m_len, m->m_pkthdr.len, TYPE_COMPRESSED_TCP,
478                     &priv->slc, &hdr, &hlen);
479                 if (vjlen <= 0) {
480                         NG_FREE_DATA(m, meta);
481                         return (EINVAL);
482                 }
483                 m_adj(m, vjlen);
484
485                 /* Copy the reconstructed TCP/IP headers into a new mbuf */
486                 MGETHDR(hm, M_DONTWAIT, MT_DATA);
487                 if (hm == NULL) {
488                         priv->slc.sls_errorin++;
489                         NG_FREE_DATA(m, meta);
490                         return (ENOBUFS);
491                 }
492                 hm->m_len = 0;
493                 hm->m_pkthdr.rcvif = NULL;
494                 if (hlen > MHLEN) {             /* unlikely, but can happen */
495                         MCLGET(hm, M_DONTWAIT);
496                         if ((hm->m_flags & M_EXT) == 0) {
497                                 m_freem(hm);
498                                 priv->slc.sls_errorin++;
499                                 NG_FREE_DATA(m, meta);
500                                 return (ENOBUFS);
501                         }
502                 }
503                 bcopy(hdr, mtod(hm, u_char *), hlen);
504                 hm->m_len = hlen;
505
506                 /* Glue TCP/IP headers and rest of packet together */
507                 hm->m_next = m;
508                 hm->m_pkthdr.len = hlen + m->m_pkthdr.len;
509                 m = hm;
510                 hook = priv->ip;
511         } else if (hook == priv->vjuncomp) {    /* incoming uncompressed pkt */
512                 u_char *hdr;
513                 u_int hlen;
514
515                 /* Are we decompressing? */
516                 if (!priv->conf.enableDecomp) {
517                         NG_FREE_DATA(m, meta);
518                         return (ENXIO);
519                 }
520
521                 /* Pull up IP+TCP headers */
522                 if ((m = ng_vjc_pulluphdrs(m, 1)) == NULL) {
523                         NG_FREE_META(meta);
524                         return (ENOBUFS);
525                 }
526
527                 /* Run packet through uncompressor */
528                 if (sl_uncompress_tcp_core(mtod(m, u_char *),
529                     m->m_len, m->m_pkthdr.len, TYPE_UNCOMPRESSED_TCP,
530                     &priv->slc, &hdr, &hlen) < 0) {
531                         NG_FREE_DATA(m, meta);
532                         return (EINVAL);
533                 }
534                 hook = priv->ip;
535         } else if (hook == priv->vjip)  /* incoming regular packet (bypass) */
536                 hook = priv->ip;
537         else
538                 panic("%s: unknown hook", __FUNCTION__);
539
540         /* Send result back out */
541         NG_SEND_DATA(error, hook, m, meta);
542         return (error);
543 }
544
545 /*
546  * Shutdown node
547  */
548 static int
549 ng_vjc_rmnode(node_p node)
550 {
551         const priv_p priv = (priv_p) node->private;
552
553         node->flags |= NG_INVALID;
554         ng_cutlinks(node);
555         ng_unname(node);
556         bzero(priv, sizeof(*priv));
557         FREE(priv, M_NETGRAPH);
558         node->private = NULL;
559         ng_unref(node);
560         return (0);
561 }
562
563 /*
564  * Hook disconnection
565  */
566 static int
567 ng_vjc_disconnect(hook_p hook)
568 {
569         const node_p node = hook->node;
570         const priv_p priv = node->private;
571
572         /* Zero out hook pointer */
573         if (hook == priv->ip)
574                 priv->ip = NULL;
575         else if (hook == priv->vjcomp)
576                 priv->vjcomp = NULL;
577         else if (hook == priv->vjuncomp)
578                 priv->vjuncomp = NULL;
579         else if (hook == priv->vjip)
580                 priv->vjip = NULL;
581         else
582                 panic("%s: unknown hook", __FUNCTION__);
583
584         /* Go away if no hooks left */
585         if (node->numhooks == 0)
586                 ng_rmnode(node);
587         return (0);
588 }
589
590 /************************************************************************
591                         HELPER STUFF
592  ************************************************************************/
593
594 /*
595  * Pull up the full IP and TCP headers of a packet. If packet is not
596  * a TCP packet, just pull up the IP header.
597  */
598 static struct mbuf *
599 ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP)
600 {
601         struct ip *ip;
602         struct tcphdr *tcp;
603         int ihlen, thlen;
604
605         if (m->m_len < sizeof(*ip) && (m = m_pullup(m, sizeof(*ip))) == NULL)
606                 return (NULL);
607         ip = mtod(m, struct ip *);
608         if (!knownTCP && ip->ip_p != IPPROTO_TCP)
609                 return (m);
610         ihlen = ip->ip_hl << 2;
611         if (m->m_len < ihlen + sizeof(*tcp)) {
612                 if ((m = m_pullup(m, ihlen + sizeof(*tcp))) == NULL)
613                         return (NULL);
614                 ip = mtod(m, struct ip *);
615         }
616         tcp = (struct tcphdr *)((u_char *)ip + ihlen);
617         thlen = tcp->th_off << 2;
618         if (m->m_len < ihlen + thlen)
619                 m = m_pullup(m, ihlen + thlen);
620         return (m);
621 }
622