1 2 /* 3 * ng_ppp.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@whistle.com> 38 * 39 * $FreeBSD$ 40 * $Whistle: ng_ppp.c,v 1.24 1999/11/01 09:24:52 julian Exp $ 41 */ 42 43 /* 44 * PPP node type. 45 */ 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/mbuf.h> 51 #include <sys/malloc.h> 52 #include <sys/errno.h> 53 #include <sys/syslog.h> 54 #include <sys/ctype.h> 55 56 #include <netgraph/ng_message.h> 57 #include <netgraph/netgraph.h> 58 #include <netgraph/ng_parse.h> 59 #include <netgraph/ng_ppp.h> 60 #include <netgraph/ng_vjc.h> 61 62 #define PROT_VALID(p) (((p) & 0x0101) == 0x0001) 63 #define PROT_COMPRESSABLE(p) (((p) & 0xff00) == 0x0000) 64 65 /* Some PPP protocol numbers we're interested in */ 66 #define PROT_APPLETALK 0x0029 67 #define PROT_COMPD 0x00fd 68 #define PROT_CRYPTD 0x0053 69 #define PROT_IP 0x0021 70 #define PROT_IPX 0x002b 71 #define PROT_LCP 0xc021 72 #define PROT_MP 0x003d 73 #define PROT_VJCOMP 0x002d 74 #define PROT_VJUNCOMP 0x002f 75 76 /* Multilink PPP definitions */ 77 #define MP_MIN_MRRU 1500 /* per RFC 1990 */ 78 #define MP_INITIAL_SEQ 0 /* per RFC 1990 */ 79 #define MP_MIN_LINK_MRU 32 80 81 #define MP_MAX_SEQ_LINGER 64 /* max frags we will hold */ 82 #define MP_INSANE_SEQ_JUMP 128 /* a sequence # jump too far */ 83 #define MP_MIN_FRAG_LEN 6 /* don't frag smaller frames */ 84 85 #define MP_SHORT_SEQ_MASK 0x00000fff /* short seq # mask */ 86 #define MP_SHORT_SEQ_HIBIT 0x00000800 /* short seq # high bit */ 87 #define MP_SHORT_FIRST_FLAG 0x00008000 /* first fragment in frame */ 88 #define MP_SHORT_LAST_FLAG 0x00004000 /* last fragment in frame */ 89 90 #define MP_LONG_SEQ_MASK 0x00ffffff /* long seq # mask */ 91 #define MP_LONG_SEQ_HIBIT 0x00800000 /* long seq # high bit */ 92 #define MP_LONG_FIRST_FLAG 0x80000000 /* first fragment in frame */ 93 #define MP_LONG_LAST_FLAG 0x40000000 /* last fragment in frame */ 94 95 #define MP_SEQ_MASK(priv) ((priv)->conf.recvShortSeq ? \ 96 MP_SHORT_SEQ_MASK : MP_LONG_SEQ_MASK) 97 98 /* Sign extension of MP sequence numbers */ 99 #define MP_SHORT_EXTEND(s) (((s) & MP_SHORT_SEQ_HIBIT) ? \ 100 ((s) | ~MP_SHORT_SEQ_MASK) : (s)) 101 #define MP_LONG_EXTEND(s) (((s) & MP_LONG_SEQ_HIBIT) ? \ 102 ((s) | ~MP_LONG_SEQ_MASK) : (s)) 103 104 /* Comparision of MP sequence numbers */ 105 #define MP_SHORT_SEQ_DIFF(x,y) (MP_SHORT_EXTEND(x) - MP_SHORT_EXTEND(y)) 106 #define MP_LONG_SEQ_DIFF(x,y) (MP_LONG_EXTEND(x) - MP_LONG_EXTEND(y)) 107 108 #define MP_SEQ_DIFF(priv,x,y) ((priv)->conf.recvShortSeq ? \ 109 MP_SHORT_SEQ_DIFF((x), (y)) : \ 110 MP_LONG_SEQ_DIFF((x), (y))) 111 112 /* We store incoming fragments this way */ 113 struct ng_ppp_frag { 114 int seq; 115 u_char first; 116 u_char last; 117 struct mbuf *data; 118 meta_p meta; 119 CIRCLEQ_ENTRY(ng_ppp_frag) f_qent; 120 }; 121 122 /* We keep track of link queue status this way */ 123 struct ng_ppp_link_qstat { 124 struct timeval lastWrite; /* time of last write */ 125 int bytesInQueue; /* bytes in the queue then */ 126 }; 127 128 /* We use integer indicies to refer to the non-link hooks */ 129 static const char *const ng_ppp_hook_names[] = { 130 NG_PPP_HOOK_ATALK, 131 #define HOOK_INDEX_ATALK 0 132 NG_PPP_HOOK_BYPASS, 133 #define HOOK_INDEX_BYPASS 1 134 NG_PPP_HOOK_COMPRESS, 135 #define HOOK_INDEX_COMPRESS 2 136 NG_PPP_HOOK_ENCRYPT, 137 #define HOOK_INDEX_ENCRYPT 3 138 NG_PPP_HOOK_DECOMPRESS, 139 #define HOOK_INDEX_DECOMPRESS 4 140 NG_PPP_HOOK_DECRYPT, 141 #define HOOK_INDEX_DECRYPT 5 142 NG_PPP_HOOK_INET, 143 #define HOOK_INDEX_INET 6 144 NG_PPP_HOOK_IPX, 145 #define HOOK_INDEX_IPX 7 146 NG_PPP_HOOK_VJC_COMP, 147 #define HOOK_INDEX_VJC_COMP 8 148 NG_PPP_HOOK_VJC_IP, 149 #define HOOK_INDEX_VJC_IP 9 150 NG_PPP_HOOK_VJC_UNCOMP, 151 #define HOOK_INDEX_VJC_UNCOMP 10 152 NG_PPP_HOOK_VJC_VJIP, 153 #define HOOK_INDEX_VJC_VJIP 11 154 NULL 155 #define HOOK_INDEX_MAX 12 156 }; 157 158 /* We store index numbers in the hook private pointer. The HOOK_INDEX() 159 for a hook is either the index (above) for normal hooks, or the ones 160 complement of the link number for link hooks. */ 161 #define HOOK_INDEX(hook) (*((int16_t *) &(hook)->private)) 162 163 /* Node private data */ 164 struct ng_ppp_private { 165 struct ng_ppp_node_config conf; 166 struct ng_ppp_link_stat bundleStats; 167 struct ng_ppp_link_stat linkStats[NG_PPP_MAX_LINKS]; 168 hook_p links[NG_PPP_MAX_LINKS]; 169 hook_p hooks[HOOK_INDEX_MAX]; 170 u_char vjCompHooked; 171 u_char allLinksEqual; 172 u_short activeLinks[NG_PPP_MAX_LINKS]; 173 u_int numActiveLinks; 174 u_int lastLink; /* for round robin */ 175 struct ng_ppp_link_qstat qstat[NG_PPP_MAX_LINKS]; 176 CIRCLEQ_HEAD(ng_ppp_fraglist, ng_ppp_frag) 177 frags; /* incoming fragments */ 178 int mpSeqOut; /* next out MP seq # */ 179 }; 180 typedef struct ng_ppp_private *priv_p; 181 182 /* Netgraph node methods */ 183 static ng_constructor_t ng_ppp_constructor; 184 static ng_rcvmsg_t ng_ppp_rcvmsg; 185 static ng_shutdown_t ng_ppp_rmnode; 186 static ng_newhook_t ng_ppp_newhook; 187 static ng_rcvdata_t ng_ppp_rcvdata; 188 static ng_disconnect_t ng_ppp_disconnect; 189 190 /* Helper functions */ 191 static int ng_ppp_input(node_p node, int bypass, 192 int linkNum, struct mbuf *m, meta_p meta); 193 static int ng_ppp_output(node_p node, int bypass, int proto, 194 int linkNum, struct mbuf *m, meta_p meta); 195 static int ng_ppp_mp_input(node_p node, int linkNum, 196 struct mbuf *m, meta_p meta); 197 static int ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta); 198 static void ng_ppp_mp_strategy(node_p node, int len, int *distrib); 199 static int ng_ppp_intcmp(const void *v1, const void *v2); 200 static struct mbuf *ng_ppp_addproto(struct mbuf *m, int proto, int compOK); 201 static struct mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len); 202 static int ng_ppp_config_valid(node_p node, 203 const struct ng_ppp_node_config *newConf); 204 static void ng_ppp_update(node_p node, int newConf); 205 static void ng_ppp_free_frags(node_p node); 206 207 /* Parse type for struct ng_ppp_link_config */ 208 static const struct ng_parse_struct_info 209 ng_ppp_link_type_info = NG_PPP_LINK_TYPE_INFO; 210 static const struct ng_parse_type ng_ppp_link_type = { 211 &ng_parse_struct_type, 212 &ng_ppp_link_type_info, 213 }; 214 215 /* Parse type for struct ng_ppp_node_config */ 216 struct ng_parse_fixedarray_info ng_ppp_array_info = { 217 &ng_ppp_link_type, 218 NG_PPP_MAX_LINKS 219 }; 220 static const struct ng_parse_type ng_ppp_link_array_type = { 221 &ng_parse_fixedarray_type, 222 &ng_ppp_array_info, 223 }; 224 static const struct ng_parse_struct_info ng_ppp_config_type_info 225 = NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_link_array_type); 226 static const struct ng_parse_type ng_ppp_config_type = { 227 &ng_parse_struct_type, 228 &ng_ppp_config_type_info 229 }; 230 231 /* Parse type for struct ng_ppp_link_stat */ 232 static const struct ng_parse_struct_info 233 ng_ppp_stats_type_info = NG_PPP_STATS_TYPE_INFO; 234 static const struct ng_parse_type ng_ppp_stats_type = { 235 &ng_parse_struct_type, 236 &ng_ppp_stats_type_info 237 }; 238 239 /* List of commands and how to convert arguments to/from ASCII */ 240 static const struct ng_cmdlist ng_ppp_cmds[] = { 241 { 242 NGM_PPP_COOKIE, 243 NGM_PPP_SET_CONFIG, 244 "setconfig", 245 &ng_ppp_config_type, 246 NULL 247 }, 248 { 249 NGM_PPP_COOKIE, 250 NGM_PPP_GET_CONFIG, 251 "getconfig", 252 NULL, 253 &ng_ppp_config_type 254 }, 255 { 256 NGM_PPP_COOKIE, 257 NGM_PPP_GET_LINK_STATS, 258 "getstats", 259 &ng_parse_int16_type, 260 &ng_ppp_stats_type 261 }, 262 { 263 NGM_PPP_COOKIE, 264 NGM_PPP_CLR_LINK_STATS, 265 "clrstats", 266 &ng_parse_int16_type, 267 NULL 268 }, 269 { 270 NGM_PPP_COOKIE, 271 NGM_PPP_GETCLR_LINK_STATS, 272 "getclrstats", 273 &ng_parse_int16_type, 274 &ng_ppp_stats_type 275 }, 276 { 0 } 277 }; 278 279 /* Node type descriptor */ 280 static struct ng_type ng_ppp_typestruct = { 281 NG_VERSION, 282 NG_PPP_NODE_TYPE, 283 NULL, 284 ng_ppp_constructor, 285 ng_ppp_rcvmsg, 286 ng_ppp_rmnode, 287 ng_ppp_newhook, 288 NULL, 289 NULL, 290 ng_ppp_rcvdata, 291 ng_ppp_rcvdata, 292 ng_ppp_disconnect, 293 ng_ppp_cmds 294 }; 295 NETGRAPH_INIT(ppp, &ng_ppp_typestruct); 296 297 static int *compareLatencies; /* hack for ng_ppp_intcmp() */ 298 299 /* Address and control field header */ 300 static const u_char ng_ppp_acf[2] = { 0xff, 0x03 }; 301 302 #define ERROUT(x) do { error = (x); goto done; } while (0) 303 304 /************************************************************************ 305 NETGRAPH NODE STUFF 306 ************************************************************************/ 307 308 /* 309 * Node type constructor 310 */ 311 static int 312 ng_ppp_constructor(node_p *nodep) 313 { 314 priv_p priv; 315 int error; 316 317 /* Allocate private structure */ 318 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK); 319 if (priv == NULL) 320 return (ENOMEM); 321 bzero(priv, sizeof(*priv)); 322 323 /* Call generic node constructor */ 324 if ((error = ng_make_node_common(&ng_ppp_typestruct, nodep))) { 325 FREE(priv, M_NETGRAPH); 326 return (error); 327 } 328 (*nodep)->private = priv; 329 330 /* Initialize state */ 331 CIRCLEQ_INIT(&priv->frags); 332 333 /* Done */ 334 return (0); 335 } 336 337 /* 338 * Give our OK for a hook to be added 339 */ 340 static int 341 ng_ppp_newhook(node_p node, hook_p hook, const char *name) 342 { 343 const priv_p priv = node->private; 344 int linkNum = -1; 345 hook_p *hookPtr = NULL; 346 int hookIndex = -1; 347 348 /* Figure out which hook it is */ 349 if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX, /* a link hook? */ 350 strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) { 351 const char *cp; 352 char *eptr; 353 354 cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX); 355 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 356 return (EINVAL); 357 linkNum = (int)strtoul(cp, &eptr, 10); 358 if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS) 359 return (EINVAL); 360 hookPtr = &priv->links[linkNum]; 361 hookIndex = ~linkNum; 362 } else { /* must be a non-link hook */ 363 int i; 364 365 for (i = 0; ng_ppp_hook_names[i] != NULL; i++) { 366 if (strcmp(name, ng_ppp_hook_names[i]) == 0) { 367 hookPtr = &priv->hooks[i]; 368 hookIndex = i; 369 break; 370 } 371 } 372 if (ng_ppp_hook_names[i] == NULL) 373 return (EINVAL); /* no such hook */ 374 } 375 376 /* See if hook is already connected */ 377 if (*hookPtr != NULL) 378 return (EISCONN); 379 380 /* Disallow more than one link unless multilink is enabled */ 381 if (linkNum != -1 && priv->conf.links[linkNum].enableLink 382 && !priv->conf.enableMultilink && priv->numActiveLinks >= 1) 383 return (ENODEV); 384 385 /* OK */ 386 *hookPtr = hook; 387 HOOK_INDEX(hook) = hookIndex; 388 ng_ppp_update(node, 0); 389 return (0); 390 } 391 392 /* 393 * Receive a control message 394 */ 395 static int 396 ng_ppp_rcvmsg(node_p node, struct ng_mesg *msg, 397 const char *raddr, struct ng_mesg **rptr) 398 { 399 const priv_p priv = node->private; 400 struct ng_mesg *resp = NULL; 401 int error = 0; 402 403 switch (msg->header.typecookie) { 404 case NGM_PPP_COOKIE: 405 switch (msg->header.cmd) { 406 case NGM_PPP_SET_CONFIG: 407 { 408 struct ng_ppp_node_config *const newConf = 409 (struct ng_ppp_node_config *) msg->data; 410 411 /* Check for invalid or illegal config */ 412 if (msg->header.arglen != sizeof(*newConf)) 413 ERROUT(EINVAL); 414 if (!ng_ppp_config_valid(node, newConf)) 415 ERROUT(EINVAL); 416 priv->conf = *newConf; 417 ng_ppp_update(node, 1); 418 break; 419 } 420 case NGM_PPP_GET_CONFIG: 421 NG_MKRESPONSE(resp, msg, sizeof(priv->conf), M_NOWAIT); 422 if (resp == NULL) 423 ERROUT(ENOMEM); 424 bcopy(&priv->conf, resp->data, sizeof(priv->conf)); 425 break; 426 case NGM_PPP_GET_LINK_STATS: 427 case NGM_PPP_CLR_LINK_STATS: 428 case NGM_PPP_GETCLR_LINK_STATS: 429 { 430 struct ng_ppp_link_stat *stats; 431 u_int16_t linkNum; 432 433 if (msg->header.arglen != sizeof(u_int16_t)) 434 ERROUT(EINVAL); 435 linkNum = *((u_int16_t *) msg->data); 436 if (linkNum >= NG_PPP_MAX_LINKS 437 && linkNum != NG_PPP_BUNDLE_LINKNUM) 438 ERROUT(EINVAL); 439 stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ? 440 &priv->bundleStats : &priv->linkStats[linkNum]; 441 if (msg->header.cmd != NGM_PPP_CLR_LINK_STATS) { 442 NG_MKRESPONSE(resp, msg, 443 sizeof(struct ng_ppp_link_stat), M_NOWAIT); 444 if (resp == NULL) 445 ERROUT(ENOMEM); 446 bcopy(stats, resp->data, sizeof(*stats)); 447 } 448 if (msg->header.cmd != NGM_PPP_GET_LINK_STATS) 449 bzero(stats, sizeof(*stats)); 450 break; 451 } 452 default: 453 error = EINVAL; 454 break; 455 } 456 break; 457 case NGM_VJC_COOKIE: 458 { 459 char path[NG_PATHLEN + 1]; 460 node_p origNode; 461 462 if ((error = ng_path2node(node, raddr, &origNode, NULL)) != 0) 463 ERROUT(error); 464 snprintf(path, sizeof(path), "[%lx]:%s", 465 (long) node, NG_PPP_HOOK_VJC_IP); 466 return ng_send_msg(origNode, msg, path, rptr); 467 break; 468 } 469 default: 470 error = EINVAL; 471 break; 472 } 473 if (rptr) 474 *rptr = resp; 475 else if (resp) 476 FREE(resp, M_NETGRAPH); 477 478 done: 479 FREE(msg, M_NETGRAPH); 480 return (error); 481 } 482 483 /* 484 * Receive data on a hook 485 */ 486 static int 487 ng_ppp_rcvdata(hook_p hook, struct mbuf *m, meta_p meta) 488 { 489 const node_p node = hook->node; 490 const priv_p priv = node->private; 491 const int index = HOOK_INDEX(hook); 492 u_int16_t linkNum = NG_PPP_BUNDLE_LINKNUM; 493 hook_p outHook = NULL; 494 int proto = 0, error; 495 496 /* Did it come from a link hook? */ 497 if (index < 0) { 498 499 /* Convert index into a link number */ 500 linkNum = (u_int16_t)~index; 501 KASSERT(linkNum < NG_PPP_MAX_LINKS, 502 ("%s: bogus index 0x%x", __FUNCTION__, index)); 503 504 /* Stats */ 505 priv->linkStats[linkNum].recvFrames++; 506 priv->linkStats[linkNum].recvOctets += m->m_pkthdr.len; 507 508 /* Strip address and control fields, if present */ 509 if (m->m_pkthdr.len >= 2) { 510 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) { 511 NG_FREE_DATA(m, meta); 512 return (ENOBUFS); 513 } 514 if (bcmp(mtod(m, u_char *), &ng_ppp_acf, 2) == 0) 515 m_adj(m, 2); 516 } 517 518 /* Dispatch incoming frame (if not enabled, to bypass) */ 519 return ng_ppp_input(node, 520 !priv->conf.links[linkNum].enableLink, linkNum, m, meta); 521 } 522 523 /* Get protocol & check if data allowed from this hook */ 524 switch (index) { 525 526 /* Outgoing data */ 527 case HOOK_INDEX_ATALK: 528 if (!priv->conf.enableAtalk) { 529 NG_FREE_DATA(m, meta); 530 return (ENXIO); 531 } 532 proto = PROT_APPLETALK; 533 break; 534 case HOOK_INDEX_IPX: 535 if (!priv->conf.enableIPX) { 536 NG_FREE_DATA(m, meta); 537 return (ENXIO); 538 } 539 proto = PROT_IPX; 540 break; 541 case HOOK_INDEX_INET: 542 case HOOK_INDEX_VJC_VJIP: 543 if (!priv->conf.enableIP) { 544 NG_FREE_DATA(m, meta); 545 return (ENXIO); 546 } 547 proto = PROT_IP; 548 break; 549 case HOOK_INDEX_VJC_COMP: 550 if (!priv->conf.enableVJCompression) { 551 NG_FREE_DATA(m, meta); 552 return (ENXIO); 553 } 554 proto = PROT_VJCOMP; 555 break; 556 case HOOK_INDEX_VJC_UNCOMP: 557 if (!priv->conf.enableVJCompression) { 558 NG_FREE_DATA(m, meta); 559 return (ENXIO); 560 } 561 proto = PROT_VJUNCOMP; 562 break; 563 case HOOK_INDEX_COMPRESS: 564 if (!priv->conf.enableCompression) { 565 NG_FREE_DATA(m, meta); 566 return (ENXIO); 567 } 568 proto = PROT_COMPD; 569 break; 570 case HOOK_INDEX_ENCRYPT: 571 if (!priv->conf.enableEncryption) { 572 NG_FREE_DATA(m, meta); 573 return (ENXIO); 574 } 575 proto = PROT_CRYPTD; 576 break; 577 case HOOK_INDEX_BYPASS: 578 if (m->m_pkthdr.len < 4) { 579 NG_FREE_META(meta); 580 return (EINVAL); 581 } 582 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) { 583 NG_FREE_META(meta); 584 return (ENOBUFS); 585 } 586 linkNum = ntohs(mtod(m, u_int16_t *)[0]); 587 proto = ntohs(mtod(m, u_int16_t *)[1]); 588 m_adj(m, 4); 589 if (linkNum >= NG_PPP_MAX_LINKS 590 && linkNum != NG_PPP_BUNDLE_LINKNUM) 591 return (EINVAL); 592 break; 593 594 /* Incoming data */ 595 case HOOK_INDEX_VJC_IP: 596 if (!priv->conf.enableIP || !priv->conf.enableVJDecompression) { 597 NG_FREE_DATA(m, meta); 598 return (ENXIO); 599 } 600 break; 601 case HOOK_INDEX_DECOMPRESS: 602 if (!priv->conf.enableDecompression) { 603 NG_FREE_DATA(m, meta); 604 return (ENXIO); 605 } 606 break; 607 case HOOK_INDEX_DECRYPT: 608 if (!priv->conf.enableDecryption) { 609 NG_FREE_DATA(m, meta); 610 return (ENXIO); 611 } 612 break; 613 default: 614 panic("%s: bogus index 0x%x", __FUNCTION__, index); 615 } 616 617 /* Now figure out what to do with the frame */ 618 switch (index) { 619 620 /* Outgoing data */ 621 case HOOK_INDEX_INET: 622 if (priv->conf.enableVJCompression && priv->vjCompHooked) { 623 outHook = priv->hooks[HOOK_INDEX_VJC_IP]; 624 break; 625 } 626 /* FALLTHROUGH */ 627 case HOOK_INDEX_ATALK: 628 case HOOK_INDEX_IPX: 629 case HOOK_INDEX_VJC_COMP: 630 case HOOK_INDEX_VJC_UNCOMP: 631 case HOOK_INDEX_VJC_VJIP: 632 if (priv->conf.enableCompression 633 && priv->hooks[HOOK_INDEX_COMPRESS] != NULL) { 634 if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) { 635 NG_FREE_META(meta); 636 return (ENOBUFS); 637 } 638 outHook = priv->hooks[HOOK_INDEX_COMPRESS]; 639 break; 640 } 641 /* FALLTHROUGH */ 642 case HOOK_INDEX_COMPRESS: 643 if (priv->conf.enableEncryption 644 && priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) { 645 if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) { 646 NG_FREE_META(meta); 647 return (ENOBUFS); 648 } 649 outHook = priv->hooks[HOOK_INDEX_ENCRYPT]; 650 break; 651 } 652 /* FALLTHROUGH */ 653 case HOOK_INDEX_ENCRYPT: 654 return ng_ppp_output(node, 0, 655 proto, NG_PPP_BUNDLE_LINKNUM, m, meta); 656 657 case HOOK_INDEX_BYPASS: 658 return ng_ppp_output(node, 1, proto, linkNum, m, meta); 659 660 /* Incoming data */ 661 case HOOK_INDEX_DECRYPT: 662 case HOOK_INDEX_DECOMPRESS: 663 return ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, m, meta); 664 665 case HOOK_INDEX_VJC_IP: 666 outHook = priv->hooks[HOOK_INDEX_INET]; 667 break; 668 } 669 670 /* Send packet out hook */ 671 NG_SEND_DATA(error, outHook, m, meta); 672 return (error); 673 } 674 675 /* 676 * Destroy node 677 */ 678 static int 679 ng_ppp_rmnode(node_p node) 680 { 681 const priv_p priv = node->private; 682 683 /* Take down netgraph node */ 684 node->flags |= NG_INVALID; 685 ng_cutlinks(node); 686 ng_unname(node); 687 ng_ppp_free_frags(node); 688 bzero(priv, sizeof(*priv)); 689 FREE(priv, M_NETGRAPH); 690 node->private = NULL; 691 ng_unref(node); /* let the node escape */ 692 return (0); 693 } 694 695 /* 696 * Hook disconnection 697 */ 698 static int 699 ng_ppp_disconnect(hook_p hook) 700 { 701 const node_p node = hook->node; 702 const priv_p priv = node->private; 703 const int index = HOOK_INDEX(hook); 704 705 /* Zero out hook pointer */ 706 if (index < 0) 707 priv->links[~index] = NULL; 708 else 709 priv->hooks[index] = NULL; 710 711 /* Update derived info (or go away if no hooks left) */ 712 if (node->numhooks > 0) 713 ng_ppp_update(node, 0); 714 else 715 ng_rmnode(hook->node); 716 return (0); 717 } 718 719 /************************************************************************ 720 HELPER STUFF 721 ************************************************************************/ 722 723 /* 724 * Handle an incoming frame. Extract the PPP protocol number 725 * and dispatch accordingly. 726 */ 727 static int 728 ng_ppp_input(node_p node, int bypass, int linkNum, struct mbuf *m, meta_p meta) 729 { 730 const priv_p priv = node->private; 731 hook_p outHook = NULL; 732 int proto, error; 733 734 /* Extract protocol number */ 735 for (proto = 0; !PROT_VALID(proto) && m->m_pkthdr.len > 0; ) { 736 if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) { 737 NG_FREE_META(meta); 738 return (ENOBUFS); 739 } 740 proto = (proto << 8) + *mtod(m, u_char *); 741 m_adj(m, 1); 742 } 743 if (!PROT_VALID(proto)) { 744 if (linkNum == NG_PPP_BUNDLE_LINKNUM) 745 priv->bundleStats.badProtos++; 746 else 747 priv->linkStats[linkNum].badProtos++; 748 NG_FREE_DATA(m, meta); 749 return (EINVAL); 750 } 751 752 /* Bypass frame? */ 753 if (bypass) 754 goto bypass; 755 756 /* Check protocol */ 757 switch (proto) { 758 case PROT_COMPD: 759 if (priv->conf.enableDecompression) 760 outHook = priv->hooks[HOOK_INDEX_DECOMPRESS]; 761 break; 762 case PROT_CRYPTD: 763 if (priv->conf.enableDecryption) 764 outHook = priv->hooks[HOOK_INDEX_DECRYPT]; 765 break; 766 case PROT_VJCOMP: 767 if (priv->conf.enableVJDecompression && priv->vjCompHooked) 768 outHook = priv->hooks[HOOK_INDEX_VJC_COMP]; 769 break; 770 case PROT_VJUNCOMP: 771 if (priv->conf.enableVJDecompression && priv->vjCompHooked) 772 outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP]; 773 break; 774 case PROT_MP: 775 if (priv->conf.enableMultilink) 776 return ng_ppp_mp_input(node, linkNum, m, meta); 777 break; 778 case PROT_APPLETALK: 779 if (priv->conf.enableAtalk) 780 outHook = priv->hooks[HOOK_INDEX_ATALK]; 781 break; 782 case PROT_IPX: 783 if (priv->conf.enableIPX) 784 outHook = priv->hooks[HOOK_INDEX_IPX]; 785 break; 786 case PROT_IP: 787 if (priv->conf.enableIP) 788 outHook = priv->hooks[HOOK_INDEX_INET]; 789 break; 790 } 791 792 /* For unknown/inactive protocols, forward out the bypass hook */ 793 bypass: 794 if (outHook == NULL) { 795 u_int16_t hdr[2]; 796 797 hdr[0] = htons(linkNum); 798 hdr[1] = htons((u_int16_t)proto); 799 if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) 800 return (ENOBUFS); 801 outHook = priv->hooks[HOOK_INDEX_BYPASS]; 802 } 803 804 /* Forward frame */ 805 NG_SEND_DATA(error, outHook, m, meta); 806 return (error); 807 } 808 809 /* 810 * Deliver a frame out a link, either a real one or NG_PPP_BUNDLE_LINKNUM 811 * If the link is not enabled then ENXIO is returned, unless "bypass" is != 0. 812 */ 813 static int 814 ng_ppp_output(node_p node, int bypass, 815 int proto, int linkNum, struct mbuf *m, meta_p meta) 816 { 817 const priv_p priv = node->private; 818 int len, error; 819 820 /* If not doing MP, map bundle virtual link to (the only) link */ 821 if (linkNum == NG_PPP_BUNDLE_LINKNUM && !priv->conf.enableMultilink) 822 linkNum = priv->activeLinks[0]; 823 824 /* Check link status (if real) */ 825 if (linkNum != NG_PPP_BUNDLE_LINKNUM) { 826 if (!bypass && !priv->conf.links[linkNum].enableLink) 827 return (ENXIO); 828 if (priv->links[linkNum] == NULL) { 829 NG_FREE_DATA(m, meta); 830 return (ENETDOWN); 831 } 832 } 833 834 /* Prepend protocol number, possibly compressed */ 835 if ((m = ng_ppp_addproto(m, proto, 836 linkNum == NG_PPP_BUNDLE_LINKNUM 837 || priv->conf.links[linkNum].enableProtoComp)) == NULL) { 838 NG_FREE_META(meta); 839 return (ENOBUFS); 840 } 841 842 /* Special handling for the MP virtual link */ 843 if (linkNum == NG_PPP_BUNDLE_LINKNUM) 844 return ng_ppp_mp_output(node, m, meta); 845 846 /* Prepend address and control field (unless compressed) */ 847 if (proto == PROT_LCP || !priv->conf.links[linkNum].enableACFComp) { 848 if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) { 849 NG_FREE_META(meta); 850 return (ENOBUFS); 851 } 852 } 853 854 /* Deliver frame */ 855 len = m->m_pkthdr.len; 856 NG_SEND_DATA(error, priv->links[linkNum], m, meta); 857 858 /* Update stats and 'bytes in queue' counter */ 859 if (error == 0) { 860 priv->linkStats[linkNum].xmitFrames++; 861 priv->linkStats[linkNum].xmitOctets += len; 862 priv->qstat[linkNum].bytesInQueue += len; 863 microtime(&priv->qstat[linkNum].lastWrite); 864 } 865 return error; 866 } 867 868 /* 869 * Handle an incoming multi-link fragment 870 */ 871 static int 872 ng_ppp_mp_input(node_p node, int linkNum, struct mbuf *m, meta_p meta) 873 { 874 const priv_p priv = node->private; 875 struct ng_ppp_frag frag0, *frag = &frag0; 876 struct ng_ppp_frag *qent, *qnext; 877 struct ng_ppp_frag *first, *last; 878 int diff, highSeq, nextSeq, inserted; 879 struct mbuf *tail; 880 881 /* Extract fragment information from MP header */ 882 if (priv->conf.recvShortSeq) { 883 u_int16_t shdr; 884 885 if (m->m_pkthdr.len < 2) { 886 NG_FREE_DATA(m, meta); 887 return (EINVAL); 888 } 889 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) { 890 NG_FREE_META(meta); 891 return (ENOBUFS); 892 } 893 shdr = ntohs(*mtod(m, u_int16_t *)); 894 frag->seq = shdr & MP_SHORT_SEQ_MASK; 895 frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0; 896 frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0; 897 highSeq = CIRCLEQ_EMPTY(&priv->frags) ? 898 frag->seq : CIRCLEQ_FIRST(&priv->frags)->seq; 899 diff = MP_SHORT_SEQ_DIFF(frag->seq, highSeq); 900 m_adj(m, 2); 901 } else { 902 u_int32_t lhdr; 903 904 if (m->m_pkthdr.len < 4) { 905 NG_FREE_DATA(m, meta); 906 return (EINVAL); 907 } 908 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) { 909 NG_FREE_META(meta); 910 return (ENOBUFS); 911 } 912 lhdr = ntohl(*mtod(m, u_int32_t *)); 913 frag->seq = lhdr & MP_LONG_SEQ_MASK; 914 frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0; 915 frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0; 916 highSeq = CIRCLEQ_EMPTY(&priv->frags) ? 917 frag->seq : CIRCLEQ_FIRST(&priv->frags)->seq; 918 diff = MP_LONG_SEQ_DIFF(frag->seq, highSeq); 919 m_adj(m, 4); 920 } 921 frag->data = m; 922 frag->meta = meta; 923 924 /* If the sequence number makes a large jump, empty the queue */ 925 if (diff <= -MP_INSANE_SEQ_JUMP || diff >= MP_INSANE_SEQ_JUMP) 926 ng_ppp_free_frags(node); 927 928 /* Optimization: handle a frame that's all in one fragment */ 929 if (frag->first && frag->last) 930 return ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, m, meta); 931 932 /* Allocate a new frag struct for the queue */ 933 MALLOC(frag, struct ng_ppp_frag *, sizeof(*frag), M_NETGRAPH, M_NOWAIT); 934 if (frag == NULL) { 935 NG_FREE_DATA(m, meta); 936 return (ENOMEM); 937 } 938 *frag = frag0; 939 meta = NULL; 940 m = NULL; 941 942 /* Add fragment to queue, which is reverse sorted by sequence number */ 943 inserted = 0; 944 CIRCLEQ_FOREACH(qent, &priv->frags, f_qent) { 945 diff = MP_SEQ_DIFF(priv, frag->seq, qent->seq); 946 if (diff > 0) { 947 CIRCLEQ_INSERT_BEFORE(&priv->frags, qent, frag, f_qent); 948 inserted = 1; 949 break; 950 } else if (diff == 0) { /* should never happen! */ 951 log(LOG_ERR, "%s: rec'd dup MP fragment\n", node->name); 952 if (linkNum == NG_PPP_BUNDLE_LINKNUM) 953 priv->linkStats[linkNum].dupFragments++; 954 else 955 priv->bundleStats.dupFragments++; 956 NG_FREE_DATA(frag->data, frag->meta); 957 FREE(frag, M_NETGRAPH); 958 return (EINVAL); 959 } 960 } 961 if (!inserted) 962 CIRCLEQ_INSERT_TAIL(&priv->frags, frag, f_qent); 963 964 /* Find the last fragment in the possibly newly completed frame */ 965 last = NULL; 966 qent = frag; 967 nextSeq = frag->seq; 968 while (qent != (void *)&priv->frags && qent->seq == nextSeq) { 969 if (qent->last) { 970 last = qent; 971 break; 972 } 973 qent = CIRCLEQ_PREV(qent, f_qent); 974 nextSeq = (nextSeq + 1) & MP_SEQ_MASK(priv); 975 } 976 if (last == NULL) 977 goto incomplete; 978 979 /* Find the first fragment in the possibly newly completed frame */ 980 first = NULL; 981 qent = frag; 982 nextSeq = frag->seq; 983 while (qent != (void *)&priv->frags && qent->seq == nextSeq) { 984 if (qent->first) { 985 first = qent; 986 break; 987 } 988 qent = CIRCLEQ_NEXT(qent, f_qent); 989 nextSeq = (nextSeq - 1) & MP_SEQ_MASK(priv); 990 } 991 if (first == NULL) 992 goto incomplete; 993 994 /* We have a complete frame, extract it from the queue */ 995 for (tail = NULL, qent = first; qent != NULL; qent = qnext) { 996 qnext = CIRCLEQ_PREV(qent, f_qent); 997 CIRCLEQ_REMOVE(&priv->frags, qent, f_qent); 998 if (tail == NULL) { 999 tail = m = qent->data; 1000 meta = qent->meta; /* inherit first frag's meta */ 1001 } else { 1002 m->m_pkthdr.len += qent->data->m_pkthdr.len; 1003 tail->m_next = qent->data; 1004 NG_FREE_META(qent->meta); /* drop other frags' metas */ 1005 } 1006 while (tail->m_next != NULL) 1007 tail = tail->m_next; 1008 if (qent == last) 1009 qnext = NULL; 1010 FREE(qent, M_NETGRAPH); 1011 } 1012 1013 incomplete: 1014 /* Prune out stale entries in the queue */ 1015 for (qent = CIRCLEQ_LAST(&priv->frags); 1016 qent != (void *)&priv->frags; qent = qnext) { 1017 if (MP_SEQ_DIFF(priv, highSeq, qent->seq) <= MP_MAX_SEQ_LINGER) 1018 break; 1019 qnext = CIRCLEQ_PREV(qent, f_qent); 1020 CIRCLEQ_REMOVE(&priv->frags, qent, f_qent); 1021 NG_FREE_DATA(qent->data, qent->meta); 1022 FREE(qent, M_NETGRAPH); 1023 } 1024 1025 /* Deliver newly completed frame, if any */ 1026 return m ? ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, m, meta) : 0; 1027 } 1028 1029 /* 1030 * Deliver a frame out on the bundle, i.e., figure out how to fragment 1031 * the frame across the individual PPP links and do so. 1032 */ 1033 static int 1034 ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta) 1035 { 1036 const priv_p priv = node->private; 1037 int distrib[NG_PPP_MAX_LINKS]; 1038 int firstFragment; 1039 int activeLinkNum; 1040 1041 /* At least one link must be active */ 1042 if (priv->numActiveLinks == 0) { 1043 NG_FREE_DATA(m, meta); 1044 return (ENETDOWN); 1045 } 1046 1047 /* Round-robin strategy */ 1048 if (priv->conf.enableRoundRobin || m->m_pkthdr.len < MP_MIN_FRAG_LEN) { 1049 activeLinkNum = priv->lastLink++ % priv->numActiveLinks; 1050 bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0])); 1051 distrib[activeLinkNum] = m->m_pkthdr.len; 1052 goto deliver; 1053 } 1054 1055 /* Strategy when all links are equivalent (optimize the common case) */ 1056 if (priv->allLinksEqual) { 1057 const int fraction = m->m_pkthdr.len / priv->numActiveLinks; 1058 int i, remain; 1059 1060 for (i = 0; i < priv->numActiveLinks; i++) 1061 distrib[priv->lastLink++ % priv->numActiveLinks] 1062 = fraction; 1063 remain = m->m_pkthdr.len - (fraction * priv->numActiveLinks); 1064 while (remain > 0) { 1065 distrib[priv->lastLink++ % priv->numActiveLinks]++; 1066 remain--; 1067 } 1068 goto deliver; 1069 } 1070 1071 /* Strategy when all links are not equivalent */ 1072 ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib); 1073 1074 deliver: 1075 /* Update stats */ 1076 priv->bundleStats.xmitFrames++; 1077 priv->bundleStats.xmitOctets += m->m_pkthdr.len; 1078 1079 /* Send alloted portions of frame out on the link(s) */ 1080 for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1; 1081 activeLinkNum >= 0; activeLinkNum--) { 1082 const int linkNum = priv->activeLinks[activeLinkNum]; 1083 1084 /* Deliver fragment(s) out the next link */ 1085 for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) { 1086 int len, lastFragment, error; 1087 struct mbuf *m2; 1088 meta_p meta2; 1089 1090 /* Calculate fragment length; don't exceed link MTU */ 1091 len = distrib[activeLinkNum]; 1092 if (len > priv->conf.links[linkNum].mru) 1093 len = priv->conf.links[linkNum].mru; 1094 distrib[activeLinkNum] -= len; 1095 lastFragment = (len == m->m_pkthdr.len); 1096 1097 /* Split off next fragment as "m2" */ 1098 m2 = m; 1099 if (!lastFragment) { 1100 struct mbuf *n = m_split(m, len, M_NOWAIT); 1101 1102 if (n == NULL) { 1103 NG_FREE_DATA(m, meta); 1104 return (ENOMEM); 1105 } 1106 m = n; 1107 } 1108 1109 /* Prepend MP header */ 1110 if (priv->conf.xmitShortSeq) { 1111 u_int16_t shdr; 1112 1113 shdr = priv->mpSeqOut; 1114 priv->mpSeqOut = 1115 (priv->mpSeqOut + 1) % MP_SHORT_SEQ_MASK; 1116 if (firstFragment) 1117 shdr |= MP_SHORT_FIRST_FLAG; 1118 if (lastFragment) 1119 shdr |= MP_SHORT_LAST_FLAG; 1120 shdr = htons(shdr); 1121 m2 = ng_ppp_prepend(m2, &shdr, 2); 1122 } else { 1123 u_int32_t lhdr; 1124 1125 lhdr = priv->mpSeqOut; 1126 priv->mpSeqOut = 1127 (priv->mpSeqOut + 1) % MP_LONG_SEQ_MASK; 1128 if (firstFragment) 1129 lhdr |= MP_LONG_FIRST_FLAG; 1130 if (lastFragment) 1131 lhdr |= MP_LONG_LAST_FLAG; 1132 lhdr = htonl(lhdr); 1133 m2 = ng_ppp_prepend(m2, &lhdr, 4); 1134 } 1135 if (m2 == NULL) { 1136 if (!lastFragment) 1137 m_freem(m); 1138 NG_FREE_META(meta); 1139 return (ENOBUFS); 1140 } 1141 1142 /* Copy the meta information, if any */ 1143 if (meta != NULL && !lastFragment) { 1144 MALLOC(meta2, meta_p, 1145 meta->used_len, M_NETGRAPH, M_NOWAIT); 1146 if (meta2 == NULL) { 1147 m_freem(m2); 1148 NG_FREE_DATA(m, meta); 1149 return (ENOMEM); 1150 } 1151 meta2->allocated_len = meta->used_len; 1152 bcopy(meta, meta2, meta->used_len); 1153 } else 1154 meta2 = meta; 1155 1156 /* Send fragment */ 1157 error = ng_ppp_output(node, 0, 1158 PROT_MP, linkNum, m2, meta2); 1159 if (error != 0) { 1160 if (!lastFragment) 1161 NG_FREE_DATA(m, meta); 1162 return (error); 1163 } 1164 } 1165 } 1166 1167 /* Done */ 1168 return (0); 1169 } 1170 1171 /* 1172 * Computing the optimal fragmentation 1173 * ----------------------------------- 1174 * 1175 * This routine tries to compute the optimal fragmentation pattern based 1176 * on each link's latency, bandwidth, and calculated additional latency. 1177 * The latter quantity is the additional latency caused by previously 1178 * written data that has not been transmitted yet. 1179 * 1180 * This algorithm is only useful when not all of the links have the 1181 * same latency and bandwidth values. 1182 * 1183 * The essential idea is to make the last bit of each fragment of the 1184 * frame arrive at the opposite end at the exact same time. This greedy 1185 * algorithm is optimal, in that no other scheduling could result in any 1186 * packet arriving any sooner unless packets are delivered out of order. 1187 * 1188 * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and 1189 * latency l_i (in miliseconds). Consider the function function f_i(t) 1190 * which is equal to the number of bytes that will have arrived at 1191 * the peer after t miliseconds if we start writing continuously at 1192 * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i). 1193 * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i). 1194 * Note that the y-intersect is always <= zero because latency can't be 1195 * negative. Note also that really the function is f_i(t) except when 1196 * f_i(t) is negative, in which case the function is zero. To take 1197 * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }. 1198 * So the actual number of bytes that will have arrived at the peer after 1199 * t miliseconds is f_i(t) * Q_i(t). 1200 * 1201 * At any given time, each link has some additional latency a_i >= 0 1202 * due to previously written fragment(s) which are still in the queue. 1203 * This value is easily computed from the time since last transmission, 1204 * the previous latency value, the number of bytes written, and the 1205 * link's bandwidth. 1206 * 1207 * Assume that l_i includes any a_i already, and that the links are 1208 * sorted by latency, so that l_i <= l_{i+1}. 1209 * 1210 * Let N be the total number of bytes in the current frame we are sending. 1211 * 1212 * Suppose we were to start writing bytes at time t = 0 on all links 1213 * simultaneously, which is the most we can possibly do. Then let 1214 * F(t) be equal to the total number of bytes received by the peer 1215 * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)). 1216 * 1217 * Our goal is simply this: fragment the frame across the links such 1218 * that the peer is able to reconstruct the completed frame as soon as 1219 * possible, i.e., at the least possible value of t. Call this value t_0. 1220 * 1221 * Then it follows that F(t_0) = N. Our strategy is first to find the value 1222 * of t_0, and then deduce how many bytes to write to each link. 1223 * 1224 * Rewriting F(t_0): 1225 * 1226 * t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) ) 1227 * 1228 * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will 1229 * lie in one of these ranges. To find it, we just need to find the i such 1230 * that F(l_i) <= N <= F(l_{i+1}). Then we compute all the constant values 1231 * for Q_i() in this range, plug in the remaining values, solving for t_0. 1232 * 1233 * Once t_0 is known, then the number of bytes to send on link i is 1234 * just f_i(t_0) * Q_i(t_0). 1235 * 1236 * In other words, we start allocating bytes to the links one at a time. 1237 * We keep adding links until the frame is completely sent. Some links 1238 * may not get any bytes because their latency is too high. 1239 * 1240 * Is all this work really worth the trouble? Depends on the situation. 1241 * The bigger the ratio of computer speed to link speed, and the more 1242 * important total bundle latency is (e.g., for interactive response time), 1243 * the more it's worth it. There is however the cost of calling this 1244 * function for every frame. The running time is O(n^2) where n is the 1245 * number of links that receive a non-zero number of bytes. 1246 * 1247 * Since latency is measured in miliseconds, the "resolution" of this 1248 * algorithm is one milisecond. 1249 * 1250 * To avoid this algorithm altogether, configure all links to have the 1251 * same latency and bandwidth. 1252 */ 1253 static void 1254 ng_ppp_mp_strategy(node_p node, int len, int *distrib) 1255 { 1256 const priv_p priv = node->private; 1257 int latency[NG_PPP_MAX_LINKS]; 1258 int sortByLatency[NG_PPP_MAX_LINKS]; 1259 int activeLinkNum, linkNum; 1260 int t0, total, topSum, botSum; 1261 struct timeval now; 1262 int i, numFragments; 1263 1264 /* If only one link, this gets real easy */ 1265 if (priv->numActiveLinks == 1) { 1266 distrib[0] = len; 1267 return; 1268 } 1269 1270 /* Get current time */ 1271 microtime(&now); 1272 1273 /* Compute latencies for each link at this point in time */ 1274 for (activeLinkNum = 0; 1275 activeLinkNum < priv->numActiveLinks; activeLinkNum++) { 1276 struct timeval diff; 1277 int xmitBytes; 1278 1279 /* Start with base latency value */ 1280 linkNum = priv->activeLinks[activeLinkNum]; 1281 latency[activeLinkNum] = priv->conf.links[linkNum].latency; 1282 sortByLatency[activeLinkNum] = activeLinkNum; /* see below */ 1283 1284 /* Any additional latency? */ 1285 if (priv->qstat[activeLinkNum].bytesInQueue == 0) 1286 continue; 1287 1288 /* Compute time delta since last write */ 1289 diff = now; 1290 timevalsub(&diff, &priv->qstat[activeLinkNum].lastWrite); 1291 if (now.tv_sec < 0 || diff.tv_sec >= 10) { /* sanity */ 1292 priv->qstat[activeLinkNum].bytesInQueue = 0; 1293 continue; 1294 } 1295 1296 /* How many bytes could have transmitted since last write? */ 1297 xmitBytes = priv->conf.links[linkNum].bandwidth * diff.tv_sec 1298 + (priv->conf.links[linkNum].bandwidth 1299 * (diff.tv_usec / 1000)) / 100; 1300 priv->qstat[activeLinkNum].bytesInQueue -= xmitBytes; 1301 if (priv->qstat[activeLinkNum].bytesInQueue < 0) 1302 priv->qstat[activeLinkNum].bytesInQueue = 0; 1303 else 1304 latency[activeLinkNum] += 1305 (100 * priv->qstat[activeLinkNum].bytesInQueue) 1306 / priv->conf.links[linkNum].bandwidth; 1307 } 1308 1309 /* Sort links by latency */ 1310 compareLatencies = latency; 1311 qsort(sortByLatency, 1312 priv->numActiveLinks, sizeof(*sortByLatency), ng_ppp_intcmp); 1313 compareLatencies = NULL; 1314 1315 /* Find the interval we need (add links in sortByLatency[] order) */ 1316 for (numFragments = 1; 1317 numFragments < priv->numActiveLinks; numFragments++) { 1318 for (total = i = 0; i < numFragments; i++) { 1319 int flowTime; 1320 1321 flowTime = latency[sortByLatency[numFragments]] 1322 - latency[sortByLatency[i]]; 1323 total += ((flowTime * priv->conf.links[ 1324 priv->activeLinks[sortByLatency[i]]].bandwidth) 1325 + 99) / 100; 1326 } 1327 if (total >= len) 1328 break; 1329 } 1330 1331 /* Solve for t_0 in that interval */ 1332 for (topSum = botSum = i = 0; i < numFragments; i++) { 1333 int bw = priv->conf.links[ 1334 priv->activeLinks[sortByLatency[i]]].bandwidth; 1335 1336 topSum += latency[sortByLatency[i]] * bw; /* / 100 */ 1337 botSum += bw; /* / 100 */ 1338 } 1339 t0 = ((len * 100) + topSum + botSum / 2) / botSum; 1340 1341 /* Compute f_i(t_0) all i */ 1342 bzero(distrib, priv->numActiveLinks * sizeof(*distrib)); 1343 for (total = i = 0; i < numFragments; i++) { 1344 int bw = priv->conf.links[ 1345 priv->activeLinks[sortByLatency[i]]].bandwidth; 1346 1347 distrib[sortByLatency[i]] = 1348 (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100; 1349 total += distrib[sortByLatency[i]]; 1350 } 1351 1352 /* Deal with any rounding error */ 1353 if (total < len) { 1354 int fast = 0; 1355 1356 /* Find the fastest link */ 1357 for (i = 1; i < numFragments; i++) { 1358 if (priv->conf.links[ 1359 priv->activeLinks[sortByLatency[i]]].bandwidth > 1360 priv->conf.links[ 1361 priv->activeLinks[sortByLatency[fast]]].bandwidth) 1362 fast = i; 1363 } 1364 distrib[sortByLatency[fast]] += len - total; 1365 } else while (total > len) { 1366 int delta, slow = 0; 1367 1368 /* Find the slowest link that still has bytes to remove */ 1369 for (i = 1; i < numFragments; i++) { 1370 if (distrib[sortByLatency[slow]] == 0 1371 || (distrib[sortByLatency[i]] > 0 1372 && priv->conf.links[priv->activeLinks[ 1373 sortByLatency[i]]].bandwidth < 1374 priv->conf.links[priv->activeLinks[ 1375 sortByLatency[slow]]].bandwidth)) 1376 slow = i; 1377 } 1378 delta = total - len; 1379 if (delta > distrib[sortByLatency[slow]]) 1380 delta = distrib[sortByLatency[slow]]; 1381 distrib[sortByLatency[slow]] -= delta; 1382 total -= delta; 1383 } 1384 } 1385 1386 /* 1387 * Compare two integers 1388 */ 1389 static int 1390 ng_ppp_intcmp(const void *v1, const void *v2) 1391 { 1392 const int index1 = *((const int *) v1); 1393 const int index2 = *((const int *) v2); 1394 1395 return compareLatencies[index1] - compareLatencies[index2]; 1396 } 1397 1398 /* 1399 * Prepend a possibly compressed PPP protocol number in front of a frame 1400 */ 1401 static struct mbuf * 1402 ng_ppp_addproto(struct mbuf *m, int proto, int compOK) 1403 { 1404 if (compOK && PROT_COMPRESSABLE(proto)) { 1405 u_char pbyte = (u_char)proto; 1406 1407 return ng_ppp_prepend(m, &pbyte, 1); 1408 } else { 1409 u_int16_t pword = htons((u_int16_t)proto); 1410 1411 return ng_ppp_prepend(m, &pword, 2); 1412 } 1413 } 1414 1415 /* 1416 * Prepend some bytes to an mbuf 1417 */ 1418 static struct mbuf * 1419 ng_ppp_prepend(struct mbuf *m, const void *buf, int len) 1420 { 1421 M_PREPEND(m, len, M_NOWAIT); 1422 if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL)) 1423 return (NULL); 1424 bcopy(buf, mtod(m, u_char *), len); 1425 return (m); 1426 } 1427 1428 /* 1429 * Update private information that is derived from other private information 1430 */ 1431 static void 1432 ng_ppp_update(node_p node, int newConf) 1433 { 1434 const priv_p priv = node->private; 1435 int i; 1436 1437 /* Update active status for VJ Compression */ 1438 priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL 1439 && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL 1440 && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL 1441 && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL; 1442 1443 /* Increase latency for each link an amount equal to one MP header */ 1444 if (newConf) { 1445 for (i = 0; i < NG_PPP_MAX_LINKS; i++) { 1446 int hdrBytes; 1447 1448 hdrBytes = (priv->conf.links[i].enableACFComp ? 0 : 2) 1449 + (priv->conf.links[i].enableProtoComp ? 1 : 2) 1450 + (priv->conf.xmitShortSeq ? 2 : 4); 1451 priv->conf.links[i].latency += 1452 ((hdrBytes * priv->conf.links[i].bandwidth) + 50) 1453 / 100; 1454 } 1455 } 1456 1457 /* Update list of active links */ 1458 bzero(&priv->activeLinks, sizeof(priv->activeLinks)); 1459 priv->numActiveLinks = 0; 1460 priv->allLinksEqual = 1; 1461 for (i = 0; i < NG_PPP_MAX_LINKS; i++) { 1462 struct ng_ppp_link_config *const lc = &priv->conf.links[i]; 1463 1464 if (lc->enableLink && priv->links[i] != NULL) { 1465 priv->activeLinks[priv->numActiveLinks++] = i; 1466 if (lc->latency != 1467 priv->conf.links[priv->activeLinks[0]].latency 1468 || lc->bandwidth != 1469 priv->conf.links[priv->activeLinks[0]].bandwidth) 1470 priv->allLinksEqual = 0; 1471 } 1472 } 1473 1474 /* Reset MP state if multi-link is no longer active */ 1475 if (!priv->conf.enableMultilink || priv->numActiveLinks == 0) { 1476 ng_ppp_free_frags(node); 1477 priv->mpSeqOut = MP_INITIAL_SEQ; 1478 bzero(&priv->qstat, sizeof(priv->qstat)); 1479 } 1480 } 1481 1482 /* 1483 * Determine if a new configuration would represent a valid change 1484 * from the current configuration and link activity status. 1485 */ 1486 static int 1487 ng_ppp_config_valid(node_p node, const struct ng_ppp_node_config *newConf) 1488 { 1489 const priv_p priv = node->private; 1490 int i, newNumLinksActive; 1491 1492 /* Check per-link config and count how many links would be active */ 1493 for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) { 1494 if (newConf->links[i].enableLink && priv->links[i] != NULL) 1495 newNumLinksActive++; 1496 if (!newConf->links[i].enableLink) 1497 continue; 1498 if (newConf->links[i].mru < MP_MIN_LINK_MRU) 1499 return (0); 1500 if (newConf->links[i].bandwidth == 0) 1501 return (0); 1502 if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH) 1503 return (0); 1504 if (newConf->links[i].latency > NG_PPP_MAX_LATENCY) 1505 return (0); 1506 } 1507 1508 /* Check bundle parameters */ 1509 if (newConf->enableMultilink && newConf->mrru < MP_MIN_MRRU) 1510 return (0); 1511 1512 /* Disallow changes to multi-link configuration while MP is active */ 1513 if (priv->numActiveLinks > 0 && newNumLinksActive > 0) { 1514 if (!priv->conf.enableMultilink != !newConf->enableMultilink 1515 || !priv->conf.xmitShortSeq != !newConf->xmitShortSeq 1516 || !priv->conf.recvShortSeq != !newConf->recvShortSeq) 1517 return (0); 1518 } 1519 1520 /* At most one link can be active unless multi-link is enabled */ 1521 if (!newConf->enableMultilink && newNumLinksActive > 1) 1522 return (0); 1523 1524 /* Configuration change would be valid */ 1525 return (1); 1526 } 1527 1528 /* 1529 * Free all entries in the fragment queue 1530 */ 1531 static void 1532 ng_ppp_free_frags(node_p node) 1533 { 1534 const priv_p priv = node->private; 1535 struct ng_ppp_frag *qent, *qnext; 1536 1537 for (qent = CIRCLEQ_FIRST(&priv->frags); 1538 qent != (void *)&priv->frags; qent = qnext) { 1539 qnext = CIRCLEQ_NEXT(qent, f_qent); 1540 NG_FREE_DATA(qent->data, qent->meta); 1541 FREE(qent, M_NETGRAPH); 1542 } 1543 CIRCLEQ_INIT(&priv->frags); 1544 } 1545 1546