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