5 * Copyright (c) 1996-2000 Whistle Communications, Inc.
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.
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
37 * Author: Archie Cobbs <archie@freebsd.org>
40 * $Whistle: ng_ppp.c,v 1.24 1999/11/01 09:24:52 julian Exp $
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/errno.h>
54 #include <sys/ctype.h>
56 #include <machine/limits.h>
58 #include <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/ng_parse.h>
61 #include <netgraph/ng_ppp.h>
62 #include <netgraph/ng_vjc.h>
64 #ifdef NG_SEPARATE_MALLOC
65 MALLOC_DEFINE(M_NETGRAPH_PPP, "netgraph_ppp", "netgraph ppp node");
67 #define M_NETGRAPH_PPP M_NETGRAPH
70 #define PROT_VALID(p) (((p) & 0x0101) == 0x0001)
71 #define PROT_COMPRESSABLE(p) (((p) & 0xff00) == 0x0000)
73 /* Some PPP protocol numbers we're interested in */
74 #define PROT_APPLETALK 0x0029
75 #define PROT_COMPD 0x00fd
76 #define PROT_CRYPTD 0x0053
77 #define PROT_IP 0x0021
78 #define PROT_IPV6 0x0057
79 #define PROT_IPX 0x002b
80 #define PROT_LCP 0xc021
81 #define PROT_MP 0x003d
82 #define PROT_VJCOMP 0x002d
83 #define PROT_VJUNCOMP 0x002f
85 /* Multilink PPP definitions */
86 #define MP_MIN_MRRU 1500 /* per RFC 1990 */
87 #define MP_INITIAL_SEQ 0 /* per RFC 1990 */
88 #define MP_MIN_LINK_MRU 32
90 #define MP_SHORT_SEQ_MASK 0x00000fff /* short seq # mask */
91 #define MP_SHORT_SEQ_HIBIT 0x00000800 /* short seq # high bit */
92 #define MP_SHORT_FIRST_FLAG 0x00008000 /* first fragment in frame */
93 #define MP_SHORT_LAST_FLAG 0x00004000 /* last fragment in frame */
95 #define MP_LONG_SEQ_MASK 0x00ffffff /* long seq # mask */
96 #define MP_LONG_SEQ_HIBIT 0x00800000 /* long seq # high bit */
97 #define MP_LONG_FIRST_FLAG 0x80000000 /* first fragment in frame */
98 #define MP_LONG_LAST_FLAG 0x40000000 /* last fragment in frame */
100 #define MP_NOSEQ 0x7fffffff /* impossible sequence number */
102 /* Sign extension of MP sequence numbers */
103 #define MP_SHORT_EXTEND(s) (((s) & MP_SHORT_SEQ_HIBIT) ? \
104 ((s) | ~MP_SHORT_SEQ_MASK) \
105 : ((s) & MP_SHORT_SEQ_MASK))
106 #define MP_LONG_EXTEND(s) (((s) & MP_LONG_SEQ_HIBIT) ? \
107 ((s) | ~MP_LONG_SEQ_MASK) \
108 : ((s) & MP_LONG_SEQ_MASK))
110 /* Comparision of MP sequence numbers. Note: all sequence numbers
111 except priv->xseq are stored with the sign bit extended. */
112 #define MP_SHORT_SEQ_DIFF(x,y) MP_SHORT_EXTEND((x) - (y))
113 #define MP_LONG_SEQ_DIFF(x,y) MP_LONG_EXTEND((x) - (y))
115 #define MP_RECV_SEQ_DIFF(priv,x,y) \
116 ((priv)->conf.recvShortSeq ? \
117 MP_SHORT_SEQ_DIFF((x), (y)) : \
118 MP_LONG_SEQ_DIFF((x), (y)))
120 /* Increment receive sequence number */
121 #define MP_NEXT_RECV_SEQ(priv,seq) \
122 (((seq) + 1) & ((priv)->conf.recvShortSeq ? \
123 MP_SHORT_SEQ_MASK : MP_LONG_SEQ_MASK))
125 /* Don't fragment transmitted packets smaller than this */
126 #define MP_MIN_FRAG_LEN 6
128 /* Maximum fragment reasssembly queue length */
129 #define MP_MAX_QUEUE_LEN 128
131 /* Fragment queue scanner period */
132 #define MP_FRAGTIMER_INTERVAL (hz/2)
134 /* We store incoming fragments this way */
136 int seq; /* fragment seq# */
137 u_char first; /* First in packet? */
138 u_char last; /* Last in packet? */
139 struct timeval timestamp; /* time of reception */
140 struct mbuf *data; /* Fragment data */
141 meta_p meta; /* Fragment meta */
142 TAILQ_ENTRY(ng_ppp_frag) f_qent; /* Fragment queue */
145 /* We use integer indicies to refer to the non-link hooks */
146 static const char *const ng_ppp_hook_names[] = {
148 #define HOOK_INDEX_ATALK 0
150 #define HOOK_INDEX_BYPASS 1
151 NG_PPP_HOOK_COMPRESS,
152 #define HOOK_INDEX_COMPRESS 2
154 #define HOOK_INDEX_ENCRYPT 3
155 NG_PPP_HOOK_DECOMPRESS,
156 #define HOOK_INDEX_DECOMPRESS 4
158 #define HOOK_INDEX_DECRYPT 5
160 #define HOOK_INDEX_INET 6
162 #define HOOK_INDEX_IPX 7
163 NG_PPP_HOOK_VJC_COMP,
164 #define HOOK_INDEX_VJC_COMP 8
166 #define HOOK_INDEX_VJC_IP 9
167 NG_PPP_HOOK_VJC_UNCOMP,
168 #define HOOK_INDEX_VJC_UNCOMP 10
169 NG_PPP_HOOK_VJC_VJIP,
170 #define HOOK_INDEX_VJC_VJIP 11
172 #define HOOK_INDEX_IPV6 12
174 #define HOOK_INDEX_MAX 13
177 /* We store index numbers in the hook private pointer. The HOOK_INDEX()
178 for a hook is either the index (above) for normal hooks, or the ones
179 complement of the link number for link hooks.
180 XXX Not any more.. (what a hack)
181 #define HOOK_INDEX(hook) (*((int16_t *) &(hook)->private))
184 /* Per-link private information */
186 struct ng_ppp_link_conf conf; /* link configuration */
187 hook_p hook; /* connection to link data */
188 int32_t seq; /* highest rec'd seq# - MSEQ */
189 struct timeval lastWrite; /* time of last write */
190 int bytesInQueue; /* bytes in the output queue */
191 struct ng_ppp_link_stat stats; /* Link stats */
194 /* Total per-node private information */
195 struct ng_ppp_private {
196 struct ng_ppp_bund_conf conf; /* bundle config */
197 struct ng_ppp_link_stat bundleStats; /* bundle stats */
198 struct ng_ppp_link links[NG_PPP_MAX_LINKS];/* per-link info */
199 int32_t xseq; /* next out MP seq # */
200 int32_t mseq; /* min links[i].seq */
201 u_char vjCompHooked; /* VJ comp hooked up? */
202 u_char allLinksEqual; /* all xmit the same? */
203 u_char timerActive; /* frag timer active? */
204 u_int numActiveLinks; /* how many links up */
205 int activeLinks[NG_PPP_MAX_LINKS]; /* indicies */
206 u_int lastLink; /* for round robin */
207 hook_p hooks[HOOK_INDEX_MAX]; /* non-link hooks */
208 TAILQ_HEAD(ng_ppp_fraglist, ng_ppp_frag) /* fragment queue */
210 int qlen; /* fraq queue length */
211 struct callout_handle fragTimer; /* fraq queue check */
213 typedef struct ng_ppp_private *priv_p;
215 /* Netgraph node methods */
216 static ng_constructor_t ng_ppp_constructor;
217 static ng_rcvmsg_t ng_ppp_rcvmsg;
218 static ng_shutdown_t ng_ppp_shutdown;
219 static ng_newhook_t ng_ppp_newhook;
220 static ng_rcvdata_t ng_ppp_rcvdata;
221 static ng_disconnect_t ng_ppp_disconnect;
223 /* Helper functions */
224 static int ng_ppp_input(node_p node, int bypass,
225 int linkNum, item_p item);
226 static int ng_ppp_output(node_p node, int bypass, int proto,
227 int linkNum, item_p item);
228 static int ng_ppp_mp_input(node_p node, int linkNum, item_p item);
229 static int ng_ppp_check_packet(node_p node);
230 static void ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap);
231 static int ng_ppp_frag_process(node_p node);
232 static int ng_ppp_frag_trim(node_p node);
233 static void ng_ppp_frag_timeout(void *arg);
234 static void ng_ppp_frag_checkstale(node_p node);
235 static void ng_ppp_frag_reset(node_p node);
236 static int ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta);
237 static void ng_ppp_mp_strategy(node_p node, int len, int *distrib);
238 static int ng_ppp_intcmp(const void *v1, const void *v2);
239 static struct mbuf *ng_ppp_addproto(struct mbuf *m, int proto, int compOK);
240 static struct mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len);
241 static int ng_ppp_config_valid(node_p node,
242 const struct ng_ppp_node_conf *newConf);
243 static void ng_ppp_update(node_p node, int newConf);
244 static void ng_ppp_start_frag_timer(node_p node);
245 static void ng_ppp_stop_frag_timer(node_p node);
247 /* Parse type for struct ng_ppp_mp_state_type */
248 static const struct ng_parse_fixedarray_info ng_ppp_rseq_array_info = {
249 &ng_parse_hint32_type,
252 static const struct ng_parse_type ng_ppp_rseq_array_type = {
253 &ng_parse_fixedarray_type,
254 &ng_ppp_rseq_array_info,
256 static const struct ng_parse_struct_info ng_ppp_mp_state_type_info
257 = NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type);
258 static const struct ng_parse_type ng_ppp_mp_state_type = {
259 &ng_parse_struct_type,
260 &ng_ppp_mp_state_type_info,
263 /* Parse type for struct ng_ppp_link_conf */
264 static const struct ng_parse_struct_info
265 ng_ppp_link_type_info = NG_PPP_LINK_TYPE_INFO;
266 static const struct ng_parse_type ng_ppp_link_type = {
267 &ng_parse_struct_type,
268 &ng_ppp_link_type_info,
271 /* Parse type for struct ng_ppp_bund_conf */
272 static const struct ng_parse_struct_info
273 ng_ppp_bund_type_info = NG_PPP_BUND_TYPE_INFO;
274 static const struct ng_parse_type ng_ppp_bund_type = {
275 &ng_parse_struct_type,
276 &ng_ppp_bund_type_info,
279 /* Parse type for struct ng_ppp_node_conf */
280 static const struct ng_parse_fixedarray_info ng_ppp_array_info = {
284 static const struct ng_parse_type ng_ppp_link_array_type = {
285 &ng_parse_fixedarray_type,
288 static const struct ng_parse_struct_info ng_ppp_conf_type_info
289 = NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type);
290 static const struct ng_parse_type ng_ppp_conf_type = {
291 &ng_parse_struct_type,
292 &ng_ppp_conf_type_info
295 /* Parse type for struct ng_ppp_link_stat */
296 static const struct ng_parse_struct_info
297 ng_ppp_stats_type_info = NG_PPP_STATS_TYPE_INFO;
298 static const struct ng_parse_type ng_ppp_stats_type = {
299 &ng_parse_struct_type,
300 &ng_ppp_stats_type_info
303 /* List of commands and how to convert arguments to/from ASCII */
304 static const struct ng_cmdlist ng_ppp_cmds[] = {
321 NGM_PPP_GET_MP_STATE,
324 &ng_ppp_mp_state_type
328 NGM_PPP_GET_LINK_STATS,
330 &ng_parse_int16_type,
335 NGM_PPP_CLR_LINK_STATS,
337 &ng_parse_int16_type,
342 NGM_PPP_GETCLR_LINK_STATS,
344 &ng_parse_int16_type,
350 /* Node type descriptor */
351 static struct ng_type ng_ppp_typestruct = {
365 NETGRAPH_INIT(ppp, &ng_ppp_typestruct);
367 static int *compareLatencies; /* hack for ng_ppp_intcmp() */
369 /* Address and control field header */
370 static const u_char ng_ppp_acf[2] = { 0xff, 0x03 };
372 /* Maximum time we'll let a complete incoming packet sit in the queue */
373 static const struct timeval ng_ppp_max_staleness = { 2, 0 }; /* 2 seconds */
375 #define ERROUT(x) do { error = (x); goto done; } while (0)
377 /************************************************************************
379 ************************************************************************/
382 * Node type constructor
385 ng_ppp_constructor(node_p node)
390 /* Allocate private structure */
391 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_PPP, M_NOWAIT | M_ZERO);
395 NG_NODE_SET_PRIVATE(node, priv);
397 /* Initialize state */
398 TAILQ_INIT(&priv->frags);
399 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
400 priv->links[i].seq = MP_NOSEQ;
401 callout_handle_init(&priv->fragTimer);
408 * Give our OK for a hook to be added
411 ng_ppp_newhook(node_p node, hook_p hook, const char *name)
413 const priv_p priv = NG_NODE_PRIVATE(node);
415 hook_p *hookPtr = NULL;
418 /* Figure out which hook it is */
419 if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX, /* a link hook? */
420 strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) {
424 cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
425 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
427 linkNum = (int)strtoul(cp, &eptr, 10);
428 if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS)
430 hookPtr = &priv->links[linkNum].hook;
431 hookIndex = ~linkNum;
432 } else { /* must be a non-link hook */
435 for (i = 0; ng_ppp_hook_names[i] != NULL; i++) {
436 if (strcmp(name, ng_ppp_hook_names[i]) == 0) {
437 hookPtr = &priv->hooks[i];
442 if (ng_ppp_hook_names[i] == NULL)
443 return (EINVAL); /* no such hook */
446 /* See if hook is already connected */
447 if (*hookPtr != NULL)
450 /* Disallow more than one link unless multilink is enabled */
451 if (linkNum != -1 && priv->links[linkNum].conf.enableLink
452 && !priv->conf.enableMultilink && priv->numActiveLinks >= 1)
457 NG_HOOK_SET_PRIVATE(hook, (void *)hookIndex);
458 ng_ppp_update(node, 0);
463 * Receive a control message
466 ng_ppp_rcvmsg(node_p node, item_p item, hook_p lasthook)
468 const priv_p priv = NG_NODE_PRIVATE(node);
469 struct ng_mesg *resp = NULL;
473 NGI_GET_MSG(item, msg);
474 switch (msg->header.typecookie) {
476 switch (msg->header.cmd) {
477 case NGM_PPP_SET_CONFIG:
479 struct ng_ppp_node_conf *const conf =
480 (struct ng_ppp_node_conf *)msg->data;
483 /* Check for invalid or illegal config */
484 if (msg->header.arglen != sizeof(*conf))
486 if (!ng_ppp_config_valid(node, conf))
490 priv->conf = conf->bund;
491 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
492 priv->links[i].conf = conf->links[i];
493 ng_ppp_update(node, 1);
496 case NGM_PPP_GET_CONFIG:
498 struct ng_ppp_node_conf *conf;
501 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
504 conf = (struct ng_ppp_node_conf *)resp->data;
505 conf->bund = priv->conf;
506 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
507 conf->links[i] = priv->links[i].conf;
510 case NGM_PPP_GET_MP_STATE:
512 struct ng_ppp_mp_state *info;
515 NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT);
518 info = (struct ng_ppp_mp_state *)resp->data;
519 bzero(info, sizeof(*info));
520 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
521 if (priv->links[i].seq != MP_NOSEQ)
522 info->rseq[i] = priv->links[i].seq;
524 info->mseq = priv->mseq;
525 info->xseq = priv->xseq;
528 case NGM_PPP_GET_LINK_STATS:
529 case NGM_PPP_CLR_LINK_STATS:
530 case NGM_PPP_GETCLR_LINK_STATS:
532 struct ng_ppp_link_stat *stats;
535 if (msg->header.arglen != sizeof(u_int16_t))
537 linkNum = *((u_int16_t *) msg->data);
538 if (linkNum >= NG_PPP_MAX_LINKS
539 && linkNum != NG_PPP_BUNDLE_LINKNUM)
541 stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ?
542 &priv->bundleStats : &priv->links[linkNum].stats;
543 if (msg->header.cmd != NGM_PPP_CLR_LINK_STATS) {
544 NG_MKRESPONSE(resp, msg,
545 sizeof(struct ng_ppp_link_stat), M_NOWAIT);
548 bcopy(stats, resp->data, sizeof(*stats));
550 if (msg->header.cmd != NGM_PPP_GET_LINK_STATS)
551 bzero(stats, sizeof(*stats));
562 * Forward it to the vjc node. leave the
563 * old return address alone.
564 * If we have no hook, let NG_RESPOND_MSG
565 * clean up any remaining resources.
566 * Because we have no resp, the item will be freed
567 * along with anything it references. Don't
568 * let msg be freed twice.
570 NGI_MSG(item) = msg; /* put it back in the item */
572 if ((lasthook = priv->links[HOOK_INDEX_VJC_IP].hook)) {
573 NG_FWD_ITEM_HOOK(error, item, lasthook);
582 NG_RESPOND_MSG(error, node, item, resp);
588 * Receive data on a hook
591 ng_ppp_rcvdata(hook_p hook, item_p item)
593 const node_p node = NG_HOOK_NODE(hook);
594 const priv_p priv = NG_NODE_PRIVATE(node);
595 const int index = (int)NG_HOOK_PRIVATE(hook);
596 u_int16_t linkNum = NG_PPP_BUNDLE_LINKNUM;
597 hook_p outHook = NULL;
598 int proto = 0, error;
602 /* Did it come from a link hook? */
604 struct ng_ppp_link *link;
606 /* Convert index into a link number */
607 linkNum = (u_int16_t)~index;
608 KASSERT(linkNum < NG_PPP_MAX_LINKS,
609 ("%s: bogus index 0x%x", __func__, index));
610 link = &priv->links[linkNum];
613 link->stats.recvFrames++;
614 link->stats.recvOctets += m->m_pkthdr.len;
616 /* Strip address and control fields, if present */
617 if (m->m_pkthdr.len >= 2) {
618 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
622 if (bcmp(mtod(m, u_char *), &ng_ppp_acf, 2) == 0)
626 /* Dispatch incoming frame (if not enabled, to bypass) */
627 NGI_M(item) = m; /* put changed m back in item */
628 return ng_ppp_input(node,
629 !link->conf.enableLink, linkNum, item);
632 /* Get protocol & check if data allowed from this hook */
633 NGI_M(item) = m; /* put possibly changed m back in item */
637 case HOOK_INDEX_ATALK:
638 if (!priv->conf.enableAtalk) {
642 proto = PROT_APPLETALK;
645 if (!priv->conf.enableIPX) {
651 case HOOK_INDEX_IPV6:
652 if (!priv->conf.enableIPv6) {
658 case HOOK_INDEX_INET:
659 case HOOK_INDEX_VJC_VJIP:
660 if (!priv->conf.enableIP) {
666 case HOOK_INDEX_VJC_COMP:
667 if (!priv->conf.enableVJCompression) {
673 case HOOK_INDEX_VJC_UNCOMP:
674 if (!priv->conf.enableVJCompression) {
678 proto = PROT_VJUNCOMP;
680 case HOOK_INDEX_COMPRESS:
681 if (!priv->conf.enableCompression) {
687 case HOOK_INDEX_ENCRYPT:
688 if (!priv->conf.enableEncryption) {
694 case HOOK_INDEX_BYPASS:
695 if (m->m_pkthdr.len < 4) {
699 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
700 NGI_M(item) = NULL; /* don't free twice */
704 NGI_M(item) = m; /* m may have changed */
705 linkNum = ntohs(mtod(m, u_int16_t *)[0]);
706 proto = ntohs(mtod(m, u_int16_t *)[1]);
708 if (linkNum >= NG_PPP_MAX_LINKS
709 && linkNum != NG_PPP_BUNDLE_LINKNUM) {
716 case HOOK_INDEX_VJC_IP:
717 if (!priv->conf.enableIP || !priv->conf.enableVJDecompression) {
722 case HOOK_INDEX_DECOMPRESS:
723 if (!priv->conf.enableDecompression) {
728 case HOOK_INDEX_DECRYPT:
729 if (!priv->conf.enableDecryption) {
735 panic("%s: bogus index 0x%x", __func__, index);
738 /* Now figure out what to do with the frame */
742 case HOOK_INDEX_INET:
743 if (priv->conf.enableVJCompression && priv->vjCompHooked) {
744 outHook = priv->hooks[HOOK_INDEX_VJC_IP];
748 case HOOK_INDEX_ATALK:
749 case HOOK_INDEX_IPV6:
751 case HOOK_INDEX_VJC_COMP:
752 case HOOK_INDEX_VJC_UNCOMP:
753 case HOOK_INDEX_VJC_VJIP:
754 if (priv->conf.enableCompression
755 && priv->hooks[HOOK_INDEX_COMPRESS] != NULL) {
756 if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
761 NGI_M(item) = m; /* m may have changed */
762 outHook = priv->hooks[HOOK_INDEX_COMPRESS];
766 case HOOK_INDEX_COMPRESS:
767 if (priv->conf.enableEncryption
768 && priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) {
769 if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
774 NGI_M(item) = m; /* m may have changed */
775 outHook = priv->hooks[HOOK_INDEX_ENCRYPT];
779 case HOOK_INDEX_ENCRYPT:
780 return ng_ppp_output(node, 0, proto, NG_PPP_BUNDLE_LINKNUM, item);
782 case HOOK_INDEX_BYPASS:
783 return ng_ppp_output(node, 1, proto, linkNum, item);
786 case HOOK_INDEX_DECRYPT:
787 case HOOK_INDEX_DECOMPRESS:
788 return ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
790 case HOOK_INDEX_VJC_IP:
791 outHook = priv->hooks[HOOK_INDEX_INET];
795 /* Send packet out hook */
796 NG_FWD_ITEM_HOOK(error, item, outHook);
804 ng_ppp_shutdown(node_p node)
806 const priv_p priv = NG_NODE_PRIVATE(node);
808 /* Stop fragment queue timer */
809 ng_ppp_stop_frag_timer(node);
811 /* Take down netgraph node */
812 ng_ppp_frag_reset(node);
813 bzero(priv, sizeof(*priv));
814 FREE(priv, M_NETGRAPH_PPP);
815 NG_NODE_SET_PRIVATE(node, NULL);
816 NG_NODE_UNREF(node); /* let the node escape */
824 ng_ppp_disconnect(hook_p hook)
826 const node_p node = NG_HOOK_NODE(hook);
827 const priv_p priv = NG_NODE_PRIVATE(node);
828 const int index = (int)NG_HOOK_PRIVATE(hook);
830 /* Zero out hook pointer */
832 priv->links[~index].hook = NULL;
834 priv->hooks[index] = NULL;
836 /* Update derived info (or go away if no hooks left) */
837 if (NG_NODE_NUMHOOKS(node) > 0) {
838 ng_ppp_update(node, 0);
840 if (NG_NODE_IS_VALID(node)) {
841 ng_rmnode_self(node);
847 /************************************************************************
849 ************************************************************************/
852 * Handle an incoming frame. Extract the PPP protocol number
853 * and dispatch accordingly.
856 ng_ppp_input(node_p node, int bypass, int linkNum, item_p item)
858 const priv_p priv = NG_NODE_PRIVATE(node);
859 hook_p outHook = NULL;
865 /* Extract protocol number */
866 for (proto = 0; !PROT_VALID(proto) && m->m_pkthdr.len > 0; ) {
867 if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) {
871 proto = (proto << 8) + *mtod(m, u_char *);
874 if (!PROT_VALID(proto)) {
875 if (linkNum == NG_PPP_BUNDLE_LINKNUM)
876 priv->bundleStats.badProtos++;
878 priv->links[linkNum].stats.badProtos++;
891 if (priv->conf.enableDecompression)
892 outHook = priv->hooks[HOOK_INDEX_DECOMPRESS];
895 if (priv->conf.enableDecryption)
896 outHook = priv->hooks[HOOK_INDEX_DECRYPT];
899 if (priv->conf.enableVJDecompression && priv->vjCompHooked)
900 outHook = priv->hooks[HOOK_INDEX_VJC_COMP];
903 if (priv->conf.enableVJDecompression && priv->vjCompHooked)
904 outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP];
907 if (priv->conf.enableMultilink
908 && linkNum != NG_PPP_BUNDLE_LINKNUM)
909 return ng_ppp_mp_input(node, linkNum, item);
912 if (priv->conf.enableAtalk)
913 outHook = priv->hooks[HOOK_INDEX_ATALK];
916 if (priv->conf.enableIPX)
917 outHook = priv->hooks[HOOK_INDEX_IPX];
920 if (priv->conf.enableIP)
921 outHook = priv->hooks[HOOK_INDEX_INET];
924 if (priv->conf.enableIPv6)
925 outHook = priv->hooks[HOOK_INDEX_IPV6];
930 /* For unknown/inactive protocols, forward out the bypass hook */
931 if (outHook == NULL) {
934 hdr[0] = htons(linkNum);
935 hdr[1] = htons((u_int16_t)proto);
936 if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) {
940 outHook = priv->hooks[HOOK_INDEX_BYPASS];
944 NG_FWD_NEW_DATA(error, item, outHook, m);
949 * Deliver a frame out a link, either a real one or NG_PPP_BUNDLE_LINKNUM
950 * If the link is not enabled then ENXIO is returned, unless "bypass" is != 0.
953 ng_ppp_output(node_p node, int bypass,
954 int proto, int linkNum, item_p item)
956 const priv_p priv = NG_NODE_PRIVATE(node);
957 struct ng_ppp_link *link;
961 NGI_GET_M(item, m); /* separate them for a while */
962 /* If not doing MP, map bundle virtual link to (the only) link */
963 if (linkNum == NG_PPP_BUNDLE_LINKNUM && !priv->conf.enableMultilink)
964 linkNum = priv->activeLinks[0];
966 /* Get link pointer (optimization) */
967 link = (linkNum != NG_PPP_BUNDLE_LINKNUM) ?
968 &priv->links[linkNum] : NULL;
970 /* Check link status (if real) */
971 if (linkNum != NG_PPP_BUNDLE_LINKNUM) {
972 if (!bypass && !link->conf.enableLink) {
977 if (link->hook == NULL) {
984 /* Prepend protocol number, possibly compressed */
985 if ((m = ng_ppp_addproto(m, proto,
986 linkNum == NG_PPP_BUNDLE_LINKNUM
987 || link->conf.enableProtoComp)) == NULL) {
992 /* Special handling for the MP virtual link */
993 if (linkNum == NG_PPP_BUNDLE_LINKNUM) {
996 /* strip off and discard the queue item */
997 NGI_GET_META(item, meta);
999 return ng_ppp_mp_output(node, m, meta);
1002 /* Prepend address and control field (unless compressed) */
1003 if (proto == PROT_LCP || !link->conf.enableACFComp) {
1004 if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) {
1011 len = m->m_pkthdr.len;
1012 NG_FWD_NEW_DATA(error, item, link->hook, m);
1014 /* Update stats and 'bytes in queue' counter */
1016 link->stats.xmitFrames++;
1017 link->stats.xmitOctets += len;
1018 link->bytesInQueue += len;
1019 getmicrouptime(&link->lastWrite);
1025 * Handle an incoming multi-link fragment
1027 * The fragment reassembly algorithm is somewhat complex. This is mainly
1028 * because we are required not to reorder the reconstructed packets, yet
1029 * fragments are only guaranteed to arrive in order on a per-link basis.
1030 * In other words, when we have a complete packet ready, but the previous
1031 * packet is still incomplete, we have to decide between delivering the
1032 * complete packet and throwing away the incomplete one, or waiting to
1033 * see if the remainder of the incomplete one arrives, at which time we
1034 * can deliver both packets, in order.
1036 * This problem is exacerbated by "sequence number slew", which is when
1037 * the sequence numbers coming in from different links are far apart from
1038 * each other. In particular, certain unnamed equipment (*cough* Ascend)
1039 * has been seen to generate sequence number slew of up to 10 on an ISDN
1040 * 2B-channel MP link. There is nothing invalid about sequence number slew
1041 * but it makes the reasssembly process have to work harder.
1043 * However, the peer is required to transmit fragments in order on each
1044 * link. That means if we define MSEQ as the minimum over all links of
1045 * the highest sequence number received on that link, then we can always
1046 * give up any hope of receiving a fragment with sequence number < MSEQ in
1047 * the future (all of this using 'wraparound' sequence number space).
1048 * Therefore we can always immediately throw away incomplete packets
1049 * missing fragments with sequence numbers < MSEQ.
1051 * Here is an overview of our algorithm:
1053 * o Received fragments are inserted into a queue, for which we
1054 * maintain these invariants between calls to this function:
1056 * - Fragments are ordered in the queue by sequence number
1057 * - If a complete packet is at the head of the queue, then
1058 * the first fragment in the packet has seq# > MSEQ + 1
1059 * (otherwise, we could deliver it immediately)
1060 * - If any fragments have seq# < MSEQ, then they are necessarily
1061 * part of a packet whose missing seq#'s are all > MSEQ (otherwise,
1062 * we can throw them away because they'll never be completed)
1063 * - The queue contains at most MP_MAX_QUEUE_LEN fragments
1065 * o We have a periodic timer that checks the queue for the first
1066 * complete packet that has been sitting in the queue "too long".
1067 * When one is detected, all previous (incomplete) fragments are
1068 * discarded, their missing fragments are declared lost and MSEQ
1071 * o If we recieve a fragment with seq# < MSEQ, we throw it away
1072 * because we've already delcared it lost.
1074 * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM.
1077 ng_ppp_mp_input(node_p node, int linkNum, item_p item)
1079 const priv_p priv = NG_NODE_PRIVATE(node);
1080 struct ng_ppp_link *const link = &priv->links[linkNum];
1081 struct ng_ppp_frag frag0, *frag = &frag0;
1082 struct ng_ppp_frag *qent;
1083 int i, diff, inserted;
1088 NGI_GET_META(item, meta);
1091 priv->bundleStats.recvFrames++;
1092 priv->bundleStats.recvOctets += m->m_pkthdr.len;
1094 /* Extract fragment information from MP header */
1095 if (priv->conf.recvShortSeq) {
1098 if (m->m_pkthdr.len < 2) {
1099 link->stats.runts++;
1104 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1108 shdr = ntohs(*mtod(m, u_int16_t *));
1109 frag->seq = MP_SHORT_EXTEND(shdr);
1110 frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0;
1111 frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0;
1112 diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq);
1117 if (m->m_pkthdr.len < 4) {
1118 link->stats.runts++;
1123 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
1127 lhdr = ntohl(*mtod(m, u_int32_t *));
1128 frag->seq = MP_LONG_EXTEND(lhdr);
1129 frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0;
1130 frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0;
1131 diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq);
1136 getmicrouptime(&frag->timestamp);
1138 /* If sequence number is < MSEQ, we've already declared this
1139 fragment as lost, so we have no choice now but to drop it */
1141 link->stats.dropFragments++;
1147 /* Update highest received sequence number on this link and MSEQ */
1148 priv->mseq = link->seq = frag->seq;
1149 for (i = 0; i < priv->numActiveLinks; i++) {
1150 struct ng_ppp_link *const alink =
1151 &priv->links[priv->activeLinks[i]];
1153 if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0)
1154 priv->mseq = alink->seq;
1157 /* Allocate a new frag struct for the queue */
1158 MALLOC(frag, struct ng_ppp_frag *, sizeof(*frag), M_NETGRAPH_PPP, M_NOWAIT);
1162 ng_ppp_frag_process(node);
1167 /* Add fragment to queue, which is sorted by sequence number */
1169 TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) {
1170 diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq);
1172 TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent);
1175 } else if (diff == 0) { /* should never happen! */
1176 link->stats.dupFragments++;
1177 NG_FREE_M(frag->data);
1178 NG_FREE_META(frag->meta);
1179 FREE(frag, M_NETGRAPH_PPP);
1184 TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent);
1187 /* Process the queue */
1188 return ng_ppp_frag_process(node);
1192 * Examine our list of fragments, and determine if there is a
1193 * complete and deliverable packet at the head of the list.
1194 * Return 1 if so, zero otherwise.
1197 ng_ppp_check_packet(node_p node)
1199 const priv_p priv = NG_NODE_PRIVATE(node);
1200 struct ng_ppp_frag *qent, *qnext;
1202 /* Check for empty queue */
1203 if (TAILQ_EMPTY(&priv->frags))
1206 /* Check first fragment is the start of a deliverable packet */
1207 qent = TAILQ_FIRST(&priv->frags);
1208 if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1)
1211 /* Check that all the fragments are there */
1212 while (!qent->last) {
1213 qnext = TAILQ_NEXT(qent, f_qent);
1214 if (qnext == NULL) /* end of queue */
1216 if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq))
1226 * Pull a completed packet off the head of the incoming fragment queue.
1227 * This assumes there is a completed packet there to pull off.
1230 ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap)
1232 const priv_p priv = NG_NODE_PRIVATE(node);
1233 struct ng_ppp_frag *qent, *qnext;
1234 struct mbuf *m = NULL, *tail;
1236 qent = TAILQ_FIRST(&priv->frags);
1237 KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first,
1238 ("%s: no packet", __func__));
1239 for (tail = NULL; qent != NULL; qent = qnext) {
1240 qnext = TAILQ_NEXT(qent, f_qent);
1241 KASSERT(!TAILQ_EMPTY(&priv->frags),
1242 ("%s: empty q", __func__));
1243 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1245 tail = m = qent->data;
1246 *metap = qent->meta; /* inherit first frag's meta */
1248 m->m_pkthdr.len += qent->data->m_pkthdr.len;
1249 tail->m_next = qent->data;
1250 NG_FREE_META(qent->meta); /* drop other frags' metas */
1252 while (tail->m_next != NULL)
1253 tail = tail->m_next;
1256 FREE(qent, M_NETGRAPH_PPP);
1263 * Trim fragments from the queue whose packets can never be completed.
1264 * This assumes a complete packet is NOT at the beginning of the queue.
1265 * Returns 1 if fragments were removed, zero otherwise.
1268 ng_ppp_frag_trim(node_p node)
1270 const priv_p priv = NG_NODE_PRIVATE(node);
1271 struct ng_ppp_frag *qent, *qnext = NULL;
1274 /* Scan for "dead" fragments and remove them */
1278 /* If queue is empty, we're done */
1279 if (TAILQ_EMPTY(&priv->frags))
1282 /* Determine whether first fragment can ever be completed */
1283 TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1284 if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0)
1286 qnext = TAILQ_NEXT(qent, f_qent);
1287 KASSERT(qnext != NULL,
1288 ("%s: last frag < MSEQ?", __func__));
1289 if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)
1290 || qent->last || qnext->first) {
1298 /* Remove fragment and all others in the same packet */
1299 while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) {
1300 KASSERT(!TAILQ_EMPTY(&priv->frags),
1301 ("%s: empty q", __func__));
1302 priv->bundleStats.dropFragments++;
1303 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1304 NG_FREE_M(qent->data);
1305 NG_FREE_META(qent->meta);
1306 FREE(qent, M_NETGRAPH_PPP);
1315 * Run the queue, restoring the queue invariants
1318 ng_ppp_frag_process(node_p node)
1320 const priv_p priv = NG_NODE_PRIVATE(node);
1325 /* Deliver any deliverable packets */
1326 while (ng_ppp_check_packet(node)) {
1327 ng_ppp_get_packet(node, &m, &meta);
1328 item = ng_package_data(m, meta);
1329 ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1332 /* Delete dead fragments and try again */
1333 if (ng_ppp_frag_trim(node)) {
1334 while (ng_ppp_check_packet(node)) {
1335 ng_ppp_get_packet(node, &m, &meta);
1336 item = ng_package_data(m, meta);
1337 ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1341 /* Check for stale fragments while we're here */
1342 ng_ppp_frag_checkstale(node);
1344 /* Check queue length */
1345 if (priv->qlen > MP_MAX_QUEUE_LEN) {
1346 struct ng_ppp_frag *qent;
1349 /* Get oldest fragment */
1350 KASSERT(!TAILQ_EMPTY(&priv->frags),
1351 ("%s: empty q", __func__));
1352 qent = TAILQ_FIRST(&priv->frags);
1354 /* Bump MSEQ if necessary */
1355 if (MP_RECV_SEQ_DIFF(priv, priv->mseq, qent->seq) < 0) {
1356 priv->mseq = qent->seq;
1357 for (i = 0; i < priv->numActiveLinks; i++) {
1358 struct ng_ppp_link *const alink =
1359 &priv->links[priv->activeLinks[i]];
1361 if (MP_RECV_SEQ_DIFF(priv,
1362 alink->seq, priv->mseq) < 0)
1363 alink->seq = priv->mseq;
1368 priv->bundleStats.dropFragments++;
1369 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1370 NG_FREE_M(qent->data);
1371 NG_FREE_META(qent->meta);
1372 FREE(qent, M_NETGRAPH_PPP);
1375 /* Process queue again */
1376 return ng_ppp_frag_process(node);
1384 * Check for 'stale' completed packets that need to be delivered
1386 * If a link goes down or has a temporary failure, MSEQ can get
1387 * "stuck", because no new incoming fragments appear on that link.
1388 * This can cause completed packets to never get delivered if
1389 * their sequence numbers are all > MSEQ + 1.
1391 * This routine checks how long all of the completed packets have
1392 * been sitting in the queue, and if too long, removes fragments
1393 * from the queue and increments MSEQ to allow them to be delivered.
1396 ng_ppp_frag_checkstale(node_p node)
1398 const priv_p priv = NG_NODE_PRIVATE(node);
1399 struct ng_ppp_frag *qent, *beg, *end;
1400 struct timeval now, age;
1406 now.tv_sec = 0; /* uninitialized state */
1409 /* If queue is empty, we're done */
1410 if (TAILQ_EMPTY(&priv->frags))
1413 /* Find the first complete packet in the queue */
1415 seq = TAILQ_FIRST(&priv->frags)->seq;
1416 TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1419 else if (qent->seq != seq)
1421 if (beg != NULL && qent->last) {
1425 seq = MP_NEXT_RECV_SEQ(priv, seq);
1428 /* If none found, exit */
1432 /* Get current time (we assume we've been up for >= 1 second) */
1433 if (now.tv_sec == 0)
1434 getmicrouptime(&now);
1436 /* Check if packet has been queued too long */
1438 timevalsub(&age, &beg->timestamp);
1439 if (timevalcmp(&age, &ng_ppp_max_staleness, < ))
1442 /* Throw away junk fragments in front of the completed packet */
1443 while ((qent = TAILQ_FIRST(&priv->frags)) != beg) {
1444 KASSERT(!TAILQ_EMPTY(&priv->frags),
1445 ("%s: empty q", __func__));
1446 priv->bundleStats.dropFragments++;
1447 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1448 NG_FREE_M(qent->data);
1449 NG_FREE_META(qent->meta);
1450 FREE(qent, M_NETGRAPH_PPP);
1454 /* Extract completed packet */
1455 ng_ppp_get_packet(node, &m, &meta);
1457 /* Bump MSEQ if necessary */
1458 if (MP_RECV_SEQ_DIFF(priv, priv->mseq, end->seq) < 0) {
1459 priv->mseq = end->seq;
1460 for (i = 0; i < priv->numActiveLinks; i++) {
1461 struct ng_ppp_link *const alink =
1462 &priv->links[priv->activeLinks[i]];
1464 if (MP_RECV_SEQ_DIFF(priv,
1465 alink->seq, priv->mseq) < 0)
1466 alink->seq = priv->mseq;
1470 /* Deliver packet */
1471 item = ng_package_data(m, meta);
1472 ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1477 * Periodically call ng_ppp_frag_checkstale()
1480 ng_ppp_frag_timeout(void *arg)
1482 const node_p node = arg;
1483 const priv_p priv = NG_NODE_PRIVATE(node);
1486 /* Handle the race where shutdown happens just before splnet() above */
1487 if (NG_NODE_NOT_VALID(node)) {
1488 NG_NODE_UNREF(node);
1493 /* Reset timer state after timeout */
1494 KASSERT(priv->timerActive, ("%s: !timerActive", __func__));
1495 priv->timerActive = 0;
1496 KASSERT(node->nd_refs > 1, ("%s: nd_refs=%d", __func__, node->nd_refs));
1497 NG_NODE_UNREF(node);
1499 /* Start timer again */
1500 ng_ppp_start_frag_timer(node);
1502 /* Scan the fragment queue */
1503 ng_ppp_frag_checkstale(node);
1508 * Deliver a frame out on the bundle, i.e., figure out how to fragment
1509 * the frame across the individual PPP links and do so.
1512 ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta)
1514 const priv_p priv = NG_NODE_PRIVATE(node);
1515 int distrib[NG_PPP_MAX_LINKS];
1520 /* At least one link must be active */
1521 if (priv->numActiveLinks == 0) {
1527 /* Round-robin strategy */
1528 if (priv->conf.enableRoundRobin || m->m_pkthdr.len < MP_MIN_FRAG_LEN) {
1529 activeLinkNum = priv->lastLink++ % priv->numActiveLinks;
1530 bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0]));
1531 distrib[activeLinkNum] = m->m_pkthdr.len;
1535 /* Strategy when all links are equivalent (optimize the common case) */
1536 if (priv->allLinksEqual) {
1537 const int fraction = m->m_pkthdr.len / priv->numActiveLinks;
1540 for (i = 0; i < priv->numActiveLinks; i++)
1541 distrib[priv->lastLink++ % priv->numActiveLinks]
1543 remain = m->m_pkthdr.len - (fraction * priv->numActiveLinks);
1544 while (remain > 0) {
1545 distrib[priv->lastLink++ % priv->numActiveLinks]++;
1551 /* Strategy when all links are not equivalent */
1552 ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib);
1556 priv->bundleStats.xmitFrames++;
1557 priv->bundleStats.xmitOctets += m->m_pkthdr.len;
1559 /* Send alloted portions of frame out on the link(s) */
1560 for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1;
1561 activeLinkNum >= 0; activeLinkNum--) {
1562 const int linkNum = priv->activeLinks[activeLinkNum];
1563 struct ng_ppp_link *const link = &priv->links[linkNum];
1565 /* Deliver fragment(s) out the next link */
1566 for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) {
1567 int len, lastFragment, error;
1571 /* Calculate fragment length; don't exceed link MTU */
1572 len = distrib[activeLinkNum];
1573 if (len > link->conf.mru)
1574 len = link->conf.mru;
1575 distrib[activeLinkNum] -= len;
1576 lastFragment = (len == m->m_pkthdr.len);
1578 /* Split off next fragment as "m2" */
1580 if (!lastFragment) {
1581 struct mbuf *n = m_split(m, len, M_NOWAIT);
1591 /* Prepend MP header */
1592 if (priv->conf.xmitShortSeq) {
1597 (priv->xseq + 1) & MP_SHORT_SEQ_MASK;
1599 shdr |= MP_SHORT_FIRST_FLAG;
1601 shdr |= MP_SHORT_LAST_FLAG;
1603 m2 = ng_ppp_prepend(m2, &shdr, 2);
1609 (priv->xseq + 1) & MP_LONG_SEQ_MASK;
1611 lhdr |= MP_LONG_FIRST_FLAG;
1613 lhdr |= MP_LONG_LAST_FLAG;
1615 m2 = ng_ppp_prepend(m2, &lhdr, 4);
1624 /* Copy the meta information, if any */
1625 meta2 = lastFragment ? meta : ng_copy_meta(meta);
1628 item = ng_package_data(m2, meta2);
1629 error = ng_ppp_output(node, 0, PROT_MP, linkNum, item);
1631 if (!lastFragment) {
1645 * Computing the optimal fragmentation
1646 * -----------------------------------
1648 * This routine tries to compute the optimal fragmentation pattern based
1649 * on each link's latency, bandwidth, and calculated additional latency.
1650 * The latter quantity is the additional latency caused by previously
1651 * written data that has not been transmitted yet.
1653 * This algorithm is only useful when not all of the links have the
1654 * same latency and bandwidth values.
1656 * The essential idea is to make the last bit of each fragment of the
1657 * frame arrive at the opposite end at the exact same time. This greedy
1658 * algorithm is optimal, in that no other scheduling could result in any
1659 * packet arriving any sooner unless packets are delivered out of order.
1661 * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and
1662 * latency l_i (in miliseconds). Consider the function function f_i(t)
1663 * which is equal to the number of bytes that will have arrived at
1664 * the peer after t miliseconds if we start writing continuously at
1665 * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i).
1666 * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i).
1667 * Note that the y-intersect is always <= zero because latency can't be
1668 * negative. Note also that really the function is f_i(t) except when
1669 * f_i(t) is negative, in which case the function is zero. To take
1670 * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }.
1671 * So the actual number of bytes that will have arrived at the peer after
1672 * t miliseconds is f_i(t) * Q_i(t).
1674 * At any given time, each link has some additional latency a_i >= 0
1675 * due to previously written fragment(s) which are still in the queue.
1676 * This value is easily computed from the time since last transmission,
1677 * the previous latency value, the number of bytes written, and the
1680 * Assume that l_i includes any a_i already, and that the links are
1681 * sorted by latency, so that l_i <= l_{i+1}.
1683 * Let N be the total number of bytes in the current frame we are sending.
1685 * Suppose we were to start writing bytes at time t = 0 on all links
1686 * simultaneously, which is the most we can possibly do. Then let
1687 * F(t) be equal to the total number of bytes received by the peer
1688 * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)).
1690 * Our goal is simply this: fragment the frame across the links such
1691 * that the peer is able to reconstruct the completed frame as soon as
1692 * possible, i.e., at the least possible value of t. Call this value t_0.
1694 * Then it follows that F(t_0) = N. Our strategy is first to find the value
1695 * of t_0, and then deduce how many bytes to write to each link.
1699 * t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) )
1701 * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will
1702 * lie in one of these ranges. To find it, we just need to find the i such
1703 * that F(l_i) <= N <= F(l_{i+1}). Then we compute all the constant values
1704 * for Q_i() in this range, plug in the remaining values, solving for t_0.
1706 * Once t_0 is known, then the number of bytes to send on link i is
1707 * just f_i(t_0) * Q_i(t_0).
1709 * In other words, we start allocating bytes to the links one at a time.
1710 * We keep adding links until the frame is completely sent. Some links
1711 * may not get any bytes because their latency is too high.
1713 * Is all this work really worth the trouble? Depends on the situation.
1714 * The bigger the ratio of computer speed to link speed, and the more
1715 * important total bundle latency is (e.g., for interactive response time),
1716 * the more it's worth it. There is however the cost of calling this
1717 * function for every frame. The running time is O(n^2) where n is the
1718 * number of links that receive a non-zero number of bytes.
1720 * Since latency is measured in miliseconds, the "resolution" of this
1721 * algorithm is one milisecond.
1723 * To avoid this algorithm altogether, configure all links to have the
1724 * same latency and bandwidth.
1727 ng_ppp_mp_strategy(node_p node, int len, int *distrib)
1729 const priv_p priv = NG_NODE_PRIVATE(node);
1730 int latency[NG_PPP_MAX_LINKS];
1731 int sortByLatency[NG_PPP_MAX_LINKS];
1733 int t0, total, topSum, botSum;
1735 int i, numFragments;
1737 /* If only one link, this gets real easy */
1738 if (priv->numActiveLinks == 1) {
1743 /* Get current time */
1744 getmicrouptime(&now);
1746 /* Compute latencies for each link at this point in time */
1747 for (activeLinkNum = 0;
1748 activeLinkNum < priv->numActiveLinks; activeLinkNum++) {
1749 struct ng_ppp_link *alink;
1750 struct timeval diff;
1753 /* Start with base latency value */
1754 alink = &priv->links[priv->activeLinks[activeLinkNum]];
1755 latency[activeLinkNum] = alink->conf.latency;
1756 sortByLatency[activeLinkNum] = activeLinkNum; /* see below */
1758 /* Any additional latency? */
1759 if (alink->bytesInQueue == 0)
1762 /* Compute time delta since last write */
1764 timevalsub(&diff, &alink->lastWrite);
1765 if (now.tv_sec < 0 || diff.tv_sec >= 10) { /* sanity */
1766 alink->bytesInQueue = 0;
1770 /* How many bytes could have transmitted since last write? */
1771 xmitBytes = (alink->conf.bandwidth * diff.tv_sec)
1772 + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100;
1773 alink->bytesInQueue -= xmitBytes;
1774 if (alink->bytesInQueue < 0)
1775 alink->bytesInQueue = 0;
1777 latency[activeLinkNum] +=
1778 (100 * alink->bytesInQueue) / alink->conf.bandwidth;
1781 /* Sort active links by latency */
1782 compareLatencies = latency;
1783 qsort(sortByLatency,
1784 priv->numActiveLinks, sizeof(*sortByLatency), ng_ppp_intcmp);
1785 compareLatencies = NULL;
1787 /* Find the interval we need (add links in sortByLatency[] order) */
1788 for (numFragments = 1;
1789 numFragments < priv->numActiveLinks; numFragments++) {
1790 for (total = i = 0; i < numFragments; i++) {
1793 flowTime = latency[sortByLatency[numFragments]]
1794 - latency[sortByLatency[i]];
1795 total += ((flowTime * priv->links[
1796 priv->activeLinks[sortByLatency[i]]].conf.bandwidth)
1803 /* Solve for t_0 in that interval */
1804 for (topSum = botSum = i = 0; i < numFragments; i++) {
1805 int bw = priv->links[
1806 priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
1808 topSum += latency[sortByLatency[i]] * bw; /* / 100 */
1809 botSum += bw; /* / 100 */
1811 t0 = ((len * 100) + topSum + botSum / 2) / botSum;
1813 /* Compute f_i(t_0) all i */
1814 bzero(distrib, priv->numActiveLinks * sizeof(*distrib));
1815 for (total = i = 0; i < numFragments; i++) {
1816 int bw = priv->links[
1817 priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
1819 distrib[sortByLatency[i]] =
1820 (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100;
1821 total += distrib[sortByLatency[i]];
1824 /* Deal with any rounding error */
1826 struct ng_ppp_link *fastLink =
1827 &priv->links[priv->activeLinks[sortByLatency[0]]];
1830 /* Find the fastest link */
1831 for (i = 1; i < numFragments; i++) {
1832 struct ng_ppp_link *const link =
1833 &priv->links[priv->activeLinks[sortByLatency[i]]];
1835 if (link->conf.bandwidth > fastLink->conf.bandwidth) {
1840 distrib[sortByLatency[fast]] += len - total;
1841 } else while (total > len) {
1842 struct ng_ppp_link *slowLink =
1843 &priv->links[priv->activeLinks[sortByLatency[0]]];
1844 int delta, slow = 0;
1846 /* Find the slowest link that still has bytes to remove */
1847 for (i = 1; i < numFragments; i++) {
1848 struct ng_ppp_link *const link =
1849 &priv->links[priv->activeLinks[sortByLatency[i]]];
1851 if (distrib[sortByLatency[slow]] == 0
1852 || (distrib[sortByLatency[i]] > 0
1853 && link->conf.bandwidth <
1854 slowLink->conf.bandwidth)) {
1859 delta = total - len;
1860 if (delta > distrib[sortByLatency[slow]])
1861 delta = distrib[sortByLatency[slow]];
1862 distrib[sortByLatency[slow]] -= delta;
1868 * Compare two integers
1871 ng_ppp_intcmp(const void *v1, const void *v2)
1873 const int index1 = *((const int *) v1);
1874 const int index2 = *((const int *) v2);
1876 return compareLatencies[index1] - compareLatencies[index2];
1880 * Prepend a possibly compressed PPP protocol number in front of a frame
1882 static struct mbuf *
1883 ng_ppp_addproto(struct mbuf *m, int proto, int compOK)
1885 if (compOK && PROT_COMPRESSABLE(proto)) {
1886 u_char pbyte = (u_char)proto;
1888 return ng_ppp_prepend(m, &pbyte, 1);
1890 u_int16_t pword = htons((u_int16_t)proto);
1892 return ng_ppp_prepend(m, &pword, 2);
1897 * Prepend some bytes to an mbuf
1899 static struct mbuf *
1900 ng_ppp_prepend(struct mbuf *m, const void *buf, int len)
1902 M_PREPEND(m, len, M_NOWAIT);
1903 if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL))
1905 bcopy(buf, mtod(m, u_char *), len);
1910 * Update private information that is derived from other private information
1913 ng_ppp_update(node_p node, int newConf)
1915 const priv_p priv = NG_NODE_PRIVATE(node);
1918 /* Update active status for VJ Compression */
1919 priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL
1920 && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL
1921 && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL
1922 && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL;
1924 /* Increase latency for each link an amount equal to one MP header */
1926 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1929 hdrBytes = (priv->links[i].conf.enableACFComp ? 0 : 2)
1930 + (priv->links[i].conf.enableProtoComp ? 1 : 2)
1931 + (priv->conf.xmitShortSeq ? 2 : 4);
1932 priv->links[i].conf.latency +=
1933 ((hdrBytes * priv->links[i].conf.bandwidth) + 50)
1938 /* Update list of active links */
1939 bzero(&priv->activeLinks, sizeof(priv->activeLinks));
1940 priv->numActiveLinks = 0;
1941 priv->allLinksEqual = 1;
1942 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1943 struct ng_ppp_link *const link = &priv->links[i];
1945 /* Is link active? */
1946 if (link->conf.enableLink && link->hook != NULL) {
1947 struct ng_ppp_link *link0;
1949 /* Add link to list of active links */
1950 priv->activeLinks[priv->numActiveLinks++] = i;
1951 link0 = &priv->links[priv->activeLinks[0]];
1953 /* Determine if all links are still equal */
1954 if (link->conf.latency != link0->conf.latency
1955 || link->conf.bandwidth != link0->conf.bandwidth)
1956 priv->allLinksEqual = 0;
1958 /* Initialize rec'd sequence number */
1959 if (link->seq == MP_NOSEQ) {
1960 link->seq = (link == link0) ?
1961 MP_INITIAL_SEQ : link0->seq;
1964 link->seq = MP_NOSEQ;
1967 /* Update MP state as multi-link is active or not */
1968 if (priv->conf.enableMultilink && priv->numActiveLinks > 0)
1969 ng_ppp_start_frag_timer(node);
1971 ng_ppp_stop_frag_timer(node);
1972 ng_ppp_frag_reset(node);
1973 priv->xseq = MP_INITIAL_SEQ;
1974 priv->mseq = MP_INITIAL_SEQ;
1975 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1976 struct ng_ppp_link *const link = &priv->links[i];
1978 bzero(&link->lastWrite, sizeof(link->lastWrite));
1979 link->bytesInQueue = 0;
1980 link->seq = MP_NOSEQ;
1986 * Determine if a new configuration would represent a valid change
1987 * from the current configuration and link activity status.
1990 ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf)
1992 const priv_p priv = NG_NODE_PRIVATE(node);
1993 int i, newNumLinksActive;
1995 /* Check per-link config and count how many links would be active */
1996 for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) {
1997 if (newConf->links[i].enableLink && priv->links[i].hook != NULL)
1998 newNumLinksActive++;
1999 if (!newConf->links[i].enableLink)
2001 if (newConf->links[i].mru < MP_MIN_LINK_MRU)
2003 if (newConf->links[i].bandwidth == 0)
2005 if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH)
2007 if (newConf->links[i].latency > NG_PPP_MAX_LATENCY)
2011 /* Check bundle parameters */
2012 if (newConf->bund.enableMultilink && newConf->bund.mrru < MP_MIN_MRRU)
2015 /* Disallow changes to multi-link configuration while MP is active */
2016 if (priv->numActiveLinks > 0 && newNumLinksActive > 0) {
2017 if (!priv->conf.enableMultilink
2018 != !newConf->bund.enableMultilink
2019 || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq
2020 || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq)
2024 /* At most one link can be active unless multi-link is enabled */
2025 if (!newConf->bund.enableMultilink && newNumLinksActive > 1)
2028 /* Configuration change would be valid */
2033 * Free all entries in the fragment queue
2036 ng_ppp_frag_reset(node_p node)
2038 const priv_p priv = NG_NODE_PRIVATE(node);
2039 struct ng_ppp_frag *qent, *qnext;
2041 for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) {
2042 qnext = TAILQ_NEXT(qent, f_qent);
2043 NG_FREE_M(qent->data);
2044 NG_FREE_META(qent->meta);
2045 FREE(qent, M_NETGRAPH_PPP);
2047 TAILQ_INIT(&priv->frags);
2052 * Start fragment queue timer
2055 ng_ppp_start_frag_timer(node_p node)
2057 const priv_p priv = NG_NODE_PRIVATE(node);
2059 if (!priv->timerActive) {
2060 priv->fragTimer = timeout(ng_ppp_frag_timeout,
2061 node, MP_FRAGTIMER_INTERVAL);
2062 priv->timerActive = 1;
2068 * Stop fragment queue timer
2071 ng_ppp_stop_frag_timer(node_p node)
2073 const priv_p priv = NG_NODE_PRIVATE(node);
2075 if (priv->timerActive) {
2076 untimeout(ng_ppp_frag_timeout, node, priv->fragTimer);
2077 priv->timerActive = 0;
2078 KASSERT(node->nd_refs > 1,
2079 ("%s: nd_refs=%d", __func__, node->nd_refs));
2080 NG_NODE_UNREF(node);