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>
50 #include <sys/limits.h>
53 #include <sys/malloc.h>
54 #include <sys/errno.h>
55 #include <sys/ctype.h>
57 #include <netgraph/ng_message.h>
58 #include <netgraph/netgraph.h>
59 #include <netgraph/ng_parse.h>
60 #include <netgraph/ng_ppp.h>
61 #include <netgraph/ng_vjc.h>
63 #ifdef NG_SEPARATE_MALLOC
64 MALLOC_DEFINE(M_NETGRAPH_PPP, "netgraph_ppp", "netgraph ppp node");
66 #define M_NETGRAPH_PPP M_NETGRAPH
69 #define PROT_VALID(p) (((p) & 0x0101) == 0x0001)
70 #define PROT_COMPRESSABLE(p) (((p) & 0xff00) == 0x0000)
72 /* Some PPP protocol numbers we're interested in */
73 #define PROT_APPLETALK 0x0029
74 #define PROT_COMPD 0x00fd
75 #define PROT_CRYPTD 0x0053
76 #define PROT_IP 0x0021
77 #define PROT_IPV6 0x0057
78 #define PROT_IPX 0x002b
79 #define PROT_LCP 0xc021
80 #define PROT_MP 0x003d
81 #define PROT_VJCOMP 0x002d
82 #define PROT_VJUNCOMP 0x002f
84 /* Multilink PPP definitions */
85 #define MP_MIN_MRRU 1500 /* per RFC 1990 */
86 #define MP_INITIAL_SEQ 0 /* per RFC 1990 */
87 #define MP_MIN_LINK_MRU 32
89 #define MP_SHORT_SEQ_MASK 0x00000fff /* short seq # mask */
90 #define MP_SHORT_SEQ_HIBIT 0x00000800 /* short seq # high bit */
91 #define MP_SHORT_FIRST_FLAG 0x00008000 /* first fragment in frame */
92 #define MP_SHORT_LAST_FLAG 0x00004000 /* last fragment in frame */
94 #define MP_LONG_SEQ_MASK 0x00ffffff /* long seq # mask */
95 #define MP_LONG_SEQ_HIBIT 0x00800000 /* long seq # high bit */
96 #define MP_LONG_FIRST_FLAG 0x80000000 /* first fragment in frame */
97 #define MP_LONG_LAST_FLAG 0x40000000 /* last fragment in frame */
99 #define MP_NOSEQ 0x7fffffff /* impossible sequence number */
101 /* Sign extension of MP sequence numbers */
102 #define MP_SHORT_EXTEND(s) (((s) & MP_SHORT_SEQ_HIBIT) ? \
103 ((s) | ~MP_SHORT_SEQ_MASK) \
104 : ((s) & MP_SHORT_SEQ_MASK))
105 #define MP_LONG_EXTEND(s) (((s) & MP_LONG_SEQ_HIBIT) ? \
106 ((s) | ~MP_LONG_SEQ_MASK) \
107 : ((s) & MP_LONG_SEQ_MASK))
109 /* Comparision of MP sequence numbers. Note: all sequence numbers
110 except priv->xseq are stored with the sign bit extended. */
111 #define MP_SHORT_SEQ_DIFF(x,y) MP_SHORT_EXTEND((x) - (y))
112 #define MP_LONG_SEQ_DIFF(x,y) MP_LONG_EXTEND((x) - (y))
114 #define MP_RECV_SEQ_DIFF(priv,x,y) \
115 ((priv)->conf.recvShortSeq ? \
116 MP_SHORT_SEQ_DIFF((x), (y)) : \
117 MP_LONG_SEQ_DIFF((x), (y)))
119 /* Increment receive sequence number */
120 #define MP_NEXT_RECV_SEQ(priv,seq) \
121 ((priv)->conf.recvShortSeq ? \
122 MP_SHORT_EXTEND((seq) + 1) : \
123 MP_LONG_EXTEND((seq) + 1))
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 TAILQ_ENTRY(ng_ppp_frag) f_qent; /* Fragment queue */
144 /* We use integer indicies to refer to the non-link hooks */
145 static const char *const ng_ppp_hook_names[] = {
147 #define HOOK_INDEX_ATALK 0
149 #define HOOK_INDEX_BYPASS 1
150 NG_PPP_HOOK_COMPRESS,
151 #define HOOK_INDEX_COMPRESS 2
153 #define HOOK_INDEX_ENCRYPT 3
154 NG_PPP_HOOK_DECOMPRESS,
155 #define HOOK_INDEX_DECOMPRESS 4
157 #define HOOK_INDEX_DECRYPT 5
159 #define HOOK_INDEX_INET 6
161 #define HOOK_INDEX_IPX 7
162 NG_PPP_HOOK_VJC_COMP,
163 #define HOOK_INDEX_VJC_COMP 8
165 #define HOOK_INDEX_VJC_IP 9
166 NG_PPP_HOOK_VJC_UNCOMP,
167 #define HOOK_INDEX_VJC_UNCOMP 10
168 NG_PPP_HOOK_VJC_VJIP,
169 #define HOOK_INDEX_VJC_VJIP 11
171 #define HOOK_INDEX_IPV6 12
173 #define HOOK_INDEX_MAX 13
176 /* We store index numbers in the hook private pointer. The HOOK_INDEX()
177 for a hook is either the index (above) for normal hooks, or the ones
178 complement of the link number for link hooks.
179 XXX Not any more.. (what a hack)
180 #define HOOK_INDEX(hook) (*((int16_t *) &(hook)->private))
183 /* Per-link private information */
185 struct ng_ppp_link_conf conf; /* link configuration */
186 hook_p hook; /* connection to link data */
187 int32_t seq; /* highest rec'd seq# - MSEQ */
188 u_int32_t latency; /* calculated link latency */
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);
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);
237 static void ng_ppp_mp_strategy(node_p node, int len, int *distrib);
238 static int ng_ppp_intcmp(void *latency, 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_field ng_ppp_mp_state_type_fields[]
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_fields
263 /* Parse type for struct ng_ppp_link_conf */
264 static const struct ng_parse_struct_field ng_ppp_link_type_fields[]
265 = 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_fields
271 /* Parse type for struct ng_ppp_bund_conf */
272 static const struct ng_parse_struct_field ng_ppp_bund_type_fields[]
273 = 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_fields
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_field ng_ppp_conf_type_fields[]
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_fields
295 /* Parse type for struct ng_ppp_link_stat */
296 static const struct ng_parse_struct_field ng_ppp_stats_type_fields[]
297 = 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_fields
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 = {
352 .version = NG_ABI_VERSION,
353 .name = NG_PPP_NODE_TYPE,
354 .constructor = ng_ppp_constructor,
355 .rcvmsg = ng_ppp_rcvmsg,
356 .shutdown = ng_ppp_shutdown,
357 .newhook = ng_ppp_newhook,
358 .rcvdata = ng_ppp_rcvdata,
359 .disconnect = ng_ppp_disconnect,
360 .cmdlist = ng_ppp_cmds,
362 NETGRAPH_INIT(ppp, &ng_ppp_typestruct);
364 /* Address and control field header */
365 static const u_char ng_ppp_acf[2] = { 0xff, 0x03 };
367 /* Maximum time we'll let a complete incoming packet sit in the queue */
368 static const struct timeval ng_ppp_max_staleness = { 2, 0 }; /* 2 seconds */
370 #define ERROUT(x) do { error = (x); goto done; } while (0)
372 /************************************************************************
374 ************************************************************************/
377 * Node type constructor
380 ng_ppp_constructor(node_p node)
385 /* Allocate private structure */
386 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_PPP, M_NOWAIT | M_ZERO);
390 NG_NODE_SET_PRIVATE(node, priv);
392 /* Initialize state */
393 TAILQ_INIT(&priv->frags);
394 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
395 priv->links[i].seq = MP_NOSEQ;
396 callout_handle_init(&priv->fragTimer);
403 * Give our OK for a hook to be added
406 ng_ppp_newhook(node_p node, hook_p hook, const char *name)
408 const priv_p priv = NG_NODE_PRIVATE(node);
410 hook_p *hookPtr = NULL;
413 /* Figure out which hook it is */
414 if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX, /* a link hook? */
415 strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) {
419 cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
420 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
422 linkNum = (int)strtoul(cp, &eptr, 10);
423 if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS)
425 hookPtr = &priv->links[linkNum].hook;
426 hookIndex = ~linkNum;
427 } else { /* must be a non-link hook */
430 for (i = 0; ng_ppp_hook_names[i] != NULL; i++) {
431 if (strcmp(name, ng_ppp_hook_names[i]) == 0) {
432 hookPtr = &priv->hooks[i];
437 if (ng_ppp_hook_names[i] == NULL)
438 return (EINVAL); /* no such hook */
441 /* See if hook is already connected */
442 if (*hookPtr != NULL)
445 /* Disallow more than one link unless multilink is enabled */
446 if (linkNum != -1 && priv->links[linkNum].conf.enableLink
447 && !priv->conf.enableMultilink && priv->numActiveLinks >= 1)
452 NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)hookIndex);
453 ng_ppp_update(node, 0);
458 * Receive a control message
461 ng_ppp_rcvmsg(node_p node, item_p item, hook_p lasthook)
463 const priv_p priv = NG_NODE_PRIVATE(node);
464 struct ng_mesg *resp = NULL;
468 NGI_GET_MSG(item, msg);
469 switch (msg->header.typecookie) {
471 switch (msg->header.cmd) {
472 case NGM_PPP_SET_CONFIG:
474 struct ng_ppp_node_conf *const conf =
475 (struct ng_ppp_node_conf *)msg->data;
478 /* Check for invalid or illegal config */
479 if (msg->header.arglen != sizeof(*conf))
481 if (!ng_ppp_config_valid(node, conf))
485 priv->conf = conf->bund;
486 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
487 priv->links[i].conf = conf->links[i];
488 ng_ppp_update(node, 1);
491 case NGM_PPP_GET_CONFIG:
493 struct ng_ppp_node_conf *conf;
496 NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
499 conf = (struct ng_ppp_node_conf *)resp->data;
500 conf->bund = priv->conf;
501 for (i = 0; i < NG_PPP_MAX_LINKS; i++)
502 conf->links[i] = priv->links[i].conf;
505 case NGM_PPP_GET_MP_STATE:
507 struct ng_ppp_mp_state *info;
510 NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT);
513 info = (struct ng_ppp_mp_state *)resp->data;
514 bzero(info, sizeof(*info));
515 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
516 if (priv->links[i].seq != MP_NOSEQ)
517 info->rseq[i] = priv->links[i].seq;
519 info->mseq = priv->mseq;
520 info->xseq = priv->xseq;
523 case NGM_PPP_GET_LINK_STATS:
524 case NGM_PPP_CLR_LINK_STATS:
525 case NGM_PPP_GETCLR_LINK_STATS:
527 struct ng_ppp_link_stat *stats;
530 if (msg->header.arglen != sizeof(u_int16_t))
532 linkNum = *((u_int16_t *) msg->data);
533 if (linkNum >= NG_PPP_MAX_LINKS
534 && linkNum != NG_PPP_BUNDLE_LINKNUM)
536 stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ?
537 &priv->bundleStats : &priv->links[linkNum].stats;
538 if (msg->header.cmd != NGM_PPP_CLR_LINK_STATS) {
539 NG_MKRESPONSE(resp, msg,
540 sizeof(struct ng_ppp_link_stat), M_NOWAIT);
543 bcopy(stats, resp->data, sizeof(*stats));
545 if (msg->header.cmd != NGM_PPP_GET_LINK_STATS)
546 bzero(stats, sizeof(*stats));
557 * Forward it to the vjc node. leave the
558 * old return address alone.
559 * If we have no hook, let NG_RESPOND_MSG
560 * clean up any remaining resources.
561 * Because we have no resp, the item will be freed
562 * along with anything it references. Don't
563 * let msg be freed twice.
565 NGI_MSG(item) = msg; /* put it back in the item */
567 if ((lasthook = priv->links[HOOK_INDEX_VJC_IP].hook)) {
568 NG_FWD_ITEM_HOOK(error, item, lasthook);
577 NG_RESPOND_MSG(error, node, item, resp);
583 * Receive data on a hook
586 ng_ppp_rcvdata(hook_p hook, item_p item)
588 const node_p node = NG_HOOK_NODE(hook);
589 const priv_p priv = NG_NODE_PRIVATE(node);
590 const int index = (intptr_t)NG_HOOK_PRIVATE(hook);
591 u_int16_t linkNum = NG_PPP_BUNDLE_LINKNUM;
592 hook_p outHook = NULL;
593 int proto = 0, error;
597 /* Did it come from a link hook? */
599 struct ng_ppp_link *link;
601 /* Convert index into a link number */
602 linkNum = (u_int16_t)~index;
603 KASSERT(linkNum < NG_PPP_MAX_LINKS,
604 ("%s: bogus index 0x%x", __func__, index));
605 link = &priv->links[linkNum];
608 link->stats.recvFrames++;
609 link->stats.recvOctets += m->m_pkthdr.len;
611 /* Strip address and control fields, if present */
612 if (m->m_pkthdr.len >= 2) {
613 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
617 if (bcmp(mtod(m, u_char *), &ng_ppp_acf, 2) == 0)
621 /* Dispatch incoming frame (if not enabled, to bypass) */
622 NGI_M(item) = m; /* put changed m back in item */
623 return ng_ppp_input(node,
624 !link->conf.enableLink, linkNum, item);
627 /* Get protocol & check if data allowed from this hook */
628 NGI_M(item) = m; /* put possibly changed m back in item */
632 case HOOK_INDEX_ATALK:
633 if (!priv->conf.enableAtalk) {
637 proto = PROT_APPLETALK;
640 if (!priv->conf.enableIPX) {
646 case HOOK_INDEX_IPV6:
647 if (!priv->conf.enableIPv6) {
653 case HOOK_INDEX_INET:
654 case HOOK_INDEX_VJC_VJIP:
655 if (!priv->conf.enableIP) {
661 case HOOK_INDEX_VJC_COMP:
662 if (!priv->conf.enableVJCompression) {
668 case HOOK_INDEX_VJC_UNCOMP:
669 if (!priv->conf.enableVJCompression) {
673 proto = PROT_VJUNCOMP;
675 case HOOK_INDEX_COMPRESS:
676 if (!priv->conf.enableCompression) {
682 case HOOK_INDEX_ENCRYPT:
683 if (!priv->conf.enableEncryption) {
689 case HOOK_INDEX_BYPASS:
690 if (m->m_pkthdr.len < 4) {
694 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
695 NGI_M(item) = NULL; /* don't free twice */
699 NGI_M(item) = m; /* m may have changed */
700 linkNum = ntohs(mtod(m, u_int16_t *)[0]);
701 proto = ntohs(mtod(m, u_int16_t *)[1]);
703 if (linkNum >= NG_PPP_MAX_LINKS
704 && linkNum != NG_PPP_BUNDLE_LINKNUM) {
711 case HOOK_INDEX_VJC_IP:
712 if (!priv->conf.enableIP || !priv->conf.enableVJDecompression) {
717 case HOOK_INDEX_DECOMPRESS:
718 if (!priv->conf.enableDecompression) {
723 case HOOK_INDEX_DECRYPT:
724 if (!priv->conf.enableDecryption) {
730 panic("%s: bogus index 0x%x", __func__, index);
733 /* Now figure out what to do with the frame */
737 case HOOK_INDEX_INET:
738 if (priv->conf.enableVJCompression && priv->vjCompHooked) {
739 outHook = priv->hooks[HOOK_INDEX_VJC_IP];
743 case HOOK_INDEX_ATALK:
744 case HOOK_INDEX_IPV6:
746 case HOOK_INDEX_VJC_COMP:
747 case HOOK_INDEX_VJC_UNCOMP:
748 case HOOK_INDEX_VJC_VJIP:
749 if (priv->conf.enableCompression
750 && priv->hooks[HOOK_INDEX_COMPRESS] != NULL) {
751 if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) {
756 NGI_M(item) = m; /* m may have changed */
757 outHook = priv->hooks[HOOK_INDEX_COMPRESS];
761 case HOOK_INDEX_COMPRESS:
762 if (priv->conf.enableEncryption
763 && priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) {
764 if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
769 NGI_M(item) = m; /* m may have changed */
770 outHook = priv->hooks[HOOK_INDEX_ENCRYPT];
774 case HOOK_INDEX_ENCRYPT:
775 return ng_ppp_output(node, 0, proto, NG_PPP_BUNDLE_LINKNUM, item);
777 case HOOK_INDEX_BYPASS:
778 return ng_ppp_output(node, 1, proto, linkNum, item);
781 case HOOK_INDEX_DECRYPT:
782 case HOOK_INDEX_DECOMPRESS:
783 return ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
785 case HOOK_INDEX_VJC_IP:
786 outHook = priv->hooks[HOOK_INDEX_INET];
790 /* Send packet out hook */
791 NG_FWD_ITEM_HOOK(error, item, outHook);
799 ng_ppp_shutdown(node_p node)
801 const priv_p priv = NG_NODE_PRIVATE(node);
803 /* Stop fragment queue timer */
804 ng_ppp_stop_frag_timer(node);
806 /* Take down netgraph node */
807 ng_ppp_frag_reset(node);
808 bzero(priv, sizeof(*priv));
809 FREE(priv, M_NETGRAPH_PPP);
810 NG_NODE_SET_PRIVATE(node, NULL);
811 NG_NODE_UNREF(node); /* let the node escape */
819 ng_ppp_disconnect(hook_p hook)
821 const node_p node = NG_HOOK_NODE(hook);
822 const priv_p priv = NG_NODE_PRIVATE(node);
823 const int index = (intptr_t)NG_HOOK_PRIVATE(hook);
825 /* Zero out hook pointer */
827 priv->links[~index].hook = NULL;
829 priv->hooks[index] = NULL;
831 /* Update derived info (or go away if no hooks left) */
832 if (NG_NODE_NUMHOOKS(node) > 0) {
833 ng_ppp_update(node, 0);
835 if (NG_NODE_IS_VALID(node)) {
836 ng_rmnode_self(node);
842 /************************************************************************
844 ************************************************************************/
847 * Handle an incoming frame. Extract the PPP protocol number
848 * and dispatch accordingly.
851 ng_ppp_input(node_p node, int bypass, int linkNum, item_p item)
853 const priv_p priv = NG_NODE_PRIVATE(node);
854 hook_p outHook = NULL;
860 /* Extract protocol number */
861 for (proto = 0; !PROT_VALID(proto) && m->m_pkthdr.len > 0; ) {
862 if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) {
866 proto = (proto << 8) + *mtod(m, u_char *);
869 if (!PROT_VALID(proto)) {
870 if (linkNum == NG_PPP_BUNDLE_LINKNUM)
871 priv->bundleStats.badProtos++;
873 priv->links[linkNum].stats.badProtos++;
886 if (priv->conf.enableDecompression)
887 outHook = priv->hooks[HOOK_INDEX_DECOMPRESS];
890 if (priv->conf.enableDecryption)
891 outHook = priv->hooks[HOOK_INDEX_DECRYPT];
894 if (priv->conf.enableVJDecompression && priv->vjCompHooked)
895 outHook = priv->hooks[HOOK_INDEX_VJC_COMP];
898 if (priv->conf.enableVJDecompression && priv->vjCompHooked)
899 outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP];
902 if (priv->conf.enableMultilink
903 && linkNum != NG_PPP_BUNDLE_LINKNUM) {
905 return ng_ppp_mp_input(node, linkNum, item);
909 if (priv->conf.enableAtalk)
910 outHook = priv->hooks[HOOK_INDEX_ATALK];
913 if (priv->conf.enableIPX)
914 outHook = priv->hooks[HOOK_INDEX_IPX];
917 if (priv->conf.enableIP)
918 outHook = priv->hooks[HOOK_INDEX_INET];
921 if (priv->conf.enableIPv6)
922 outHook = priv->hooks[HOOK_INDEX_IPV6];
927 /* For unknown/inactive protocols, forward out the bypass hook */
928 if (outHook == NULL) {
931 hdr[0] = htons(linkNum);
932 hdr[1] = htons((u_int16_t)proto);
933 if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) {
937 outHook = priv->hooks[HOOK_INDEX_BYPASS];
941 NG_FWD_NEW_DATA(error, item, outHook, m);
946 * Deliver a frame out a link, either a real one or NG_PPP_BUNDLE_LINKNUM.
947 * If the link is not enabled then ENXIO is returned, unless "bypass" is != 0.
949 * If the frame is too big for the particular link, return EMSGSIZE.
952 ng_ppp_output(node_p node, int bypass,
953 int proto, int linkNum, item_p item)
955 const priv_p priv = NG_NODE_PRIVATE(node);
956 struct ng_ppp_link *link;
964 /* If not doing MP, map bundle virtual link to (the only) link */
965 if (linkNum == NG_PPP_BUNDLE_LINKNUM && !priv->conf.enableMultilink)
966 linkNum = priv->activeLinks[0];
968 /* Get link pointer (optimization) */
969 link = (linkNum != NG_PPP_BUNDLE_LINKNUM) ?
970 &priv->links[linkNum] : NULL;
972 /* Check link status (if real) */
973 if (linkNum != NG_PPP_BUNDLE_LINKNUM) {
974 if (!bypass && !link->conf.enableLink) {
979 if (link->hook == NULL) {
986 /* Check peer's MRU for this link */
987 mru = (link != NULL) ? link->conf.mru : priv->conf.mrru;
988 if (mru != 0 && m->m_pkthdr.len > mru) {
994 /* Prepend protocol number, possibly compressed */
995 if ((m = ng_ppp_addproto(m, proto,
996 linkNum == NG_PPP_BUNDLE_LINKNUM
997 || link->conf.enableProtoComp)) == NULL) {
1002 /* Special handling for the MP virtual link */
1003 if (linkNum == NG_PPP_BUNDLE_LINKNUM) {
1004 /* discard the queue item */
1006 return ng_ppp_mp_output(node, m);
1009 /* Prepend address and control field (unless compressed) */
1010 if (proto == PROT_LCP || !link->conf.enableACFComp) {
1011 if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) {
1018 len = m->m_pkthdr.len;
1019 NG_FWD_NEW_DATA(error, item, link->hook, m);
1021 /* Update stats and 'bytes in queue' counter */
1023 link->stats.xmitFrames++;
1024 link->stats.xmitOctets += len;
1025 link->bytesInQueue += len;
1026 getmicrouptime(&link->lastWrite);
1032 * Handle an incoming multi-link fragment
1034 * The fragment reassembly algorithm is somewhat complex. This is mainly
1035 * because we are required not to reorder the reconstructed packets, yet
1036 * fragments are only guaranteed to arrive in order on a per-link basis.
1037 * In other words, when we have a complete packet ready, but the previous
1038 * packet is still incomplete, we have to decide between delivering the
1039 * complete packet and throwing away the incomplete one, or waiting to
1040 * see if the remainder of the incomplete one arrives, at which time we
1041 * can deliver both packets, in order.
1043 * This problem is exacerbated by "sequence number slew", which is when
1044 * the sequence numbers coming in from different links are far apart from
1045 * each other. In particular, certain unnamed equipment (*cough* Ascend)
1046 * has been seen to generate sequence number slew of up to 10 on an ISDN
1047 * 2B-channel MP link. There is nothing invalid about sequence number slew
1048 * but it makes the reasssembly process have to work harder.
1050 * However, the peer is required to transmit fragments in order on each
1051 * link. That means if we define MSEQ as the minimum over all links of
1052 * the highest sequence number received on that link, then we can always
1053 * give up any hope of receiving a fragment with sequence number < MSEQ in
1054 * the future (all of this using 'wraparound' sequence number space).
1055 * Therefore we can always immediately throw away incomplete packets
1056 * missing fragments with sequence numbers < MSEQ.
1058 * Here is an overview of our algorithm:
1060 * o Received fragments are inserted into a queue, for which we
1061 * maintain these invariants between calls to this function:
1063 * - Fragments are ordered in the queue by sequence number
1064 * - If a complete packet is at the head of the queue, then
1065 * the first fragment in the packet has seq# > MSEQ + 1
1066 * (otherwise, we could deliver it immediately)
1067 * - If any fragments have seq# < MSEQ, then they are necessarily
1068 * part of a packet whose missing seq#'s are all > MSEQ (otherwise,
1069 * we can throw them away because they'll never be completed)
1070 * - The queue contains at most MP_MAX_QUEUE_LEN fragments
1072 * o We have a periodic timer that checks the queue for the first
1073 * complete packet that has been sitting in the queue "too long".
1074 * When one is detected, all previous (incomplete) fragments are
1075 * discarded, their missing fragments are declared lost and MSEQ
1078 * o If we recieve a fragment with seq# < MSEQ, we throw it away
1079 * because we've already delcared it lost.
1081 * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM.
1084 ng_ppp_mp_input(node_p node, int linkNum, item_p item)
1086 const priv_p priv = NG_NODE_PRIVATE(node);
1087 struct ng_ppp_link *const link = &priv->links[linkNum];
1088 struct ng_ppp_frag frag0, *frag = &frag0;
1089 struct ng_ppp_frag *qent;
1090 int i, diff, inserted;
1096 priv->bundleStats.recvFrames++;
1097 priv->bundleStats.recvOctets += m->m_pkthdr.len;
1099 /* Extract fragment information from MP header */
1100 if (priv->conf.recvShortSeq) {
1103 if (m->m_pkthdr.len < 2) {
1104 link->stats.runts++;
1108 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL)
1111 shdr = ntohs(*mtod(m, u_int16_t *));
1112 frag->seq = MP_SHORT_EXTEND(shdr);
1113 frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0;
1114 frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0;
1115 diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq);
1120 if (m->m_pkthdr.len < 4) {
1121 link->stats.runts++;
1125 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
1128 lhdr = ntohl(*mtod(m, u_int32_t *));
1129 frag->seq = MP_LONG_EXTEND(lhdr);
1130 frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0;
1131 frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0;
1132 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++;
1146 /* Update highest received sequence number on this link and MSEQ */
1147 priv->mseq = link->seq = frag->seq;
1148 for (i = 0; i < priv->numActiveLinks; i++) {
1149 struct ng_ppp_link *const alink =
1150 &priv->links[priv->activeLinks[i]];
1152 if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0)
1153 priv->mseq = alink->seq;
1156 /* Allocate a new frag struct for the queue */
1157 MALLOC(frag, struct ng_ppp_frag *, sizeof(*frag), M_NETGRAPH_PPP, M_NOWAIT);
1160 ng_ppp_frag_process(node);
1165 /* Add fragment to queue, which is sorted by sequence number */
1167 TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) {
1168 diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq);
1170 TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent);
1173 } else if (diff == 0) { /* should never happen! */
1174 link->stats.dupFragments++;
1175 NG_FREE_M(frag->data);
1176 FREE(frag, M_NETGRAPH_PPP);
1181 TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent);
1184 /* Process the queue */
1185 return ng_ppp_frag_process(node);
1189 * Examine our list of fragments, and determine if there is a
1190 * complete and deliverable packet at the head of the list.
1191 * Return 1 if so, zero otherwise.
1194 ng_ppp_check_packet(node_p node)
1196 const priv_p priv = NG_NODE_PRIVATE(node);
1197 struct ng_ppp_frag *qent, *qnext;
1199 /* Check for empty queue */
1200 if (TAILQ_EMPTY(&priv->frags))
1203 /* Check first fragment is the start of a deliverable packet */
1204 qent = TAILQ_FIRST(&priv->frags);
1205 if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1)
1208 /* Check that all the fragments are there */
1209 while (!qent->last) {
1210 qnext = TAILQ_NEXT(qent, f_qent);
1211 if (qnext == NULL) /* end of queue */
1213 if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq))
1223 * Pull a completed packet off the head of the incoming fragment queue.
1224 * This assumes there is a completed packet there to pull off.
1227 ng_ppp_get_packet(node_p node, struct mbuf **mp)
1229 const priv_p priv = NG_NODE_PRIVATE(node);
1230 struct ng_ppp_frag *qent, *qnext;
1231 struct mbuf *m = NULL, *tail;
1233 qent = TAILQ_FIRST(&priv->frags);
1234 KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first,
1235 ("%s: no packet", __func__));
1236 for (tail = NULL; qent != NULL; qent = qnext) {
1237 qnext = TAILQ_NEXT(qent, f_qent);
1238 KASSERT(!TAILQ_EMPTY(&priv->frags),
1239 ("%s: empty q", __func__));
1240 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1242 tail = m = qent->data;
1244 m->m_pkthdr.len += qent->data->m_pkthdr.len;
1245 tail->m_next = qent->data;
1247 while (tail->m_next != NULL)
1248 tail = tail->m_next;
1251 FREE(qent, M_NETGRAPH_PPP);
1258 * Trim fragments from the queue whose packets can never be completed.
1259 * This assumes a complete packet is NOT at the beginning of the queue.
1260 * Returns 1 if fragments were removed, zero otherwise.
1263 ng_ppp_frag_trim(node_p node)
1265 const priv_p priv = NG_NODE_PRIVATE(node);
1266 struct ng_ppp_frag *qent, *qnext = NULL;
1269 /* Scan for "dead" fragments and remove them */
1273 /* If queue is empty, we're done */
1274 if (TAILQ_EMPTY(&priv->frags))
1277 /* Determine whether first fragment can ever be completed */
1278 TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1279 if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0)
1281 qnext = TAILQ_NEXT(qent, f_qent);
1282 KASSERT(qnext != NULL,
1283 ("%s: last frag < MSEQ?", __func__));
1284 if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)
1285 || qent->last || qnext->first) {
1293 /* Remove fragment and all others in the same packet */
1294 while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) {
1295 KASSERT(!TAILQ_EMPTY(&priv->frags),
1296 ("%s: empty q", __func__));
1297 priv->bundleStats.dropFragments++;
1298 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1299 NG_FREE_M(qent->data);
1300 FREE(qent, M_NETGRAPH_PPP);
1309 * Run the queue, restoring the queue invariants
1312 ng_ppp_frag_process(node_p node)
1314 const priv_p priv = NG_NODE_PRIVATE(node);
1318 /* Deliver any deliverable packets */
1319 while (ng_ppp_check_packet(node)) {
1320 ng_ppp_get_packet(node, &m);
1321 item = ng_package_data(m, NULL);
1322 ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1325 /* Delete dead fragments and try again */
1326 if (ng_ppp_frag_trim(node)) {
1327 while (ng_ppp_check_packet(node)) {
1328 ng_ppp_get_packet(node, &m);
1329 item = ng_package_data(m, NULL);
1330 ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1334 /* Check for stale fragments while we're here */
1335 ng_ppp_frag_checkstale(node);
1337 /* Check queue length */
1338 if (priv->qlen > MP_MAX_QUEUE_LEN) {
1339 struct ng_ppp_frag *qent;
1342 /* Get oldest fragment */
1343 KASSERT(!TAILQ_EMPTY(&priv->frags),
1344 ("%s: empty q", __func__));
1345 qent = TAILQ_FIRST(&priv->frags);
1347 /* Bump MSEQ if necessary */
1348 if (MP_RECV_SEQ_DIFF(priv, priv->mseq, qent->seq) < 0) {
1349 priv->mseq = qent->seq;
1350 for (i = 0; i < priv->numActiveLinks; i++) {
1351 struct ng_ppp_link *const alink =
1352 &priv->links[priv->activeLinks[i]];
1354 if (MP_RECV_SEQ_DIFF(priv,
1355 alink->seq, priv->mseq) < 0)
1356 alink->seq = priv->mseq;
1361 priv->bundleStats.dropFragments++;
1362 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1363 NG_FREE_M(qent->data);
1364 FREE(qent, M_NETGRAPH_PPP);
1367 /* Process queue again */
1368 return ng_ppp_frag_process(node);
1376 * Check for 'stale' completed packets that need to be delivered
1378 * If a link goes down or has a temporary failure, MSEQ can get
1379 * "stuck", because no new incoming fragments appear on that link.
1380 * This can cause completed packets to never get delivered if
1381 * their sequence numbers are all > MSEQ + 1.
1383 * This routine checks how long all of the completed packets have
1384 * been sitting in the queue, and if too long, removes fragments
1385 * from the queue and increments MSEQ to allow them to be delivered.
1388 ng_ppp_frag_checkstale(node_p node)
1390 const priv_p priv = NG_NODE_PRIVATE(node);
1391 struct ng_ppp_frag *qent, *beg, *end;
1392 struct timeval now, age;
1398 now.tv_sec = 0; /* uninitialized state */
1401 /* If queue is empty, we're done */
1402 if (TAILQ_EMPTY(&priv->frags))
1405 /* Find the first complete packet in the queue */
1407 seq = TAILQ_FIRST(&priv->frags)->seq;
1408 TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1411 else if (qent->seq != seq)
1413 if (beg != NULL && qent->last) {
1417 seq = MP_NEXT_RECV_SEQ(priv, seq);
1420 /* If none found, exit */
1424 /* Get current time (we assume we've been up for >= 1 second) */
1425 if (now.tv_sec == 0)
1426 getmicrouptime(&now);
1428 /* Check if packet has been queued too long */
1430 timevalsub(&age, &beg->timestamp);
1431 if (timevalcmp(&age, &ng_ppp_max_staleness, < ))
1434 /* Throw away junk fragments in front of the completed packet */
1435 while ((qent = TAILQ_FIRST(&priv->frags)) != beg) {
1436 KASSERT(!TAILQ_EMPTY(&priv->frags),
1437 ("%s: empty q", __func__));
1438 priv->bundleStats.dropFragments++;
1439 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1440 NG_FREE_M(qent->data);
1441 FREE(qent, M_NETGRAPH_PPP);
1445 /* Extract completed packet */
1447 ng_ppp_get_packet(node, &m);
1449 /* Bump MSEQ if necessary */
1450 if (MP_RECV_SEQ_DIFF(priv, priv->mseq, endseq) < 0) {
1451 priv->mseq = endseq;
1452 for (i = 0; i < priv->numActiveLinks; i++) {
1453 struct ng_ppp_link *const alink =
1454 &priv->links[priv->activeLinks[i]];
1456 if (MP_RECV_SEQ_DIFF(priv,
1457 alink->seq, priv->mseq) < 0)
1458 alink->seq = priv->mseq;
1462 /* Deliver packet */
1463 item = ng_package_data(m, NULL);
1464 ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1469 * Periodically call ng_ppp_frag_checkstale()
1472 ng_ppp_frag_timeout(void *arg)
1474 const node_p node = arg;
1475 const priv_p priv = NG_NODE_PRIVATE(node);
1478 /* Handle the race where shutdown happens just before splnet() above */
1479 if (NG_NODE_NOT_VALID(node)) {
1480 NG_NODE_UNREF(node);
1485 /* Reset timer state after timeout */
1486 KASSERT(priv->timerActive, ("%s: !timerActive", __func__));
1487 priv->timerActive = 0;
1488 KASSERT(node->nd_refs > 1, ("%s: nd_refs=%d", __func__, node->nd_refs));
1489 NG_NODE_UNREF(node);
1491 /* Start timer again */
1492 ng_ppp_start_frag_timer(node);
1494 /* Scan the fragment queue */
1495 ng_ppp_frag_checkstale(node);
1500 * Deliver a frame out on the bundle, i.e., figure out how to fragment
1501 * the frame across the individual PPP links and do so.
1504 ng_ppp_mp_output(node_p node, struct mbuf *m)
1506 const priv_p priv = NG_NODE_PRIVATE(node);
1507 const int hdr_len = priv->conf.xmitShortSeq ? 2 : 4;
1508 int distrib[NG_PPP_MAX_LINKS];
1513 /* At least one link must be active */
1514 if (priv->numActiveLinks == 0) {
1519 /* Round-robin strategy */
1520 if (priv->conf.enableRoundRobin || m->m_pkthdr.len < MP_MIN_FRAG_LEN) {
1521 activeLinkNum = priv->lastLink++ % priv->numActiveLinks;
1522 bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0]));
1523 distrib[activeLinkNum] = m->m_pkthdr.len;
1527 /* Strategy when all links are equivalent (optimize the common case) */
1528 if (priv->allLinksEqual) {
1529 const int fraction = m->m_pkthdr.len / priv->numActiveLinks;
1532 for (i = 0; i < priv->numActiveLinks; i++)
1533 distrib[priv->lastLink++ % priv->numActiveLinks]
1535 remain = m->m_pkthdr.len - (fraction * priv->numActiveLinks);
1536 while (remain > 0) {
1537 distrib[priv->lastLink++ % priv->numActiveLinks]++;
1543 /* Strategy when all links are not equivalent */
1544 ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib);
1548 priv->bundleStats.xmitFrames++;
1549 priv->bundleStats.xmitOctets += m->m_pkthdr.len;
1551 /* Send alloted portions of frame out on the link(s) */
1552 for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1;
1553 activeLinkNum >= 0; activeLinkNum--) {
1554 const int linkNum = priv->activeLinks[activeLinkNum];
1555 struct ng_ppp_link *const link = &priv->links[linkNum];
1557 /* Deliver fragment(s) out the next link */
1558 for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) {
1559 int len, lastFragment, error;
1562 /* Calculate fragment length; don't exceed link MTU */
1563 len = distrib[activeLinkNum];
1564 if (len > link->conf.mru - hdr_len)
1565 len = link->conf.mru - hdr_len;
1566 distrib[activeLinkNum] -= len;
1567 lastFragment = (len == m->m_pkthdr.len);
1569 /* Split off next fragment as "m2" */
1571 if (!lastFragment) {
1572 struct mbuf *n = m_split(m, len, M_DONTWAIT);
1581 /* Prepend MP header */
1582 if (priv->conf.xmitShortSeq) {
1587 (priv->xseq + 1) & MP_SHORT_SEQ_MASK;
1589 shdr |= MP_SHORT_FIRST_FLAG;
1591 shdr |= MP_SHORT_LAST_FLAG;
1593 m2 = ng_ppp_prepend(m2, &shdr, 2);
1599 (priv->xseq + 1) & MP_LONG_SEQ_MASK;
1601 lhdr |= MP_LONG_FIRST_FLAG;
1603 lhdr |= MP_LONG_LAST_FLAG;
1605 m2 = ng_ppp_prepend(m2, &lhdr, 4);
1614 item = ng_package_data(m2, NULL);
1615 error = ng_ppp_output(node, 0, PROT_MP, linkNum, item);
1629 * Computing the optimal fragmentation
1630 * -----------------------------------
1632 * This routine tries to compute the optimal fragmentation pattern based
1633 * on each link's latency, bandwidth, and calculated additional latency.
1634 * The latter quantity is the additional latency caused by previously
1635 * written data that has not been transmitted yet.
1637 * This algorithm is only useful when not all of the links have the
1638 * same latency and bandwidth values.
1640 * The essential idea is to make the last bit of each fragment of the
1641 * frame arrive at the opposite end at the exact same time. This greedy
1642 * algorithm is optimal, in that no other scheduling could result in any
1643 * packet arriving any sooner unless packets are delivered out of order.
1645 * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and
1646 * latency l_i (in miliseconds). Consider the function function f_i(t)
1647 * which is equal to the number of bytes that will have arrived at
1648 * the peer after t miliseconds if we start writing continuously at
1649 * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i).
1650 * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i).
1651 * Note that the y-intersect is always <= zero because latency can't be
1652 * negative. Note also that really the function is f_i(t) except when
1653 * f_i(t) is negative, in which case the function is zero. To take
1654 * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }.
1655 * So the actual number of bytes that will have arrived at the peer after
1656 * t miliseconds is f_i(t) * Q_i(t).
1658 * At any given time, each link has some additional latency a_i >= 0
1659 * due to previously written fragment(s) which are still in the queue.
1660 * This value is easily computed from the time since last transmission,
1661 * the previous latency value, the number of bytes written, and the
1664 * Assume that l_i includes any a_i already, and that the links are
1665 * sorted by latency, so that l_i <= l_{i+1}.
1667 * Let N be the total number of bytes in the current frame we are sending.
1669 * Suppose we were to start writing bytes at time t = 0 on all links
1670 * simultaneously, which is the most we can possibly do. Then let
1671 * F(t) be equal to the total number of bytes received by the peer
1672 * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)).
1674 * Our goal is simply this: fragment the frame across the links such
1675 * that the peer is able to reconstruct the completed frame as soon as
1676 * possible, i.e., at the least possible value of t. Call this value t_0.
1678 * Then it follows that F(t_0) = N. Our strategy is first to find the value
1679 * of t_0, and then deduce how many bytes to write to each link.
1683 * t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) )
1685 * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will
1686 * lie in one of these ranges. To find it, we just need to find the i such
1687 * that F(l_i) <= N <= F(l_{i+1}). Then we compute all the constant values
1688 * for Q_i() in this range, plug in the remaining values, solving for t_0.
1690 * Once t_0 is known, then the number of bytes to send on link i is
1691 * just f_i(t_0) * Q_i(t_0).
1693 * In other words, we start allocating bytes to the links one at a time.
1694 * We keep adding links until the frame is completely sent. Some links
1695 * may not get any bytes because their latency is too high.
1697 * Is all this work really worth the trouble? Depends on the situation.
1698 * The bigger the ratio of computer speed to link speed, and the more
1699 * important total bundle latency is (e.g., for interactive response time),
1700 * the more it's worth it. There is however the cost of calling this
1701 * function for every frame. The running time is O(n^2) where n is the
1702 * number of links that receive a non-zero number of bytes.
1704 * Since latency is measured in miliseconds, the "resolution" of this
1705 * algorithm is one milisecond.
1707 * To avoid this algorithm altogether, configure all links to have the
1708 * same latency and bandwidth.
1711 ng_ppp_mp_strategy(node_p node, int len, int *distrib)
1713 const priv_p priv = NG_NODE_PRIVATE(node);
1714 int latency[NG_PPP_MAX_LINKS];
1715 int sortByLatency[NG_PPP_MAX_LINKS];
1717 int t0, total, topSum, botSum;
1719 int i, numFragments;
1721 /* If only one link, this gets real easy */
1722 if (priv->numActiveLinks == 1) {
1727 /* Get current time */
1728 getmicrouptime(&now);
1730 /* Compute latencies for each link at this point in time */
1731 for (activeLinkNum = 0;
1732 activeLinkNum < priv->numActiveLinks; activeLinkNum++) {
1733 struct ng_ppp_link *alink;
1734 struct timeval diff;
1737 /* Start with base latency value */
1738 alink = &priv->links[priv->activeLinks[activeLinkNum]];
1739 latency[activeLinkNum] = alink->latency;
1740 sortByLatency[activeLinkNum] = activeLinkNum; /* see below */
1742 /* Any additional latency? */
1743 if (alink->bytesInQueue == 0)
1746 /* Compute time delta since last write */
1748 timevalsub(&diff, &alink->lastWrite);
1749 if (now.tv_sec < 0 || diff.tv_sec >= 10) { /* sanity */
1750 alink->bytesInQueue = 0;
1754 /* How many bytes could have transmitted since last write? */
1755 xmitBytes = (alink->conf.bandwidth * diff.tv_sec)
1756 + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100;
1757 alink->bytesInQueue -= xmitBytes;
1758 if (alink->bytesInQueue < 0)
1759 alink->bytesInQueue = 0;
1761 latency[activeLinkNum] +=
1762 (100 * alink->bytesInQueue) / alink->conf.bandwidth;
1765 /* Sort active links by latency */
1766 qsort_r(sortByLatency,
1767 priv->numActiveLinks, sizeof(*sortByLatency), latency, ng_ppp_intcmp);
1769 /* Find the interval we need (add links in sortByLatency[] order) */
1770 for (numFragments = 1;
1771 numFragments < priv->numActiveLinks; numFragments++) {
1772 for (total = i = 0; i < numFragments; i++) {
1775 flowTime = latency[sortByLatency[numFragments]]
1776 - latency[sortByLatency[i]];
1777 total += ((flowTime * priv->links[
1778 priv->activeLinks[sortByLatency[i]]].conf.bandwidth)
1785 /* Solve for t_0 in that interval */
1786 for (topSum = botSum = i = 0; i < numFragments; i++) {
1787 int bw = priv->links[
1788 priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
1790 topSum += latency[sortByLatency[i]] * bw; /* / 100 */
1791 botSum += bw; /* / 100 */
1793 t0 = ((len * 100) + topSum + botSum / 2) / botSum;
1795 /* Compute f_i(t_0) all i */
1796 bzero(distrib, priv->numActiveLinks * sizeof(*distrib));
1797 for (total = i = 0; i < numFragments; i++) {
1798 int bw = priv->links[
1799 priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
1801 distrib[sortByLatency[i]] =
1802 (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100;
1803 total += distrib[sortByLatency[i]];
1806 /* Deal with any rounding error */
1808 struct ng_ppp_link *fastLink =
1809 &priv->links[priv->activeLinks[sortByLatency[0]]];
1812 /* Find the fastest link */
1813 for (i = 1; i < numFragments; i++) {
1814 struct ng_ppp_link *const link =
1815 &priv->links[priv->activeLinks[sortByLatency[i]]];
1817 if (link->conf.bandwidth > fastLink->conf.bandwidth) {
1822 distrib[sortByLatency[fast]] += len - total;
1823 } else while (total > len) {
1824 struct ng_ppp_link *slowLink =
1825 &priv->links[priv->activeLinks[sortByLatency[0]]];
1826 int delta, slow = 0;
1828 /* Find the slowest link that still has bytes to remove */
1829 for (i = 1; i < numFragments; i++) {
1830 struct ng_ppp_link *const link =
1831 &priv->links[priv->activeLinks[sortByLatency[i]]];
1833 if (distrib[sortByLatency[slow]] == 0
1834 || (distrib[sortByLatency[i]] > 0
1835 && link->conf.bandwidth <
1836 slowLink->conf.bandwidth)) {
1841 delta = total - len;
1842 if (delta > distrib[sortByLatency[slow]])
1843 delta = distrib[sortByLatency[slow]];
1844 distrib[sortByLatency[slow]] -= delta;
1850 * Compare two integers
1853 ng_ppp_intcmp(void *latency, const void *v1, const void *v2)
1855 const int index1 = *((const int *) v1);
1856 const int index2 = *((const int *) v2);
1858 return ((int *)latency)[index1] - ((int *)latency)[index2];
1862 * Prepend a possibly compressed PPP protocol number in front of a frame
1864 static struct mbuf *
1865 ng_ppp_addproto(struct mbuf *m, int proto, int compOK)
1867 if (compOK && PROT_COMPRESSABLE(proto)) {
1868 u_char pbyte = (u_char)proto;
1870 return ng_ppp_prepend(m, &pbyte, 1);
1872 u_int16_t pword = htons((u_int16_t)proto);
1874 return ng_ppp_prepend(m, &pword, 2);
1879 * Prepend some bytes to an mbuf
1881 static struct mbuf *
1882 ng_ppp_prepend(struct mbuf *m, const void *buf, int len)
1884 M_PREPEND(m, len, M_DONTWAIT);
1885 if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL))
1887 bcopy(buf, mtod(m, u_char *), len);
1892 * Update private information that is derived from other private information
1895 ng_ppp_update(node_p node, int newConf)
1897 const priv_p priv = NG_NODE_PRIVATE(node);
1900 /* Update active status for VJ Compression */
1901 priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL
1902 && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL
1903 && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL
1904 && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL;
1906 /* Increase latency for each link an amount equal to one MP header */
1908 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1911 hdrBytes = (priv->links[i].conf.enableACFComp ? 0 : 2)
1912 + (priv->links[i].conf.enableProtoComp ? 1 : 2)
1913 + (priv->conf.xmitShortSeq ? 2 : 4);
1914 priv->links[i].latency =
1915 priv->links[i].conf.latency +
1916 ((hdrBytes * priv->links[i].conf.bandwidth) + 50)
1921 /* Update list of active links */
1922 bzero(&priv->activeLinks, sizeof(priv->activeLinks));
1923 priv->numActiveLinks = 0;
1924 priv->allLinksEqual = 1;
1925 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1926 struct ng_ppp_link *const link = &priv->links[i];
1928 /* Is link active? */
1929 if (link->conf.enableLink && link->hook != NULL) {
1930 struct ng_ppp_link *link0;
1932 /* Add link to list of active links */
1933 priv->activeLinks[priv->numActiveLinks++] = i;
1934 link0 = &priv->links[priv->activeLinks[0]];
1936 /* Determine if all links are still equal */
1937 if (link->latency != link0->latency
1938 || link->conf.bandwidth != link0->conf.bandwidth)
1939 priv->allLinksEqual = 0;
1941 /* Initialize rec'd sequence number */
1942 if (link->seq == MP_NOSEQ) {
1943 link->seq = (link == link0) ?
1944 MP_INITIAL_SEQ : link0->seq;
1947 link->seq = MP_NOSEQ;
1950 /* Update MP state as multi-link is active or not */
1951 if (priv->conf.enableMultilink && priv->numActiveLinks > 0)
1952 ng_ppp_start_frag_timer(node);
1954 ng_ppp_stop_frag_timer(node);
1955 ng_ppp_frag_reset(node);
1956 priv->xseq = MP_INITIAL_SEQ;
1957 priv->mseq = MP_INITIAL_SEQ;
1958 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1959 struct ng_ppp_link *const link = &priv->links[i];
1961 bzero(&link->lastWrite, sizeof(link->lastWrite));
1962 link->bytesInQueue = 0;
1963 link->seq = MP_NOSEQ;
1969 * Determine if a new configuration would represent a valid change
1970 * from the current configuration and link activity status.
1973 ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf)
1975 const priv_p priv = NG_NODE_PRIVATE(node);
1976 int i, newNumLinksActive;
1978 /* Check per-link config and count how many links would be active */
1979 for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) {
1980 if (newConf->links[i].enableLink && priv->links[i].hook != NULL)
1981 newNumLinksActive++;
1982 if (!newConf->links[i].enableLink)
1984 if (newConf->links[i].mru < MP_MIN_LINK_MRU)
1986 if (newConf->links[i].bandwidth == 0)
1988 if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH)
1990 if (newConf->links[i].latency > NG_PPP_MAX_LATENCY)
1994 /* Check bundle parameters */
1995 if (newConf->bund.enableMultilink && newConf->bund.mrru < MP_MIN_MRRU)
1998 /* Disallow changes to multi-link configuration while MP is active */
1999 if (priv->numActiveLinks > 0 && newNumLinksActive > 0) {
2000 if (!priv->conf.enableMultilink
2001 != !newConf->bund.enableMultilink
2002 || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq
2003 || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq)
2007 /* At most one link can be active unless multi-link is enabled */
2008 if (!newConf->bund.enableMultilink && newNumLinksActive > 1)
2011 /* Configuration change would be valid */
2016 * Free all entries in the fragment queue
2019 ng_ppp_frag_reset(node_p node)
2021 const priv_p priv = NG_NODE_PRIVATE(node);
2022 struct ng_ppp_frag *qent, *qnext;
2024 for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) {
2025 qnext = TAILQ_NEXT(qent, f_qent);
2026 NG_FREE_M(qent->data);
2027 FREE(qent, M_NETGRAPH_PPP);
2029 TAILQ_INIT(&priv->frags);
2034 * Start fragment queue timer
2037 ng_ppp_start_frag_timer(node_p node)
2039 const priv_p priv = NG_NODE_PRIVATE(node);
2041 if (!priv->timerActive) {
2042 priv->fragTimer = timeout(ng_ppp_frag_timeout,
2043 node, MP_FRAGTIMER_INTERVAL);
2044 priv->timerActive = 1;
2050 * Stop fragment queue timer
2053 ng_ppp_stop_frag_timer(node_p node)
2055 const priv_p priv = NG_NODE_PRIVATE(node);
2057 if (priv->timerActive) {
2058 untimeout(ng_ppp_frag_timeout, node, priv->fragTimer);
2059 priv->timerActive = 0;
2060 KASSERT(node->nd_refs > 1,
2061 ("%s: nd_refs=%d", __func__, node->nd_refs));
2062 NG_NODE_UNREF(node);