xref: /freebsd/sys/netgraph/ng_ppp.c (revision 104a9b7e3edbd88cdda0698c5c77a2ad6dafcc16)
14cf49a43SJulian Elischer 
24cf49a43SJulian Elischer /*
34cf49a43SJulian Elischer  * ng_ppp.c
44cf49a43SJulian Elischer  *
5a9b3dca5SArchie Cobbs  * Copyright (c) 1996-2000 Whistle Communications, Inc.
64cf49a43SJulian Elischer  * All rights reserved.
74cf49a43SJulian Elischer  *
84cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
94cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
104cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
114cf49a43SJulian Elischer  * provided, however, that:
124cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
134cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
144cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
154cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
164cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
174cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
184cf49a43SJulian Elischer  *
194cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
204cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
214cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
224cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
234cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
244cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
254cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
264cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
274cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
284cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
294cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
304cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
314cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
324cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
334cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
344cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
354cf49a43SJulian Elischer  * OF SUCH DAMAGE.
364cf49a43SJulian Elischer  *
376f16db81SArchie Cobbs  * Author: Archie Cobbs <archie@freebsd.org>
384cf49a43SJulian Elischer  *
394cf49a43SJulian Elischer  * $FreeBSD$
4074f5c6aaSJulian Elischer  * $Whistle: ng_ppp.c,v 1.24 1999/11/01 09:24:52 julian Exp $
414cf49a43SJulian Elischer  */
424cf49a43SJulian Elischer 
434cf49a43SJulian Elischer /*
443949bee8SArchie Cobbs  * PPP node type.
454cf49a43SJulian Elischer  */
464cf49a43SJulian Elischer 
474cf49a43SJulian Elischer #include <sys/param.h>
484cf49a43SJulian Elischer #include <sys/systm.h>
494cf49a43SJulian Elischer #include <sys/kernel.h>
50104a9b7eSAlexander Kabaev #include <sys/limits.h>
51a9b3dca5SArchie Cobbs #include <sys/time.h>
524cf49a43SJulian Elischer #include <sys/mbuf.h>
534cf49a43SJulian Elischer #include <sys/malloc.h>
544cf49a43SJulian Elischer #include <sys/errno.h>
555b664c7cSPoul-Henning Kamp #include <sys/ctype.h>
564cf49a43SJulian Elischer 
574cf49a43SJulian Elischer #include <netgraph/ng_message.h>
584cf49a43SJulian Elischer #include <netgraph/netgraph.h>
59f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
604cf49a43SJulian Elischer #include <netgraph/ng_ppp.h>
613949bee8SArchie Cobbs #include <netgraph/ng_vjc.h>
624cf49a43SJulian Elischer 
639c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC
649c8c302fSJulian Elischer MALLOC_DEFINE(M_NETGRAPH_PPP, "netgraph_ppp", "netgraph ppp node");
659c8c302fSJulian Elischer #else
669c8c302fSJulian Elischer #define M_NETGRAPH_PPP M_NETGRAPH
679c8c302fSJulian Elischer #endif
689c8c302fSJulian Elischer 
694cf49a43SJulian Elischer #define PROT_VALID(p)		(((p) & 0x0101) == 0x0001)
702b70adcbSArchie Cobbs #define PROT_COMPRESSABLE(p)	(((p) & 0xff00) == 0x0000)
714cf49a43SJulian Elischer 
723949bee8SArchie Cobbs /* Some PPP protocol numbers we're interested in */
733949bee8SArchie Cobbs #define PROT_APPLETALK		0x0029
743949bee8SArchie Cobbs #define PROT_COMPD		0x00fd
753949bee8SArchie Cobbs #define PROT_CRYPTD		0x0053
763949bee8SArchie Cobbs #define PROT_IP			0x0021
77a9b3dca5SArchie Cobbs #define PROT_IPV6		0x0057
782b70adcbSArchie Cobbs #define PROT_IPX		0x002b
79d690a6e7SArchie Cobbs #define PROT_LCP		0xc021
803949bee8SArchie Cobbs #define PROT_MP			0x003d
813949bee8SArchie Cobbs #define PROT_VJCOMP		0x002d
823949bee8SArchie Cobbs #define PROT_VJUNCOMP		0x002f
833949bee8SArchie Cobbs 
843949bee8SArchie Cobbs /* Multilink PPP definitions */
853949bee8SArchie Cobbs #define MP_MIN_MRRU		1500		/* per RFC 1990 */
863949bee8SArchie Cobbs #define MP_INITIAL_SEQ		0		/* per RFC 1990 */
873949bee8SArchie Cobbs #define MP_MIN_LINK_MRU		32
883949bee8SArchie Cobbs 
893949bee8SArchie Cobbs #define MP_SHORT_SEQ_MASK	0x00000fff	/* short seq # mask */
903949bee8SArchie Cobbs #define MP_SHORT_SEQ_HIBIT	0x00000800	/* short seq # high bit */
913949bee8SArchie Cobbs #define MP_SHORT_FIRST_FLAG	0x00008000	/* first fragment in frame */
923949bee8SArchie Cobbs #define MP_SHORT_LAST_FLAG	0x00004000	/* last fragment in frame */
933949bee8SArchie Cobbs 
943949bee8SArchie Cobbs #define MP_LONG_SEQ_MASK	0x00ffffff	/* long seq # mask */
953949bee8SArchie Cobbs #define MP_LONG_SEQ_HIBIT	0x00800000	/* long seq # high bit */
963949bee8SArchie Cobbs #define MP_LONG_FIRST_FLAG	0x80000000	/* first fragment in frame */
973949bee8SArchie Cobbs #define MP_LONG_LAST_FLAG	0x40000000	/* last fragment in frame */
983949bee8SArchie Cobbs 
996f16db81SArchie Cobbs #define MP_NOSEQ		0x7fffffff	/* impossible sequence number */
1003949bee8SArchie Cobbs 
1013949bee8SArchie Cobbs /* Sign extension of MP sequence numbers */
1023949bee8SArchie Cobbs #define MP_SHORT_EXTEND(s)	(((s) & MP_SHORT_SEQ_HIBIT) ?		\
1036f16db81SArchie Cobbs 				    ((s) | ~MP_SHORT_SEQ_MASK)		\
1046f16db81SArchie Cobbs 				    : ((s) & MP_SHORT_SEQ_MASK))
1053949bee8SArchie Cobbs #define MP_LONG_EXTEND(s)	(((s) & MP_LONG_SEQ_HIBIT) ?		\
1066f16db81SArchie Cobbs 				    ((s) | ~MP_LONG_SEQ_MASK)		\
1076f16db81SArchie Cobbs 				    : ((s) & MP_LONG_SEQ_MASK))
1083949bee8SArchie Cobbs 
1096f16db81SArchie Cobbs /* Comparision of MP sequence numbers. Note: all sequence numbers
1106f16db81SArchie Cobbs    except priv->xseq are stored with the sign bit extended. */
1116f16db81SArchie Cobbs #define MP_SHORT_SEQ_DIFF(x,y)	MP_SHORT_EXTEND((x) - (y))
1126f16db81SArchie Cobbs #define MP_LONG_SEQ_DIFF(x,y)	MP_LONG_EXTEND((x) - (y))
1133949bee8SArchie Cobbs 
1146f16db81SArchie Cobbs #define MP_RECV_SEQ_DIFF(priv,x,y)					\
1156f16db81SArchie Cobbs 				((priv)->conf.recvShortSeq ?		\
1163949bee8SArchie Cobbs 				    MP_SHORT_SEQ_DIFF((x), (y)) :	\
1173949bee8SArchie Cobbs 				    MP_LONG_SEQ_DIFF((x), (y)))
1183949bee8SArchie Cobbs 
1196f16db81SArchie Cobbs /* Increment receive sequence number */
1206f16db81SArchie Cobbs #define MP_NEXT_RECV_SEQ(priv,seq)					\
121b5a60ddbSArchie Cobbs 				((priv)->conf.recvShortSeq ?		\
122b5a60ddbSArchie Cobbs 				    MP_SHORT_EXTEND((seq) + 1) :	\
123b5a60ddbSArchie Cobbs 				    MP_LONG_EXTEND((seq) + 1))
124a9b3dca5SArchie Cobbs 
125a9b3dca5SArchie Cobbs /* Don't fragment transmitted packets smaller than this */
126a9b3dca5SArchie Cobbs #define MP_MIN_FRAG_LEN		6
127a9b3dca5SArchie Cobbs 
128a9b3dca5SArchie Cobbs /* Maximum fragment reasssembly queue length */
129a9b3dca5SArchie Cobbs #define MP_MAX_QUEUE_LEN	128
130a9b3dca5SArchie Cobbs 
131a9b3dca5SArchie Cobbs /* Fragment queue scanner period */
132a9b3dca5SArchie Cobbs #define MP_FRAGTIMER_INTERVAL	(hz/2)
133a9b3dca5SArchie Cobbs 
1343949bee8SArchie Cobbs /* We store incoming fragments this way */
1353949bee8SArchie Cobbs struct ng_ppp_frag {
136a9b3dca5SArchie Cobbs 	int				seq;		/* fragment seq# */
137a9b3dca5SArchie Cobbs 	u_char				first;		/* First in packet? */
138a9b3dca5SArchie Cobbs 	u_char				last;		/* Last in packet? */
139a9b3dca5SArchie Cobbs 	struct timeval			timestamp;	/* time of reception */
140a9b3dca5SArchie Cobbs 	struct mbuf			*data;		/* Fragment data */
141a9b3dca5SArchie Cobbs 	meta_p				meta;		/* Fragment meta */
1425f90cac7SKirk McKusick 	TAILQ_ENTRY(ng_ppp_frag)	f_qent;		/* Fragment queue */
1433949bee8SArchie Cobbs };
1443949bee8SArchie Cobbs 
1453949bee8SArchie Cobbs /* We use integer indicies to refer to the non-link hooks */
1463949bee8SArchie Cobbs static const char *const ng_ppp_hook_names[] = {
1473949bee8SArchie Cobbs 	NG_PPP_HOOK_ATALK,
1483949bee8SArchie Cobbs #define HOOK_INDEX_ATALK		0
1493949bee8SArchie Cobbs 	NG_PPP_HOOK_BYPASS,
1503949bee8SArchie Cobbs #define HOOK_INDEX_BYPASS		1
1513949bee8SArchie Cobbs 	NG_PPP_HOOK_COMPRESS,
1523949bee8SArchie Cobbs #define HOOK_INDEX_COMPRESS		2
1533949bee8SArchie Cobbs 	NG_PPP_HOOK_ENCRYPT,
1543949bee8SArchie Cobbs #define HOOK_INDEX_ENCRYPT		3
1553949bee8SArchie Cobbs 	NG_PPP_HOOK_DECOMPRESS,
1563949bee8SArchie Cobbs #define HOOK_INDEX_DECOMPRESS		4
1573949bee8SArchie Cobbs 	NG_PPP_HOOK_DECRYPT,
1583949bee8SArchie Cobbs #define HOOK_INDEX_DECRYPT		5
1593949bee8SArchie Cobbs 	NG_PPP_HOOK_INET,
1603949bee8SArchie Cobbs #define HOOK_INDEX_INET			6
1613949bee8SArchie Cobbs 	NG_PPP_HOOK_IPX,
1623949bee8SArchie Cobbs #define HOOK_INDEX_IPX			7
1633949bee8SArchie Cobbs 	NG_PPP_HOOK_VJC_COMP,
1643949bee8SArchie Cobbs #define HOOK_INDEX_VJC_COMP		8
1653949bee8SArchie Cobbs 	NG_PPP_HOOK_VJC_IP,
1663949bee8SArchie Cobbs #define HOOK_INDEX_VJC_IP		9
1673949bee8SArchie Cobbs 	NG_PPP_HOOK_VJC_UNCOMP,
1683949bee8SArchie Cobbs #define HOOK_INDEX_VJC_UNCOMP		10
1693949bee8SArchie Cobbs 	NG_PPP_HOOK_VJC_VJIP,
1703949bee8SArchie Cobbs #define HOOK_INDEX_VJC_VJIP		11
171a9b3dca5SArchie Cobbs 	NG_PPP_HOOK_IPV6,
172a9b3dca5SArchie Cobbs #define HOOK_INDEX_IPV6			12
1733949bee8SArchie Cobbs 	NULL
174a9b3dca5SArchie Cobbs #define HOOK_INDEX_MAX			13
1753949bee8SArchie Cobbs };
1763949bee8SArchie Cobbs 
1773949bee8SArchie Cobbs /* We store index numbers in the hook private pointer. The HOOK_INDEX()
1783949bee8SArchie Cobbs    for a hook is either the index (above) for normal hooks, or the ones
17930400f03SJulian Elischer    complement of the link number for link hooks.
18030400f03SJulian Elischer XXX Not any more.. (what a hack)
1813949bee8SArchie Cobbs #define HOOK_INDEX(hook)	(*((int16_t *) &(hook)->private))
18230400f03SJulian Elischer */
1834cf49a43SJulian Elischer 
184a9b3dca5SArchie Cobbs /* Per-link private information */
185a9b3dca5SArchie Cobbs struct ng_ppp_link {
186a9b3dca5SArchie Cobbs 	struct ng_ppp_link_conf	conf;		/* link configuration */
187a9b3dca5SArchie Cobbs 	hook_p			hook;		/* connection to link data */
1886f16db81SArchie Cobbs 	int32_t			seq;		/* highest rec'd seq# - MSEQ */
189a9b3dca5SArchie Cobbs 	struct timeval		lastWrite;	/* time of last write */
190a9b3dca5SArchie Cobbs 	int			bytesInQueue;	/* bytes in the output queue */
191a9b3dca5SArchie Cobbs 	struct ng_ppp_link_stat	stats;		/* Link stats */
192a9b3dca5SArchie Cobbs };
193a9b3dca5SArchie Cobbs 
194a9b3dca5SArchie Cobbs /* Total per-node private information */
19562838faeSArchie Cobbs struct ng_ppp_private {
196a9b3dca5SArchie Cobbs 	struct ng_ppp_bund_conf	conf;			/* bundle config */
197a9b3dca5SArchie Cobbs 	struct ng_ppp_link_stat	bundleStats;		/* bundle stats */
198a9b3dca5SArchie Cobbs 	struct ng_ppp_link	links[NG_PPP_MAX_LINKS];/* per-link info */
1996f16db81SArchie Cobbs 	int32_t			xseq;			/* next out MP seq # */
2006f16db81SArchie Cobbs 	int32_t			mseq;			/* min links[i].seq */
201a9b3dca5SArchie Cobbs 	u_char			vjCompHooked;		/* VJ comp hooked up? */
202a9b3dca5SArchie Cobbs 	u_char			allLinksEqual;		/* all xmit the same? */
203a9b3dca5SArchie Cobbs 	u_char			timerActive;		/* frag timer active? */
204a9b3dca5SArchie Cobbs 	u_int			numActiveLinks;		/* how many links up */
205a9b3dca5SArchie Cobbs 	int			activeLinks[NG_PPP_MAX_LINKS];	/* indicies */
2063949bee8SArchie Cobbs 	u_int			lastLink;		/* for round robin */
207a9b3dca5SArchie Cobbs 	hook_p			hooks[HOOK_INDEX_MAX];	/* non-link hooks */
2085f90cac7SKirk McKusick 	TAILQ_HEAD(ng_ppp_fraglist, ng_ppp_frag)	/* fragment queue */
209a9b3dca5SArchie Cobbs 				frags;
210a9b3dca5SArchie Cobbs 	int			qlen;			/* fraq queue length */
211a9b3dca5SArchie Cobbs 	struct callout_handle	fragTimer;		/* fraq queue check */
2124cf49a43SJulian Elischer };
21362838faeSArchie Cobbs typedef struct ng_ppp_private *priv_p;
2144cf49a43SJulian Elischer 
2154cf49a43SJulian Elischer /* Netgraph node methods */
21674f5c6aaSJulian Elischer static ng_constructor_t	ng_ppp_constructor;
21774f5c6aaSJulian Elischer static ng_rcvmsg_t	ng_ppp_rcvmsg;
218069154d5SJulian Elischer static ng_shutdown_t	ng_ppp_shutdown;
21974f5c6aaSJulian Elischer static ng_newhook_t	ng_ppp_newhook;
22074f5c6aaSJulian Elischer static ng_rcvdata_t	ng_ppp_rcvdata;
22174f5c6aaSJulian Elischer static ng_disconnect_t	ng_ppp_disconnect;
2224cf49a43SJulian Elischer 
2233949bee8SArchie Cobbs /* Helper functions */
224e149c4e2SArchie Cobbs static int	ng_ppp_input(node_p node, int bypass,
225069154d5SJulian Elischer 			int linkNum, item_p item);
226d690a6e7SArchie Cobbs static int	ng_ppp_output(node_p node, int bypass, int proto,
227069154d5SJulian Elischer 			int linkNum, item_p item);
228069154d5SJulian Elischer static int	ng_ppp_mp_input(node_p node, int linkNum, item_p item);
229a9b3dca5SArchie Cobbs static int	ng_ppp_check_packet(node_p node);
230a9b3dca5SArchie Cobbs static void	ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap);
231a9b3dca5SArchie Cobbs static int	ng_ppp_frag_process(node_p node);
232a9b3dca5SArchie Cobbs static int	ng_ppp_frag_trim(node_p node);
233a9b3dca5SArchie Cobbs static void	ng_ppp_frag_timeout(void *arg);
234a9b3dca5SArchie Cobbs static void	ng_ppp_frag_checkstale(node_p node);
235a9b3dca5SArchie Cobbs static void	ng_ppp_frag_reset(node_p node);
2363949bee8SArchie Cobbs static int	ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta);
2373949bee8SArchie Cobbs static void	ng_ppp_mp_strategy(node_p node, int len, int *distrib);
2383949bee8SArchie Cobbs static int	ng_ppp_intcmp(const void *v1, const void *v2);
2393949bee8SArchie Cobbs static struct	mbuf *ng_ppp_addproto(struct mbuf *m, int proto, int compOK);
240d690a6e7SArchie Cobbs static struct	mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len);
2413949bee8SArchie Cobbs static int	ng_ppp_config_valid(node_p node,
242a9b3dca5SArchie Cobbs 			const struct ng_ppp_node_conf *newConf);
2433949bee8SArchie Cobbs static void	ng_ppp_update(node_p node, int newConf);
244a9b3dca5SArchie Cobbs static void	ng_ppp_start_frag_timer(node_p node);
245a9b3dca5SArchie Cobbs static void	ng_ppp_stop_frag_timer(node_p node);
2464cf49a43SJulian Elischer 
2476f16db81SArchie Cobbs /* Parse type for struct ng_ppp_mp_state_type */
2486f16db81SArchie Cobbs static const struct ng_parse_fixedarray_info ng_ppp_rseq_array_info = {
2496f16db81SArchie Cobbs 	&ng_parse_hint32_type,
2506f16db81SArchie Cobbs 	NG_PPP_MAX_LINKS
2516f16db81SArchie Cobbs };
2526f16db81SArchie Cobbs static const struct ng_parse_type ng_ppp_rseq_array_type = {
2536f16db81SArchie Cobbs 	&ng_parse_fixedarray_type,
2546f16db81SArchie Cobbs 	&ng_ppp_rseq_array_info,
2556f16db81SArchie Cobbs };
256f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_mp_state_type_fields[]
2576f16db81SArchie Cobbs 	= NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type);
2586f16db81SArchie Cobbs static const struct ng_parse_type ng_ppp_mp_state_type = {
2596f16db81SArchie Cobbs 	&ng_parse_struct_type,
260f0184ff8SArchie Cobbs 	&ng_ppp_mp_state_type_fields
2616f16db81SArchie Cobbs };
2626f16db81SArchie Cobbs 
263a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_link_conf */
264f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_link_type_fields[]
265f0184ff8SArchie Cobbs 	= NG_PPP_LINK_TYPE_INFO;
266f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_link_type = {
267f8307e12SArchie Cobbs 	&ng_parse_struct_type,
268f0184ff8SArchie Cobbs 	&ng_ppp_link_type_fields
269f8307e12SArchie Cobbs };
270f8307e12SArchie Cobbs 
271a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_bund_conf */
272f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_bund_type_fields[]
273f0184ff8SArchie Cobbs 	= NG_PPP_BUND_TYPE_INFO;
274a9b3dca5SArchie Cobbs static const struct ng_parse_type ng_ppp_bund_type = {
275a9b3dca5SArchie Cobbs 	&ng_parse_struct_type,
276f0184ff8SArchie Cobbs 	&ng_ppp_bund_type_fields
277a9b3dca5SArchie Cobbs };
278a9b3dca5SArchie Cobbs 
279a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_node_conf */
2806f16db81SArchie Cobbs static const struct ng_parse_fixedarray_info ng_ppp_array_info = {
281f8307e12SArchie Cobbs 	&ng_ppp_link_type,
282f8307e12SArchie Cobbs 	NG_PPP_MAX_LINKS
283f8307e12SArchie Cobbs };
284f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_link_array_type = {
285f8307e12SArchie Cobbs 	&ng_parse_fixedarray_type,
286f8307e12SArchie Cobbs 	&ng_ppp_array_info,
287f8307e12SArchie Cobbs };
288f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_conf_type_fields[]
289a9b3dca5SArchie Cobbs 	= NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type);
290a9b3dca5SArchie Cobbs static const struct ng_parse_type ng_ppp_conf_type = {
291f8307e12SArchie Cobbs 	&ng_parse_struct_type,
292f0184ff8SArchie Cobbs 	&ng_ppp_conf_type_fields
293f8307e12SArchie Cobbs };
294f8307e12SArchie Cobbs 
295f8307e12SArchie Cobbs /* Parse type for struct ng_ppp_link_stat */
296f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_stats_type_fields[]
297f0184ff8SArchie Cobbs 	= NG_PPP_STATS_TYPE_INFO;
298f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_stats_type = {
299f8307e12SArchie Cobbs 	&ng_parse_struct_type,
300f0184ff8SArchie Cobbs 	&ng_ppp_stats_type_fields
301f8307e12SArchie Cobbs };
302f8307e12SArchie Cobbs 
303f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
304f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ppp_cmds[] = {
305f8307e12SArchie Cobbs 	{
306f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
307f8307e12SArchie Cobbs 	  NGM_PPP_SET_CONFIG,
308f8307e12SArchie Cobbs 	  "setconfig",
309a9b3dca5SArchie Cobbs 	  &ng_ppp_conf_type,
310f8307e12SArchie Cobbs 	  NULL
311f8307e12SArchie Cobbs 	},
312f8307e12SArchie Cobbs 	{
313f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
314f8307e12SArchie Cobbs 	  NGM_PPP_GET_CONFIG,
315f8307e12SArchie Cobbs 	  "getconfig",
316f8307e12SArchie Cobbs 	  NULL,
317a9b3dca5SArchie Cobbs 	  &ng_ppp_conf_type
318f8307e12SArchie Cobbs 	},
319f8307e12SArchie Cobbs 	{
320f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
3216f16db81SArchie Cobbs 	  NGM_PPP_GET_MP_STATE,
3226f16db81SArchie Cobbs 	  "getmpstate",
3236f16db81SArchie Cobbs 	  NULL,
3246f16db81SArchie Cobbs 	  &ng_ppp_mp_state_type
3256f16db81SArchie Cobbs 	},
3266f16db81SArchie Cobbs 	{
3276f16db81SArchie Cobbs 	  NGM_PPP_COOKIE,
328f8307e12SArchie Cobbs 	  NGM_PPP_GET_LINK_STATS,
329f8307e12SArchie Cobbs 	  "getstats",
330f8307e12SArchie Cobbs 	  &ng_parse_int16_type,
331f8307e12SArchie Cobbs 	  &ng_ppp_stats_type
332f8307e12SArchie Cobbs 	},
333f8307e12SArchie Cobbs 	{
334f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
335f8307e12SArchie Cobbs 	  NGM_PPP_CLR_LINK_STATS,
336f8307e12SArchie Cobbs 	  "clrstats",
337f8307e12SArchie Cobbs 	  &ng_parse_int16_type,
338f8307e12SArchie Cobbs 	  NULL
339f8307e12SArchie Cobbs 	},
340f8307e12SArchie Cobbs 	{
341f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
342f8307e12SArchie Cobbs 	  NGM_PPP_GETCLR_LINK_STATS,
343f8307e12SArchie Cobbs 	  "getclrstats",
344f8307e12SArchie Cobbs 	  &ng_parse_int16_type,
345f8307e12SArchie Cobbs 	  &ng_ppp_stats_type
346f8307e12SArchie Cobbs 	},
347f8307e12SArchie Cobbs 	{ 0 }
348f8307e12SArchie Cobbs };
349f8307e12SArchie Cobbs 
3504cf49a43SJulian Elischer /* Node type descriptor */
3513949bee8SArchie Cobbs static struct ng_type ng_ppp_typestruct = {
352589f6ed8SJulian Elischer 	NG_ABI_VERSION,
3534cf49a43SJulian Elischer 	NG_PPP_NODE_TYPE,
3544cf49a43SJulian Elischer 	NULL,
3554cf49a43SJulian Elischer 	ng_ppp_constructor,
3564cf49a43SJulian Elischer 	ng_ppp_rcvmsg,
357069154d5SJulian Elischer 	ng_ppp_shutdown,
3584cf49a43SJulian Elischer 	ng_ppp_newhook,
3594cf49a43SJulian Elischer 	NULL,
3604cf49a43SJulian Elischer 	NULL,
3614cf49a43SJulian Elischer 	ng_ppp_rcvdata,
362f8307e12SArchie Cobbs 	ng_ppp_disconnect,
363f8307e12SArchie Cobbs 	ng_ppp_cmds
3644cf49a43SJulian Elischer };
3653949bee8SArchie Cobbs NETGRAPH_INIT(ppp, &ng_ppp_typestruct);
3664cf49a43SJulian Elischer 
3673949bee8SArchie Cobbs static int *compareLatencies;			/* hack for ng_ppp_intcmp() */
3684cf49a43SJulian Elischer 
369d690a6e7SArchie Cobbs /* Address and control field header */
370d690a6e7SArchie Cobbs static const u_char ng_ppp_acf[2] = { 0xff, 0x03 };
371d690a6e7SArchie Cobbs 
372a9b3dca5SArchie Cobbs /* Maximum time we'll let a complete incoming packet sit in the queue */
373a9b3dca5SArchie Cobbs static const struct timeval ng_ppp_max_staleness = { 2, 0 };	/* 2 seconds */
374a9b3dca5SArchie Cobbs 
3754cf49a43SJulian Elischer #define ERROUT(x)	do { error = (x); goto done; } while (0)
3764cf49a43SJulian Elischer 
3774cf49a43SJulian Elischer /************************************************************************
3784cf49a43SJulian Elischer 			NETGRAPH NODE STUFF
3794cf49a43SJulian Elischer  ************************************************************************/
3804cf49a43SJulian Elischer 
3814cf49a43SJulian Elischer /*
3823949bee8SArchie Cobbs  * Node type constructor
3834cf49a43SJulian Elischer  */
3844cf49a43SJulian Elischer static int
385069154d5SJulian Elischer ng_ppp_constructor(node_p node)
3864cf49a43SJulian Elischer {
3874cf49a43SJulian Elischer 	priv_p priv;
388069154d5SJulian Elischer 	int i;
3894cf49a43SJulian Elischer 
3904cf49a43SJulian Elischer 	/* Allocate private structure */
3919c8c302fSJulian Elischer 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_PPP, M_NOWAIT | M_ZERO);
3924cf49a43SJulian Elischer 	if (priv == NULL)
3934cf49a43SJulian Elischer 		return (ENOMEM);
3944cf49a43SJulian Elischer 
39530400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
3964cf49a43SJulian Elischer 
3973949bee8SArchie Cobbs 	/* Initialize state */
3985f90cac7SKirk McKusick 	TAILQ_INIT(&priv->frags);
399a9b3dca5SArchie Cobbs 	for (i = 0; i < NG_PPP_MAX_LINKS; i++)
400a9b3dca5SArchie Cobbs 		priv->links[i].seq = MP_NOSEQ;
401a9b3dca5SArchie Cobbs 	callout_handle_init(&priv->fragTimer);
4023949bee8SArchie Cobbs 
4034cf49a43SJulian Elischer 	/* Done */
4044cf49a43SJulian Elischer 	return (0);
4054cf49a43SJulian Elischer }
4064cf49a43SJulian Elischer 
4074cf49a43SJulian Elischer /*
4084cf49a43SJulian Elischer  * Give our OK for a hook to be added
4094cf49a43SJulian Elischer  */
4104cf49a43SJulian Elischer static int
4114cf49a43SJulian Elischer ng_ppp_newhook(node_p node, hook_p hook, const char *name)
4124cf49a43SJulian Elischer {
41330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
4143949bee8SArchie Cobbs 	int linkNum = -1;
4153949bee8SArchie Cobbs 	hook_p *hookPtr = NULL;
4163949bee8SArchie Cobbs 	int hookIndex = -1;
4174cf49a43SJulian Elischer 
4183949bee8SArchie Cobbs 	/* Figure out which hook it is */
4193949bee8SArchie Cobbs 	if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX,	/* a link hook? */
4203949bee8SArchie Cobbs 	    strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) {
42125792ef3SArchie Cobbs 		const char *cp;
42225792ef3SArchie Cobbs 		char *eptr;
4233949bee8SArchie Cobbs 
4242b70adcbSArchie Cobbs 		cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
4252b70adcbSArchie Cobbs 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
4264cf49a43SJulian Elischer 			return (EINVAL);
4272b70adcbSArchie Cobbs 		linkNum = (int)strtoul(cp, &eptr, 10);
4282b70adcbSArchie Cobbs 		if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS)
4293949bee8SArchie Cobbs 			return (EINVAL);
430a9b3dca5SArchie Cobbs 		hookPtr = &priv->links[linkNum].hook;
4313949bee8SArchie Cobbs 		hookIndex = ~linkNum;
4323949bee8SArchie Cobbs 	} else {				/* must be a non-link hook */
4333949bee8SArchie Cobbs 		int i;
4344cf49a43SJulian Elischer 
4353949bee8SArchie Cobbs 		for (i = 0; ng_ppp_hook_names[i] != NULL; i++) {
4363949bee8SArchie Cobbs 			if (strcmp(name, ng_ppp_hook_names[i]) == 0) {
4373949bee8SArchie Cobbs 				hookPtr = &priv->hooks[i];
4383949bee8SArchie Cobbs 				hookIndex = i;
4393949bee8SArchie Cobbs 				break;
4403949bee8SArchie Cobbs 			}
4413949bee8SArchie Cobbs 		}
4423949bee8SArchie Cobbs 		if (ng_ppp_hook_names[i] == NULL)
4433949bee8SArchie Cobbs 			return (EINVAL);	/* no such hook */
4443949bee8SArchie Cobbs 	}
4453949bee8SArchie Cobbs 
4463949bee8SArchie Cobbs 	/* See if hook is already connected */
4473949bee8SArchie Cobbs 	if (*hookPtr != NULL)
4484cf49a43SJulian Elischer 		return (EISCONN);
4494cf49a43SJulian Elischer 
4503949bee8SArchie Cobbs 	/* Disallow more than one link unless multilink is enabled */
451a9b3dca5SArchie Cobbs 	if (linkNum != -1 && priv->links[linkNum].conf.enableLink
4523949bee8SArchie Cobbs 	    && !priv->conf.enableMultilink && priv->numActiveLinks >= 1)
4533949bee8SArchie Cobbs 		return (ENODEV);
4544cf49a43SJulian Elischer 
4554cf49a43SJulian Elischer 	/* OK */
4563949bee8SArchie Cobbs 	*hookPtr = hook;
4578214d60eSJohn Baldwin 	NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)hookIndex);
4583949bee8SArchie Cobbs 	ng_ppp_update(node, 0);
4594cf49a43SJulian Elischer 	return (0);
4604cf49a43SJulian Elischer }
4614cf49a43SJulian Elischer 
4624cf49a43SJulian Elischer /*
4634cf49a43SJulian Elischer  * Receive a control message
4644cf49a43SJulian Elischer  */
4654cf49a43SJulian Elischer static int
466069154d5SJulian Elischer ng_ppp_rcvmsg(node_p node, item_p item, hook_p lasthook)
4674cf49a43SJulian Elischer {
46830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
4694cf49a43SJulian Elischer 	struct ng_mesg *resp = NULL;
4704cf49a43SJulian Elischer 	int error = 0;
471069154d5SJulian Elischer 	struct ng_mesg *msg;
4724cf49a43SJulian Elischer 
473069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
4744cf49a43SJulian Elischer 	switch (msg->header.typecookie) {
4754cf49a43SJulian Elischer 	case NGM_PPP_COOKIE:
4764cf49a43SJulian Elischer 		switch (msg->header.cmd) {
4773949bee8SArchie Cobbs 		case NGM_PPP_SET_CONFIG:
4783949bee8SArchie Cobbs 		    {
479a9b3dca5SArchie Cobbs 			struct ng_ppp_node_conf *const conf =
480a9b3dca5SArchie Cobbs 			    (struct ng_ppp_node_conf *)msg->data;
481a9b3dca5SArchie Cobbs 			int i;
4823949bee8SArchie Cobbs 
4833949bee8SArchie Cobbs 			/* Check for invalid or illegal config */
484a9b3dca5SArchie Cobbs 			if (msg->header.arglen != sizeof(*conf))
4854cf49a43SJulian Elischer 				ERROUT(EINVAL);
486a9b3dca5SArchie Cobbs 			if (!ng_ppp_config_valid(node, conf))
4873949bee8SArchie Cobbs 				ERROUT(EINVAL);
488a9b3dca5SArchie Cobbs 
489a9b3dca5SArchie Cobbs 			/* Copy config */
490a9b3dca5SArchie Cobbs 			priv->conf = conf->bund;
491a9b3dca5SArchie Cobbs 			for (i = 0; i < NG_PPP_MAX_LINKS; i++)
492a9b3dca5SArchie Cobbs 				priv->links[i].conf = conf->links[i];
4933949bee8SArchie Cobbs 			ng_ppp_update(node, 1);
4944cf49a43SJulian Elischer 			break;
4953949bee8SArchie Cobbs 		    }
4963949bee8SArchie Cobbs 		case NGM_PPP_GET_CONFIG:
497a9b3dca5SArchie Cobbs 		    {
498a9b3dca5SArchie Cobbs 			struct ng_ppp_node_conf *conf;
499a9b3dca5SArchie Cobbs 			int i;
500a9b3dca5SArchie Cobbs 
501a9b3dca5SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
5024cf49a43SJulian Elischer 			if (resp == NULL)
5034cf49a43SJulian Elischer 				ERROUT(ENOMEM);
504a9b3dca5SArchie Cobbs 			conf = (struct ng_ppp_node_conf *)resp->data;
505a9b3dca5SArchie Cobbs 			conf->bund = priv->conf;
506a9b3dca5SArchie Cobbs 			for (i = 0; i < NG_PPP_MAX_LINKS; i++)
507a9b3dca5SArchie Cobbs 				conf->links[i] = priv->links[i].conf;
5084cf49a43SJulian Elischer 			break;
509a9b3dca5SArchie Cobbs 		    }
5106f16db81SArchie Cobbs 		case NGM_PPP_GET_MP_STATE:
5116f16db81SArchie Cobbs 		    {
5126f16db81SArchie Cobbs 			struct ng_ppp_mp_state *info;
5136f16db81SArchie Cobbs 			int i;
5146f16db81SArchie Cobbs 
5156f16db81SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT);
5166f16db81SArchie Cobbs 			if (resp == NULL)
5176f16db81SArchie Cobbs 				ERROUT(ENOMEM);
5186f16db81SArchie Cobbs 			info = (struct ng_ppp_mp_state *)resp->data;
5196f16db81SArchie Cobbs 			bzero(info, sizeof(*info));
5206f16db81SArchie Cobbs 			for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
5216f16db81SArchie Cobbs 				if (priv->links[i].seq != MP_NOSEQ)
5226f16db81SArchie Cobbs 					info->rseq[i] = priv->links[i].seq;
5236f16db81SArchie Cobbs 			}
5246f16db81SArchie Cobbs 			info->mseq = priv->mseq;
5256f16db81SArchie Cobbs 			info->xseq = priv->xseq;
5266f16db81SArchie Cobbs 			break;
5276f16db81SArchie Cobbs 		    }
5283949bee8SArchie Cobbs 		case NGM_PPP_GET_LINK_STATS:
5293949bee8SArchie Cobbs 		case NGM_PPP_CLR_LINK_STATS:
530e149c4e2SArchie Cobbs 		case NGM_PPP_GETCLR_LINK_STATS:
5313949bee8SArchie Cobbs 		    {
5323949bee8SArchie Cobbs 			struct ng_ppp_link_stat *stats;
5333949bee8SArchie Cobbs 			u_int16_t linkNum;
5343949bee8SArchie Cobbs 
5353949bee8SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int16_t))
5363949bee8SArchie Cobbs 				ERROUT(EINVAL);
5373949bee8SArchie Cobbs 			linkNum = *((u_int16_t *) msg->data);
5383949bee8SArchie Cobbs 			if (linkNum >= NG_PPP_MAX_LINKS
5393949bee8SArchie Cobbs 			    && linkNum != NG_PPP_BUNDLE_LINKNUM)
5403949bee8SArchie Cobbs 				ERROUT(EINVAL);
5413949bee8SArchie Cobbs 			stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ?
542a9b3dca5SArchie Cobbs 			    &priv->bundleStats : &priv->links[linkNum].stats;
543e149c4e2SArchie Cobbs 			if (msg->header.cmd != NGM_PPP_CLR_LINK_STATS) {
5443949bee8SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
5453949bee8SArchie Cobbs 				    sizeof(struct ng_ppp_link_stat), M_NOWAIT);
5463949bee8SArchie Cobbs 				if (resp == NULL)
5473949bee8SArchie Cobbs 					ERROUT(ENOMEM);
5483949bee8SArchie Cobbs 				bcopy(stats, resp->data, sizeof(*stats));
549e149c4e2SArchie Cobbs 			}
550e149c4e2SArchie Cobbs 			if (msg->header.cmd != NGM_PPP_GET_LINK_STATS)
5513949bee8SArchie Cobbs 				bzero(stats, sizeof(*stats));
5524cf49a43SJulian Elischer 			break;
5533949bee8SArchie Cobbs 		    }
5544cf49a43SJulian Elischer 		default:
5554cf49a43SJulian Elischer 			error = EINVAL;
5564cf49a43SJulian Elischer 			break;
5574cf49a43SJulian Elischer 		}
5584cf49a43SJulian Elischer 		break;
5593949bee8SArchie Cobbs 	case NGM_VJC_COOKIE:
5603949bee8SArchie Cobbs 	    {
561069154d5SJulian Elischer 		/*
562069154d5SJulian Elischer 		 * Forward it to the vjc node. leave the
563069154d5SJulian Elischer 		 * old return address alone.
56430400f03SJulian Elischer 		 * If we have no hook, let NG_RESPOND_MSG
56530400f03SJulian Elischer 		 * clean up any remaining resources.
56630400f03SJulian Elischer 		 * Because we have no resp, the item will be freed
56730400f03SJulian Elischer 		 * along with anything it references. Don't
56830400f03SJulian Elischer 		 * let msg be freed twice.
569069154d5SJulian Elischer 		 */
570069154d5SJulian Elischer 		NGI_MSG(item) = msg;	/* put it back in the item */
57130400f03SJulian Elischer 		msg = NULL;
57230400f03SJulian Elischer 		if ((lasthook = priv->links[HOOK_INDEX_VJC_IP].hook)) {
57330400f03SJulian Elischer 			NG_FWD_ITEM_HOOK(error, item, lasthook);
574069154d5SJulian Elischer 		}
575069154d5SJulian Elischer 		return (error);
5763949bee8SArchie Cobbs 	    }
5774cf49a43SJulian Elischer 	default:
5784cf49a43SJulian Elischer 		error = EINVAL;
5794cf49a43SJulian Elischer 		break;
5804cf49a43SJulian Elischer 	}
5814cf49a43SJulian Elischer done:
582069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
583069154d5SJulian Elischer 	NG_FREE_MSG(msg);
5844cf49a43SJulian Elischer 	return (error);
5854cf49a43SJulian Elischer }
5864cf49a43SJulian Elischer 
5874cf49a43SJulian Elischer /*
5884cf49a43SJulian Elischer  * Receive data on a hook
5894cf49a43SJulian Elischer  */
5904cf49a43SJulian Elischer static int
591069154d5SJulian Elischer ng_ppp_rcvdata(hook_p hook, item_p item)
5924cf49a43SJulian Elischer {
59330400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
59430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
5958214d60eSJohn Baldwin 	const int index = (intptr_t)NG_HOOK_PRIVATE(hook);
5963949bee8SArchie Cobbs 	u_int16_t linkNum = NG_PPP_BUNDLE_LINKNUM;
5973949bee8SArchie Cobbs 	hook_p outHook = NULL;
5983949bee8SArchie Cobbs 	int proto = 0, error;
599069154d5SJulian Elischer 	struct mbuf *m;
6004cf49a43SJulian Elischer 
601069154d5SJulian Elischer 	NGI_GET_M(item, m);
6023949bee8SArchie Cobbs 	/* Did it come from a link hook? */
6033949bee8SArchie Cobbs 	if (index < 0) {
604a9b3dca5SArchie Cobbs 		struct ng_ppp_link *link;
6054cf49a43SJulian Elischer 
606e149c4e2SArchie Cobbs 		/* Convert index into a link number */
6073949bee8SArchie Cobbs 		linkNum = (u_int16_t)~index;
6083949bee8SArchie Cobbs 		KASSERT(linkNum < NG_PPP_MAX_LINKS,
6096e551fb6SDavid E. O'Brien 		    ("%s: bogus index 0x%x", __func__, index));
610a9b3dca5SArchie Cobbs 		link = &priv->links[linkNum];
6113949bee8SArchie Cobbs 
6123949bee8SArchie Cobbs 		/* Stats */
613a9b3dca5SArchie Cobbs 		link->stats.recvFrames++;
614a9b3dca5SArchie Cobbs 		link->stats.recvOctets += m->m_pkthdr.len;
6153949bee8SArchie Cobbs 
616d690a6e7SArchie Cobbs 		/* Strip address and control fields, if present */
617d690a6e7SArchie Cobbs 		if (m->m_pkthdr.len >= 2) {
618d690a6e7SArchie Cobbs 			if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
619069154d5SJulian Elischer 				NG_FREE_ITEM(item);
620d690a6e7SArchie Cobbs 				return (ENOBUFS);
621d690a6e7SArchie Cobbs 			}
622d690a6e7SArchie Cobbs 			if (bcmp(mtod(m, u_char *), &ng_ppp_acf, 2) == 0)
623d690a6e7SArchie Cobbs 				m_adj(m, 2);
624d690a6e7SArchie Cobbs 		}
625d690a6e7SArchie Cobbs 
626e149c4e2SArchie Cobbs 		/* Dispatch incoming frame (if not enabled, to bypass) */
627069154d5SJulian Elischer 		NGI_M(item) = m; 	/* put changed m back in item */
628e149c4e2SArchie Cobbs 		return ng_ppp_input(node,
629069154d5SJulian Elischer 		    !link->conf.enableLink, linkNum, item);
6303949bee8SArchie Cobbs 	}
6313949bee8SArchie Cobbs 
6323949bee8SArchie Cobbs 	/* Get protocol & check if data allowed from this hook */
633069154d5SJulian Elischer 	NGI_M(item) = m; 	/* put possibly changed m back in item */
6343949bee8SArchie Cobbs 	switch (index) {
6353949bee8SArchie Cobbs 
636e149c4e2SArchie Cobbs 	/* Outgoing data */
6373949bee8SArchie Cobbs 	case HOOK_INDEX_ATALK:
6383949bee8SArchie Cobbs 		if (!priv->conf.enableAtalk) {
639069154d5SJulian Elischer 			NG_FREE_ITEM(item);
6403949bee8SArchie Cobbs 			return (ENXIO);
6413949bee8SArchie Cobbs 		}
6423949bee8SArchie Cobbs 		proto = PROT_APPLETALK;
6433949bee8SArchie Cobbs 		break;
6443949bee8SArchie Cobbs 	case HOOK_INDEX_IPX:
6453949bee8SArchie Cobbs 		if (!priv->conf.enableIPX) {
646069154d5SJulian Elischer 			NG_FREE_ITEM(item);
6473949bee8SArchie Cobbs 			return (ENXIO);
6483949bee8SArchie Cobbs 		}
6493949bee8SArchie Cobbs 		proto = PROT_IPX;
6503949bee8SArchie Cobbs 		break;
651a9b3dca5SArchie Cobbs 	case HOOK_INDEX_IPV6:
652a9b3dca5SArchie Cobbs 		if (!priv->conf.enableIPv6) {
653069154d5SJulian Elischer 			NG_FREE_ITEM(item);
654a9b3dca5SArchie Cobbs 			return (ENXIO);
655a9b3dca5SArchie Cobbs 		}
656a9b3dca5SArchie Cobbs 		proto = PROT_IPV6;
657a9b3dca5SArchie Cobbs 		break;
6583949bee8SArchie Cobbs 	case HOOK_INDEX_INET:
6593949bee8SArchie Cobbs 	case HOOK_INDEX_VJC_VJIP:
6603949bee8SArchie Cobbs 		if (!priv->conf.enableIP) {
661069154d5SJulian Elischer 			NG_FREE_ITEM(item);
6623949bee8SArchie Cobbs 			return (ENXIO);
6633949bee8SArchie Cobbs 		}
6643949bee8SArchie Cobbs 		proto = PROT_IP;
6653949bee8SArchie Cobbs 		break;
6663949bee8SArchie Cobbs 	case HOOK_INDEX_VJC_COMP:
6673949bee8SArchie Cobbs 		if (!priv->conf.enableVJCompression) {
668069154d5SJulian Elischer 			NG_FREE_ITEM(item);
6693949bee8SArchie Cobbs 			return (ENXIO);
6703949bee8SArchie Cobbs 		}
6713949bee8SArchie Cobbs 		proto = PROT_VJCOMP;
6723949bee8SArchie Cobbs 		break;
6733949bee8SArchie Cobbs 	case HOOK_INDEX_VJC_UNCOMP:
6743949bee8SArchie Cobbs 		if (!priv->conf.enableVJCompression) {
675069154d5SJulian Elischer 			NG_FREE_ITEM(item);
6763949bee8SArchie Cobbs 			return (ENXIO);
6773949bee8SArchie Cobbs 		}
6783949bee8SArchie Cobbs 		proto = PROT_VJUNCOMP;
6793949bee8SArchie Cobbs 		break;
6803949bee8SArchie Cobbs 	case HOOK_INDEX_COMPRESS:
6813949bee8SArchie Cobbs 		if (!priv->conf.enableCompression) {
682069154d5SJulian Elischer 			NG_FREE_ITEM(item);
6833949bee8SArchie Cobbs 			return (ENXIO);
6843949bee8SArchie Cobbs 		}
6853949bee8SArchie Cobbs 		proto = PROT_COMPD;
6863949bee8SArchie Cobbs 		break;
6873949bee8SArchie Cobbs 	case HOOK_INDEX_ENCRYPT:
6883949bee8SArchie Cobbs 		if (!priv->conf.enableEncryption) {
689069154d5SJulian Elischer 			NG_FREE_ITEM(item);
6903949bee8SArchie Cobbs 			return (ENXIO);
6913949bee8SArchie Cobbs 		}
6923949bee8SArchie Cobbs 		proto = PROT_CRYPTD;
6933949bee8SArchie Cobbs 		break;
6943949bee8SArchie Cobbs 	case HOOK_INDEX_BYPASS:
6953949bee8SArchie Cobbs 		if (m->m_pkthdr.len < 4) {
696069154d5SJulian Elischer 			NG_FREE_ITEM(item);
6973949bee8SArchie Cobbs 			return (EINVAL);
6983949bee8SArchie Cobbs 		}
6993949bee8SArchie Cobbs 		if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
700069154d5SJulian Elischer 			NGI_M(item) = NULL; /* don't free twice */
701069154d5SJulian Elischer 			NG_FREE_ITEM(item);
7023949bee8SArchie Cobbs 			return (ENOBUFS);
7033949bee8SArchie Cobbs 		}
704069154d5SJulian Elischer 		NGI_M(item) = m; /* m may have changed */
7053949bee8SArchie Cobbs 		linkNum = ntohs(mtod(m, u_int16_t *)[0]);
7063949bee8SArchie Cobbs 		proto = ntohs(mtod(m, u_int16_t *)[1]);
7073949bee8SArchie Cobbs 		m_adj(m, 4);
7083949bee8SArchie Cobbs 		if (linkNum >= NG_PPP_MAX_LINKS
7094c641908SArchie Cobbs 		    && linkNum != NG_PPP_BUNDLE_LINKNUM) {
710069154d5SJulian Elischer 			NG_FREE_ITEM(item);
7113949bee8SArchie Cobbs 			return (EINVAL);
7124c641908SArchie Cobbs 		}
7133949bee8SArchie Cobbs 		break;
7143949bee8SArchie Cobbs 
715e149c4e2SArchie Cobbs 	/* Incoming data */
7163949bee8SArchie Cobbs 	case HOOK_INDEX_VJC_IP:
717e149c4e2SArchie Cobbs 		if (!priv->conf.enableIP || !priv->conf.enableVJDecompression) {
718069154d5SJulian Elischer 			NG_FREE_ITEM(item);
7193949bee8SArchie Cobbs 			return (ENXIO);
7203949bee8SArchie Cobbs 		}
7213949bee8SArchie Cobbs 		break;
7223949bee8SArchie Cobbs 	case HOOK_INDEX_DECOMPRESS:
7233949bee8SArchie Cobbs 		if (!priv->conf.enableDecompression) {
724069154d5SJulian Elischer 			NG_FREE_ITEM(item);
7253949bee8SArchie Cobbs 			return (ENXIO);
7263949bee8SArchie Cobbs 		}
7273949bee8SArchie Cobbs 		break;
7283949bee8SArchie Cobbs 	case HOOK_INDEX_DECRYPT:
7293949bee8SArchie Cobbs 		if (!priv->conf.enableDecryption) {
730069154d5SJulian Elischer 			NG_FREE_ITEM(item);
7313949bee8SArchie Cobbs 			return (ENXIO);
7323949bee8SArchie Cobbs 		}
7333949bee8SArchie Cobbs 		break;
7343949bee8SArchie Cobbs 	default:
7356e551fb6SDavid E. O'Brien 		panic("%s: bogus index 0x%x", __func__, index);
7363949bee8SArchie Cobbs 	}
7373949bee8SArchie Cobbs 
7383949bee8SArchie Cobbs 	/* Now figure out what to do with the frame */
7393949bee8SArchie Cobbs 	switch (index) {
740e149c4e2SArchie Cobbs 
741e149c4e2SArchie Cobbs 	/* Outgoing data */
7423949bee8SArchie Cobbs 	case HOOK_INDEX_INET:
7433949bee8SArchie Cobbs 		if (priv->conf.enableVJCompression && priv->vjCompHooked) {
7443949bee8SArchie Cobbs 			outHook = priv->hooks[HOOK_INDEX_VJC_IP];
7453949bee8SArchie Cobbs 			break;
7463949bee8SArchie Cobbs 		}
7473949bee8SArchie Cobbs 		/* FALLTHROUGH */
7483949bee8SArchie Cobbs 	case HOOK_INDEX_ATALK:
749a9b3dca5SArchie Cobbs 	case HOOK_INDEX_IPV6:
7503949bee8SArchie Cobbs 	case HOOK_INDEX_IPX:
7513949bee8SArchie Cobbs 	case HOOK_INDEX_VJC_COMP:
7523949bee8SArchie Cobbs 	case HOOK_INDEX_VJC_UNCOMP:
7533949bee8SArchie Cobbs 	case HOOK_INDEX_VJC_VJIP:
7543949bee8SArchie Cobbs 		if (priv->conf.enableCompression
7553949bee8SArchie Cobbs 		    && priv->hooks[HOOK_INDEX_COMPRESS] != NULL) {
7563949bee8SArchie Cobbs 			if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
757069154d5SJulian Elischer 				NGI_M(item) = NULL;
758069154d5SJulian Elischer 				NG_FREE_ITEM(item);
7593949bee8SArchie Cobbs 				return (ENOBUFS);
7603949bee8SArchie Cobbs 			}
761069154d5SJulian Elischer 			NGI_M(item) = m; /* m may have changed */
7623949bee8SArchie Cobbs 			outHook = priv->hooks[HOOK_INDEX_COMPRESS];
7633949bee8SArchie Cobbs 			break;
7643949bee8SArchie Cobbs 		}
7653949bee8SArchie Cobbs 		/* FALLTHROUGH */
7663949bee8SArchie Cobbs 	case HOOK_INDEX_COMPRESS:
7673949bee8SArchie Cobbs 		if (priv->conf.enableEncryption
7683949bee8SArchie Cobbs 		    && priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) {
7693949bee8SArchie Cobbs 			if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
770069154d5SJulian Elischer 				NGI_M(item) = NULL;
771069154d5SJulian Elischer 				NG_FREE_ITEM(item);
7723949bee8SArchie Cobbs 				return (ENOBUFS);
7733949bee8SArchie Cobbs 			}
774069154d5SJulian Elischer 			NGI_M(item) = m; /* m may have changed */
7753949bee8SArchie Cobbs 			outHook = priv->hooks[HOOK_INDEX_ENCRYPT];
7763949bee8SArchie Cobbs 			break;
7773949bee8SArchie Cobbs 		}
7783949bee8SArchie Cobbs 		/* FALLTHROUGH */
7793949bee8SArchie Cobbs 	case HOOK_INDEX_ENCRYPT:
780069154d5SJulian Elischer 		return ng_ppp_output(node, 0, proto, NG_PPP_BUNDLE_LINKNUM, item);
7813949bee8SArchie Cobbs 
7820e11d0f3SArchie Cobbs 	case HOOK_INDEX_BYPASS:
783069154d5SJulian Elischer 		return ng_ppp_output(node, 1, proto, linkNum, item);
7840e11d0f3SArchie Cobbs 
7853949bee8SArchie Cobbs 	/* Incoming data */
7863949bee8SArchie Cobbs 	case HOOK_INDEX_DECRYPT:
7873949bee8SArchie Cobbs 	case HOOK_INDEX_DECOMPRESS:
788069154d5SJulian Elischer 		return ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
789e149c4e2SArchie Cobbs 
7903949bee8SArchie Cobbs 	case HOOK_INDEX_VJC_IP:
791e149c4e2SArchie Cobbs 		outHook = priv->hooks[HOOK_INDEX_INET];
792e149c4e2SArchie Cobbs 		break;
7933949bee8SArchie Cobbs 	}
7943949bee8SArchie Cobbs 
7953949bee8SArchie Cobbs 	/* Send packet out hook */
79630400f03SJulian Elischer 	NG_FWD_ITEM_HOOK(error, item, outHook);
7974cf49a43SJulian Elischer 	return (error);
7984cf49a43SJulian Elischer }
7994cf49a43SJulian Elischer 
8004cf49a43SJulian Elischer /*
8014cf49a43SJulian Elischer  * Destroy node
8024cf49a43SJulian Elischer  */
8034cf49a43SJulian Elischer static int
804069154d5SJulian Elischer ng_ppp_shutdown(node_p node)
8054cf49a43SJulian Elischer {
80630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
8074cf49a43SJulian Elischer 
808a9b3dca5SArchie Cobbs 	/* Stop fragment queue timer */
809a9b3dca5SArchie Cobbs 	ng_ppp_stop_frag_timer(node);
810a9b3dca5SArchie Cobbs 
8114cf49a43SJulian Elischer 	/* Take down netgraph node */
812a9b3dca5SArchie Cobbs 	ng_ppp_frag_reset(node);
8134cf49a43SJulian Elischer 	bzero(priv, sizeof(*priv));
8149c8c302fSJulian Elischer 	FREE(priv, M_NETGRAPH_PPP);
81530400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, NULL);
81630400f03SJulian Elischer 	NG_NODE_UNREF(node);		/* let the node escape */
8174cf49a43SJulian Elischer 	return (0);
8184cf49a43SJulian Elischer }
8194cf49a43SJulian Elischer 
8204cf49a43SJulian Elischer /*
8214cf49a43SJulian Elischer  * Hook disconnection
8224cf49a43SJulian Elischer  */
8234cf49a43SJulian Elischer static int
8244cf49a43SJulian Elischer ng_ppp_disconnect(hook_p hook)
8254cf49a43SJulian Elischer {
82630400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
82730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
8288214d60eSJohn Baldwin 	const int index = (intptr_t)NG_HOOK_PRIVATE(hook);
82962838faeSArchie Cobbs 
83062838faeSArchie Cobbs 	/* Zero out hook pointer */
83162838faeSArchie Cobbs 	if (index < 0)
832a9b3dca5SArchie Cobbs 		priv->links[~index].hook = NULL;
83362838faeSArchie Cobbs 	else
83462838faeSArchie Cobbs 		priv->hooks[index] = NULL;
83562838faeSArchie Cobbs 
83662838faeSArchie Cobbs 	/* Update derived info (or go away if no hooks left) */
83730400f03SJulian Elischer 	if (NG_NODE_NUMHOOKS(node) > 0) {
83862838faeSArchie Cobbs 		ng_ppp_update(node, 0);
839069154d5SJulian Elischer 	} else {
84030400f03SJulian Elischer 		if (NG_NODE_IS_VALID(node)) {
841069154d5SJulian Elischer 			ng_rmnode_self(node);
842069154d5SJulian Elischer 		}
843069154d5SJulian Elischer 	}
8444cf49a43SJulian Elischer 	return (0);
8454cf49a43SJulian Elischer }
8464cf49a43SJulian Elischer 
8474cf49a43SJulian Elischer /************************************************************************
8484cf49a43SJulian Elischer 			HELPER STUFF
8494cf49a43SJulian Elischer  ************************************************************************/
8504cf49a43SJulian Elischer 
8514cf49a43SJulian Elischer /*
8523949bee8SArchie Cobbs  * Handle an incoming frame.  Extract the PPP protocol number
8533949bee8SArchie Cobbs  * and dispatch accordingly.
8544cf49a43SJulian Elischer  */
8554cf49a43SJulian Elischer static int
856069154d5SJulian Elischer ng_ppp_input(node_p node, int bypass, int linkNum, item_p item)
8574cf49a43SJulian Elischer {
85830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
8593949bee8SArchie Cobbs 	hook_p outHook = NULL;
8603949bee8SArchie Cobbs 	int proto, error;
861069154d5SJulian Elischer 	struct mbuf *m;
8624cf49a43SJulian Elischer 
863069154d5SJulian Elischer 
864069154d5SJulian Elischer 	NGI_GET_M(item, m);
8653949bee8SArchie Cobbs 	/* Extract protocol number */
8663949bee8SArchie Cobbs 	for (proto = 0; !PROT_VALID(proto) && m->m_pkthdr.len > 0; ) {
8673949bee8SArchie Cobbs 		if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL) {
868069154d5SJulian Elischer 			NG_FREE_ITEM(item);
8693949bee8SArchie Cobbs 			return (ENOBUFS);
8704cf49a43SJulian Elischer 		}
8713949bee8SArchie Cobbs 		proto = (proto << 8) + *mtod(m, u_char *);
8723949bee8SArchie Cobbs 		m_adj(m, 1);
8733949bee8SArchie Cobbs 	}
8743949bee8SArchie Cobbs 	if (!PROT_VALID(proto)) {
8753949bee8SArchie Cobbs 		if (linkNum == NG_PPP_BUNDLE_LINKNUM)
8763949bee8SArchie Cobbs 			priv->bundleStats.badProtos++;
8773949bee8SArchie Cobbs 		else
878a9b3dca5SArchie Cobbs 			priv->links[linkNum].stats.badProtos++;
879069154d5SJulian Elischer 		NG_FREE_ITEM(item);
880069154d5SJulian Elischer 		NG_FREE_M(m);
8813949bee8SArchie Cobbs 		return (EINVAL);
8823949bee8SArchie Cobbs 	}
8833949bee8SArchie Cobbs 
884e149c4e2SArchie Cobbs 	/* Bypass frame? */
885e149c4e2SArchie Cobbs 	if (bypass)
886e149c4e2SArchie Cobbs 		goto bypass;
887e149c4e2SArchie Cobbs 
8883949bee8SArchie Cobbs 	/* Check protocol */
8893949bee8SArchie Cobbs 	switch (proto) {
8903949bee8SArchie Cobbs 	case PROT_COMPD:
8913949bee8SArchie Cobbs 		if (priv->conf.enableDecompression)
8923949bee8SArchie Cobbs 			outHook = priv->hooks[HOOK_INDEX_DECOMPRESS];
8933949bee8SArchie Cobbs 		break;
8943949bee8SArchie Cobbs 	case PROT_CRYPTD:
8953949bee8SArchie Cobbs 		if (priv->conf.enableDecryption)
8963949bee8SArchie Cobbs 			outHook = priv->hooks[HOOK_INDEX_DECRYPT];
8973949bee8SArchie Cobbs 		break;
8983949bee8SArchie Cobbs 	case PROT_VJCOMP:
8993949bee8SArchie Cobbs 		if (priv->conf.enableVJDecompression && priv->vjCompHooked)
9003949bee8SArchie Cobbs 			outHook = priv->hooks[HOOK_INDEX_VJC_COMP];
9013949bee8SArchie Cobbs 		break;
9023949bee8SArchie Cobbs 	case PROT_VJUNCOMP:
9033949bee8SArchie Cobbs 		if (priv->conf.enableVJDecompression && priv->vjCompHooked)
9043949bee8SArchie Cobbs 			outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP];
9053949bee8SArchie Cobbs 		break;
9063949bee8SArchie Cobbs 	case PROT_MP:
907a9b3dca5SArchie Cobbs 		if (priv->conf.enableMultilink
908ed283669SJulian Elischer 		    && linkNum != NG_PPP_BUNDLE_LINKNUM) {
909ed283669SJulian Elischer 			NGI_M(item) = m;
910069154d5SJulian Elischer 			return ng_ppp_mp_input(node, linkNum, item);
911ed283669SJulian Elischer 		}
9123949bee8SArchie Cobbs 		break;
9133949bee8SArchie Cobbs 	case PROT_APPLETALK:
9143949bee8SArchie Cobbs 		if (priv->conf.enableAtalk)
9153949bee8SArchie Cobbs 			outHook = priv->hooks[HOOK_INDEX_ATALK];
9163949bee8SArchie Cobbs 		break;
9173949bee8SArchie Cobbs 	case PROT_IPX:
9183949bee8SArchie Cobbs 		if (priv->conf.enableIPX)
9193949bee8SArchie Cobbs 			outHook = priv->hooks[HOOK_INDEX_IPX];
9203949bee8SArchie Cobbs 		break;
9213949bee8SArchie Cobbs 	case PROT_IP:
9223949bee8SArchie Cobbs 		if (priv->conf.enableIP)
9233949bee8SArchie Cobbs 			outHook = priv->hooks[HOOK_INDEX_INET];
9243949bee8SArchie Cobbs 		break;
925a9b3dca5SArchie Cobbs 	case PROT_IPV6:
926a9b3dca5SArchie Cobbs 		if (priv->conf.enableIPv6)
927a9b3dca5SArchie Cobbs 			outHook = priv->hooks[HOOK_INDEX_IPV6];
928a9b3dca5SArchie Cobbs 		break;
9293949bee8SArchie Cobbs 	}
9303949bee8SArchie Cobbs 
931e149c4e2SArchie Cobbs bypass:
9324c641908SArchie Cobbs 	/* For unknown/inactive protocols, forward out the bypass hook */
9333949bee8SArchie Cobbs 	if (outHook == NULL) {
934d690a6e7SArchie Cobbs 		u_int16_t hdr[2];
935d690a6e7SArchie Cobbs 
936d690a6e7SArchie Cobbs 		hdr[0] = htons(linkNum);
937d690a6e7SArchie Cobbs 		hdr[1] = htons((u_int16_t)proto);
9384c641908SArchie Cobbs 		if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) {
939069154d5SJulian Elischer 			NG_FREE_ITEM(item);
9403949bee8SArchie Cobbs 			return (ENOBUFS);
9414c641908SArchie Cobbs 		}
9423949bee8SArchie Cobbs 		outHook = priv->hooks[HOOK_INDEX_BYPASS];
9433949bee8SArchie Cobbs 	}
9443949bee8SArchie Cobbs 
9453949bee8SArchie Cobbs 	/* Forward frame */
946069154d5SJulian Elischer 	NG_FWD_NEW_DATA(error, item, outHook, m);
9473949bee8SArchie Cobbs 	return (error);
9484cf49a43SJulian Elischer }
9494cf49a43SJulian Elischer 
9504cf49a43SJulian Elischer /*
951931c828aSArchie Cobbs  * Deliver a frame out a link, either a real one or NG_PPP_BUNDLE_LINKNUM.
9520e11d0f3SArchie Cobbs  * If the link is not enabled then ENXIO is returned, unless "bypass" is != 0.
953931c828aSArchie Cobbs  *
954931c828aSArchie Cobbs  * If the frame is too big for the particular link, return EMSGSIZE.
9554cf49a43SJulian Elischer  */
9563949bee8SArchie Cobbs static int
957d690a6e7SArchie Cobbs ng_ppp_output(node_p node, int bypass,
958069154d5SJulian Elischer 	int proto, int linkNum, item_p item)
9594cf49a43SJulian Elischer {
96030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
961a9b3dca5SArchie Cobbs 	struct ng_ppp_link *link;
962e149c4e2SArchie Cobbs 	int len, error;
963069154d5SJulian Elischer 	struct mbuf *m;
964931c828aSArchie Cobbs 	u_int16_t mru;
9654cf49a43SJulian Elischer 
966931c828aSArchie Cobbs 	/* Extract mbuf */
967931c828aSArchie Cobbs 	NGI_GET_M(item, m);
968931c828aSArchie Cobbs 
969d690a6e7SArchie Cobbs 	/* If not doing MP, map bundle virtual link to (the only) link */
970d690a6e7SArchie Cobbs 	if (linkNum == NG_PPP_BUNDLE_LINKNUM && !priv->conf.enableMultilink)
9713949bee8SArchie Cobbs 		linkNum = priv->activeLinks[0];
9723949bee8SArchie Cobbs 
973a9b3dca5SArchie Cobbs 	/* Get link pointer (optimization) */
974a9b3dca5SArchie Cobbs 	link = (linkNum != NG_PPP_BUNDLE_LINKNUM) ?
975a9b3dca5SArchie Cobbs 	    &priv->links[linkNum] : NULL;
976a9b3dca5SArchie Cobbs 
977d690a6e7SArchie Cobbs 	/* Check link status (if real) */
9780e11d0f3SArchie Cobbs 	if (linkNum != NG_PPP_BUNDLE_LINKNUM) {
979a9b3dca5SArchie Cobbs 		if (!bypass && !link->conf.enableLink) {
980069154d5SJulian Elischer 			NG_FREE_M(m);
981069154d5SJulian Elischer 			NG_FREE_ITEM(item);
9823949bee8SArchie Cobbs 			return (ENXIO);
9834c641908SArchie Cobbs 		}
984a9b3dca5SArchie Cobbs 		if (link->hook == NULL) {
985069154d5SJulian Elischer 			NG_FREE_M(m);
986069154d5SJulian Elischer 			NG_FREE_ITEM(item);
9873949bee8SArchie Cobbs 			return (ENETDOWN);
9883949bee8SArchie Cobbs 		}
9890e11d0f3SArchie Cobbs 	}
9903949bee8SArchie Cobbs 
991931c828aSArchie Cobbs 	/* Check peer's MRU for this link */
992931c828aSArchie Cobbs 	mru = (link != NULL) ? link->conf.mru : priv->conf.mrru;
993931c828aSArchie Cobbs 	if (mru != 0 && m->m_pkthdr.len > mru) {
994931c828aSArchie Cobbs 		NG_FREE_M(m);
995931c828aSArchie Cobbs 		NG_FREE_ITEM(item);
996931c828aSArchie Cobbs 		return (EMSGSIZE);
997931c828aSArchie Cobbs 	}
998931c828aSArchie Cobbs 
999d690a6e7SArchie Cobbs 	/* Prepend protocol number, possibly compressed */
1000d690a6e7SArchie Cobbs 	if ((m = ng_ppp_addproto(m, proto,
1001d690a6e7SArchie Cobbs 	    linkNum == NG_PPP_BUNDLE_LINKNUM
1002a9b3dca5SArchie Cobbs 	      || link->conf.enableProtoComp)) == NULL) {
1003069154d5SJulian Elischer 		NG_FREE_ITEM(item);
1004d690a6e7SArchie Cobbs 		return (ENOBUFS);
1005d690a6e7SArchie Cobbs 	}
1006d690a6e7SArchie Cobbs 
1007d690a6e7SArchie Cobbs 	/* Special handling for the MP virtual link */
1008069154d5SJulian Elischer 	if (linkNum == NG_PPP_BUNDLE_LINKNUM) {
1009069154d5SJulian Elischer 		meta_p meta;
1010069154d5SJulian Elischer 
1011069154d5SJulian Elischer 		/* strip off and discard the queue item */
1012069154d5SJulian Elischer 		NGI_GET_META(item, meta);
1013069154d5SJulian Elischer 		NG_FREE_ITEM(item);
1014d690a6e7SArchie Cobbs 		return ng_ppp_mp_output(node, m, meta);
1015069154d5SJulian Elischer 	}
1016d690a6e7SArchie Cobbs 
1017d690a6e7SArchie Cobbs 	/* Prepend address and control field (unless compressed) */
1018a9b3dca5SArchie Cobbs 	if (proto == PROT_LCP || !link->conf.enableACFComp) {
1019d690a6e7SArchie Cobbs 		if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) {
1020069154d5SJulian Elischer 			NG_FREE_ITEM(item);
1021d690a6e7SArchie Cobbs 			return (ENOBUFS);
1022d690a6e7SArchie Cobbs 		}
1023d690a6e7SArchie Cobbs 	}
1024d690a6e7SArchie Cobbs 
10253949bee8SArchie Cobbs 	/* Deliver frame */
1026e149c4e2SArchie Cobbs 	len = m->m_pkthdr.len;
1027069154d5SJulian Elischer 	NG_FWD_NEW_DATA(error, item,  link->hook, m);
1028fb1fc8abSArchie Cobbs 
1029fb1fc8abSArchie Cobbs 	/* Update stats and 'bytes in queue' counter */
1030fb1fc8abSArchie Cobbs 	if (error == 0) {
1031a9b3dca5SArchie Cobbs 		link->stats.xmitFrames++;
1032a9b3dca5SArchie Cobbs 		link->stats.xmitOctets += len;
1033a9b3dca5SArchie Cobbs 		link->bytesInQueue += len;
1034a9b3dca5SArchie Cobbs 		getmicrouptime(&link->lastWrite);
1035fb1fc8abSArchie Cobbs 	}
10363949bee8SArchie Cobbs 	return error;
10373949bee8SArchie Cobbs }
10383949bee8SArchie Cobbs 
10393949bee8SArchie Cobbs /*
10403949bee8SArchie Cobbs  * Handle an incoming multi-link fragment
1041a9b3dca5SArchie Cobbs  *
1042a9b3dca5SArchie Cobbs  * The fragment reassembly algorithm is somewhat complex. This is mainly
1043a9b3dca5SArchie Cobbs  * because we are required not to reorder the reconstructed packets, yet
1044a9b3dca5SArchie Cobbs  * fragments are only guaranteed to arrive in order on a per-link basis.
1045a9b3dca5SArchie Cobbs  * In other words, when we have a complete packet ready, but the previous
1046a9b3dca5SArchie Cobbs  * packet is still incomplete, we have to decide between delivering the
1047a9b3dca5SArchie Cobbs  * complete packet and throwing away the incomplete one, or waiting to
1048a9b3dca5SArchie Cobbs  * see if the remainder of the incomplete one arrives, at which time we
1049a9b3dca5SArchie Cobbs  * can deliver both packets, in order.
1050a9b3dca5SArchie Cobbs  *
1051a9b3dca5SArchie Cobbs  * This problem is exacerbated by "sequence number slew", which is when
1052a9b3dca5SArchie Cobbs  * the sequence numbers coming in from different links are far apart from
1053a9b3dca5SArchie Cobbs  * each other. In particular, certain unnamed equipment (*cough* Ascend)
1054a9b3dca5SArchie Cobbs  * has been seen to generate sequence number slew of up to 10 on an ISDN
1055a9b3dca5SArchie Cobbs  * 2B-channel MP link. There is nothing invalid about sequence number slew
1056a9b3dca5SArchie Cobbs  * but it makes the reasssembly process have to work harder.
1057a9b3dca5SArchie Cobbs  *
1058a9b3dca5SArchie Cobbs  * However, the peer is required to transmit fragments in order on each
1059a9b3dca5SArchie Cobbs  * link. That means if we define MSEQ as the minimum over all links of
1060a9b3dca5SArchie Cobbs  * the highest sequence number received on that link, then we can always
1061a9b3dca5SArchie Cobbs  * give up any hope of receiving a fragment with sequence number < MSEQ in
1062a9b3dca5SArchie Cobbs  * the future (all of this using 'wraparound' sequence number space).
1063a9b3dca5SArchie Cobbs  * Therefore we can always immediately throw away incomplete packets
1064a9b3dca5SArchie Cobbs  * missing fragments with sequence numbers < MSEQ.
1065a9b3dca5SArchie Cobbs  *
1066a9b3dca5SArchie Cobbs  * Here is an overview of our algorithm:
1067a9b3dca5SArchie Cobbs  *
1068a9b3dca5SArchie Cobbs  *    o Received fragments are inserted into a queue, for which we
1069a9b3dca5SArchie Cobbs  *	maintain these invariants between calls to this function:
1070a9b3dca5SArchie Cobbs  *
1071a9b3dca5SArchie Cobbs  *	- Fragments are ordered in the queue by sequence number
1072a9b3dca5SArchie Cobbs  *	- If a complete packet is at the head of the queue, then
1073a9b3dca5SArchie Cobbs  *	  the first fragment in the packet has seq# > MSEQ + 1
1074a9b3dca5SArchie Cobbs  *	  (otherwise, we could deliver it immediately)
1075a9b3dca5SArchie Cobbs  *	- If any fragments have seq# < MSEQ, then they are necessarily
1076a9b3dca5SArchie Cobbs  *	  part of a packet whose missing seq#'s are all > MSEQ (otherwise,
1077a9b3dca5SArchie Cobbs  *	  we can throw them away because they'll never be completed)
1078a9b3dca5SArchie Cobbs  *	- The queue contains at most MP_MAX_QUEUE_LEN fragments
1079a9b3dca5SArchie Cobbs  *
1080a9b3dca5SArchie Cobbs  *    o We have a periodic timer that checks the queue for the first
1081a9b3dca5SArchie Cobbs  *	complete packet that has been sitting in the queue "too long".
1082a9b3dca5SArchie Cobbs  *	When one is detected, all previous (incomplete) fragments are
1083a9b3dca5SArchie Cobbs  *	discarded, their missing fragments are declared lost and MSEQ
1084a9b3dca5SArchie Cobbs  *	is increased.
1085a9b3dca5SArchie Cobbs  *
1086a9b3dca5SArchie Cobbs  *    o If we recieve a fragment with seq# < MSEQ, we throw it away
1087a9b3dca5SArchie Cobbs  *	because we've already delcared it lost.
1088a9b3dca5SArchie Cobbs  *
1089a9b3dca5SArchie Cobbs  * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM.
10903949bee8SArchie Cobbs  */
10913949bee8SArchie Cobbs static int
1092069154d5SJulian Elischer ng_ppp_mp_input(node_p node, int linkNum, item_p item)
10933949bee8SArchie Cobbs {
109430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1095a9b3dca5SArchie Cobbs 	struct ng_ppp_link *const link = &priv->links[linkNum];
10963949bee8SArchie Cobbs 	struct ng_ppp_frag frag0, *frag = &frag0;
1097a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent;
1098a9b3dca5SArchie Cobbs 	int i, diff, inserted;
1099069154d5SJulian Elischer 	struct mbuf *m;
1100069154d5SJulian Elischer 	meta_p meta;
11013949bee8SArchie Cobbs 
1102069154d5SJulian Elischer 	NGI_GET_M(item, m);
1103069154d5SJulian Elischer 	NGI_GET_META(item, meta);
1104069154d5SJulian Elischer 	NG_FREE_ITEM(item);
1105b4c44c30SArchie Cobbs 	/* Stats */
1106b4c44c30SArchie Cobbs 	priv->bundleStats.recvFrames++;
1107b4c44c30SArchie Cobbs 	priv->bundleStats.recvOctets += m->m_pkthdr.len;
1108b4c44c30SArchie Cobbs 
11093949bee8SArchie Cobbs 	/* Extract fragment information from MP header */
11103949bee8SArchie Cobbs 	if (priv->conf.recvShortSeq) {
11113949bee8SArchie Cobbs 		u_int16_t shdr;
11123949bee8SArchie Cobbs 
11133949bee8SArchie Cobbs 		if (m->m_pkthdr.len < 2) {
1114a9b3dca5SArchie Cobbs 			link->stats.runts++;
1115069154d5SJulian Elischer 			NG_FREE_M(m);
1116069154d5SJulian Elischer 			NG_FREE_META(meta);
11173949bee8SArchie Cobbs 			return (EINVAL);
11183949bee8SArchie Cobbs 		}
11193949bee8SArchie Cobbs 		if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
11203949bee8SArchie Cobbs 			NG_FREE_META(meta);
11213949bee8SArchie Cobbs 			return (ENOBUFS);
11223949bee8SArchie Cobbs 		}
11233949bee8SArchie Cobbs 		shdr = ntohs(*mtod(m, u_int16_t *));
11246f16db81SArchie Cobbs 		frag->seq = MP_SHORT_EXTEND(shdr);
11253949bee8SArchie Cobbs 		frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0;
11263949bee8SArchie Cobbs 		frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0;
1127a9b3dca5SArchie Cobbs 		diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq);
11283949bee8SArchie Cobbs 		m_adj(m, 2);
11293949bee8SArchie Cobbs 	} else {
11303949bee8SArchie Cobbs 		u_int32_t lhdr;
11313949bee8SArchie Cobbs 
11323949bee8SArchie Cobbs 		if (m->m_pkthdr.len < 4) {
1133a9b3dca5SArchie Cobbs 			link->stats.runts++;
1134069154d5SJulian Elischer 			NG_FREE_M(m);
1135069154d5SJulian Elischer 			NG_FREE_META(meta);
11363949bee8SArchie Cobbs 			return (EINVAL);
11373949bee8SArchie Cobbs 		}
11383949bee8SArchie Cobbs 		if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
11393949bee8SArchie Cobbs 			NG_FREE_META(meta);
11403949bee8SArchie Cobbs 			return (ENOBUFS);
11413949bee8SArchie Cobbs 		}
11423949bee8SArchie Cobbs 		lhdr = ntohl(*mtod(m, u_int32_t *));
11436f16db81SArchie Cobbs 		frag->seq = MP_LONG_EXTEND(lhdr);
11443949bee8SArchie Cobbs 		frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0;
11453949bee8SArchie Cobbs 		frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0;
1146a9b3dca5SArchie Cobbs 		diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq);
11473949bee8SArchie Cobbs 		m_adj(m, 4);
11483949bee8SArchie Cobbs 	}
11493949bee8SArchie Cobbs 	frag->data = m;
11503949bee8SArchie Cobbs 	frag->meta = meta;
1151a9b3dca5SArchie Cobbs 	getmicrouptime(&frag->timestamp);
11523949bee8SArchie Cobbs 
1153a9b3dca5SArchie Cobbs 	/* If sequence number is < MSEQ, we've already declared this
1154a9b3dca5SArchie Cobbs 	   fragment as lost, so we have no choice now but to drop it */
1155a9b3dca5SArchie Cobbs 	if (diff < 0) {
1156a9b3dca5SArchie Cobbs 		link->stats.dropFragments++;
1157069154d5SJulian Elischer 		NG_FREE_M(m);
1158069154d5SJulian Elischer 		NG_FREE_META(meta);
1159a9b3dca5SArchie Cobbs 		return (0);
1160a9b3dca5SArchie Cobbs 	}
11613949bee8SArchie Cobbs 
1162a9b3dca5SArchie Cobbs 	/* Update highest received sequence number on this link and MSEQ */
1163a9b3dca5SArchie Cobbs 	priv->mseq = link->seq = frag->seq;
1164a9b3dca5SArchie Cobbs 	for (i = 0; i < priv->numActiveLinks; i++) {
1165a9b3dca5SArchie Cobbs 		struct ng_ppp_link *const alink =
1166a9b3dca5SArchie Cobbs 		    &priv->links[priv->activeLinks[i]];
1167a9b3dca5SArchie Cobbs 
11686f16db81SArchie Cobbs 		if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0)
1169a9b3dca5SArchie Cobbs 			priv->mseq = alink->seq;
1170a9b3dca5SArchie Cobbs 	}
11713949bee8SArchie Cobbs 
11723949bee8SArchie Cobbs 	/* Allocate a new frag struct for the queue */
11739c8c302fSJulian Elischer 	MALLOC(frag, struct ng_ppp_frag *, sizeof(*frag), M_NETGRAPH_PPP, M_NOWAIT);
11743949bee8SArchie Cobbs 	if (frag == NULL) {
1175069154d5SJulian Elischer 		NG_FREE_M(m);
1176069154d5SJulian Elischer 		NG_FREE_META(meta);
1177a9b3dca5SArchie Cobbs 		ng_ppp_frag_process(node);
11783949bee8SArchie Cobbs 		return (ENOMEM);
11793949bee8SArchie Cobbs 	}
11803949bee8SArchie Cobbs 	*frag = frag0;
11813949bee8SArchie Cobbs 
1182a9b3dca5SArchie Cobbs 	/* Add fragment to queue, which is sorted by sequence number */
11831e7a9f72SArchie Cobbs 	inserted = 0;
11845f90cac7SKirk McKusick 	TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) {
11856f16db81SArchie Cobbs 		diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq);
11863949bee8SArchie Cobbs 		if (diff > 0) {
11875f90cac7SKirk McKusick 			TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent);
11881e7a9f72SArchie Cobbs 			inserted = 1;
11893949bee8SArchie Cobbs 			break;
11903949bee8SArchie Cobbs 		} else if (diff == 0) {	     /* should never happen! */
1191a9b3dca5SArchie Cobbs 			link->stats.dupFragments++;
1192069154d5SJulian Elischer 			NG_FREE_M(frag->data);
1193069154d5SJulian Elischer 			NG_FREE_META(frag->meta);
11949c8c302fSJulian Elischer 			FREE(frag, M_NETGRAPH_PPP);
11953949bee8SArchie Cobbs 			return (EINVAL);
11963949bee8SArchie Cobbs 		}
11973949bee8SArchie Cobbs 	}
11981e7a9f72SArchie Cobbs 	if (!inserted)
11995f90cac7SKirk McKusick 		TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent);
1200a9b3dca5SArchie Cobbs 	priv->qlen++;
12013949bee8SArchie Cobbs 
1202a9b3dca5SArchie Cobbs 	/* Process the queue */
1203a9b3dca5SArchie Cobbs 	return ng_ppp_frag_process(node);
12043949bee8SArchie Cobbs }
12051e7a9f72SArchie Cobbs 
1206a9b3dca5SArchie Cobbs /*
1207a9b3dca5SArchie Cobbs  * Examine our list of fragments, and determine if there is a
1208a9b3dca5SArchie Cobbs  * complete and deliverable packet at the head of the list.
1209a9b3dca5SArchie Cobbs  * Return 1 if so, zero otherwise.
1210a9b3dca5SArchie Cobbs  */
1211a9b3dca5SArchie Cobbs static int
1212a9b3dca5SArchie Cobbs ng_ppp_check_packet(node_p node)
1213a9b3dca5SArchie Cobbs {
121430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1215a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext;
12163949bee8SArchie Cobbs 
1217a9b3dca5SArchie Cobbs 	/* Check for empty queue */
12185f90cac7SKirk McKusick 	if (TAILQ_EMPTY(&priv->frags))
1219a9b3dca5SArchie Cobbs 		return (0);
1220a9b3dca5SArchie Cobbs 
1221a9b3dca5SArchie Cobbs 	/* Check first fragment is the start of a deliverable packet */
12225f90cac7SKirk McKusick 	qent = TAILQ_FIRST(&priv->frags);
12236f16db81SArchie Cobbs 	if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1)
1224a9b3dca5SArchie Cobbs 		return (0);
1225a9b3dca5SArchie Cobbs 
1226a9b3dca5SArchie Cobbs 	/* Check that all the fragments are there */
1227a9b3dca5SArchie Cobbs 	while (!qent->last) {
12285f90cac7SKirk McKusick 		qnext = TAILQ_NEXT(qent, f_qent);
12295f90cac7SKirk McKusick 		if (qnext == NULL)	/* end of queue */
1230a9b3dca5SArchie Cobbs 			return (0);
12316f16db81SArchie Cobbs 		if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq))
1232a9b3dca5SArchie Cobbs 			return (0);
1233a9b3dca5SArchie Cobbs 		qent = qnext;
1234a9b3dca5SArchie Cobbs 	}
1235a9b3dca5SArchie Cobbs 
1236a9b3dca5SArchie Cobbs 	/* Got one */
1237a9b3dca5SArchie Cobbs 	return (1);
1238a9b3dca5SArchie Cobbs }
1239a9b3dca5SArchie Cobbs 
1240a9b3dca5SArchie Cobbs /*
1241a9b3dca5SArchie Cobbs  * Pull a completed packet off the head of the incoming fragment queue.
1242a9b3dca5SArchie Cobbs  * This assumes there is a completed packet there to pull off.
1243a9b3dca5SArchie Cobbs  */
1244a9b3dca5SArchie Cobbs static void
1245a9b3dca5SArchie Cobbs ng_ppp_get_packet(node_p node, struct mbuf **mp, meta_p *metap)
1246a9b3dca5SArchie Cobbs {
124730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1248a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext;
1249a9b3dca5SArchie Cobbs 	struct mbuf *m = NULL, *tail;
1250a9b3dca5SArchie Cobbs 
12515f90cac7SKirk McKusick 	qent = TAILQ_FIRST(&priv->frags);
12525f90cac7SKirk McKusick 	KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first,
12536e551fb6SDavid E. O'Brien 	    ("%s: no packet", __func__));
1254a9b3dca5SArchie Cobbs 	for (tail = NULL; qent != NULL; qent = qnext) {
12555f90cac7SKirk McKusick 		qnext = TAILQ_NEXT(qent, f_qent);
12565f90cac7SKirk McKusick 		KASSERT(!TAILQ_EMPTY(&priv->frags),
12576e551fb6SDavid E. O'Brien 		    ("%s: empty q", __func__));
12585f90cac7SKirk McKusick 		TAILQ_REMOVE(&priv->frags, qent, f_qent);
12593949bee8SArchie Cobbs 		if (tail == NULL) {
12603949bee8SArchie Cobbs 			tail = m = qent->data;
1261a9b3dca5SArchie Cobbs 			*metap = qent->meta;	/* inherit first frag's meta */
12623949bee8SArchie Cobbs 		} else {
12633949bee8SArchie Cobbs 			m->m_pkthdr.len += qent->data->m_pkthdr.len;
12643949bee8SArchie Cobbs 			tail->m_next = qent->data;
1265d690a6e7SArchie Cobbs 			NG_FREE_META(qent->meta); /* drop other frags' metas */
12663949bee8SArchie Cobbs 		}
12673949bee8SArchie Cobbs 		while (tail->m_next != NULL)
12683949bee8SArchie Cobbs 			tail = tail->m_next;
1269a9b3dca5SArchie Cobbs 		if (qent->last)
12703949bee8SArchie Cobbs 			qnext = NULL;
12719c8c302fSJulian Elischer 		FREE(qent, M_NETGRAPH_PPP);
1272a9b3dca5SArchie Cobbs 		priv->qlen--;
1273a9b3dca5SArchie Cobbs 	}
1274a9b3dca5SArchie Cobbs 	*mp = m;
12753949bee8SArchie Cobbs }
12763949bee8SArchie Cobbs 
1277a9b3dca5SArchie Cobbs /*
1278a9b3dca5SArchie Cobbs  * Trim fragments from the queue whose packets can never be completed.
1279a9b3dca5SArchie Cobbs  * This assumes a complete packet is NOT at the beginning of the queue.
1280a9b3dca5SArchie Cobbs  * Returns 1 if fragments were removed, zero otherwise.
1281a9b3dca5SArchie Cobbs  */
1282a9b3dca5SArchie Cobbs static int
1283a9b3dca5SArchie Cobbs ng_ppp_frag_trim(node_p node)
1284a9b3dca5SArchie Cobbs {
128530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1286a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext = NULL;
1287a9b3dca5SArchie Cobbs 	int removed = 0;
1288a9b3dca5SArchie Cobbs 
1289a9b3dca5SArchie Cobbs 	/* Scan for "dead" fragments and remove them */
1290a9b3dca5SArchie Cobbs 	while (1) {
1291a9b3dca5SArchie Cobbs 		int dead = 0;
1292a9b3dca5SArchie Cobbs 
1293a9b3dca5SArchie Cobbs 		/* If queue is empty, we're done */
12945f90cac7SKirk McKusick 		if (TAILQ_EMPTY(&priv->frags))
12953949bee8SArchie Cobbs 			break;
1296a9b3dca5SArchie Cobbs 
1297a9b3dca5SArchie Cobbs 		/* Determine whether first fragment can ever be completed */
12985f90cac7SKirk McKusick 		TAILQ_FOREACH(qent, &priv->frags, f_qent) {
12996f16db81SArchie Cobbs 			if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0)
1300a9b3dca5SArchie Cobbs 				break;
13015f90cac7SKirk McKusick 			qnext = TAILQ_NEXT(qent, f_qent);
13025f90cac7SKirk McKusick 			KASSERT(qnext != NULL,
13036e551fb6SDavid E. O'Brien 			    ("%s: last frag < MSEQ?", __func__));
13046f16db81SArchie Cobbs 			if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)
1305a9b3dca5SArchie Cobbs 			    || qent->last || qnext->first) {
1306a9b3dca5SArchie Cobbs 				dead = 1;
1307a9b3dca5SArchie Cobbs 				break;
1308a9b3dca5SArchie Cobbs 			}
1309a9b3dca5SArchie Cobbs 		}
1310a9b3dca5SArchie Cobbs 		if (!dead)
1311a9b3dca5SArchie Cobbs 			break;
1312a9b3dca5SArchie Cobbs 
1313a9b3dca5SArchie Cobbs 		/* Remove fragment and all others in the same packet */
13145f90cac7SKirk McKusick 		while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) {
13155f90cac7SKirk McKusick 			KASSERT(!TAILQ_EMPTY(&priv->frags),
13166e551fb6SDavid E. O'Brien 			    ("%s: empty q", __func__));
1317a9b3dca5SArchie Cobbs 			priv->bundleStats.dropFragments++;
13185f90cac7SKirk McKusick 			TAILQ_REMOVE(&priv->frags, qent, f_qent);
1319069154d5SJulian Elischer 			NG_FREE_M(qent->data);
1320069154d5SJulian Elischer 			NG_FREE_META(qent->meta);
13219c8c302fSJulian Elischer 			FREE(qent, M_NETGRAPH_PPP);
1322a9b3dca5SArchie Cobbs 			priv->qlen--;
1323a9b3dca5SArchie Cobbs 			removed = 1;
1324a9b3dca5SArchie Cobbs 		}
1325a9b3dca5SArchie Cobbs 	}
1326a9b3dca5SArchie Cobbs 	return (removed);
13273949bee8SArchie Cobbs }
13283949bee8SArchie Cobbs 
1329a9b3dca5SArchie Cobbs /*
1330a9b3dca5SArchie Cobbs  * Run the queue, restoring the queue invariants
1331a9b3dca5SArchie Cobbs  */
1332a9b3dca5SArchie Cobbs static int
1333a9b3dca5SArchie Cobbs ng_ppp_frag_process(node_p node)
1334a9b3dca5SArchie Cobbs {
133530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1336a9b3dca5SArchie Cobbs 	struct mbuf *m;
1337a9b3dca5SArchie Cobbs 	meta_p meta;
1338069154d5SJulian Elischer 	item_p item;
1339a9b3dca5SArchie Cobbs 
1340a9b3dca5SArchie Cobbs 	/* Deliver any deliverable packets */
1341a9b3dca5SArchie Cobbs 	while (ng_ppp_check_packet(node)) {
1342a9b3dca5SArchie Cobbs 		ng_ppp_get_packet(node, &m, &meta);
1343069154d5SJulian Elischer 		item = ng_package_data(m, meta);
1344069154d5SJulian Elischer 		ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1345a9b3dca5SArchie Cobbs 	}
1346a9b3dca5SArchie Cobbs 
1347a9b3dca5SArchie Cobbs 	/* Delete dead fragments and try again */
1348a9b3dca5SArchie Cobbs 	if (ng_ppp_frag_trim(node)) {
1349a9b3dca5SArchie Cobbs 		while (ng_ppp_check_packet(node)) {
1350a9b3dca5SArchie Cobbs 			ng_ppp_get_packet(node, &m, &meta);
1351069154d5SJulian Elischer 			item = ng_package_data(m, meta);
1352069154d5SJulian Elischer 			ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1353a9b3dca5SArchie Cobbs 		}
1354a9b3dca5SArchie Cobbs 	}
1355a9b3dca5SArchie Cobbs 
1356a9b3dca5SArchie Cobbs 	/* Check for stale fragments while we're here */
1357a9b3dca5SArchie Cobbs 	ng_ppp_frag_checkstale(node);
1358a9b3dca5SArchie Cobbs 
1359a9b3dca5SArchie Cobbs 	/* Check queue length */
1360a9b3dca5SArchie Cobbs 	if (priv->qlen > MP_MAX_QUEUE_LEN) {
1361a9b3dca5SArchie Cobbs 		struct ng_ppp_frag *qent;
1362a9b3dca5SArchie Cobbs 		int i;
1363a9b3dca5SArchie Cobbs 
1364a9b3dca5SArchie Cobbs 		/* Get oldest fragment */
13655f90cac7SKirk McKusick 		KASSERT(!TAILQ_EMPTY(&priv->frags),
13666e551fb6SDavid E. O'Brien 		    ("%s: empty q", __func__));
13675f90cac7SKirk McKusick 		qent = TAILQ_FIRST(&priv->frags);
1368a9b3dca5SArchie Cobbs 
1369a9b3dca5SArchie Cobbs 		/* Bump MSEQ if necessary */
13706f16db81SArchie Cobbs 		if (MP_RECV_SEQ_DIFF(priv, priv->mseq, qent->seq) < 0) {
1371a9b3dca5SArchie Cobbs 			priv->mseq = qent->seq;
1372a9b3dca5SArchie Cobbs 			for (i = 0; i < priv->numActiveLinks; i++) {
1373a9b3dca5SArchie Cobbs 				struct ng_ppp_link *const alink =
1374a9b3dca5SArchie Cobbs 				    &priv->links[priv->activeLinks[i]];
1375a9b3dca5SArchie Cobbs 
13766f16db81SArchie Cobbs 				if (MP_RECV_SEQ_DIFF(priv,
1377a9b3dca5SArchie Cobbs 				    alink->seq, priv->mseq) < 0)
1378a9b3dca5SArchie Cobbs 					alink->seq = priv->mseq;
1379a9b3dca5SArchie Cobbs 			}
1380a9b3dca5SArchie Cobbs 		}
1381a9b3dca5SArchie Cobbs 
1382a9b3dca5SArchie Cobbs 		/* Drop it */
1383a9b3dca5SArchie Cobbs 		priv->bundleStats.dropFragments++;
13845f90cac7SKirk McKusick 		TAILQ_REMOVE(&priv->frags, qent, f_qent);
1385069154d5SJulian Elischer 		NG_FREE_M(qent->data);
1386069154d5SJulian Elischer 		NG_FREE_META(qent->meta);
13879c8c302fSJulian Elischer 		FREE(qent, M_NETGRAPH_PPP);
1388a9b3dca5SArchie Cobbs 		priv->qlen--;
1389a9b3dca5SArchie Cobbs 
1390a9b3dca5SArchie Cobbs 		/* Process queue again */
1391a9b3dca5SArchie Cobbs 		return ng_ppp_frag_process(node);
1392a9b3dca5SArchie Cobbs 	}
1393a9b3dca5SArchie Cobbs 
1394a9b3dca5SArchie Cobbs 	/* Done */
1395a9b3dca5SArchie Cobbs 	return (0);
1396a9b3dca5SArchie Cobbs }
1397a9b3dca5SArchie Cobbs 
1398a9b3dca5SArchie Cobbs /*
1399a9b3dca5SArchie Cobbs  * Check for 'stale' completed packets that need to be delivered
1400a9b3dca5SArchie Cobbs  *
1401a9b3dca5SArchie Cobbs  * If a link goes down or has a temporary failure, MSEQ can get
1402a9b3dca5SArchie Cobbs  * "stuck", because no new incoming fragments appear on that link.
1403a9b3dca5SArchie Cobbs  * This can cause completed packets to never get delivered if
1404a9b3dca5SArchie Cobbs  * their sequence numbers are all > MSEQ + 1.
1405a9b3dca5SArchie Cobbs  *
1406a9b3dca5SArchie Cobbs  * This routine checks how long all of the completed packets have
1407a9b3dca5SArchie Cobbs  * been sitting in the queue, and if too long, removes fragments
1408a9b3dca5SArchie Cobbs  * from the queue and increments MSEQ to allow them to be delivered.
1409a9b3dca5SArchie Cobbs  */
1410a9b3dca5SArchie Cobbs static void
1411a9b3dca5SArchie Cobbs ng_ppp_frag_checkstale(node_p node)
1412a9b3dca5SArchie Cobbs {
141330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1414a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *beg, *end;
1415a9b3dca5SArchie Cobbs 	struct timeval now, age;
1416a9b3dca5SArchie Cobbs 	struct mbuf *m;
1417a9b3dca5SArchie Cobbs 	meta_p meta;
1418a9b3dca5SArchie Cobbs 	int i, seq;
1419069154d5SJulian Elischer 	item_p item;
14202b2a8188SArchie Cobbs 	int endseq;
1421a9b3dca5SArchie Cobbs 
1422a9b3dca5SArchie Cobbs 	now.tv_sec = 0;			/* uninitialized state */
1423a9b3dca5SArchie Cobbs 	while (1) {
1424a9b3dca5SArchie Cobbs 
1425a9b3dca5SArchie Cobbs 		/* If queue is empty, we're done */
14265f90cac7SKirk McKusick 		if (TAILQ_EMPTY(&priv->frags))
1427a9b3dca5SArchie Cobbs 			break;
1428a9b3dca5SArchie Cobbs 
1429a9b3dca5SArchie Cobbs 		/* Find the first complete packet in the queue */
1430a9b3dca5SArchie Cobbs 		beg = end = NULL;
14315f90cac7SKirk McKusick 		seq = TAILQ_FIRST(&priv->frags)->seq;
14325f90cac7SKirk McKusick 		TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1433a9b3dca5SArchie Cobbs 			if (qent->first)
1434a9b3dca5SArchie Cobbs 				beg = qent;
1435a9b3dca5SArchie Cobbs 			else if (qent->seq != seq)
1436a9b3dca5SArchie Cobbs 				beg = NULL;
1437a9b3dca5SArchie Cobbs 			if (beg != NULL && qent->last) {
1438a9b3dca5SArchie Cobbs 				end = qent;
1439a9b3dca5SArchie Cobbs 				break;
1440a9b3dca5SArchie Cobbs 			}
14416f16db81SArchie Cobbs 			seq = MP_NEXT_RECV_SEQ(priv, seq);
1442a9b3dca5SArchie Cobbs 		}
1443a9b3dca5SArchie Cobbs 
1444a9b3dca5SArchie Cobbs 		/* If none found, exit */
1445a9b3dca5SArchie Cobbs 		if (end == NULL)
1446a9b3dca5SArchie Cobbs 			break;
1447a9b3dca5SArchie Cobbs 
1448a9b3dca5SArchie Cobbs 		/* Get current time (we assume we've been up for >= 1 second) */
1449a9b3dca5SArchie Cobbs 		if (now.tv_sec == 0)
1450a9b3dca5SArchie Cobbs 			getmicrouptime(&now);
1451a9b3dca5SArchie Cobbs 
1452a9b3dca5SArchie Cobbs 		/* Check if packet has been queued too long */
1453a9b3dca5SArchie Cobbs 		age = now;
1454a9b3dca5SArchie Cobbs 		timevalsub(&age, &beg->timestamp);
1455a9b3dca5SArchie Cobbs 		if (timevalcmp(&age, &ng_ppp_max_staleness, < ))
1456a9b3dca5SArchie Cobbs 			break;
1457a9b3dca5SArchie Cobbs 
1458a9b3dca5SArchie Cobbs 		/* Throw away junk fragments in front of the completed packet */
14595f90cac7SKirk McKusick 		while ((qent = TAILQ_FIRST(&priv->frags)) != beg) {
14605f90cac7SKirk McKusick 			KASSERT(!TAILQ_EMPTY(&priv->frags),
14616e551fb6SDavid E. O'Brien 			    ("%s: empty q", __func__));
1462a9b3dca5SArchie Cobbs 			priv->bundleStats.dropFragments++;
14635f90cac7SKirk McKusick 			TAILQ_REMOVE(&priv->frags, qent, f_qent);
1464069154d5SJulian Elischer 			NG_FREE_M(qent->data);
1465069154d5SJulian Elischer 			NG_FREE_META(qent->meta);
14669c8c302fSJulian Elischer 			FREE(qent, M_NETGRAPH_PPP);
1467a9b3dca5SArchie Cobbs 			priv->qlen--;
1468a9b3dca5SArchie Cobbs 		}
1469a9b3dca5SArchie Cobbs 
1470a9b3dca5SArchie Cobbs 		/* Extract completed packet */
14712b2a8188SArchie Cobbs 		endseq = end->seq;
1472a9b3dca5SArchie Cobbs 		ng_ppp_get_packet(node, &m, &meta);
1473a9b3dca5SArchie Cobbs 
1474a9b3dca5SArchie Cobbs 		/* Bump MSEQ if necessary */
14752b2a8188SArchie Cobbs 		if (MP_RECV_SEQ_DIFF(priv, priv->mseq, endseq) < 0) {
14762b2a8188SArchie Cobbs 			priv->mseq = endseq;
1477a9b3dca5SArchie Cobbs 			for (i = 0; i < priv->numActiveLinks; i++) {
1478a9b3dca5SArchie Cobbs 				struct ng_ppp_link *const alink =
1479a9b3dca5SArchie Cobbs 				    &priv->links[priv->activeLinks[i]];
1480a9b3dca5SArchie Cobbs 
14816f16db81SArchie Cobbs 				if (MP_RECV_SEQ_DIFF(priv,
1482a9b3dca5SArchie Cobbs 				    alink->seq, priv->mseq) < 0)
1483a9b3dca5SArchie Cobbs 					alink->seq = priv->mseq;
1484a9b3dca5SArchie Cobbs 			}
1485a9b3dca5SArchie Cobbs 		}
1486a9b3dca5SArchie Cobbs 
1487a9b3dca5SArchie Cobbs 		/* Deliver packet */
1488069154d5SJulian Elischer 		item = ng_package_data(m, meta);
1489069154d5SJulian Elischer 		ng_ppp_input(node, 0, NG_PPP_BUNDLE_LINKNUM, item);
1490a9b3dca5SArchie Cobbs 	}
1491a9b3dca5SArchie Cobbs }
1492a9b3dca5SArchie Cobbs 
1493a9b3dca5SArchie Cobbs /*
1494a9b3dca5SArchie Cobbs  * Periodically call ng_ppp_frag_checkstale()
1495a9b3dca5SArchie Cobbs  */
1496a9b3dca5SArchie Cobbs static void
1497a9b3dca5SArchie Cobbs ng_ppp_frag_timeout(void *arg)
1498a9b3dca5SArchie Cobbs {
1499a9b3dca5SArchie Cobbs 	const node_p node = arg;
150030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1501a9b3dca5SArchie Cobbs 	int s = splnet();
1502a9b3dca5SArchie Cobbs 
1503a9b3dca5SArchie Cobbs 	/* Handle the race where shutdown happens just before splnet() above */
150430400f03SJulian Elischer 	if (NG_NODE_NOT_VALID(node)) {
150530400f03SJulian Elischer 		NG_NODE_UNREF(node);
1506a9b3dca5SArchie Cobbs 		splx(s);
1507a9b3dca5SArchie Cobbs 		return;
1508a9b3dca5SArchie Cobbs 	}
1509a9b3dca5SArchie Cobbs 
1510a9b3dca5SArchie Cobbs 	/* Reset timer state after timeout */
15116e551fb6SDavid E. O'Brien 	KASSERT(priv->timerActive, ("%s: !timerActive", __func__));
1512a9b3dca5SArchie Cobbs 	priv->timerActive = 0;
15136e551fb6SDavid E. O'Brien 	KASSERT(node->nd_refs > 1, ("%s: nd_refs=%d", __func__, node->nd_refs));
151430400f03SJulian Elischer 	NG_NODE_UNREF(node);
1515a9b3dca5SArchie Cobbs 
1516a9b3dca5SArchie Cobbs 	/* Start timer again */
1517a9b3dca5SArchie Cobbs 	ng_ppp_start_frag_timer(node);
1518a9b3dca5SArchie Cobbs 
1519a9b3dca5SArchie Cobbs 	/* Scan the fragment queue */
1520a9b3dca5SArchie Cobbs 	ng_ppp_frag_checkstale(node);
1521a9b3dca5SArchie Cobbs 	splx(s);
15223949bee8SArchie Cobbs }
15233949bee8SArchie Cobbs 
15243949bee8SArchie Cobbs /*
15253949bee8SArchie Cobbs  * Deliver a frame out on the bundle, i.e., figure out how to fragment
15263949bee8SArchie Cobbs  * the frame across the individual PPP links and do so.
15273949bee8SArchie Cobbs  */
15283949bee8SArchie Cobbs static int
15293949bee8SArchie Cobbs ng_ppp_mp_output(node_p node, struct mbuf *m, meta_p meta)
15303949bee8SArchie Cobbs {
153130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1532931c828aSArchie Cobbs 	const int hdr_len = priv->conf.xmitShortSeq ? 2 : 4;
15333949bee8SArchie Cobbs 	int distrib[NG_PPP_MAX_LINKS];
15343949bee8SArchie Cobbs 	int firstFragment;
15353949bee8SArchie Cobbs 	int activeLinkNum;
1536069154d5SJulian Elischer 	item_p item;
15373949bee8SArchie Cobbs 
15383949bee8SArchie Cobbs 	/* At least one link must be active */
15393949bee8SArchie Cobbs 	if (priv->numActiveLinks == 0) {
1540069154d5SJulian Elischer 		NG_FREE_M(m);
1541069154d5SJulian Elischer 		NG_FREE_META(meta);
15423949bee8SArchie Cobbs 		return (ENETDOWN);
15433949bee8SArchie Cobbs 	}
15443949bee8SArchie Cobbs 
15453949bee8SArchie Cobbs 	/* Round-robin strategy */
15463949bee8SArchie Cobbs 	if (priv->conf.enableRoundRobin || m->m_pkthdr.len < MP_MIN_FRAG_LEN) {
15473949bee8SArchie Cobbs 		activeLinkNum = priv->lastLink++ % priv->numActiveLinks;
15483949bee8SArchie Cobbs 		bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0]));
15493949bee8SArchie Cobbs 		distrib[activeLinkNum] = m->m_pkthdr.len;
15503949bee8SArchie Cobbs 		goto deliver;
15513949bee8SArchie Cobbs 	}
15523949bee8SArchie Cobbs 
15533949bee8SArchie Cobbs 	/* Strategy when all links are equivalent (optimize the common case) */
15543949bee8SArchie Cobbs 	if (priv->allLinksEqual) {
15553949bee8SArchie Cobbs 		const int fraction = m->m_pkthdr.len / priv->numActiveLinks;
15563949bee8SArchie Cobbs 		int i, remain;
15573949bee8SArchie Cobbs 
15583949bee8SArchie Cobbs 		for (i = 0; i < priv->numActiveLinks; i++)
15593949bee8SArchie Cobbs 			distrib[priv->lastLink++ % priv->numActiveLinks]
15603949bee8SArchie Cobbs 			    = fraction;
15613949bee8SArchie Cobbs 		remain = m->m_pkthdr.len - (fraction * priv->numActiveLinks);
15623949bee8SArchie Cobbs 		while (remain > 0) {
15633949bee8SArchie Cobbs 			distrib[priv->lastLink++ % priv->numActiveLinks]++;
15643949bee8SArchie Cobbs 			remain--;
15653949bee8SArchie Cobbs 		}
15663949bee8SArchie Cobbs 		goto deliver;
15673949bee8SArchie Cobbs 	}
15683949bee8SArchie Cobbs 
15693949bee8SArchie Cobbs 	/* Strategy when all links are not equivalent */
15703949bee8SArchie Cobbs 	ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib);
15713949bee8SArchie Cobbs 
15723949bee8SArchie Cobbs deliver:
15733949bee8SArchie Cobbs 	/* Update stats */
15743949bee8SArchie Cobbs 	priv->bundleStats.xmitFrames++;
15753949bee8SArchie Cobbs 	priv->bundleStats.xmitOctets += m->m_pkthdr.len;
15763949bee8SArchie Cobbs 
15773949bee8SArchie Cobbs 	/* Send alloted portions of frame out on the link(s) */
15783949bee8SArchie Cobbs 	for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1;
15793949bee8SArchie Cobbs 	    activeLinkNum >= 0; activeLinkNum--) {
15803949bee8SArchie Cobbs 		const int linkNum = priv->activeLinks[activeLinkNum];
1581a9b3dca5SArchie Cobbs 		struct ng_ppp_link *const link = &priv->links[linkNum];
15823949bee8SArchie Cobbs 
15833949bee8SArchie Cobbs 		/* Deliver fragment(s) out the next link */
15843949bee8SArchie Cobbs 		for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) {
15853949bee8SArchie Cobbs 			int len, lastFragment, error;
15863949bee8SArchie Cobbs 			struct mbuf *m2;
15873949bee8SArchie Cobbs 			meta_p meta2;
15883949bee8SArchie Cobbs 
15893949bee8SArchie Cobbs 			/* Calculate fragment length; don't exceed link MTU */
15903949bee8SArchie Cobbs 			len = distrib[activeLinkNum];
1591931c828aSArchie Cobbs 			if (len > link->conf.mru - hdr_len)
1592931c828aSArchie Cobbs 				len = link->conf.mru - hdr_len;
15933949bee8SArchie Cobbs 			distrib[activeLinkNum] -= len;
15943949bee8SArchie Cobbs 			lastFragment = (len == m->m_pkthdr.len);
15953949bee8SArchie Cobbs 
15963949bee8SArchie Cobbs 			/* Split off next fragment as "m2" */
15973949bee8SArchie Cobbs 			m2 = m;
15983949bee8SArchie Cobbs 			if (!lastFragment) {
1599a163d034SWarner Losh 				struct mbuf *n = m_split(m, len, M_DONTWAIT);
16003949bee8SArchie Cobbs 
16013949bee8SArchie Cobbs 				if (n == NULL) {
1602069154d5SJulian Elischer 					NG_FREE_M(m);
1603069154d5SJulian Elischer 					NG_FREE_META(meta);
16043949bee8SArchie Cobbs 					return (ENOMEM);
16053949bee8SArchie Cobbs 				}
16063949bee8SArchie Cobbs 				m = n;
16073949bee8SArchie Cobbs 			}
16083949bee8SArchie Cobbs 
16093949bee8SArchie Cobbs 			/* Prepend MP header */
16103949bee8SArchie Cobbs 			if (priv->conf.xmitShortSeq) {
16113949bee8SArchie Cobbs 				u_int16_t shdr;
16123949bee8SArchie Cobbs 
1613a9b3dca5SArchie Cobbs 				shdr = priv->xseq;
1614a9b3dca5SArchie Cobbs 				priv->xseq =
16156f16db81SArchie Cobbs 				    (priv->xseq + 1) & MP_SHORT_SEQ_MASK;
16163949bee8SArchie Cobbs 				if (firstFragment)
16173949bee8SArchie Cobbs 					shdr |= MP_SHORT_FIRST_FLAG;
16183949bee8SArchie Cobbs 				if (lastFragment)
16193949bee8SArchie Cobbs 					shdr |= MP_SHORT_LAST_FLAG;
1620d690a6e7SArchie Cobbs 				shdr = htons(shdr);
1621d690a6e7SArchie Cobbs 				m2 = ng_ppp_prepend(m2, &shdr, 2);
16223949bee8SArchie Cobbs 			} else {
16233949bee8SArchie Cobbs 				u_int32_t lhdr;
16243949bee8SArchie Cobbs 
1625a9b3dca5SArchie Cobbs 				lhdr = priv->xseq;
1626a9b3dca5SArchie Cobbs 				priv->xseq =
16276f16db81SArchie Cobbs 				    (priv->xseq + 1) & MP_LONG_SEQ_MASK;
16283949bee8SArchie Cobbs 				if (firstFragment)
16293949bee8SArchie Cobbs 					lhdr |= MP_LONG_FIRST_FLAG;
16303949bee8SArchie Cobbs 				if (lastFragment)
16313949bee8SArchie Cobbs 					lhdr |= MP_LONG_LAST_FLAG;
1632d690a6e7SArchie Cobbs 				lhdr = htonl(lhdr);
1633d690a6e7SArchie Cobbs 				m2 = ng_ppp_prepend(m2, &lhdr, 4);
16343949bee8SArchie Cobbs 			}
16353949bee8SArchie Cobbs 			if (m2 == NULL) {
16363949bee8SArchie Cobbs 				if (!lastFragment)
16373949bee8SArchie Cobbs 					m_freem(m);
16383949bee8SArchie Cobbs 				NG_FREE_META(meta);
16393949bee8SArchie Cobbs 				return (ENOBUFS);
16403949bee8SArchie Cobbs 			}
16413949bee8SArchie Cobbs 
16423949bee8SArchie Cobbs 			/* Copy the meta information, if any */
1643a9b3dca5SArchie Cobbs 			meta2 = lastFragment ? meta : ng_copy_meta(meta);
16443949bee8SArchie Cobbs 
16453949bee8SArchie Cobbs 			/* Send fragment */
1646069154d5SJulian Elischer 			item = ng_package_data(m2, meta2);
1647069154d5SJulian Elischer 			error = ng_ppp_output(node, 0, PROT_MP, linkNum, item);
16483949bee8SArchie Cobbs 			if (error != 0) {
1649069154d5SJulian Elischer 				if (!lastFragment) {
1650069154d5SJulian Elischer 					NG_FREE_M(m);
1651069154d5SJulian Elischer 					NG_FREE_META(meta);
1652069154d5SJulian Elischer 				}
16533949bee8SArchie Cobbs 				return (error);
16543949bee8SArchie Cobbs 			}
16553949bee8SArchie Cobbs 		}
16563949bee8SArchie Cobbs 	}
16573949bee8SArchie Cobbs 
16583949bee8SArchie Cobbs 	/* Done */
16593949bee8SArchie Cobbs 	return (0);
16603949bee8SArchie Cobbs }
16613949bee8SArchie Cobbs 
16623949bee8SArchie Cobbs /*
16633949bee8SArchie Cobbs  * Computing the optimal fragmentation
16643949bee8SArchie Cobbs  * -----------------------------------
16653949bee8SArchie Cobbs  *
16663949bee8SArchie Cobbs  * This routine tries to compute the optimal fragmentation pattern based
16673949bee8SArchie Cobbs  * on each link's latency, bandwidth, and calculated additional latency.
16683949bee8SArchie Cobbs  * The latter quantity is the additional latency caused by previously
16693949bee8SArchie Cobbs  * written data that has not been transmitted yet.
16703949bee8SArchie Cobbs  *
16713949bee8SArchie Cobbs  * This algorithm is only useful when not all of the links have the
16723949bee8SArchie Cobbs  * same latency and bandwidth values.
16733949bee8SArchie Cobbs  *
16743949bee8SArchie Cobbs  * The essential idea is to make the last bit of each fragment of the
16753949bee8SArchie Cobbs  * frame arrive at the opposite end at the exact same time. This greedy
16763949bee8SArchie Cobbs  * algorithm is optimal, in that no other scheduling could result in any
16773949bee8SArchie Cobbs  * packet arriving any sooner unless packets are delivered out of order.
16783949bee8SArchie Cobbs  *
16793949bee8SArchie Cobbs  * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and
16803949bee8SArchie Cobbs  * latency l_i (in miliseconds). Consider the function function f_i(t)
16813949bee8SArchie Cobbs  * which is equal to the number of bytes that will have arrived at
16823949bee8SArchie Cobbs  * the peer after t miliseconds if we start writing continuously at
16833949bee8SArchie Cobbs  * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i).
16843949bee8SArchie Cobbs  * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i).
16853949bee8SArchie Cobbs  * Note that the y-intersect is always <= zero because latency can't be
16863949bee8SArchie Cobbs  * negative.  Note also that really the function is f_i(t) except when
16873949bee8SArchie Cobbs  * f_i(t) is negative, in which case the function is zero.  To take
16883949bee8SArchie Cobbs  * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }.
16893949bee8SArchie Cobbs  * So the actual number of bytes that will have arrived at the peer after
16903949bee8SArchie Cobbs  * t miliseconds is f_i(t) * Q_i(t).
16913949bee8SArchie Cobbs  *
16923949bee8SArchie Cobbs  * At any given time, each link has some additional latency a_i >= 0
16933949bee8SArchie Cobbs  * due to previously written fragment(s) which are still in the queue.
16943949bee8SArchie Cobbs  * This value is easily computed from the time since last transmission,
16953949bee8SArchie Cobbs  * the previous latency value, the number of bytes written, and the
16963949bee8SArchie Cobbs  * link's bandwidth.
16973949bee8SArchie Cobbs  *
16983949bee8SArchie Cobbs  * Assume that l_i includes any a_i already, and that the links are
16993949bee8SArchie Cobbs  * sorted by latency, so that l_i <= l_{i+1}.
17003949bee8SArchie Cobbs  *
17013949bee8SArchie Cobbs  * Let N be the total number of bytes in the current frame we are sending.
17023949bee8SArchie Cobbs  *
17033949bee8SArchie Cobbs  * Suppose we were to start writing bytes at time t = 0 on all links
17043949bee8SArchie Cobbs  * simultaneously, which is the most we can possibly do.  Then let
17053949bee8SArchie Cobbs  * F(t) be equal to the total number of bytes received by the peer
17063949bee8SArchie Cobbs  * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)).
17073949bee8SArchie Cobbs  *
17083949bee8SArchie Cobbs  * Our goal is simply this: fragment the frame across the links such
17093949bee8SArchie Cobbs  * that the peer is able to reconstruct the completed frame as soon as
17103949bee8SArchie Cobbs  * possible, i.e., at the least possible value of t. Call this value t_0.
17113949bee8SArchie Cobbs  *
17123949bee8SArchie Cobbs  * Then it follows that F(t_0) = N. Our strategy is first to find the value
17133949bee8SArchie Cobbs  * of t_0, and then deduce how many bytes to write to each link.
17143949bee8SArchie Cobbs  *
17153949bee8SArchie Cobbs  * Rewriting F(t_0):
17163949bee8SArchie Cobbs  *
17173949bee8SArchie Cobbs  *   t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) )
17183949bee8SArchie Cobbs  *
17193949bee8SArchie Cobbs  * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will
17203949bee8SArchie Cobbs  * lie in one of these ranges.  To find it, we just need to find the i such
17213949bee8SArchie Cobbs  * that F(l_i) <= N <= F(l_{i+1}).  Then we compute all the constant values
17223949bee8SArchie Cobbs  * for Q_i() in this range, plug in the remaining values, solving for t_0.
17233949bee8SArchie Cobbs  *
17243949bee8SArchie Cobbs  * Once t_0 is known, then the number of bytes to send on link i is
17253949bee8SArchie Cobbs  * just f_i(t_0) * Q_i(t_0).
17263949bee8SArchie Cobbs  *
17273949bee8SArchie Cobbs  * In other words, we start allocating bytes to the links one at a time.
17283949bee8SArchie Cobbs  * We keep adding links until the frame is completely sent.  Some links
17293949bee8SArchie Cobbs  * may not get any bytes because their latency is too high.
17303949bee8SArchie Cobbs  *
17313949bee8SArchie Cobbs  * Is all this work really worth the trouble?  Depends on the situation.
17323949bee8SArchie Cobbs  * The bigger the ratio of computer speed to link speed, and the more
17333949bee8SArchie Cobbs  * important total bundle latency is (e.g., for interactive response time),
17343949bee8SArchie Cobbs  * the more it's worth it.  There is however the cost of calling this
17353949bee8SArchie Cobbs  * function for every frame.  The running time is O(n^2) where n is the
17363949bee8SArchie Cobbs  * number of links that receive a non-zero number of bytes.
17373949bee8SArchie Cobbs  *
17383949bee8SArchie Cobbs  * Since latency is measured in miliseconds, the "resolution" of this
17393949bee8SArchie Cobbs  * algorithm is one milisecond.
17403949bee8SArchie Cobbs  *
17413949bee8SArchie Cobbs  * To avoid this algorithm altogether, configure all links to have the
17423949bee8SArchie Cobbs  * same latency and bandwidth.
17433949bee8SArchie Cobbs  */
17443949bee8SArchie Cobbs static void
17453949bee8SArchie Cobbs ng_ppp_mp_strategy(node_p node, int len, int *distrib)
17463949bee8SArchie Cobbs {
174730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
17483949bee8SArchie Cobbs 	int latency[NG_PPP_MAX_LINKS];
17493949bee8SArchie Cobbs 	int sortByLatency[NG_PPP_MAX_LINKS];
1750a9b3dca5SArchie Cobbs 	int activeLinkNum;
17513949bee8SArchie Cobbs 	int t0, total, topSum, botSum;
17523949bee8SArchie Cobbs 	struct timeval now;
17533949bee8SArchie Cobbs 	int i, numFragments;
17543949bee8SArchie Cobbs 
17553949bee8SArchie Cobbs 	/* If only one link, this gets real easy */
17563949bee8SArchie Cobbs 	if (priv->numActiveLinks == 1) {
17573949bee8SArchie Cobbs 		distrib[0] = len;
17583949bee8SArchie Cobbs 		return;
17593949bee8SArchie Cobbs 	}
17603949bee8SArchie Cobbs 
17613949bee8SArchie Cobbs 	/* Get current time */
1762a9b3dca5SArchie Cobbs 	getmicrouptime(&now);
17633949bee8SArchie Cobbs 
17643949bee8SArchie Cobbs 	/* Compute latencies for each link at this point in time */
17653949bee8SArchie Cobbs 	for (activeLinkNum = 0;
17663949bee8SArchie Cobbs 	    activeLinkNum < priv->numActiveLinks; activeLinkNum++) {
1767a9b3dca5SArchie Cobbs 		struct ng_ppp_link *alink;
17683949bee8SArchie Cobbs 		struct timeval diff;
17693949bee8SArchie Cobbs 		int xmitBytes;
17703949bee8SArchie Cobbs 
17713949bee8SArchie Cobbs 		/* Start with base latency value */
1772a9b3dca5SArchie Cobbs 		alink = &priv->links[priv->activeLinks[activeLinkNum]];
1773a9b3dca5SArchie Cobbs 		latency[activeLinkNum] = alink->conf.latency;
17743949bee8SArchie Cobbs 		sortByLatency[activeLinkNum] = activeLinkNum;	/* see below */
17753949bee8SArchie Cobbs 
17763949bee8SArchie Cobbs 		/* Any additional latency? */
1777a9b3dca5SArchie Cobbs 		if (alink->bytesInQueue == 0)
17783949bee8SArchie Cobbs 			continue;
17793949bee8SArchie Cobbs 
17803949bee8SArchie Cobbs 		/* Compute time delta since last write */
17813949bee8SArchie Cobbs 		diff = now;
1782a9b3dca5SArchie Cobbs 		timevalsub(&diff, &alink->lastWrite);
17833949bee8SArchie Cobbs 		if (now.tv_sec < 0 || diff.tv_sec >= 10) {	/* sanity */
1784a9b3dca5SArchie Cobbs 			alink->bytesInQueue = 0;
17853949bee8SArchie Cobbs 			continue;
17863949bee8SArchie Cobbs 		}
17873949bee8SArchie Cobbs 
17883949bee8SArchie Cobbs 		/* How many bytes could have transmitted since last write? */
1789a9b3dca5SArchie Cobbs 		xmitBytes = (alink->conf.bandwidth * diff.tv_sec)
1790a9b3dca5SArchie Cobbs 		    + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100;
1791a9b3dca5SArchie Cobbs 		alink->bytesInQueue -= xmitBytes;
1792a9b3dca5SArchie Cobbs 		if (alink->bytesInQueue < 0)
1793a9b3dca5SArchie Cobbs 			alink->bytesInQueue = 0;
17943949bee8SArchie Cobbs 		else
17953949bee8SArchie Cobbs 			latency[activeLinkNum] +=
1796a9b3dca5SArchie Cobbs 			    (100 * alink->bytesInQueue) / alink->conf.bandwidth;
17973949bee8SArchie Cobbs 	}
17983949bee8SArchie Cobbs 
1799a9b3dca5SArchie Cobbs 	/* Sort active links by latency */
18003949bee8SArchie Cobbs 	compareLatencies = latency;
18013949bee8SArchie Cobbs 	qsort(sortByLatency,
18023949bee8SArchie Cobbs 	    priv->numActiveLinks, sizeof(*sortByLatency), ng_ppp_intcmp);
18033949bee8SArchie Cobbs 	compareLatencies = NULL;
18043949bee8SArchie Cobbs 
18053949bee8SArchie Cobbs 	/* Find the interval we need (add links in sortByLatency[] order) */
18063949bee8SArchie Cobbs 	for (numFragments = 1;
18073949bee8SArchie Cobbs 	    numFragments < priv->numActiveLinks; numFragments++) {
18083949bee8SArchie Cobbs 		for (total = i = 0; i < numFragments; i++) {
18093949bee8SArchie Cobbs 			int flowTime;
18103949bee8SArchie Cobbs 
18113949bee8SArchie Cobbs 			flowTime = latency[sortByLatency[numFragments]]
18123949bee8SArchie Cobbs 			    - latency[sortByLatency[i]];
1813a9b3dca5SArchie Cobbs 			total += ((flowTime * priv->links[
1814a9b3dca5SArchie Cobbs 			    priv->activeLinks[sortByLatency[i]]].conf.bandwidth)
18153949bee8SArchie Cobbs 			    	+ 99) / 100;
18163949bee8SArchie Cobbs 		}
18173949bee8SArchie Cobbs 		if (total >= len)
18183949bee8SArchie Cobbs 			break;
18193949bee8SArchie Cobbs 	}
18203949bee8SArchie Cobbs 
18213949bee8SArchie Cobbs 	/* Solve for t_0 in that interval */
18223949bee8SArchie Cobbs 	for (topSum = botSum = i = 0; i < numFragments; i++) {
1823a9b3dca5SArchie Cobbs 		int bw = priv->links[
1824a9b3dca5SArchie Cobbs 		    priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
18253949bee8SArchie Cobbs 
18263949bee8SArchie Cobbs 		topSum += latency[sortByLatency[i]] * bw;	/* / 100 */
18273949bee8SArchie Cobbs 		botSum += bw;					/* / 100 */
18283949bee8SArchie Cobbs 	}
18293949bee8SArchie Cobbs 	t0 = ((len * 100) + topSum + botSum / 2) / botSum;
18303949bee8SArchie Cobbs 
18313949bee8SArchie Cobbs 	/* Compute f_i(t_0) all i */
18323949bee8SArchie Cobbs 	bzero(distrib, priv->numActiveLinks * sizeof(*distrib));
18333949bee8SArchie Cobbs 	for (total = i = 0; i < numFragments; i++) {
1834a9b3dca5SArchie Cobbs 		int bw = priv->links[
1835a9b3dca5SArchie Cobbs 		    priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
18363949bee8SArchie Cobbs 
18373949bee8SArchie Cobbs 		distrib[sortByLatency[i]] =
18383949bee8SArchie Cobbs 		    (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100;
18393949bee8SArchie Cobbs 		total += distrib[sortByLatency[i]];
18403949bee8SArchie Cobbs 	}
18413949bee8SArchie Cobbs 
1842fb1fc8abSArchie Cobbs 	/* Deal with any rounding error */
1843fb1fc8abSArchie Cobbs 	if (total < len) {
1844a9b3dca5SArchie Cobbs 		struct ng_ppp_link *fastLink =
1845a9b3dca5SArchie Cobbs 		    &priv->links[priv->activeLinks[sortByLatency[0]]];
18463949bee8SArchie Cobbs 		int fast = 0;
18473949bee8SArchie Cobbs 
1848fb1fc8abSArchie Cobbs 		/* Find the fastest link */
18493949bee8SArchie Cobbs 		for (i = 1; i < numFragments; i++) {
1850a9b3dca5SArchie Cobbs 			struct ng_ppp_link *const link =
1851a9b3dca5SArchie Cobbs 			    &priv->links[priv->activeLinks[sortByLatency[i]]];
1852a9b3dca5SArchie Cobbs 
1853a9b3dca5SArchie Cobbs 			if (link->conf.bandwidth > fastLink->conf.bandwidth) {
18543949bee8SArchie Cobbs 				fast = i;
1855a9b3dca5SArchie Cobbs 				fastLink = link;
1856a9b3dca5SArchie Cobbs 			}
18573949bee8SArchie Cobbs 		}
18583949bee8SArchie Cobbs 		distrib[sortByLatency[fast]] += len - total;
1859fb1fc8abSArchie Cobbs 	} else while (total > len) {
1860a9b3dca5SArchie Cobbs 		struct ng_ppp_link *slowLink =
1861a9b3dca5SArchie Cobbs 		    &priv->links[priv->activeLinks[sortByLatency[0]]];
1862fb1fc8abSArchie Cobbs 		int delta, slow = 0;
18633949bee8SArchie Cobbs 
1864fb1fc8abSArchie Cobbs 		/* Find the slowest link that still has bytes to remove */
1865fb1fc8abSArchie Cobbs 		for (i = 1; i < numFragments; i++) {
1866a9b3dca5SArchie Cobbs 			struct ng_ppp_link *const link =
1867a9b3dca5SArchie Cobbs 			    &priv->links[priv->activeLinks[sortByLatency[i]]];
1868a9b3dca5SArchie Cobbs 
1869fb1fc8abSArchie Cobbs 			if (distrib[sortByLatency[slow]] == 0
1870fb1fc8abSArchie Cobbs 			  || (distrib[sortByLatency[i]] > 0
1871a9b3dca5SArchie Cobbs 			    && link->conf.bandwidth <
1872a9b3dca5SArchie Cobbs 			      slowLink->conf.bandwidth)) {
1873fb1fc8abSArchie Cobbs 				slow = i;
1874a9b3dca5SArchie Cobbs 				slowLink = link;
1875a9b3dca5SArchie Cobbs 			}
1876fb1fc8abSArchie Cobbs 		}
1877fb1fc8abSArchie Cobbs 		delta = total - len;
1878fb1fc8abSArchie Cobbs 		if (delta > distrib[sortByLatency[slow]])
1879fb1fc8abSArchie Cobbs 			delta = distrib[sortByLatency[slow]];
1880fb1fc8abSArchie Cobbs 		distrib[sortByLatency[slow]] -= delta;
1881fb1fc8abSArchie Cobbs 		total -= delta;
18823949bee8SArchie Cobbs 	}
18833949bee8SArchie Cobbs }
18843949bee8SArchie Cobbs 
18853949bee8SArchie Cobbs /*
18863949bee8SArchie Cobbs  * Compare two integers
18873949bee8SArchie Cobbs  */
18883949bee8SArchie Cobbs static int
18893949bee8SArchie Cobbs ng_ppp_intcmp(const void *v1, const void *v2)
18903949bee8SArchie Cobbs {
18913949bee8SArchie Cobbs 	const int index1 = *((const int *) v1);
18923949bee8SArchie Cobbs 	const int index2 = *((const int *) v2);
18933949bee8SArchie Cobbs 
18943949bee8SArchie Cobbs 	return compareLatencies[index1] - compareLatencies[index2];
18953949bee8SArchie Cobbs }
18963949bee8SArchie Cobbs 
18973949bee8SArchie Cobbs /*
18983949bee8SArchie Cobbs  * Prepend a possibly compressed PPP protocol number in front of a frame
18993949bee8SArchie Cobbs  */
19003949bee8SArchie Cobbs static struct mbuf *
19013949bee8SArchie Cobbs ng_ppp_addproto(struct mbuf *m, int proto, int compOK)
19023949bee8SArchie Cobbs {
1903d690a6e7SArchie Cobbs 	if (compOK && PROT_COMPRESSABLE(proto)) {
1904d690a6e7SArchie Cobbs 		u_char pbyte = (u_char)proto;
19053949bee8SArchie Cobbs 
1906d690a6e7SArchie Cobbs 		return ng_ppp_prepend(m, &pbyte, 1);
1907d690a6e7SArchie Cobbs 	} else {
1908d690a6e7SArchie Cobbs 		u_int16_t pword = htons((u_int16_t)proto);
1909d690a6e7SArchie Cobbs 
1910d690a6e7SArchie Cobbs 		return ng_ppp_prepend(m, &pword, 2);
1911d690a6e7SArchie Cobbs 	}
1912d690a6e7SArchie Cobbs }
1913d690a6e7SArchie Cobbs 
1914d690a6e7SArchie Cobbs /*
1915d690a6e7SArchie Cobbs  * Prepend some bytes to an mbuf
1916d690a6e7SArchie Cobbs  */
1917d690a6e7SArchie Cobbs static struct mbuf *
1918d690a6e7SArchie Cobbs ng_ppp_prepend(struct mbuf *m, const void *buf, int len)
1919d690a6e7SArchie Cobbs {
1920a163d034SWarner Losh 	M_PREPEND(m, len, M_DONTWAIT);
1921d690a6e7SArchie Cobbs 	if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL))
19224cf49a43SJulian Elischer 		return (NULL);
1923d690a6e7SArchie Cobbs 	bcopy(buf, mtod(m, u_char *), len);
19243949bee8SArchie Cobbs 	return (m);
19253949bee8SArchie Cobbs }
19263949bee8SArchie Cobbs 
19273949bee8SArchie Cobbs /*
19283949bee8SArchie Cobbs  * Update private information that is derived from other private information
19293949bee8SArchie Cobbs  */
19303949bee8SArchie Cobbs static void
19313949bee8SArchie Cobbs ng_ppp_update(node_p node, int newConf)
19323949bee8SArchie Cobbs {
193330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
19343949bee8SArchie Cobbs 	int i;
19353949bee8SArchie Cobbs 
19363949bee8SArchie Cobbs 	/* Update active status for VJ Compression */
19373949bee8SArchie Cobbs 	priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL
19383949bee8SArchie Cobbs 	    && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL
19393949bee8SArchie Cobbs 	    && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL
19403949bee8SArchie Cobbs 	    && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL;
19413949bee8SArchie Cobbs 
19423949bee8SArchie Cobbs 	/* Increase latency for each link an amount equal to one MP header */
19433949bee8SArchie Cobbs 	if (newConf) {
19443949bee8SArchie Cobbs 		for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
19453949bee8SArchie Cobbs 			int hdrBytes;
19463949bee8SArchie Cobbs 
1947a9b3dca5SArchie Cobbs 			hdrBytes = (priv->links[i].conf.enableACFComp ? 0 : 2)
1948a9b3dca5SArchie Cobbs 			    + (priv->links[i].conf.enableProtoComp ? 1 : 2)
19493949bee8SArchie Cobbs 			    + (priv->conf.xmitShortSeq ? 2 : 4);
1950a9b3dca5SArchie Cobbs 			priv->links[i].conf.latency +=
1951a9b3dca5SArchie Cobbs 			    ((hdrBytes * priv->links[i].conf.bandwidth) + 50)
19523949bee8SArchie Cobbs 				/ 100;
19533949bee8SArchie Cobbs 		}
19543949bee8SArchie Cobbs 	}
19553949bee8SArchie Cobbs 
19563949bee8SArchie Cobbs 	/* Update list of active links */
19573949bee8SArchie Cobbs 	bzero(&priv->activeLinks, sizeof(priv->activeLinks));
19583949bee8SArchie Cobbs 	priv->numActiveLinks = 0;
19593949bee8SArchie Cobbs 	priv->allLinksEqual = 1;
19603949bee8SArchie Cobbs 	for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1961a9b3dca5SArchie Cobbs 		struct ng_ppp_link *const link = &priv->links[i];
19620e11d0f3SArchie Cobbs 
1963a9b3dca5SArchie Cobbs 		/* Is link active? */
1964a9b3dca5SArchie Cobbs 		if (link->conf.enableLink && link->hook != NULL) {
1965a9b3dca5SArchie Cobbs 			struct ng_ppp_link *link0;
1966a9b3dca5SArchie Cobbs 
1967a9b3dca5SArchie Cobbs 			/* Add link to list of active links */
19683949bee8SArchie Cobbs 			priv->activeLinks[priv->numActiveLinks++] = i;
1969a9b3dca5SArchie Cobbs 			link0 = &priv->links[priv->activeLinks[0]];
1970a9b3dca5SArchie Cobbs 
1971a9b3dca5SArchie Cobbs 			/* Determine if all links are still equal */
1972a9b3dca5SArchie Cobbs 			if (link->conf.latency != link0->conf.latency
1973a9b3dca5SArchie Cobbs 			  || link->conf.bandwidth != link0->conf.bandwidth)
19743949bee8SArchie Cobbs 				priv->allLinksEqual = 0;
1975a9b3dca5SArchie Cobbs 
1976a9b3dca5SArchie Cobbs 			/* Initialize rec'd sequence number */
1977a9b3dca5SArchie Cobbs 			if (link->seq == MP_NOSEQ) {
1978a9b3dca5SArchie Cobbs 				link->seq = (link == link0) ?
1979a9b3dca5SArchie Cobbs 				    MP_INITIAL_SEQ : link0->seq;
19803949bee8SArchie Cobbs 			}
1981a9b3dca5SArchie Cobbs 		} else
1982a9b3dca5SArchie Cobbs 			link->seq = MP_NOSEQ;
19833949bee8SArchie Cobbs 	}
19843949bee8SArchie Cobbs 
1985a9b3dca5SArchie Cobbs 	/* Update MP state as multi-link is active or not */
1986a9b3dca5SArchie Cobbs 	if (priv->conf.enableMultilink && priv->numActiveLinks > 0)
1987a9b3dca5SArchie Cobbs 		ng_ppp_start_frag_timer(node);
1988a9b3dca5SArchie Cobbs 	else {
1989a9b3dca5SArchie Cobbs 		ng_ppp_stop_frag_timer(node);
1990a9b3dca5SArchie Cobbs 		ng_ppp_frag_reset(node);
1991a9b3dca5SArchie Cobbs 		priv->xseq = MP_INITIAL_SEQ;
1992a9b3dca5SArchie Cobbs 		priv->mseq = MP_INITIAL_SEQ;
1993a9b3dca5SArchie Cobbs 		for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
1994a9b3dca5SArchie Cobbs 			struct ng_ppp_link *const link = &priv->links[i];
1995a9b3dca5SArchie Cobbs 
1996a9b3dca5SArchie Cobbs 			bzero(&link->lastWrite, sizeof(link->lastWrite));
1997a9b3dca5SArchie Cobbs 			link->bytesInQueue = 0;
1998a9b3dca5SArchie Cobbs 			link->seq = MP_NOSEQ;
1999a9b3dca5SArchie Cobbs 		}
20003949bee8SArchie Cobbs 	}
20013949bee8SArchie Cobbs }
20023949bee8SArchie Cobbs 
20033949bee8SArchie Cobbs /*
20043949bee8SArchie Cobbs  * Determine if a new configuration would represent a valid change
20053949bee8SArchie Cobbs  * from the current configuration and link activity status.
20063949bee8SArchie Cobbs  */
20073949bee8SArchie Cobbs static int
2008a9b3dca5SArchie Cobbs ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf)
20093949bee8SArchie Cobbs {
201030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
20113949bee8SArchie Cobbs 	int i, newNumLinksActive;
20123949bee8SArchie Cobbs 
20133949bee8SArchie Cobbs 	/* Check per-link config and count how many links would be active */
20143949bee8SArchie Cobbs 	for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) {
2015a9b3dca5SArchie Cobbs 		if (newConf->links[i].enableLink && priv->links[i].hook != NULL)
2016e149c4e2SArchie Cobbs 			newNumLinksActive++;
2017e149c4e2SArchie Cobbs 		if (!newConf->links[i].enableLink)
2018e149c4e2SArchie Cobbs 			continue;
20193949bee8SArchie Cobbs 		if (newConf->links[i].mru < MP_MIN_LINK_MRU)
20203949bee8SArchie Cobbs 			return (0);
20213949bee8SArchie Cobbs 		if (newConf->links[i].bandwidth == 0)
20223949bee8SArchie Cobbs 			return (0);
20233949bee8SArchie Cobbs 		if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH)
20243949bee8SArchie Cobbs 			return (0);
20253949bee8SArchie Cobbs 		if (newConf->links[i].latency > NG_PPP_MAX_LATENCY)
20263949bee8SArchie Cobbs 			return (0);
20273949bee8SArchie Cobbs 	}
20283949bee8SArchie Cobbs 
20293949bee8SArchie Cobbs 	/* Check bundle parameters */
2030a9b3dca5SArchie Cobbs 	if (newConf->bund.enableMultilink && newConf->bund.mrru < MP_MIN_MRRU)
20313949bee8SArchie Cobbs 		return (0);
20323949bee8SArchie Cobbs 
20333949bee8SArchie Cobbs 	/* Disallow changes to multi-link configuration while MP is active */
20343949bee8SArchie Cobbs 	if (priv->numActiveLinks > 0 && newNumLinksActive > 0) {
2035a9b3dca5SArchie Cobbs 		if (!priv->conf.enableMultilink
2036a9b3dca5SArchie Cobbs 				!= !newConf->bund.enableMultilink
2037a9b3dca5SArchie Cobbs 		    || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq
2038a9b3dca5SArchie Cobbs 		    || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq)
20393949bee8SArchie Cobbs 			return (0);
20403949bee8SArchie Cobbs 	}
20413949bee8SArchie Cobbs 
2042e149c4e2SArchie Cobbs 	/* At most one link can be active unless multi-link is enabled */
2043a9b3dca5SArchie Cobbs 	if (!newConf->bund.enableMultilink && newNumLinksActive > 1)
2044e149c4e2SArchie Cobbs 		return (0);
2045e149c4e2SArchie Cobbs 
2046e149c4e2SArchie Cobbs 	/* Configuration change would be valid */
20473949bee8SArchie Cobbs 	return (1);
20483949bee8SArchie Cobbs }
20493949bee8SArchie Cobbs 
20503949bee8SArchie Cobbs /*
20513949bee8SArchie Cobbs  * Free all entries in the fragment queue
20523949bee8SArchie Cobbs  */
20533949bee8SArchie Cobbs static void
2054a9b3dca5SArchie Cobbs ng_ppp_frag_reset(node_p node)
20553949bee8SArchie Cobbs {
205630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
20571e7a9f72SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext;
20583949bee8SArchie Cobbs 
20595f90cac7SKirk McKusick 	for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) {
20605f90cac7SKirk McKusick 		qnext = TAILQ_NEXT(qent, f_qent);
2061069154d5SJulian Elischer 		NG_FREE_M(qent->data);
2062069154d5SJulian Elischer 		NG_FREE_META(qent->meta);
20639c8c302fSJulian Elischer 		FREE(qent, M_NETGRAPH_PPP);
20643949bee8SArchie Cobbs 	}
20655f90cac7SKirk McKusick 	TAILQ_INIT(&priv->frags);
2066a9b3dca5SArchie Cobbs 	priv->qlen = 0;
2067a9b3dca5SArchie Cobbs }
2068a9b3dca5SArchie Cobbs 
2069a9b3dca5SArchie Cobbs /*
2070a9b3dca5SArchie Cobbs  * Start fragment queue timer
2071a9b3dca5SArchie Cobbs  */
2072a9b3dca5SArchie Cobbs static void
2073a9b3dca5SArchie Cobbs ng_ppp_start_frag_timer(node_p node)
2074a9b3dca5SArchie Cobbs {
207530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2076a9b3dca5SArchie Cobbs 
2077a9b3dca5SArchie Cobbs 	if (!priv->timerActive) {
2078a9b3dca5SArchie Cobbs 		priv->fragTimer = timeout(ng_ppp_frag_timeout,
2079a9b3dca5SArchie Cobbs 		    node, MP_FRAGTIMER_INTERVAL);
2080a9b3dca5SArchie Cobbs 		priv->timerActive = 1;
208130400f03SJulian Elischer 		NG_NODE_REF(node);
2082a9b3dca5SArchie Cobbs 	}
2083a9b3dca5SArchie Cobbs }
2084a9b3dca5SArchie Cobbs 
2085a9b3dca5SArchie Cobbs /*
2086a9b3dca5SArchie Cobbs  * Stop fragment queue timer
2087a9b3dca5SArchie Cobbs  */
2088a9b3dca5SArchie Cobbs static void
2089a9b3dca5SArchie Cobbs ng_ppp_stop_frag_timer(node_p node)
2090a9b3dca5SArchie Cobbs {
209130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2092a9b3dca5SArchie Cobbs 
2093a9b3dca5SArchie Cobbs 	if (priv->timerActive) {
2094a9b3dca5SArchie Cobbs 		untimeout(ng_ppp_frag_timeout, node, priv->fragTimer);
2095a9b3dca5SArchie Cobbs 		priv->timerActive = 0;
209630400f03SJulian Elischer 		KASSERT(node->nd_refs > 1,
20976e551fb6SDavid E. O'Brien 		    ("%s: nd_refs=%d", __func__, node->nd_refs));
209830400f03SJulian Elischer 		NG_NODE_UNREF(node);
2099a9b3dca5SArchie Cobbs 	}
21004cf49a43SJulian Elischer }
21014cf49a43SJulian Elischer 
2102