xref: /freebsd/sys/netgraph/ng_ppp.c (revision fd58342c2684fdd73f6011b9432031df380a1831)
1c398230bSWarner Losh /*-
2a9b3dca5SArchie Cobbs  * Copyright (c) 1996-2000 Whistle Communications, Inc.
34cf49a43SJulian Elischer  * All rights reserved.
44cf49a43SJulian Elischer  *
54cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
64cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
74cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
84cf49a43SJulian Elischer  * provided, however, that:
94cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
104cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
114cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
124cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
134cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
144cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
154cf49a43SJulian Elischer  *
164cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
174cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
184cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
194cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
204cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
214cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
224cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
234cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
244cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
254cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
264cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
274cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
284cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
294cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
304cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
314cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
324cf49a43SJulian Elischer  * OF SUCH DAMAGE.
334cf49a43SJulian Elischer  *
34164b576eSGleb Smirnoff  * Copyright (c) 2007 Alexander Motin <mav@alkar.net>
35164b576eSGleb Smirnoff  * All rights reserved.
36164b576eSGleb Smirnoff  *
37164b576eSGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
38164b576eSGleb Smirnoff  * modification, are permitted provided that the following conditions
39164b576eSGleb Smirnoff  * are met:
40164b576eSGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
41164b576eSGleb Smirnoff  *    notice unmodified, this list of conditions, and the following
42164b576eSGleb Smirnoff  *    disclaimer.
43164b576eSGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
44164b576eSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
45164b576eSGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
46164b576eSGleb Smirnoff  *
47164b576eSGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48164b576eSGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49164b576eSGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50164b576eSGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
51164b576eSGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52164b576eSGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53164b576eSGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54164b576eSGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55164b576eSGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56164b576eSGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57164b576eSGleb Smirnoff  * SUCH DAMAGE.
58164b576eSGleb Smirnoff  *
59164b576eSGleb Smirnoff  * Authors: Archie Cobbs <archie@freebsd.org>, Alexander Motin <mav@alkar.net>
604cf49a43SJulian Elischer  *
614cf49a43SJulian Elischer  * $FreeBSD$
6274f5c6aaSJulian Elischer  * $Whistle: ng_ppp.c,v 1.24 1999/11/01 09:24:52 julian Exp $
634cf49a43SJulian Elischer  */
644cf49a43SJulian Elischer 
654cf49a43SJulian Elischer /*
66164b576eSGleb Smirnoff  * PPP node type data-flow.
67164b576eSGleb Smirnoff  *
68164b576eSGleb Smirnoff  *       hook      xmit        layer         recv      hook
69164b576eSGleb Smirnoff  *              ------------------------------------
70164b576eSGleb Smirnoff  *       inet ->                                    -> inet
71164b576eSGleb Smirnoff  *       ipv6 ->                                    -> ipv6
72164b576eSGleb Smirnoff  *        ipx ->               proto                -> ipx
73164b576eSGleb Smirnoff  *      atalk ->                                    -> atalk
74164b576eSGleb Smirnoff  *     bypass ->                                    -> bypass
75164b576eSGleb Smirnoff  *              -hcomp_xmit()----------proto_recv()-
76164b576eSGleb Smirnoff  *     vjc_ip <-                                    <- vjc_ip
77164b576eSGleb Smirnoff  *   vjc_comp ->         header compression         -> vjc_comp
78164b576eSGleb Smirnoff  * vjc_uncomp ->                                    -> vjc_uncomp
7939c14742SGleb Smirnoff  *   vjc_vjip ->
80164b576eSGleb Smirnoff  *              -comp_xmit()-----------hcomp_recv()-
81164b576eSGleb Smirnoff  *   compress <-            compression             <- decompress
82164b576eSGleb Smirnoff  *   compress ->                                    -> decompress
83164b576eSGleb Smirnoff  *              -crypt_xmit()-----------comp_recv()-
84164b576eSGleb Smirnoff  *    encrypt <-             encryption             <- decrypt
85164b576eSGleb Smirnoff  *    encrypt ->                                    -> decrypt
86164b576eSGleb Smirnoff  *              -ml_xmit()-------------crypt_recv()-
87164b576eSGleb Smirnoff  *                           multilink
88164b576eSGleb Smirnoff  *              -link_xmit()--------------ml_recv()-
89164b576eSGleb Smirnoff  *      linkX <-               link                 <- linkX
90164b576eSGleb Smirnoff  *
914cf49a43SJulian Elischer  */
924cf49a43SJulian Elischer 
934cf49a43SJulian Elischer #include <sys/param.h>
944cf49a43SJulian Elischer #include <sys/systm.h>
954cf49a43SJulian Elischer #include <sys/kernel.h>
96104a9b7eSAlexander Kabaev #include <sys/limits.h>
97a9b3dca5SArchie Cobbs #include <sys/time.h>
984cf49a43SJulian Elischer #include <sys/mbuf.h>
994cf49a43SJulian Elischer #include <sys/malloc.h>
1004cf49a43SJulian Elischer #include <sys/errno.h>
1015b664c7cSPoul-Henning Kamp #include <sys/ctype.h>
1024cf49a43SJulian Elischer 
1034cf49a43SJulian Elischer #include <netgraph/ng_message.h>
1044cf49a43SJulian Elischer #include <netgraph/netgraph.h>
105f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
1064cf49a43SJulian Elischer #include <netgraph/ng_ppp.h>
1073949bee8SArchie Cobbs #include <netgraph/ng_vjc.h>
1084cf49a43SJulian Elischer 
1099c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC
1109c8c302fSJulian Elischer MALLOC_DEFINE(M_NETGRAPH_PPP, "netgraph_ppp", "netgraph ppp node");
1119c8c302fSJulian Elischer #else
1129c8c302fSJulian Elischer #define M_NETGRAPH_PPP M_NETGRAPH
1139c8c302fSJulian Elischer #endif
1149c8c302fSJulian Elischer 
1154cf49a43SJulian Elischer #define PROT_VALID(p)		(((p) & 0x0101) == 0x0001)
1162b70adcbSArchie Cobbs #define PROT_COMPRESSABLE(p)	(((p) & 0xff00) == 0x0000)
1174cf49a43SJulian Elischer 
1183949bee8SArchie Cobbs /* Some PPP protocol numbers we're interested in */
119164b576eSGleb Smirnoff #define PROT_ATALK		0x0029
1203949bee8SArchie Cobbs #define PROT_COMPD		0x00fd
1213949bee8SArchie Cobbs #define PROT_CRYPTD		0x0053
1223949bee8SArchie Cobbs #define PROT_IP			0x0021
123a9b3dca5SArchie Cobbs #define PROT_IPV6		0x0057
1242b70adcbSArchie Cobbs #define PROT_IPX		0x002b
125d690a6e7SArchie Cobbs #define PROT_LCP		0xc021
1263949bee8SArchie Cobbs #define PROT_MP			0x003d
1273949bee8SArchie Cobbs #define PROT_VJCOMP		0x002d
1283949bee8SArchie Cobbs #define PROT_VJUNCOMP		0x002f
1293949bee8SArchie Cobbs 
1303949bee8SArchie Cobbs /* Multilink PPP definitions */
1313949bee8SArchie Cobbs #define MP_MIN_MRRU		1500		/* per RFC 1990 */
1323949bee8SArchie Cobbs #define MP_INITIAL_SEQ		0		/* per RFC 1990 */
1333949bee8SArchie Cobbs #define MP_MIN_LINK_MRU		32
1343949bee8SArchie Cobbs 
1353949bee8SArchie Cobbs #define MP_SHORT_SEQ_MASK	0x00000fff	/* short seq # mask */
1363949bee8SArchie Cobbs #define MP_SHORT_SEQ_HIBIT	0x00000800	/* short seq # high bit */
1373949bee8SArchie Cobbs #define MP_SHORT_FIRST_FLAG	0x00008000	/* first fragment in frame */
1383949bee8SArchie Cobbs #define MP_SHORT_LAST_FLAG	0x00004000	/* last fragment in frame */
1393949bee8SArchie Cobbs 
1403949bee8SArchie Cobbs #define MP_LONG_SEQ_MASK	0x00ffffff	/* long seq # mask */
1413949bee8SArchie Cobbs #define MP_LONG_SEQ_HIBIT	0x00800000	/* long seq # high bit */
1423949bee8SArchie Cobbs #define MP_LONG_FIRST_FLAG	0x80000000	/* first fragment in frame */
1433949bee8SArchie Cobbs #define MP_LONG_LAST_FLAG	0x40000000	/* last fragment in frame */
1443949bee8SArchie Cobbs 
1456f16db81SArchie Cobbs #define MP_NOSEQ		0x7fffffff	/* impossible sequence number */
1463949bee8SArchie Cobbs 
1473949bee8SArchie Cobbs /* Sign extension of MP sequence numbers */
1483949bee8SArchie Cobbs #define MP_SHORT_EXTEND(s)	(((s) & MP_SHORT_SEQ_HIBIT) ?		\
1496f16db81SArchie Cobbs 				    ((s) | ~MP_SHORT_SEQ_MASK)		\
1506f16db81SArchie Cobbs 				    : ((s) & MP_SHORT_SEQ_MASK))
1513949bee8SArchie Cobbs #define MP_LONG_EXTEND(s)	(((s) & MP_LONG_SEQ_HIBIT) ?		\
1526f16db81SArchie Cobbs 				    ((s) | ~MP_LONG_SEQ_MASK)		\
1536f16db81SArchie Cobbs 				    : ((s) & MP_LONG_SEQ_MASK))
1543949bee8SArchie Cobbs 
1556f16db81SArchie Cobbs /* Comparision of MP sequence numbers. Note: all sequence numbers
1566f16db81SArchie Cobbs    except priv->xseq are stored with the sign bit extended. */
1576f16db81SArchie Cobbs #define MP_SHORT_SEQ_DIFF(x,y)	MP_SHORT_EXTEND((x) - (y))
1586f16db81SArchie Cobbs #define MP_LONG_SEQ_DIFF(x,y)	MP_LONG_EXTEND((x) - (y))
1593949bee8SArchie Cobbs 
1606f16db81SArchie Cobbs #define MP_RECV_SEQ_DIFF(priv,x,y)					\
1616f16db81SArchie Cobbs 				((priv)->conf.recvShortSeq ?		\
1623949bee8SArchie Cobbs 				    MP_SHORT_SEQ_DIFF((x), (y)) :	\
1633949bee8SArchie Cobbs 				    MP_LONG_SEQ_DIFF((x), (y)))
1643949bee8SArchie Cobbs 
1656f16db81SArchie Cobbs /* Increment receive sequence number */
1666f16db81SArchie Cobbs #define MP_NEXT_RECV_SEQ(priv,seq)					\
167b5a60ddbSArchie Cobbs 				((priv)->conf.recvShortSeq ?		\
168b5a60ddbSArchie Cobbs 				    MP_SHORT_EXTEND((seq) + 1) :	\
169b5a60ddbSArchie Cobbs 				    MP_LONG_EXTEND((seq) + 1))
170a9b3dca5SArchie Cobbs 
171164b576eSGleb Smirnoff /* Don't fragment transmitted packets to parts smaller than this */
172164b576eSGleb Smirnoff #define MP_MIN_FRAG_LEN		32
173a9b3dca5SArchie Cobbs 
174a9b3dca5SArchie Cobbs /* Maximum fragment reasssembly queue length */
175a9b3dca5SArchie Cobbs #define MP_MAX_QUEUE_LEN	128
176a9b3dca5SArchie Cobbs 
177a9b3dca5SArchie Cobbs /* Fragment queue scanner period */
178a9b3dca5SArchie Cobbs #define MP_FRAGTIMER_INTERVAL	(hz/2)
179a9b3dca5SArchie Cobbs 
180fd58342cSAlexander Motin /* Average link overhead. XXX: Should be given by user-level */
181fd58342cSAlexander Motin #define MP_AVERAGE_LINK_OVERHEAD	16
182fd58342cSAlexander Motin 
183164b576eSGleb Smirnoff /* Keep this equal to ng_ppp_hook_names lower! */
184164b576eSGleb Smirnoff #define HOOK_INDEX_MAX		13
185164b576eSGleb Smirnoff 
1863949bee8SArchie Cobbs /* We store incoming fragments this way */
1873949bee8SArchie Cobbs struct ng_ppp_frag {
188a9b3dca5SArchie Cobbs 	int				seq;		/* fragment seq# */
189164b576eSGleb Smirnoff 	uint8_t				first;		/* First in packet? */
190164b576eSGleb Smirnoff 	uint8_t				last;		/* Last in packet? */
191a9b3dca5SArchie Cobbs 	struct timeval			timestamp;	/* time of reception */
192a9b3dca5SArchie Cobbs 	struct mbuf			*data;		/* Fragment data */
1935f90cac7SKirk McKusick 	TAILQ_ENTRY(ng_ppp_frag)	f_qent;		/* Fragment queue */
1943949bee8SArchie Cobbs };
1953949bee8SArchie Cobbs 
196a9b3dca5SArchie Cobbs /* Per-link private information */
197a9b3dca5SArchie Cobbs struct ng_ppp_link {
198a9b3dca5SArchie Cobbs 	struct ng_ppp_link_conf	conf;		/* link configuration */
199164b576eSGleb Smirnoff 	struct ng_ppp_link_stat	stats;		/* link stats */
200a9b3dca5SArchie Cobbs 	hook_p			hook;		/* connection to link data */
2016f16db81SArchie Cobbs 	int32_t			seq;		/* highest rec'd seq# - MSEQ */
202164b576eSGleb Smirnoff 	uint32_t		latency;	/* calculated link latency */
203164b576eSGleb Smirnoff 	struct timeval		lastWrite;	/* time of last write for MP */
204164b576eSGleb Smirnoff 	int			bytesInQueue;	/* bytes in the output queue for MP */
205a9b3dca5SArchie Cobbs };
206a9b3dca5SArchie Cobbs 
207a9b3dca5SArchie Cobbs /* Total per-node private information */
20862838faeSArchie Cobbs struct ng_ppp_private {
209a9b3dca5SArchie Cobbs 	struct ng_ppp_bund_conf	conf;			/* bundle config */
210a9b3dca5SArchie Cobbs 	struct ng_ppp_link_stat	bundleStats;		/* bundle stats */
211a9b3dca5SArchie Cobbs 	struct ng_ppp_link	links[NG_PPP_MAX_LINKS];/* per-link info */
2126f16db81SArchie Cobbs 	int32_t			xseq;			/* next out MP seq # */
2136f16db81SArchie Cobbs 	int32_t			mseq;			/* min links[i].seq */
214164b576eSGleb Smirnoff 	uint16_t		activeLinks[NG_PPP_MAX_LINKS];	/* indicies */
215164b576eSGleb Smirnoff 	uint16_t		numActiveLinks;		/* how many links up */
216164b576eSGleb Smirnoff 	uint16_t		lastLink;		/* for round robin */
217164b576eSGleb Smirnoff 	uint8_t			vjCompHooked;		/* VJ comp hooked up? */
218164b576eSGleb Smirnoff 	uint8_t			allLinksEqual;		/* all xmit the same? */
219a9b3dca5SArchie Cobbs 	hook_p			hooks[HOOK_INDEX_MAX];	/* non-link hooks */
2205f90cac7SKirk McKusick 	TAILQ_HEAD(ng_ppp_fraglist, ng_ppp_frag)	/* fragment queue */
221a9b3dca5SArchie Cobbs 				frags;
222a9b3dca5SArchie Cobbs 	int			qlen;			/* fraq queue length */
2239d952bd3SGleb Smirnoff 	struct callout		fragTimer;		/* fraq queue check */
2244cf49a43SJulian Elischer };
22562838faeSArchie Cobbs typedef struct ng_ppp_private *priv_p;
2264cf49a43SJulian Elischer 
2274cf49a43SJulian Elischer /* Netgraph node methods */
22874f5c6aaSJulian Elischer static ng_constructor_t	ng_ppp_constructor;
22974f5c6aaSJulian Elischer static ng_rcvmsg_t	ng_ppp_rcvmsg;
230069154d5SJulian Elischer static ng_shutdown_t	ng_ppp_shutdown;
23174f5c6aaSJulian Elischer static ng_newhook_t	ng_ppp_newhook;
23274f5c6aaSJulian Elischer static ng_rcvdata_t	ng_ppp_rcvdata;
23374f5c6aaSJulian Elischer static ng_disconnect_t	ng_ppp_disconnect;
2344cf49a43SJulian Elischer 
235164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_inet;
236164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_ipv6;
237164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_ipx;
238164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_atalk;
239164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_bypass;
240164b576eSGleb Smirnoff 
241164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_vjc_ip;
242164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_vjc_comp;
243164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_vjc_uncomp;
244164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_vjc_vjip;
245164b576eSGleb Smirnoff 
246164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_compress;
247164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_decompress;
248164b576eSGleb Smirnoff 
249164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_encrypt;
250164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_decrypt;
251164b576eSGleb Smirnoff 
252164b576eSGleb Smirnoff /* We use integer indicies to refer to the non-link hooks. */
253164b576eSGleb Smirnoff static const struct {
254164b576eSGleb Smirnoff 	char *const name;
255164b576eSGleb Smirnoff 	ng_rcvdata_t *fn;
256164b576eSGleb Smirnoff } ng_ppp_hook_names[] = {
257164b576eSGleb Smirnoff #define HOOK_INDEX_ATALK	0
258164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_ATALK,	ng_ppp_rcvdata_atalk },
259164b576eSGleb Smirnoff #define HOOK_INDEX_BYPASS	1
260164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_BYPASS,	ng_ppp_rcvdata_bypass },
261164b576eSGleb Smirnoff #define HOOK_INDEX_COMPRESS	2
262164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_COMPRESS,	ng_ppp_rcvdata_compress },
263164b576eSGleb Smirnoff #define HOOK_INDEX_ENCRYPT	3
264164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_ENCRYPT,	ng_ppp_rcvdata_encrypt },
265164b576eSGleb Smirnoff #define HOOK_INDEX_DECOMPRESS	4
266164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_DECOMPRESS, ng_ppp_rcvdata_decompress },
267164b576eSGleb Smirnoff #define HOOK_INDEX_DECRYPT	5
268164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_DECRYPT,	ng_ppp_rcvdata_decrypt },
269164b576eSGleb Smirnoff #define HOOK_INDEX_INET		6
270164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_INET,	ng_ppp_rcvdata_inet },
271164b576eSGleb Smirnoff #define HOOK_INDEX_IPX		7
272164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_IPX,	ng_ppp_rcvdata_ipx },
273164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_COMP	8
274164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_VJC_COMP,	ng_ppp_rcvdata_vjc_comp },
275164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_IP	9
276164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_VJC_IP,	ng_ppp_rcvdata_vjc_ip },
277164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_UNCOMP	10
278164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_VJC_UNCOMP, ng_ppp_rcvdata_vjc_uncomp },
279164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_VJIP	11
280164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_VJC_VJIP,	ng_ppp_rcvdata_vjc_vjip },
281164b576eSGleb Smirnoff #define HOOK_INDEX_IPV6		12
282164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_IPV6,	ng_ppp_rcvdata_ipv6 },
283164b576eSGleb Smirnoff 	{ NULL, NULL }
284164b576eSGleb Smirnoff };
285164b576eSGleb Smirnoff 
2863949bee8SArchie Cobbs /* Helper functions */
287164b576eSGleb Smirnoff static int	ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto,
288164b576eSGleb Smirnoff 		    uint16_t linkNum);
289164b576eSGleb Smirnoff static int	ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto);
290164b576eSGleb Smirnoff static int	ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto,
291164b576eSGleb Smirnoff 		    uint16_t linkNum);
292164b576eSGleb Smirnoff static int	ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto);
293164b576eSGleb Smirnoff static int	ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto,
294164b576eSGleb Smirnoff 		    uint16_t linkNum);
295164b576eSGleb Smirnoff static int	ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto);
296164b576eSGleb Smirnoff static int	ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto,
297164b576eSGleb Smirnoff 		    uint16_t linkNum);
298164b576eSGleb Smirnoff static int	ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto);
299164b576eSGleb Smirnoff static int	ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto,
300164b576eSGleb Smirnoff 		    uint16_t linkNum);
301164b576eSGleb Smirnoff static int	ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto,
302164b576eSGleb Smirnoff 		    uint16_t linkNum);
303164b576eSGleb Smirnoff 
30439c14742SGleb Smirnoff static int	ng_ppp_bypass(node_p node, item_p item, uint16_t proto,
30539c14742SGleb Smirnoff 		    uint16_t linkNum);
30639c14742SGleb Smirnoff 
307a9b3dca5SArchie Cobbs static int	ng_ppp_check_packet(node_p node);
3083ca24c28SJulian Elischer static void	ng_ppp_get_packet(node_p node, struct mbuf **mp);
309a9b3dca5SArchie Cobbs static int	ng_ppp_frag_process(node_p node);
310a9b3dca5SArchie Cobbs static int	ng_ppp_frag_trim(node_p node);
3119d952bd3SGleb Smirnoff static void	ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1,
3129d952bd3SGleb Smirnoff 		    int arg2);
313a9b3dca5SArchie Cobbs static void	ng_ppp_frag_checkstale(node_p node);
314a9b3dca5SArchie Cobbs static void	ng_ppp_frag_reset(node_p node);
3153949bee8SArchie Cobbs static void	ng_ppp_mp_strategy(node_p node, int len, int *distrib);
3168250de83SGleb Smirnoff static int	ng_ppp_intcmp(void *latency, const void *v1, const void *v2);
317164b576eSGleb Smirnoff static struct mbuf *ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK);
318164b576eSGleb Smirnoff static struct mbuf *ng_ppp_cutproto(struct mbuf *m, uint16_t *proto);
319d690a6e7SArchie Cobbs static struct mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len);
3203949bee8SArchie Cobbs static int	ng_ppp_config_valid(node_p node,
321a9b3dca5SArchie Cobbs 		    const struct ng_ppp_node_conf *newConf);
3223949bee8SArchie Cobbs static void	ng_ppp_update(node_p node, int newConf);
323a9b3dca5SArchie Cobbs static void	ng_ppp_start_frag_timer(node_p node);
324a9b3dca5SArchie Cobbs static void	ng_ppp_stop_frag_timer(node_p node);
3254cf49a43SJulian Elischer 
3266f16db81SArchie Cobbs /* Parse type for struct ng_ppp_mp_state_type */
3276f16db81SArchie Cobbs static const struct ng_parse_fixedarray_info ng_ppp_rseq_array_info = {
3286f16db81SArchie Cobbs 	&ng_parse_hint32_type,
3296f16db81SArchie Cobbs 	NG_PPP_MAX_LINKS
3306f16db81SArchie Cobbs };
3316f16db81SArchie Cobbs static const struct ng_parse_type ng_ppp_rseq_array_type = {
3326f16db81SArchie Cobbs 	&ng_parse_fixedarray_type,
3336f16db81SArchie Cobbs 	&ng_ppp_rseq_array_info,
3346f16db81SArchie Cobbs };
335f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_mp_state_type_fields[]
3366f16db81SArchie Cobbs 	= NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type);
3376f16db81SArchie Cobbs static const struct ng_parse_type ng_ppp_mp_state_type = {
3386f16db81SArchie Cobbs 	&ng_parse_struct_type,
339f0184ff8SArchie Cobbs 	&ng_ppp_mp_state_type_fields
3406f16db81SArchie Cobbs };
3416f16db81SArchie Cobbs 
342a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_link_conf */
343f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_link_type_fields[]
344f0184ff8SArchie Cobbs 	= NG_PPP_LINK_TYPE_INFO;
345f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_link_type = {
346f8307e12SArchie Cobbs 	&ng_parse_struct_type,
347f0184ff8SArchie Cobbs 	&ng_ppp_link_type_fields
348f8307e12SArchie Cobbs };
349f8307e12SArchie Cobbs 
350a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_bund_conf */
351f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_bund_type_fields[]
352f0184ff8SArchie Cobbs 	= NG_PPP_BUND_TYPE_INFO;
353a9b3dca5SArchie Cobbs static const struct ng_parse_type ng_ppp_bund_type = {
354a9b3dca5SArchie Cobbs 	&ng_parse_struct_type,
355f0184ff8SArchie Cobbs 	&ng_ppp_bund_type_fields
356a9b3dca5SArchie Cobbs };
357a9b3dca5SArchie Cobbs 
358a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_node_conf */
3596f16db81SArchie Cobbs static const struct ng_parse_fixedarray_info ng_ppp_array_info = {
360f8307e12SArchie Cobbs 	&ng_ppp_link_type,
361f8307e12SArchie Cobbs 	NG_PPP_MAX_LINKS
362f8307e12SArchie Cobbs };
363f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_link_array_type = {
364f8307e12SArchie Cobbs 	&ng_parse_fixedarray_type,
365f8307e12SArchie Cobbs 	&ng_ppp_array_info,
366f8307e12SArchie Cobbs };
367f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_conf_type_fields[]
368a9b3dca5SArchie Cobbs 	= NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type);
369a9b3dca5SArchie Cobbs static const struct ng_parse_type ng_ppp_conf_type = {
370f8307e12SArchie Cobbs 	&ng_parse_struct_type,
371f0184ff8SArchie Cobbs 	&ng_ppp_conf_type_fields
372f8307e12SArchie Cobbs };
373f8307e12SArchie Cobbs 
374f8307e12SArchie Cobbs /* Parse type for struct ng_ppp_link_stat */
375f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_stats_type_fields[]
376f0184ff8SArchie Cobbs 	= NG_PPP_STATS_TYPE_INFO;
377f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_stats_type = {
378f8307e12SArchie Cobbs 	&ng_parse_struct_type,
379f0184ff8SArchie Cobbs 	&ng_ppp_stats_type_fields
380f8307e12SArchie Cobbs };
381f8307e12SArchie Cobbs 
382f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
383f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ppp_cmds[] = {
384f8307e12SArchie Cobbs 	{
385f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
386f8307e12SArchie Cobbs 	  NGM_PPP_SET_CONFIG,
387f8307e12SArchie Cobbs 	  "setconfig",
388a9b3dca5SArchie Cobbs 	  &ng_ppp_conf_type,
389f8307e12SArchie Cobbs 	  NULL
390f8307e12SArchie Cobbs 	},
391f8307e12SArchie Cobbs 	{
392f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
393f8307e12SArchie Cobbs 	  NGM_PPP_GET_CONFIG,
394f8307e12SArchie Cobbs 	  "getconfig",
395f8307e12SArchie Cobbs 	  NULL,
396a9b3dca5SArchie Cobbs 	  &ng_ppp_conf_type
397f8307e12SArchie Cobbs 	},
398f8307e12SArchie Cobbs 	{
399f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
4006f16db81SArchie Cobbs 	  NGM_PPP_GET_MP_STATE,
4016f16db81SArchie Cobbs 	  "getmpstate",
4026f16db81SArchie Cobbs 	  NULL,
4036f16db81SArchie Cobbs 	  &ng_ppp_mp_state_type
4046f16db81SArchie Cobbs 	},
4056f16db81SArchie Cobbs 	{
4066f16db81SArchie Cobbs 	  NGM_PPP_COOKIE,
407f8307e12SArchie Cobbs 	  NGM_PPP_GET_LINK_STATS,
408f8307e12SArchie Cobbs 	  "getstats",
409f8307e12SArchie Cobbs 	  &ng_parse_int16_type,
410f8307e12SArchie Cobbs 	  &ng_ppp_stats_type
411f8307e12SArchie Cobbs 	},
412f8307e12SArchie Cobbs 	{
413f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
414f8307e12SArchie Cobbs 	  NGM_PPP_CLR_LINK_STATS,
415f8307e12SArchie Cobbs 	  "clrstats",
416f8307e12SArchie Cobbs 	  &ng_parse_int16_type,
417f8307e12SArchie Cobbs 	  NULL
418f8307e12SArchie Cobbs 	},
419f8307e12SArchie Cobbs 	{
420f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
421f8307e12SArchie Cobbs 	  NGM_PPP_GETCLR_LINK_STATS,
422f8307e12SArchie Cobbs 	  "getclrstats",
423f8307e12SArchie Cobbs 	  &ng_parse_int16_type,
424f8307e12SArchie Cobbs 	  &ng_ppp_stats_type
425f8307e12SArchie Cobbs 	},
426f8307e12SArchie Cobbs 	{ 0 }
427f8307e12SArchie Cobbs };
428f8307e12SArchie Cobbs 
4294cf49a43SJulian Elischer /* Node type descriptor */
4303949bee8SArchie Cobbs static struct ng_type ng_ppp_typestruct = {
431f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
432f8aae777SJulian Elischer 	.name =		NG_PPP_NODE_TYPE,
433f8aae777SJulian Elischer 	.constructor =	ng_ppp_constructor,
434f8aae777SJulian Elischer 	.rcvmsg =	ng_ppp_rcvmsg,
435f8aae777SJulian Elischer 	.shutdown =	ng_ppp_shutdown,
436f8aae777SJulian Elischer 	.newhook =	ng_ppp_newhook,
437f8aae777SJulian Elischer 	.rcvdata =	ng_ppp_rcvdata,
438f8aae777SJulian Elischer 	.disconnect =	ng_ppp_disconnect,
439f8aae777SJulian Elischer 	.cmdlist =	ng_ppp_cmds,
4404cf49a43SJulian Elischer };
4413949bee8SArchie Cobbs NETGRAPH_INIT(ppp, &ng_ppp_typestruct);
4424cf49a43SJulian Elischer 
443d690a6e7SArchie Cobbs /* Address and control field header */
444164b576eSGleb Smirnoff static const uint8_t ng_ppp_acf[2] = { 0xff, 0x03 };
445d690a6e7SArchie Cobbs 
446a9b3dca5SArchie Cobbs /* Maximum time we'll let a complete incoming packet sit in the queue */
447a9b3dca5SArchie Cobbs static const struct timeval ng_ppp_max_staleness = { 2, 0 };	/* 2 seconds */
448a9b3dca5SArchie Cobbs 
4494cf49a43SJulian Elischer #define ERROUT(x)	do { error = (x); goto done; } while (0)
4504cf49a43SJulian Elischer 
4514cf49a43SJulian Elischer /************************************************************************
4524cf49a43SJulian Elischer 			NETGRAPH NODE STUFF
4534cf49a43SJulian Elischer  ************************************************************************/
4544cf49a43SJulian Elischer 
4554cf49a43SJulian Elischer /*
4563949bee8SArchie Cobbs  * Node type constructor
4574cf49a43SJulian Elischer  */
4584cf49a43SJulian Elischer static int
459069154d5SJulian Elischer ng_ppp_constructor(node_p node)
4604cf49a43SJulian Elischer {
4614cf49a43SJulian Elischer 	priv_p priv;
462069154d5SJulian Elischer 	int i;
4634cf49a43SJulian Elischer 
4644cf49a43SJulian Elischer 	/* Allocate private structure */
4659c8c302fSJulian Elischer 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_PPP, M_NOWAIT | M_ZERO);
4664cf49a43SJulian Elischer 	if (priv == NULL)
4674cf49a43SJulian Elischer 		return (ENOMEM);
4684cf49a43SJulian Elischer 
46930400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
4704cf49a43SJulian Elischer 
4713949bee8SArchie Cobbs 	/* Initialize state */
4725f90cac7SKirk McKusick 	TAILQ_INIT(&priv->frags);
473a9b3dca5SArchie Cobbs 	for (i = 0; i < NG_PPP_MAX_LINKS; i++)
474a9b3dca5SArchie Cobbs 		priv->links[i].seq = MP_NOSEQ;
4759d952bd3SGleb Smirnoff 	ng_callout_init(&priv->fragTimer);
4763949bee8SArchie Cobbs 
4774cf49a43SJulian Elischer 	/* Done */
4784cf49a43SJulian Elischer 	return (0);
4794cf49a43SJulian Elischer }
4804cf49a43SJulian Elischer 
4814cf49a43SJulian Elischer /*
4824cf49a43SJulian Elischer  * Give our OK for a hook to be added
4834cf49a43SJulian Elischer  */
4844cf49a43SJulian Elischer static int
4854cf49a43SJulian Elischer ng_ppp_newhook(node_p node, hook_p hook, const char *name)
4864cf49a43SJulian Elischer {
48730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
4883949bee8SArchie Cobbs 	hook_p *hookPtr = NULL;
489164b576eSGleb Smirnoff 	int linkNum = -1;
4903949bee8SArchie Cobbs 	int hookIndex = -1;
4914cf49a43SJulian Elischer 
4923949bee8SArchie Cobbs 	/* Figure out which hook it is */
4933949bee8SArchie Cobbs 	if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX,	/* a link hook? */
4943949bee8SArchie Cobbs 	    strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) {
49525792ef3SArchie Cobbs 		const char *cp;
49625792ef3SArchie Cobbs 		char *eptr;
4973949bee8SArchie Cobbs 
4982b70adcbSArchie Cobbs 		cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
4992b70adcbSArchie Cobbs 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
5004cf49a43SJulian Elischer 			return (EINVAL);
5012b70adcbSArchie Cobbs 		linkNum = (int)strtoul(cp, &eptr, 10);
5022b70adcbSArchie Cobbs 		if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS)
5033949bee8SArchie Cobbs 			return (EINVAL);
504a9b3dca5SArchie Cobbs 		hookPtr = &priv->links[linkNum].hook;
5053949bee8SArchie Cobbs 		hookIndex = ~linkNum;
506164b576eSGleb Smirnoff 
507164b576eSGleb Smirnoff 		/* See if hook is already connected. */
508164b576eSGleb Smirnoff 		if (*hookPtr != NULL)
509164b576eSGleb Smirnoff 			return (EISCONN);
510164b576eSGleb Smirnoff 
511164b576eSGleb Smirnoff 		/* Disallow more than one link unless multilink is enabled. */
512164b576eSGleb Smirnoff 		if (priv->links[linkNum].conf.enableLink &&
513164b576eSGleb Smirnoff 		    !priv->conf.enableMultilink && priv->numActiveLinks >= 1)
514164b576eSGleb Smirnoff 			return (ENODEV);
515164b576eSGleb Smirnoff 
516164b576eSGleb Smirnoff 		/* MP recv code is not thread-safe. */
517164b576eSGleb Smirnoff 		NG_HOOK_FORCE_WRITER(hook);
518164b576eSGleb Smirnoff 
5193949bee8SArchie Cobbs 	} else {				/* must be a non-link hook */
5203949bee8SArchie Cobbs 		int i;
5214cf49a43SJulian Elischer 
522164b576eSGleb Smirnoff 		for (i = 0; ng_ppp_hook_names[i].name != NULL; i++) {
523164b576eSGleb Smirnoff 			if (strcmp(name, ng_ppp_hook_names[i].name) == 0) {
5243949bee8SArchie Cobbs 				hookPtr = &priv->hooks[i];
5253949bee8SArchie Cobbs 				hookIndex = i;
5263949bee8SArchie Cobbs 				break;
5273949bee8SArchie Cobbs 			}
5283949bee8SArchie Cobbs 		}
529164b576eSGleb Smirnoff 		if (ng_ppp_hook_names[i].name == NULL)
5303949bee8SArchie Cobbs 			return (EINVAL);	/* no such hook */
5313949bee8SArchie Cobbs 
5323949bee8SArchie Cobbs 		/* See if hook is already connected */
5333949bee8SArchie Cobbs 		if (*hookPtr != NULL)
5344cf49a43SJulian Elischer 			return (EISCONN);
5354cf49a43SJulian Elischer 
536164b576eSGleb Smirnoff 		/* Every non-linkX hook have it's own function. */
537164b576eSGleb Smirnoff 		NG_HOOK_SET_RCVDATA(hook, ng_ppp_hook_names[i].fn);
538164b576eSGleb Smirnoff 	}
5394cf49a43SJulian Elischer 
5404cf49a43SJulian Elischer 	/* OK */
5413949bee8SArchie Cobbs 	*hookPtr = hook;
5428214d60eSJohn Baldwin 	NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)hookIndex);
5433949bee8SArchie Cobbs 	ng_ppp_update(node, 0);
5444cf49a43SJulian Elischer 	return (0);
5454cf49a43SJulian Elischer }
5464cf49a43SJulian Elischer 
5474cf49a43SJulian Elischer /*
5484cf49a43SJulian Elischer  * Receive a control message
5494cf49a43SJulian Elischer  */
5504cf49a43SJulian Elischer static int
551069154d5SJulian Elischer ng_ppp_rcvmsg(node_p node, item_p item, hook_p lasthook)
5524cf49a43SJulian Elischer {
55330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
5544cf49a43SJulian Elischer 	struct ng_mesg *resp = NULL;
5554cf49a43SJulian Elischer 	int error = 0;
556069154d5SJulian Elischer 	struct ng_mesg *msg;
5574cf49a43SJulian Elischer 
558069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
5594cf49a43SJulian Elischer 	switch (msg->header.typecookie) {
5604cf49a43SJulian Elischer 	case NGM_PPP_COOKIE:
5614cf49a43SJulian Elischer 		switch (msg->header.cmd) {
5623949bee8SArchie Cobbs 		case NGM_PPP_SET_CONFIG:
5633949bee8SArchie Cobbs 		    {
564a9b3dca5SArchie Cobbs 			struct ng_ppp_node_conf *const conf =
565a9b3dca5SArchie Cobbs 			    (struct ng_ppp_node_conf *)msg->data;
566a9b3dca5SArchie Cobbs 			int i;
5673949bee8SArchie Cobbs 
5683949bee8SArchie Cobbs 			/* Check for invalid or illegal config */
569a9b3dca5SArchie Cobbs 			if (msg->header.arglen != sizeof(*conf))
5704cf49a43SJulian Elischer 				ERROUT(EINVAL);
571a9b3dca5SArchie Cobbs 			if (!ng_ppp_config_valid(node, conf))
5723949bee8SArchie Cobbs 				ERROUT(EINVAL);
573a9b3dca5SArchie Cobbs 
574a9b3dca5SArchie Cobbs 			/* Copy config */
575a9b3dca5SArchie Cobbs 			priv->conf = conf->bund;
576a9b3dca5SArchie Cobbs 			for (i = 0; i < NG_PPP_MAX_LINKS; i++)
577a9b3dca5SArchie Cobbs 				priv->links[i].conf = conf->links[i];
5783949bee8SArchie Cobbs 			ng_ppp_update(node, 1);
5794cf49a43SJulian Elischer 			break;
5803949bee8SArchie Cobbs 		    }
5813949bee8SArchie Cobbs 		case NGM_PPP_GET_CONFIG:
582a9b3dca5SArchie Cobbs 		    {
583a9b3dca5SArchie Cobbs 			struct ng_ppp_node_conf *conf;
584a9b3dca5SArchie Cobbs 			int i;
585a9b3dca5SArchie Cobbs 
586a9b3dca5SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
5874cf49a43SJulian Elischer 			if (resp == NULL)
5884cf49a43SJulian Elischer 				ERROUT(ENOMEM);
589a9b3dca5SArchie Cobbs 			conf = (struct ng_ppp_node_conf *)resp->data;
590a9b3dca5SArchie Cobbs 			conf->bund = priv->conf;
591a9b3dca5SArchie Cobbs 			for (i = 0; i < NG_PPP_MAX_LINKS; i++)
592a9b3dca5SArchie Cobbs 				conf->links[i] = priv->links[i].conf;
5934cf49a43SJulian Elischer 			break;
594a9b3dca5SArchie Cobbs 		    }
5956f16db81SArchie Cobbs 		case NGM_PPP_GET_MP_STATE:
5966f16db81SArchie Cobbs 		    {
5976f16db81SArchie Cobbs 			struct ng_ppp_mp_state *info;
5986f16db81SArchie Cobbs 			int i;
5996f16db81SArchie Cobbs 
6006f16db81SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT);
6016f16db81SArchie Cobbs 			if (resp == NULL)
6026f16db81SArchie Cobbs 				ERROUT(ENOMEM);
6036f16db81SArchie Cobbs 			info = (struct ng_ppp_mp_state *)resp->data;
6046f16db81SArchie Cobbs 			bzero(info, sizeof(*info));
6056f16db81SArchie Cobbs 			for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
6066f16db81SArchie Cobbs 				if (priv->links[i].seq != MP_NOSEQ)
6076f16db81SArchie Cobbs 					info->rseq[i] = priv->links[i].seq;
6086f16db81SArchie Cobbs 			}
6096f16db81SArchie Cobbs 			info->mseq = priv->mseq;
6106f16db81SArchie Cobbs 			info->xseq = priv->xseq;
6116f16db81SArchie Cobbs 			break;
6126f16db81SArchie Cobbs 		    }
6133949bee8SArchie Cobbs 		case NGM_PPP_GET_LINK_STATS:
6143949bee8SArchie Cobbs 		case NGM_PPP_CLR_LINK_STATS:
615e149c4e2SArchie Cobbs 		case NGM_PPP_GETCLR_LINK_STATS:
6163949bee8SArchie Cobbs 		    {
6173949bee8SArchie Cobbs 			struct ng_ppp_link_stat *stats;
618164b576eSGleb Smirnoff 			uint16_t linkNum;
6193949bee8SArchie Cobbs 
620164b576eSGleb Smirnoff 			if (msg->header.arglen != sizeof(uint16_t))
6213949bee8SArchie Cobbs 				ERROUT(EINVAL);
622164b576eSGleb Smirnoff 			linkNum = *((uint16_t *) msg->data);
6233949bee8SArchie Cobbs 			if (linkNum >= NG_PPP_MAX_LINKS
6243949bee8SArchie Cobbs 			    && linkNum != NG_PPP_BUNDLE_LINKNUM)
6253949bee8SArchie Cobbs 				ERROUT(EINVAL);
6263949bee8SArchie Cobbs 			stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ?
627a9b3dca5SArchie Cobbs 			    &priv->bundleStats : &priv->links[linkNum].stats;
628e149c4e2SArchie Cobbs 			if (msg->header.cmd != NGM_PPP_CLR_LINK_STATS) {
6293949bee8SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
6303949bee8SArchie Cobbs 				    sizeof(struct ng_ppp_link_stat), M_NOWAIT);
6313949bee8SArchie Cobbs 				if (resp == NULL)
6323949bee8SArchie Cobbs 					ERROUT(ENOMEM);
6333949bee8SArchie Cobbs 				bcopy(stats, resp->data, sizeof(*stats));
634e149c4e2SArchie Cobbs 			}
635e149c4e2SArchie Cobbs 			if (msg->header.cmd != NGM_PPP_GET_LINK_STATS)
6363949bee8SArchie Cobbs 				bzero(stats, sizeof(*stats));
6374cf49a43SJulian Elischer 			break;
6383949bee8SArchie Cobbs 		    }
6394cf49a43SJulian Elischer 		default:
6404cf49a43SJulian Elischer 			error = EINVAL;
6414cf49a43SJulian Elischer 			break;
6424cf49a43SJulian Elischer 		}
6434cf49a43SJulian Elischer 		break;
6443949bee8SArchie Cobbs 	case NGM_VJC_COOKIE:
6453949bee8SArchie Cobbs 	    {
646069154d5SJulian Elischer 		/*
647069154d5SJulian Elischer 		 * Forward it to the vjc node. leave the
648069154d5SJulian Elischer 		 * old return address alone.
64930400f03SJulian Elischer 		 * If we have no hook, let NG_RESPOND_MSG
65030400f03SJulian Elischer 		 * clean up any remaining resources.
65130400f03SJulian Elischer 		 * Because we have no resp, the item will be freed
65230400f03SJulian Elischer 		 * along with anything it references. Don't
65330400f03SJulian Elischer 		 * let msg be freed twice.
654069154d5SJulian Elischer 		 */
655069154d5SJulian Elischer 		NGI_MSG(item) = msg;	/* put it back in the item */
65630400f03SJulian Elischer 		msg = NULL;
657164b576eSGleb Smirnoff 		if ((lasthook = priv->hooks[HOOK_INDEX_VJC_IP])) {
65830400f03SJulian Elischer 			NG_FWD_ITEM_HOOK(error, item, lasthook);
659069154d5SJulian Elischer 		}
660069154d5SJulian Elischer 		return (error);
6613949bee8SArchie Cobbs 	    }
6624cf49a43SJulian Elischer 	default:
6634cf49a43SJulian Elischer 		error = EINVAL;
6644cf49a43SJulian Elischer 		break;
6654cf49a43SJulian Elischer 	}
6664cf49a43SJulian Elischer done:
667069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
668069154d5SJulian Elischer 	NG_FREE_MSG(msg);
6694cf49a43SJulian Elischer 	return (error);
6704cf49a43SJulian Elischer }
6714cf49a43SJulian Elischer 
6724cf49a43SJulian Elischer /*
6734cf49a43SJulian Elischer  * Destroy node
6744cf49a43SJulian Elischer  */
6754cf49a43SJulian Elischer static int
676069154d5SJulian Elischer ng_ppp_shutdown(node_p node)
6774cf49a43SJulian Elischer {
67830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
6794cf49a43SJulian Elischer 
680a9b3dca5SArchie Cobbs 	/* Stop fragment queue timer */
681a9b3dca5SArchie Cobbs 	ng_ppp_stop_frag_timer(node);
682a9b3dca5SArchie Cobbs 
6834cf49a43SJulian Elischer 	/* Take down netgraph node */
684a9b3dca5SArchie Cobbs 	ng_ppp_frag_reset(node);
6854cf49a43SJulian Elischer 	bzero(priv, sizeof(*priv));
6869c8c302fSJulian Elischer 	FREE(priv, M_NETGRAPH_PPP);
68730400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, NULL);
68830400f03SJulian Elischer 	NG_NODE_UNREF(node);		/* let the node escape */
6894cf49a43SJulian Elischer 	return (0);
6904cf49a43SJulian Elischer }
6914cf49a43SJulian Elischer 
6924cf49a43SJulian Elischer /*
6934cf49a43SJulian Elischer  * Hook disconnection
6944cf49a43SJulian Elischer  */
6954cf49a43SJulian Elischer static int
6964cf49a43SJulian Elischer ng_ppp_disconnect(hook_p hook)
6974cf49a43SJulian Elischer {
69830400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
69930400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
7008214d60eSJohn Baldwin 	const int index = (intptr_t)NG_HOOK_PRIVATE(hook);
70162838faeSArchie Cobbs 
70262838faeSArchie Cobbs 	/* Zero out hook pointer */
70362838faeSArchie Cobbs 	if (index < 0)
704a9b3dca5SArchie Cobbs 		priv->links[~index].hook = NULL;
70562838faeSArchie Cobbs 	else
70662838faeSArchie Cobbs 		priv->hooks[index] = NULL;
70762838faeSArchie Cobbs 
708164b576eSGleb Smirnoff 	/* Update derived info (or go away if no hooks left). */
709164b576eSGleb Smirnoff 	if (NG_NODE_NUMHOOKS(node) > 0)
71062838faeSArchie Cobbs 		ng_ppp_update(node, 0);
711164b576eSGleb Smirnoff 	else if (NG_NODE_IS_VALID(node))
712069154d5SJulian Elischer 		ng_rmnode_self(node);
713164b576eSGleb Smirnoff 
7144cf49a43SJulian Elischer 	return (0);
7154cf49a43SJulian Elischer }
7164cf49a43SJulian Elischer 
717164b576eSGleb Smirnoff /*
718164b576eSGleb Smirnoff  * Proto layer
719164b576eSGleb Smirnoff  */
7204cf49a43SJulian Elischer 
7214cf49a43SJulian Elischer /*
722164b576eSGleb Smirnoff  * Receive data on a hook inet.
7234cf49a43SJulian Elischer  */
7244cf49a43SJulian Elischer static int
725164b576eSGleb Smirnoff ng_ppp_rcvdata_inet(hook_p hook, item_p item)
726164b576eSGleb Smirnoff {
727164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
728164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
729164b576eSGleb Smirnoff 
730164b576eSGleb Smirnoff 	if (!priv->conf.enableIP) {
731164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
732164b576eSGleb Smirnoff 		return (ENXIO);
733164b576eSGleb Smirnoff 	}
734164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IP));
735164b576eSGleb Smirnoff }
736164b576eSGleb Smirnoff 
737164b576eSGleb Smirnoff /*
738164b576eSGleb Smirnoff  * Receive data on a hook ipv6.
739164b576eSGleb Smirnoff  */
740164b576eSGleb Smirnoff static int
741164b576eSGleb Smirnoff ng_ppp_rcvdata_ipv6(hook_p hook, item_p item)
742164b576eSGleb Smirnoff {
743164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
744164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
745164b576eSGleb Smirnoff 
746164b576eSGleb Smirnoff 	if (!priv->conf.enableIPv6) {
747164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
748164b576eSGleb Smirnoff 		return (ENXIO);
749164b576eSGleb Smirnoff 	}
750164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPV6));
751164b576eSGleb Smirnoff }
752164b576eSGleb Smirnoff 
753164b576eSGleb Smirnoff /*
754164b576eSGleb Smirnoff  * Receive data on a hook atalk.
755164b576eSGleb Smirnoff  */
756164b576eSGleb Smirnoff static int
757164b576eSGleb Smirnoff ng_ppp_rcvdata_atalk(hook_p hook, item_p item)
758164b576eSGleb Smirnoff {
759164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
760164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
761164b576eSGleb Smirnoff 
762164b576eSGleb Smirnoff 	if (!priv->conf.enableAtalk) {
763164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
764164b576eSGleb Smirnoff 		return (ENXIO);
765164b576eSGleb Smirnoff 	}
766164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_ATALK));
767164b576eSGleb Smirnoff }
768164b576eSGleb Smirnoff 
769164b576eSGleb Smirnoff /*
770164b576eSGleb Smirnoff  * Receive data on a hook ipx
771164b576eSGleb Smirnoff  */
772164b576eSGleb Smirnoff static int
773164b576eSGleb Smirnoff ng_ppp_rcvdata_ipx(hook_p hook, item_p item)
774164b576eSGleb Smirnoff {
775164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
776164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
777164b576eSGleb Smirnoff 
778164b576eSGleb Smirnoff 	if (!priv->conf.enableIPX) {
779164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
780164b576eSGleb Smirnoff 		return (ENXIO);
781164b576eSGleb Smirnoff 	}
782164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPX));
783164b576eSGleb Smirnoff }
784164b576eSGleb Smirnoff 
785164b576eSGleb Smirnoff /*
786164b576eSGleb Smirnoff  * Receive data on a hook bypass
787164b576eSGleb Smirnoff  */
788164b576eSGleb Smirnoff static int
789164b576eSGleb Smirnoff ng_ppp_rcvdata_bypass(hook_p hook, item_p item)
790164b576eSGleb Smirnoff {
791164b576eSGleb Smirnoff 	uint16_t linkNum;
792164b576eSGleb Smirnoff 	uint16_t proto;
793164b576eSGleb Smirnoff 	struct mbuf *m;
794164b576eSGleb Smirnoff 
795164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
796164b576eSGleb Smirnoff 	if (m->m_pkthdr.len < 4) {
797164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
798164b576eSGleb Smirnoff 		return (EINVAL);
799164b576eSGleb Smirnoff 	}
800164b576eSGleb Smirnoff 	if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
801164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
802164b576eSGleb Smirnoff 		return (ENOBUFS);
803164b576eSGleb Smirnoff 	}
804164b576eSGleb Smirnoff 	linkNum = ntohs(mtod(m, uint16_t *)[0]);
805164b576eSGleb Smirnoff 	proto = ntohs(mtod(m, uint16_t *)[1]);
806164b576eSGleb Smirnoff 	m_adj(m, 4);
807164b576eSGleb Smirnoff 	NGI_M(item) = m;
808164b576eSGleb Smirnoff 
809164b576eSGleb Smirnoff 	if (linkNum == NG_PPP_BUNDLE_LINKNUM)
810164b576eSGleb Smirnoff 		return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, proto));
811164b576eSGleb Smirnoff 	else
812164b576eSGleb Smirnoff 		return (ng_ppp_link_xmit(NG_HOOK_NODE(hook), item, proto,
813164b576eSGleb Smirnoff 		    linkNum));
814164b576eSGleb Smirnoff }
815164b576eSGleb Smirnoff 
816164b576eSGleb Smirnoff static int
81739c14742SGleb Smirnoff ng_ppp_bypass(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
81839c14742SGleb Smirnoff {
81939c14742SGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
82039c14742SGleb Smirnoff 	uint16_t hdr[2];
82139c14742SGleb Smirnoff 	struct mbuf *m;
82239c14742SGleb Smirnoff 	int error;
82339c14742SGleb Smirnoff 
82439c14742SGleb Smirnoff 	if (priv->hooks[HOOK_INDEX_BYPASS] == NULL) {
82539c14742SGleb Smirnoff 	    NG_FREE_ITEM(item);
82639c14742SGleb Smirnoff 	    return (ENXIO);
82739c14742SGleb Smirnoff 	}
82839c14742SGleb Smirnoff 
82939c14742SGleb Smirnoff 	/* Add 4-byte bypass header. */
83039c14742SGleb Smirnoff 	hdr[0] = htons(linkNum);
83139c14742SGleb Smirnoff 	hdr[1] = htons(proto);
83239c14742SGleb Smirnoff 
83339c14742SGleb Smirnoff 	NGI_GET_M(item, m);
83439c14742SGleb Smirnoff 	if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) {
83539c14742SGleb Smirnoff 		NG_FREE_ITEM(item);
83639c14742SGleb Smirnoff 		return (ENOBUFS);
83739c14742SGleb Smirnoff 	}
83839c14742SGleb Smirnoff 	NGI_M(item) = m;
83939c14742SGleb Smirnoff 
84039c14742SGleb Smirnoff 	/* Send packet out hook. */
84139c14742SGleb Smirnoff 	NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_BYPASS]);
84239c14742SGleb Smirnoff 	return (error);
84339c14742SGleb Smirnoff }
84439c14742SGleb Smirnoff 
84539c14742SGleb Smirnoff static int
846164b576eSGleb Smirnoff ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
8474cf49a43SJulian Elischer {
84830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
8493949bee8SArchie Cobbs 	hook_p outHook = NULL;
850164b576eSGleb Smirnoff 	int error;
8514cf49a43SJulian Elischer 
852ccb07cc3SGleb Smirnoff 	switch (proto) {
8533949bee8SArchie Cobbs 	    case PROT_IP:
8543949bee8SArchie Cobbs 		if (priv->conf.enableIP)
8553949bee8SArchie Cobbs 		    outHook = priv->hooks[HOOK_INDEX_INET];
8563949bee8SArchie Cobbs 		break;
857a9b3dca5SArchie Cobbs 	    case PROT_IPV6:
858a9b3dca5SArchie Cobbs 		if (priv->conf.enableIPv6)
859a9b3dca5SArchie Cobbs 		    outHook = priv->hooks[HOOK_INDEX_IPV6];
860a9b3dca5SArchie Cobbs 		break;
861164b576eSGleb Smirnoff 	    case PROT_ATALK:
862164b576eSGleb Smirnoff 		if (priv->conf.enableAtalk)
863164b576eSGleb Smirnoff 		    outHook = priv->hooks[HOOK_INDEX_ATALK];
864164b576eSGleb Smirnoff 		break;
865164b576eSGleb Smirnoff 	    case PROT_IPX:
866164b576eSGleb Smirnoff 		if (priv->conf.enableIPX)
867164b576eSGleb Smirnoff 		    outHook = priv->hooks[HOOK_INDEX_IPX];
868164b576eSGleb Smirnoff 		break;
869ccb07cc3SGleb Smirnoff 	}
8703949bee8SArchie Cobbs 
87139c14742SGleb Smirnoff 	if (outHook == NULL)
87239c14742SGleb Smirnoff 		return (ng_ppp_bypass(node, item, proto, linkNum));
873d690a6e7SArchie Cobbs 
874164b576eSGleb Smirnoff 	/* Send packet out hook. */
875164b576eSGleb Smirnoff 	NG_FWD_ITEM_HOOK(error, item, outHook);
8763949bee8SArchie Cobbs 	return (error);
8774cf49a43SJulian Elischer }
8784cf49a43SJulian Elischer 
8794cf49a43SJulian Elischer /*
880164b576eSGleb Smirnoff  * Header compression layer
881164b576eSGleb Smirnoff  */
882164b576eSGleb Smirnoff 
883164b576eSGleb Smirnoff static int
884164b576eSGleb Smirnoff ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto)
885164b576eSGleb Smirnoff {
886164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
887164b576eSGleb Smirnoff 
888164b576eSGleb Smirnoff 	if (proto == PROT_IP &&
889164b576eSGleb Smirnoff 	    priv->conf.enableVJCompression &&
890164b576eSGleb Smirnoff 	    priv->vjCompHooked) {
891164b576eSGleb Smirnoff 		int error;
892164b576eSGleb Smirnoff 
893164b576eSGleb Smirnoff 		/* Send packet out hook. */
894164b576eSGleb Smirnoff 		NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_VJC_IP]);
895164b576eSGleb Smirnoff 		return (error);
896164b576eSGleb Smirnoff 	}
897164b576eSGleb Smirnoff 
898164b576eSGleb Smirnoff 	return (ng_ppp_comp_xmit(node, item, proto));
899164b576eSGleb Smirnoff }
900164b576eSGleb Smirnoff 
901164b576eSGleb Smirnoff /*
902164b576eSGleb Smirnoff  * Receive data on a hook vjc_comp.
9034cf49a43SJulian Elischer  */
9043949bee8SArchie Cobbs static int
905164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_comp(hook_p hook, item_p item)
906164b576eSGleb Smirnoff {
907164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
908164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
909164b576eSGleb Smirnoff 
910164b576eSGleb Smirnoff 	if (!priv->conf.enableVJCompression) {
911164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
912164b576eSGleb Smirnoff 		return (ENXIO);
913164b576eSGleb Smirnoff 	}
914164b576eSGleb Smirnoff 	return (ng_ppp_comp_xmit(node, item, PROT_VJCOMP));
915164b576eSGleb Smirnoff }
916164b576eSGleb Smirnoff 
917164b576eSGleb Smirnoff /*
918164b576eSGleb Smirnoff  * Receive data on a hook vjc_uncomp.
919164b576eSGleb Smirnoff  */
920164b576eSGleb Smirnoff static int
921164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_uncomp(hook_p hook, item_p item)
922164b576eSGleb Smirnoff {
923164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
924164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
925164b576eSGleb Smirnoff 
926164b576eSGleb Smirnoff 	if (!priv->conf.enableVJCompression) {
927164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
928164b576eSGleb Smirnoff 		return (ENXIO);
929164b576eSGleb Smirnoff 	}
930164b576eSGleb Smirnoff 	return (ng_ppp_comp_xmit(node, item, PROT_VJUNCOMP));
931164b576eSGleb Smirnoff }
932164b576eSGleb Smirnoff 
933164b576eSGleb Smirnoff /*
934164b576eSGleb Smirnoff  * Receive data on a hook vjc_vjip.
935164b576eSGleb Smirnoff  */
936164b576eSGleb Smirnoff static int
937164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_vjip(hook_p hook, item_p item)
938164b576eSGleb Smirnoff {
939164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
940164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
941164b576eSGleb Smirnoff 
942164b576eSGleb Smirnoff 	if (!priv->conf.enableVJCompression) {
943164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
944164b576eSGleb Smirnoff 		return (ENXIO);
945164b576eSGleb Smirnoff 	}
946164b576eSGleb Smirnoff 	return (ng_ppp_comp_xmit(node, item, PROT_IP));
947164b576eSGleb Smirnoff }
948164b576eSGleb Smirnoff 
949164b576eSGleb Smirnoff static int
950164b576eSGleb Smirnoff ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
951164b576eSGleb Smirnoff {
952164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
953164b576eSGleb Smirnoff 
954164b576eSGleb Smirnoff 	if (priv->conf.enableVJDecompression && priv->vjCompHooked) {
955164b576eSGleb Smirnoff 		hook_p outHook = NULL;
956164b576eSGleb Smirnoff 
957164b576eSGleb Smirnoff 		switch (proto) {
958164b576eSGleb Smirnoff 		    case PROT_VJCOMP:
959164b576eSGleb Smirnoff 			outHook = priv->hooks[HOOK_INDEX_VJC_COMP];
960164b576eSGleb Smirnoff 			break;
961164b576eSGleb Smirnoff 		    case PROT_VJUNCOMP:
962164b576eSGleb Smirnoff 			outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP];
963164b576eSGleb Smirnoff 			break;
964164b576eSGleb Smirnoff 		}
965164b576eSGleb Smirnoff 
966164b576eSGleb Smirnoff 		if (outHook) {
96739c14742SGleb Smirnoff 			int error;
96839c14742SGleb Smirnoff 
969164b576eSGleb Smirnoff 			/* Send packet out hook. */
970164b576eSGleb Smirnoff 			NG_FWD_ITEM_HOOK(error, item, outHook);
971164b576eSGleb Smirnoff 			return (error);
972164b576eSGleb Smirnoff 		}
973164b576eSGleb Smirnoff 	}
974164b576eSGleb Smirnoff 
975164b576eSGleb Smirnoff 	return (ng_ppp_proto_recv(node, item, proto, linkNum));
976164b576eSGleb Smirnoff }
977164b576eSGleb Smirnoff 
978164b576eSGleb Smirnoff /*
979164b576eSGleb Smirnoff  * Receive data on a hook vjc_ip.
980164b576eSGleb Smirnoff  */
981164b576eSGleb Smirnoff static int
982164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_ip(hook_p hook, item_p item)
983164b576eSGleb Smirnoff {
984164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
985164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
986164b576eSGleb Smirnoff 
987164b576eSGleb Smirnoff 	if (!priv->conf.enableVJCompression) {
988164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
989164b576eSGleb Smirnoff 		return (ENXIO);
990164b576eSGleb Smirnoff 	}
991164b576eSGleb Smirnoff 	return (ng_ppp_proto_recv(node, item, PROT_IP, NG_PPP_BUNDLE_LINKNUM));
992164b576eSGleb Smirnoff }
993164b576eSGleb Smirnoff 
994164b576eSGleb Smirnoff /*
995164b576eSGleb Smirnoff  * Compression layer
996164b576eSGleb Smirnoff  */
997164b576eSGleb Smirnoff 
998164b576eSGleb Smirnoff static int
999164b576eSGleb Smirnoff ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto)
1000164b576eSGleb Smirnoff {
1001164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1002164b576eSGleb Smirnoff 
1003164b576eSGleb Smirnoff 	if (priv->conf.enableCompression &&
1004164b576eSGleb Smirnoff 	    proto < 0x4000 &&
1005164b576eSGleb Smirnoff 	    proto != PROT_COMPD &&
1006164b576eSGleb Smirnoff 	    proto != PROT_CRYPTD &&
1007164b576eSGleb Smirnoff 	    priv->hooks[HOOK_INDEX_COMPRESS] != NULL) {
1008164b576eSGleb Smirnoff 	        struct mbuf *m;
1009164b576eSGleb Smirnoff 		int error;
1010164b576eSGleb Smirnoff 
1011164b576eSGleb Smirnoff 	        NGI_GET_M(item, m);
1012164b576eSGleb Smirnoff 		if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) {
1013164b576eSGleb Smirnoff 			NG_FREE_ITEM(item);
1014164b576eSGleb Smirnoff 			return (ENOBUFS);
1015164b576eSGleb Smirnoff 		}
1016164b576eSGleb Smirnoff 		NGI_M(item) = m;
1017164b576eSGleb Smirnoff 
1018164b576eSGleb Smirnoff 		/* Send packet out hook. */
1019164b576eSGleb Smirnoff 		NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_COMPRESS]);
1020164b576eSGleb Smirnoff 		return (error);
1021164b576eSGleb Smirnoff 	}
1022164b576eSGleb Smirnoff 
1023164b576eSGleb Smirnoff 	return (ng_ppp_crypt_xmit(node, item, proto));
1024164b576eSGleb Smirnoff }
1025164b576eSGleb Smirnoff 
1026164b576eSGleb Smirnoff /*
1027164b576eSGleb Smirnoff  * Receive data on a hook compress.
1028164b576eSGleb Smirnoff  */
1029164b576eSGleb Smirnoff static int
1030164b576eSGleb Smirnoff ng_ppp_rcvdata_compress(hook_p hook, item_p item)
1031164b576eSGleb Smirnoff {
1032164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
1033164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1034164b576eSGleb Smirnoff 	uint16_t proto;
1035164b576eSGleb Smirnoff 
1036164b576eSGleb Smirnoff 	switch (priv->conf.enableCompression) {
1037164b576eSGleb Smirnoff 	    case NG_PPP_COMPRESS_NONE:
1038164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1039164b576eSGleb Smirnoff 		return (ENXIO);
1040164b576eSGleb Smirnoff 	    case NG_PPP_COMPRESS_FULL:
1041164b576eSGleb Smirnoff 		{
1042164b576eSGleb Smirnoff 			struct mbuf *m;
1043164b576eSGleb Smirnoff 
1044164b576eSGleb Smirnoff 			NGI_GET_M(item, m);
1045164b576eSGleb Smirnoff 			if ((m = ng_ppp_cutproto(m, &proto)) == NULL) {
1046164b576eSGleb Smirnoff 				NG_FREE_ITEM(item);
1047164b576eSGleb Smirnoff 				return (EIO);
1048164b576eSGleb Smirnoff 			}
1049164b576eSGleb Smirnoff 			NGI_M(item) = m;
1050164b576eSGleb Smirnoff 			if (!PROT_VALID(proto)) {
1051164b576eSGleb Smirnoff 				NG_FREE_ITEM(item);
1052164b576eSGleb Smirnoff 				return (EIO);
1053164b576eSGleb Smirnoff 			}
1054164b576eSGleb Smirnoff 		}
1055164b576eSGleb Smirnoff 		break;
1056164b576eSGleb Smirnoff 	    default:
1057164b576eSGleb Smirnoff 		proto = PROT_COMPD;
1058164b576eSGleb Smirnoff 		break;
1059164b576eSGleb Smirnoff 	}
1060164b576eSGleb Smirnoff 	return (ng_ppp_crypt_xmit(node, item, proto));
1061164b576eSGleb Smirnoff }
1062164b576eSGleb Smirnoff 
1063164b576eSGleb Smirnoff static int
1064164b576eSGleb Smirnoff ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
1065164b576eSGleb Smirnoff {
1066164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1067164b576eSGleb Smirnoff 
1068164b576eSGleb Smirnoff 	if (proto < 0x4000 &&
1069164b576eSGleb Smirnoff 	    ((proto == PROT_COMPD && priv->conf.enableDecompression) ||
1070164b576eSGleb Smirnoff 	    priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) &&
1071164b576eSGleb Smirnoff 	    priv->hooks[HOOK_INDEX_DECOMPRESS] != NULL) {
1072164b576eSGleb Smirnoff 		int error;
1073164b576eSGleb Smirnoff 
1074164b576eSGleb Smirnoff 		if (priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) {
1075164b576eSGleb Smirnoff 			struct mbuf *m;
1076164b576eSGleb Smirnoff 			NGI_GET_M(item, m);
1077164b576eSGleb Smirnoff 			if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) {
1078164b576eSGleb Smirnoff 				NG_FREE_ITEM(item);
1079164b576eSGleb Smirnoff 				return (EIO);
1080164b576eSGleb Smirnoff 			}
1081164b576eSGleb Smirnoff 			NGI_M(item) = m;
1082164b576eSGleb Smirnoff 		}
1083164b576eSGleb Smirnoff 
1084164b576eSGleb Smirnoff 		/* Send packet out hook. */
1085164b576eSGleb Smirnoff 		NG_FWD_ITEM_HOOK(error, item,
1086164b576eSGleb Smirnoff 		    priv->hooks[HOOK_INDEX_DECOMPRESS]);
1087164b576eSGleb Smirnoff 		return (error);
108839c14742SGleb Smirnoff 	} else if (proto == PROT_COMPD) {
108939c14742SGleb Smirnoff 		/* Disabled protos MUST be silently discarded, but
109039c14742SGleb Smirnoff 		 * unsupported MUST not. Let user-level decide this. */
109139c14742SGleb Smirnoff 		return (ng_ppp_bypass(node, item, proto, linkNum));
1092164b576eSGleb Smirnoff 	}
1093164b576eSGleb Smirnoff 
1094164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_recv(node, item, proto, linkNum));
1095164b576eSGleb Smirnoff }
1096164b576eSGleb Smirnoff 
1097164b576eSGleb Smirnoff /*
1098164b576eSGleb Smirnoff  * Receive data on a hook decompress.
1099164b576eSGleb Smirnoff  */
1100164b576eSGleb Smirnoff static int
1101164b576eSGleb Smirnoff ng_ppp_rcvdata_decompress(hook_p hook, item_p item)
1102164b576eSGleb Smirnoff {
1103164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
1104164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1105164b576eSGleb Smirnoff 	uint16_t proto;
1106164b576eSGleb Smirnoff 	struct mbuf *m;
1107164b576eSGleb Smirnoff 
1108164b576eSGleb Smirnoff 	if (!priv->conf.enableDecompression) {
1109164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1110164b576eSGleb Smirnoff 		return (ENXIO);
1111164b576eSGleb Smirnoff 	}
1112164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
1113164b576eSGleb Smirnoff 	if ((m = ng_ppp_cutproto(m, &proto)) == NULL) {
1114164b576eSGleb Smirnoff 	        NG_FREE_ITEM(item);
1115164b576eSGleb Smirnoff 	        return (EIO);
1116164b576eSGleb Smirnoff 	}
1117164b576eSGleb Smirnoff 	NGI_M(item) = m;
1118164b576eSGleb Smirnoff 	if (!PROT_VALID(proto)) {
1119164b576eSGleb Smirnoff 		priv->bundleStats.badProtos++;
1120164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1121164b576eSGleb Smirnoff 		return (EIO);
1122164b576eSGleb Smirnoff 	}
1123164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM));
1124164b576eSGleb Smirnoff }
1125164b576eSGleb Smirnoff 
1126164b576eSGleb Smirnoff /*
1127164b576eSGleb Smirnoff  * Encryption layer
1128164b576eSGleb Smirnoff  */
1129164b576eSGleb Smirnoff 
1130164b576eSGleb Smirnoff static int
1131164b576eSGleb Smirnoff ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto)
1132164b576eSGleb Smirnoff {
1133164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1134164b576eSGleb Smirnoff 
1135164b576eSGleb Smirnoff 	if (priv->conf.enableEncryption &&
1136164b576eSGleb Smirnoff 	    proto < 0x4000 &&
1137164b576eSGleb Smirnoff 	    proto != PROT_CRYPTD &&
1138164b576eSGleb Smirnoff 	    priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) {
1139164b576eSGleb Smirnoff 		struct mbuf *m;
1140164b576eSGleb Smirnoff 		int error;
1141164b576eSGleb Smirnoff 
1142164b576eSGleb Smirnoff 	        NGI_GET_M(item, m);
1143164b576eSGleb Smirnoff 		if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) {
1144164b576eSGleb Smirnoff 			NG_FREE_ITEM(item);
1145164b576eSGleb Smirnoff 			return (ENOBUFS);
1146164b576eSGleb Smirnoff 		}
1147164b576eSGleb Smirnoff 		NGI_M(item) = m;
1148164b576eSGleb Smirnoff 
1149164b576eSGleb Smirnoff 		/* Send packet out hook. */
1150164b576eSGleb Smirnoff 		NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_ENCRYPT]);
1151164b576eSGleb Smirnoff 		return (error);
1152164b576eSGleb Smirnoff 	}
1153164b576eSGleb Smirnoff 
1154164b576eSGleb Smirnoff 	return (ng_ppp_mp_xmit(node, item, proto));
1155164b576eSGleb Smirnoff }
1156164b576eSGleb Smirnoff 
1157164b576eSGleb Smirnoff /*
1158164b576eSGleb Smirnoff  * Receive data on a hook encrypt.
1159164b576eSGleb Smirnoff  */
1160164b576eSGleb Smirnoff static int
1161164b576eSGleb Smirnoff ng_ppp_rcvdata_encrypt(hook_p hook, item_p item)
1162164b576eSGleb Smirnoff {
1163164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
1164164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1165164b576eSGleb Smirnoff 
1166164b576eSGleb Smirnoff 	if (!priv->conf.enableEncryption) {
1167164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1168164b576eSGleb Smirnoff 		return (ENXIO);
1169164b576eSGleb Smirnoff 	}
1170164b576eSGleb Smirnoff 	return (ng_ppp_mp_xmit(node, item, PROT_CRYPTD));
1171164b576eSGleb Smirnoff }
1172164b576eSGleb Smirnoff 
1173164b576eSGleb Smirnoff static int
1174164b576eSGleb Smirnoff ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
1175164b576eSGleb Smirnoff {
1176164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1177164b576eSGleb Smirnoff 
1178164b576eSGleb Smirnoff 	/* Stats */
1179164b576eSGleb Smirnoff 	priv->bundleStats.recvFrames++;
1180164b576eSGleb Smirnoff 	priv->bundleStats.recvOctets += NGI_M(item)->m_pkthdr.len;
1181164b576eSGleb Smirnoff 
118239c14742SGleb Smirnoff 	if (proto == PROT_CRYPTD) {
118339c14742SGleb Smirnoff 		if (priv->conf.enableDecryption &&
1184164b576eSGleb Smirnoff 		    priv->hooks[HOOK_INDEX_DECRYPT] != NULL) {
1185164b576eSGleb Smirnoff 			int error;
1186164b576eSGleb Smirnoff 
1187164b576eSGleb Smirnoff 			/* Send packet out hook. */
118839c14742SGleb Smirnoff 			NG_FWD_ITEM_HOOK(error, item,
118939c14742SGleb Smirnoff 			    priv->hooks[HOOK_INDEX_DECRYPT]);
1190164b576eSGleb Smirnoff 			return (error);
119139c14742SGleb Smirnoff 		} else {
119239c14742SGleb Smirnoff 			/* Disabled protos MUST be silently discarded, but
119339c14742SGleb Smirnoff 			 * unsupported MUST not. Let user-level decide this. */
119439c14742SGleb Smirnoff 			return (ng_ppp_bypass(node, item, proto, linkNum));
119539c14742SGleb Smirnoff 		}
1196164b576eSGleb Smirnoff 	}
1197164b576eSGleb Smirnoff 
1198164b576eSGleb Smirnoff 	return (ng_ppp_comp_recv(node, item, proto, linkNum));
1199164b576eSGleb Smirnoff }
1200164b576eSGleb Smirnoff 
1201164b576eSGleb Smirnoff /*
1202164b576eSGleb Smirnoff  * Receive data on a hook decrypt.
1203164b576eSGleb Smirnoff  */
1204164b576eSGleb Smirnoff static int
1205164b576eSGleb Smirnoff ng_ppp_rcvdata_decrypt(hook_p hook, item_p item)
1206164b576eSGleb Smirnoff {
1207164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
1208164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1209164b576eSGleb Smirnoff 	uint16_t proto;
1210164b576eSGleb Smirnoff 	struct mbuf *m;
1211164b576eSGleb Smirnoff 
1212164b576eSGleb Smirnoff 	if (!priv->conf.enableDecryption) {
1213164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1214164b576eSGleb Smirnoff 		return (ENXIO);
1215164b576eSGleb Smirnoff 	}
1216164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
1217164b576eSGleb Smirnoff 	if ((m = ng_ppp_cutproto(m, &proto)) == NULL) {
1218164b576eSGleb Smirnoff 	        NG_FREE_ITEM(item);
1219164b576eSGleb Smirnoff 	        return (EIO);
1220164b576eSGleb Smirnoff 	}
1221164b576eSGleb Smirnoff 	NGI_M(item) = m;
1222164b576eSGleb Smirnoff 	if (!PROT_VALID(proto)) {
1223164b576eSGleb Smirnoff 		priv->bundleStats.badProtos++;
1224164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1225164b576eSGleb Smirnoff 		return (EIO);
1226164b576eSGleb Smirnoff 	}
1227164b576eSGleb Smirnoff 	return (ng_ppp_comp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM));
1228164b576eSGleb Smirnoff }
1229164b576eSGleb Smirnoff 
1230164b576eSGleb Smirnoff /*
1231164b576eSGleb Smirnoff  * Link layer
1232164b576eSGleb Smirnoff  */
1233164b576eSGleb Smirnoff 
1234164b576eSGleb Smirnoff static int
1235164b576eSGleb Smirnoff ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
12364cf49a43SJulian Elischer {
123730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1238a9b3dca5SArchie Cobbs 	struct ng_ppp_link *link;
1239e149c4e2SArchie Cobbs 	int len, error;
1240069154d5SJulian Elischer 	struct mbuf *m;
1241164b576eSGleb Smirnoff 	uint16_t mru;
12424cf49a43SJulian Elischer 
1243164b576eSGleb Smirnoff 	/* Check if link correct. */
1244164b576eSGleb Smirnoff 	if (linkNum >= NG_PPP_MAX_LINKS) {
1245069154d5SJulian Elischer 		NG_FREE_ITEM(item);
12463949bee8SArchie Cobbs 		return (ENETDOWN);
12473949bee8SArchie Cobbs 	}
1248164b576eSGleb Smirnoff 
1249164b576eSGleb Smirnoff 	/* Get link pointer (optimization). */
1250164b576eSGleb Smirnoff 	link = &priv->links[linkNum];
1251164b576eSGleb Smirnoff 
1252164b576eSGleb Smirnoff 	/* Check link status (if real). */
1253164b576eSGleb Smirnoff 	if (link->hook == NULL) {
1254164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1255164b576eSGleb Smirnoff 		return (ENETDOWN);
12560e11d0f3SArchie Cobbs 	}
12573949bee8SArchie Cobbs 
1258164b576eSGleb Smirnoff 	/* Extract mbuf. */
1259164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
1260164b576eSGleb Smirnoff 
1261164b576eSGleb Smirnoff 	/* Check peer's MRU for this link. */
1262164b576eSGleb Smirnoff 	mru = link->conf.mru;
1263931c828aSArchie Cobbs 	if (mru != 0 && m->m_pkthdr.len > mru) {
1264931c828aSArchie Cobbs 		NG_FREE_M(m);
1265931c828aSArchie Cobbs 		NG_FREE_ITEM(item);
1266931c828aSArchie Cobbs 		return (EMSGSIZE);
1267931c828aSArchie Cobbs 	}
1268931c828aSArchie Cobbs 
1269164b576eSGleb Smirnoff 	/* Prepend protocol number, possibly compressed. */
1270164b576eSGleb Smirnoff 	if ((m = ng_ppp_addproto(m, proto, link->conf.enableProtoComp)) ==
1271164b576eSGleb Smirnoff 	    NULL) {
1272069154d5SJulian Elischer 		NG_FREE_ITEM(item);
1273d690a6e7SArchie Cobbs 		return (ENOBUFS);
1274d690a6e7SArchie Cobbs 	}
1275d690a6e7SArchie Cobbs 
1276164b576eSGleb Smirnoff 	/* Prepend address and control field (unless compressed). */
1277a9b3dca5SArchie Cobbs 	if (proto == PROT_LCP || !link->conf.enableACFComp) {
1278d690a6e7SArchie Cobbs 		if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL) {
1279069154d5SJulian Elischer 			NG_FREE_ITEM(item);
1280d690a6e7SArchie Cobbs 			return (ENOBUFS);
1281d690a6e7SArchie Cobbs 		}
1282d690a6e7SArchie Cobbs 	}
1283d690a6e7SArchie Cobbs 
1284164b576eSGleb Smirnoff 	/* Deliver frame. */
1285e149c4e2SArchie Cobbs 	len = m->m_pkthdr.len;
1286069154d5SJulian Elischer 	NG_FWD_NEW_DATA(error, item, link->hook, m);
1287fb1fc8abSArchie Cobbs 
1288164b576eSGleb Smirnoff 	/* Update stats and 'bytes in queue' counter. */
1289fb1fc8abSArchie Cobbs 	if (error == 0) {
1290a9b3dca5SArchie Cobbs 		link->stats.xmitFrames++;
1291a9b3dca5SArchie Cobbs 		link->stats.xmitOctets += len;
1292164b576eSGleb Smirnoff 
1293164b576eSGleb Smirnoff 		/* bytesInQueue and lastWrite required only for mp_strategy. */
1294164b576eSGleb Smirnoff 		if (priv->conf.enableMultilink && !priv->allLinksEqual) {
1295fd58342cSAlexander Motin 		    /* If queue was empty, then mark this time. */
1296fd58342cSAlexander Motin 		    if (link->bytesInQueue == 0)
1297a9b3dca5SArchie Cobbs 			getmicrouptime(&link->lastWrite);
1298fd58342cSAlexander Motin 		    link->bytesInQueue += len + MP_AVERAGE_LINK_OVERHEAD;
1299fd58342cSAlexander Motin 		    /* Limit max queue length to 50 pkts. BW can be defined
1300fd58342cSAlexander Motin 		       incorrectly and link may not signal overload. */
1301fd58342cSAlexander Motin 		    if (link->bytesInQueue > 50 * 1600)
1302fd58342cSAlexander Motin 			link->bytesInQueue = 50 * 1600;
1303fb1fc8abSArchie Cobbs 		}
13043949bee8SArchie Cobbs 	}
1305164b576eSGleb Smirnoff 	return (error);
1306164b576eSGleb Smirnoff }
1307164b576eSGleb Smirnoff 
1308164b576eSGleb Smirnoff /*
1309164b576eSGleb Smirnoff  * Receive data on a hook linkX.
1310164b576eSGleb Smirnoff  */
1311164b576eSGleb Smirnoff static int
1312164b576eSGleb Smirnoff ng_ppp_rcvdata(hook_p hook, item_p item)
1313164b576eSGleb Smirnoff {
1314164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
1315164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1316164b576eSGleb Smirnoff 	const int index = (intptr_t)NG_HOOK_PRIVATE(hook);
1317164b576eSGleb Smirnoff 	const uint16_t linkNum = (uint16_t)~index;
1318164b576eSGleb Smirnoff 	struct ng_ppp_link * const link = &priv->links[linkNum];
1319164b576eSGleb Smirnoff 	uint16_t proto;
1320164b576eSGleb Smirnoff 	struct mbuf *m;
1321164b576eSGleb Smirnoff 
132233eb7cb0SMatt Jacob 	KASSERT(linkNum < NG_PPP_MAX_LINKS,
1323164b576eSGleb Smirnoff 	    ("%s: bogus index 0x%x", __func__, index));
1324164b576eSGleb Smirnoff 
1325164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
1326164b576eSGleb Smirnoff 
1327164b576eSGleb Smirnoff 	/* Stats */
1328164b576eSGleb Smirnoff 	link->stats.recvFrames++;
1329164b576eSGleb Smirnoff 	link->stats.recvOctets += m->m_pkthdr.len;
1330164b576eSGleb Smirnoff 
1331164b576eSGleb Smirnoff 	/* Strip address and control fields, if present. */
1332164b576eSGleb Smirnoff 	if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL) {
1333164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1334164b576eSGleb Smirnoff 		return (ENOBUFS);
1335164b576eSGleb Smirnoff 	}
1336164b576eSGleb Smirnoff 	if (bcmp(mtod(m, uint8_t *), &ng_ppp_acf, 2) == 0)
1337164b576eSGleb Smirnoff 		m_adj(m, 2);
1338164b576eSGleb Smirnoff 
1339164b576eSGleb Smirnoff 	if ((m = ng_ppp_cutproto(m, &proto)) == NULL) {
1340164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1341164b576eSGleb Smirnoff 		return (ENOBUFS);
1342164b576eSGleb Smirnoff 	}
1343164b576eSGleb Smirnoff 	NGI_M(item) = m; 	/* Put changed m back into item. */
1344164b576eSGleb Smirnoff 
1345164b576eSGleb Smirnoff 	if (!PROT_VALID(proto)) {
1346164b576eSGleb Smirnoff 		link->stats.badProtos++;
1347164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1348164b576eSGleb Smirnoff 		return (EIO);
1349164b576eSGleb Smirnoff 	}
1350164b576eSGleb Smirnoff 
135139c14742SGleb Smirnoff 	/* LCP packets must go directly to bypass. */
135239c14742SGleb Smirnoff 	if (proto >= 0xB000)
135339c14742SGleb Smirnoff 		return (ng_ppp_bypass(node, item, proto, linkNum));
135439c14742SGleb Smirnoff 
135539c14742SGleb Smirnoff 	if (!link->conf.enableLink) {
135639c14742SGleb Smirnoff 		/* Non-LCP packets are denied on a disabled link. */
135739c14742SGleb Smirnoff 		NG_FREE_ITEM(item);
135839c14742SGleb Smirnoff 		return (ENXIO);
135939c14742SGleb Smirnoff 	}
136039c14742SGleb Smirnoff 
1361164b576eSGleb Smirnoff 	return (ng_ppp_mp_recv(node, item, proto, linkNum));
1362164b576eSGleb Smirnoff }
1363164b576eSGleb Smirnoff 
1364164b576eSGleb Smirnoff /*
1365164b576eSGleb Smirnoff  * Multilink layer
1366164b576eSGleb Smirnoff  */
13673949bee8SArchie Cobbs 
13683949bee8SArchie Cobbs /*
13693949bee8SArchie Cobbs  * Handle an incoming multi-link fragment
1370a9b3dca5SArchie Cobbs  *
1371a9b3dca5SArchie Cobbs  * The fragment reassembly algorithm is somewhat complex. This is mainly
1372a9b3dca5SArchie Cobbs  * because we are required not to reorder the reconstructed packets, yet
1373a9b3dca5SArchie Cobbs  * fragments are only guaranteed to arrive in order on a per-link basis.
1374a9b3dca5SArchie Cobbs  * In other words, when we have a complete packet ready, but the previous
1375a9b3dca5SArchie Cobbs  * packet is still incomplete, we have to decide between delivering the
1376a9b3dca5SArchie Cobbs  * complete packet and throwing away the incomplete one, or waiting to
1377a9b3dca5SArchie Cobbs  * see if the remainder of the incomplete one arrives, at which time we
1378a9b3dca5SArchie Cobbs  * can deliver both packets, in order.
1379a9b3dca5SArchie Cobbs  *
1380a9b3dca5SArchie Cobbs  * This problem is exacerbated by "sequence number slew", which is when
1381a9b3dca5SArchie Cobbs  * the sequence numbers coming in from different links are far apart from
1382a9b3dca5SArchie Cobbs  * each other. In particular, certain unnamed equipment (*cough* Ascend)
1383a9b3dca5SArchie Cobbs  * has been seen to generate sequence number slew of up to 10 on an ISDN
1384a9b3dca5SArchie Cobbs  * 2B-channel MP link. There is nothing invalid about sequence number slew
1385a9b3dca5SArchie Cobbs  * but it makes the reasssembly process have to work harder.
1386a9b3dca5SArchie Cobbs  *
1387a9b3dca5SArchie Cobbs  * However, the peer is required to transmit fragments in order on each
1388a9b3dca5SArchie Cobbs  * link. That means if we define MSEQ as the minimum over all links of
1389a9b3dca5SArchie Cobbs  * the highest sequence number received on that link, then we can always
1390a9b3dca5SArchie Cobbs  * give up any hope of receiving a fragment with sequence number < MSEQ in
1391a9b3dca5SArchie Cobbs  * the future (all of this using 'wraparound' sequence number space).
1392a9b3dca5SArchie Cobbs  * Therefore we can always immediately throw away incomplete packets
1393a9b3dca5SArchie Cobbs  * missing fragments with sequence numbers < MSEQ.
1394a9b3dca5SArchie Cobbs  *
1395a9b3dca5SArchie Cobbs  * Here is an overview of our algorithm:
1396a9b3dca5SArchie Cobbs  *
1397a9b3dca5SArchie Cobbs  *    o Received fragments are inserted into a queue, for which we
1398a9b3dca5SArchie Cobbs  *	maintain these invariants between calls to this function:
1399a9b3dca5SArchie Cobbs  *
1400a9b3dca5SArchie Cobbs  *	- Fragments are ordered in the queue by sequence number
1401a9b3dca5SArchie Cobbs  *	- If a complete packet is at the head of the queue, then
1402a9b3dca5SArchie Cobbs  *	  the first fragment in the packet has seq# > MSEQ + 1
1403a9b3dca5SArchie Cobbs  *	  (otherwise, we could deliver it immediately)
1404a9b3dca5SArchie Cobbs  *	- If any fragments have seq# < MSEQ, then they are necessarily
1405a9b3dca5SArchie Cobbs  *	  part of a packet whose missing seq#'s are all > MSEQ (otherwise,
1406a9b3dca5SArchie Cobbs  *	  we can throw them away because they'll never be completed)
1407a9b3dca5SArchie Cobbs  *	- The queue contains at most MP_MAX_QUEUE_LEN fragments
1408a9b3dca5SArchie Cobbs  *
1409a9b3dca5SArchie Cobbs  *    o We have a periodic timer that checks the queue for the first
1410a9b3dca5SArchie Cobbs  *	complete packet that has been sitting in the queue "too long".
1411a9b3dca5SArchie Cobbs  *	When one is detected, all previous (incomplete) fragments are
1412a9b3dca5SArchie Cobbs  *	discarded, their missing fragments are declared lost and MSEQ
1413a9b3dca5SArchie Cobbs  *	is increased.
1414a9b3dca5SArchie Cobbs  *
1415a9b3dca5SArchie Cobbs  *    o If we recieve a fragment with seq# < MSEQ, we throw it away
1416a9b3dca5SArchie Cobbs  *	because we've already delcared it lost.
1417a9b3dca5SArchie Cobbs  *
1418a9b3dca5SArchie Cobbs  * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM.
14193949bee8SArchie Cobbs  */
14203949bee8SArchie Cobbs static int
1421164b576eSGleb Smirnoff ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
14223949bee8SArchie Cobbs {
142330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1424a9b3dca5SArchie Cobbs 	struct ng_ppp_link *const link = &priv->links[linkNum];
14253949bee8SArchie Cobbs 	struct ng_ppp_frag frag0, *frag = &frag0;
1426a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent;
1427a9b3dca5SArchie Cobbs 	int i, diff, inserted;
1428069154d5SJulian Elischer 	struct mbuf *m;
14293949bee8SArchie Cobbs 
1430164b576eSGleb Smirnoff 	if ((!priv->conf.enableMultilink) || proto != PROT_MP)
1431164b576eSGleb Smirnoff 		return (ng_ppp_crypt_recv(node, item, proto, linkNum));
1432164b576eSGleb Smirnoff 
1433069154d5SJulian Elischer 	NGI_GET_M(item, m);
1434069154d5SJulian Elischer 	NG_FREE_ITEM(item);
1435b4c44c30SArchie Cobbs 
14363949bee8SArchie Cobbs 	/* Extract fragment information from MP header */
14373949bee8SArchie Cobbs 	if (priv->conf.recvShortSeq) {
1438164b576eSGleb Smirnoff 		uint16_t shdr;
14393949bee8SArchie Cobbs 
14403949bee8SArchie Cobbs 		if (m->m_pkthdr.len < 2) {
1441a9b3dca5SArchie Cobbs 			link->stats.runts++;
1442069154d5SJulian Elischer 			NG_FREE_M(m);
14433949bee8SArchie Cobbs 			return (EINVAL);
14443949bee8SArchie Cobbs 		}
14453ca24c28SJulian Elischer 		if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL)
14463949bee8SArchie Cobbs 			return (ENOBUFS);
14473ca24c28SJulian Elischer 
1448164b576eSGleb Smirnoff 		shdr = ntohs(*mtod(m, uint16_t *));
14496f16db81SArchie Cobbs 		frag->seq = MP_SHORT_EXTEND(shdr);
14503949bee8SArchie Cobbs 		frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0;
14513949bee8SArchie Cobbs 		frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0;
1452a9b3dca5SArchie Cobbs 		diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq);
14533949bee8SArchie Cobbs 		m_adj(m, 2);
14543949bee8SArchie Cobbs 	} else {
1455164b576eSGleb Smirnoff 		uint32_t lhdr;
14563949bee8SArchie Cobbs 
14573949bee8SArchie Cobbs 		if (m->m_pkthdr.len < 4) {
1458a9b3dca5SArchie Cobbs 			link->stats.runts++;
1459069154d5SJulian Elischer 			NG_FREE_M(m);
14603949bee8SArchie Cobbs 			return (EINVAL);
14613949bee8SArchie Cobbs 		}
14623ca24c28SJulian Elischer 		if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
14633949bee8SArchie Cobbs 			return (ENOBUFS);
14643ca24c28SJulian Elischer 
1465164b576eSGleb Smirnoff 		lhdr = ntohl(*mtod(m, uint32_t *));
14666f16db81SArchie Cobbs 		frag->seq = MP_LONG_EXTEND(lhdr);
14673949bee8SArchie Cobbs 		frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0;
14683949bee8SArchie Cobbs 		frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0;
1469a9b3dca5SArchie Cobbs 		diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq);
14703949bee8SArchie Cobbs 		m_adj(m, 4);
14713949bee8SArchie Cobbs 	}
14723949bee8SArchie Cobbs 	frag->data = m;
1473a9b3dca5SArchie Cobbs 	getmicrouptime(&frag->timestamp);
14743949bee8SArchie Cobbs 
1475a9b3dca5SArchie Cobbs 	/* If sequence number is < MSEQ, we've already declared this
1476a9b3dca5SArchie Cobbs 	   fragment as lost, so we have no choice now but to drop it */
1477a9b3dca5SArchie Cobbs 	if (diff < 0) {
1478a9b3dca5SArchie Cobbs 		link->stats.dropFragments++;
1479069154d5SJulian Elischer 		NG_FREE_M(m);
1480a9b3dca5SArchie Cobbs 		return (0);
1481a9b3dca5SArchie Cobbs 	}
14823949bee8SArchie Cobbs 
1483a9b3dca5SArchie Cobbs 	/* Update highest received sequence number on this link and MSEQ */
1484a9b3dca5SArchie Cobbs 	priv->mseq = link->seq = frag->seq;
1485a9b3dca5SArchie Cobbs 	for (i = 0; i < priv->numActiveLinks; i++) {
1486a9b3dca5SArchie Cobbs 		struct ng_ppp_link *const alink =
1487a9b3dca5SArchie Cobbs 		    &priv->links[priv->activeLinks[i]];
1488a9b3dca5SArchie Cobbs 
14896f16db81SArchie Cobbs 		if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0)
1490a9b3dca5SArchie Cobbs 			priv->mseq = alink->seq;
1491a9b3dca5SArchie Cobbs 	}
14923949bee8SArchie Cobbs 
14933949bee8SArchie Cobbs 	/* Allocate a new frag struct for the queue */
14949c8c302fSJulian Elischer 	MALLOC(frag, struct ng_ppp_frag *, sizeof(*frag), M_NETGRAPH_PPP, M_NOWAIT);
14953949bee8SArchie Cobbs 	if (frag == NULL) {
1496069154d5SJulian Elischer 		NG_FREE_M(m);
1497a9b3dca5SArchie Cobbs 		ng_ppp_frag_process(node);
14983949bee8SArchie Cobbs 		return (ENOMEM);
14993949bee8SArchie Cobbs 	}
15003949bee8SArchie Cobbs 	*frag = frag0;
15013949bee8SArchie Cobbs 
1502a9b3dca5SArchie Cobbs 	/* Add fragment to queue, which is sorted by sequence number */
15031e7a9f72SArchie Cobbs 	inserted = 0;
15045f90cac7SKirk McKusick 	TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) {
15056f16db81SArchie Cobbs 		diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq);
15063949bee8SArchie Cobbs 		if (diff > 0) {
15075f90cac7SKirk McKusick 			TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent);
15081e7a9f72SArchie Cobbs 			inserted = 1;
15093949bee8SArchie Cobbs 			break;
15103949bee8SArchie Cobbs 		} else if (diff == 0) {	     /* should never happen! */
1511a9b3dca5SArchie Cobbs 			link->stats.dupFragments++;
1512069154d5SJulian Elischer 			NG_FREE_M(frag->data);
15139c8c302fSJulian Elischer 			FREE(frag, M_NETGRAPH_PPP);
15143949bee8SArchie Cobbs 			return (EINVAL);
15153949bee8SArchie Cobbs 		}
15163949bee8SArchie Cobbs 	}
15171e7a9f72SArchie Cobbs 	if (!inserted)
15185f90cac7SKirk McKusick 		TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent);
1519a9b3dca5SArchie Cobbs 	priv->qlen++;
15203949bee8SArchie Cobbs 
1521a9b3dca5SArchie Cobbs 	/* Process the queue */
1522a9b3dca5SArchie Cobbs 	return ng_ppp_frag_process(node);
15233949bee8SArchie Cobbs }
15241e7a9f72SArchie Cobbs 
1525164b576eSGleb Smirnoff /************************************************************************
1526164b576eSGleb Smirnoff 			HELPER STUFF
1527164b576eSGleb Smirnoff  ************************************************************************/
1528164b576eSGleb Smirnoff 
1529a9b3dca5SArchie Cobbs /*
1530a9b3dca5SArchie Cobbs  * Examine our list of fragments, and determine if there is a
1531a9b3dca5SArchie Cobbs  * complete and deliverable packet at the head of the list.
1532a9b3dca5SArchie Cobbs  * Return 1 if so, zero otherwise.
1533a9b3dca5SArchie Cobbs  */
1534a9b3dca5SArchie Cobbs static int
1535a9b3dca5SArchie Cobbs ng_ppp_check_packet(node_p node)
1536a9b3dca5SArchie Cobbs {
153730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1538a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext;
15393949bee8SArchie Cobbs 
1540a9b3dca5SArchie Cobbs 	/* Check for empty queue */
15415f90cac7SKirk McKusick 	if (TAILQ_EMPTY(&priv->frags))
1542a9b3dca5SArchie Cobbs 		return (0);
1543a9b3dca5SArchie Cobbs 
1544a9b3dca5SArchie Cobbs 	/* Check first fragment is the start of a deliverable packet */
15455f90cac7SKirk McKusick 	qent = TAILQ_FIRST(&priv->frags);
15466f16db81SArchie Cobbs 	if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1)
1547a9b3dca5SArchie Cobbs 		return (0);
1548a9b3dca5SArchie Cobbs 
1549a9b3dca5SArchie Cobbs 	/* Check that all the fragments are there */
1550a9b3dca5SArchie Cobbs 	while (!qent->last) {
15515f90cac7SKirk McKusick 		qnext = TAILQ_NEXT(qent, f_qent);
15525f90cac7SKirk McKusick 		if (qnext == NULL)	/* end of queue */
1553a9b3dca5SArchie Cobbs 			return (0);
15546f16db81SArchie Cobbs 		if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq))
1555a9b3dca5SArchie Cobbs 			return (0);
1556a9b3dca5SArchie Cobbs 		qent = qnext;
1557a9b3dca5SArchie Cobbs 	}
1558a9b3dca5SArchie Cobbs 
1559a9b3dca5SArchie Cobbs 	/* Got one */
1560a9b3dca5SArchie Cobbs 	return (1);
1561a9b3dca5SArchie Cobbs }
1562a9b3dca5SArchie Cobbs 
1563a9b3dca5SArchie Cobbs /*
1564a9b3dca5SArchie Cobbs  * Pull a completed packet off the head of the incoming fragment queue.
1565a9b3dca5SArchie Cobbs  * This assumes there is a completed packet there to pull off.
1566a9b3dca5SArchie Cobbs  */
1567a9b3dca5SArchie Cobbs static void
15683ca24c28SJulian Elischer ng_ppp_get_packet(node_p node, struct mbuf **mp)
1569a9b3dca5SArchie Cobbs {
157030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1571a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext;
1572a9b3dca5SArchie Cobbs 	struct mbuf *m = NULL, *tail;
1573a9b3dca5SArchie Cobbs 
15745f90cac7SKirk McKusick 	qent = TAILQ_FIRST(&priv->frags);
15755f90cac7SKirk McKusick 	KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first,
15766e551fb6SDavid E. O'Brien 	    ("%s: no packet", __func__));
1577a9b3dca5SArchie Cobbs 	for (tail = NULL; qent != NULL; qent = qnext) {
15785f90cac7SKirk McKusick 		qnext = TAILQ_NEXT(qent, f_qent);
15795f90cac7SKirk McKusick 		KASSERT(!TAILQ_EMPTY(&priv->frags),
15806e551fb6SDavid E. O'Brien 		    ("%s: empty q", __func__));
15815f90cac7SKirk McKusick 		TAILQ_REMOVE(&priv->frags, qent, f_qent);
15823ca24c28SJulian Elischer 		if (tail == NULL)
15833949bee8SArchie Cobbs 			tail = m = qent->data;
15843ca24c28SJulian Elischer 		else {
15853949bee8SArchie Cobbs 			m->m_pkthdr.len += qent->data->m_pkthdr.len;
15863949bee8SArchie Cobbs 			tail->m_next = qent->data;
15873949bee8SArchie Cobbs 		}
15883949bee8SArchie Cobbs 		while (tail->m_next != NULL)
15893949bee8SArchie Cobbs 			tail = tail->m_next;
1590a9b3dca5SArchie Cobbs 		if (qent->last)
15913949bee8SArchie Cobbs 			qnext = NULL;
15929c8c302fSJulian Elischer 		FREE(qent, M_NETGRAPH_PPP);
1593a9b3dca5SArchie Cobbs 		priv->qlen--;
1594a9b3dca5SArchie Cobbs 	}
1595a9b3dca5SArchie Cobbs 	*mp = m;
15963949bee8SArchie Cobbs }
15973949bee8SArchie Cobbs 
1598a9b3dca5SArchie Cobbs /*
1599a9b3dca5SArchie Cobbs  * Trim fragments from the queue whose packets can never be completed.
1600a9b3dca5SArchie Cobbs  * This assumes a complete packet is NOT at the beginning of the queue.
1601a9b3dca5SArchie Cobbs  * Returns 1 if fragments were removed, zero otherwise.
1602a9b3dca5SArchie Cobbs  */
1603a9b3dca5SArchie Cobbs static int
1604a9b3dca5SArchie Cobbs ng_ppp_frag_trim(node_p node)
1605a9b3dca5SArchie Cobbs {
160630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1607a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext = NULL;
1608a9b3dca5SArchie Cobbs 	int removed = 0;
1609a9b3dca5SArchie Cobbs 
1610a9b3dca5SArchie Cobbs 	/* Scan for "dead" fragments and remove them */
1611a9b3dca5SArchie Cobbs 	while (1) {
1612a9b3dca5SArchie Cobbs 		int dead = 0;
1613a9b3dca5SArchie Cobbs 
1614a9b3dca5SArchie Cobbs 		/* If queue is empty, we're done */
16155f90cac7SKirk McKusick 		if (TAILQ_EMPTY(&priv->frags))
16163949bee8SArchie Cobbs 			break;
1617a9b3dca5SArchie Cobbs 
1618a9b3dca5SArchie Cobbs 		/* Determine whether first fragment can ever be completed */
16195f90cac7SKirk McKusick 		TAILQ_FOREACH(qent, &priv->frags, f_qent) {
16206f16db81SArchie Cobbs 			if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0)
1621a9b3dca5SArchie Cobbs 				break;
16225f90cac7SKirk McKusick 			qnext = TAILQ_NEXT(qent, f_qent);
16235f90cac7SKirk McKusick 			KASSERT(qnext != NULL,
16246e551fb6SDavid E. O'Brien 			    ("%s: last frag < MSEQ?", __func__));
16256f16db81SArchie Cobbs 			if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)
1626a9b3dca5SArchie Cobbs 			    || qent->last || qnext->first) {
1627a9b3dca5SArchie Cobbs 				dead = 1;
1628a9b3dca5SArchie Cobbs 				break;
1629a9b3dca5SArchie Cobbs 			}
1630a9b3dca5SArchie Cobbs 		}
1631a9b3dca5SArchie Cobbs 		if (!dead)
1632a9b3dca5SArchie Cobbs 			break;
1633a9b3dca5SArchie Cobbs 
1634a9b3dca5SArchie Cobbs 		/* Remove fragment and all others in the same packet */
16355f90cac7SKirk McKusick 		while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) {
16365f90cac7SKirk McKusick 			KASSERT(!TAILQ_EMPTY(&priv->frags),
16376e551fb6SDavid E. O'Brien 			    ("%s: empty q", __func__));
1638a9b3dca5SArchie Cobbs 			priv->bundleStats.dropFragments++;
16395f90cac7SKirk McKusick 			TAILQ_REMOVE(&priv->frags, qent, f_qent);
1640069154d5SJulian Elischer 			NG_FREE_M(qent->data);
16419c8c302fSJulian Elischer 			FREE(qent, M_NETGRAPH_PPP);
1642a9b3dca5SArchie Cobbs 			priv->qlen--;
1643a9b3dca5SArchie Cobbs 			removed = 1;
1644a9b3dca5SArchie Cobbs 		}
1645a9b3dca5SArchie Cobbs 	}
1646a9b3dca5SArchie Cobbs 	return (removed);
16473949bee8SArchie Cobbs }
16483949bee8SArchie Cobbs 
1649a9b3dca5SArchie Cobbs /*
1650a9b3dca5SArchie Cobbs  * Run the queue, restoring the queue invariants
1651a9b3dca5SArchie Cobbs  */
1652a9b3dca5SArchie Cobbs static int
1653a9b3dca5SArchie Cobbs ng_ppp_frag_process(node_p node)
1654a9b3dca5SArchie Cobbs {
165530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1656a9b3dca5SArchie Cobbs 	struct mbuf *m;
1657069154d5SJulian Elischer 	item_p item;
1658164b576eSGleb Smirnoff 	uint16_t proto;
1659a9b3dca5SArchie Cobbs 
1660a9b3dca5SArchie Cobbs 	/* Deliver any deliverable packets */
1661a9b3dca5SArchie Cobbs 	while (ng_ppp_check_packet(node)) {
16623ca24c28SJulian Elischer 		ng_ppp_get_packet(node, &m);
1663164b576eSGleb Smirnoff 		if ((m = ng_ppp_cutproto(m, &proto)) == NULL)
1664164b576eSGleb Smirnoff 			continue;
1665164b576eSGleb Smirnoff 		if (!PROT_VALID(proto)) {
1666164b576eSGleb Smirnoff 			priv->bundleStats.badProtos++;
1667164b576eSGleb Smirnoff 			NG_FREE_M(m);
1668164b576eSGleb Smirnoff 			continue;
1669164b576eSGleb Smirnoff 		}
1670d0784b47SGleb Smirnoff 		if ((item = ng_package_data(m, NG_NOFLAGS)) != NULL)
1671164b576eSGleb Smirnoff 			ng_ppp_crypt_recv(node, item, proto,
1672164b576eSGleb Smirnoff 				NG_PPP_BUNDLE_LINKNUM);
1673a9b3dca5SArchie Cobbs 	}
1674a9b3dca5SArchie Cobbs 
1675a9b3dca5SArchie Cobbs 	/* Delete dead fragments and try again */
1676a9b3dca5SArchie Cobbs 	if (ng_ppp_frag_trim(node)) {
1677a9b3dca5SArchie Cobbs 		while (ng_ppp_check_packet(node)) {
16783ca24c28SJulian Elischer 			ng_ppp_get_packet(node, &m);
1679164b576eSGleb Smirnoff 			if ((m = ng_ppp_cutproto(m, &proto)) == NULL)
1680164b576eSGleb Smirnoff 				continue;
1681164b576eSGleb Smirnoff 			if (!PROT_VALID(proto)) {
1682164b576eSGleb Smirnoff 				priv->bundleStats.badProtos++;
1683164b576eSGleb Smirnoff 				NG_FREE_M(m);
1684164b576eSGleb Smirnoff 				continue;
1685164b576eSGleb Smirnoff 			}
1686d0784b47SGleb Smirnoff 			if ((item = ng_package_data(m, NG_NOFLAGS)) != NULL)
1687164b576eSGleb Smirnoff 				ng_ppp_crypt_recv(node, item, proto,
1688164b576eSGleb Smirnoff 					NG_PPP_BUNDLE_LINKNUM);
1689a9b3dca5SArchie Cobbs 		}
1690a9b3dca5SArchie Cobbs 	}
1691a9b3dca5SArchie Cobbs 
1692a9b3dca5SArchie Cobbs 	/* Check queue length */
1693a9b3dca5SArchie Cobbs 	if (priv->qlen > MP_MAX_QUEUE_LEN) {
1694a9b3dca5SArchie Cobbs 		struct ng_ppp_frag *qent;
1695a9b3dca5SArchie Cobbs 		int i;
1696a9b3dca5SArchie Cobbs 
1697a9b3dca5SArchie Cobbs 		/* Get oldest fragment */
16985f90cac7SKirk McKusick 		KASSERT(!TAILQ_EMPTY(&priv->frags),
16996e551fb6SDavid E. O'Brien 		    ("%s: empty q", __func__));
17005f90cac7SKirk McKusick 		qent = TAILQ_FIRST(&priv->frags);
1701a9b3dca5SArchie Cobbs 
1702a9b3dca5SArchie Cobbs 		/* Bump MSEQ if necessary */
17036f16db81SArchie Cobbs 		if (MP_RECV_SEQ_DIFF(priv, priv->mseq, qent->seq) < 0) {
1704a9b3dca5SArchie Cobbs 			priv->mseq = qent->seq;
1705a9b3dca5SArchie Cobbs 			for (i = 0; i < priv->numActiveLinks; i++) {
1706a9b3dca5SArchie Cobbs 				struct ng_ppp_link *const alink =
1707a9b3dca5SArchie Cobbs 				    &priv->links[priv->activeLinks[i]];
1708a9b3dca5SArchie Cobbs 
17096f16db81SArchie Cobbs 				if (MP_RECV_SEQ_DIFF(priv,
1710a9b3dca5SArchie Cobbs 				    alink->seq, priv->mseq) < 0)
1711a9b3dca5SArchie Cobbs 					alink->seq = priv->mseq;
1712a9b3dca5SArchie Cobbs 			}
1713a9b3dca5SArchie Cobbs 		}
1714a9b3dca5SArchie Cobbs 
1715a9b3dca5SArchie Cobbs 		/* Drop it */
1716a9b3dca5SArchie Cobbs 		priv->bundleStats.dropFragments++;
17175f90cac7SKirk McKusick 		TAILQ_REMOVE(&priv->frags, qent, f_qent);
1718069154d5SJulian Elischer 		NG_FREE_M(qent->data);
17199c8c302fSJulian Elischer 		FREE(qent, M_NETGRAPH_PPP);
1720a9b3dca5SArchie Cobbs 		priv->qlen--;
1721a9b3dca5SArchie Cobbs 
1722a9b3dca5SArchie Cobbs 		/* Process queue again */
1723a9b3dca5SArchie Cobbs 		return ng_ppp_frag_process(node);
1724a9b3dca5SArchie Cobbs 	}
1725a9b3dca5SArchie Cobbs 
1726a9b3dca5SArchie Cobbs 	/* Done */
1727a9b3dca5SArchie Cobbs 	return (0);
1728a9b3dca5SArchie Cobbs }
1729a9b3dca5SArchie Cobbs 
1730a9b3dca5SArchie Cobbs /*
1731a9b3dca5SArchie Cobbs  * Check for 'stale' completed packets that need to be delivered
1732a9b3dca5SArchie Cobbs  *
1733a9b3dca5SArchie Cobbs  * If a link goes down or has a temporary failure, MSEQ can get
1734a9b3dca5SArchie Cobbs  * "stuck", because no new incoming fragments appear on that link.
1735a9b3dca5SArchie Cobbs  * This can cause completed packets to never get delivered if
1736a9b3dca5SArchie Cobbs  * their sequence numbers are all > MSEQ + 1.
1737a9b3dca5SArchie Cobbs  *
1738a9b3dca5SArchie Cobbs  * This routine checks how long all of the completed packets have
1739a9b3dca5SArchie Cobbs  * been sitting in the queue, and if too long, removes fragments
1740a9b3dca5SArchie Cobbs  * from the queue and increments MSEQ to allow them to be delivered.
1741a9b3dca5SArchie Cobbs  */
1742a9b3dca5SArchie Cobbs static void
1743a9b3dca5SArchie Cobbs ng_ppp_frag_checkstale(node_p node)
1744a9b3dca5SArchie Cobbs {
174530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1746a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *beg, *end;
1747a9b3dca5SArchie Cobbs 	struct timeval now, age;
1748a9b3dca5SArchie Cobbs 	struct mbuf *m;
1749a9b3dca5SArchie Cobbs 	int i, seq;
1750069154d5SJulian Elischer 	item_p item;
17512b2a8188SArchie Cobbs 	int endseq;
1752164b576eSGleb Smirnoff 	uint16_t proto;
1753a9b3dca5SArchie Cobbs 
1754a9b3dca5SArchie Cobbs 	now.tv_sec = 0;			/* uninitialized state */
1755a9b3dca5SArchie Cobbs 	while (1) {
1756a9b3dca5SArchie Cobbs 
1757a9b3dca5SArchie Cobbs 		/* If queue is empty, we're done */
17585f90cac7SKirk McKusick 		if (TAILQ_EMPTY(&priv->frags))
1759a9b3dca5SArchie Cobbs 			break;
1760a9b3dca5SArchie Cobbs 
1761a9b3dca5SArchie Cobbs 		/* Find the first complete packet in the queue */
1762a9b3dca5SArchie Cobbs 		beg = end = NULL;
17635f90cac7SKirk McKusick 		seq = TAILQ_FIRST(&priv->frags)->seq;
17645f90cac7SKirk McKusick 		TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1765a9b3dca5SArchie Cobbs 			if (qent->first)
1766a9b3dca5SArchie Cobbs 				beg = qent;
1767a9b3dca5SArchie Cobbs 			else if (qent->seq != seq)
1768a9b3dca5SArchie Cobbs 				beg = NULL;
1769a9b3dca5SArchie Cobbs 			if (beg != NULL && qent->last) {
1770a9b3dca5SArchie Cobbs 				end = qent;
1771a9b3dca5SArchie Cobbs 				break;
1772a9b3dca5SArchie Cobbs 			}
17736f16db81SArchie Cobbs 			seq = MP_NEXT_RECV_SEQ(priv, seq);
1774a9b3dca5SArchie Cobbs 		}
1775a9b3dca5SArchie Cobbs 
1776a9b3dca5SArchie Cobbs 		/* If none found, exit */
1777a9b3dca5SArchie Cobbs 		if (end == NULL)
1778a9b3dca5SArchie Cobbs 			break;
1779a9b3dca5SArchie Cobbs 
1780a9b3dca5SArchie Cobbs 		/* Get current time (we assume we've been up for >= 1 second) */
1781a9b3dca5SArchie Cobbs 		if (now.tv_sec == 0)
1782a9b3dca5SArchie Cobbs 			getmicrouptime(&now);
1783a9b3dca5SArchie Cobbs 
1784a9b3dca5SArchie Cobbs 		/* Check if packet has been queued too long */
1785a9b3dca5SArchie Cobbs 		age = now;
1786a9b3dca5SArchie Cobbs 		timevalsub(&age, &beg->timestamp);
1787a9b3dca5SArchie Cobbs 		if (timevalcmp(&age, &ng_ppp_max_staleness, < ))
1788a9b3dca5SArchie Cobbs 			break;
1789a9b3dca5SArchie Cobbs 
1790a9b3dca5SArchie Cobbs 		/* Throw away junk fragments in front of the completed packet */
17915f90cac7SKirk McKusick 		while ((qent = TAILQ_FIRST(&priv->frags)) != beg) {
17925f90cac7SKirk McKusick 			KASSERT(!TAILQ_EMPTY(&priv->frags),
17936e551fb6SDavid E. O'Brien 			    ("%s: empty q", __func__));
1794a9b3dca5SArchie Cobbs 			priv->bundleStats.dropFragments++;
17955f90cac7SKirk McKusick 			TAILQ_REMOVE(&priv->frags, qent, f_qent);
1796069154d5SJulian Elischer 			NG_FREE_M(qent->data);
17979c8c302fSJulian Elischer 			FREE(qent, M_NETGRAPH_PPP);
1798a9b3dca5SArchie Cobbs 			priv->qlen--;
1799a9b3dca5SArchie Cobbs 		}
1800a9b3dca5SArchie Cobbs 
1801a9b3dca5SArchie Cobbs 		/* Extract completed packet */
18022b2a8188SArchie Cobbs 		endseq = end->seq;
18033ca24c28SJulian Elischer 		ng_ppp_get_packet(node, &m);
1804a9b3dca5SArchie Cobbs 
1805a9b3dca5SArchie Cobbs 		/* Bump MSEQ if necessary */
18062b2a8188SArchie Cobbs 		if (MP_RECV_SEQ_DIFF(priv, priv->mseq, endseq) < 0) {
18072b2a8188SArchie Cobbs 			priv->mseq = endseq;
1808a9b3dca5SArchie Cobbs 			for (i = 0; i < priv->numActiveLinks; i++) {
1809a9b3dca5SArchie Cobbs 				struct ng_ppp_link *const alink =
1810a9b3dca5SArchie Cobbs 				    &priv->links[priv->activeLinks[i]];
1811a9b3dca5SArchie Cobbs 
18126f16db81SArchie Cobbs 				if (MP_RECV_SEQ_DIFF(priv,
1813a9b3dca5SArchie Cobbs 				    alink->seq, priv->mseq) < 0)
1814a9b3dca5SArchie Cobbs 					alink->seq = priv->mseq;
1815a9b3dca5SArchie Cobbs 			}
1816a9b3dca5SArchie Cobbs 		}
1817a9b3dca5SArchie Cobbs 
1818164b576eSGleb Smirnoff 		if ((m = ng_ppp_cutproto(m, &proto)) == NULL)
1819164b576eSGleb Smirnoff 			continue;
1820164b576eSGleb Smirnoff 		if (!PROT_VALID(proto)) {
1821164b576eSGleb Smirnoff 			priv->bundleStats.badProtos++;
1822164b576eSGleb Smirnoff 			NG_FREE_M(m);
1823164b576eSGleb Smirnoff 			continue;
1824164b576eSGleb Smirnoff 		}
1825164b576eSGleb Smirnoff 
1826a9b3dca5SArchie Cobbs 		/* Deliver packet */
182774eafa88SGleb Smirnoff 		if ((item = ng_package_data(m, NG_NOFLAGS)) != NULL)
1828164b576eSGleb Smirnoff 			ng_ppp_crypt_recv(node, item, proto,
1829164b576eSGleb Smirnoff 				NG_PPP_BUNDLE_LINKNUM);
1830a9b3dca5SArchie Cobbs 	}
1831a9b3dca5SArchie Cobbs }
1832a9b3dca5SArchie Cobbs 
1833a9b3dca5SArchie Cobbs /*
1834a9b3dca5SArchie Cobbs  * Periodically call ng_ppp_frag_checkstale()
1835a9b3dca5SArchie Cobbs  */
1836a9b3dca5SArchie Cobbs static void
18379d952bd3SGleb Smirnoff ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1838a9b3dca5SArchie Cobbs {
18399d952bd3SGleb Smirnoff 	/* XXX: is this needed? */
18409d952bd3SGleb Smirnoff 	if (NG_NODE_NOT_VALID(node))
1841a9b3dca5SArchie Cobbs 		return;
1842a9b3dca5SArchie Cobbs 
1843a9b3dca5SArchie Cobbs 	/* Scan the fragment queue */
1844a9b3dca5SArchie Cobbs 	ng_ppp_frag_checkstale(node);
18459d952bd3SGleb Smirnoff 
18469d952bd3SGleb Smirnoff 	/* Start timer again */
18479d952bd3SGleb Smirnoff 	ng_ppp_start_frag_timer(node);
18483949bee8SArchie Cobbs }
18493949bee8SArchie Cobbs 
18503949bee8SArchie Cobbs /*
18513949bee8SArchie Cobbs  * Deliver a frame out on the bundle, i.e., figure out how to fragment
18523949bee8SArchie Cobbs  * the frame across the individual PPP links and do so.
18533949bee8SArchie Cobbs  */
18543949bee8SArchie Cobbs static int
1855164b576eSGleb Smirnoff ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto)
18563949bee8SArchie Cobbs {
185730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1858931c828aSArchie Cobbs 	const int hdr_len = priv->conf.xmitShortSeq ? 2 : 4;
18593949bee8SArchie Cobbs 	int distrib[NG_PPP_MAX_LINKS];
18603949bee8SArchie Cobbs 	int firstFragment;
18613949bee8SArchie Cobbs 	int activeLinkNum;
1862164b576eSGleb Smirnoff 	struct mbuf *m;
18633949bee8SArchie Cobbs 
18643949bee8SArchie Cobbs 	/* At least one link must be active */
18653949bee8SArchie Cobbs 	if (priv->numActiveLinks == 0) {
1866164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
18673949bee8SArchie Cobbs 		return (ENETDOWN);
18683949bee8SArchie Cobbs 	}
18693949bee8SArchie Cobbs 
1870164b576eSGleb Smirnoff 	/* Update stats. */
1871164b576eSGleb Smirnoff 	priv->bundleStats.xmitFrames++;
1872164b576eSGleb Smirnoff 	priv->bundleStats.xmitOctets += NGI_M(item)->m_pkthdr.len;
1873164b576eSGleb Smirnoff 
1874164b576eSGleb Smirnoff 	if (!priv->conf.enableMultilink)
1875164b576eSGleb Smirnoff 		return (ng_ppp_link_xmit(node, item, proto,
1876164b576eSGleb Smirnoff 		    priv->activeLinks[0]));
1877164b576eSGleb Smirnoff 
1878164b576eSGleb Smirnoff 	/* Extract mbuf. */
1879164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
1880164b576eSGleb Smirnoff 	NG_FREE_ITEM(item);
1881164b576eSGleb Smirnoff 
1882164b576eSGleb Smirnoff 	/* Prepend protocol number, possibly compressed. */
1883164b576eSGleb Smirnoff 	if ((m = ng_ppp_addproto(m, proto, 1)) == NULL)
1884164b576eSGleb Smirnoff 		return (ENOBUFS);
1885164b576eSGleb Smirnoff 
18863949bee8SArchie Cobbs 	/* Round-robin strategy */
1887164b576eSGleb Smirnoff 	if (priv->conf.enableRoundRobin ||
1888164b576eSGleb Smirnoff 	    (m->m_pkthdr.len < priv->numActiveLinks * MP_MIN_FRAG_LEN)) {
18893949bee8SArchie Cobbs 		activeLinkNum = priv->lastLink++ % priv->numActiveLinks;
18903949bee8SArchie Cobbs 		bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0]));
18913949bee8SArchie Cobbs 		distrib[activeLinkNum] = m->m_pkthdr.len;
18923949bee8SArchie Cobbs 		goto deliver;
18933949bee8SArchie Cobbs 	}
18943949bee8SArchie Cobbs 
18953949bee8SArchie Cobbs 	/* Strategy when all links are equivalent (optimize the common case) */
18963949bee8SArchie Cobbs 	if (priv->allLinksEqual) {
18973949bee8SArchie Cobbs 		const int fraction = m->m_pkthdr.len / priv->numActiveLinks;
18983949bee8SArchie Cobbs 		int i, remain;
18993949bee8SArchie Cobbs 
19003949bee8SArchie Cobbs 		for (i = 0; i < priv->numActiveLinks; i++)
19013949bee8SArchie Cobbs 			distrib[priv->lastLink++ % priv->numActiveLinks]
19023949bee8SArchie Cobbs 			    = fraction;
19033949bee8SArchie Cobbs 		remain = m->m_pkthdr.len - (fraction * priv->numActiveLinks);
19043949bee8SArchie Cobbs 		while (remain > 0) {
19053949bee8SArchie Cobbs 			distrib[priv->lastLink++ % priv->numActiveLinks]++;
19063949bee8SArchie Cobbs 			remain--;
19073949bee8SArchie Cobbs 		}
19083949bee8SArchie Cobbs 		goto deliver;
19093949bee8SArchie Cobbs 	}
19103949bee8SArchie Cobbs 
19113949bee8SArchie Cobbs 	/* Strategy when all links are not equivalent */
19123949bee8SArchie Cobbs 	ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib);
19133949bee8SArchie Cobbs 
19143949bee8SArchie Cobbs deliver:
19153949bee8SArchie Cobbs 	/* Send alloted portions of frame out on the link(s) */
19163949bee8SArchie Cobbs 	for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1;
19173949bee8SArchie Cobbs 	    activeLinkNum >= 0; activeLinkNum--) {
1918164b576eSGleb Smirnoff 		const uint16_t linkNum = priv->activeLinks[activeLinkNum];
1919a9b3dca5SArchie Cobbs 		struct ng_ppp_link *const link = &priv->links[linkNum];
19203949bee8SArchie Cobbs 
19213949bee8SArchie Cobbs 		/* Deliver fragment(s) out the next link */
19223949bee8SArchie Cobbs 		for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) {
19233949bee8SArchie Cobbs 			int len, lastFragment, error;
19243949bee8SArchie Cobbs 			struct mbuf *m2;
19253949bee8SArchie Cobbs 
19263949bee8SArchie Cobbs 			/* Calculate fragment length; don't exceed link MTU */
19273949bee8SArchie Cobbs 			len = distrib[activeLinkNum];
1928931c828aSArchie Cobbs 			if (len > link->conf.mru - hdr_len)
1929931c828aSArchie Cobbs 				len = link->conf.mru - hdr_len;
19303949bee8SArchie Cobbs 			distrib[activeLinkNum] -= len;
19313949bee8SArchie Cobbs 			lastFragment = (len == m->m_pkthdr.len);
19323949bee8SArchie Cobbs 
19333949bee8SArchie Cobbs 			/* Split off next fragment as "m2" */
19343949bee8SArchie Cobbs 			m2 = m;
19353949bee8SArchie Cobbs 			if (!lastFragment) {
1936a163d034SWarner Losh 				struct mbuf *n = m_split(m, len, M_DONTWAIT);
19373949bee8SArchie Cobbs 
19383949bee8SArchie Cobbs 				if (n == NULL) {
1939069154d5SJulian Elischer 					NG_FREE_M(m);
19403949bee8SArchie Cobbs 					return (ENOMEM);
19413949bee8SArchie Cobbs 				}
19423949bee8SArchie Cobbs 				m = n;
19433949bee8SArchie Cobbs 			}
19443949bee8SArchie Cobbs 
19453949bee8SArchie Cobbs 			/* Prepend MP header */
19463949bee8SArchie Cobbs 			if (priv->conf.xmitShortSeq) {
1947164b576eSGleb Smirnoff 				uint16_t shdr;
19483949bee8SArchie Cobbs 
1949a9b3dca5SArchie Cobbs 				shdr = priv->xseq;
1950a9b3dca5SArchie Cobbs 				priv->xseq =
19516f16db81SArchie Cobbs 				    (priv->xseq + 1) & MP_SHORT_SEQ_MASK;
19523949bee8SArchie Cobbs 				if (firstFragment)
19533949bee8SArchie Cobbs 					shdr |= MP_SHORT_FIRST_FLAG;
19543949bee8SArchie Cobbs 				if (lastFragment)
19553949bee8SArchie Cobbs 					shdr |= MP_SHORT_LAST_FLAG;
1956d690a6e7SArchie Cobbs 				shdr = htons(shdr);
1957d690a6e7SArchie Cobbs 				m2 = ng_ppp_prepend(m2, &shdr, 2);
19583949bee8SArchie Cobbs 			} else {
1959164b576eSGleb Smirnoff 				uint32_t lhdr;
19603949bee8SArchie Cobbs 
1961a9b3dca5SArchie Cobbs 				lhdr = priv->xseq;
1962a9b3dca5SArchie Cobbs 				priv->xseq =
19636f16db81SArchie Cobbs 				    (priv->xseq + 1) & MP_LONG_SEQ_MASK;
19643949bee8SArchie Cobbs 				if (firstFragment)
19653949bee8SArchie Cobbs 					lhdr |= MP_LONG_FIRST_FLAG;
19663949bee8SArchie Cobbs 				if (lastFragment)
19673949bee8SArchie Cobbs 					lhdr |= MP_LONG_LAST_FLAG;
1968d690a6e7SArchie Cobbs 				lhdr = htonl(lhdr);
1969d690a6e7SArchie Cobbs 				m2 = ng_ppp_prepend(m2, &lhdr, 4);
19703949bee8SArchie Cobbs 			}
19713949bee8SArchie Cobbs 			if (m2 == NULL) {
19723949bee8SArchie Cobbs 				if (!lastFragment)
19733949bee8SArchie Cobbs 					m_freem(m);
19743949bee8SArchie Cobbs 				return (ENOBUFS);
19753949bee8SArchie Cobbs 			}
19763949bee8SArchie Cobbs 
19773949bee8SArchie Cobbs 			/* Send fragment */
1978d0784b47SGleb Smirnoff 			if ((item = ng_package_data(m2, NG_NOFLAGS)) != NULL) {
1979164b576eSGleb Smirnoff 				error = ng_ppp_link_xmit(node, item, PROT_MP,
1980164b576eSGleb Smirnoff 					    linkNum);
19813949bee8SArchie Cobbs 				if (error != 0) {
19823ca24c28SJulian Elischer 					if (!lastFragment)
1983069154d5SJulian Elischer 						NG_FREE_M(m);
19843949bee8SArchie Cobbs 					return (error);
19853949bee8SArchie Cobbs 				}
19863949bee8SArchie Cobbs 			}
19873949bee8SArchie Cobbs 		}
1988d0784b47SGleb Smirnoff 	}
19893949bee8SArchie Cobbs 
19903949bee8SArchie Cobbs 	/* Done */
19913949bee8SArchie Cobbs 	return (0);
19923949bee8SArchie Cobbs }
19933949bee8SArchie Cobbs 
19943949bee8SArchie Cobbs /*
19953949bee8SArchie Cobbs  * Computing the optimal fragmentation
19963949bee8SArchie Cobbs  * -----------------------------------
19973949bee8SArchie Cobbs  *
19983949bee8SArchie Cobbs  * This routine tries to compute the optimal fragmentation pattern based
19993949bee8SArchie Cobbs  * on each link's latency, bandwidth, and calculated additional latency.
20003949bee8SArchie Cobbs  * The latter quantity is the additional latency caused by previously
20013949bee8SArchie Cobbs  * written data that has not been transmitted yet.
20023949bee8SArchie Cobbs  *
20033949bee8SArchie Cobbs  * This algorithm is only useful when not all of the links have the
20043949bee8SArchie Cobbs  * same latency and bandwidth values.
20053949bee8SArchie Cobbs  *
20063949bee8SArchie Cobbs  * The essential idea is to make the last bit of each fragment of the
20073949bee8SArchie Cobbs  * frame arrive at the opposite end at the exact same time. This greedy
20083949bee8SArchie Cobbs  * algorithm is optimal, in that no other scheduling could result in any
20093949bee8SArchie Cobbs  * packet arriving any sooner unless packets are delivered out of order.
20103949bee8SArchie Cobbs  *
20113949bee8SArchie Cobbs  * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and
20123949bee8SArchie Cobbs  * latency l_i (in miliseconds). Consider the function function f_i(t)
20133949bee8SArchie Cobbs  * which is equal to the number of bytes that will have arrived at
20143949bee8SArchie Cobbs  * the peer after t miliseconds if we start writing continuously at
20153949bee8SArchie Cobbs  * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i).
20163949bee8SArchie Cobbs  * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i).
20173949bee8SArchie Cobbs  * Note that the y-intersect is always <= zero because latency can't be
20183949bee8SArchie Cobbs  * negative.  Note also that really the function is f_i(t) except when
20193949bee8SArchie Cobbs  * f_i(t) is negative, in which case the function is zero.  To take
20203949bee8SArchie Cobbs  * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }.
20213949bee8SArchie Cobbs  * So the actual number of bytes that will have arrived at the peer after
20223949bee8SArchie Cobbs  * t miliseconds is f_i(t) * Q_i(t).
20233949bee8SArchie Cobbs  *
20243949bee8SArchie Cobbs  * At any given time, each link has some additional latency a_i >= 0
20253949bee8SArchie Cobbs  * due to previously written fragment(s) which are still in the queue.
20263949bee8SArchie Cobbs  * This value is easily computed from the time since last transmission,
20273949bee8SArchie Cobbs  * the previous latency value, the number of bytes written, and the
20283949bee8SArchie Cobbs  * link's bandwidth.
20293949bee8SArchie Cobbs  *
20303949bee8SArchie Cobbs  * Assume that l_i includes any a_i already, and that the links are
20313949bee8SArchie Cobbs  * sorted by latency, so that l_i <= l_{i+1}.
20323949bee8SArchie Cobbs  *
20333949bee8SArchie Cobbs  * Let N be the total number of bytes in the current frame we are sending.
20343949bee8SArchie Cobbs  *
20353949bee8SArchie Cobbs  * Suppose we were to start writing bytes at time t = 0 on all links
20363949bee8SArchie Cobbs  * simultaneously, which is the most we can possibly do.  Then let
20373949bee8SArchie Cobbs  * F(t) be equal to the total number of bytes received by the peer
20383949bee8SArchie Cobbs  * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)).
20393949bee8SArchie Cobbs  *
20403949bee8SArchie Cobbs  * Our goal is simply this: fragment the frame across the links such
20413949bee8SArchie Cobbs  * that the peer is able to reconstruct the completed frame as soon as
20423949bee8SArchie Cobbs  * possible, i.e., at the least possible value of t. Call this value t_0.
20433949bee8SArchie Cobbs  *
20443949bee8SArchie Cobbs  * Then it follows that F(t_0) = N. Our strategy is first to find the value
20453949bee8SArchie Cobbs  * of t_0, and then deduce how many bytes to write to each link.
20463949bee8SArchie Cobbs  *
20473949bee8SArchie Cobbs  * Rewriting F(t_0):
20483949bee8SArchie Cobbs  *
20493949bee8SArchie Cobbs  *   t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) )
20503949bee8SArchie Cobbs  *
20513949bee8SArchie Cobbs  * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will
20523949bee8SArchie Cobbs  * lie in one of these ranges.  To find it, we just need to find the i such
20533949bee8SArchie Cobbs  * that F(l_i) <= N <= F(l_{i+1}).  Then we compute all the constant values
20543949bee8SArchie Cobbs  * for Q_i() in this range, plug in the remaining values, solving for t_0.
20553949bee8SArchie Cobbs  *
20563949bee8SArchie Cobbs  * Once t_0 is known, then the number of bytes to send on link i is
20573949bee8SArchie Cobbs  * just f_i(t_0) * Q_i(t_0).
20583949bee8SArchie Cobbs  *
20593949bee8SArchie Cobbs  * In other words, we start allocating bytes to the links one at a time.
20603949bee8SArchie Cobbs  * We keep adding links until the frame is completely sent.  Some links
20613949bee8SArchie Cobbs  * may not get any bytes because their latency is too high.
20623949bee8SArchie Cobbs  *
20633949bee8SArchie Cobbs  * Is all this work really worth the trouble?  Depends on the situation.
20643949bee8SArchie Cobbs  * The bigger the ratio of computer speed to link speed, and the more
20653949bee8SArchie Cobbs  * important total bundle latency is (e.g., for interactive response time),
20663949bee8SArchie Cobbs  * the more it's worth it.  There is however the cost of calling this
20673949bee8SArchie Cobbs  * function for every frame.  The running time is O(n^2) where n is the
20683949bee8SArchie Cobbs  * number of links that receive a non-zero number of bytes.
20693949bee8SArchie Cobbs  *
20703949bee8SArchie Cobbs  * Since latency is measured in miliseconds, the "resolution" of this
20713949bee8SArchie Cobbs  * algorithm is one milisecond.
20723949bee8SArchie Cobbs  *
20733949bee8SArchie Cobbs  * To avoid this algorithm altogether, configure all links to have the
20743949bee8SArchie Cobbs  * same latency and bandwidth.
20753949bee8SArchie Cobbs  */
20763949bee8SArchie Cobbs static void
20773949bee8SArchie Cobbs ng_ppp_mp_strategy(node_p node, int len, int *distrib)
20783949bee8SArchie Cobbs {
207930400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
20803949bee8SArchie Cobbs 	int latency[NG_PPP_MAX_LINKS];
20813949bee8SArchie Cobbs 	int sortByLatency[NG_PPP_MAX_LINKS];
2082a9b3dca5SArchie Cobbs 	int activeLinkNum;
20833949bee8SArchie Cobbs 	int t0, total, topSum, botSum;
20843949bee8SArchie Cobbs 	struct timeval now;
20853949bee8SArchie Cobbs 	int i, numFragments;
20863949bee8SArchie Cobbs 
20873949bee8SArchie Cobbs 	/* If only one link, this gets real easy */
20883949bee8SArchie Cobbs 	if (priv->numActiveLinks == 1) {
20893949bee8SArchie Cobbs 		distrib[0] = len;
20903949bee8SArchie Cobbs 		return;
20913949bee8SArchie Cobbs 	}
20923949bee8SArchie Cobbs 
20933949bee8SArchie Cobbs 	/* Get current time */
2094a9b3dca5SArchie Cobbs 	getmicrouptime(&now);
20953949bee8SArchie Cobbs 
20963949bee8SArchie Cobbs 	/* Compute latencies for each link at this point in time */
20973949bee8SArchie Cobbs 	for (activeLinkNum = 0;
20983949bee8SArchie Cobbs 	    activeLinkNum < priv->numActiveLinks; activeLinkNum++) {
2099a9b3dca5SArchie Cobbs 		struct ng_ppp_link *alink;
21003949bee8SArchie Cobbs 		struct timeval diff;
21013949bee8SArchie Cobbs 		int xmitBytes;
21023949bee8SArchie Cobbs 
21033949bee8SArchie Cobbs 		/* Start with base latency value */
2104a9b3dca5SArchie Cobbs 		alink = &priv->links[priv->activeLinks[activeLinkNum]];
210549728ffcSBjoern A. Zeeb 		latency[activeLinkNum] = alink->latency;
21063949bee8SArchie Cobbs 		sortByLatency[activeLinkNum] = activeLinkNum;	/* see below */
21073949bee8SArchie Cobbs 
21083949bee8SArchie Cobbs 		/* Any additional latency? */
2109a9b3dca5SArchie Cobbs 		if (alink->bytesInQueue == 0)
21103949bee8SArchie Cobbs 			continue;
21113949bee8SArchie Cobbs 
21123949bee8SArchie Cobbs 		/* Compute time delta since last write */
21133949bee8SArchie Cobbs 		diff = now;
2114a9b3dca5SArchie Cobbs 		timevalsub(&diff, &alink->lastWrite);
2115fd58342cSAlexander Motin 
2116fd58342cSAlexander Motin 		/* alink->bytesInQueue will be changed, mark change time. */
2117fd58342cSAlexander Motin 		alink->lastWrite = now;
2118fd58342cSAlexander Motin 
21193949bee8SArchie Cobbs 		if (now.tv_sec < 0 || diff.tv_sec >= 10) {	/* sanity */
2120a9b3dca5SArchie Cobbs 			alink->bytesInQueue = 0;
21213949bee8SArchie Cobbs 			continue;
21223949bee8SArchie Cobbs 		}
21233949bee8SArchie Cobbs 
21243949bee8SArchie Cobbs 		/* How many bytes could have transmitted since last write? */
2125fd58342cSAlexander Motin 		xmitBytes = (alink->conf.bandwidth * 10 * diff.tv_sec)
2126a9b3dca5SArchie Cobbs 		    + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100;
2127a9b3dca5SArchie Cobbs 		alink->bytesInQueue -= xmitBytes;
2128a9b3dca5SArchie Cobbs 		if (alink->bytesInQueue < 0)
2129a9b3dca5SArchie Cobbs 			alink->bytesInQueue = 0;
21303949bee8SArchie Cobbs 		else
21313949bee8SArchie Cobbs 			latency[activeLinkNum] +=
2132a9b3dca5SArchie Cobbs 			    (100 * alink->bytesInQueue) / alink->conf.bandwidth;
21333949bee8SArchie Cobbs 	}
21343949bee8SArchie Cobbs 
2135a9b3dca5SArchie Cobbs 	/* Sort active links by latency */
21368250de83SGleb Smirnoff 	qsort_r(sortByLatency,
21378250de83SGleb Smirnoff 	    priv->numActiveLinks, sizeof(*sortByLatency), latency, ng_ppp_intcmp);
21383949bee8SArchie Cobbs 
21393949bee8SArchie Cobbs 	/* Find the interval we need (add links in sortByLatency[] order) */
21403949bee8SArchie Cobbs 	for (numFragments = 1;
21413949bee8SArchie Cobbs 	    numFragments < priv->numActiveLinks; numFragments++) {
21423949bee8SArchie Cobbs 		for (total = i = 0; i < numFragments; i++) {
21433949bee8SArchie Cobbs 			int flowTime;
21443949bee8SArchie Cobbs 
21453949bee8SArchie Cobbs 			flowTime = latency[sortByLatency[numFragments]]
21463949bee8SArchie Cobbs 			    - latency[sortByLatency[i]];
2147a9b3dca5SArchie Cobbs 			total += ((flowTime * priv->links[
2148a9b3dca5SArchie Cobbs 			    priv->activeLinks[sortByLatency[i]]].conf.bandwidth)
21493949bee8SArchie Cobbs 			    	+ 99) / 100;
21503949bee8SArchie Cobbs 		}
21513949bee8SArchie Cobbs 		if (total >= len)
21523949bee8SArchie Cobbs 			break;
21533949bee8SArchie Cobbs 	}
21543949bee8SArchie Cobbs 
21553949bee8SArchie Cobbs 	/* Solve for t_0 in that interval */
21563949bee8SArchie Cobbs 	for (topSum = botSum = i = 0; i < numFragments; i++) {
2157a9b3dca5SArchie Cobbs 		int bw = priv->links[
2158a9b3dca5SArchie Cobbs 		    priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
21593949bee8SArchie Cobbs 
21603949bee8SArchie Cobbs 		topSum += latency[sortByLatency[i]] * bw;	/* / 100 */
21613949bee8SArchie Cobbs 		botSum += bw;					/* / 100 */
21623949bee8SArchie Cobbs 	}
21633949bee8SArchie Cobbs 	t0 = ((len * 100) + topSum + botSum / 2) / botSum;
21643949bee8SArchie Cobbs 
21653949bee8SArchie Cobbs 	/* Compute f_i(t_0) all i */
21663949bee8SArchie Cobbs 	bzero(distrib, priv->numActiveLinks * sizeof(*distrib));
21673949bee8SArchie Cobbs 	for (total = i = 0; i < numFragments; i++) {
2168a9b3dca5SArchie Cobbs 		int bw = priv->links[
2169a9b3dca5SArchie Cobbs 		    priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
21703949bee8SArchie Cobbs 
21713949bee8SArchie Cobbs 		distrib[sortByLatency[i]] =
21723949bee8SArchie Cobbs 		    (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100;
21733949bee8SArchie Cobbs 		total += distrib[sortByLatency[i]];
21743949bee8SArchie Cobbs 	}
21753949bee8SArchie Cobbs 
2176fb1fc8abSArchie Cobbs 	/* Deal with any rounding error */
2177fb1fc8abSArchie Cobbs 	if (total < len) {
2178a9b3dca5SArchie Cobbs 		struct ng_ppp_link *fastLink =
2179a9b3dca5SArchie Cobbs 		    &priv->links[priv->activeLinks[sortByLatency[0]]];
21803949bee8SArchie Cobbs 		int fast = 0;
21813949bee8SArchie Cobbs 
2182fb1fc8abSArchie Cobbs 		/* Find the fastest link */
21833949bee8SArchie Cobbs 		for (i = 1; i < numFragments; i++) {
2184a9b3dca5SArchie Cobbs 			struct ng_ppp_link *const link =
2185a9b3dca5SArchie Cobbs 			    &priv->links[priv->activeLinks[sortByLatency[i]]];
2186a9b3dca5SArchie Cobbs 
2187a9b3dca5SArchie Cobbs 			if (link->conf.bandwidth > fastLink->conf.bandwidth) {
21883949bee8SArchie Cobbs 				fast = i;
2189a9b3dca5SArchie Cobbs 				fastLink = link;
2190a9b3dca5SArchie Cobbs 			}
21913949bee8SArchie Cobbs 		}
21923949bee8SArchie Cobbs 		distrib[sortByLatency[fast]] += len - total;
2193fb1fc8abSArchie Cobbs 	} else while (total > len) {
2194a9b3dca5SArchie Cobbs 		struct ng_ppp_link *slowLink =
2195a9b3dca5SArchie Cobbs 		    &priv->links[priv->activeLinks[sortByLatency[0]]];
2196fb1fc8abSArchie Cobbs 		int delta, slow = 0;
21973949bee8SArchie Cobbs 
2198fb1fc8abSArchie Cobbs 		/* Find the slowest link that still has bytes to remove */
2199fb1fc8abSArchie Cobbs 		for (i = 1; i < numFragments; i++) {
2200a9b3dca5SArchie Cobbs 			struct ng_ppp_link *const link =
2201a9b3dca5SArchie Cobbs 			    &priv->links[priv->activeLinks[sortByLatency[i]]];
2202a9b3dca5SArchie Cobbs 
2203fb1fc8abSArchie Cobbs 			if (distrib[sortByLatency[slow]] == 0
2204fb1fc8abSArchie Cobbs 			  || (distrib[sortByLatency[i]] > 0
2205a9b3dca5SArchie Cobbs 			    && link->conf.bandwidth <
2206a9b3dca5SArchie Cobbs 			      slowLink->conf.bandwidth)) {
2207fb1fc8abSArchie Cobbs 				slow = i;
2208a9b3dca5SArchie Cobbs 				slowLink = link;
2209a9b3dca5SArchie Cobbs 			}
2210fb1fc8abSArchie Cobbs 		}
2211fb1fc8abSArchie Cobbs 		delta = total - len;
2212fb1fc8abSArchie Cobbs 		if (delta > distrib[sortByLatency[slow]])
2213fb1fc8abSArchie Cobbs 			delta = distrib[sortByLatency[slow]];
2214fb1fc8abSArchie Cobbs 		distrib[sortByLatency[slow]] -= delta;
2215fb1fc8abSArchie Cobbs 		total -= delta;
22163949bee8SArchie Cobbs 	}
22173949bee8SArchie Cobbs }
22183949bee8SArchie Cobbs 
22193949bee8SArchie Cobbs /*
22203949bee8SArchie Cobbs  * Compare two integers
22213949bee8SArchie Cobbs  */
22223949bee8SArchie Cobbs static int
22238250de83SGleb Smirnoff ng_ppp_intcmp(void *latency, const void *v1, const void *v2)
22243949bee8SArchie Cobbs {
22253949bee8SArchie Cobbs 	const int index1 = *((const int *) v1);
22263949bee8SArchie Cobbs 	const int index2 = *((const int *) v2);
22273949bee8SArchie Cobbs 
22288250de83SGleb Smirnoff 	return ((int *)latency)[index1] - ((int *)latency)[index2];
22293949bee8SArchie Cobbs }
22303949bee8SArchie Cobbs 
22313949bee8SArchie Cobbs /*
22323949bee8SArchie Cobbs  * Prepend a possibly compressed PPP protocol number in front of a frame
22333949bee8SArchie Cobbs  */
22343949bee8SArchie Cobbs static struct mbuf *
2235164b576eSGleb Smirnoff ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK)
22363949bee8SArchie Cobbs {
2237d690a6e7SArchie Cobbs 	if (compOK && PROT_COMPRESSABLE(proto)) {
2238164b576eSGleb Smirnoff 		uint8_t pbyte = (uint8_t)proto;
22393949bee8SArchie Cobbs 
2240d690a6e7SArchie Cobbs 		return ng_ppp_prepend(m, &pbyte, 1);
2241d690a6e7SArchie Cobbs 	} else {
2242164b576eSGleb Smirnoff 		uint16_t pword = htons((uint16_t)proto);
2243d690a6e7SArchie Cobbs 
2244d690a6e7SArchie Cobbs 		return ng_ppp_prepend(m, &pword, 2);
2245d690a6e7SArchie Cobbs 	}
2246d690a6e7SArchie Cobbs }
2247d690a6e7SArchie Cobbs 
2248d690a6e7SArchie Cobbs /*
2249164b576eSGleb Smirnoff  * Cut a possibly compressed PPP protocol number from the front of a frame.
2250164b576eSGleb Smirnoff  */
2251164b576eSGleb Smirnoff static struct mbuf *
2252164b576eSGleb Smirnoff ng_ppp_cutproto(struct mbuf *m, uint16_t *proto)
2253164b576eSGleb Smirnoff {
2254164b576eSGleb Smirnoff 
2255164b576eSGleb Smirnoff 	*proto = 0;
2256164b576eSGleb Smirnoff 	if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL)
2257164b576eSGleb Smirnoff 		return (NULL);
2258164b576eSGleb Smirnoff 
2259164b576eSGleb Smirnoff 	*proto = *mtod(m, uint8_t *);
2260164b576eSGleb Smirnoff 	m_adj(m, 1);
2261164b576eSGleb Smirnoff 
2262164b576eSGleb Smirnoff 	if (!PROT_VALID(*proto)) {
2263164b576eSGleb Smirnoff 		if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL)
2264164b576eSGleb Smirnoff 			return (NULL);
2265164b576eSGleb Smirnoff 
2266164b576eSGleb Smirnoff 		*proto = (*proto << 8) + *mtod(m, uint8_t *);
2267164b576eSGleb Smirnoff 		m_adj(m, 1);
2268164b576eSGleb Smirnoff 	}
2269164b576eSGleb Smirnoff 
2270164b576eSGleb Smirnoff 	return (m);
2271164b576eSGleb Smirnoff }
2272164b576eSGleb Smirnoff 
2273164b576eSGleb Smirnoff /*
2274164b576eSGleb Smirnoff  * Prepend some bytes to an mbuf.
2275d690a6e7SArchie Cobbs  */
2276d690a6e7SArchie Cobbs static struct mbuf *
2277d690a6e7SArchie Cobbs ng_ppp_prepend(struct mbuf *m, const void *buf, int len)
2278d690a6e7SArchie Cobbs {
2279a163d034SWarner Losh 	M_PREPEND(m, len, M_DONTWAIT);
2280d690a6e7SArchie Cobbs 	if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL))
22814cf49a43SJulian Elischer 		return (NULL);
2282164b576eSGleb Smirnoff 	bcopy(buf, mtod(m, uint8_t *), len);
22833949bee8SArchie Cobbs 	return (m);
22843949bee8SArchie Cobbs }
22853949bee8SArchie Cobbs 
22863949bee8SArchie Cobbs /*
22873949bee8SArchie Cobbs  * Update private information that is derived from other private information
22883949bee8SArchie Cobbs  */
22893949bee8SArchie Cobbs static void
22903949bee8SArchie Cobbs ng_ppp_update(node_p node, int newConf)
22913949bee8SArchie Cobbs {
229230400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
22933949bee8SArchie Cobbs 	int i;
22943949bee8SArchie Cobbs 
22953949bee8SArchie Cobbs 	/* Update active status for VJ Compression */
22963949bee8SArchie Cobbs 	priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL
22973949bee8SArchie Cobbs 	    && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL
22983949bee8SArchie Cobbs 	    && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL
22993949bee8SArchie Cobbs 	    && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL;
23003949bee8SArchie Cobbs 
23013949bee8SArchie Cobbs 	/* Increase latency for each link an amount equal to one MP header */
23023949bee8SArchie Cobbs 	if (newConf) {
23033949bee8SArchie Cobbs 		for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
23043949bee8SArchie Cobbs 			int hdrBytes;
23053949bee8SArchie Cobbs 
2306fd58342cSAlexander Motin 			if (priv->links[i].conf.bandwidth == 0)
2307fd58342cSAlexander Motin 			    continue;
2308fd58342cSAlexander Motin 
2309fd58342cSAlexander Motin 			hdrBytes = MP_AVERAGE_LINK_OVERHEAD
2310fd58342cSAlexander Motin 			    + (priv->links[i].conf.enableACFComp ? 0 : 2)
2311a9b3dca5SArchie Cobbs 			    + (priv->links[i].conf.enableProtoComp ? 1 : 2)
23123949bee8SArchie Cobbs 			    + (priv->conf.xmitShortSeq ? 2 : 4);
231349728ffcSBjoern A. Zeeb 			priv->links[i].latency =
231449728ffcSBjoern A. Zeeb 			    priv->links[i].conf.latency +
2315fd58342cSAlexander Motin 			    (hdrBytes / priv->links[i].conf.bandwidth + 50) / 100;
23163949bee8SArchie Cobbs 		}
23173949bee8SArchie Cobbs 	}
23183949bee8SArchie Cobbs 
23193949bee8SArchie Cobbs 	/* Update list of active links */
23203949bee8SArchie Cobbs 	bzero(&priv->activeLinks, sizeof(priv->activeLinks));
23213949bee8SArchie Cobbs 	priv->numActiveLinks = 0;
23223949bee8SArchie Cobbs 	priv->allLinksEqual = 1;
23233949bee8SArchie Cobbs 	for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
2324a9b3dca5SArchie Cobbs 		struct ng_ppp_link *const link = &priv->links[i];
23250e11d0f3SArchie Cobbs 
2326a9b3dca5SArchie Cobbs 		/* Is link active? */
2327a9b3dca5SArchie Cobbs 		if (link->conf.enableLink && link->hook != NULL) {
2328a9b3dca5SArchie Cobbs 			struct ng_ppp_link *link0;
2329a9b3dca5SArchie Cobbs 
2330a9b3dca5SArchie Cobbs 			/* Add link to list of active links */
23313949bee8SArchie Cobbs 			priv->activeLinks[priv->numActiveLinks++] = i;
2332a9b3dca5SArchie Cobbs 			link0 = &priv->links[priv->activeLinks[0]];
2333a9b3dca5SArchie Cobbs 
2334a9b3dca5SArchie Cobbs 			/* Determine if all links are still equal */
233549728ffcSBjoern A. Zeeb 			if (link->latency != link0->latency
2336a9b3dca5SArchie Cobbs 			  || link->conf.bandwidth != link0->conf.bandwidth)
23373949bee8SArchie Cobbs 				priv->allLinksEqual = 0;
2338a9b3dca5SArchie Cobbs 
2339a9b3dca5SArchie Cobbs 			/* Initialize rec'd sequence number */
2340a9b3dca5SArchie Cobbs 			if (link->seq == MP_NOSEQ) {
2341a9b3dca5SArchie Cobbs 				link->seq = (link == link0) ?
2342a9b3dca5SArchie Cobbs 				    MP_INITIAL_SEQ : link0->seq;
23433949bee8SArchie Cobbs 			}
2344a9b3dca5SArchie Cobbs 		} else
2345a9b3dca5SArchie Cobbs 			link->seq = MP_NOSEQ;
23463949bee8SArchie Cobbs 	}
23473949bee8SArchie Cobbs 
2348a9b3dca5SArchie Cobbs 	/* Update MP state as multi-link is active or not */
2349a9b3dca5SArchie Cobbs 	if (priv->conf.enableMultilink && priv->numActiveLinks > 0)
2350a9b3dca5SArchie Cobbs 		ng_ppp_start_frag_timer(node);
2351a9b3dca5SArchie Cobbs 	else {
2352a9b3dca5SArchie Cobbs 		ng_ppp_stop_frag_timer(node);
2353a9b3dca5SArchie Cobbs 		ng_ppp_frag_reset(node);
2354a9b3dca5SArchie Cobbs 		priv->xseq = MP_INITIAL_SEQ;
2355a9b3dca5SArchie Cobbs 		priv->mseq = MP_INITIAL_SEQ;
2356a9b3dca5SArchie Cobbs 		for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
2357a9b3dca5SArchie Cobbs 			struct ng_ppp_link *const link = &priv->links[i];
2358a9b3dca5SArchie Cobbs 
2359a9b3dca5SArchie Cobbs 			bzero(&link->lastWrite, sizeof(link->lastWrite));
2360a9b3dca5SArchie Cobbs 			link->bytesInQueue = 0;
2361a9b3dca5SArchie Cobbs 			link->seq = MP_NOSEQ;
2362a9b3dca5SArchie Cobbs 		}
23633949bee8SArchie Cobbs 	}
23643949bee8SArchie Cobbs }
23653949bee8SArchie Cobbs 
23663949bee8SArchie Cobbs /*
23673949bee8SArchie Cobbs  * Determine if a new configuration would represent a valid change
23683949bee8SArchie Cobbs  * from the current configuration and link activity status.
23693949bee8SArchie Cobbs  */
23703949bee8SArchie Cobbs static int
2371a9b3dca5SArchie Cobbs ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf)
23723949bee8SArchie Cobbs {
237330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
23743949bee8SArchie Cobbs 	int i, newNumLinksActive;
23753949bee8SArchie Cobbs 
23763949bee8SArchie Cobbs 	/* Check per-link config and count how many links would be active */
23773949bee8SArchie Cobbs 	for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) {
2378a9b3dca5SArchie Cobbs 		if (newConf->links[i].enableLink && priv->links[i].hook != NULL)
2379e149c4e2SArchie Cobbs 			newNumLinksActive++;
2380e149c4e2SArchie Cobbs 		if (!newConf->links[i].enableLink)
2381e149c4e2SArchie Cobbs 			continue;
23823949bee8SArchie Cobbs 		if (newConf->links[i].mru < MP_MIN_LINK_MRU)
23833949bee8SArchie Cobbs 			return (0);
23843949bee8SArchie Cobbs 		if (newConf->links[i].bandwidth == 0)
23853949bee8SArchie Cobbs 			return (0);
23863949bee8SArchie Cobbs 		if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH)
23873949bee8SArchie Cobbs 			return (0);
23883949bee8SArchie Cobbs 		if (newConf->links[i].latency > NG_PPP_MAX_LATENCY)
23893949bee8SArchie Cobbs 			return (0);
23903949bee8SArchie Cobbs 	}
23913949bee8SArchie Cobbs 
23923949bee8SArchie Cobbs 	/* Check bundle parameters */
2393a9b3dca5SArchie Cobbs 	if (newConf->bund.enableMultilink && newConf->bund.mrru < MP_MIN_MRRU)
23943949bee8SArchie Cobbs 		return (0);
23953949bee8SArchie Cobbs 
23963949bee8SArchie Cobbs 	/* Disallow changes to multi-link configuration while MP is active */
23973949bee8SArchie Cobbs 	if (priv->numActiveLinks > 0 && newNumLinksActive > 0) {
2398a9b3dca5SArchie Cobbs 		if (!priv->conf.enableMultilink
2399a9b3dca5SArchie Cobbs 				!= !newConf->bund.enableMultilink
2400a9b3dca5SArchie Cobbs 		    || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq
2401a9b3dca5SArchie Cobbs 		    || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq)
24023949bee8SArchie Cobbs 			return (0);
24033949bee8SArchie Cobbs 	}
24043949bee8SArchie Cobbs 
2405e149c4e2SArchie Cobbs 	/* At most one link can be active unless multi-link is enabled */
2406a9b3dca5SArchie Cobbs 	if (!newConf->bund.enableMultilink && newNumLinksActive > 1)
2407e149c4e2SArchie Cobbs 		return (0);
2408e149c4e2SArchie Cobbs 
2409e149c4e2SArchie Cobbs 	/* Configuration change would be valid */
24103949bee8SArchie Cobbs 	return (1);
24113949bee8SArchie Cobbs }
24123949bee8SArchie Cobbs 
24133949bee8SArchie Cobbs /*
24143949bee8SArchie Cobbs  * Free all entries in the fragment queue
24153949bee8SArchie Cobbs  */
24163949bee8SArchie Cobbs static void
2417a9b3dca5SArchie Cobbs ng_ppp_frag_reset(node_p node)
24183949bee8SArchie Cobbs {
241930400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
24201e7a9f72SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext;
24213949bee8SArchie Cobbs 
24225f90cac7SKirk McKusick 	for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) {
24235f90cac7SKirk McKusick 		qnext = TAILQ_NEXT(qent, f_qent);
2424069154d5SJulian Elischer 		NG_FREE_M(qent->data);
24259c8c302fSJulian Elischer 		FREE(qent, M_NETGRAPH_PPP);
24263949bee8SArchie Cobbs 	}
24275f90cac7SKirk McKusick 	TAILQ_INIT(&priv->frags);
2428a9b3dca5SArchie Cobbs 	priv->qlen = 0;
2429a9b3dca5SArchie Cobbs }
2430a9b3dca5SArchie Cobbs 
2431a9b3dca5SArchie Cobbs /*
2432a9b3dca5SArchie Cobbs  * Start fragment queue timer
2433a9b3dca5SArchie Cobbs  */
2434a9b3dca5SArchie Cobbs static void
2435a9b3dca5SArchie Cobbs ng_ppp_start_frag_timer(node_p node)
2436a9b3dca5SArchie Cobbs {
243730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2438a9b3dca5SArchie Cobbs 
2439dc2f4d7fSGleb Smirnoff 	if (!(callout_pending(&priv->fragTimer)))
24409d952bd3SGleb Smirnoff 		ng_callout(&priv->fragTimer, node, NULL, MP_FRAGTIMER_INTERVAL,
24419d952bd3SGleb Smirnoff 		    ng_ppp_frag_timeout, NULL, 0);
2442a9b3dca5SArchie Cobbs }
2443a9b3dca5SArchie Cobbs 
2444a9b3dca5SArchie Cobbs /*
2445a9b3dca5SArchie Cobbs  * Stop fragment queue timer
2446a9b3dca5SArchie Cobbs  */
2447a9b3dca5SArchie Cobbs static void
2448a9b3dca5SArchie Cobbs ng_ppp_stop_frag_timer(node_p node)
2449a9b3dca5SArchie Cobbs {
245030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2451a9b3dca5SArchie Cobbs 
2452dc2f4d7fSGleb Smirnoff 	if (callout_pending(&priv->fragTimer))
24539d952bd3SGleb Smirnoff 		ng_uncallout(&priv->fragTimer, node);
2454a9b3dca5SArchie Cobbs }
2455