2 * Copyright (c) 1996-2000 Whistle Communications, Inc.
5 * Subject to the following obligations and disclaimer of warranty, use and
6 * redistribution of this software, in source or object code forms, with or
7 * without modifications are expressly permitted by Whistle Communications;
8 * provided, however, that:
9 * 1. Any and all reproductions of the source or object code must include the
10 * copyright notice above and the following disclaimer of warranties; and
11 * 2. No rights are granted, in any manner or form, to use Whistle
12 * Communications, Inc. trademarks, including the mark "WHISTLE
13 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
14 * such appears in the above copyright notice or in the software.
16 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
17 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
18 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
19 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
21 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
22 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
23 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
24 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
25 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
26 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
34 * Copyright (c) 2007 Alexander Motin <mav@alkar.net>
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice unmodified, this list of conditions, and the following
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Authors: Archie Cobbs <archie@freebsd.org>, Alexander Motin <mav@alkar.net>
62 * $Whistle: ng_ppp.c,v 1.24 1999/11/01 09:24:52 julian Exp $
66 * PPP node type data-flow.
68 * hook xmit layer recv hook
69 * ------------------------------------
75 * -hcomp_xmit()----------proto_recv()-
77 * vjc_comp -> header compression -> vjc_comp
78 * vjc_uncomp -> -> vjc_uncomp
80 * -comp_xmit()-----------hcomp_recv()-
81 * compress <- compression <- decompress
82 * compress -> -> decompress
83 * -crypt_xmit()-----------comp_recv()-
84 * encrypt <- encryption <- decrypt
85 * encrypt -> -> decrypt
86 * -ml_xmit()-------------crypt_recv()-
88 * -link_xmit()--------------ml_recv()-
89 * linkX <- link <- linkX
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/kernel.h>
96 #include <sys/limits.h>
99 #include <sys/malloc.h>
100 #include <sys/errno.h>
101 #include <sys/ctype.h>
103 #include <netgraph/ng_message.h>
104 #include <netgraph/netgraph.h>
105 #include <netgraph/ng_parse.h>
106 #include <netgraph/ng_ppp.h>
107 #include <netgraph/ng_vjc.h>
109 #ifdef NG_SEPARATE_MALLOC
110 MALLOC_DEFINE(M_NETGRAPH_PPP, "netgraph_ppp", "netgraph ppp node");
112 #define M_NETGRAPH_PPP M_NETGRAPH
115 #define PROT_VALID(p) (((p) & 0x0101) == 0x0001)
116 #define PROT_COMPRESSABLE(p) (((p) & 0xff00) == 0x0000)
118 /* Some PPP protocol numbers we're interested in */
119 #define PROT_ATALK 0x0029
120 #define PROT_COMPD 0x00fd
121 #define PROT_CRYPTD 0x0053
122 #define PROT_IP 0x0021
123 #define PROT_IPV6 0x0057
124 #define PROT_IPX 0x002b
125 #define PROT_LCP 0xc021
126 #define PROT_MP 0x003d
127 #define PROT_VJCOMP 0x002d
128 #define PROT_VJUNCOMP 0x002f
130 /* Multilink PPP definitions */
131 #define MP_MIN_MRRU 1500 /* per RFC 1990 */
132 #define MP_INITIAL_SEQ 0 /* per RFC 1990 */
133 #define MP_MIN_LINK_MRU 32
135 #define MP_SHORT_SEQ_MASK 0x00000fff /* short seq # mask */
136 #define MP_SHORT_SEQ_HIBIT 0x00000800 /* short seq # high bit */
137 #define MP_SHORT_FIRST_FLAG 0x00008000 /* first fragment in frame */
138 #define MP_SHORT_LAST_FLAG 0x00004000 /* last fragment in frame */
140 #define MP_LONG_SEQ_MASK 0x00ffffff /* long seq # mask */
141 #define MP_LONG_SEQ_HIBIT 0x00800000 /* long seq # high bit */
142 #define MP_LONG_FIRST_FLAG 0x80000000 /* first fragment in frame */
143 #define MP_LONG_LAST_FLAG 0x40000000 /* last fragment in frame */
145 #define MP_NOSEQ 0x7fffffff /* impossible sequence number */
147 /* Sign extension of MP sequence numbers */
148 #define MP_SHORT_EXTEND(s) (((s) & MP_SHORT_SEQ_HIBIT) ? \
149 ((s) | ~MP_SHORT_SEQ_MASK) \
150 : ((s) & MP_SHORT_SEQ_MASK))
151 #define MP_LONG_EXTEND(s) (((s) & MP_LONG_SEQ_HIBIT) ? \
152 ((s) | ~MP_LONG_SEQ_MASK) \
153 : ((s) & MP_LONG_SEQ_MASK))
155 /* Comparision of MP sequence numbers. Note: all sequence numbers
156 except priv->xseq are stored with the sign bit extended. */
157 #define MP_SHORT_SEQ_DIFF(x,y) MP_SHORT_EXTEND((x) - (y))
158 #define MP_LONG_SEQ_DIFF(x,y) MP_LONG_EXTEND((x) - (y))
160 #define MP_RECV_SEQ_DIFF(priv,x,y) \
161 ((priv)->conf.recvShortSeq ? \
162 MP_SHORT_SEQ_DIFF((x), (y)) : \
163 MP_LONG_SEQ_DIFF((x), (y)))
165 /* Increment receive sequence number */
166 #define MP_NEXT_RECV_SEQ(priv,seq) \
167 ((priv)->conf.recvShortSeq ? \
168 MP_SHORT_EXTEND((seq) + 1) : \
169 MP_LONG_EXTEND((seq) + 1))
171 /* Don't fragment transmitted packets to parts smaller than this */
172 #define MP_MIN_FRAG_LEN 32
174 /* Maximum fragment reasssembly queue length */
175 #define MP_MAX_QUEUE_LEN 128
177 /* Fragment queue scanner period */
178 #define MP_FRAGTIMER_INTERVAL (hz/2)
180 /* Average link overhead. XXX: Should be given by user-level */
181 #define MP_AVERAGE_LINK_OVERHEAD 16
183 /* Keep this equal to ng_ppp_hook_names lower! */
184 #define HOOK_INDEX_MAX 13
186 /* We store incoming fragments this way */
188 int seq; /* fragment seq# */
189 uint8_t first; /* First in packet? */
190 uint8_t last; /* Last in packet? */
191 struct timeval timestamp; /* time of reception */
192 struct mbuf *data; /* Fragment data */
193 TAILQ_ENTRY(ng_ppp_frag) f_qent; /* Fragment queue */
196 /* Per-link private information */
198 struct ng_ppp_link_conf conf; /* link configuration */
199 struct ng_ppp_link_stat stats; /* link stats */
200 hook_p hook; /* connection to link data */
201 int32_t seq; /* highest rec'd seq# - MSEQ */
202 uint32_t latency; /* calculated link latency */
203 struct timeval lastWrite; /* time of last write for MP */
204 int bytesInQueue; /* bytes in the output queue for MP */
207 /* Total per-node private information */
208 struct ng_ppp_private {
209 struct ng_ppp_bund_conf conf; /* bundle config */
210 struct ng_ppp_link_stat bundleStats; /* bundle stats */
211 struct ng_ppp_link links[NG_PPP_MAX_LINKS];/* per-link info */
212 int32_t xseq; /* next out MP seq # */
213 int32_t mseq; /* min links[i].seq */
214 uint16_t activeLinks[NG_PPP_MAX_LINKS]; /* indicies */
215 uint16_t numActiveLinks; /* how many links up */
216 uint16_t lastLink; /* for round robin */
217 uint8_t vjCompHooked; /* VJ comp hooked up? */
218 uint8_t allLinksEqual; /* all xmit the same? */
219 hook_p hooks[HOOK_INDEX_MAX]; /* non-link hooks */
220 TAILQ_HEAD(ng_ppp_fraglist, ng_ppp_frag) /* fragment queue */
222 int qlen; /* fraq queue length */
223 struct callout fragTimer; /* fraq queue check */
225 typedef struct ng_ppp_private *priv_p;
227 /* Netgraph node methods */
228 static ng_constructor_t ng_ppp_constructor;
229 static ng_rcvmsg_t ng_ppp_rcvmsg;
230 static ng_shutdown_t ng_ppp_shutdown;
231 static ng_newhook_t ng_ppp_newhook;
232 static ng_rcvdata_t ng_ppp_rcvdata;
233 static ng_disconnect_t ng_ppp_disconnect;
235 static ng_rcvdata_t ng_ppp_rcvdata_inet;
236 static ng_rcvdata_t ng_ppp_rcvdata_ipv6;
237 static ng_rcvdata_t ng_ppp_rcvdata_ipx;
238 static ng_rcvdata_t ng_ppp_rcvdata_atalk;
239 static ng_rcvdata_t ng_ppp_rcvdata_bypass;
241 static ng_rcvdata_t ng_ppp_rcvdata_vjc_ip;
242 static ng_rcvdata_t ng_ppp_rcvdata_vjc_comp;
243 static ng_rcvdata_t ng_ppp_rcvdata_vjc_uncomp;
244 static ng_rcvdata_t ng_ppp_rcvdata_vjc_vjip;
246 static ng_rcvdata_t ng_ppp_rcvdata_compress;
247 static ng_rcvdata_t ng_ppp_rcvdata_decompress;
249 static ng_rcvdata_t ng_ppp_rcvdata_encrypt;
250 static ng_rcvdata_t ng_ppp_rcvdata_decrypt;
252 /* We use integer indicies to refer to the non-link hooks. */
253 static const struct {
256 } ng_ppp_hook_names[] = {
257 #define HOOK_INDEX_ATALK 0
258 { NG_PPP_HOOK_ATALK, ng_ppp_rcvdata_atalk },
259 #define HOOK_INDEX_BYPASS 1
260 { NG_PPP_HOOK_BYPASS, ng_ppp_rcvdata_bypass },
261 #define HOOK_INDEX_COMPRESS 2
262 { NG_PPP_HOOK_COMPRESS, ng_ppp_rcvdata_compress },
263 #define HOOK_INDEX_ENCRYPT 3
264 { NG_PPP_HOOK_ENCRYPT, ng_ppp_rcvdata_encrypt },
265 #define HOOK_INDEX_DECOMPRESS 4
266 { NG_PPP_HOOK_DECOMPRESS, ng_ppp_rcvdata_decompress },
267 #define HOOK_INDEX_DECRYPT 5
268 { NG_PPP_HOOK_DECRYPT, ng_ppp_rcvdata_decrypt },
269 #define HOOK_INDEX_INET 6
270 { NG_PPP_HOOK_INET, ng_ppp_rcvdata_inet },
271 #define HOOK_INDEX_IPX 7
272 { NG_PPP_HOOK_IPX, ng_ppp_rcvdata_ipx },
273 #define HOOK_INDEX_VJC_COMP 8
274 { NG_PPP_HOOK_VJC_COMP, ng_ppp_rcvdata_vjc_comp },
275 #define HOOK_INDEX_VJC_IP 9
276 { NG_PPP_HOOK_VJC_IP, ng_ppp_rcvdata_vjc_ip },
277 #define HOOK_INDEX_VJC_UNCOMP 10
278 { NG_PPP_HOOK_VJC_UNCOMP, ng_ppp_rcvdata_vjc_uncomp },
279 #define HOOK_INDEX_VJC_VJIP 11
280 { NG_PPP_HOOK_VJC_VJIP, ng_ppp_rcvdata_vjc_vjip },
281 #define HOOK_INDEX_IPV6 12
282 { NG_PPP_HOOK_IPV6, ng_ppp_rcvdata_ipv6 },
286 /* Helper functions */
287 static int ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto,
289 static int ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto);
290 static int ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto,
292 static int ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto);
293 static int ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto,
295 static int ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto);
296 static int ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto,
298 static int ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto);
299 static int ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto,
301 static int ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto,
304 static int ng_ppp_bypass(node_p node, item_p item, uint16_t proto,
307 static void ng_ppp_bump_mseq(node_p node, int32_t new_mseq);
308 static int ng_ppp_frag_drop(node_p node);
309 static int ng_ppp_check_packet(node_p node);
310 static void ng_ppp_get_packet(node_p node, struct mbuf **mp);
311 static int ng_ppp_frag_process(node_p node);
312 static int ng_ppp_frag_trim(node_p node);
313 static void ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1,
315 static void ng_ppp_frag_checkstale(node_p node);
316 static void ng_ppp_frag_reset(node_p node);
317 static void ng_ppp_mp_strategy(node_p node, int len, int *distrib);
318 static int ng_ppp_intcmp(void *latency, const void *v1, const void *v2);
319 static struct mbuf *ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK);
320 static struct mbuf *ng_ppp_cutproto(struct mbuf *m, uint16_t *proto);
321 static struct mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len);
322 static int ng_ppp_config_valid(node_p node,
323 const struct ng_ppp_node_conf *newConf);
324 static void ng_ppp_update(node_p node, int newConf);
325 static void ng_ppp_start_frag_timer(node_p node);
326 static void ng_ppp_stop_frag_timer(node_p node);
328 /* Parse type for struct ng_ppp_mp_state_type */
329 static const struct ng_parse_fixedarray_info ng_ppp_rseq_array_info = {
330 &ng_parse_hint32_type,
333 static const struct ng_parse_type ng_ppp_rseq_array_type = {
334 &ng_parse_fixedarray_type,
335 &ng_ppp_rseq_array_info,
337 static const struct ng_parse_struct_field ng_ppp_mp_state_type_fields[]
338 = NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type);
339 static const struct ng_parse_type ng_ppp_mp_state_type = {
340 &ng_parse_struct_type,
341 &ng_ppp_mp_state_type_fields
344 /* Parse type for struct ng_ppp_link_conf */
345 static const struct ng_parse_struct_field ng_ppp_link_type_fields[]
346 = NG_PPP_LINK_TYPE_INFO;
347 static const struct ng_parse_type ng_ppp_link_type = {
348 &ng_parse_struct_type,
349 &ng_ppp_link_type_fields
352 /* Parse type for struct ng_ppp_bund_conf */
353 static const struct ng_parse_struct_field ng_ppp_bund_type_fields[]
354 = NG_PPP_BUND_TYPE_INFO;
355 static const struct ng_parse_type ng_ppp_bund_type = {
356 &ng_parse_struct_type,
357 &ng_ppp_bund_type_fields
360 /* Parse type for struct ng_ppp_node_conf */
361 static const struct ng_parse_fixedarray_info ng_ppp_array_info = {
365 static const struct ng_parse_type ng_ppp_link_array_type = {
366 &ng_parse_fixedarray_type,
369 static const struct ng_parse_struct_field ng_ppp_conf_type_fields[]
370 = NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type);
371 static const struct ng_parse_type ng_ppp_conf_type = {
372 &ng_parse_struct_type,
373 &ng_ppp_conf_type_fields
376 /* Parse type for struct ng_ppp_link_stat */
377 static const struct ng_parse_struct_field ng_ppp_stats_type_fields[]
378 = NG_PPP_STATS_TYPE_INFO;
379 static const struct ng_parse_type ng_ppp_stats_type = {
380 &ng_parse_struct_type,
381 &ng_ppp_stats_type_fields
384 /* List of commands and how to convert arguments to/from ASCII */
385 static const struct ng_cmdlist ng_ppp_cmds[] = {
402 NGM_PPP_GET_MP_STATE,
405 &ng_ppp_mp_state_type
409 NGM_PPP_GET_LINK_STATS,
411 &ng_parse_int16_type,
416 NGM_PPP_CLR_LINK_STATS,
418 &ng_parse_int16_type,
423 NGM_PPP_GETCLR_LINK_STATS,
425 &ng_parse_int16_type,
431 /* Node type descriptor */
432 static struct ng_type ng_ppp_typestruct = {
433 .version = NG_ABI_VERSION,
434 .name = NG_PPP_NODE_TYPE,
435 .constructor = ng_ppp_constructor,
436 .rcvmsg = ng_ppp_rcvmsg,
437 .shutdown = ng_ppp_shutdown,
438 .newhook = ng_ppp_newhook,
439 .rcvdata = ng_ppp_rcvdata,
440 .disconnect = ng_ppp_disconnect,
441 .cmdlist = ng_ppp_cmds,
443 NETGRAPH_INIT(ppp, &ng_ppp_typestruct);
445 /* Address and control field header */
446 static const uint8_t ng_ppp_acf[2] = { 0xff, 0x03 };
448 /* Maximum time we'll let a complete incoming packet sit in the queue */
449 static const struct timeval ng_ppp_max_staleness = { 2, 0 }; /* 2 seconds */
451 #define ERROUT(x) do { error = (x); goto done; } while (0)
453 /************************************************************************
455 ************************************************************************/
458 * Node type constructor
461 ng_ppp_constructor(node_p node)
466 /* Allocate private structure */
467 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_PPP, M_NOWAIT | M_ZERO);
471 NG_NODE_SET_PRIVATE(node, priv);
473 /* Initialize state */
474 TAILQ_INIT(&priv->frags);
475 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
476 priv->links[i].seq = MP_NOSEQ;
477 ng_callout_init(&priv->fragTimer);
484 * Give our OK for a hook to be added
487 ng_ppp_newhook(node_p node, hook_p hook, const char *name)
489 const priv_p priv = NG_NODE_PRIVATE(node);
490 hook_p *hookPtr = NULL;
494 /* Figure out which hook it is */
495 if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX, /* a link hook? */
496 strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) {
500 cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
501 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
503 linkNum = (int)strtoul(cp, &eptr, 10);
504 if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS)
506 hookPtr = &priv->links[linkNum].hook;
507 hookIndex = ~linkNum;
509 /* See if hook is already connected. */
510 if (*hookPtr != NULL)
513 /* Disallow more than one link unless multilink is enabled. */
514 if (priv->links[linkNum].conf.enableLink &&
515 !priv->conf.enableMultilink && priv->numActiveLinks >= 1)
518 /* MP recv code is not thread-safe. */
519 NG_HOOK_FORCE_WRITER(hook);
521 } else { /* must be a non-link hook */
524 for (i = 0; ng_ppp_hook_names[i].name != NULL; i++) {
525 if (strcmp(name, ng_ppp_hook_names[i].name) == 0) {
526 hookPtr = &priv->hooks[i];
531 if (ng_ppp_hook_names[i].name == NULL)
532 return (EINVAL); /* no such hook */
534 /* See if hook is already connected */
535 if (*hookPtr != NULL)
538 /* Every non-linkX hook have it's own function. */
539 NG_HOOK_SET_RCVDATA(hook, ng_ppp_hook_names[i].fn);
544 NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)hookIndex);
545 ng_ppp_update(node, 0);
550 * Receive a control message
553 ng_ppp_rcvmsg(node_p node, item_p item, hook_p lasthook)
555 const priv_p priv = NG_NODE_PRIVATE(node);
556 struct ng_mesg *resp = NULL;
560 NGI_GET_MSG(item, msg);
561 switch (msg->header.typecookie) {
563 switch (msg->header.cmd) {
564 case NGM_PPP_SET_CONFIG:
566 struct ng_ppp_node_conf *const conf =
567 (struct ng_ppp_node_conf *)msg->data;
570 /* Check for invalid or illegal config */
571 if (msg->header.arglen != sizeof(*conf))
573 if (!ng_ppp_config_valid(node, conf))
577 priv->conf = conf->bund;
578 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
579 priv->links[i].conf = conf->links[i];
580 ng_ppp_update(node, 1);
583 case NGM_PPP_GET_CONFIG:
585 struct ng_ppp_node_conf *conf;
588 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
591 conf = (struct ng_ppp_node_conf *)resp->data;
592 conf->bund = priv->conf;
593 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
594 conf->links[i] = priv->links[i].conf;
597 case NGM_PPP_GET_MP_STATE:
599 struct ng_ppp_mp_state *info;
602 NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT);
605 info = (struct ng_ppp_mp_state *)resp->data;
606 bzero(info, sizeof(*info));
607 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
608 if (priv->links[i].seq != MP_NOSEQ)
609 info->rseq[i] = priv->links[i].seq;
611 info->mseq = priv->mseq;
612 info->xseq = priv->xseq;
615 case NGM_PPP_GET_LINK_STATS:
616 case NGM_PPP_CLR_LINK_STATS:
617 case NGM_PPP_GETCLR_LINK_STATS:
619 struct ng_ppp_link_stat *stats;
622 if (msg->header.arglen != sizeof(uint16_t))
624 linkNum = *((uint16_t *) msg->data);
625 if (linkNum >= NG_PPP_MAX_LINKS
626 && linkNum != NG_PPP_BUNDLE_LINKNUM)
628 stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ?
629 &priv->bundleStats : &priv->links[linkNum].stats;
630 if (msg->header.cmd != NGM_PPP_CLR_LINK_STATS) {
631 NG_MKRESPONSE(resp, msg,
632 sizeof(struct ng_ppp_link_stat), M_NOWAIT);
635 bcopy(stats, resp->data, sizeof(*stats));
637 if (msg->header.cmd != NGM_PPP_GET_LINK_STATS)
638 bzero(stats, sizeof(*stats));
649 * Forward it to the vjc node. leave the
650 * old return address alone.
651 * If we have no hook, let NG_RESPOND_MSG
652 * clean up any remaining resources.
653 * Because we have no resp, the item will be freed
654 * along with anything it references. Don't
655 * let msg be freed twice.
657 NGI_MSG(item) = msg; /* put it back in the item */
659 if ((lasthook = priv->hooks[HOOK_INDEX_VJC_IP])) {
660 NG_FWD_ITEM_HOOK(error, item, lasthook);
669 NG_RESPOND_MSG(error, node, item, resp);
678 ng_ppp_shutdown(node_p node)
680 const priv_p priv = NG_NODE_PRIVATE(node);
682 /* Stop fragment queue timer */
683 ng_ppp_stop_frag_timer(node);
685 /* Take down netgraph node */
686 ng_ppp_frag_reset(node);
687 bzero(priv, sizeof(*priv));
688 FREE(priv, M_NETGRAPH_PPP);
689 NG_NODE_SET_PRIVATE(node, NULL);
690 NG_NODE_UNREF(node); /* let the node escape */
698 ng_ppp_disconnect(hook_p hook)
700 const node_p node = NG_HOOK_NODE(hook);
701 const priv_p priv = NG_NODE_PRIVATE(node);
702 const int index = (intptr_t)NG_HOOK_PRIVATE(hook);
704 /* Zero out hook pointer */
706 priv->links[~index].hook = NULL;
708 priv->hooks[index] = NULL;
710 /* Update derived info (or go away if no hooks left). */
711 if (NG_NODE_NUMHOOKS(node) > 0)
712 ng_ppp_update(node, 0);
713 else if (NG_NODE_IS_VALID(node))
714 ng_rmnode_self(node);
724 * Receive data on a hook inet.
727 ng_ppp_rcvdata_inet(hook_p hook, item_p item)
729 const node_p node = NG_HOOK_NODE(hook);
730 const priv_p priv = NG_NODE_PRIVATE(node);
732 if (!priv->conf.enableIP) {
736 return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IP));
740 * Receive data on a hook ipv6.
743 ng_ppp_rcvdata_ipv6(hook_p hook, item_p item)
745 const node_p node = NG_HOOK_NODE(hook);
746 const priv_p priv = NG_NODE_PRIVATE(node);
748 if (!priv->conf.enableIPv6) {
752 return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPV6));
756 * Receive data on a hook atalk.
759 ng_ppp_rcvdata_atalk(hook_p hook, item_p item)
761 const node_p node = NG_HOOK_NODE(hook);
762 const priv_p priv = NG_NODE_PRIVATE(node);
764 if (!priv->conf.enableAtalk) {
768 return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_ATALK));
772 * Receive data on a hook ipx
775 ng_ppp_rcvdata_ipx(hook_p hook, item_p item)
777 const node_p node = NG_HOOK_NODE(hook);
778 const priv_p priv = NG_NODE_PRIVATE(node);
780 if (!priv->conf.enableIPX) {
784 return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPX));
788 * Receive data on a hook bypass
791 ng_ppp_rcvdata_bypass(hook_p hook, item_p item)
798 if (m->m_pkthdr.len < 4) {
802 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
806 linkNum = ntohs(mtod(m, uint16_t *)[0]);
807 proto = ntohs(mtod(m, uint16_t *)[1]);
811 if (linkNum == NG_PPP_BUNDLE_LINKNUM)
812 return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, proto));
814 return (ng_ppp_link_xmit(NG_HOOK_NODE(hook), item, proto,
819 ng_ppp_bypass(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
821 const priv_p priv = NG_NODE_PRIVATE(node);
826 if (priv->hooks[HOOK_INDEX_BYPASS] == NULL) {
831 /* Add 4-byte bypass header. */
832 hdr[0] = htons(linkNum);
833 hdr[1] = htons(proto);
836 if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) {
842 /* Send packet out hook. */
843 NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_BYPASS]);
848 ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
850 const priv_p priv = NG_NODE_PRIVATE(node);
851 hook_p outHook = NULL;
856 if (priv->conf.enableIP)
857 outHook = priv->hooks[HOOK_INDEX_INET];
860 if (priv->conf.enableIPv6)
861 outHook = priv->hooks[HOOK_INDEX_IPV6];
864 if (priv->conf.enableAtalk)
865 outHook = priv->hooks[HOOK_INDEX_ATALK];
868 if (priv->conf.enableIPX)
869 outHook = priv->hooks[HOOK_INDEX_IPX];
874 return (ng_ppp_bypass(node, item, proto, linkNum));
876 /* Send packet out hook. */
877 NG_FWD_ITEM_HOOK(error, item, outHook);
882 * Header compression layer
886 ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto)
888 const priv_p priv = NG_NODE_PRIVATE(node);
890 if (proto == PROT_IP &&
891 priv->conf.enableVJCompression &&
892 priv->vjCompHooked) {
895 /* Send packet out hook. */
896 NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_VJC_IP]);
900 return (ng_ppp_comp_xmit(node, item, proto));
904 * Receive data on a hook vjc_comp.
907 ng_ppp_rcvdata_vjc_comp(hook_p hook, item_p item)
909 const node_p node = NG_HOOK_NODE(hook);
910 const priv_p priv = NG_NODE_PRIVATE(node);
912 if (!priv->conf.enableVJCompression) {
916 return (ng_ppp_comp_xmit(node, item, PROT_VJCOMP));
920 * Receive data on a hook vjc_uncomp.
923 ng_ppp_rcvdata_vjc_uncomp(hook_p hook, item_p item)
925 const node_p node = NG_HOOK_NODE(hook);
926 const priv_p priv = NG_NODE_PRIVATE(node);
928 if (!priv->conf.enableVJCompression) {
932 return (ng_ppp_comp_xmit(node, item, PROT_VJUNCOMP));
936 * Receive data on a hook vjc_vjip.
939 ng_ppp_rcvdata_vjc_vjip(hook_p hook, item_p item)
941 const node_p node = NG_HOOK_NODE(hook);
942 const priv_p priv = NG_NODE_PRIVATE(node);
944 if (!priv->conf.enableVJCompression) {
948 return (ng_ppp_comp_xmit(node, item, PROT_IP));
952 ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
954 const priv_p priv = NG_NODE_PRIVATE(node);
956 if (priv->conf.enableVJDecompression && priv->vjCompHooked) {
957 hook_p outHook = NULL;
961 outHook = priv->hooks[HOOK_INDEX_VJC_COMP];
964 outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP];
971 /* Send packet out hook. */
972 NG_FWD_ITEM_HOOK(error, item, outHook);
977 return (ng_ppp_proto_recv(node, item, proto, linkNum));
981 * Receive data on a hook vjc_ip.
984 ng_ppp_rcvdata_vjc_ip(hook_p hook, item_p item)
986 const node_p node = NG_HOOK_NODE(hook);
987 const priv_p priv = NG_NODE_PRIVATE(node);
989 if (!priv->conf.enableVJCompression) {
993 return (ng_ppp_proto_recv(node, item, PROT_IP, NG_PPP_BUNDLE_LINKNUM));
1001 ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto)
1003 const priv_p priv = NG_NODE_PRIVATE(node);
1005 if (priv->conf.enableCompression &&
1007 proto != PROT_COMPD &&
1008 proto != PROT_CRYPTD &&
1009 priv->hooks[HOOK_INDEX_COMPRESS] != NULL) {
1014 if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) {
1020 /* Send packet out hook. */
1021 NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_COMPRESS]);
1025 return (ng_ppp_crypt_xmit(node, item, proto));
1029 * Receive data on a hook compress.
1032 ng_ppp_rcvdata_compress(hook_p hook, item_p item)
1034 const node_p node = NG_HOOK_NODE(hook);
1035 const priv_p priv = NG_NODE_PRIVATE(node);
1038 switch (priv->conf.enableCompression) {
1039 case NG_PPP_COMPRESS_NONE:
1042 case NG_PPP_COMPRESS_FULL:
1047 if ((m = ng_ppp_cutproto(m, &proto)) == NULL) {
1052 if (!PROT_VALID(proto)) {
1062 return (ng_ppp_crypt_xmit(node, item, proto));
1066 ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
1068 const priv_p priv = NG_NODE_PRIVATE(node);
1070 if (proto < 0x4000 &&
1071 ((proto == PROT_COMPD && priv->conf.enableDecompression) ||
1072 priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) &&
1073 priv->hooks[HOOK_INDEX_DECOMPRESS] != NULL) {
1076 if (priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) {
1079 if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) {
1086 /* Send packet out hook. */
1087 NG_FWD_ITEM_HOOK(error, item,
1088 priv->hooks[HOOK_INDEX_DECOMPRESS]);
1090 } else if (proto == PROT_COMPD) {
1091 /* Disabled protos MUST be silently discarded, but
1092 * unsupported MUST not. Let user-level decide this. */
1093 return (ng_ppp_bypass(node, item, proto, linkNum));
1096 return (ng_ppp_hcomp_recv(node, item, proto, linkNum));
1100 * Receive data on a hook decompress.
1103 ng_ppp_rcvdata_decompress(hook_p hook, item_p item)
1105 const node_p node = NG_HOOK_NODE(hook);
1106 const priv_p priv = NG_NODE_PRIVATE(node);
1110 if (!priv->conf.enableDecompression) {
1115 if ((m = ng_ppp_cutproto(m, &proto)) == NULL) {
1120 if (!PROT_VALID(proto)) {
1121 priv->bundleStats.badProtos++;
1125 return (ng_ppp_hcomp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM));
1133 ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto)
1135 const priv_p priv = NG_NODE_PRIVATE(node);
1137 if (priv->conf.enableEncryption &&
1139 proto != PROT_CRYPTD &&
1140 priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) {
1145 if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) {
1151 /* Send packet out hook. */
1152 NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_ENCRYPT]);
1156 return (ng_ppp_mp_xmit(node, item, proto));
1160 * Receive data on a hook encrypt.
1163 ng_ppp_rcvdata_encrypt(hook_p hook, item_p item)
1165 const node_p node = NG_HOOK_NODE(hook);
1166 const priv_p priv = NG_NODE_PRIVATE(node);
1168 if (!priv->conf.enableEncryption) {
1172 return (ng_ppp_mp_xmit(node, item, PROT_CRYPTD));
1176 ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
1178 const priv_p priv = NG_NODE_PRIVATE(node);
1181 priv->bundleStats.recvFrames++;
1182 priv->bundleStats.recvOctets += NGI_M(item)->m_pkthdr.len;
1184 if (proto == PROT_CRYPTD) {
1185 if (priv->conf.enableDecryption &&
1186 priv->hooks[HOOK_INDEX_DECRYPT] != NULL) {
1189 /* Send packet out hook. */
1190 NG_FWD_ITEM_HOOK(error, item,
1191 priv->hooks[HOOK_INDEX_DECRYPT]);
1194 /* Disabled protos MUST be silently discarded, but
1195 * unsupported MUST not. Let user-level decide this. */
1196 return (ng_ppp_bypass(node, item, proto, linkNum));
1200 return (ng_ppp_comp_recv(node, item, proto, linkNum));
1204 * Receive data on a hook decrypt.
1207 ng_ppp_rcvdata_decrypt(hook_p hook, item_p item)
1209 const node_p node = NG_HOOK_NODE(hook);
1210 const priv_p priv = NG_NODE_PRIVATE(node);
1214 if (!priv->conf.enableDecryption) {
1219 if ((m = ng_ppp_cutproto(m, &proto)) == NULL) {
1224 if (!PROT_VALID(proto)) {
1225 priv->bundleStats.badProtos++;
1229 return (ng_ppp_comp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM));
1237 ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
1239 const priv_p priv = NG_NODE_PRIVATE(node);
1240 struct ng_ppp_link *link;
1245 /* Check if link correct. */
1246 if (linkNum >= NG_PPP_MAX_LINKS) {
1251 /* Get link pointer (optimization). */
1252 link = &priv->links[linkNum];
1254 /* Check link status (if real). */
1255 if (link->hook == NULL) {
1263 /* Check peer's MRU for this link. */
1264 mru = link->conf.mru;
1265 if (mru != 0 && m->m_pkthdr.len > mru) {
1271 /* Prepend protocol number, possibly compressed. */
1272 if ((m = ng_ppp_addproto(m, proto, link->conf.enableProtoComp)) ==
1278 /* Prepend address and control field (unless compressed). */
1279 if (proto == PROT_LCP || !link->conf.enableACFComp) {
1280 if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) {
1286 /* Deliver frame. */
1287 len = m->m_pkthdr.len;
1288 NG_FWD_NEW_DATA(error, item, link->hook, m);
1290 /* Update stats and 'bytes in queue' counter. */
1292 link->stats.xmitFrames++;
1293 link->stats.xmitOctets += len;
1295 /* bytesInQueue and lastWrite required only for mp_strategy. */
1296 if (priv->conf.enableMultilink && !priv->allLinksEqual &&
1297 !priv->conf.enableRoundRobin) {
1298 /* If queue was empty, then mark this time. */
1299 if (link->bytesInQueue == 0)
1300 getmicrouptime(&link->lastWrite);
1301 link->bytesInQueue += len + MP_AVERAGE_LINK_OVERHEAD;
1302 /* Limit max queue length to 50 pkts. BW can be defined
1303 incorrectly and link may not signal overload. */
1304 if (link->bytesInQueue > 50 * 1600)
1305 link->bytesInQueue = 50 * 1600;
1312 * Receive data on a hook linkX.
1315 ng_ppp_rcvdata(hook_p hook, item_p item)
1317 const node_p node = NG_HOOK_NODE(hook);
1318 const priv_p priv = NG_NODE_PRIVATE(node);
1319 const int index = (intptr_t)NG_HOOK_PRIVATE(hook);
1320 const uint16_t linkNum = (uint16_t)~index;
1321 struct ng_ppp_link * const link = &priv->links[linkNum];
1325 KASSERT(linkNum < NG_PPP_MAX_LINKS,
1326 ("%s: bogus index 0x%x", __func__, index));
1331 link->stats.recvFrames++;
1332 link->stats.recvOctets += m->m_pkthdr.len;
1334 /* Strip address and control fields, if present. */
1335 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1339 if (bcmp(mtod(m, uint8_t *), &ng_ppp_acf, 2) == 0)
1342 if ((m = ng_ppp_cutproto(m, &proto)) == NULL) {
1346 NGI_M(item) = m; /* Put changed m back into item. */
1348 if (!PROT_VALID(proto)) {
1349 link->stats.badProtos++;
1354 /* LCP packets must go directly to bypass. */
1355 if (proto >= 0xB000)
1356 return (ng_ppp_bypass(node, item, proto, linkNum));
1358 if (!link->conf.enableLink) {
1359 /* Non-LCP packets are denied on a disabled link. */
1364 return (ng_ppp_mp_recv(node, item, proto, linkNum));
1372 * Handle an incoming multi-link fragment
1374 * The fragment reassembly algorithm is somewhat complex. This is mainly
1375 * because we are required not to reorder the reconstructed packets, yet
1376 * fragments are only guaranteed to arrive in order on a per-link basis.
1377 * In other words, when we have a complete packet ready, but the previous
1378 * packet is still incomplete, we have to decide between delivering the
1379 * complete packet and throwing away the incomplete one, or waiting to
1380 * see if the remainder of the incomplete one arrives, at which time we
1381 * can deliver both packets, in order.
1383 * This problem is exacerbated by "sequence number slew", which is when
1384 * the sequence numbers coming in from different links are far apart from
1385 * each other. In particular, certain unnamed equipment (*cough* Ascend)
1386 * has been seen to generate sequence number slew of up to 10 on an ISDN
1387 * 2B-channel MP link. There is nothing invalid about sequence number slew
1388 * but it makes the reasssembly process have to work harder.
1390 * However, the peer is required to transmit fragments in order on each
1391 * link. That means if we define MSEQ as the minimum over all links of
1392 * the highest sequence number received on that link, then we can always
1393 * give up any hope of receiving a fragment with sequence number < MSEQ in
1394 * the future (all of this using 'wraparound' sequence number space).
1395 * Therefore we can always immediately throw away incomplete packets
1396 * missing fragments with sequence numbers < MSEQ.
1398 * Here is an overview of our algorithm:
1400 * o Received fragments are inserted into a queue, for which we
1401 * maintain these invariants between calls to this function:
1403 * - Fragments are ordered in the queue by sequence number
1404 * - If a complete packet is at the head of the queue, then
1405 * the first fragment in the packet has seq# > MSEQ + 1
1406 * (otherwise, we could deliver it immediately)
1407 * - If any fragments have seq# < MSEQ, then they are necessarily
1408 * part of a packet whose missing seq#'s are all > MSEQ (otherwise,
1409 * we can throw them away because they'll never be completed)
1410 * - The queue contains at most MP_MAX_QUEUE_LEN fragments
1412 * o We have a periodic timer that checks the queue for the first
1413 * complete packet that has been sitting in the queue "too long".
1414 * When one is detected, all previous (incomplete) fragments are
1415 * discarded, their missing fragments are declared lost and MSEQ
1418 * o If we recieve a fragment with seq# < MSEQ, we throw it away
1419 * because we've already delcared it lost.
1421 * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM.
1424 ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
1426 const priv_p priv = NG_NODE_PRIVATE(node);
1427 struct ng_ppp_link *const link = &priv->links[linkNum];
1428 struct ng_ppp_frag frag0, *frag = &frag0;
1429 struct ng_ppp_frag *qent;
1430 int i, diff, inserted;
1433 if ((!priv->conf.enableMultilink) || proto != PROT_MP)
1434 return (ng_ppp_crypt_recv(node, item, proto, linkNum));
1439 /* Extract fragment information from MP header */
1440 if (priv->conf.recvShortSeq) {
1443 if (m->m_pkthdr.len < 2) {
1444 link->stats.runts++;
1448 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL)
1451 shdr = ntohs(*mtod(m, uint16_t *));
1452 frag->seq = MP_SHORT_EXTEND(shdr);
1453 frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0;
1454 frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0;
1455 diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq);
1460 if (m->m_pkthdr.len < 4) {
1461 link->stats.runts++;
1465 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
1468 lhdr = ntohl(*mtod(m, uint32_t *));
1469 frag->seq = MP_LONG_EXTEND(lhdr);
1470 frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0;
1471 frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0;
1472 diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq);
1476 getmicrouptime(&frag->timestamp);
1478 /* If sequence number is < MSEQ, we've already declared this
1479 fragment as lost, so we have no choice now but to drop it */
1481 link->stats.dropFragments++;
1486 /* Update highest received sequence number on this link and MSEQ */
1487 priv->mseq = link->seq = frag->seq;
1488 for (i = 0; i < priv->numActiveLinks; i++) {
1489 struct ng_ppp_link *const alink =
1490 &priv->links[priv->activeLinks[i]];
1492 if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0)
1493 priv->mseq = alink->seq;
1496 /* Allocate a new frag struct for the queue */
1497 MALLOC(frag, struct ng_ppp_frag *, sizeof(*frag), M_NETGRAPH_PPP, M_NOWAIT);
1500 ng_ppp_frag_process(node);
1505 /* Add fragment to queue, which is sorted by sequence number */
1507 TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) {
1508 diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq);
1510 TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent);
1513 } else if (diff == 0) { /* should never happen! */
1514 link->stats.dupFragments++;
1515 NG_FREE_M(frag->data);
1516 FREE(frag, M_NETGRAPH_PPP);
1521 TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent);
1524 /* Process the queue */
1525 return ng_ppp_frag_process(node);
1528 /************************************************************************
1530 ************************************************************************/
1533 * If new mseq > current then set it and update all active links
1536 ng_ppp_bump_mseq(node_p node, int32_t new_mseq)
1538 const priv_p priv = NG_NODE_PRIVATE(node);
1541 if (MP_RECV_SEQ_DIFF(priv, priv->mseq, new_mseq) < 0) {
1542 priv->mseq = new_mseq;
1543 for (i = 0; i < priv->numActiveLinks; i++) {
1544 struct ng_ppp_link *const alink =
1545 &priv->links[priv->activeLinks[i]];
1547 if (MP_RECV_SEQ_DIFF(priv,
1548 alink->seq, new_mseq) < 0)
1549 alink->seq = new_mseq;
1555 * Examine our list of fragments, and determine if there is a
1556 * complete and deliverable packet at the head of the list.
1557 * Return 1 if so, zero otherwise.
1560 ng_ppp_check_packet(node_p node)
1562 const priv_p priv = NG_NODE_PRIVATE(node);
1563 struct ng_ppp_frag *qent, *qnext;
1565 /* Check for empty queue */
1566 if (TAILQ_EMPTY(&priv->frags))
1569 /* Check first fragment is the start of a deliverable packet */
1570 qent = TAILQ_FIRST(&priv->frags);
1571 if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1)
1574 /* Check that all the fragments are there */
1575 while (!qent->last) {
1576 qnext = TAILQ_NEXT(qent, f_qent);
1577 if (qnext == NULL) /* end of queue */
1579 if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq))
1589 * Pull a completed packet off the head of the incoming fragment queue.
1590 * This assumes there is a completed packet there to pull off.
1593 ng_ppp_get_packet(node_p node, struct mbuf **mp)
1595 const priv_p priv = NG_NODE_PRIVATE(node);
1596 struct ng_ppp_frag *qent, *qnext;
1597 struct mbuf *m = NULL, *tail;
1599 qent = TAILQ_FIRST(&priv->frags);
1600 KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first,
1601 ("%s: no packet", __func__));
1602 for (tail = NULL; qent != NULL; qent = qnext) {
1603 qnext = TAILQ_NEXT(qent, f_qent);
1604 KASSERT(!TAILQ_EMPTY(&priv->frags),
1605 ("%s: empty q", __func__));
1606 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1608 tail = m = qent->data;
1610 m->m_pkthdr.len += qent->data->m_pkthdr.len;
1611 tail->m_next = qent->data;
1613 while (tail->m_next != NULL)
1614 tail = tail->m_next;
1617 /* Bump MSEQ if necessary */
1618 ng_ppp_bump_mseq(node, qent->seq);
1620 FREE(qent, M_NETGRAPH_PPP);
1627 * Trim fragments from the queue whose packets can never be completed.
1628 * This assumes a complete packet is NOT at the beginning of the queue.
1629 * Returns 1 if fragments were removed, zero otherwise.
1632 ng_ppp_frag_trim(node_p node)
1634 const priv_p priv = NG_NODE_PRIVATE(node);
1635 struct ng_ppp_frag *qent, *qnext = NULL;
1638 /* Scan for "dead" fragments and remove them */
1642 /* If queue is empty, we're done */
1643 if (TAILQ_EMPTY(&priv->frags))
1646 /* Determine whether first fragment can ever be completed */
1647 TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1648 if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0)
1650 qnext = TAILQ_NEXT(qent, f_qent);
1651 KASSERT(qnext != NULL,
1652 ("%s: last frag < MSEQ?", __func__));
1653 if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)
1654 || qent->last || qnext->first) {
1662 /* Remove fragment and all others in the same packet */
1663 while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) {
1664 KASSERT(!TAILQ_EMPTY(&priv->frags),
1665 ("%s: empty q", __func__));
1666 priv->bundleStats.dropFragments++;
1667 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1668 NG_FREE_M(qent->data);
1669 FREE(qent, M_NETGRAPH_PPP);
1678 * Drop fragments on queue overflow.
1679 * Returns 1 if fragments were removed, zero otherwise.
1682 ng_ppp_frag_drop(node_p node)
1684 const priv_p priv = NG_NODE_PRIVATE(node);
1686 /* Check queue length */
1687 if (priv->qlen > MP_MAX_QUEUE_LEN) {
1688 struct ng_ppp_frag *qent;
1690 /* Get oldest fragment */
1691 KASSERT(!TAILQ_EMPTY(&priv->frags),
1692 ("%s: empty q", __func__));
1693 qent = TAILQ_FIRST(&priv->frags);
1695 /* Bump MSEQ if necessary */
1696 ng_ppp_bump_mseq(node, qent->seq);
1699 priv->bundleStats.dropFragments++;
1700 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1701 NG_FREE_M(qent->data);
1702 FREE(qent, M_NETGRAPH_PPP);
1711 * Run the queue, restoring the queue invariants
1714 ng_ppp_frag_process(node_p node)
1716 const priv_p priv = NG_NODE_PRIVATE(node);
1722 /* Deliver any deliverable packets */
1723 while (ng_ppp_check_packet(node)) {
1724 ng_ppp_get_packet(node, &m);
1725 if ((m = ng_ppp_cutproto(m, &proto)) == NULL)
1727 if (!PROT_VALID(proto)) {
1728 priv->bundleStats.badProtos++;
1732 if ((item = ng_package_data(m, NG_NOFLAGS)) != NULL)
1733 ng_ppp_crypt_recv(node, item, proto,
1734 NG_PPP_BUNDLE_LINKNUM);
1736 /* Delete dead fragments and try again */
1737 } while (ng_ppp_frag_trim(node) || ng_ppp_frag_drop(node));
1744 * Check for 'stale' completed packets that need to be delivered
1746 * If a link goes down or has a temporary failure, MSEQ can get
1747 * "stuck", because no new incoming fragments appear on that link.
1748 * This can cause completed packets to never get delivered if
1749 * their sequence numbers are all > MSEQ + 1.
1751 * This routine checks how long all of the completed packets have
1752 * been sitting in the queue, and if too long, removes fragments
1753 * from the queue and increments MSEQ to allow them to be delivered.
1756 ng_ppp_frag_checkstale(node_p node)
1758 const priv_p priv = NG_NODE_PRIVATE(node);
1759 struct ng_ppp_frag *qent, *beg, *end;
1760 struct timeval now, age;
1767 now.tv_sec = 0; /* uninitialized state */
1770 /* If queue is empty, we're done */
1771 if (TAILQ_EMPTY(&priv->frags))
1774 /* Find the first complete packet in the queue */
1776 seq = TAILQ_FIRST(&priv->frags)->seq;
1777 TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1780 else if (qent->seq != seq)
1782 if (beg != NULL && qent->last) {
1786 seq = MP_NEXT_RECV_SEQ(priv, seq);
1789 /* If none found, exit */
1793 /* Get current time (we assume we've been up for >= 1 second) */
1794 if (now.tv_sec == 0)
1795 getmicrouptime(&now);
1797 /* Check if packet has been queued too long */
1799 timevalsub(&age, &beg->timestamp);
1800 if (timevalcmp(&age, &ng_ppp_max_staleness, < ))
1803 /* Throw away junk fragments in front of the completed packet */
1804 while ((qent = TAILQ_FIRST(&priv->frags)) != beg) {
1805 KASSERT(!TAILQ_EMPTY(&priv->frags),
1806 ("%s: empty q", __func__));
1807 priv->bundleStats.dropFragments++;
1808 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1809 NG_FREE_M(qent->data);
1810 FREE(qent, M_NETGRAPH_PPP);
1814 /* Extract completed packet */
1816 ng_ppp_get_packet(node, &m);
1818 if ((m = ng_ppp_cutproto(m, &proto)) == NULL)
1820 if (!PROT_VALID(proto)) {
1821 priv->bundleStats.badProtos++;
1826 /* Deliver packet */
1827 if ((item = ng_package_data(m, NG_NOFLAGS)) != NULL)
1828 ng_ppp_crypt_recv(node, item, proto,
1829 NG_PPP_BUNDLE_LINKNUM);
1834 * Periodically call ng_ppp_frag_checkstale()
1837 ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1839 /* XXX: is this needed? */
1840 if (NG_NODE_NOT_VALID(node))
1843 /* Scan the fragment queue */
1844 ng_ppp_frag_checkstale(node);
1846 /* Start timer again */
1847 ng_ppp_start_frag_timer(node);
1851 * Deliver a frame out on the bundle, i.e., figure out how to fragment
1852 * the frame across the individual PPP links and do so.
1855 ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto)
1857 const priv_p priv = NG_NODE_PRIVATE(node);
1858 const int hdr_len = priv->conf.xmitShortSeq ? 2 : 4;
1859 int distrib[NG_PPP_MAX_LINKS];
1864 /* At least one link must be active */
1865 if (priv->numActiveLinks == 0) {
1871 priv->bundleStats.xmitFrames++;
1872 priv->bundleStats.xmitOctets += NGI_M(item)->m_pkthdr.len;
1874 if (!priv->conf.enableMultilink)
1875 return (ng_ppp_link_xmit(node, item, proto,
1876 priv->activeLinks[0]));
1882 /* Prepend protocol number, possibly compressed. */
1883 if ((m = ng_ppp_addproto(m, proto, 1)) == NULL)
1886 /* Clear distribution plan */
1887 bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0]));
1889 /* Round-robin strategy */
1890 if (priv->conf.enableRoundRobin) {
1891 activeLinkNum = priv->lastLink++ % priv->numActiveLinks;
1892 distrib[activeLinkNum] = m->m_pkthdr.len;
1896 /* Strategy when all links are equivalent (optimize the common case) */
1897 if (priv->allLinksEqual) {
1898 int numFrags, fraction, remain;
1901 /* Calculate optimal fragment count */
1902 numFrags = priv->numActiveLinks;
1903 if (numFrags > m->m_pkthdr.len / MP_MIN_FRAG_LEN)
1904 numFrags = m->m_pkthdr.len / MP_MIN_FRAG_LEN;
1908 fraction = m->m_pkthdr.len / numFrags;
1909 remain = m->m_pkthdr.len - (fraction * numFrags);
1911 /* Assign distribution */
1912 for (i = 0; i < numFrags; i++) {
1913 distrib[priv->lastLink++ % priv->numActiveLinks]
1914 = fraction + (((remain--) > 0)?1:0);
1919 /* Strategy when all links are not equivalent */
1920 ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib);
1923 /* Send alloted portions of frame out on the link(s) */
1924 for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1;
1925 activeLinkNum >= 0; activeLinkNum--) {
1926 const uint16_t linkNum = priv->activeLinks[activeLinkNum];
1927 struct ng_ppp_link *const link = &priv->links[linkNum];
1929 /* Deliver fragment(s) out the next link */
1930 for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) {
1931 int len, lastFragment, error;
1934 /* Calculate fragment length; don't exceed link MTU */
1935 len = distrib[activeLinkNum];
1936 if (len > link->conf.mru - hdr_len)
1937 len = link->conf.mru - hdr_len;
1938 distrib[activeLinkNum] -= len;
1939 lastFragment = (len == m->m_pkthdr.len);
1941 /* Split off next fragment as "m2" */
1943 if (!lastFragment) {
1944 struct mbuf *n = m_split(m, len, M_DONTWAIT);
1950 m_tag_copy_chain(n, m, M_DONTWAIT);
1954 /* Prepend MP header */
1955 if (priv->conf.xmitShortSeq) {
1960 (priv->xseq + 1) & MP_SHORT_SEQ_MASK;
1962 shdr |= MP_SHORT_FIRST_FLAG;
1964 shdr |= MP_SHORT_LAST_FLAG;
1966 m2 = ng_ppp_prepend(m2, &shdr, 2);
1972 (priv->xseq + 1) & MP_LONG_SEQ_MASK;
1974 lhdr |= MP_LONG_FIRST_FLAG;
1976 lhdr |= MP_LONG_LAST_FLAG;
1978 m2 = ng_ppp_prepend(m2, &lhdr, 4);
1987 if ((item = ng_package_data(m2, NG_NOFLAGS)) != NULL) {
1988 error = ng_ppp_link_xmit(node, item, PROT_MP,
2004 * Computing the optimal fragmentation
2005 * -----------------------------------
2007 * This routine tries to compute the optimal fragmentation pattern based
2008 * on each link's latency, bandwidth, and calculated additional latency.
2009 * The latter quantity is the additional latency caused by previously
2010 * written data that has not been transmitted yet.
2012 * This algorithm is only useful when not all of the links have the
2013 * same latency and bandwidth values.
2015 * The essential idea is to make the last bit of each fragment of the
2016 * frame arrive at the opposite end at the exact same time. This greedy
2017 * algorithm is optimal, in that no other scheduling could result in any
2018 * packet arriving any sooner unless packets are delivered out of order.
2020 * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and
2021 * latency l_i (in miliseconds). Consider the function function f_i(t)
2022 * which is equal to the number of bytes that will have arrived at
2023 * the peer after t miliseconds if we start writing continuously at
2024 * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i).
2025 * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i).
2026 * Note that the y-intersect is always <= zero because latency can't be
2027 * negative. Note also that really the function is f_i(t) except when
2028 * f_i(t) is negative, in which case the function is zero. To take
2029 * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }.
2030 * So the actual number of bytes that will have arrived at the peer after
2031 * t miliseconds is f_i(t) * Q_i(t).
2033 * At any given time, each link has some additional latency a_i >= 0
2034 * due to previously written fragment(s) which are still in the queue.
2035 * This value is easily computed from the time since last transmission,
2036 * the previous latency value, the number of bytes written, and the
2039 * Assume that l_i includes any a_i already, and that the links are
2040 * sorted by latency, so that l_i <= l_{i+1}.
2042 * Let N be the total number of bytes in the current frame we are sending.
2044 * Suppose we were to start writing bytes at time t = 0 on all links
2045 * simultaneously, which is the most we can possibly do. Then let
2046 * F(t) be equal to the total number of bytes received by the peer
2047 * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)).
2049 * Our goal is simply this: fragment the frame across the links such
2050 * that the peer is able to reconstruct the completed frame as soon as
2051 * possible, i.e., at the least possible value of t. Call this value t_0.
2053 * Then it follows that F(t_0) = N. Our strategy is first to find the value
2054 * of t_0, and then deduce how many bytes to write to each link.
2058 * t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) )
2060 * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will
2061 * lie in one of these ranges. To find it, we just need to find the i such
2062 * that F(l_i) <= N <= F(l_{i+1}). Then we compute all the constant values
2063 * for Q_i() in this range, plug in the remaining values, solving for t_0.
2065 * Once t_0 is known, then the number of bytes to send on link i is
2066 * just f_i(t_0) * Q_i(t_0).
2068 * In other words, we start allocating bytes to the links one at a time.
2069 * We keep adding links until the frame is completely sent. Some links
2070 * may not get any bytes because their latency is too high.
2072 * Is all this work really worth the trouble? Depends on the situation.
2073 * The bigger the ratio of computer speed to link speed, and the more
2074 * important total bundle latency is (e.g., for interactive response time),
2075 * the more it's worth it. There is however the cost of calling this
2076 * function for every frame. The running time is O(n^2) where n is the
2077 * number of links that receive a non-zero number of bytes.
2079 * Since latency is measured in miliseconds, the "resolution" of this
2080 * algorithm is one milisecond.
2082 * To avoid this algorithm altogether, configure all links to have the
2083 * same latency and bandwidth.
2086 ng_ppp_mp_strategy(node_p node, int len, int *distrib)
2088 const priv_p priv = NG_NODE_PRIVATE(node);
2089 int latency[NG_PPP_MAX_LINKS];
2090 int sortByLatency[NG_PPP_MAX_LINKS];
2092 int t0, total, topSum, botSum;
2094 int i, numFragments;
2096 /* If only one link, this gets real easy */
2097 if (priv->numActiveLinks == 1) {
2102 /* Get current time */
2103 getmicrouptime(&now);
2105 /* Compute latencies for each link at this point in time */
2106 for (activeLinkNum = 0;
2107 activeLinkNum < priv->numActiveLinks; activeLinkNum++) {
2108 struct ng_ppp_link *alink;
2109 struct timeval diff;
2112 /* Start with base latency value */
2113 alink = &priv->links[priv->activeLinks[activeLinkNum]];
2114 latency[activeLinkNum] = alink->latency;
2115 sortByLatency[activeLinkNum] = activeLinkNum; /* see below */
2117 /* Any additional latency? */
2118 if (alink->bytesInQueue == 0)
2121 /* Compute time delta since last write */
2123 timevalsub(&diff, &alink->lastWrite);
2125 /* alink->bytesInQueue will be changed, mark change time. */
2126 alink->lastWrite = now;
2128 if (now.tv_sec < 0 || diff.tv_sec >= 10) { /* sanity */
2129 alink->bytesInQueue = 0;
2133 /* How many bytes could have transmitted since last write? */
2134 xmitBytes = (alink->conf.bandwidth * 10 * diff.tv_sec)
2135 + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100;
2136 alink->bytesInQueue -= xmitBytes;
2137 if (alink->bytesInQueue < 0)
2138 alink->bytesInQueue = 0;
2140 latency[activeLinkNum] +=
2141 (100 * alink->bytesInQueue) / alink->conf.bandwidth;
2144 /* Sort active links by latency */
2145 qsort_r(sortByLatency,
2146 priv->numActiveLinks, sizeof(*sortByLatency), latency, ng_ppp_intcmp);
2148 /* Find the interval we need (add links in sortByLatency[] order) */
2149 for (numFragments = 1;
2150 numFragments < priv->numActiveLinks; numFragments++) {
2151 for (total = i = 0; i < numFragments; i++) {
2154 flowTime = latency[sortByLatency[numFragments]]
2155 - latency[sortByLatency[i]];
2156 total += ((flowTime * priv->links[
2157 priv->activeLinks[sortByLatency[i]]].conf.bandwidth)
2164 /* Solve for t_0 in that interval */
2165 for (topSum = botSum = i = 0; i < numFragments; i++) {
2166 int bw = priv->links[
2167 priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
2169 topSum += latency[sortByLatency[i]] * bw; /* / 100 */
2170 botSum += bw; /* / 100 */
2172 t0 = ((len * 100) + topSum + botSum / 2) / botSum;
2174 /* Compute f_i(t_0) all i */
2175 for (total = i = 0; i < numFragments; i++) {
2176 int bw = priv->links[
2177 priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
2179 distrib[sortByLatency[i]] =
2180 (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100;
2181 total += distrib[sortByLatency[i]];
2184 /* Deal with any rounding error */
2186 struct ng_ppp_link *fastLink =
2187 &priv->links[priv->activeLinks[sortByLatency[0]]];
2190 /* Find the fastest link */
2191 for (i = 1; i < numFragments; i++) {
2192 struct ng_ppp_link *const link =
2193 &priv->links[priv->activeLinks[sortByLatency[i]]];
2195 if (link->conf.bandwidth > fastLink->conf.bandwidth) {
2200 distrib[sortByLatency[fast]] += len - total;
2201 } else while (total > len) {
2202 struct ng_ppp_link *slowLink =
2203 &priv->links[priv->activeLinks[sortByLatency[0]]];
2204 int delta, slow = 0;
2206 /* Find the slowest link that still has bytes to remove */
2207 for (i = 1; i < numFragments; i++) {
2208 struct ng_ppp_link *const link =
2209 &priv->links[priv->activeLinks[sortByLatency[i]]];
2211 if (distrib[sortByLatency[slow]] == 0
2212 || (distrib[sortByLatency[i]] > 0
2213 && link->conf.bandwidth <
2214 slowLink->conf.bandwidth)) {
2219 delta = total - len;
2220 if (delta > distrib[sortByLatency[slow]])
2221 delta = distrib[sortByLatency[slow]];
2222 distrib[sortByLatency[slow]] -= delta;
2228 * Compare two integers
2231 ng_ppp_intcmp(void *latency, const void *v1, const void *v2)
2233 const int index1 = *((const int *) v1);
2234 const int index2 = *((const int *) v2);
2236 return ((int *)latency)[index1] - ((int *)latency)[index2];
2240 * Prepend a possibly compressed PPP protocol number in front of a frame
2242 static struct mbuf *
2243 ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK)
2245 if (compOK && PROT_COMPRESSABLE(proto)) {
2246 uint8_t pbyte = (uint8_t)proto;
2248 return ng_ppp_prepend(m, &pbyte, 1);
2250 uint16_t pword = htons((uint16_t)proto);
2252 return ng_ppp_prepend(m, &pword, 2);
2257 * Cut a possibly compressed PPP protocol number from the front of a frame.
2259 static struct mbuf *
2260 ng_ppp_cutproto(struct mbuf *m, uint16_t *proto)
2264 if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL)
2267 *proto = *mtod(m, uint8_t *);
2270 if (!PROT_VALID(*proto)) {
2271 if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL)
2274 *proto = (*proto << 8) + *mtod(m, uint8_t *);
2282 * Prepend some bytes to an mbuf.
2284 static struct mbuf *
2285 ng_ppp_prepend(struct mbuf *m, const void *buf, int len)
2287 M_PREPEND(m, len, M_DONTWAIT);
2288 if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL))
2290 bcopy(buf, mtod(m, uint8_t *), len);
2295 * Update private information that is derived from other private information
2298 ng_ppp_update(node_p node, int newConf)
2300 const priv_p priv = NG_NODE_PRIVATE(node);
2303 /* Update active status for VJ Compression */
2304 priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL
2305 && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL
2306 && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL
2307 && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL;
2309 /* Increase latency for each link an amount equal to one MP header */
2311 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
2314 if (priv->links[i].conf.bandwidth == 0)
2317 hdrBytes = MP_AVERAGE_LINK_OVERHEAD
2318 + (priv->links[i].conf.enableACFComp ? 0 : 2)
2319 + (priv->links[i].conf.enableProtoComp ? 1 : 2)
2320 + (priv->conf.xmitShortSeq ? 2 : 4);
2321 priv->links[i].latency =
2322 priv->links[i].conf.latency +
2323 (hdrBytes / priv->links[i].conf.bandwidth + 50) / 100;
2327 /* Update list of active links */
2328 bzero(&priv->activeLinks, sizeof(priv->activeLinks));
2329 priv->numActiveLinks = 0;
2330 priv->allLinksEqual = 1;
2331 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
2332 struct ng_ppp_link *const link = &priv->links[i];
2334 /* Is link active? */
2335 if (link->conf.enableLink && link->hook != NULL) {
2336 struct ng_ppp_link *link0;
2338 /* Add link to list of active links */
2339 priv->activeLinks[priv->numActiveLinks++] = i;
2340 link0 = &priv->links[priv->activeLinks[0]];
2342 /* Determine if all links are still equal */
2343 if (link->latency != link0->latency
2344 || link->conf.bandwidth != link0->conf.bandwidth)
2345 priv->allLinksEqual = 0;
2347 /* Initialize rec'd sequence number */
2348 if (link->seq == MP_NOSEQ) {
2349 link->seq = (link == link0) ?
2350 MP_INITIAL_SEQ : link0->seq;
2353 link->seq = MP_NOSEQ;
2356 /* Update MP state as multi-link is active or not */
2357 if (priv->conf.enableMultilink && priv->numActiveLinks > 0)
2358 ng_ppp_start_frag_timer(node);
2360 ng_ppp_stop_frag_timer(node);
2361 ng_ppp_frag_reset(node);
2362 priv->xseq = MP_INITIAL_SEQ;
2363 priv->mseq = MP_INITIAL_SEQ;
2364 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
2365 struct ng_ppp_link *const link = &priv->links[i];
2367 bzero(&link->lastWrite, sizeof(link->lastWrite));
2368 link->bytesInQueue = 0;
2369 link->seq = MP_NOSEQ;
2375 * Determine if a new configuration would represent a valid change
2376 * from the current configuration and link activity status.
2379 ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf)
2381 const priv_p priv = NG_NODE_PRIVATE(node);
2382 int i, newNumLinksActive;
2384 /* Check per-link config and count how many links would be active */
2385 for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) {
2386 if (newConf->links[i].enableLink && priv->links[i].hook != NULL)
2387 newNumLinksActive++;
2388 if (!newConf->links[i].enableLink)
2390 if (newConf->links[i].mru < MP_MIN_LINK_MRU)
2392 if (newConf->links[i].bandwidth == 0)
2394 if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH)
2396 if (newConf->links[i].latency > NG_PPP_MAX_LATENCY)
2400 /* Check bundle parameters */
2401 if (newConf->bund.enableMultilink && newConf->bund.mrru < MP_MIN_MRRU)
2404 /* Disallow changes to multi-link configuration while MP is active */
2405 if (priv->numActiveLinks > 0 && newNumLinksActive > 0) {
2406 if (!priv->conf.enableMultilink
2407 != !newConf->bund.enableMultilink
2408 || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq
2409 || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq)
2413 /* At most one link can be active unless multi-link is enabled */
2414 if (!newConf->bund.enableMultilink && newNumLinksActive > 1)
2417 /* Configuration change would be valid */
2422 * Free all entries in the fragment queue
2425 ng_ppp_frag_reset(node_p node)
2427 const priv_p priv = NG_NODE_PRIVATE(node);
2428 struct ng_ppp_frag *qent, *qnext;
2430 for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) {
2431 qnext = TAILQ_NEXT(qent, f_qent);
2432 NG_FREE_M(qent->data);
2433 FREE(qent, M_NETGRAPH_PPP);
2435 TAILQ_INIT(&priv->frags);
2440 * Start fragment queue timer
2443 ng_ppp_start_frag_timer(node_p node)
2445 const priv_p priv = NG_NODE_PRIVATE(node);
2447 if (!(callout_pending(&priv->fragTimer)))
2448 ng_callout(&priv->fragTimer, node, NULL, MP_FRAGTIMER_INTERVAL,
2449 ng_ppp_frag_timeout, NULL, 0);
2453 * Stop fragment queue timer
2456 ng_ppp_stop_frag_timer(node_p node)
2458 const priv_p priv = NG_NODE_PRIVATE(node);
2460 if (callout_pending(&priv->fragTimer))
2461 ng_uncallout(&priv->fragTimer, node);