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