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