]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_ppp.c
This commit was generated by cvs2svn to compensate for changes in r89402,
[FreeBSD/FreeBSD.git] / sys / netgraph / ng_ppp.c
1
2 /*
3  * ng_ppp.c
4  *
5  * Copyright (c) 1996-2000 Whistle Communications, Inc.
6  * All rights reserved.
7  * 
8  * Subject to the following obligations and disclaimer of warranty, use and
9  * redistribution of this software, in source or object code forms, with or
10  * without modifications are expressly permitted by Whistle Communications;
11  * provided, however, that:
12  * 1. Any and all reproductions of the source or object code must include the
13  *    copyright notice above and the following disclaimer of warranties; and
14  * 2. No rights are granted, in any manner or form, to use Whistle
15  *    Communications, Inc. trademarks, including the mark "WHISTLE
16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17  *    such appears in the above copyright notice or in the software.
18  * 
19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * Author: Archie Cobbs <archie@freebsd.org>
38  *
39  * $FreeBSD$
40  * $Whistle: ng_ppp.c,v 1.24 1999/11/01 09:24:52 julian Exp $
41  */
42
43 /*
44  * PPP node type.
45  */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/time.h>
51 #include <sys/mbuf.h>
52 #include <sys/malloc.h>
53 #include <sys/errno.h>
54 #include <sys/ctype.h>
55
56 #include <machine/limits.h>
57
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>
63
64 #ifdef NG_SEPARATE_MALLOC
65 MALLOC_DEFINE(M_NETGRAPH_PPP, "netgraph_ppp", "netgraph ppp node");
66 #else
67 #define M_NETGRAPH_PPP M_NETGRAPH
68 #endif
69
70 #define PROT_VALID(p)           (((p) & 0x0101) == 0x0001)
71 #define PROT_COMPRESSABLE(p)    (((p) & 0xff00) == 0x0000)
72
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
84
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
89
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 */
94
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 */
99
100 #define MP_NOSEQ                0x7fffffff      /* impossible sequence number */
101
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))
109
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))
114
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)))
119
120 /* Increment receive sequence number */
121 #define MP_NEXT_RECV_SEQ(priv,seq)                                          \
122                                 (((seq) + 1) & ((priv)->conf.recvShortSeq ? \
123                                     MP_SHORT_SEQ_MASK : MP_LONG_SEQ_MASK))
124
125 /* Don't fragment transmitted packets smaller than this */
126 #define MP_MIN_FRAG_LEN         6
127
128 /* Maximum fragment reasssembly queue length */
129 #define MP_MAX_QUEUE_LEN        128
130
131 /* Fragment queue scanner period */
132 #define MP_FRAGTIMER_INTERVAL   (hz/2)
133
134 /* We store incoming fragments this way */
135 struct ng_ppp_frag {
136         int                             seq;            /* fragment seq# */
137         u_char                          first;          /* First in packet? */
138         u_char                          last;           /* Last in packet? */
139         struct timeval                  timestamp;      /* time of reception */
140         struct mbuf                     *data;          /* Fragment data */
141         meta_p                          meta;           /* Fragment meta */
142         TAILQ_ENTRY(ng_ppp_frag)        f_qent;         /* Fragment queue */
143 };
144
145 /* We use integer indicies to refer to the non-link hooks */
146 static const char *const ng_ppp_hook_names[] = {
147         NG_PPP_HOOK_ATALK,
148 #define HOOK_INDEX_ATALK                0
149         NG_PPP_HOOK_BYPASS,
150 #define HOOK_INDEX_BYPASS               1
151         NG_PPP_HOOK_COMPRESS,
152 #define HOOK_INDEX_COMPRESS             2
153         NG_PPP_HOOK_ENCRYPT,
154 #define HOOK_INDEX_ENCRYPT              3
155         NG_PPP_HOOK_DECOMPRESS,
156 #define HOOK_INDEX_DECOMPRESS           4
157         NG_PPP_HOOK_DECRYPT,
158 #define HOOK_INDEX_DECRYPT              5
159         NG_PPP_HOOK_INET,
160 #define HOOK_INDEX_INET                 6
161         NG_PPP_HOOK_IPX,
162 #define HOOK_INDEX_IPX                  7
163         NG_PPP_HOOK_VJC_COMP,
164 #define HOOK_INDEX_VJC_COMP             8
165         NG_PPP_HOOK_VJC_IP,
166 #define HOOK_INDEX_VJC_IP               9
167         NG_PPP_HOOK_VJC_UNCOMP,
168 #define HOOK_INDEX_VJC_UNCOMP           10
169         NG_PPP_HOOK_VJC_VJIP,
170 #define HOOK_INDEX_VJC_VJIP             11
171         NG_PPP_HOOK_IPV6,
172 #define HOOK_INDEX_IPV6                 12
173         NULL
174 #define HOOK_INDEX_MAX                  13
175 };
176
177 /* We store index numbers in the hook private pointer. The HOOK_INDEX()
178    for a hook is either the index (above) for normal hooks, or the ones
179    complement of the link number for link hooks.
180 XXX Not any more.. (what a hack)
181 #define HOOK_INDEX(hook)        (*((int16_t *) &(hook)->private))
182 */
183
184 /* Per-link private information */
185 struct ng_ppp_link {
186         struct ng_ppp_link_conf conf;           /* link configuration */
187         hook_p                  hook;           /* connection to link data */
188         int32_t                 seq;            /* highest rec'd seq# - MSEQ */
189         struct timeval          lastWrite;      /* time of last write */
190         int                     bytesInQueue;   /* bytes in the output queue */
191         struct ng_ppp_link_stat stats;          /* Link stats */
192 };
193
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 */
209                                 frags;
210         int                     qlen;                   /* fraq queue length */
211         struct callout_handle   fragTimer;              /* fraq queue check */
212 };
213 typedef struct ng_ppp_private *priv_p;
214
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;
222
223 /* Helper functions */
224 static int      ng_ppp_input(node_p node, int bypass,
225                         int linkNum, item_p item);
226 static int      ng_ppp_output(node_p node, int bypass, int proto,
227                         int linkNum, item_p item);
228 static int      ng_ppp_mp_input(node_p node, int linkNum, item_p item);
229 static int      ng_ppp_check_packet(node_p node);
230 static void     ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap);
231 static int      ng_ppp_frag_process(node_p node);
232 static int      ng_ppp_frag_trim(node_p node);
233 static void     ng_ppp_frag_timeout(void *arg);
234 static void     ng_ppp_frag_checkstale(node_p node);
235 static void     ng_ppp_frag_reset(node_p node);
236 static int      ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta);
237 static void     ng_ppp_mp_strategy(node_p node, int len, int *distrib);
238 static int      ng_ppp_intcmp(const void *v1, const void *v2);
239 static struct   mbuf *ng_ppp_addproto(struct mbuf *m, int proto, int compOK);
240 static struct   mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len);
241 static int      ng_ppp_config_valid(node_p node,
242                         const struct ng_ppp_node_conf *newConf);
243 static void     ng_ppp_update(node_p node, int newConf);
244 static void     ng_ppp_start_frag_timer(node_p node);
245 static void     ng_ppp_stop_frag_timer(node_p node);
246
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,
250         NG_PPP_MAX_LINKS
251 };
252 static const struct ng_parse_type ng_ppp_rseq_array_type = {
253         &ng_parse_fixedarray_type,
254         &ng_ppp_rseq_array_info,
255 };
256 static const struct ng_parse_struct_info ng_ppp_mp_state_type_info
257         = NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type);
258 static const struct ng_parse_type ng_ppp_mp_state_type = {
259         &ng_parse_struct_type,
260         &ng_ppp_mp_state_type_info,
261 };
262
263 /* Parse type for struct ng_ppp_link_conf */
264 static const struct ng_parse_struct_info
265         ng_ppp_link_type_info = NG_PPP_LINK_TYPE_INFO;
266 static const struct ng_parse_type ng_ppp_link_type = {
267         &ng_parse_struct_type,
268         &ng_ppp_link_type_info,
269 };
270
271 /* Parse type for struct ng_ppp_bund_conf */
272 static const struct ng_parse_struct_info
273         ng_ppp_bund_type_info = NG_PPP_BUND_TYPE_INFO;
274 static const struct ng_parse_type ng_ppp_bund_type = {
275         &ng_parse_struct_type,
276         &ng_ppp_bund_type_info,
277 };
278
279 /* Parse type for struct ng_ppp_node_conf */
280 static const struct ng_parse_fixedarray_info ng_ppp_array_info = {
281         &ng_ppp_link_type,
282         NG_PPP_MAX_LINKS
283 };
284 static const struct ng_parse_type ng_ppp_link_array_type = {
285         &ng_parse_fixedarray_type,
286         &ng_ppp_array_info,
287 };
288 static const struct ng_parse_struct_info ng_ppp_conf_type_info
289         = NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type);
290 static const struct ng_parse_type ng_ppp_conf_type = {
291         &ng_parse_struct_type,
292         &ng_ppp_conf_type_info
293 };
294
295 /* Parse type for struct ng_ppp_link_stat */
296 static const struct ng_parse_struct_info
297         ng_ppp_stats_type_info = NG_PPP_STATS_TYPE_INFO;
298 static const struct ng_parse_type ng_ppp_stats_type = {
299         &ng_parse_struct_type,
300         &ng_ppp_stats_type_info
301 };
302
303 /* List of commands and how to convert arguments to/from ASCII */
304 static const struct ng_cmdlist ng_ppp_cmds[] = {
305         {
306           NGM_PPP_COOKIE,
307           NGM_PPP_SET_CONFIG,
308           "setconfig",
309           &ng_ppp_conf_type,
310           NULL
311         },
312         {
313           NGM_PPP_COOKIE,
314           NGM_PPP_GET_CONFIG,
315           "getconfig",
316           NULL,
317           &ng_ppp_conf_type
318         },
319         {
320           NGM_PPP_COOKIE,
321           NGM_PPP_GET_MP_STATE,
322           "getmpstate",
323           NULL,
324           &ng_ppp_mp_state_type
325         },
326         {
327           NGM_PPP_COOKIE,
328           NGM_PPP_GET_LINK_STATS,
329           "getstats",
330           &ng_parse_int16_type,
331           &ng_ppp_stats_type
332         },
333         {
334           NGM_PPP_COOKIE,
335           NGM_PPP_CLR_LINK_STATS,
336           "clrstats",
337           &ng_parse_int16_type,
338           NULL
339         },
340         {
341           NGM_PPP_COOKIE,
342           NGM_PPP_GETCLR_LINK_STATS,
343           "getclrstats",
344           &ng_parse_int16_type,
345           &ng_ppp_stats_type
346         },
347         { 0 }
348 };
349
350 /* Node type descriptor */
351 static struct ng_type ng_ppp_typestruct = {
352         NG_ABI_VERSION,
353         NG_PPP_NODE_TYPE,
354         NULL,
355         ng_ppp_constructor,
356         ng_ppp_rcvmsg,
357         ng_ppp_shutdown,
358         ng_ppp_newhook,
359         NULL,
360         NULL,
361         ng_ppp_rcvdata,
362         ng_ppp_disconnect,
363         ng_ppp_cmds
364 };
365 NETGRAPH_INIT(ppp, &ng_ppp_typestruct);
366
367 static int *compareLatencies;                   /* hack for ng_ppp_intcmp() */
368
369 /* Address and control field header */
370 static const u_char ng_ppp_acf[2] = { 0xff, 0x03 };
371
372 /* Maximum time we'll let a complete incoming packet sit in the queue */
373 static const struct timeval ng_ppp_max_staleness = { 2, 0 };    /* 2 seconds */
374
375 #define ERROUT(x)       do { error = (x); goto done; } while (0)
376
377 /************************************************************************
378                         NETGRAPH NODE STUFF
379  ************************************************************************/
380
381 /*
382  * Node type constructor
383  */
384 static int
385 ng_ppp_constructor(node_p node)
386 {
387         priv_p priv;
388         int i;
389
390         /* Allocate private structure */
391         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_PPP, M_NOWAIT | M_ZERO);
392         if (priv == NULL)
393                 return (ENOMEM);
394
395         NG_NODE_SET_PRIVATE(node, priv);
396
397         /* Initialize state */
398         TAILQ_INIT(&priv->frags);
399         for (i = 0; i < NG_PPP_MAX_LINKS; i++)
400                 priv->links[i].seq = MP_NOSEQ;
401         callout_handle_init(&priv->fragTimer);
402
403         /* Done */
404         return (0);
405 }
406
407 /*
408  * Give our OK for a hook to be added
409  */
410 static int
411 ng_ppp_newhook(node_p node, hook_p hook, const char *name)
412 {
413         const priv_p priv = NG_NODE_PRIVATE(node);
414         int linkNum = -1;
415         hook_p *hookPtr = NULL;
416         int hookIndex = -1;
417
418         /* Figure out which hook it is */
419         if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX,      /* a link hook? */
420             strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) {
421                 const char *cp;
422                 char *eptr;
423
424                 cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
425                 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
426                         return (EINVAL);
427                 linkNum = (int)strtoul(cp, &eptr, 10);
428                 if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS)
429                         return (EINVAL);
430                 hookPtr = &priv->links[linkNum].hook;
431                 hookIndex = ~linkNum;
432         } else {                                /* must be a non-link hook */
433                 int i;
434
435                 for (i = 0; ng_ppp_hook_names[i] != NULL; i++) {
436                         if (strcmp(name, ng_ppp_hook_names[i]) == 0) {
437                                 hookPtr = &priv->hooks[i];
438                                 hookIndex = i;
439                                 break;
440                         }
441                 }
442                 if (ng_ppp_hook_names[i] == NULL)
443                         return (EINVAL);        /* no such hook */
444         }
445
446         /* See if hook is already connected */
447         if (*hookPtr != NULL)
448                 return (EISCONN);
449
450         /* Disallow more than one link unless multilink is enabled */
451         if (linkNum != -1 && priv->links[linkNum].conf.enableLink
452             && !priv->conf.enableMultilink && priv->numActiveLinks >= 1)
453                 return (ENODEV);
454
455         /* OK */
456         *hookPtr = hook;
457         NG_HOOK_SET_PRIVATE(hook, (void *)hookIndex);
458         ng_ppp_update(node, 0);
459         return (0);
460 }
461
462 /*
463  * Receive a control message
464  */
465 static int
466 ng_ppp_rcvmsg(node_p node, item_p item, hook_p lasthook)
467 {
468         const priv_p priv = NG_NODE_PRIVATE(node);
469         struct ng_mesg *resp = NULL;
470         int error = 0;
471         struct ng_mesg *msg;
472
473         NGI_GET_MSG(item, msg);
474         switch (msg->header.typecookie) {
475         case NGM_PPP_COOKIE:
476                 switch (msg->header.cmd) {
477                 case NGM_PPP_SET_CONFIG:
478                     {
479                         struct ng_ppp_node_conf *const conf =
480                             (struct ng_ppp_node_conf *)msg->data;
481                         int i;
482
483                         /* Check for invalid or illegal config */
484                         if (msg->header.arglen != sizeof(*conf))
485                                 ERROUT(EINVAL);
486                         if (!ng_ppp_config_valid(node, conf))
487                                 ERROUT(EINVAL);
488
489                         /* Copy config */
490                         priv->conf = conf->bund;
491                         for (i = 0; i < NG_PPP_MAX_LINKS; i++)
492                                 priv->links[i].conf = conf->links[i];
493                         ng_ppp_update(node, 1);
494                         break;
495                     }
496                 case NGM_PPP_GET_CONFIG:
497                     {
498                         struct ng_ppp_node_conf *conf;
499                         int i;
500
501                         NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
502                         if (resp == NULL)
503                                 ERROUT(ENOMEM);
504                         conf = (struct ng_ppp_node_conf *)resp->data;
505                         conf->bund = priv->conf;
506                         for (i = 0; i < NG_PPP_MAX_LINKS; i++)
507                                 conf->links[i] = priv->links[i].conf;
508                         break;
509                     }
510                 case NGM_PPP_GET_MP_STATE:
511                     {
512                         struct ng_ppp_mp_state *info;
513                         int i;
514
515                         NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT);
516                         if (resp == NULL)
517                                 ERROUT(ENOMEM);
518                         info = (struct ng_ppp_mp_state *)resp->data;
519                         bzero(info, sizeof(*info));
520                         for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
521                                 if (priv->links[i].seq != MP_NOSEQ)
522                                         info->rseq[i] = priv->links[i].seq;
523                         }
524                         info->mseq = priv->mseq;
525                         info->xseq = priv->xseq;
526                         break;
527                     }
528                 case NGM_PPP_GET_LINK_STATS:
529                 case NGM_PPP_CLR_LINK_STATS:
530                 case NGM_PPP_GETCLR_LINK_STATS:
531                     {
532                         struct ng_ppp_link_stat *stats;
533                         u_int16_t linkNum;
534
535                         if (msg->header.arglen != sizeof(u_int16_t))
536                                 ERROUT(EINVAL);
537                         linkNum = *((u_int16_t *) msg->data);
538                         if (linkNum >= NG_PPP_MAX_LINKS
539                             && linkNum != NG_PPP_BUNDLE_LINKNUM)
540                                 ERROUT(EINVAL);
541                         stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ?
542                             &priv->bundleStats : &priv->links[linkNum].stats;
543                         if (msg->header.cmd != NGM_PPP_CLR_LINK_STATS) {
544                                 NG_MKRESPONSE(resp, msg,
545                                     sizeof(struct ng_ppp_link_stat), M_NOWAIT);
546                                 if (resp == NULL)
547                                         ERROUT(ENOMEM);
548                                 bcopy(stats, resp->data, sizeof(*stats));
549                         }
550                         if (msg->header.cmd != NGM_PPP_GET_LINK_STATS)
551                                 bzero(stats, sizeof(*stats));
552                         break;
553                     }
554                 default:
555                         error = EINVAL;
556                         break;
557                 }
558                 break;
559         case NGM_VJC_COOKIE:
560             {
561                 /*
562                  * Forward it to the vjc node. leave the 
563                  * old return address alone.
564                  * If we have no hook, let NG_RESPOND_MSG
565                  * clean up any remaining resources.
566                  * Because we have no resp, the item will be freed
567                  * along with anything it references. Don't
568                  * let msg be freed twice.
569                  */
570                 NGI_MSG(item) = msg;    /* put it back in the item */
571                 msg = NULL;
572                 if ((lasthook = priv->links[HOOK_INDEX_VJC_IP].hook)) {
573                         NG_FWD_ITEM_HOOK(error, item, lasthook);
574                 }
575                 return (error);
576             }
577         default:
578                 error = EINVAL;
579                 break;
580         }
581 done:
582         NG_RESPOND_MSG(error, node, item, resp);
583         NG_FREE_MSG(msg);
584         return (error);
585 }
586
587 /*
588  * Receive data on a hook
589  */
590 static int
591 ng_ppp_rcvdata(hook_p hook, item_p item)
592 {
593         const node_p node = NG_HOOK_NODE(hook);
594         const priv_p priv = NG_NODE_PRIVATE(node);
595         const int index = (int)NG_HOOK_PRIVATE(hook);
596         u_int16_t linkNum = NG_PPP_BUNDLE_LINKNUM;
597         hook_p outHook = NULL;
598         int proto = 0, error;
599         struct mbuf *m;
600
601         NGI_GET_M(item, m);
602         /* Did it come from a link hook? */
603         if (index < 0) {
604                 struct ng_ppp_link *link;
605
606                 /* Convert index into a link number */
607                 linkNum = (u_int16_t)~index;
608                 KASSERT(linkNum < NG_PPP_MAX_LINKS,
609                     ("%s: bogus index 0x%x", __func__, index));
610                 link = &priv->links[linkNum];
611
612                 /* Stats */
613                 link->stats.recvFrames++;
614                 link->stats.recvOctets += m->m_pkthdr.len;
615
616                 /* Strip address and control fields, if present */
617                 if (m->m_pkthdr.len >= 2) {
618                         if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
619                                 NG_FREE_ITEM(item);
620                                 return (ENOBUFS);
621                         }
622                         if (bcmp(mtod(m, u_char *), &ng_ppp_acf, 2) == 0)
623                                 m_adj(m, 2);
624                 }
625
626                 /* Dispatch incoming frame (if not enabled, to bypass) */
627                 NGI_M(item) = m;        /* put changed m back in item */
628                 return ng_ppp_input(node,
629                     !link->conf.enableLink, linkNum, item);
630         }
631
632         /* Get protocol & check if data allowed from this hook */
633         NGI_M(item) = m;        /* put possibly changed m back in item */
634         switch (index) {
635
636         /* Outgoing data */
637         case HOOK_INDEX_ATALK:
638                 if (!priv->conf.enableAtalk) {
639                         NG_FREE_ITEM(item);
640                         return (ENXIO);
641                 }
642                 proto = PROT_APPLETALK;
643                 break;
644         case HOOK_INDEX_IPX:
645                 if (!priv->conf.enableIPX) {
646                         NG_FREE_ITEM(item);
647                         return (ENXIO);
648                 }
649                 proto = PROT_IPX;
650                 break;
651         case HOOK_INDEX_IPV6:
652                 if (!priv->conf.enableIPv6) {
653                         NG_FREE_ITEM(item);
654                         return (ENXIO);
655                 }
656                 proto = PROT_IPV6;
657                 break;
658         case HOOK_INDEX_INET:
659         case HOOK_INDEX_VJC_VJIP:
660                 if (!priv->conf.enableIP) {
661                         NG_FREE_ITEM(item);
662                         return (ENXIO);
663                 }
664                 proto = PROT_IP;
665                 break;
666         case HOOK_INDEX_VJC_COMP:
667                 if (!priv->conf.enableVJCompression) {
668                         NG_FREE_ITEM(item);
669                         return (ENXIO);
670                 }
671                 proto = PROT_VJCOMP;
672                 break;
673         case HOOK_INDEX_VJC_UNCOMP:
674                 if (!priv->conf.enableVJCompression) {
675                         NG_FREE_ITEM(item);
676                         return (ENXIO);
677                 }
678                 proto = PROT_VJUNCOMP;
679                 break;
680         case HOOK_INDEX_COMPRESS:
681                 if (!priv->conf.enableCompression) {
682                         NG_FREE_ITEM(item);
683                         return (ENXIO);
684                 }
685                 proto = PROT_COMPD;
686                 break;
687         case HOOK_INDEX_ENCRYPT:
688                 if (!priv->conf.enableEncryption) {
689                         NG_FREE_ITEM(item);
690                         return (ENXIO);
691                 }
692                 proto = PROT_CRYPTD;
693                 break;
694         case HOOK_INDEX_BYPASS:
695                 if (m->m_pkthdr.len < 4) {
696                         NG_FREE_ITEM(item);
697                         return (EINVAL);
698                 }
699                 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
700                         NGI_M(item) = NULL; /* don't free twice */
701                         NG_FREE_ITEM(item);
702                         return (ENOBUFS);
703                 }
704                 NGI_M(item) = m; /* m may have changed */
705                 linkNum = ntohs(mtod(m, u_int16_t *)[0]);
706                 proto = ntohs(mtod(m, u_int16_t *)[1]);
707                 m_adj(m, 4);
708                 if (linkNum >= NG_PPP_MAX_LINKS
709                     && linkNum != NG_PPP_BUNDLE_LINKNUM) {
710                         NG_FREE_ITEM(item);
711                         return (EINVAL);
712                 }
713                 break;
714
715         /* Incoming data */
716         case HOOK_INDEX_VJC_IP:
717                 if (!priv->conf.enableIP || !priv->conf.enableVJDecompression) {
718                         NG_FREE_ITEM(item);
719                         return (ENXIO);
720                 }
721                 break;
722         case HOOK_INDEX_DECOMPRESS:
723                 if (!priv->conf.enableDecompression) {
724                         NG_FREE_ITEM(item);
725                         return (ENXIO);
726                 }
727                 break;
728         case HOOK_INDEX_DECRYPT:
729                 if (!priv->conf.enableDecryption) {
730                         NG_FREE_ITEM(item);
731                         return (ENXIO);
732                 }
733                 break;
734         default:
735                 panic("%s: bogus index 0x%x", __func__, index);
736         }
737
738         /* Now figure out what to do with the frame */
739         switch (index) {
740
741         /* Outgoing data */
742         case HOOK_INDEX_INET:
743                 if (priv->conf.enableVJCompression && priv->vjCompHooked) {
744                         outHook = priv->hooks[HOOK_INDEX_VJC_IP];
745                         break;
746                 }
747                 /* FALLTHROUGH */
748         case HOOK_INDEX_ATALK:
749         case HOOK_INDEX_IPV6:
750         case HOOK_INDEX_IPX:
751         case HOOK_INDEX_VJC_COMP:
752         case HOOK_INDEX_VJC_UNCOMP:
753         case HOOK_INDEX_VJC_VJIP:
754                 if (priv->conf.enableCompression
755                     && priv->hooks[HOOK_INDEX_COMPRESS] != NULL) {
756                         if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
757                                 NGI_M(item) = NULL;
758                                 NG_FREE_ITEM(item);
759                                 return (ENOBUFS);
760                         }
761                         NGI_M(item) = m; /* m may have changed */
762                         outHook = priv->hooks[HOOK_INDEX_COMPRESS];
763                         break;
764                 }
765                 /* FALLTHROUGH */
766         case HOOK_INDEX_COMPRESS:
767                 if (priv->conf.enableEncryption
768                     && priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) {
769                         if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
770                                 NGI_M(item) = NULL;
771                                 NG_FREE_ITEM(item);
772                                 return (ENOBUFS);
773                         }
774                         NGI_M(item) = m; /* m may have changed */
775                         outHook = priv->hooks[HOOK_INDEX_ENCRYPT];
776                         break;
777                 }
778                 /* FALLTHROUGH */
779         case HOOK_INDEX_ENCRYPT:
780                 return ng_ppp_output(node, 0, proto, NG_PPP_BUNDLE_LINKNUM, item);
781
782         case HOOK_INDEX_BYPASS:
783                 return ng_ppp_output(node, 1, proto, linkNum, item);
784
785         /* Incoming data */
786         case HOOK_INDEX_DECRYPT:
787         case HOOK_INDEX_DECOMPRESS:
788                 return ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
789
790         case HOOK_INDEX_VJC_IP:
791                 outHook = priv->hooks[HOOK_INDEX_INET];
792                 break;
793         }
794
795         /* Send packet out hook */
796         NG_FWD_ITEM_HOOK(error, item, outHook);
797         return (error);
798 }
799
800 /*
801  * Destroy node
802  */
803 static int
804 ng_ppp_shutdown(node_p node)
805 {
806         const priv_p priv = NG_NODE_PRIVATE(node);
807
808         /* Stop fragment queue timer */
809         ng_ppp_stop_frag_timer(node);
810
811         /* Take down netgraph node */
812         ng_ppp_frag_reset(node);
813         bzero(priv, sizeof(*priv));
814         FREE(priv, M_NETGRAPH_PPP);
815         NG_NODE_SET_PRIVATE(node, NULL);
816         NG_NODE_UNREF(node);            /* let the node escape */
817         return (0);
818 }
819
820 /*
821  * Hook disconnection
822  */
823 static int
824 ng_ppp_disconnect(hook_p hook)
825 {
826         const node_p node = NG_HOOK_NODE(hook);
827         const priv_p priv = NG_NODE_PRIVATE(node);
828         const int index = (int)NG_HOOK_PRIVATE(hook);
829
830         /* Zero out hook pointer */
831         if (index < 0)
832                 priv->links[~index].hook = NULL;
833         else
834                 priv->hooks[index] = NULL;
835
836         /* Update derived info (or go away if no hooks left) */
837         if (NG_NODE_NUMHOOKS(node) > 0) {
838                 ng_ppp_update(node, 0);
839         } else {
840                 if (NG_NODE_IS_VALID(node)) {
841                         ng_rmnode_self(node);
842                 }
843         }
844         return (0);
845 }
846
847 /************************************************************************
848                         HELPER STUFF
849  ************************************************************************/
850
851 /*
852  * Handle an incoming frame.  Extract the PPP protocol number
853  * and dispatch accordingly.
854  */
855 static int
856 ng_ppp_input(node_p node, int bypass, int linkNum, item_p item)
857 {
858         const priv_p priv = NG_NODE_PRIVATE(node);
859         hook_p outHook = NULL;
860         int proto, error;
861         struct mbuf *m;
862
863
864         NGI_GET_M(item, m);
865         /* Extract protocol number */
866         for (proto = 0; !PROT_VALID(proto) && m->m_pkthdr.len > 0; ) {
867                 if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) {
868                         NG_FREE_ITEM(item);
869                         return (ENOBUFS);
870                 }
871                 proto = (proto << 8) + *mtod(m, u_char *);
872                 m_adj(m, 1);
873         }
874         if (!PROT_VALID(proto)) {
875                 if (linkNum == NG_PPP_BUNDLE_LINKNUM)
876                         priv->bundleStats.badProtos++;
877                 else
878                         priv->links[linkNum].stats.badProtos++;
879                 NG_FREE_ITEM(item);
880                 NG_FREE_M(m);
881                 return (EINVAL);
882         }
883
884         /* Bypass frame? */
885         if (bypass)
886                 goto bypass;
887
888         /* Check protocol */
889         switch (proto) {
890         case PROT_COMPD:
891                 if (priv->conf.enableDecompression)
892                         outHook = priv->hooks[HOOK_INDEX_DECOMPRESS];
893                 break;
894         case PROT_CRYPTD:
895                 if (priv->conf.enableDecryption)
896                         outHook = priv->hooks[HOOK_INDEX_DECRYPT];
897                 break;
898         case PROT_VJCOMP:
899                 if (priv->conf.enableVJDecompression && priv->vjCompHooked)
900                         outHook = priv->hooks[HOOK_INDEX_VJC_COMP];
901                 break;
902         case PROT_VJUNCOMP:
903                 if (priv->conf.enableVJDecompression && priv->vjCompHooked)
904                         outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP];
905                 break;
906         case PROT_MP:
907                 if (priv->conf.enableMultilink
908                     && linkNum != NG_PPP_BUNDLE_LINKNUM)
909                         return ng_ppp_mp_input(node, linkNum, item);
910                 break;
911         case PROT_APPLETALK:
912                 if (priv->conf.enableAtalk)
913                         outHook = priv->hooks[HOOK_INDEX_ATALK];
914                 break;
915         case PROT_IPX:
916                 if (priv->conf.enableIPX)
917                         outHook = priv->hooks[HOOK_INDEX_IPX];
918                 break;
919         case PROT_IP:
920                 if (priv->conf.enableIP)
921                         outHook = priv->hooks[HOOK_INDEX_INET];
922                 break;
923         case PROT_IPV6:
924                 if (priv->conf.enableIPv6)
925                         outHook = priv->hooks[HOOK_INDEX_IPV6];
926                 break;
927         }
928
929 bypass:
930         /* For unknown/inactive protocols, forward out the bypass hook */
931         if (outHook == NULL) {
932                 u_int16_t hdr[2];
933
934                 hdr[0] = htons(linkNum);
935                 hdr[1] = htons((u_int16_t)proto);
936                 if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) {
937                         NG_FREE_ITEM(item);
938                         return (ENOBUFS);
939                 }
940                 outHook = priv->hooks[HOOK_INDEX_BYPASS];
941         }
942
943         /* Forward frame */
944         NG_FWD_NEW_DATA(error, item, outHook, m);
945         return (error);
946 }
947
948 /*
949  * Deliver a frame out a link, either a real one or NG_PPP_BUNDLE_LINKNUM
950  * If the link is not enabled then ENXIO is returned, unless "bypass" is != 0.
951  */
952 static int
953 ng_ppp_output(node_p node, int bypass,
954         int proto, int linkNum, item_p item)
955 {
956         const priv_p priv = NG_NODE_PRIVATE(node);
957         struct ng_ppp_link *link;
958         int len, error;
959         struct mbuf *m;
960
961          NGI_GET_M(item, m);    /* separate them for a while */
962         /* If not doing MP, map bundle virtual link to (the only) link */
963         if (linkNum == NG_PPP_BUNDLE_LINKNUM && !priv->conf.enableMultilink)
964                 linkNum = priv->activeLinks[0];
965
966         /* Get link pointer (optimization) */
967         link = (linkNum != NG_PPP_BUNDLE_LINKNUM) ?
968             &priv->links[linkNum] : NULL;
969
970         /* Check link status (if real) */
971         if (linkNum != NG_PPP_BUNDLE_LINKNUM) {
972                 if (!bypass && !link->conf.enableLink) {
973                         NG_FREE_M(m);
974                         NG_FREE_ITEM(item);
975                         return (ENXIO);
976                 }
977                 if (link->hook == NULL) {
978                         NG_FREE_M(m);
979                         NG_FREE_ITEM(item);
980                         return (ENETDOWN);
981                 }
982         }
983
984         /* Prepend protocol number, possibly compressed */
985         if ((m = ng_ppp_addproto(m, proto,
986             linkNum == NG_PPP_BUNDLE_LINKNUM
987               || link->conf.enableProtoComp)) == NULL) {
988                 NG_FREE_ITEM(item);
989                 return (ENOBUFS);
990         }
991
992         /* Special handling for the MP virtual link */
993         if (linkNum == NG_PPP_BUNDLE_LINKNUM) {
994                 meta_p meta;
995
996                 /* strip off and discard the queue item */
997                 NGI_GET_META(item, meta);
998                 NG_FREE_ITEM(item);
999                 return ng_ppp_mp_output(node, m, meta);
1000         }
1001
1002         /* Prepend address and control field (unless compressed) */
1003         if (proto == PROT_LCP || !link->conf.enableACFComp) {
1004                 if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) {
1005                         NG_FREE_ITEM(item);
1006                         return (ENOBUFS);
1007                 }
1008         }
1009
1010         /* Deliver frame */
1011         len = m->m_pkthdr.len;
1012         NG_FWD_NEW_DATA(error, item,  link->hook, m);
1013
1014         /* Update stats and 'bytes in queue' counter */
1015         if (error == 0) {
1016                 link->stats.xmitFrames++;
1017                 link->stats.xmitOctets += len;
1018                 link->bytesInQueue += len;
1019                 getmicrouptime(&link->lastWrite);
1020         }
1021         return error;
1022 }
1023
1024 /*
1025  * Handle an incoming multi-link fragment
1026  *
1027  * The fragment reassembly algorithm is somewhat complex. This is mainly
1028  * because we are required not to reorder the reconstructed packets, yet
1029  * fragments are only guaranteed to arrive in order on a per-link basis.
1030  * In other words, when we have a complete packet ready, but the previous
1031  * packet is still incomplete, we have to decide between delivering the
1032  * complete packet and throwing away the incomplete one, or waiting to
1033  * see if the remainder of the incomplete one arrives, at which time we
1034  * can deliver both packets, in order.
1035  *
1036  * This problem is exacerbated by "sequence number slew", which is when
1037  * the sequence numbers coming in from different links are far apart from
1038  * each other. In particular, certain unnamed equipment (*cough* Ascend)
1039  * has been seen to generate sequence number slew of up to 10 on an ISDN
1040  * 2B-channel MP link. There is nothing invalid about sequence number slew
1041  * but it makes the reasssembly process have to work harder.
1042  *
1043  * However, the peer is required to transmit fragments in order on each
1044  * link. That means if we define MSEQ as the minimum over all links of
1045  * the highest sequence number received on that link, then we can always
1046  * give up any hope of receiving a fragment with sequence number < MSEQ in
1047  * the future (all of this using 'wraparound' sequence number space).
1048  * Therefore we can always immediately throw away incomplete packets
1049  * missing fragments with sequence numbers < MSEQ.
1050  *
1051  * Here is an overview of our algorithm:
1052  *
1053  *    o Received fragments are inserted into a queue, for which we
1054  *      maintain these invariants between calls to this function:
1055  *
1056  *      - Fragments are ordered in the queue by sequence number
1057  *      - If a complete packet is at the head of the queue, then
1058  *        the first fragment in the packet has seq# > MSEQ + 1
1059  *        (otherwise, we could deliver it immediately)
1060  *      - If any fragments have seq# < MSEQ, then they are necessarily
1061  *        part of a packet whose missing seq#'s are all > MSEQ (otherwise,
1062  *        we can throw them away because they'll never be completed)
1063  *      - The queue contains at most MP_MAX_QUEUE_LEN fragments
1064  *
1065  *    o We have a periodic timer that checks the queue for the first
1066  *      complete packet that has been sitting in the queue "too long".
1067  *      When one is detected, all previous (incomplete) fragments are
1068  *      discarded, their missing fragments are declared lost and MSEQ
1069  *      is increased.
1070  *
1071  *    o If we recieve a fragment with seq# < MSEQ, we throw it away
1072  *      because we've already delcared it lost.
1073  *
1074  * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM.
1075  */
1076 static int
1077 ng_ppp_mp_input(node_p node, int linkNum, item_p item)
1078 {
1079         const priv_p priv = NG_NODE_PRIVATE(node);
1080         struct ng_ppp_link *const link = &priv->links[linkNum];
1081         struct ng_ppp_frag frag0, *frag = &frag0;
1082         struct ng_ppp_frag *qent;
1083         int i, diff, inserted;
1084         struct mbuf *m;
1085         meta_p meta;
1086
1087         NGI_GET_M(item, m);
1088         NGI_GET_META(item, meta);
1089         NG_FREE_ITEM(item);
1090         /* Stats */
1091         priv->bundleStats.recvFrames++;
1092         priv->bundleStats.recvOctets += m->m_pkthdr.len;
1093
1094         /* Extract fragment information from MP header */
1095         if (priv->conf.recvShortSeq) {
1096                 u_int16_t shdr;
1097
1098                 if (m->m_pkthdr.len < 2) {
1099                         link->stats.runts++;
1100                         NG_FREE_M(m);
1101                         NG_FREE_META(meta);
1102                         return (EINVAL);
1103                 }
1104                 if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1105                         NG_FREE_META(meta);
1106                         return (ENOBUFS);
1107                 }
1108                 shdr = ntohs(*mtod(m, u_int16_t *));
1109                 frag->seq = MP_SHORT_EXTEND(shdr);
1110                 frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0;
1111                 frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0;
1112                 diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq);
1113                 m_adj(m, 2);
1114         } else {
1115                 u_int32_t lhdr;
1116
1117                 if (m->m_pkthdr.len < 4) {
1118                         link->stats.runts++;
1119                         NG_FREE_M(m);
1120                         NG_FREE_META(meta);
1121                         return (EINVAL);
1122                 }
1123                 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
1124                         NG_FREE_META(meta);
1125                         return (ENOBUFS);
1126                 }
1127                 lhdr = ntohl(*mtod(m, u_int32_t *));
1128                 frag->seq = MP_LONG_EXTEND(lhdr);
1129                 frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0;
1130                 frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0;
1131                 diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq);
1132                 m_adj(m, 4);
1133         }
1134         frag->data = m;
1135         frag->meta = meta;
1136         getmicrouptime(&frag->timestamp);
1137
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 */
1140         if (diff < 0) {
1141                 link->stats.dropFragments++;
1142                 NG_FREE_M(m);
1143                 NG_FREE_META(meta);
1144                 return (0);
1145         }
1146
1147         /* Update highest received sequence number on this link and MSEQ */
1148         priv->mseq = link->seq = frag->seq;
1149         for (i = 0; i < priv->numActiveLinks; i++) {
1150                 struct ng_ppp_link *const alink =
1151                     &priv->links[priv->activeLinks[i]];
1152
1153                 if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0)
1154                         priv->mseq = alink->seq;
1155         }
1156
1157         /* Allocate a new frag struct for the queue */
1158         MALLOC(frag, struct ng_ppp_frag *, sizeof(*frag), M_NETGRAPH_PPP, M_NOWAIT);
1159         if (frag == NULL) {
1160                 NG_FREE_M(m);
1161                 NG_FREE_META(meta);
1162                 ng_ppp_frag_process(node);
1163                 return (ENOMEM);
1164         }
1165         *frag = frag0;
1166
1167         /* Add fragment to queue, which is sorted by sequence number */
1168         inserted = 0;
1169         TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) {
1170                 diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq);
1171                 if (diff > 0) {
1172                         TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent);
1173                         inserted = 1;
1174                         break;
1175                 } else if (diff == 0) {      /* should never happen! */
1176                         link->stats.dupFragments++;
1177                         NG_FREE_M(frag->data);
1178                         NG_FREE_META(frag->meta);
1179                         FREE(frag, M_NETGRAPH_PPP);
1180                         return (EINVAL);
1181                 }
1182         }
1183         if (!inserted)
1184                 TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent);
1185         priv->qlen++;
1186
1187         /* Process the queue */
1188         return ng_ppp_frag_process(node);
1189 }
1190
1191 /*
1192  * Examine our list of fragments, and determine if there is a
1193  * complete and deliverable packet at the head of the list.
1194  * Return 1 if so, zero otherwise.
1195  */
1196 static int
1197 ng_ppp_check_packet(node_p node)
1198 {
1199         const priv_p priv = NG_NODE_PRIVATE(node);
1200         struct ng_ppp_frag *qent, *qnext;
1201
1202         /* Check for empty queue */
1203         if (TAILQ_EMPTY(&priv->frags))
1204                 return (0);
1205
1206         /* Check first fragment is the start of a deliverable packet */
1207         qent = TAILQ_FIRST(&priv->frags);
1208         if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1)
1209                 return (0);
1210
1211         /* Check that all the fragments are there */
1212         while (!qent->last) {
1213                 qnext = TAILQ_NEXT(qent, f_qent);
1214                 if (qnext == NULL)      /* end of queue */
1215                         return (0);
1216                 if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq))
1217                         return (0);
1218                 qent = qnext;
1219         }
1220
1221         /* Got one */
1222         return (1);
1223 }
1224
1225 /*
1226  * Pull a completed packet off the head of the incoming fragment queue.
1227  * This assumes there is a completed packet there to pull off.
1228  */
1229 static void
1230 ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap)
1231 {
1232         const priv_p priv = NG_NODE_PRIVATE(node);
1233         struct ng_ppp_frag *qent, *qnext;
1234         struct mbuf *m = NULL, *tail;
1235
1236         qent = TAILQ_FIRST(&priv->frags);
1237         KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first,
1238             ("%s: no packet", __func__));
1239         for (tail = NULL; qent != NULL; qent = qnext) {
1240                 qnext = TAILQ_NEXT(qent, f_qent);
1241                 KASSERT(!TAILQ_EMPTY(&priv->frags),
1242                     ("%s: empty q", __func__));
1243                 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1244                 if (tail == NULL) {
1245                         tail = m = qent->data;
1246                         *metap = qent->meta;    /* inherit first frag's meta */
1247                 } else {
1248                         m->m_pkthdr.len += qent->data->m_pkthdr.len;
1249                         tail->m_next = qent->data;
1250                         NG_FREE_META(qent->meta); /* drop other frags' metas */
1251                 }
1252                 while (tail->m_next != NULL)
1253                         tail = tail->m_next;
1254                 if (qent->last)
1255                         qnext = NULL;
1256                 FREE(qent, M_NETGRAPH_PPP);
1257                 priv->qlen--;
1258         }
1259         *mp = m;
1260 }
1261
1262 /*
1263  * Trim fragments from the queue whose packets can never be completed.
1264  * This assumes a complete packet is NOT at the beginning of the queue.
1265  * Returns 1 if fragments were removed, zero otherwise.
1266  */
1267 static int
1268 ng_ppp_frag_trim(node_p node)
1269 {
1270         const priv_p priv = NG_NODE_PRIVATE(node);
1271         struct ng_ppp_frag *qent, *qnext = NULL;
1272         int removed = 0;
1273
1274         /* Scan for "dead" fragments and remove them */
1275         while (1) {
1276                 int dead = 0;
1277
1278                 /* If queue is empty, we're done */
1279                 if (TAILQ_EMPTY(&priv->frags))
1280                         break;
1281
1282                 /* Determine whether first fragment can ever be completed */
1283                 TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1284                         if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0)
1285                                 break;
1286                         qnext = TAILQ_NEXT(qent, f_qent);
1287                         KASSERT(qnext != NULL,
1288                             ("%s: last frag < MSEQ?", __func__));
1289                         if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)
1290                             || qent->last || qnext->first) {
1291                                 dead = 1;
1292                                 break;
1293                         }
1294                 }
1295                 if (!dead)
1296                         break;
1297
1298                 /* Remove fragment and all others in the same packet */
1299                 while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) {
1300                         KASSERT(!TAILQ_EMPTY(&priv->frags),
1301                             ("%s: empty q", __func__));
1302                         priv->bundleStats.dropFragments++;
1303                         TAILQ_REMOVE(&priv->frags, qent, f_qent);
1304                         NG_FREE_M(qent->data);
1305                         NG_FREE_META(qent->meta);
1306                         FREE(qent, M_NETGRAPH_PPP);
1307                         priv->qlen--;
1308                         removed = 1;
1309                 }
1310         }
1311         return (removed);
1312 }
1313
1314 /*
1315  * Run the queue, restoring the queue invariants
1316  */
1317 static int
1318 ng_ppp_frag_process(node_p node)
1319 {
1320         const priv_p priv = NG_NODE_PRIVATE(node);
1321         struct mbuf *m;
1322         meta_p meta;
1323         item_p item;
1324
1325         /* Deliver any deliverable packets */
1326         while (ng_ppp_check_packet(node)) {
1327                 ng_ppp_get_packet(node, &m, &meta);
1328                 item = ng_package_data(m, meta);
1329                 ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1330         }
1331
1332         /* Delete dead fragments and try again */
1333         if (ng_ppp_frag_trim(node)) {
1334                 while (ng_ppp_check_packet(node)) {
1335                         ng_ppp_get_packet(node, &m, &meta);
1336                         item = ng_package_data(m, meta);
1337                         ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1338                 }
1339         }
1340
1341         /* Check for stale fragments while we're here */
1342         ng_ppp_frag_checkstale(node);
1343
1344         /* Check queue length */
1345         if (priv->qlen > MP_MAX_QUEUE_LEN) {
1346                 struct ng_ppp_frag *qent;
1347                 int i;
1348
1349                 /* Get oldest fragment */
1350                 KASSERT(!TAILQ_EMPTY(&priv->frags),
1351                     ("%s: empty q", __func__));
1352                 qent = TAILQ_FIRST(&priv->frags);
1353
1354                 /* Bump MSEQ if necessary */
1355                 if (MP_RECV_SEQ_DIFF(priv, priv->mseq, qent->seq) < 0) {
1356                         priv->mseq = qent->seq;
1357                         for (i = 0; i < priv->numActiveLinks; i++) {
1358                                 struct ng_ppp_link *const alink =
1359                                     &priv->links[priv->activeLinks[i]];
1360
1361                                 if (MP_RECV_SEQ_DIFF(priv,
1362                                     alink->seq, priv->mseq) < 0)
1363                                         alink->seq = priv->mseq;
1364                         }
1365                 }
1366
1367                 /* Drop it */
1368                 priv->bundleStats.dropFragments++;
1369                 TAILQ_REMOVE(&priv->frags, qent, f_qent);
1370                 NG_FREE_M(qent->data);
1371                 NG_FREE_META(qent->meta);
1372                 FREE(qent, M_NETGRAPH_PPP);
1373                 priv->qlen--;
1374
1375                 /* Process queue again */
1376                 return ng_ppp_frag_process(node);
1377         }
1378
1379         /* Done */
1380         return (0);
1381 }
1382
1383 /*
1384  * Check for 'stale' completed packets that need to be delivered
1385  *
1386  * If a link goes down or has a temporary failure, MSEQ can get
1387  * "stuck", because no new incoming fragments appear on that link.
1388  * This can cause completed packets to never get delivered if
1389  * their sequence numbers are all > MSEQ + 1.
1390  *
1391  * This routine checks how long all of the completed packets have
1392  * been sitting in the queue, and if too long, removes fragments
1393  * from the queue and increments MSEQ to allow them to be delivered.
1394  */
1395 static void
1396 ng_ppp_frag_checkstale(node_p node)
1397 {
1398         const priv_p priv = NG_NODE_PRIVATE(node);
1399         struct ng_ppp_frag *qent, *beg, *end;
1400         struct timeval now, age;
1401         struct mbuf *m;
1402         meta_p meta;
1403         int i, seq;
1404         item_p item;
1405
1406         now.tv_sec = 0;                 /* uninitialized state */
1407         while (1) {
1408
1409                 /* If queue is empty, we're done */
1410                 if (TAILQ_EMPTY(&priv->frags))
1411                         break;
1412
1413                 /* Find the first complete packet in the queue */
1414                 beg = end = NULL;
1415                 seq = TAILQ_FIRST(&priv->frags)->seq;
1416                 TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1417                         if (qent->first)
1418                                 beg = qent;
1419                         else if (qent->seq != seq)
1420                                 beg = NULL;
1421                         if (beg != NULL && qent->last) {
1422                                 end = qent;
1423                                 break;
1424                         }
1425                         seq = MP_NEXT_RECV_SEQ(priv, seq);
1426                 }
1427
1428                 /* If none found, exit */
1429                 if (end == NULL)
1430                         break;
1431
1432                 /* Get current time (we assume we've been up for >= 1 second) */
1433                 if (now.tv_sec == 0)
1434                         getmicrouptime(&now);
1435
1436                 /* Check if packet has been queued too long */
1437                 age = now;
1438                 timevalsub(&age, &beg->timestamp);
1439                 if (timevalcmp(&age, &ng_ppp_max_staleness, < ))
1440                         break;
1441
1442                 /* Throw away junk fragments in front of the completed packet */
1443                 while ((qent = TAILQ_FIRST(&priv->frags)) != beg) {
1444                         KASSERT(!TAILQ_EMPTY(&priv->frags),
1445                             ("%s: empty q", __func__));
1446                         priv->bundleStats.dropFragments++;
1447                         TAILQ_REMOVE(&priv->frags, qent, f_qent);
1448                         NG_FREE_M(qent->data);
1449                         NG_FREE_META(qent->meta);
1450                         FREE(qent, M_NETGRAPH_PPP);
1451                         priv->qlen--;
1452                 }
1453
1454                 /* Extract completed packet */
1455                 ng_ppp_get_packet(node, &m, &meta);
1456
1457                 /* Bump MSEQ if necessary */
1458                 if (MP_RECV_SEQ_DIFF(priv, priv->mseq, end->seq) < 0) {
1459                         priv->mseq = end->seq;
1460                         for (i = 0; i < priv->numActiveLinks; i++) {
1461                                 struct ng_ppp_link *const alink =
1462                                     &priv->links[priv->activeLinks[i]];
1463
1464                                 if (MP_RECV_SEQ_DIFF(priv,
1465                                     alink->seq, priv->mseq) < 0)
1466                                         alink->seq = priv->mseq;
1467                         }
1468                 }
1469
1470                 /* Deliver packet */
1471                 item = ng_package_data(m, meta);
1472                 ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1473         }
1474 }
1475
1476 /*
1477  * Periodically call ng_ppp_frag_checkstale()
1478  */
1479 static void
1480 ng_ppp_frag_timeout(void *arg)
1481 {
1482         const node_p node = arg;
1483         const priv_p priv = NG_NODE_PRIVATE(node);
1484         int s = splnet();
1485
1486         /* Handle the race where shutdown happens just before splnet() above */
1487         if (NG_NODE_NOT_VALID(node)) {
1488                 NG_NODE_UNREF(node);
1489                 splx(s);
1490                 return;
1491         }
1492
1493         /* Reset timer state after timeout */
1494         KASSERT(priv->timerActive, ("%s: !timerActive", __func__));
1495         priv->timerActive = 0;
1496         KASSERT(node->nd_refs > 1, ("%s: nd_refs=%d", __func__, node->nd_refs));
1497         NG_NODE_UNREF(node);
1498
1499         /* Start timer again */
1500         ng_ppp_start_frag_timer(node);
1501
1502         /* Scan the fragment queue */
1503         ng_ppp_frag_checkstale(node);
1504         splx(s);
1505 }
1506
1507 /*
1508  * Deliver a frame out on the bundle, i.e., figure out how to fragment
1509  * the frame across the individual PPP links and do so.
1510  */
1511 static int
1512 ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta)
1513 {
1514         const priv_p priv = NG_NODE_PRIVATE(node);
1515         int distrib[NG_PPP_MAX_LINKS];
1516         int firstFragment;
1517         int activeLinkNum;
1518         item_p item;
1519
1520         /* At least one link must be active */
1521         if (priv->numActiveLinks == 0) {
1522                 NG_FREE_M(m);
1523                 NG_FREE_META(meta);
1524                 return (ENETDOWN);
1525         }
1526
1527         /* Round-robin strategy */
1528         if (priv->conf.enableRoundRobin || m->m_pkthdr.len < MP_MIN_FRAG_LEN) {
1529                 activeLinkNum = priv->lastLink++ % priv->numActiveLinks;
1530                 bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0]));
1531                 distrib[activeLinkNum] = m->m_pkthdr.len;
1532                 goto deliver;
1533         }
1534
1535         /* Strategy when all links are equivalent (optimize the common case) */
1536         if (priv->allLinksEqual) {
1537                 const int fraction = m->m_pkthdr.len / priv->numActiveLinks;
1538                 int i, remain;
1539
1540                 for (i = 0; i < priv->numActiveLinks; i++)
1541                         distrib[priv->lastLink++ % priv->numActiveLinks]
1542                             = fraction;
1543                 remain = m->m_pkthdr.len - (fraction * priv->numActiveLinks);
1544                 while (remain > 0) {
1545                         distrib[priv->lastLink++ % priv->numActiveLinks]++;
1546                         remain--;
1547                 }
1548                 goto deliver;
1549         }
1550
1551         /* Strategy when all links are not equivalent */
1552         ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib);
1553
1554 deliver:
1555         /* Update stats */
1556         priv->bundleStats.xmitFrames++;
1557         priv->bundleStats.xmitOctets += m->m_pkthdr.len;
1558
1559         /* Send alloted portions of frame out on the link(s) */
1560         for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1;
1561             activeLinkNum >= 0; activeLinkNum--) {
1562                 const int linkNum = priv->activeLinks[activeLinkNum];
1563                 struct ng_ppp_link *const link = &priv->links[linkNum];
1564
1565                 /* Deliver fragment(s) out the next link */
1566                 for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) {
1567                         int len, lastFragment, error;
1568                         struct mbuf *m2;
1569                         meta_p meta2;
1570
1571                         /* Calculate fragment length; don't exceed link MTU */
1572                         len = distrib[activeLinkNum];
1573                         if (len > link->conf.mru)
1574                                 len = link->conf.mru;
1575                         distrib[activeLinkNum] -= len;
1576                         lastFragment = (len == m->m_pkthdr.len);
1577
1578                         /* Split off next fragment as "m2" */
1579                         m2 = m;
1580                         if (!lastFragment) {
1581                                 struct mbuf *n = m_split(m, len, M_NOWAIT);
1582
1583                                 if (n == NULL) {
1584                                         NG_FREE_M(m);
1585                                         NG_FREE_META(meta);
1586                                         return (ENOMEM);
1587                                 }
1588                                 m = n;
1589                         }
1590
1591                         /* Prepend MP header */
1592                         if (priv->conf.xmitShortSeq) {
1593                                 u_int16_t shdr;
1594
1595                                 shdr = priv->xseq;
1596                                 priv->xseq =
1597                                     (priv->xseq + 1) & MP_SHORT_SEQ_MASK;
1598                                 if (firstFragment)
1599                                         shdr |= MP_SHORT_FIRST_FLAG;
1600                                 if (lastFragment)
1601                                         shdr |= MP_SHORT_LAST_FLAG;
1602                                 shdr = htons(shdr);
1603                                 m2 = ng_ppp_prepend(m2, &shdr, 2);
1604                         } else {
1605                                 u_int32_t lhdr;
1606
1607                                 lhdr = priv->xseq;
1608                                 priv->xseq =
1609                                     (priv->xseq + 1) & MP_LONG_SEQ_MASK;
1610                                 if (firstFragment)
1611                                         lhdr |= MP_LONG_FIRST_FLAG;
1612                                 if (lastFragment)
1613                                         lhdr |= MP_LONG_LAST_FLAG;
1614                                 lhdr = htonl(lhdr);
1615                                 m2 = ng_ppp_prepend(m2, &lhdr, 4);
1616                         }
1617                         if (m2 == NULL) {
1618                                 if (!lastFragment)
1619                                         m_freem(m);
1620                                 NG_FREE_META(meta);
1621                                 return (ENOBUFS);
1622                         }
1623
1624                         /* Copy the meta information, if any */
1625                         meta2 = lastFragment ? meta : ng_copy_meta(meta);
1626
1627                         /* Send fragment */
1628                         item = ng_package_data(m2, meta2);
1629                         error = ng_ppp_output(node, 0, PROT_MP, linkNum, item);
1630                         if (error != 0) {
1631                                 if (!lastFragment) {
1632                                         NG_FREE_M(m);
1633                                         NG_FREE_META(meta);
1634                                 }
1635                                 return (error);
1636                         }
1637                 }
1638         }
1639
1640         /* Done */
1641         return (0);
1642 }
1643
1644 /*
1645  * Computing the optimal fragmentation
1646  * -----------------------------------
1647  *
1648  * This routine tries to compute the optimal fragmentation pattern based
1649  * on each link's latency, bandwidth, and calculated additional latency.
1650  * The latter quantity is the additional latency caused by previously
1651  * written data that has not been transmitted yet.
1652  *
1653  * This algorithm is only useful when not all of the links have the
1654  * same latency and bandwidth values.
1655  *
1656  * The essential idea is to make the last bit of each fragment of the
1657  * frame arrive at the opposite end at the exact same time. This greedy
1658  * algorithm is optimal, in that no other scheduling could result in any
1659  * packet arriving any sooner unless packets are delivered out of order.
1660  *
1661  * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and
1662  * latency l_i (in miliseconds). Consider the function function f_i(t)
1663  * which is equal to the number of bytes that will have arrived at
1664  * the peer after t miliseconds if we start writing continuously at
1665  * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i).
1666  * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i).
1667  * Note that the y-intersect is always <= zero because latency can't be
1668  * negative.  Note also that really the function is f_i(t) except when
1669  * f_i(t) is negative, in which case the function is zero.  To take
1670  * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }.
1671  * So the actual number of bytes that will have arrived at the peer after
1672  * t miliseconds is f_i(t) * Q_i(t).
1673  *
1674  * At any given time, each link has some additional latency a_i >= 0
1675  * due to previously written fragment(s) which are still in the queue.
1676  * This value is easily computed from the time since last transmission,
1677  * the previous latency value, the number of bytes written, and the
1678  * link's bandwidth.
1679  *
1680  * Assume that l_i includes any a_i already, and that the links are
1681  * sorted by latency, so that l_i <= l_{i+1}.
1682  *
1683  * Let N be the total number of bytes in the current frame we are sending.
1684  *
1685  * Suppose we were to start writing bytes at time t = 0 on all links
1686  * simultaneously, which is the most we can possibly do.  Then let
1687  * F(t) be equal to the total number of bytes received by the peer
1688  * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)).
1689  *
1690  * Our goal is simply this: fragment the frame across the links such
1691  * that the peer is able to reconstruct the completed frame as soon as
1692  * possible, i.e., at the least possible value of t. Call this value t_0.
1693  *
1694  * Then it follows that F(t_0) = N. Our strategy is first to find the value
1695  * of t_0, and then deduce how many bytes to write to each link.
1696  *
1697  * Rewriting F(t_0):
1698  *
1699  *   t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) )
1700  *
1701  * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will
1702  * lie in one of these ranges.  To find it, we just need to find the i such
1703  * that F(l_i) <= N <= F(l_{i+1}).  Then we compute all the constant values
1704  * for Q_i() in this range, plug in the remaining values, solving for t_0.
1705  *
1706  * Once t_0 is known, then the number of bytes to send on link i is
1707  * just f_i(t_0) * Q_i(t_0).
1708  *
1709  * In other words, we start allocating bytes to the links one at a time.
1710  * We keep adding links until the frame is completely sent.  Some links
1711  * may not get any bytes because their latency is too high.
1712  *
1713  * Is all this work really worth the trouble?  Depends on the situation.
1714  * The bigger the ratio of computer speed to link speed, and the more
1715  * important total bundle latency is (e.g., for interactive response time),
1716  * the more it's worth it.  There is however the cost of calling this
1717  * function for every frame.  The running time is O(n^2) where n is the
1718  * number of links that receive a non-zero number of bytes.
1719  *
1720  * Since latency is measured in miliseconds, the "resolution" of this
1721  * algorithm is one milisecond.
1722  *
1723  * To avoid this algorithm altogether, configure all links to have the
1724  * same latency and bandwidth.
1725  */
1726 static void
1727 ng_ppp_mp_strategy(node_p node, int len, int *distrib)
1728 {
1729         const priv_p priv = NG_NODE_PRIVATE(node);
1730         int latency[NG_PPP_MAX_LINKS];
1731         int sortByLatency[NG_PPP_MAX_LINKS];
1732         int activeLinkNum;
1733         int t0, total, topSum, botSum;
1734         struct timeval now;
1735         int i, numFragments;
1736
1737         /* If only one link, this gets real easy */
1738         if (priv->numActiveLinks == 1) {
1739                 distrib[0] = len;
1740                 return;
1741         }
1742
1743         /* Get current time */
1744         getmicrouptime(&now);
1745
1746         /* Compute latencies for each link at this point in time */
1747         for (activeLinkNum = 0;
1748             activeLinkNum < priv->numActiveLinks; activeLinkNum++) {
1749                 struct ng_ppp_link *alink;
1750                 struct timeval diff;
1751                 int xmitBytes;
1752
1753                 /* Start with base latency value */
1754                 alink = &priv->links[priv->activeLinks[activeLinkNum]];
1755                 latency[activeLinkNum] = alink->conf.latency;
1756                 sortByLatency[activeLinkNum] = activeLinkNum;   /* see below */
1757
1758                 /* Any additional latency? */
1759                 if (alink->bytesInQueue == 0)
1760                         continue;
1761
1762                 /* Compute time delta since last write */
1763                 diff = now;
1764                 timevalsub(&diff, &alink->lastWrite);
1765                 if (now.tv_sec < 0 || diff.tv_sec >= 10) {      /* sanity */
1766                         alink->bytesInQueue = 0;
1767                         continue;
1768                 }
1769
1770                 /* How many bytes could have transmitted since last write? */
1771                 xmitBytes = (alink->conf.bandwidth * diff.tv_sec)
1772                     + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100;
1773                 alink->bytesInQueue -= xmitBytes;
1774                 if (alink->bytesInQueue < 0)
1775                         alink->bytesInQueue = 0;
1776                 else
1777                         latency[activeLinkNum] +=
1778                             (100 * alink->bytesInQueue) / alink->conf.bandwidth;
1779         }
1780
1781         /* Sort active links by latency */
1782         compareLatencies = latency;
1783         qsort(sortByLatency,
1784             priv->numActiveLinks, sizeof(*sortByLatency), ng_ppp_intcmp);
1785         compareLatencies = NULL;
1786
1787         /* Find the interval we need (add links in sortByLatency[] order) */
1788         for (numFragments = 1;
1789             numFragments < priv->numActiveLinks; numFragments++) {
1790                 for (total = i = 0; i < numFragments; i++) {
1791                         int flowTime;
1792
1793                         flowTime = latency[sortByLatency[numFragments]]
1794                             - latency[sortByLatency[i]];
1795                         total += ((flowTime * priv->links[
1796                             priv->activeLinks[sortByLatency[i]]].conf.bandwidth)
1797                                 + 99) / 100;
1798                 }
1799                 if (total >= len)
1800                         break;
1801         }
1802
1803         /* Solve for t_0 in that interval */
1804         for (topSum = botSum = i = 0; i < numFragments; i++) {
1805                 int bw = priv->links[
1806                     priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
1807
1808                 topSum += latency[sortByLatency[i]] * bw;       /* / 100 */
1809                 botSum += bw;                                   /* / 100 */
1810         }
1811         t0 = ((len * 100) + topSum + botSum / 2) / botSum;
1812
1813         /* Compute f_i(t_0) all i */
1814         bzero(distrib, priv->numActiveLinks * sizeof(*distrib));
1815         for (total = i = 0; i < numFragments; i++) {
1816                 int bw = priv->links[
1817                     priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
1818
1819                 distrib[sortByLatency[i]] =
1820                     (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100;
1821                 total += distrib[sortByLatency[i]];
1822         }
1823
1824         /* Deal with any rounding error */
1825         if (total < len) {
1826                 struct ng_ppp_link *fastLink =
1827                     &priv->links[priv->activeLinks[sortByLatency[0]]];
1828                 int fast = 0;
1829
1830                 /* Find the fastest link */
1831                 for (i = 1; i < numFragments; i++) {
1832                         struct ng_ppp_link *const link =
1833                             &priv->links[priv->activeLinks[sortByLatency[i]]];
1834
1835                         if (link->conf.bandwidth > fastLink->conf.bandwidth) {
1836                                 fast = i;
1837                                 fastLink = link;
1838                         }
1839                 }
1840                 distrib[sortByLatency[fast]] += len - total;
1841         } else while (total > len) {
1842                 struct ng_ppp_link *slowLink =
1843                     &priv->links[priv->activeLinks[sortByLatency[0]]];
1844                 int delta, slow = 0;
1845
1846                 /* Find the slowest link that still has bytes to remove */
1847                 for (i = 1; i < numFragments; i++) {
1848                         struct ng_ppp_link *const link =
1849                             &priv->links[priv->activeLinks[sortByLatency[i]]];
1850
1851                         if (distrib[sortByLatency[slow]] == 0
1852                           || (distrib[sortByLatency[i]] > 0
1853                             && link->conf.bandwidth <
1854                               slowLink->conf.bandwidth)) {
1855                                 slow = i;
1856                                 slowLink = link;
1857                         }
1858                 }
1859                 delta = total - len;
1860                 if (delta > distrib[sortByLatency[slow]])
1861                         delta = distrib[sortByLatency[slow]];
1862                 distrib[sortByLatency[slow]] -= delta;
1863                 total -= delta;
1864         }
1865 }
1866
1867 /*
1868  * Compare two integers
1869  */
1870 static int
1871 ng_ppp_intcmp(const void *v1, const void *v2)
1872 {
1873         const int index1 = *((const int *) v1);
1874         const int index2 = *((const int *) v2);
1875
1876         return compareLatencies[index1] - compareLatencies[index2];
1877 }
1878
1879 /*
1880  * Prepend a possibly compressed PPP protocol number in front of a frame
1881  */
1882 static struct mbuf *
1883 ng_ppp_addproto(struct mbuf *m, int proto, int compOK)
1884 {
1885         if (compOK && PROT_COMPRESSABLE(proto)) {
1886                 u_char pbyte = (u_char)proto;
1887
1888                 return ng_ppp_prepend(m, &pbyte, 1);
1889         } else {
1890                 u_int16_t pword = htons((u_int16_t)proto);
1891
1892                 return ng_ppp_prepend(m, &pword, 2);
1893         }
1894 }
1895
1896 /*
1897  * Prepend some bytes to an mbuf
1898  */
1899 static struct mbuf *
1900 ng_ppp_prepend(struct mbuf *m, const void *buf, int len)
1901 {
1902         M_PREPEND(m, len, M_NOWAIT);
1903         if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL))
1904                 return (NULL);
1905         bcopy(buf, mtod(m, u_char *), len);
1906         return (m);
1907 }
1908
1909 /*
1910  * Update private information that is derived from other private information
1911  */
1912 static void
1913 ng_ppp_update(node_p node, int newConf)
1914 {
1915         const priv_p priv = NG_NODE_PRIVATE(node);
1916         int i;
1917
1918         /* Update active status for VJ Compression */
1919         priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL
1920             && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL
1921             && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL
1922             && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL;
1923
1924         /* Increase latency for each link an amount equal to one MP header */
1925         if (newConf) {
1926                 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1927                         int hdrBytes;
1928
1929                         hdrBytes = (priv->links[i].conf.enableACFComp ? 0 : 2)
1930                             + (priv->links[i].conf.enableProtoComp ? 1 : 2)
1931                             + (priv->conf.xmitShortSeq ? 2 : 4);
1932                         priv->links[i].conf.latency +=
1933                             ((hdrBytes * priv->links[i].conf.bandwidth) + 50)
1934                                 / 100;
1935                 }
1936         }
1937
1938         /* Update list of active links */
1939         bzero(&priv->activeLinks, sizeof(priv->activeLinks));
1940         priv->numActiveLinks = 0;
1941         priv->allLinksEqual = 1;
1942         for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1943                 struct ng_ppp_link *const link = &priv->links[i];
1944
1945                 /* Is link active? */
1946                 if (link->conf.enableLink && link->hook != NULL) {
1947                         struct ng_ppp_link *link0;
1948
1949                         /* Add link to list of active links */
1950                         priv->activeLinks[priv->numActiveLinks++] = i;
1951                         link0 = &priv->links[priv->activeLinks[0]];
1952
1953                         /* Determine if all links are still equal */
1954                         if (link->conf.latency != link0->conf.latency
1955                           || link->conf.bandwidth != link0->conf.bandwidth)
1956                                 priv->allLinksEqual = 0;
1957
1958                         /* Initialize rec'd sequence number */
1959                         if (link->seq == MP_NOSEQ) {
1960                                 link->seq = (link == link0) ?
1961                                     MP_INITIAL_SEQ : link0->seq;
1962                         }
1963                 } else
1964                         link->seq = MP_NOSEQ;
1965         }
1966
1967         /* Update MP state as multi-link is active or not */
1968         if (priv->conf.enableMultilink && priv->numActiveLinks > 0)
1969                 ng_ppp_start_frag_timer(node);
1970         else {
1971                 ng_ppp_stop_frag_timer(node);
1972                 ng_ppp_frag_reset(node);
1973                 priv->xseq = MP_INITIAL_SEQ;
1974                 priv->mseq = MP_INITIAL_SEQ;
1975                 for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1976                         struct ng_ppp_link *const link = &priv->links[i];
1977
1978                         bzero(&link->lastWrite, sizeof(link->lastWrite));
1979                         link->bytesInQueue = 0;
1980                         link->seq = MP_NOSEQ;
1981                 }
1982         }
1983 }
1984
1985 /*
1986  * Determine if a new configuration would represent a valid change
1987  * from the current configuration and link activity status.
1988  */
1989 static int
1990 ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf)
1991 {
1992         const priv_p priv = NG_NODE_PRIVATE(node);
1993         int i, newNumLinksActive;
1994
1995         /* Check per-link config and count how many links would be active */
1996         for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) {
1997                 if (newConf->links[i].enableLink && priv->links[i].hook != NULL)
1998                         newNumLinksActive++;
1999                 if (!newConf->links[i].enableLink)
2000                         continue;
2001                 if (newConf->links[i].mru < MP_MIN_LINK_MRU)
2002                         return (0);
2003                 if (newConf->links[i].bandwidth == 0)
2004                         return (0);
2005                 if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH)
2006                         return (0);
2007                 if (newConf->links[i].latency > NG_PPP_MAX_LATENCY)
2008                         return (0);
2009         }
2010
2011         /* Check bundle parameters */
2012         if (newConf->bund.enableMultilink && newConf->bund.mrru < MP_MIN_MRRU)
2013                 return (0);
2014
2015         /* Disallow changes to multi-link configuration while MP is active */
2016         if (priv->numActiveLinks > 0 && newNumLinksActive > 0) {
2017                 if (!priv->conf.enableMultilink
2018                                 != !newConf->bund.enableMultilink
2019                     || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq
2020                     || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq)
2021                         return (0);
2022         }
2023
2024         /* At most one link can be active unless multi-link is enabled */
2025         if (!newConf->bund.enableMultilink && newNumLinksActive > 1)
2026                 return (0);
2027
2028         /* Configuration change would be valid */
2029         return (1);
2030 }
2031
2032 /*
2033  * Free all entries in the fragment queue
2034  */
2035 static void
2036 ng_ppp_frag_reset(node_p node)
2037 {
2038         const priv_p priv = NG_NODE_PRIVATE(node);
2039         struct ng_ppp_frag *qent, *qnext;
2040
2041         for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) {
2042                 qnext = TAILQ_NEXT(qent, f_qent);
2043                 NG_FREE_M(qent->data);
2044                 NG_FREE_META(qent->meta);
2045                 FREE(qent, M_NETGRAPH_PPP);
2046         }
2047         TAILQ_INIT(&priv->frags);
2048         priv->qlen = 0;
2049 }
2050
2051 /*
2052  * Start fragment queue timer
2053  */
2054 static void
2055 ng_ppp_start_frag_timer(node_p node)
2056 {
2057         const priv_p priv = NG_NODE_PRIVATE(node);
2058
2059         if (!priv->timerActive) {
2060                 priv->fragTimer = timeout(ng_ppp_frag_timeout,
2061                     node, MP_FRAGTIMER_INTERVAL);
2062                 priv->timerActive = 1;
2063                 NG_NODE_REF(node);
2064         }
2065 }
2066
2067 /*
2068  * Stop fragment queue timer
2069  */
2070 static void
2071 ng_ppp_stop_frag_timer(node_p node)
2072 {
2073         const priv_p priv = NG_NODE_PRIVATE(node);
2074
2075         if (priv->timerActive) {
2076                 untimeout(ng_ppp_frag_timeout, node, priv->fragTimer);
2077                 priv->timerActive = 0;
2078                 KASSERT(node->nd_refs > 1,
2079                     ("%s: nd_refs=%d", __func__, node->nd_refs));
2080                 NG_NODE_UNREF(node);
2081         }
2082 }
2083