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 ((priv)->conf.recvShortSeq ? \
123 MP_SHORT_EXTEND((seq) + 1) : \
124 MP_LONG_EXTEND((seq) + 1))
126 /* Don't fragment transmitted packets smaller than this */
127 #define MP_MIN_FRAG_LEN 6
129 /* Maximum fragment reasssembly queue length */
130 #define MP_MAX_QUEUE_LEN 128
132 /* Fragment queue scanner period */
133 #define MP_FRAGTIMER_INTERVAL (hz/2)
135 /* We store incoming fragments this way */
137 int seq; /* fragment seq# */
138 u_char first; /* First in packet? */
139 u_char last; /* Last in packet? */
140 struct timeval timestamp; /* time of reception */
141 struct mbuf *data; /* Fragment data */
142 meta_p meta; /* Fragment meta */
143 TAILQ_ENTRY(ng_ppp_frag) f_qent; /* Fragment queue */
146 /* We use integer indicies to refer to the non-link hooks */
147 static const char *const ng_ppp_hook_names[] = {
149 #define HOOK_INDEX_ATALK 0
151 #define HOOK_INDEX_BYPASS 1
152 NG_PPP_HOOK_COMPRESS,
153 #define HOOK_INDEX_COMPRESS 2
155 #define HOOK_INDEX_ENCRYPT 3
156 NG_PPP_HOOK_DECOMPRESS,
157 #define HOOK_INDEX_DECOMPRESS 4
159 #define HOOK_INDEX_DECRYPT 5
161 #define HOOK_INDEX_INET 6
163 #define HOOK_INDEX_IPX 7
164 NG_PPP_HOOK_VJC_COMP,
165 #define HOOK_INDEX_VJC_COMP 8
167 #define HOOK_INDEX_VJC_IP 9
168 NG_PPP_HOOK_VJC_UNCOMP,
169 #define HOOK_INDEX_VJC_UNCOMP 10
170 NG_PPP_HOOK_VJC_VJIP,
171 #define HOOK_INDEX_VJC_VJIP 11
173 #define HOOK_INDEX_IPV6 12
175 #define HOOK_INDEX_MAX 13
178 /* We store index numbers in the hook private pointer. The HOOK_INDEX()
179 for a hook is either the index (above) for normal hooks, or the ones
180 complement of the link number for link hooks.
181 XXX Not any more.. (what a hack)
182 #define HOOK_INDEX(hook) (*((int16_t *) &(hook)->private))
185 /* Per-link private information */
187 struct ng_ppp_link_conf conf; /* link configuration */
188 hook_p hook; /* connection to link data */
189 int32_t seq; /* highest rec'd seq# - MSEQ */
190 struct timeval lastWrite; /* time of last write */
191 int bytesInQueue; /* bytes in the output queue */
192 struct ng_ppp_link_stat stats; /* Link stats */
195 /* Total per-node private information */
196 struct ng_ppp_private {
197 struct ng_ppp_bund_conf conf; /* bundle config */
198 struct ng_ppp_link_stat bundleStats; /* bundle stats */
199 struct ng_ppp_link links[NG_PPP_MAX_LINKS];/* per-link info */
200 int32_t xseq; /* next out MP seq # */
201 int32_t mseq; /* min links[i].seq */
202 u_char vjCompHooked; /* VJ comp hooked up? */
203 u_char allLinksEqual; /* all xmit the same? */
204 u_char timerActive; /* frag timer active? */
205 u_int numActiveLinks; /* how many links up */
206 int activeLinks[NG_PPP_MAX_LINKS]; /* indicies */
207 u_int lastLink; /* for round robin */
208 hook_p hooks[HOOK_INDEX_MAX]; /* non-link hooks */
209 TAILQ_HEAD(ng_ppp_fraglist, ng_ppp_frag) /* fragment queue */
211 int qlen; /* fraq queue length */
212 struct callout_handle fragTimer; /* fraq queue check */
214 typedef struct ng_ppp_private *priv_p;
216 /* Netgraph node methods */
217 static ng_constructor_t ng_ppp_constructor;
218 static ng_rcvmsg_t ng_ppp_rcvmsg;
219 static ng_shutdown_t ng_ppp_shutdown;
220 static ng_newhook_t ng_ppp_newhook;
221 static ng_rcvdata_t ng_ppp_rcvdata;
222 static ng_disconnect_t ng_ppp_disconnect;
224 /* Helper functions */
225 static int ng_ppp_input(node_p node, int bypass,
226 int linkNum, item_p item);
227 static int ng_ppp_output(node_p node, int bypass, int proto,
228 int linkNum, item_p item);
229 static int ng_ppp_mp_input(node_p node, int linkNum, item_p item);
230 static int ng_ppp_check_packet(node_p node);
231 static void ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap);
232 static int ng_ppp_frag_process(node_p node);
233 static int ng_ppp_frag_trim(node_p node);
234 static void ng_ppp_frag_timeout(void *arg);
235 static void ng_ppp_frag_checkstale(node_p node);
236 static void ng_ppp_frag_reset(node_p node);
237 static int ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta);
238 static void ng_ppp_mp_strategy(node_p node, int len, int *distrib);
239 static int ng_ppp_intcmp(const void *v1, const void *v2);
240 static struct mbuf *ng_ppp_addproto(struct mbuf *m, int proto, int compOK);
241 static struct mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len);
242 static int ng_ppp_config_valid(node_p node,
243 const struct ng_ppp_node_conf *newConf);
244 static void ng_ppp_update(node_p node, int newConf);
245 static void ng_ppp_start_frag_timer(node_p node);
246 static void ng_ppp_stop_frag_timer(node_p node);
248 /* Parse type for struct ng_ppp_mp_state_type */
249 static const struct ng_parse_fixedarray_info ng_ppp_rseq_array_info = {
250 &ng_parse_hint32_type,
253 static const struct ng_parse_type ng_ppp_rseq_array_type = {
254 &ng_parse_fixedarray_type,
255 &ng_ppp_rseq_array_info,
257 static const struct ng_parse_struct_field ng_ppp_mp_state_type_fields[]
258 = NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type);
259 static const struct ng_parse_type ng_ppp_mp_state_type = {
260 &ng_parse_struct_type,
261 &ng_ppp_mp_state_type_fields
264 /* Parse type for struct ng_ppp_link_conf */
265 static const struct ng_parse_struct_field ng_ppp_link_type_fields[]
266 = NG_PPP_LINK_TYPE_INFO;
267 static const struct ng_parse_type ng_ppp_link_type = {
268 &ng_parse_struct_type,
269 &ng_ppp_link_type_fields
272 /* Parse type for struct ng_ppp_bund_conf */
273 static const struct ng_parse_struct_field ng_ppp_bund_type_fields[]
274 = NG_PPP_BUND_TYPE_INFO;
275 static const struct ng_parse_type ng_ppp_bund_type = {
276 &ng_parse_struct_type,
277 &ng_ppp_bund_type_fields
280 /* Parse type for struct ng_ppp_node_conf */
281 static const struct ng_parse_fixedarray_info ng_ppp_array_info = {
285 static const struct ng_parse_type ng_ppp_link_array_type = {
286 &ng_parse_fixedarray_type,
289 static const struct ng_parse_struct_field ng_ppp_conf_type_fields[]
290 = NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type);
291 static const struct ng_parse_type ng_ppp_conf_type = {
292 &ng_parse_struct_type,
293 &ng_ppp_conf_type_fields
296 /* Parse type for struct ng_ppp_link_stat */
297 static const struct ng_parse_struct_field ng_ppp_stats_type_fields[]
298 = NG_PPP_STATS_TYPE_INFO;
299 static const struct ng_parse_type ng_ppp_stats_type = {
300 &ng_parse_struct_type,
301 &ng_ppp_stats_type_fields
304 /* List of commands and how to convert arguments to/from ASCII */
305 static const struct ng_cmdlist ng_ppp_cmds[] = {
322 NGM_PPP_GET_MP_STATE,
325 &ng_ppp_mp_state_type
329 NGM_PPP_GET_LINK_STATS,
331 &ng_parse_int16_type,
336 NGM_PPP_CLR_LINK_STATS,
338 &ng_parse_int16_type,
343 NGM_PPP_GETCLR_LINK_STATS,
345 &ng_parse_int16_type,
351 /* Node type descriptor */
352 static struct ng_type ng_ppp_typestruct = {
366 NETGRAPH_INIT(ppp, &ng_ppp_typestruct);
368 static int *compareLatencies; /* hack for ng_ppp_intcmp() */
370 /* Address and control field header */
371 static const u_char ng_ppp_acf[2] = { 0xff, 0x03 };
373 /* Maximum time we'll let a complete incoming packet sit in the queue */
374 static const struct timeval ng_ppp_max_staleness = { 2, 0 }; /* 2 seconds */
376 #define ERROUT(x) do { error = (x); goto done; } while (0)
378 /************************************************************************
380 ************************************************************************/
383 * Node type constructor
386 ng_ppp_constructor(node_p node)
391 /* Allocate private structure */
392 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_PPP, M_NOWAIT | M_ZERO);
396 NG_NODE_SET_PRIVATE(node, priv);
398 /* Initialize state */
399 TAILQ_INIT(&priv->frags);
400 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
401 priv->links[i].seq = MP_NOSEQ;
402 callout_handle_init(&priv->fragTimer);
409 * Give our OK for a hook to be added
412 ng_ppp_newhook(node_p node, hook_p hook, const char *name)
414 const priv_p priv = NG_NODE_PRIVATE(node);
416 hook_p *hookPtr = NULL;
419 /* Figure out which hook it is */
420 if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX, /* a link hook? */
421 strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) {
425 cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
426 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
428 linkNum = (int)strtoul(cp, &eptr, 10);
429 if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS)
431 hookPtr = &priv->links[linkNum].hook;
432 hookIndex = ~linkNum;
433 } else { /* must be a non-link hook */
436 for (i = 0; ng_ppp_hook_names[i] != NULL; i++) {
437 if (strcmp(name, ng_ppp_hook_names[i]) == 0) {
438 hookPtr = &priv->hooks[i];
443 if (ng_ppp_hook_names[i] == NULL)
444 return (EINVAL); /* no such hook */
447 /* See if hook is already connected */
448 if (*hookPtr != NULL)
451 /* Disallow more than one link unless multilink is enabled */
452 if (linkNum != -1 && priv->links[linkNum].conf.enableLink
453 && !priv->conf.enableMultilink && priv->numActiveLinks >= 1)
458 NG_HOOK_SET_PRIVATE(hook, (void *)hookIndex);
459 ng_ppp_update(node, 0);
464 * Receive a control message
467 ng_ppp_rcvmsg(node_p node, item_p item, hook_p lasthook)
469 const priv_p priv = NG_NODE_PRIVATE(node);
470 struct ng_mesg *resp = NULL;
474 NGI_GET_MSG(item, msg);
475 switch (msg->header.typecookie) {
477 switch (msg->header.cmd) {
478 case NGM_PPP_SET_CONFIG:
480 struct ng_ppp_node_conf *const conf =
481 (struct ng_ppp_node_conf *)msg->data;
484 /* Check for invalid or illegal config */
485 if (msg->header.arglen != sizeof(*conf))
487 if (!ng_ppp_config_valid(node, conf))
491 priv->conf = conf->bund;
492 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
493 priv->links[i].conf = conf->links[i];
494 ng_ppp_update(node, 1);
497 case NGM_PPP_GET_CONFIG:
499 struct ng_ppp_node_conf *conf;
502 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
505 conf = (struct ng_ppp_node_conf *)resp->data;
506 conf->bund = priv->conf;
507 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
508 conf->links[i] = priv->links[i].conf;
511 case NGM_PPP_GET_MP_STATE:
513 struct ng_ppp_mp_state *info;
516 NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT);
519 info = (struct ng_ppp_mp_state *)resp->data;
520 bzero(info, sizeof(*info));
521 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
522 if (priv->links[i].seq != MP_NOSEQ)
523 info->rseq[i] = priv->links[i].seq;
525 info->mseq = priv->mseq;
526 info->xseq = priv->xseq;
529 case NGM_PPP_GET_LINK_STATS:
530 case NGM_PPP_CLR_LINK_STATS:
531 case NGM_PPP_GETCLR_LINK_STATS:
533 struct ng_ppp_link_stat *stats;
536 if (msg->header.arglen != sizeof(u_int16_t))
538 linkNum = *((u_int16_t *) msg->data);
539 if (linkNum >= NG_PPP_MAX_LINKS
540 && linkNum != NG_PPP_BUNDLE_LINKNUM)
542 stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ?
543 &priv->bundleStats : &priv->links[linkNum].stats;
544 if (msg->header.cmd != NGM_PPP_CLR_LINK_STATS) {
545 NG_MKRESPONSE(resp, msg,
546 sizeof(struct ng_ppp_link_stat), M_NOWAIT);
549 bcopy(stats, resp->data, sizeof(*stats));
551 if (msg->header.cmd != NGM_PPP_GET_LINK_STATS)
552 bzero(stats, sizeof(*stats));
563 * Forward it to the vjc node. leave the
564 * old return address alone.
565 * If we have no hook, let NG_RESPOND_MSG
566 * clean up any remaining resources.
567 * Because we have no resp, the item will be freed
568 * along with anything it references. Don't
569 * let msg be freed twice.
571 NGI_MSG(item) = msg; /* put it back in the item */
573 if ((lasthook = priv->links[HOOK_INDEX_VJC_IP].hook)) {
574 NG_FWD_ITEM_HOOK(error, item, lasthook);
583 NG_RESPOND_MSG(error, node, item, resp);
589 * Receive data on a hook
592 ng_ppp_rcvdata(hook_p hook, item_p item)
594 const node_p node = NG_HOOK_NODE(hook);
595 const priv_p priv = NG_NODE_PRIVATE(node);
596 const int index = (int)NG_HOOK_PRIVATE(hook);
597 u_int16_t linkNum = NG_PPP_BUNDLE_LINKNUM;
598 hook_p outHook = NULL;
599 int proto = 0, error;
603 /* Did it come from a link hook? */
605 struct ng_ppp_link *link;
607 /* Convert index into a link number */
608 linkNum = (u_int16_t)~index;
609 KASSERT(linkNum < NG_PPP_MAX_LINKS,
610 ("%s: bogus index 0x%x", __func__, index));
611 link = &priv->links[linkNum];
614 link->stats.recvFrames++;
615 link->stats.recvOctets += m->m_pkthdr.len;
617 /* Strip address and control fields, if present */
618 if (m->m_pkthdr.len >= 2) {
619 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
623 if (bcmp(mtod(m, u_char *), &ng_ppp_acf, 2) == 0)
627 /* Dispatch incoming frame (if not enabled, to bypass) */
628 NGI_M(item) = m; /* put changed m back in item */
629 return ng_ppp_input(node,
630 !link->conf.enableLink, linkNum, item);
633 /* Get protocol & check if data allowed from this hook */
634 NGI_M(item) = m; /* put possibly changed m back in item */
638 case HOOK_INDEX_ATALK:
639 if (!priv->conf.enableAtalk) {
643 proto = PROT_APPLETALK;
646 if (!priv->conf.enableIPX) {
652 case HOOK_INDEX_IPV6:
653 if (!priv->conf.enableIPv6) {
659 case HOOK_INDEX_INET:
660 case HOOK_INDEX_VJC_VJIP:
661 if (!priv->conf.enableIP) {
667 case HOOK_INDEX_VJC_COMP:
668 if (!priv->conf.enableVJCompression) {
674 case HOOK_INDEX_VJC_UNCOMP:
675 if (!priv->conf.enableVJCompression) {
679 proto = PROT_VJUNCOMP;
681 case HOOK_INDEX_COMPRESS:
682 if (!priv->conf.enableCompression) {
688 case HOOK_INDEX_ENCRYPT:
689 if (!priv->conf.enableEncryption) {
695 case HOOK_INDEX_BYPASS:
696 if (m->m_pkthdr.len < 4) {
700 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
701 NGI_M(item) = NULL; /* don't free twice */
705 NGI_M(item) = m; /* m may have changed */
706 linkNum = ntohs(mtod(m, u_int16_t *)[0]);
707 proto = ntohs(mtod(m, u_int16_t *)[1]);
709 if (linkNum >= NG_PPP_MAX_LINKS
710 && linkNum != NG_PPP_BUNDLE_LINKNUM) {
717 case HOOK_INDEX_VJC_IP:
718 if (!priv->conf.enableIP || !priv->conf.enableVJDecompression) {
723 case HOOK_INDEX_DECOMPRESS:
724 if (!priv->conf.enableDecompression) {
729 case HOOK_INDEX_DECRYPT:
730 if (!priv->conf.enableDecryption) {
736 panic("%s: bogus index 0x%x", __func__, index);
739 /* Now figure out what to do with the frame */
743 case HOOK_INDEX_INET:
744 if (priv->conf.enableVJCompression && priv->vjCompHooked) {
745 outHook = priv->hooks[HOOK_INDEX_VJC_IP];
749 case HOOK_INDEX_ATALK:
750 case HOOK_INDEX_IPV6:
752 case HOOK_INDEX_VJC_COMP:
753 case HOOK_INDEX_VJC_UNCOMP:
754 case HOOK_INDEX_VJC_VJIP:
755 if (priv->conf.enableCompression
756 && priv->hooks[HOOK_INDEX_COMPRESS] != NULL) {
757 if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
762 NGI_M(item) = m; /* m may have changed */
763 outHook = priv->hooks[HOOK_INDEX_COMPRESS];
767 case HOOK_INDEX_COMPRESS:
768 if (priv->conf.enableEncryption
769 && priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) {
770 if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
775 NGI_M(item) = m; /* m may have changed */
776 outHook = priv->hooks[HOOK_INDEX_ENCRYPT];
780 case HOOK_INDEX_ENCRYPT:
781 return ng_ppp_output(node, 0, proto, NG_PPP_BUNDLE_LINKNUM, item);
783 case HOOK_INDEX_BYPASS:
784 return ng_ppp_output(node, 1, proto, linkNum, item);
787 case HOOK_INDEX_DECRYPT:
788 case HOOK_INDEX_DECOMPRESS:
789 return ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
791 case HOOK_INDEX_VJC_IP:
792 outHook = priv->hooks[HOOK_INDEX_INET];
796 /* Send packet out hook */
797 NG_FWD_ITEM_HOOK(error, item, outHook);
805 ng_ppp_shutdown(node_p node)
807 const priv_p priv = NG_NODE_PRIVATE(node);
809 /* Stop fragment queue timer */
810 ng_ppp_stop_frag_timer(node);
812 /* Take down netgraph node */
813 ng_ppp_frag_reset(node);
814 bzero(priv, sizeof(*priv));
815 FREE(priv, M_NETGRAPH_PPP);
816 NG_NODE_SET_PRIVATE(node, NULL);
817 NG_NODE_UNREF(node); /* let the node escape */
825 ng_ppp_disconnect(hook_p hook)
827 const node_p node = NG_HOOK_NODE(hook);
828 const priv_p priv = NG_NODE_PRIVATE(node);
829 const int index = (int)NG_HOOK_PRIVATE(hook);
831 /* Zero out hook pointer */
833 priv->links[~index].hook = NULL;
835 priv->hooks[index] = NULL;
837 /* Update derived info (or go away if no hooks left) */
838 if (NG_NODE_NUMHOOKS(node) > 0) {
839 ng_ppp_update(node, 0);
841 if (NG_NODE_IS_VALID(node)) {
842 ng_rmnode_self(node);
848 /************************************************************************
850 ************************************************************************/
853 * Handle an incoming frame. Extract the PPP protocol number
854 * and dispatch accordingly.
857 ng_ppp_input(node_p node, int bypass, int linkNum, item_p item)
859 const priv_p priv = NG_NODE_PRIVATE(node);
860 hook_p outHook = NULL;
866 /* Extract protocol number */
867 for (proto = 0; !PROT_VALID(proto) && m->m_pkthdr.len > 0; ) {
868 if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) {
872 proto = (proto << 8) + *mtod(m, u_char *);
875 if (!PROT_VALID(proto)) {
876 if (linkNum == NG_PPP_BUNDLE_LINKNUM)
877 priv->bundleStats.badProtos++;
879 priv->links[linkNum].stats.badProtos++;
892 if (priv->conf.enableDecompression)
893 outHook = priv->hooks[HOOK_INDEX_DECOMPRESS];
896 if (priv->conf.enableDecryption)
897 outHook = priv->hooks[HOOK_INDEX_DECRYPT];
900 if (priv->conf.enableVJDecompression && priv->vjCompHooked)
901 outHook = priv->hooks[HOOK_INDEX_VJC_COMP];
904 if (priv->conf.enableVJDecompression && priv->vjCompHooked)
905 outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP];
908 if (priv->conf.enableMultilink
909 && linkNum != NG_PPP_BUNDLE_LINKNUM) {
911 return ng_ppp_mp_input(node, linkNum, item);
915 if (priv->conf.enableAtalk)
916 outHook = priv->hooks[HOOK_INDEX_ATALK];
919 if (priv->conf.enableIPX)
920 outHook = priv->hooks[HOOK_INDEX_IPX];
923 if (priv->conf.enableIP)
924 outHook = priv->hooks[HOOK_INDEX_INET];
927 if (priv->conf.enableIPv6)
928 outHook = priv->hooks[HOOK_INDEX_IPV6];
933 /* For unknown/inactive protocols, forward out the bypass hook */
934 if (outHook == NULL) {
937 hdr[0] = htons(linkNum);
938 hdr[1] = htons((u_int16_t)proto);
939 if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) {
943 outHook = priv->hooks[HOOK_INDEX_BYPASS];
947 NG_FWD_NEW_DATA(error, item, outHook, m);
952 * Deliver a frame out a link, either a real one or NG_PPP_BUNDLE_LINKNUM.
953 * If the link is not enabled then ENXIO is returned, unless "bypass" is != 0.
955 * If the frame is too big for the particular link, return EMSGSIZE.
958 ng_ppp_output(node_p node, int bypass,
959 int proto, int linkNum, item_p item)
961 const priv_p priv = NG_NODE_PRIVATE(node);
962 struct ng_ppp_link *link;
970 /* If not doing MP, map bundle virtual link to (the only) link */
971 if (linkNum == NG_PPP_BUNDLE_LINKNUM && !priv->conf.enableMultilink)
972 linkNum = priv->activeLinks[0];
974 /* Get link pointer (optimization) */
975 link = (linkNum != NG_PPP_BUNDLE_LINKNUM) ?
976 &priv->links[linkNum] : NULL;
978 /* Check link status (if real) */
979 if (linkNum != NG_PPP_BUNDLE_LINKNUM) {
980 if (!bypass && !link->conf.enableLink) {
985 if (link->hook == NULL) {
992 /* Check peer's MRU for this link */
993 mru = (link != NULL) ? link->conf.mru : priv->conf.mrru;
994 if (mru != 0 && m->m_pkthdr.len > mru) {
1000 /* Prepend protocol number, possibly compressed */
1001 if ((m = ng_ppp_addproto(m, proto,
1002 linkNum == NG_PPP_BUNDLE_LINKNUM
1003 || link->conf.enableProtoComp)) == NULL) {
1008 /* Special handling for the MP virtual link */
1009 if (linkNum == NG_PPP_BUNDLE_LINKNUM) {
1012 /* strip off and discard the queue item */
1013 NGI_GET_META(item, meta);
1015 return ng_ppp_mp_output(node, m, meta);
1018 /* Prepend address and control field (unless compressed) */
1019 if (proto == PROT_LCP || !link->conf.enableACFComp) {
1020 if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) {
1027 len = m->m_pkthdr.len;
1028 NG_FWD_NEW_DATA(error, item, link->hook, m);
1030 /* Update stats and 'bytes in queue' counter */
1032 link->stats.xmitFrames++;
1033 link->stats.xmitOctets += len;
1034 link->bytesInQueue += len;
1035 getmicrouptime(&link->lastWrite);
1041 * Handle an incoming multi-link fragment
1043 * The fragment reassembly algorithm is somewhat complex. This is mainly
1044 * because we are required not to reorder the reconstructed packets, yet
1045 * fragments are only guaranteed to arrive in order on a per-link basis.
1046 * In other words, when we have a complete packet ready, but the previous
1047 * packet is still incomplete, we have to decide between delivering the
1048 * complete packet and throwing away the incomplete one, or waiting to
1049 * see if the remainder of the incomplete one arrives, at which time we
1050 * can deliver both packets, in order.
1052 * This problem is exacerbated by "sequence number slew", which is when
1053 * the sequence numbers coming in from different links are far apart from
1054 * each other. In particular, certain unnamed equipment (*cough* Ascend)
1055 * has been seen to generate sequence number slew of up to 10 on an ISDN
1056 * 2B-channel MP link. There is nothing invalid about sequence number slew
1057 * but it makes the reasssembly process have to work harder.
1059 * However, the peer is required to transmit fragments in order on each
1060 * link. That means if we define MSEQ as the minimum over all links of
1061 * the highest sequence number received on that link, then we can always
1062 * give up any hope of receiving a fragment with sequence number < MSEQ in
1063 * the future (all of this using 'wraparound' sequence number space).
1064 * Therefore we can always immediately throw away incomplete packets
1065 * missing fragments with sequence numbers < MSEQ.
1067 * Here is an overview of our algorithm:
1069 * o Received fragments are inserted into a queue, for which we
1070 * maintain these invariants between calls to this function:
1072 * - Fragments are ordered in the queue by sequence number
1073 * - If a complete packet is at the head of the queue, then
1074 * the first fragment in the packet has seq# > MSEQ + 1
1075 * (otherwise, we could deliver it immediately)
1076 * - If any fragments have seq# < MSEQ, then they are necessarily
1077 * part of a packet whose missing seq#'s are all > MSEQ (otherwise,
1078 * we can throw them away because they'll never be completed)
1079 * - The queue contains at most MP_MAX_QUEUE_LEN fragments
1081 * o We have a periodic timer that checks the queue for the first
1082 * complete packet that has been sitting in the queue "too long".
1083 * When one is detected, all previous (incomplete) fragments are
1084 * discarded, their missing fragments are declared lost and MSEQ
1087 * o If we recieve a fragment with seq# < MSEQ, we throw it away
1088 * because we've already delcared it lost.
1090 * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM.
1093 ng_ppp_mp_input(node_p node, int linkNum, item_p item)
1095 const priv_p priv = NG_NODE_PRIVATE(node);
1096 struct ng_ppp_link *const link = &priv->links[linkNum];
1097 struct ng_ppp_frag frag0, *frag = &frag0;
1098 struct ng_ppp_frag *qent;
1099 int i, diff, inserted;
1104 NGI_GET_META(item, meta);
1107 priv->bundleStats.recvFrames++;
1108 priv->bundleStats.recvOctets += m->m_pkthdr.len;
1110 /* Extract fragment information from MP header */
1111 if (priv->conf.recvShortSeq) {
1114 if (m->m_pkthdr.len < 2) {
1115 link->stats.runts++;
1120 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1124 shdr = ntohs(*mtod(m, u_int16_t *));
1125 frag->seq = MP_SHORT_EXTEND(shdr);
1126 frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0;
1127 frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0;
1128 diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq);
1133 if (m->m_pkthdr.len < 4) {
1134 link->stats.runts++;
1139 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
1143 lhdr = ntohl(*mtod(m, u_int32_t *));
1144 frag->seq = MP_LONG_EXTEND(lhdr);
1145 frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0;
1146 frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0;
1147 diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq);
1152 getmicrouptime(&frag->timestamp);
1154 /* If sequence number is < MSEQ, we've already declared this
1155 fragment as lost, so we have no choice now but to drop it */
1157 link->stats.dropFragments++;
1163 /* Update highest received sequence number on this link and MSEQ */
1164 priv->mseq = link->seq = frag->seq;
1165 for (i = 0; i < priv->numActiveLinks; i++) {
1166 struct ng_ppp_link *const alink =
1167 &priv->links[priv->activeLinks[i]];
1169 if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0)
1170 priv->mseq = alink->seq;
1173 /* Allocate a new frag struct for the queue */
1174 MALLOC(frag, struct ng_ppp_frag *, sizeof(*frag), M_NETGRAPH_PPP, M_NOWAIT);
1178 ng_ppp_frag_process(node);
1183 /* Add fragment to queue, which is sorted by sequence number */
1185 TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) {
1186 diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq);
1188 TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent);
1191 } else if (diff == 0) { /* should never happen! */
1192 link->stats.dupFragments++;
1193 NG_FREE_M(frag->data);
1194 NG_FREE_META(frag->meta);
1195 FREE(frag, M_NETGRAPH_PPP);
1200 TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent);
1203 /* Process the queue */
1204 return ng_ppp_frag_process(node);
1208 * Examine our list of fragments, and determine if there is a
1209 * complete and deliverable packet at the head of the list.
1210 * Return 1 if so, zero otherwise.
1213 ng_ppp_check_packet(node_p node)
1215 const priv_p priv = NG_NODE_PRIVATE(node);
1216 struct ng_ppp_frag *qent, *qnext;
1218 /* Check for empty queue */
1219 if (TAILQ_EMPTY(&priv->frags))
1222 /* Check first fragment is the start of a deliverable packet */
1223 qent = TAILQ_FIRST(&priv->frags);
1224 if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1)
1227 /* Check that all the fragments are there */
1228 while (!qent->last) {
1229 qnext = TAILQ_NEXT(qent, f_qent);
1230 if (qnext == NULL) /* end of queue */
1232 if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq))
1242 * Pull a completed packet off the head of the incoming fragment queue.
1243 * This assumes there is a completed packet there to pull off.
1246 ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap)
1248 const priv_p priv = NG_NODE_PRIVATE(node);
1249 struct ng_ppp_frag *qent, *qnext;
1250 struct mbuf *m = NULL, *tail;
1252 qent = TAILQ_FIRST(&priv->frags);
1253 KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first,
1254 ("%s: no packet", __func__));
1255 for (tail = NULL; qent != NULL; qent = qnext) {
1256 qnext = TAILQ_NEXT(qent, f_qent);
1257 KASSERT(!TAILQ_EMPTY(&priv->frags),
1258 ("%s: empty q", __func__));
1259 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1261 tail = m = qent->data;
1262 *metap = qent->meta; /* inherit first frag's meta */
1264 m->m_pkthdr.len += qent->data->m_pkthdr.len;
1265 tail->m_next = qent->data;
1266 NG_FREE_META(qent->meta); /* drop other frags' metas */
1268 while (tail->m_next != NULL)
1269 tail = tail->m_next;
1272 FREE(qent, M_NETGRAPH_PPP);
1279 * Trim fragments from the queue whose packets can never be completed.
1280 * This assumes a complete packet is NOT at the beginning of the queue.
1281 * Returns 1 if fragments were removed, zero otherwise.
1284 ng_ppp_frag_trim(node_p node)
1286 const priv_p priv = NG_NODE_PRIVATE(node);
1287 struct ng_ppp_frag *qent, *qnext = NULL;
1290 /* Scan for "dead" fragments and remove them */
1294 /* If queue is empty, we're done */
1295 if (TAILQ_EMPTY(&priv->frags))
1298 /* Determine whether first fragment can ever be completed */
1299 TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1300 if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0)
1302 qnext = TAILQ_NEXT(qent, f_qent);
1303 KASSERT(qnext != NULL,
1304 ("%s: last frag < MSEQ?", __func__));
1305 if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)
1306 || qent->last || qnext->first) {
1314 /* Remove fragment and all others in the same packet */
1315 while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) {
1316 KASSERT(!TAILQ_EMPTY(&priv->frags),
1317 ("%s: empty q", __func__));
1318 priv->bundleStats.dropFragments++;
1319 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1320 NG_FREE_M(qent->data);
1321 NG_FREE_META(qent->meta);
1322 FREE(qent, M_NETGRAPH_PPP);
1331 * Run the queue, restoring the queue invariants
1334 ng_ppp_frag_process(node_p node)
1336 const priv_p priv = NG_NODE_PRIVATE(node);
1341 /* Deliver any deliverable packets */
1342 while (ng_ppp_check_packet(node)) {
1343 ng_ppp_get_packet(node, &m, &meta);
1344 item = ng_package_data(m, meta);
1345 ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1348 /* Delete dead fragments and try again */
1349 if (ng_ppp_frag_trim(node)) {
1350 while (ng_ppp_check_packet(node)) {
1351 ng_ppp_get_packet(node, &m, &meta);
1352 item = ng_package_data(m, meta);
1353 ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1357 /* Check for stale fragments while we're here */
1358 ng_ppp_frag_checkstale(node);
1360 /* Check queue length */
1361 if (priv->qlen > MP_MAX_QUEUE_LEN) {
1362 struct ng_ppp_frag *qent;
1365 /* Get oldest fragment */
1366 KASSERT(!TAILQ_EMPTY(&priv->frags),
1367 ("%s: empty q", __func__));
1368 qent = TAILQ_FIRST(&priv->frags);
1370 /* Bump MSEQ if necessary */
1371 if (MP_RECV_SEQ_DIFF(priv, priv->mseq, qent->seq) < 0) {
1372 priv->mseq = qent->seq;
1373 for (i = 0; i < priv->numActiveLinks; i++) {
1374 struct ng_ppp_link *const alink =
1375 &priv->links[priv->activeLinks[i]];
1377 if (MP_RECV_SEQ_DIFF(priv,
1378 alink->seq, priv->mseq) < 0)
1379 alink->seq = priv->mseq;
1384 priv->bundleStats.dropFragments++;
1385 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1386 NG_FREE_M(qent->data);
1387 NG_FREE_META(qent->meta);
1388 FREE(qent, M_NETGRAPH_PPP);
1391 /* Process queue again */
1392 return ng_ppp_frag_process(node);
1400 * Check for 'stale' completed packets that need to be delivered
1402 * If a link goes down or has a temporary failure, MSEQ can get
1403 * "stuck", because no new incoming fragments appear on that link.
1404 * This can cause completed packets to never get delivered if
1405 * their sequence numbers are all > MSEQ + 1.
1407 * This routine checks how long all of the completed packets have
1408 * been sitting in the queue, and if too long, removes fragments
1409 * from the queue and increments MSEQ to allow them to be delivered.
1412 ng_ppp_frag_checkstale(node_p node)
1414 const priv_p priv = NG_NODE_PRIVATE(node);
1415 struct ng_ppp_frag *qent, *beg, *end;
1416 struct timeval now, age;
1422 now.tv_sec = 0; /* uninitialized state */
1425 /* If queue is empty, we're done */
1426 if (TAILQ_EMPTY(&priv->frags))
1429 /* Find the first complete packet in the queue */
1431 seq = TAILQ_FIRST(&priv->frags)->seq;
1432 TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1435 else if (qent->seq != seq)
1437 if (beg != NULL && qent->last) {
1441 seq = MP_NEXT_RECV_SEQ(priv, seq);
1444 /* If none found, exit */
1448 /* Get current time (we assume we've been up for >= 1 second) */
1449 if (now.tv_sec == 0)
1450 getmicrouptime(&now);
1452 /* Check if packet has been queued too long */
1454 timevalsub(&age, &beg->timestamp);
1455 if (timevalcmp(&age, &ng_ppp_max_staleness, < ))
1458 /* Throw away junk fragments in front of the completed packet */
1459 while ((qent = TAILQ_FIRST(&priv->frags)) != beg) {
1460 KASSERT(!TAILQ_EMPTY(&priv->frags),
1461 ("%s: empty q", __func__));
1462 priv->bundleStats.dropFragments++;
1463 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1464 NG_FREE_M(qent->data);
1465 NG_FREE_META(qent->meta);
1466 FREE(qent, M_NETGRAPH_PPP);
1470 /* Extract completed packet */
1471 ng_ppp_get_packet(node, &m, &meta);
1473 /* Bump MSEQ if necessary */
1474 if (MP_RECV_SEQ_DIFF(priv, priv->mseq, end->seq) < 0) {
1475 priv->mseq = end->seq;
1476 for (i = 0; i < priv->numActiveLinks; i++) {
1477 struct ng_ppp_link *const alink =
1478 &priv->links[priv->activeLinks[i]];
1480 if (MP_RECV_SEQ_DIFF(priv,
1481 alink->seq, priv->mseq) < 0)
1482 alink->seq = priv->mseq;
1486 /* Deliver packet */
1487 item = ng_package_data(m, meta);
1488 ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1493 * Periodically call ng_ppp_frag_checkstale()
1496 ng_ppp_frag_timeout(void *arg)
1498 const node_p node = arg;
1499 const priv_p priv = NG_NODE_PRIVATE(node);
1502 /* Handle the race where shutdown happens just before splnet() above */
1503 if (NG_NODE_NOT_VALID(node)) {
1504 NG_NODE_UNREF(node);
1509 /* Reset timer state after timeout */
1510 KASSERT(priv->timerActive, ("%s: !timerActive", __func__));
1511 priv->timerActive = 0;
1512 KASSERT(node->nd_refs > 1, ("%s: nd_refs=%d", __func__, node->nd_refs));
1513 NG_NODE_UNREF(node);
1515 /* Start timer again */
1516 ng_ppp_start_frag_timer(node);
1518 /* Scan the fragment queue */
1519 ng_ppp_frag_checkstale(node);
1524 * Deliver a frame out on the bundle, i.e., figure out how to fragment
1525 * the frame across the individual PPP links and do so.
1528 ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta)
1530 const priv_p priv = NG_NODE_PRIVATE(node);
1531 const int hdr_len = priv->conf.xmitShortSeq ? 2 : 4;
1532 int distrib[NG_PPP_MAX_LINKS];
1537 /* At least one link must be active */
1538 if (priv->numActiveLinks == 0) {
1544 /* Round-robin strategy */
1545 if (priv->conf.enableRoundRobin || m->m_pkthdr.len < MP_MIN_FRAG_LEN) {
1546 activeLinkNum = priv->lastLink++ % priv->numActiveLinks;
1547 bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0]));
1548 distrib[activeLinkNum] = m->m_pkthdr.len;
1552 /* Strategy when all links are equivalent (optimize the common case) */
1553 if (priv->allLinksEqual) {
1554 const int fraction = m->m_pkthdr.len / priv->numActiveLinks;
1557 for (i = 0; i < priv->numActiveLinks; i++)
1558 distrib[priv->lastLink++ % priv->numActiveLinks]
1560 remain = m->m_pkthdr.len - (fraction * priv->numActiveLinks);
1561 while (remain > 0) {
1562 distrib[priv->lastLink++ % priv->numActiveLinks]++;
1568 /* Strategy when all links are not equivalent */
1569 ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib);
1573 priv->bundleStats.xmitFrames++;
1574 priv->bundleStats.xmitOctets += m->m_pkthdr.len;
1576 /* Send alloted portions of frame out on the link(s) */
1577 for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1;
1578 activeLinkNum >= 0; activeLinkNum--) {
1579 const int linkNum = priv->activeLinks[activeLinkNum];
1580 struct ng_ppp_link *const link = &priv->links[linkNum];
1582 /* Deliver fragment(s) out the next link */
1583 for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) {
1584 int len, lastFragment, error;
1588 /* Calculate fragment length; don't exceed link MTU */
1589 len = distrib[activeLinkNum];
1590 if (len > link->conf.mru - hdr_len)
1591 len = link->conf.mru - hdr_len;
1592 distrib[activeLinkNum] -= len;
1593 lastFragment = (len == m->m_pkthdr.len);
1595 /* Split off next fragment as "m2" */
1597 if (!lastFragment) {
1598 struct mbuf *n = m_split(m, len, M_NOWAIT);
1608 /* Prepend MP header */
1609 if (priv->conf.xmitShortSeq) {
1614 (priv->xseq + 1) & MP_SHORT_SEQ_MASK;
1616 shdr |= MP_SHORT_FIRST_FLAG;
1618 shdr |= MP_SHORT_LAST_FLAG;
1620 m2 = ng_ppp_prepend(m2, &shdr, 2);
1626 (priv->xseq + 1) & MP_LONG_SEQ_MASK;
1628 lhdr |= MP_LONG_FIRST_FLAG;
1630 lhdr |= MP_LONG_LAST_FLAG;
1632 m2 = ng_ppp_prepend(m2, &lhdr, 4);
1641 /* Copy the meta information, if any */
1642 meta2 = lastFragment ? meta : ng_copy_meta(meta);
1645 item = ng_package_data(m2, meta2);
1646 error = ng_ppp_output(node, 0, PROT_MP, linkNum, item);
1648 if (!lastFragment) {
1662 * Computing the optimal fragmentation
1663 * -----------------------------------
1665 * This routine tries to compute the optimal fragmentation pattern based
1666 * on each link's latency, bandwidth, and calculated additional latency.
1667 * The latter quantity is the additional latency caused by previously
1668 * written data that has not been transmitted yet.
1670 * This algorithm is only useful when not all of the links have the
1671 * same latency and bandwidth values.
1673 * The essential idea is to make the last bit of each fragment of the
1674 * frame arrive at the opposite end at the exact same time. This greedy
1675 * algorithm is optimal, in that no other scheduling could result in any
1676 * packet arriving any sooner unless packets are delivered out of order.
1678 * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and
1679 * latency l_i (in miliseconds). Consider the function function f_i(t)
1680 * which is equal to the number of bytes that will have arrived at
1681 * the peer after t miliseconds if we start writing continuously at
1682 * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i).
1683 * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i).
1684 * Note that the y-intersect is always <= zero because latency can't be
1685 * negative. Note also that really the function is f_i(t) except when
1686 * f_i(t) is negative, in which case the function is zero. To take
1687 * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }.
1688 * So the actual number of bytes that will have arrived at the peer after
1689 * t miliseconds is f_i(t) * Q_i(t).
1691 * At any given time, each link has some additional latency a_i >= 0
1692 * due to previously written fragment(s) which are still in the queue.
1693 * This value is easily computed from the time since last transmission,
1694 * the previous latency value, the number of bytes written, and the
1697 * Assume that l_i includes any a_i already, and that the links are
1698 * sorted by latency, so that l_i <= l_{i+1}.
1700 * Let N be the total number of bytes in the current frame we are sending.
1702 * Suppose we were to start writing bytes at time t = 0 on all links
1703 * simultaneously, which is the most we can possibly do. Then let
1704 * F(t) be equal to the total number of bytes received by the peer
1705 * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)).
1707 * Our goal is simply this: fragment the frame across the links such
1708 * that the peer is able to reconstruct the completed frame as soon as
1709 * possible, i.e., at the least possible value of t. Call this value t_0.
1711 * Then it follows that F(t_0) = N. Our strategy is first to find the value
1712 * of t_0, and then deduce how many bytes to write to each link.
1716 * t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) )
1718 * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will
1719 * lie in one of these ranges. To find it, we just need to find the i such
1720 * that F(l_i) <= N <= F(l_{i+1}). Then we compute all the constant values
1721 * for Q_i() in this range, plug in the remaining values, solving for t_0.
1723 * Once t_0 is known, then the number of bytes to send on link i is
1724 * just f_i(t_0) * Q_i(t_0).
1726 * In other words, we start allocating bytes to the links one at a time.
1727 * We keep adding links until the frame is completely sent. Some links
1728 * may not get any bytes because their latency is too high.
1730 * Is all this work really worth the trouble? Depends on the situation.
1731 * The bigger the ratio of computer speed to link speed, and the more
1732 * important total bundle latency is (e.g., for interactive response time),
1733 * the more it's worth it. There is however the cost of calling this
1734 * function for every frame. The running time is O(n^2) where n is the
1735 * number of links that receive a non-zero number of bytes.
1737 * Since latency is measured in miliseconds, the "resolution" of this
1738 * algorithm is one milisecond.
1740 * To avoid this algorithm altogether, configure all links to have the
1741 * same latency and bandwidth.
1744 ng_ppp_mp_strategy(node_p node, int len, int *distrib)
1746 const priv_p priv = NG_NODE_PRIVATE(node);
1747 int latency[NG_PPP_MAX_LINKS];
1748 int sortByLatency[NG_PPP_MAX_LINKS];
1750 int t0, total, topSum, botSum;
1752 int i, numFragments;
1754 /* If only one link, this gets real easy */
1755 if (priv->numActiveLinks == 1) {
1760 /* Get current time */
1761 getmicrouptime(&now);
1763 /* Compute latencies for each link at this point in time */
1764 for (activeLinkNum = 0;
1765 activeLinkNum < priv->numActiveLinks; activeLinkNum++) {
1766 struct ng_ppp_link *alink;
1767 struct timeval diff;
1770 /* Start with base latency value */
1771 alink = &priv->links[priv->activeLinks[activeLinkNum]];
1772 latency[activeLinkNum] = alink->conf.latency;
1773 sortByLatency[activeLinkNum] = activeLinkNum; /* see below */
1775 /* Any additional latency? */
1776 if (alink->bytesInQueue == 0)
1779 /* Compute time delta since last write */
1781 timevalsub(&diff, &alink->lastWrite);
1782 if (now.tv_sec < 0 || diff.tv_sec >= 10) { /* sanity */
1783 alink->bytesInQueue = 0;
1787 /* How many bytes could have transmitted since last write? */
1788 xmitBytes = (alink->conf.bandwidth * diff.tv_sec)
1789 + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100;
1790 alink->bytesInQueue -= xmitBytes;
1791 if (alink->bytesInQueue < 0)
1792 alink->bytesInQueue = 0;
1794 latency[activeLinkNum] +=
1795 (100 * alink->bytesInQueue) / alink->conf.bandwidth;
1798 /* Sort active links by latency */
1799 compareLatencies = latency;
1800 qsort(sortByLatency,
1801 priv->numActiveLinks, sizeof(*sortByLatency), ng_ppp_intcmp);
1802 compareLatencies = NULL;
1804 /* Find the interval we need (add links in sortByLatency[] order) */
1805 for (numFragments = 1;
1806 numFragments < priv->numActiveLinks; numFragments++) {
1807 for (total = i = 0; i < numFragments; i++) {
1810 flowTime = latency[sortByLatency[numFragments]]
1811 - latency[sortByLatency[i]];
1812 total += ((flowTime * priv->links[
1813 priv->activeLinks[sortByLatency[i]]].conf.bandwidth)
1820 /* Solve for t_0 in that interval */
1821 for (topSum = botSum = i = 0; i < numFragments; i++) {
1822 int bw = priv->links[
1823 priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
1825 topSum += latency[sortByLatency[i]] * bw; /* / 100 */
1826 botSum += bw; /* / 100 */
1828 t0 = ((len * 100) + topSum + botSum / 2) / botSum;
1830 /* Compute f_i(t_0) all i */
1831 bzero(distrib, priv->numActiveLinks * sizeof(*distrib));
1832 for (total = i = 0; i < numFragments; i++) {
1833 int bw = priv->links[
1834 priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
1836 distrib[sortByLatency[i]] =
1837 (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100;
1838 total += distrib[sortByLatency[i]];
1841 /* Deal with any rounding error */
1843 struct ng_ppp_link *fastLink =
1844 &priv->links[priv->activeLinks[sortByLatency[0]]];
1847 /* Find the fastest link */
1848 for (i = 1; i < numFragments; i++) {
1849 struct ng_ppp_link *const link =
1850 &priv->links[priv->activeLinks[sortByLatency[i]]];
1852 if (link->conf.bandwidth > fastLink->conf.bandwidth) {
1857 distrib[sortByLatency[fast]] += len - total;
1858 } else while (total > len) {
1859 struct ng_ppp_link *slowLink =
1860 &priv->links[priv->activeLinks[sortByLatency[0]]];
1861 int delta, slow = 0;
1863 /* Find the slowest link that still has bytes to remove */
1864 for (i = 1; i < numFragments; i++) {
1865 struct ng_ppp_link *const link =
1866 &priv->links[priv->activeLinks[sortByLatency[i]]];
1868 if (distrib[sortByLatency[slow]] == 0
1869 || (distrib[sortByLatency[i]] > 0
1870 && link->conf.bandwidth <
1871 slowLink->conf.bandwidth)) {
1876 delta = total - len;
1877 if (delta > distrib[sortByLatency[slow]])
1878 delta = distrib[sortByLatency[slow]];
1879 distrib[sortByLatency[slow]] -= delta;
1885 * Compare two integers
1888 ng_ppp_intcmp(const void *v1, const void *v2)
1890 const int index1 = *((const int *) v1);
1891 const int index2 = *((const int *) v2);
1893 return compareLatencies[index1] - compareLatencies[index2];
1897 * Prepend a possibly compressed PPP protocol number in front of a frame
1899 static struct mbuf *
1900 ng_ppp_addproto(struct mbuf *m, int proto, int compOK)
1902 if (compOK && PROT_COMPRESSABLE(proto)) {
1903 u_char pbyte = (u_char)proto;
1905 return ng_ppp_prepend(m, &pbyte, 1);
1907 u_int16_t pword = htons((u_int16_t)proto);
1909 return ng_ppp_prepend(m, &pword, 2);
1914 * Prepend some bytes to an mbuf
1916 static struct mbuf *
1917 ng_ppp_prepend(struct mbuf *m, const void *buf, int len)
1919 M_PREPEND(m, len, M_NOWAIT);
1920 if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL))
1922 bcopy(buf, mtod(m, u_char *), len);
1927 * Update private information that is derived from other private information
1930 ng_ppp_update(node_p node, int newConf)
1932 const priv_p priv = NG_NODE_PRIVATE(node);
1935 /* Update active status for VJ Compression */
1936 priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL
1937 && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL
1938 && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL
1939 && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL;
1941 /* Increase latency for each link an amount equal to one MP header */
1943 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1946 hdrBytes = (priv->links[i].conf.enableACFComp ? 0 : 2)
1947 + (priv->links[i].conf.enableProtoComp ? 1 : 2)
1948 + (priv->conf.xmitShortSeq ? 2 : 4);
1949 priv->links[i].conf.latency +=
1950 ((hdrBytes * priv->links[i].conf.bandwidth) + 50)
1955 /* Update list of active links */
1956 bzero(&priv->activeLinks, sizeof(priv->activeLinks));
1957 priv->numActiveLinks = 0;
1958 priv->allLinksEqual = 1;
1959 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1960 struct ng_ppp_link *const link = &priv->links[i];
1962 /* Is link active? */
1963 if (link->conf.enableLink && link->hook != NULL) {
1964 struct ng_ppp_link *link0;
1966 /* Add link to list of active links */
1967 priv->activeLinks[priv->numActiveLinks++] = i;
1968 link0 = &priv->links[priv->activeLinks[0]];
1970 /* Determine if all links are still equal */
1971 if (link->conf.latency != link0->conf.latency
1972 || link->conf.bandwidth != link0->conf.bandwidth)
1973 priv->allLinksEqual = 0;
1975 /* Initialize rec'd sequence number */
1976 if (link->seq == MP_NOSEQ) {
1977 link->seq = (link == link0) ?
1978 MP_INITIAL_SEQ : link0->seq;
1981 link->seq = MP_NOSEQ;
1984 /* Update MP state as multi-link is active or not */
1985 if (priv->conf.enableMultilink && priv->numActiveLinks > 0)
1986 ng_ppp_start_frag_timer(node);
1988 ng_ppp_stop_frag_timer(node);
1989 ng_ppp_frag_reset(node);
1990 priv->xseq = MP_INITIAL_SEQ;
1991 priv->mseq = MP_INITIAL_SEQ;
1992 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1993 struct ng_ppp_link *const link = &priv->links[i];
1995 bzero(&link->lastWrite, sizeof(link->lastWrite));
1996 link->bytesInQueue = 0;
1997 link->seq = MP_NOSEQ;
2003 * Determine if a new configuration would represent a valid change
2004 * from the current configuration and link activity status.
2007 ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf)
2009 const priv_p priv = NG_NODE_PRIVATE(node);
2010 int i, newNumLinksActive;
2012 /* Check per-link config and count how many links would be active */
2013 for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) {
2014 if (newConf->links[i].enableLink && priv->links[i].hook != NULL)
2015 newNumLinksActive++;
2016 if (!newConf->links[i].enableLink)
2018 if (newConf->links[i].mru < MP_MIN_LINK_MRU)
2020 if (newConf->links[i].bandwidth == 0)
2022 if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH)
2024 if (newConf->links[i].latency > NG_PPP_MAX_LATENCY)
2028 /* Check bundle parameters */
2029 if (newConf->bund.enableMultilink && newConf->bund.mrru < MP_MIN_MRRU)
2032 /* Disallow changes to multi-link configuration while MP is active */
2033 if (priv->numActiveLinks > 0 && newNumLinksActive > 0) {
2034 if (!priv->conf.enableMultilink
2035 != !newConf->bund.enableMultilink
2036 || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq
2037 || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq)
2041 /* At most one link can be active unless multi-link is enabled */
2042 if (!newConf->bund.enableMultilink && newNumLinksActive > 1)
2045 /* Configuration change would be valid */
2050 * Free all entries in the fragment queue
2053 ng_ppp_frag_reset(node_p node)
2055 const priv_p priv = NG_NODE_PRIVATE(node);
2056 struct ng_ppp_frag *qent, *qnext;
2058 for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) {
2059 qnext = TAILQ_NEXT(qent, f_qent);
2060 NG_FREE_M(qent->data);
2061 NG_FREE_META(qent->meta);
2062 FREE(qent, M_NETGRAPH_PPP);
2064 TAILQ_INIT(&priv->frags);
2069 * Start fragment queue timer
2072 ng_ppp_start_frag_timer(node_p node)
2074 const priv_p priv = NG_NODE_PRIVATE(node);
2076 if (!priv->timerActive) {
2077 priv->fragTimer = timeout(ng_ppp_frag_timeout,
2078 node, MP_FRAGTIMER_INTERVAL);
2079 priv->timerActive = 1;
2085 * Stop fragment queue timer
2088 ng_ppp_stop_frag_timer(node_p node)
2090 const priv_p priv = NG_NODE_PRIVATE(node);
2092 if (priv->timerActive) {
2093 untimeout(ng_ppp_frag_timeout, node, priv->fragTimer);
2094 priv->timerActive = 0;
2095 KASSERT(node->nd_refs > 1,
2096 ("%s: nd_refs=%d", __func__, node->nd_refs));
2097 NG_NODE_UNREF(node);