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