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