GCC supports two pseudo variables to get the function name, __FUNCTION__
[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  * $DragonFly: src/sys/netgraph/vjc/ng_vjc.c,v 1.5 2005/02/17 14:00:00 joerg Exp $
41  * $Whistle: ng_vjc.c,v 1.17 1999/11/01 09:24:52 julian Exp $
42  */
43
44 /*
45  * This node performs Van Jacobson IP header (de)compression.
46  * You must have included net/slcompress.c in your kernel compilation.
47  */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/errno.h>
52 #include <sys/kernel.h>
53 #include <sys/mbuf.h>
54 #include <sys/malloc.h>
55 #include <sys/errno.h>
56
57 #include <netgraph/ng_message.h>
58 #include <netgraph/netgraph.h>
59 #include <netgraph/ng_parse.h>
60 #include "ng_vjc.h"
61
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/tcp.h>
66
67 #include <net/slcompress.h>
68
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
72 #endif
73
74 /* Maximum length of a compressed TCP VJ header */
75 #define MAX_VJHEADER            19
76
77 /* Node private data */
78 struct ng_vjc_private {
79         struct  ngm_vjc_config conf;
80         struct  slcompress slc;
81         hook_p  ip;
82         hook_p  vjcomp;
83         hook_p  vjuncomp;
84         hook_p  vjip;
85 };
86 typedef struct ng_vjc_private *priv_p;
87
88 #define ERROUT(x)       do { error = (x); goto done; } while (0)
89
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;
97
98 /* Helper stuff */
99 static struct mbuf *ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP);
100
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
107 };
108
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
115 #else
116 #error Unspported _MACHINE_ARCH
117 #endif
118
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
124 };
125 static const struct ng_parse_type ng_vjc_cs_hdr_type = {
126         &ng_parse_fixedarray_type,
127         &ng_vjc_cs_hdr_type_info
128 };
129
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 }
138 };
139 static const struct ng_parse_type ng_vjc_cstate_type = {
140         &ng_parse_struct_type,
141         &ng_vjc_cstate_type_fields
142 };
143
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
148 };
149 static const struct ng_parse_type ng_vjc_cstatearray_type = {
150         &ng_parse_fixedarray_type,
151         &ng_vjc_cstatearray_type_info
152 };
153
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 }
174 };
175 static const struct ng_parse_type ng_vjc_slcompress_type = {
176         &ng_parse_struct_type,
177         &ng_vjc_slcompress_type_fields
178 };
179
180 /* List of commands and how to convert arguments to/from ASCII */
181 static const struct ng_cmdlist ng_vjc_cmds[] = {
182         {
183           NGM_VJC_COOKIE,
184           NGM_VJC_SET_CONFIG,
185           "setconfig",
186           &ng_vjc_config_type,
187           NULL
188         },
189         {
190           NGM_VJC_COOKIE,
191           NGM_VJC_GET_CONFIG,
192           "getconfig",
193           NULL,
194           &ng_vjc_config_type,
195         },
196         {
197           NGM_VJC_COOKIE,
198           NGM_VJC_GET_STATE,
199           "getstate",
200           NULL,
201           &ng_vjc_slcompress_type,
202         },
203         {
204           NGM_VJC_COOKIE,
205           NGM_VJC_CLR_STATS,
206           "clrstats",
207           NULL,
208           NULL,
209         },
210         {
211           NGM_VJC_COOKIE,
212           NGM_VJC_RECV_ERROR,
213           "recverror",
214           NULL,
215           NULL,
216         },
217         { 0 }
218 };
219
220 /* Node type descriptor */
221 static struct ng_type ng_vjc_typestruct = {
222         NG_VERSION,
223         NG_VJC_NODE_TYPE,
224         NULL,
225         ng_vjc_constructor,
226         ng_vjc_rcvmsg,
227         ng_vjc_rmnode,
228         ng_vjc_newhook,
229         NULL,
230         NULL,
231         ng_vjc_rcvdata,
232         ng_vjc_rcvdata,
233         ng_vjc_disconnect,
234         ng_vjc_cmds
235 };
236 NETGRAPH_INIT(vjc, &ng_vjc_typestruct);
237
238 /************************************************************************
239                         NETGRAPH NODE METHODS
240  ************************************************************************/
241
242 /*
243  * Create a new node
244  */
245 static int
246 ng_vjc_constructor(node_p *nodep)
247 {
248         priv_p priv;
249         int error;
250
251         /* Allocate private structure */
252         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
253         if (priv == NULL)
254                 return (ENOMEM);
255         bzero(priv, sizeof(*priv));
256
257         /* Call generic node constructor */
258         if ((error = ng_make_node_common(&ng_vjc_typestruct, nodep))) {
259                 FREE(priv, M_NETGRAPH);
260                 return (error);
261         }
262         (*nodep)->private = priv;
263
264         /* Done */
265         return (0);
266 }
267
268 /*
269  * Add a new hook
270  */
271 static int
272 ng_vjc_newhook(node_p node, hook_p hook, const char *name)
273 {
274         const priv_p priv = (priv_p) node->private;
275         hook_p *hookp;
276
277         /* Get hook */
278         if (strcmp(name, NG_VJC_HOOK_IP) == 0)
279                 hookp = &priv->ip;
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)
285                 hookp = &priv->vjip;
286         else
287                 return (EINVAL);
288
289         /* See if already connected */
290         if (*hookp)
291                 return (EISCONN);
292
293         /* OK */
294         *hookp = hook;
295         return (0);
296 }
297
298 /*
299  * Receive a control message
300  */
301 static int
302 ng_vjc_rcvmsg(node_p node, struct ng_mesg *msg,
303               const char *raddr, struct ng_mesg **rptr)
304 {
305         const priv_p priv = (priv_p) node->private;
306         struct ng_mesg *resp = NULL;
307         int error = 0;
308
309         /* Check type cookie */
310         switch (msg->header.typecookie) {
311         case NGM_VJC_COOKIE:
312                 switch (msg->header.cmd) {
313                 case NGM_VJC_SET_CONFIG:
314                     {
315                         struct ngm_vjc_config *const c =
316                                 (struct ngm_vjc_config *) msg->data;
317
318                         if (msg->header.arglen != sizeof(*c))
319                                 ERROUT(EINVAL);
320                         if ((priv->conf.enableComp || priv->conf.enableDecomp)
321                             && (c->enableComp || c->enableDecomp))
322                                 ERROUT(EALREADY);
323                         if (c->enableComp) {
324                                 if (c->maxChannel > NG_VJC_MAX_CHANNELS - 1
325                                     || c->maxChannel < NG_VJC_MIN_CHANNELS - 1)
326                                         ERROUT(EINVAL);
327                         } else
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);
332                         }
333                         priv->conf = *c;
334                         break;
335                     }
336                 case NGM_VJC_GET_CONFIG:
337                     {
338                         struct ngm_vjc_config *conf;
339
340                         NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
341                         if (resp == NULL)
342                                 ERROUT(ENOMEM);
343                         conf = (struct ngm_vjc_config *)resp->data;
344                         *conf = priv->conf;
345                         break;
346                     }
347                 case NGM_VJC_GET_STATE:
348                     {
349                         const struct slcompress *const sl0 = &priv->slc;
350                         struct slcompress *sl;
351                         u_int16_t index;
352                         int i;
353
354                         /* Get response structure */
355                         NG_MKRESPONSE(resp, msg, sizeof(*sl), M_NOWAIT);
356                         if (resp == NULL)
357                                 ERROUT(ENOMEM);
358                         sl = (struct slcompress *)resp->data;
359                         *sl = *sl0;
360
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;
366                         }
367                         for (i = 0; i < MAX_STATES; i++) {
368                                 struct cstate *const cs = &sl->tstate[i];
369
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;
373                         }
374                         break;
375                     }
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;
385                         break;
386                 case NGM_VJC_RECV_ERROR:
387                         sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &priv->slc);
388                         break;
389                 default:
390                         error = EINVAL;
391                         break;
392                 }
393                 break;
394         default:
395                 error = EINVAL;
396                 break;
397         }
398         if (rptr)
399                 *rptr = resp;
400         else if (resp)
401                 FREE(resp, M_NETGRAPH);
402
403 done:
404         FREE(msg, M_NETGRAPH);
405         return (error);
406 }
407
408 /*
409  * Receive data
410  */
411 static int
412 ng_vjc_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
413 {
414         const node_p node = hook->node;
415         const priv_p priv = (priv_p) node->private;
416         int error = 0;
417
418         if (hook == priv->ip) {                 /* outgoing packet */
419                 u_int type = TYPE_IP;
420
421                 /* Compress packet if enabled and proto is TCP */
422                 if (priv->conf.enableComp) {
423                         struct ip *ip;
424
425                         if ((m = ng_vjc_pulluphdrs(m, 0)) == NULL) {
426                                 NG_FREE_META(meta);
427                                 return (ENOBUFS);
428                         }
429                         ip = mtod(m, struct ip *);
430                         if (ip->ip_p == IPPROTO_TCP) {
431                                 const int origLen = m->m_len;
432
433                                 type = sl_compress_tcp(m, ip,
434                                     &priv->slc, priv->conf.compressCID);
435                                 m->m_pkthdr.len += m->m_len - origLen;
436                         }
437                 }
438
439                 /* Dispatch to the appropriate outgoing hook */
440                 switch (type) {
441                 case TYPE_IP:
442                         hook = priv->vjip;
443                         break;
444                 case TYPE_UNCOMPRESSED_TCP:
445                         hook = priv->vjuncomp;
446                         break;
447                 case TYPE_COMPRESSED_TCP:
448                         hook = priv->vjcomp;
449                         break;
450                 default:
451                         panic("%s: type=%d", __func__, type);
452                 }
453         } else if (hook == priv->vjcomp) {      /* incoming compressed packet */
454                 int vjlen, need2pullup;
455                 struct mbuf *hm;
456                 u_char *hdr;
457                 u_int hlen;
458
459                 /* Are we decompressing? */
460                 if (!priv->conf.enableDecomp) {
461                         NG_FREE_DATA(m, meta);
462                         return (ENXIO);
463                 }
464
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++;
472                         NG_FREE_META(meta);
473                         return (ENOBUFS);
474                 }
475
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);
480                 if (vjlen <= 0) {
481                         NG_FREE_DATA(m, meta);
482                         return (EINVAL);
483                 }
484                 m_adj(m, vjlen);
485
486                 /* Copy the reconstructed TCP/IP headers into a new mbuf */
487                 MGETHDR(hm, MB_DONTWAIT, MT_DATA);
488                 if (hm == NULL) {
489                         priv->slc.sls_errorin++;
490                         NG_FREE_DATA(m, meta);
491                         return (ENOBUFS);
492                 }
493                 hm->m_len = 0;
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) {
498                                 m_freem(hm);
499                                 priv->slc.sls_errorin++;
500                                 NG_FREE_DATA(m, meta);
501                                 return (ENOBUFS);
502                         }
503                 }
504                 bcopy(hdr, mtod(hm, u_char *), hlen);
505                 hm->m_len = hlen;
506
507                 /* Glue TCP/IP headers and rest of packet together */
508                 hm->m_next = m;
509                 hm->m_pkthdr.len = hlen + m->m_pkthdr.len;
510                 m = hm;
511                 hook = priv->ip;
512         } else if (hook == priv->vjuncomp) {    /* incoming uncompressed pkt */
513                 u_char *hdr;
514                 u_int hlen;
515
516                 /* Are we decompressing? */
517                 if (!priv->conf.enableDecomp) {
518                         NG_FREE_DATA(m, meta);
519                         return (ENXIO);
520                 }
521
522                 /* Pull up IP+TCP headers */
523                 if ((m = ng_vjc_pulluphdrs(m, 1)) == NULL) {
524                         NG_FREE_META(meta);
525                         return (ENOBUFS);
526                 }
527
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);
533                         return (EINVAL);
534                 }
535                 hook = priv->ip;
536         } else if (hook == priv->vjip)  /* incoming regular packet (bypass) */
537                 hook = priv->ip;
538         else
539                 panic("%s: unknown hook", __func__);
540
541         /* Send result back out */
542         NG_SEND_DATA(error, hook, m, meta);
543         return (error);
544 }
545
546 /*
547  * Shutdown node
548  */
549 static int
550 ng_vjc_rmnode(node_p node)
551 {
552         const priv_p priv = (priv_p) node->private;
553
554         node->flags |= NG_INVALID;
555         ng_cutlinks(node);
556         ng_unname(node);
557         bzero(priv, sizeof(*priv));
558         FREE(priv, M_NETGRAPH);
559         node->private = NULL;
560         ng_unref(node);
561         return (0);
562 }
563
564 /*
565  * Hook disconnection
566  */
567 static int
568 ng_vjc_disconnect(hook_p hook)
569 {
570         const node_p node = hook->node;
571         const priv_p priv = node->private;
572
573         /* Zero out hook pointer */
574         if (hook == priv->ip)
575                 priv->ip = NULL;
576         else if (hook == priv->vjcomp)
577                 priv->vjcomp = NULL;
578         else if (hook == priv->vjuncomp)
579                 priv->vjuncomp = NULL;
580         else if (hook == priv->vjip)
581                 priv->vjip = NULL;
582         else
583                 panic("%s: unknown hook", __func__);
584
585         /* Go away if no hooks left */
586         if (node->numhooks == 0)
587                 ng_rmnode(node);
588         return (0);
589 }
590
591 /************************************************************************
592                         HELPER STUFF
593  ************************************************************************/
594
595 /*
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.
598  */
599 static struct mbuf *
600 ng_vjc_pulluphdrs(struct mbuf *m, int knownTCP)
601 {
602         struct ip *ip;
603         struct tcphdr *tcp;
604         int ihlen, thlen;
605
606         if (m->m_len < sizeof(*ip) && (m = m_pullup(m, sizeof(*ip))) == NULL)
607                 return (NULL);
608         ip = mtod(m, struct ip *);
609         if (!knownTCP && ip->ip_p != IPPROTO_TCP)
610                 return (m);
611         ihlen = ip->ip_hl << 2;
612         if (m->m_len < ihlen + sizeof(*tcp)) {
613                 if ((m = m_pullup(m, ihlen + sizeof(*tcp))) == NULL)
614                         return (NULL);
615                 ip = mtod(m, struct ip *);
616         }
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);
621         return (m);
622 }
623