xref: /freebsd/sys/netgraph/ng_ppp.c (revision 1ede983cc905643549d8cae56a9d0e28fc68375f)
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 */
199d6fe462aSAlexander Motin 	struct ng_ppp_link_stat64	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 */
210d6fe462aSAlexander Motin 	struct ng_ppp_link_stat64	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 */
22093caf2e2SAlexander Motin 	struct ng_ppp_frag	fragsmem[MP_MAX_QUEUE_LEN]; /* fragments storage */
2215f90cac7SKirk McKusick 	TAILQ_HEAD(ng_ppp_fraglist, ng_ppp_frag)	/* fragment queue */
222a9b3dca5SArchie Cobbs 				frags;
22393caf2e2SAlexander Motin 	TAILQ_HEAD(ng_ppp_fragfreelist, ng_ppp_frag)	/* free fragment queue */
22493caf2e2SAlexander Motin 				fragsfree;
2259d952bd3SGleb Smirnoff 	struct callout		fragTimer;		/* fraq queue check */
226e89c1507SAlexander Motin 	struct mtx		rmtx;			/* recv mutex */
227e89c1507SAlexander Motin 	struct mtx		xmtx;			/* xmit mutex */
2284cf49a43SJulian Elischer };
22962838faeSArchie Cobbs typedef struct ng_ppp_private *priv_p;
2304cf49a43SJulian Elischer 
2314cf49a43SJulian Elischer /* Netgraph node methods */
23274f5c6aaSJulian Elischer static ng_constructor_t	ng_ppp_constructor;
23374f5c6aaSJulian Elischer static ng_rcvmsg_t	ng_ppp_rcvmsg;
234069154d5SJulian Elischer static ng_shutdown_t	ng_ppp_shutdown;
23574f5c6aaSJulian Elischer static ng_newhook_t	ng_ppp_newhook;
23674f5c6aaSJulian Elischer static ng_rcvdata_t	ng_ppp_rcvdata;
23774f5c6aaSJulian Elischer static ng_disconnect_t	ng_ppp_disconnect;
2384cf49a43SJulian Elischer 
239164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_inet;
240164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_ipv6;
241164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_ipx;
242164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_atalk;
243164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_bypass;
244164b576eSGleb Smirnoff 
245164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_vjc_ip;
246164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_vjc_comp;
247164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_vjc_uncomp;
248164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_vjc_vjip;
249164b576eSGleb Smirnoff 
250164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_compress;
251164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_decompress;
252164b576eSGleb Smirnoff 
253164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_encrypt;
254164b576eSGleb Smirnoff static ng_rcvdata_t	ng_ppp_rcvdata_decrypt;
255164b576eSGleb Smirnoff 
256164b576eSGleb Smirnoff /* We use integer indicies to refer to the non-link hooks. */
257164b576eSGleb Smirnoff static const struct {
258164b576eSGleb Smirnoff 	char *const name;
259164b576eSGleb Smirnoff 	ng_rcvdata_t *fn;
260164b576eSGleb Smirnoff } ng_ppp_hook_names[] = {
261164b576eSGleb Smirnoff #define HOOK_INDEX_ATALK	0
262164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_ATALK,	ng_ppp_rcvdata_atalk },
263164b576eSGleb Smirnoff #define HOOK_INDEX_BYPASS	1
264164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_BYPASS,	ng_ppp_rcvdata_bypass },
265164b576eSGleb Smirnoff #define HOOK_INDEX_COMPRESS	2
266164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_COMPRESS,	ng_ppp_rcvdata_compress },
267164b576eSGleb Smirnoff #define HOOK_INDEX_ENCRYPT	3
268164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_ENCRYPT,	ng_ppp_rcvdata_encrypt },
269164b576eSGleb Smirnoff #define HOOK_INDEX_DECOMPRESS	4
270164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_DECOMPRESS, ng_ppp_rcvdata_decompress },
271164b576eSGleb Smirnoff #define HOOK_INDEX_DECRYPT	5
272164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_DECRYPT,	ng_ppp_rcvdata_decrypt },
273164b576eSGleb Smirnoff #define HOOK_INDEX_INET		6
274164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_INET,	ng_ppp_rcvdata_inet },
275164b576eSGleb Smirnoff #define HOOK_INDEX_IPX		7
276164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_IPX,	ng_ppp_rcvdata_ipx },
277164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_COMP	8
278164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_VJC_COMP,	ng_ppp_rcvdata_vjc_comp },
279164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_IP	9
280164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_VJC_IP,	ng_ppp_rcvdata_vjc_ip },
281164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_UNCOMP	10
282164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_VJC_UNCOMP, ng_ppp_rcvdata_vjc_uncomp },
283164b576eSGleb Smirnoff #define HOOK_INDEX_VJC_VJIP	11
284164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_VJC_VJIP,	ng_ppp_rcvdata_vjc_vjip },
285164b576eSGleb Smirnoff #define HOOK_INDEX_IPV6		12
286164b576eSGleb Smirnoff 	{ NG_PPP_HOOK_IPV6,	ng_ppp_rcvdata_ipv6 },
287164b576eSGleb Smirnoff 	{ NULL, NULL }
288164b576eSGleb Smirnoff };
289164b576eSGleb Smirnoff 
2903949bee8SArchie Cobbs /* Helper functions */
291164b576eSGleb Smirnoff static int	ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto,
292164b576eSGleb Smirnoff 		    uint16_t linkNum);
293164b576eSGleb Smirnoff static int	ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto);
294164b576eSGleb Smirnoff static int	ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto,
295164b576eSGleb Smirnoff 		    uint16_t linkNum);
296164b576eSGleb Smirnoff static int	ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto);
297164b576eSGleb Smirnoff static int	ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto,
298164b576eSGleb Smirnoff 		    uint16_t linkNum);
299164b576eSGleb Smirnoff static int	ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto);
300164b576eSGleb Smirnoff static int	ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto,
301164b576eSGleb Smirnoff 		    uint16_t linkNum);
302164b576eSGleb Smirnoff static int	ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto);
303164b576eSGleb Smirnoff static int	ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto,
304164b576eSGleb Smirnoff 		    uint16_t linkNum);
305164b576eSGleb Smirnoff static int	ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto,
306e89c1507SAlexander Motin 		    uint16_t linkNum, int plen);
307164b576eSGleb Smirnoff 
30839c14742SGleb Smirnoff static int	ng_ppp_bypass(node_p node, item_p item, uint16_t proto,
30939c14742SGleb Smirnoff 		    uint16_t linkNum);
31039c14742SGleb Smirnoff 
3118e8f114eSAlexander Motin static void	ng_ppp_bump_mseq(node_p node, int32_t new_mseq);
3128e8f114eSAlexander Motin static int	ng_ppp_frag_drop(node_p node);
313a9b3dca5SArchie Cobbs static int	ng_ppp_check_packet(node_p node);
3143ca24c28SJulian Elischer static void	ng_ppp_get_packet(node_p node, struct mbuf **mp);
3155716b3f1SAlexander Motin static int	ng_ppp_frag_process(node_p node, item_p oitem);
316a9b3dca5SArchie Cobbs static int	ng_ppp_frag_trim(node_p node);
3179d952bd3SGleb Smirnoff static void	ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1,
3189d952bd3SGleb Smirnoff 		    int arg2);
319a9b3dca5SArchie Cobbs static void	ng_ppp_frag_checkstale(node_p node);
320a9b3dca5SArchie Cobbs static void	ng_ppp_frag_reset(node_p node);
3213949bee8SArchie Cobbs static void	ng_ppp_mp_strategy(node_p node, int len, int *distrib);
3228250de83SGleb Smirnoff static int	ng_ppp_intcmp(void *latency, const void *v1, const void *v2);
323164b576eSGleb Smirnoff static struct mbuf *ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK);
324164b576eSGleb Smirnoff static struct mbuf *ng_ppp_cutproto(struct mbuf *m, uint16_t *proto);
325d690a6e7SArchie Cobbs static struct mbuf *ng_ppp_prepend(struct mbuf *m, const void *buf, int len);
3263949bee8SArchie Cobbs static int	ng_ppp_config_valid(node_p node,
327a9b3dca5SArchie Cobbs 		    const struct ng_ppp_node_conf *newConf);
3283949bee8SArchie Cobbs static void	ng_ppp_update(node_p node, int newConf);
329a9b3dca5SArchie Cobbs static void	ng_ppp_start_frag_timer(node_p node);
330a9b3dca5SArchie Cobbs static void	ng_ppp_stop_frag_timer(node_p node);
3314cf49a43SJulian Elischer 
3326f16db81SArchie Cobbs /* Parse type for struct ng_ppp_mp_state_type */
3336f16db81SArchie Cobbs static const struct ng_parse_fixedarray_info ng_ppp_rseq_array_info = {
3346f16db81SArchie Cobbs 	&ng_parse_hint32_type,
3356f16db81SArchie Cobbs 	NG_PPP_MAX_LINKS
3366f16db81SArchie Cobbs };
3376f16db81SArchie Cobbs static const struct ng_parse_type ng_ppp_rseq_array_type = {
3386f16db81SArchie Cobbs 	&ng_parse_fixedarray_type,
3396f16db81SArchie Cobbs 	&ng_ppp_rseq_array_info,
3406f16db81SArchie Cobbs };
341f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_mp_state_type_fields[]
3426f16db81SArchie Cobbs 	= NG_PPP_MP_STATE_TYPE_INFO(&ng_ppp_rseq_array_type);
3436f16db81SArchie Cobbs static const struct ng_parse_type ng_ppp_mp_state_type = {
3446f16db81SArchie Cobbs 	&ng_parse_struct_type,
345f0184ff8SArchie Cobbs 	&ng_ppp_mp_state_type_fields
3466f16db81SArchie Cobbs };
3476f16db81SArchie Cobbs 
348a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_link_conf */
349f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_link_type_fields[]
350f0184ff8SArchie Cobbs 	= NG_PPP_LINK_TYPE_INFO;
351f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_link_type = {
352f8307e12SArchie Cobbs 	&ng_parse_struct_type,
353f0184ff8SArchie Cobbs 	&ng_ppp_link_type_fields
354f8307e12SArchie Cobbs };
355f8307e12SArchie Cobbs 
356a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_bund_conf */
357f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_bund_type_fields[]
358f0184ff8SArchie Cobbs 	= NG_PPP_BUND_TYPE_INFO;
359a9b3dca5SArchie Cobbs static const struct ng_parse_type ng_ppp_bund_type = {
360a9b3dca5SArchie Cobbs 	&ng_parse_struct_type,
361f0184ff8SArchie Cobbs 	&ng_ppp_bund_type_fields
362a9b3dca5SArchie Cobbs };
363a9b3dca5SArchie Cobbs 
364a9b3dca5SArchie Cobbs /* Parse type for struct ng_ppp_node_conf */
3656f16db81SArchie Cobbs static const struct ng_parse_fixedarray_info ng_ppp_array_info = {
366f8307e12SArchie Cobbs 	&ng_ppp_link_type,
367f8307e12SArchie Cobbs 	NG_PPP_MAX_LINKS
368f8307e12SArchie Cobbs };
369f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_link_array_type = {
370f8307e12SArchie Cobbs 	&ng_parse_fixedarray_type,
371f8307e12SArchie Cobbs 	&ng_ppp_array_info,
372f8307e12SArchie Cobbs };
373f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_conf_type_fields[]
374a9b3dca5SArchie Cobbs 	= NG_PPP_CONFIG_TYPE_INFO(&ng_ppp_bund_type, &ng_ppp_link_array_type);
375a9b3dca5SArchie Cobbs static const struct ng_parse_type ng_ppp_conf_type = {
376f8307e12SArchie Cobbs 	&ng_parse_struct_type,
377f0184ff8SArchie Cobbs 	&ng_ppp_conf_type_fields
378f8307e12SArchie Cobbs };
379f8307e12SArchie Cobbs 
380f8307e12SArchie Cobbs /* Parse type for struct ng_ppp_link_stat */
381f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_ppp_stats_type_fields[]
382f0184ff8SArchie Cobbs 	= NG_PPP_STATS_TYPE_INFO;
383f8307e12SArchie Cobbs static const struct ng_parse_type ng_ppp_stats_type = {
384f8307e12SArchie Cobbs 	&ng_parse_struct_type,
385f0184ff8SArchie Cobbs 	&ng_ppp_stats_type_fields
386f8307e12SArchie Cobbs };
387f8307e12SArchie Cobbs 
388d6fe462aSAlexander Motin /* Parse type for struct ng_ppp_link_stat64 */
389d6fe462aSAlexander Motin static const struct ng_parse_struct_field ng_ppp_stats64_type_fields[]
390d6fe462aSAlexander Motin 	= NG_PPP_STATS64_TYPE_INFO;
391d6fe462aSAlexander Motin static const struct ng_parse_type ng_ppp_stats64_type = {
392d6fe462aSAlexander Motin 	&ng_parse_struct_type,
393d6fe462aSAlexander Motin 	&ng_ppp_stats64_type_fields
394d6fe462aSAlexander Motin };
395d6fe462aSAlexander Motin 
396f8307e12SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
397f8307e12SArchie Cobbs static const struct ng_cmdlist ng_ppp_cmds[] = {
398f8307e12SArchie Cobbs 	{
399f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
400f8307e12SArchie Cobbs 	  NGM_PPP_SET_CONFIG,
401f8307e12SArchie Cobbs 	  "setconfig",
402a9b3dca5SArchie Cobbs 	  &ng_ppp_conf_type,
403f8307e12SArchie Cobbs 	  NULL
404f8307e12SArchie Cobbs 	},
405f8307e12SArchie Cobbs 	{
406f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
407f8307e12SArchie Cobbs 	  NGM_PPP_GET_CONFIG,
408f8307e12SArchie Cobbs 	  "getconfig",
409f8307e12SArchie Cobbs 	  NULL,
410a9b3dca5SArchie Cobbs 	  &ng_ppp_conf_type
411f8307e12SArchie Cobbs 	},
412f8307e12SArchie Cobbs 	{
413f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
4146f16db81SArchie Cobbs 	  NGM_PPP_GET_MP_STATE,
4156f16db81SArchie Cobbs 	  "getmpstate",
4166f16db81SArchie Cobbs 	  NULL,
4176f16db81SArchie Cobbs 	  &ng_ppp_mp_state_type
4186f16db81SArchie Cobbs 	},
4196f16db81SArchie Cobbs 	{
4206f16db81SArchie Cobbs 	  NGM_PPP_COOKIE,
421f8307e12SArchie Cobbs 	  NGM_PPP_GET_LINK_STATS,
422f8307e12SArchie Cobbs 	  "getstats",
423f8307e12SArchie Cobbs 	  &ng_parse_int16_type,
424f8307e12SArchie Cobbs 	  &ng_ppp_stats_type
425f8307e12SArchie Cobbs 	},
426f8307e12SArchie Cobbs 	{
427f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
428f8307e12SArchie Cobbs 	  NGM_PPP_CLR_LINK_STATS,
429f8307e12SArchie Cobbs 	  "clrstats",
430f8307e12SArchie Cobbs 	  &ng_parse_int16_type,
431f8307e12SArchie Cobbs 	  NULL
432f8307e12SArchie Cobbs 	},
433f8307e12SArchie Cobbs 	{
434f8307e12SArchie Cobbs 	  NGM_PPP_COOKIE,
435f8307e12SArchie Cobbs 	  NGM_PPP_GETCLR_LINK_STATS,
436f8307e12SArchie Cobbs 	  "getclrstats",
437f8307e12SArchie Cobbs 	  &ng_parse_int16_type,
438f8307e12SArchie Cobbs 	  &ng_ppp_stats_type
439f8307e12SArchie Cobbs 	},
440d6fe462aSAlexander Motin 	{
441d6fe462aSAlexander Motin 	  NGM_PPP_COOKIE,
442d6fe462aSAlexander Motin 	  NGM_PPP_GET_LINK_STATS64,
443d6fe462aSAlexander Motin 	  "getstats64",
444d6fe462aSAlexander Motin 	  &ng_parse_int16_type,
445d6fe462aSAlexander Motin 	  &ng_ppp_stats64_type
446d6fe462aSAlexander Motin 	},
447d6fe462aSAlexander Motin 	{
448d6fe462aSAlexander Motin 	  NGM_PPP_COOKIE,
449d6fe462aSAlexander Motin 	  NGM_PPP_GETCLR_LINK_STATS64,
450d6fe462aSAlexander Motin 	  "getclrstats64",
451d6fe462aSAlexander Motin 	  &ng_parse_int16_type,
452d6fe462aSAlexander Motin 	  &ng_ppp_stats64_type
453d6fe462aSAlexander Motin 	},
454f8307e12SArchie Cobbs 	{ 0 }
455f8307e12SArchie Cobbs };
456f8307e12SArchie Cobbs 
4574cf49a43SJulian Elischer /* Node type descriptor */
4583949bee8SArchie Cobbs static struct ng_type ng_ppp_typestruct = {
459f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
460f8aae777SJulian Elischer 	.name =		NG_PPP_NODE_TYPE,
461f8aae777SJulian Elischer 	.constructor =	ng_ppp_constructor,
462f8aae777SJulian Elischer 	.rcvmsg =	ng_ppp_rcvmsg,
463f8aae777SJulian Elischer 	.shutdown =	ng_ppp_shutdown,
464f8aae777SJulian Elischer 	.newhook =	ng_ppp_newhook,
465f8aae777SJulian Elischer 	.rcvdata =	ng_ppp_rcvdata,
466f8aae777SJulian Elischer 	.disconnect =	ng_ppp_disconnect,
467f8aae777SJulian Elischer 	.cmdlist =	ng_ppp_cmds,
4684cf49a43SJulian Elischer };
4693949bee8SArchie Cobbs NETGRAPH_INIT(ppp, &ng_ppp_typestruct);
4704cf49a43SJulian Elischer 
471d690a6e7SArchie Cobbs /* Address and control field header */
472164b576eSGleb Smirnoff static const uint8_t ng_ppp_acf[2] = { 0xff, 0x03 };
473d690a6e7SArchie Cobbs 
474a9b3dca5SArchie Cobbs /* Maximum time we'll let a complete incoming packet sit in the queue */
475a9b3dca5SArchie Cobbs static const struct timeval ng_ppp_max_staleness = { 2, 0 };	/* 2 seconds */
476a9b3dca5SArchie Cobbs 
4774cf49a43SJulian Elischer #define ERROUT(x)	do { error = (x); goto done; } while (0)
4784cf49a43SJulian Elischer 
4794cf49a43SJulian Elischer /************************************************************************
4804cf49a43SJulian Elischer 			NETGRAPH NODE STUFF
4814cf49a43SJulian Elischer  ************************************************************************/
4824cf49a43SJulian Elischer 
4834cf49a43SJulian Elischer /*
4843949bee8SArchie Cobbs  * Node type constructor
4854cf49a43SJulian Elischer  */
4864cf49a43SJulian Elischer static int
487069154d5SJulian Elischer ng_ppp_constructor(node_p node)
4884cf49a43SJulian Elischer {
4894cf49a43SJulian Elischer 	priv_p priv;
490069154d5SJulian Elischer 	int i;
4914cf49a43SJulian Elischer 
4924cf49a43SJulian Elischer 	/* Allocate private structure */
4931ede983cSDag-Erling Smørgrav 	priv = malloc(sizeof(*priv), M_NETGRAPH_PPP, M_NOWAIT | M_ZERO);
4944cf49a43SJulian Elischer 	if (priv == NULL)
4954cf49a43SJulian Elischer 		return (ENOMEM);
4964cf49a43SJulian Elischer 
49730400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
4984cf49a43SJulian Elischer 
4993949bee8SArchie Cobbs 	/* Initialize state */
5005f90cac7SKirk McKusick 	TAILQ_INIT(&priv->frags);
50193caf2e2SAlexander Motin 	TAILQ_INIT(&priv->fragsfree);
50293caf2e2SAlexander Motin 	for (i = 0; i < MP_MAX_QUEUE_LEN; i++)
50393caf2e2SAlexander Motin 		TAILQ_INSERT_TAIL(&priv->fragsfree, &priv->fragsmem[i], f_qent);
504a9b3dca5SArchie Cobbs 	for (i = 0; i < NG_PPP_MAX_LINKS; i++)
505a9b3dca5SArchie Cobbs 		priv->links[i].seq = MP_NOSEQ;
5069d952bd3SGleb Smirnoff 	ng_callout_init(&priv->fragTimer);
5073949bee8SArchie Cobbs 
508e89c1507SAlexander Motin 	mtx_init(&priv->rmtx, "ng_ppp_recv", NULL, MTX_DEF);
509e89c1507SAlexander Motin 	mtx_init(&priv->xmtx, "ng_ppp_xmit", NULL, MTX_DEF);
510e89c1507SAlexander Motin 
5114cf49a43SJulian Elischer 	/* Done */
5124cf49a43SJulian Elischer 	return (0);
5134cf49a43SJulian Elischer }
5144cf49a43SJulian Elischer 
5154cf49a43SJulian Elischer /*
5164cf49a43SJulian Elischer  * Give our OK for a hook to be added
5174cf49a43SJulian Elischer  */
5184cf49a43SJulian Elischer static int
5194cf49a43SJulian Elischer ng_ppp_newhook(node_p node, hook_p hook, const char *name)
5204cf49a43SJulian Elischer {
52130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
5223949bee8SArchie Cobbs 	hook_p *hookPtr = NULL;
523164b576eSGleb Smirnoff 	int linkNum = -1;
5243949bee8SArchie Cobbs 	int hookIndex = -1;
5254cf49a43SJulian Elischer 
5263949bee8SArchie Cobbs 	/* Figure out which hook it is */
5273949bee8SArchie Cobbs 	if (strncmp(name, NG_PPP_HOOK_LINK_PREFIX,	/* a link hook? */
5283949bee8SArchie Cobbs 	    strlen(NG_PPP_HOOK_LINK_PREFIX)) == 0) {
52925792ef3SArchie Cobbs 		const char *cp;
53025792ef3SArchie Cobbs 		char *eptr;
5313949bee8SArchie Cobbs 
5322b70adcbSArchie Cobbs 		cp = name + strlen(NG_PPP_HOOK_LINK_PREFIX);
5332b70adcbSArchie Cobbs 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
5344cf49a43SJulian Elischer 			return (EINVAL);
5352b70adcbSArchie Cobbs 		linkNum = (int)strtoul(cp, &eptr, 10);
5362b70adcbSArchie Cobbs 		if (*eptr != '\0' || linkNum < 0 || linkNum >= NG_PPP_MAX_LINKS)
5373949bee8SArchie Cobbs 			return (EINVAL);
538a9b3dca5SArchie Cobbs 		hookPtr = &priv->links[linkNum].hook;
5393949bee8SArchie Cobbs 		hookIndex = ~linkNum;
540164b576eSGleb Smirnoff 
541164b576eSGleb Smirnoff 		/* See if hook is already connected. */
542164b576eSGleb Smirnoff 		if (*hookPtr != NULL)
543164b576eSGleb Smirnoff 			return (EISCONN);
544164b576eSGleb Smirnoff 
545164b576eSGleb Smirnoff 		/* Disallow more than one link unless multilink is enabled. */
546164b576eSGleb Smirnoff 		if (priv->links[linkNum].conf.enableLink &&
547164b576eSGleb Smirnoff 		    !priv->conf.enableMultilink && priv->numActiveLinks >= 1)
548164b576eSGleb Smirnoff 			return (ENODEV);
549164b576eSGleb Smirnoff 
5503949bee8SArchie Cobbs 	} else {				/* must be a non-link hook */
5513949bee8SArchie Cobbs 		int i;
5524cf49a43SJulian Elischer 
553164b576eSGleb Smirnoff 		for (i = 0; ng_ppp_hook_names[i].name != NULL; i++) {
554164b576eSGleb Smirnoff 			if (strcmp(name, ng_ppp_hook_names[i].name) == 0) {
5553949bee8SArchie Cobbs 				hookPtr = &priv->hooks[i];
5563949bee8SArchie Cobbs 				hookIndex = i;
5573949bee8SArchie Cobbs 				break;
5583949bee8SArchie Cobbs 			}
5593949bee8SArchie Cobbs 		}
560164b576eSGleb Smirnoff 		if (ng_ppp_hook_names[i].name == NULL)
5613949bee8SArchie Cobbs 			return (EINVAL);	/* no such hook */
5623949bee8SArchie Cobbs 
5633949bee8SArchie Cobbs 		/* See if hook is already connected */
5643949bee8SArchie Cobbs 		if (*hookPtr != NULL)
5654cf49a43SJulian Elischer 			return (EISCONN);
5664cf49a43SJulian Elischer 
567164b576eSGleb Smirnoff 		/* Every non-linkX hook have it's own function. */
568164b576eSGleb Smirnoff 		NG_HOOK_SET_RCVDATA(hook, ng_ppp_hook_names[i].fn);
569164b576eSGleb Smirnoff 	}
5704cf49a43SJulian Elischer 
5714cf49a43SJulian Elischer 	/* OK */
5723949bee8SArchie Cobbs 	*hookPtr = hook;
5738214d60eSJohn Baldwin 	NG_HOOK_SET_PRIVATE(hook, (void *)(intptr_t)hookIndex);
5743949bee8SArchie Cobbs 	ng_ppp_update(node, 0);
5754cf49a43SJulian Elischer 	return (0);
5764cf49a43SJulian Elischer }
5774cf49a43SJulian Elischer 
5784cf49a43SJulian Elischer /*
5794cf49a43SJulian Elischer  * Receive a control message
5804cf49a43SJulian Elischer  */
5814cf49a43SJulian Elischer static int
582069154d5SJulian Elischer ng_ppp_rcvmsg(node_p node, item_p item, hook_p lasthook)
5834cf49a43SJulian Elischer {
58430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
5854cf49a43SJulian Elischer 	struct ng_mesg *resp = NULL;
5864cf49a43SJulian Elischer 	int error = 0;
587069154d5SJulian Elischer 	struct ng_mesg *msg;
5884cf49a43SJulian Elischer 
589069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
5904cf49a43SJulian Elischer 	switch (msg->header.typecookie) {
5914cf49a43SJulian Elischer 	case NGM_PPP_COOKIE:
5924cf49a43SJulian Elischer 		switch (msg->header.cmd) {
5933949bee8SArchie Cobbs 		case NGM_PPP_SET_CONFIG:
5943949bee8SArchie Cobbs 		    {
595a9b3dca5SArchie Cobbs 			struct ng_ppp_node_conf *const conf =
596a9b3dca5SArchie Cobbs 			    (struct ng_ppp_node_conf *)msg->data;
597a9b3dca5SArchie Cobbs 			int i;
5983949bee8SArchie Cobbs 
5993949bee8SArchie Cobbs 			/* Check for invalid or illegal config */
600a9b3dca5SArchie Cobbs 			if (msg->header.arglen != sizeof(*conf))
6014cf49a43SJulian Elischer 				ERROUT(EINVAL);
602a9b3dca5SArchie Cobbs 			if (!ng_ppp_config_valid(node, conf))
6033949bee8SArchie Cobbs 				ERROUT(EINVAL);
604a9b3dca5SArchie Cobbs 
605a9b3dca5SArchie Cobbs 			/* Copy config */
606a9b3dca5SArchie Cobbs 			priv->conf = conf->bund;
607a9b3dca5SArchie Cobbs 			for (i = 0; i < NG_PPP_MAX_LINKS; i++)
608a9b3dca5SArchie Cobbs 				priv->links[i].conf = conf->links[i];
6093949bee8SArchie Cobbs 			ng_ppp_update(node, 1);
6104cf49a43SJulian Elischer 			break;
6113949bee8SArchie Cobbs 		    }
6123949bee8SArchie Cobbs 		case NGM_PPP_GET_CONFIG:
613a9b3dca5SArchie Cobbs 		    {
614a9b3dca5SArchie Cobbs 			struct ng_ppp_node_conf *conf;
615a9b3dca5SArchie Cobbs 			int i;
616a9b3dca5SArchie Cobbs 
617a9b3dca5SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*conf), M_NOWAIT);
6184cf49a43SJulian Elischer 			if (resp == NULL)
6194cf49a43SJulian Elischer 				ERROUT(ENOMEM);
620a9b3dca5SArchie Cobbs 			conf = (struct ng_ppp_node_conf *)resp->data;
621a9b3dca5SArchie Cobbs 			conf->bund = priv->conf;
622a9b3dca5SArchie Cobbs 			for (i = 0; i < NG_PPP_MAX_LINKS; i++)
623a9b3dca5SArchie Cobbs 				conf->links[i] = priv->links[i].conf;
6244cf49a43SJulian Elischer 			break;
625a9b3dca5SArchie Cobbs 		    }
6266f16db81SArchie Cobbs 		case NGM_PPP_GET_MP_STATE:
6276f16db81SArchie Cobbs 		    {
6286f16db81SArchie Cobbs 			struct ng_ppp_mp_state *info;
6296f16db81SArchie Cobbs 			int i;
6306f16db81SArchie Cobbs 
6316f16db81SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*info), M_NOWAIT);
6326f16db81SArchie Cobbs 			if (resp == NULL)
6336f16db81SArchie Cobbs 				ERROUT(ENOMEM);
6346f16db81SArchie Cobbs 			info = (struct ng_ppp_mp_state *)resp->data;
6356f16db81SArchie Cobbs 			bzero(info, sizeof(*info));
6366f16db81SArchie Cobbs 			for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
6376f16db81SArchie Cobbs 				if (priv->links[i].seq != MP_NOSEQ)
6386f16db81SArchie Cobbs 					info->rseq[i] = priv->links[i].seq;
6396f16db81SArchie Cobbs 			}
6406f16db81SArchie Cobbs 			info->mseq = priv->mseq;
6416f16db81SArchie Cobbs 			info->xseq = priv->xseq;
6426f16db81SArchie Cobbs 			break;
6436f16db81SArchie Cobbs 		    }
6443949bee8SArchie Cobbs 		case NGM_PPP_GET_LINK_STATS:
6453949bee8SArchie Cobbs 		case NGM_PPP_CLR_LINK_STATS:
646e149c4e2SArchie Cobbs 		case NGM_PPP_GETCLR_LINK_STATS:
647d6fe462aSAlexander Motin 		case NGM_PPP_GET_LINK_STATS64:
648d6fe462aSAlexander Motin 		case NGM_PPP_GETCLR_LINK_STATS64:
6493949bee8SArchie Cobbs 		    {
650d6fe462aSAlexander Motin 			struct ng_ppp_link_stat64 *stats;
651164b576eSGleb Smirnoff 			uint16_t linkNum;
6523949bee8SArchie Cobbs 
653d6fe462aSAlexander Motin 			/* Process request. */
654164b576eSGleb Smirnoff 			if (msg->header.arglen != sizeof(uint16_t))
6553949bee8SArchie Cobbs 				ERROUT(EINVAL);
656164b576eSGleb Smirnoff 			linkNum = *((uint16_t *) msg->data);
6573949bee8SArchie Cobbs 			if (linkNum >= NG_PPP_MAX_LINKS
6583949bee8SArchie Cobbs 			    && linkNum != NG_PPP_BUNDLE_LINKNUM)
6593949bee8SArchie Cobbs 				ERROUT(EINVAL);
6603949bee8SArchie Cobbs 			stats = (linkNum == NG_PPP_BUNDLE_LINKNUM) ?
661a9b3dca5SArchie Cobbs 			    &priv->bundleStats : &priv->links[linkNum].stats;
662d6fe462aSAlexander Motin 
663d6fe462aSAlexander Motin 			/* Make 64bit reply. */
664d6fe462aSAlexander Motin 			if (msg->header.cmd == NGM_PPP_GET_LINK_STATS64 ||
665d6fe462aSAlexander Motin 			    msg->header.cmd == NGM_PPP_GETCLR_LINK_STATS64) {
666d6fe462aSAlexander Motin 				NG_MKRESPONSE(resp, msg,
667d6fe462aSAlexander Motin 				    sizeof(struct ng_ppp_link_stat64), M_NOWAIT);
668d6fe462aSAlexander Motin 				if (resp == NULL)
669d6fe462aSAlexander Motin 					ERROUT(ENOMEM);
670d6fe462aSAlexander Motin 				bcopy(stats, resp->data, sizeof(*stats));
671d6fe462aSAlexander Motin 			} else
672d6fe462aSAlexander Motin 			/* Make 32bit reply. */
673d6fe462aSAlexander Motin 			if (msg->header.cmd == NGM_PPP_GET_LINK_STATS ||
674d6fe462aSAlexander Motin 			    msg->header.cmd == NGM_PPP_GETCLR_LINK_STATS) {
675d6fe462aSAlexander Motin 				struct ng_ppp_link_stat *rs;
6763949bee8SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
6773949bee8SArchie Cobbs 				    sizeof(struct ng_ppp_link_stat), M_NOWAIT);
6783949bee8SArchie Cobbs 				if (resp == NULL)
6793949bee8SArchie Cobbs 					ERROUT(ENOMEM);
680d6fe462aSAlexander Motin 				rs = (struct ng_ppp_link_stat *)resp->data;
681d6fe462aSAlexander Motin 				/* Truncate 64->32 bits. */
682d6fe462aSAlexander Motin 				rs->xmitFrames = stats->xmitFrames;
683d6fe462aSAlexander Motin 				rs->xmitOctets = stats->xmitOctets;
684d6fe462aSAlexander Motin 				rs->recvFrames = stats->recvFrames;
685d6fe462aSAlexander Motin 				rs->recvOctets = stats->recvOctets;
686d6fe462aSAlexander Motin 				rs->badProtos = stats->badProtos;
687d6fe462aSAlexander Motin 				rs->runts = stats->runts;
688d6fe462aSAlexander Motin 				rs->dupFragments = stats->dupFragments;
689d6fe462aSAlexander Motin 				rs->dropFragments = stats->dropFragments;
690e149c4e2SArchie Cobbs 			}
691d6fe462aSAlexander Motin 			/* Clear stats. */
692d6fe462aSAlexander Motin 			if (msg->header.cmd != NGM_PPP_GET_LINK_STATS &&
693d6fe462aSAlexander Motin 			    msg->header.cmd != NGM_PPP_GET_LINK_STATS64)
6943949bee8SArchie Cobbs 				bzero(stats, sizeof(*stats));
6954cf49a43SJulian Elischer 			break;
6963949bee8SArchie Cobbs 		    }
6974cf49a43SJulian Elischer 		default:
6984cf49a43SJulian Elischer 			error = EINVAL;
6994cf49a43SJulian Elischer 			break;
7004cf49a43SJulian Elischer 		}
7014cf49a43SJulian Elischer 		break;
7023949bee8SArchie Cobbs 	case NGM_VJC_COOKIE:
7033949bee8SArchie Cobbs 	    {
704069154d5SJulian Elischer 		/*
705069154d5SJulian Elischer 		 * Forward it to the vjc node. leave the
706069154d5SJulian Elischer 		 * old return address alone.
70730400f03SJulian Elischer 		 * If we have no hook, let NG_RESPOND_MSG
70830400f03SJulian Elischer 		 * clean up any remaining resources.
70930400f03SJulian Elischer 		 * Because we have no resp, the item will be freed
71030400f03SJulian Elischer 		 * along with anything it references. Don't
71130400f03SJulian Elischer 		 * let msg be freed twice.
712069154d5SJulian Elischer 		 */
713069154d5SJulian Elischer 		NGI_MSG(item) = msg;	/* put it back in the item */
71430400f03SJulian Elischer 		msg = NULL;
715164b576eSGleb Smirnoff 		if ((lasthook = priv->hooks[HOOK_INDEX_VJC_IP])) {
71630400f03SJulian Elischer 			NG_FWD_ITEM_HOOK(error, item, lasthook);
717069154d5SJulian Elischer 		}
718069154d5SJulian Elischer 		return (error);
7193949bee8SArchie Cobbs 	    }
7204cf49a43SJulian Elischer 	default:
7214cf49a43SJulian Elischer 		error = EINVAL;
7224cf49a43SJulian Elischer 		break;
7234cf49a43SJulian Elischer 	}
7244cf49a43SJulian Elischer done:
725069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
726069154d5SJulian Elischer 	NG_FREE_MSG(msg);
7274cf49a43SJulian Elischer 	return (error);
7284cf49a43SJulian Elischer }
7294cf49a43SJulian Elischer 
7304cf49a43SJulian Elischer /*
7314cf49a43SJulian Elischer  * Destroy node
7324cf49a43SJulian Elischer  */
7334cf49a43SJulian Elischer static int
734069154d5SJulian Elischer ng_ppp_shutdown(node_p node)
7354cf49a43SJulian Elischer {
73630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
7374cf49a43SJulian Elischer 
738a9b3dca5SArchie Cobbs 	/* Stop fragment queue timer */
739a9b3dca5SArchie Cobbs 	ng_ppp_stop_frag_timer(node);
740a9b3dca5SArchie Cobbs 
7414cf49a43SJulian Elischer 	/* Take down netgraph node */
742a9b3dca5SArchie Cobbs 	ng_ppp_frag_reset(node);
743e89c1507SAlexander Motin 	mtx_destroy(&priv->rmtx);
744e89c1507SAlexander Motin 	mtx_destroy(&priv->xmtx);
7454cf49a43SJulian Elischer 	bzero(priv, sizeof(*priv));
7461ede983cSDag-Erling Smørgrav 	free(priv, M_NETGRAPH_PPP);
74730400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, NULL);
74830400f03SJulian Elischer 	NG_NODE_UNREF(node);		/* let the node escape */
7494cf49a43SJulian Elischer 	return (0);
7504cf49a43SJulian Elischer }
7514cf49a43SJulian Elischer 
7524cf49a43SJulian Elischer /*
7534cf49a43SJulian Elischer  * Hook disconnection
7544cf49a43SJulian Elischer  */
7554cf49a43SJulian Elischer static int
7564cf49a43SJulian Elischer ng_ppp_disconnect(hook_p hook)
7574cf49a43SJulian Elischer {
75830400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
75930400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
7608214d60eSJohn Baldwin 	const int index = (intptr_t)NG_HOOK_PRIVATE(hook);
76162838faeSArchie Cobbs 
76262838faeSArchie Cobbs 	/* Zero out hook pointer */
76362838faeSArchie Cobbs 	if (index < 0)
764a9b3dca5SArchie Cobbs 		priv->links[~index].hook = NULL;
76562838faeSArchie Cobbs 	else
76662838faeSArchie Cobbs 		priv->hooks[index] = NULL;
76762838faeSArchie Cobbs 
768164b576eSGleb Smirnoff 	/* Update derived info (or go away if no hooks left). */
769164b576eSGleb Smirnoff 	if (NG_NODE_NUMHOOKS(node) > 0)
77062838faeSArchie Cobbs 		ng_ppp_update(node, 0);
771164b576eSGleb Smirnoff 	else if (NG_NODE_IS_VALID(node))
772069154d5SJulian Elischer 		ng_rmnode_self(node);
773164b576eSGleb Smirnoff 
7744cf49a43SJulian Elischer 	return (0);
7754cf49a43SJulian Elischer }
7764cf49a43SJulian Elischer 
777164b576eSGleb Smirnoff /*
778164b576eSGleb Smirnoff  * Proto layer
779164b576eSGleb Smirnoff  */
7804cf49a43SJulian Elischer 
7814cf49a43SJulian Elischer /*
782164b576eSGleb Smirnoff  * Receive data on a hook inet.
7834cf49a43SJulian Elischer  */
7844cf49a43SJulian Elischer static int
785164b576eSGleb Smirnoff ng_ppp_rcvdata_inet(hook_p hook, item_p item)
786164b576eSGleb Smirnoff {
787164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
788164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
789164b576eSGleb Smirnoff 
790164b576eSGleb Smirnoff 	if (!priv->conf.enableIP) {
791164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
792164b576eSGleb Smirnoff 		return (ENXIO);
793164b576eSGleb Smirnoff 	}
794164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IP));
795164b576eSGleb Smirnoff }
796164b576eSGleb Smirnoff 
797164b576eSGleb Smirnoff /*
798164b576eSGleb Smirnoff  * Receive data on a hook ipv6.
799164b576eSGleb Smirnoff  */
800164b576eSGleb Smirnoff static int
801164b576eSGleb Smirnoff ng_ppp_rcvdata_ipv6(hook_p hook, item_p item)
802164b576eSGleb Smirnoff {
803164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
804164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
805164b576eSGleb Smirnoff 
806164b576eSGleb Smirnoff 	if (!priv->conf.enableIPv6) {
807164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
808164b576eSGleb Smirnoff 		return (ENXIO);
809164b576eSGleb Smirnoff 	}
810164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPV6));
811164b576eSGleb Smirnoff }
812164b576eSGleb Smirnoff 
813164b576eSGleb Smirnoff /*
814164b576eSGleb Smirnoff  * Receive data on a hook atalk.
815164b576eSGleb Smirnoff  */
816164b576eSGleb Smirnoff static int
817164b576eSGleb Smirnoff ng_ppp_rcvdata_atalk(hook_p hook, item_p item)
818164b576eSGleb Smirnoff {
819164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
820164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
821164b576eSGleb Smirnoff 
822164b576eSGleb Smirnoff 	if (!priv->conf.enableAtalk) {
823164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
824164b576eSGleb Smirnoff 		return (ENXIO);
825164b576eSGleb Smirnoff 	}
826164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_ATALK));
827164b576eSGleb Smirnoff }
828164b576eSGleb Smirnoff 
829164b576eSGleb Smirnoff /*
830164b576eSGleb Smirnoff  * Receive data on a hook ipx
831164b576eSGleb Smirnoff  */
832164b576eSGleb Smirnoff static int
833164b576eSGleb Smirnoff ng_ppp_rcvdata_ipx(hook_p hook, item_p item)
834164b576eSGleb Smirnoff {
835164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
836164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
837164b576eSGleb Smirnoff 
838164b576eSGleb Smirnoff 	if (!priv->conf.enableIPX) {
839164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
840164b576eSGleb Smirnoff 		return (ENXIO);
841164b576eSGleb Smirnoff 	}
842164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, PROT_IPX));
843164b576eSGleb Smirnoff }
844164b576eSGleb Smirnoff 
845164b576eSGleb Smirnoff /*
846164b576eSGleb Smirnoff  * Receive data on a hook bypass
847164b576eSGleb Smirnoff  */
848164b576eSGleb Smirnoff static int
849164b576eSGleb Smirnoff ng_ppp_rcvdata_bypass(hook_p hook, item_p item)
850164b576eSGleb Smirnoff {
851164b576eSGleb Smirnoff 	uint16_t linkNum;
852164b576eSGleb Smirnoff 	uint16_t proto;
853164b576eSGleb Smirnoff 	struct mbuf *m;
854164b576eSGleb Smirnoff 
855164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
856164b576eSGleb Smirnoff 	if (m->m_pkthdr.len < 4) {
857164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
858164b576eSGleb Smirnoff 		return (EINVAL);
859164b576eSGleb Smirnoff 	}
860164b576eSGleb Smirnoff 	if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
861164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
862164b576eSGleb Smirnoff 		return (ENOBUFS);
863164b576eSGleb Smirnoff 	}
864164b576eSGleb Smirnoff 	linkNum = ntohs(mtod(m, uint16_t *)[0]);
865164b576eSGleb Smirnoff 	proto = ntohs(mtod(m, uint16_t *)[1]);
866164b576eSGleb Smirnoff 	m_adj(m, 4);
867164b576eSGleb Smirnoff 	NGI_M(item) = m;
868164b576eSGleb Smirnoff 
869164b576eSGleb Smirnoff 	if (linkNum == NG_PPP_BUNDLE_LINKNUM)
870164b576eSGleb Smirnoff 		return (ng_ppp_hcomp_xmit(NG_HOOK_NODE(hook), item, proto));
871164b576eSGleb Smirnoff 	else
872164b576eSGleb Smirnoff 		return (ng_ppp_link_xmit(NG_HOOK_NODE(hook), item, proto,
873e89c1507SAlexander Motin 		    linkNum, 0));
874164b576eSGleb Smirnoff }
875164b576eSGleb Smirnoff 
876164b576eSGleb Smirnoff static int
87739c14742SGleb Smirnoff ng_ppp_bypass(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
87839c14742SGleb Smirnoff {
87939c14742SGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
88039c14742SGleb Smirnoff 	uint16_t hdr[2];
88139c14742SGleb Smirnoff 	struct mbuf *m;
88239c14742SGleb Smirnoff 	int error;
88339c14742SGleb Smirnoff 
88439c14742SGleb Smirnoff 	if (priv->hooks[HOOK_INDEX_BYPASS] == NULL) {
88539c14742SGleb Smirnoff 	    NG_FREE_ITEM(item);
88639c14742SGleb Smirnoff 	    return (ENXIO);
88739c14742SGleb Smirnoff 	}
88839c14742SGleb Smirnoff 
88939c14742SGleb Smirnoff 	/* Add 4-byte bypass header. */
89039c14742SGleb Smirnoff 	hdr[0] = htons(linkNum);
89139c14742SGleb Smirnoff 	hdr[1] = htons(proto);
89239c14742SGleb Smirnoff 
89339c14742SGleb Smirnoff 	NGI_GET_M(item, m);
89439c14742SGleb Smirnoff 	if ((m = ng_ppp_prepend(m, &hdr, 4)) == NULL) {
89539c14742SGleb Smirnoff 		NG_FREE_ITEM(item);
89639c14742SGleb Smirnoff 		return (ENOBUFS);
89739c14742SGleb Smirnoff 	}
89839c14742SGleb Smirnoff 	NGI_M(item) = m;
89939c14742SGleb Smirnoff 
90039c14742SGleb Smirnoff 	/* Send packet out hook. */
90139c14742SGleb Smirnoff 	NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_BYPASS]);
90239c14742SGleb Smirnoff 	return (error);
90339c14742SGleb Smirnoff }
90439c14742SGleb Smirnoff 
90539c14742SGleb Smirnoff static int
906164b576eSGleb Smirnoff ng_ppp_proto_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
9074cf49a43SJulian Elischer {
90830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
9093949bee8SArchie Cobbs 	hook_p outHook = NULL;
910164b576eSGleb Smirnoff 	int error;
9114cf49a43SJulian Elischer 
912ccb07cc3SGleb Smirnoff 	switch (proto) {
9133949bee8SArchie Cobbs 	    case PROT_IP:
9143949bee8SArchie Cobbs 		if (priv->conf.enableIP)
9153949bee8SArchie Cobbs 		    outHook = priv->hooks[HOOK_INDEX_INET];
9163949bee8SArchie Cobbs 		break;
917a9b3dca5SArchie Cobbs 	    case PROT_IPV6:
918a9b3dca5SArchie Cobbs 		if (priv->conf.enableIPv6)
919a9b3dca5SArchie Cobbs 		    outHook = priv->hooks[HOOK_INDEX_IPV6];
920a9b3dca5SArchie Cobbs 		break;
921164b576eSGleb Smirnoff 	    case PROT_ATALK:
922164b576eSGleb Smirnoff 		if (priv->conf.enableAtalk)
923164b576eSGleb Smirnoff 		    outHook = priv->hooks[HOOK_INDEX_ATALK];
924164b576eSGleb Smirnoff 		break;
925164b576eSGleb Smirnoff 	    case PROT_IPX:
926164b576eSGleb Smirnoff 		if (priv->conf.enableIPX)
927164b576eSGleb Smirnoff 		    outHook = priv->hooks[HOOK_INDEX_IPX];
928164b576eSGleb Smirnoff 		break;
929ccb07cc3SGleb Smirnoff 	}
9303949bee8SArchie Cobbs 
93139c14742SGleb Smirnoff 	if (outHook == NULL)
93239c14742SGleb Smirnoff 		return (ng_ppp_bypass(node, item, proto, linkNum));
933d690a6e7SArchie Cobbs 
934164b576eSGleb Smirnoff 	/* Send packet out hook. */
935164b576eSGleb Smirnoff 	NG_FWD_ITEM_HOOK(error, item, outHook);
9363949bee8SArchie Cobbs 	return (error);
9374cf49a43SJulian Elischer }
9384cf49a43SJulian Elischer 
9394cf49a43SJulian Elischer /*
940164b576eSGleb Smirnoff  * Header compression layer
941164b576eSGleb Smirnoff  */
942164b576eSGleb Smirnoff 
943164b576eSGleb Smirnoff static int
944164b576eSGleb Smirnoff ng_ppp_hcomp_xmit(node_p node, item_p item, uint16_t proto)
945164b576eSGleb Smirnoff {
946164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
947164b576eSGleb Smirnoff 
948164b576eSGleb Smirnoff 	if (proto == PROT_IP &&
949164b576eSGleb Smirnoff 	    priv->conf.enableVJCompression &&
950164b576eSGleb Smirnoff 	    priv->vjCompHooked) {
951164b576eSGleb Smirnoff 		int error;
952164b576eSGleb Smirnoff 
953164b576eSGleb Smirnoff 		/* Send packet out hook. */
954164b576eSGleb Smirnoff 		NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_VJC_IP]);
955164b576eSGleb Smirnoff 		return (error);
956164b576eSGleb Smirnoff 	}
957164b576eSGleb Smirnoff 
958164b576eSGleb Smirnoff 	return (ng_ppp_comp_xmit(node, item, proto));
959164b576eSGleb Smirnoff }
960164b576eSGleb Smirnoff 
961164b576eSGleb Smirnoff /*
962164b576eSGleb Smirnoff  * Receive data on a hook vjc_comp.
9634cf49a43SJulian Elischer  */
9643949bee8SArchie Cobbs static int
965164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_comp(hook_p hook, item_p item)
966164b576eSGleb Smirnoff {
967164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
968164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
969164b576eSGleb Smirnoff 
970164b576eSGleb Smirnoff 	if (!priv->conf.enableVJCompression) {
971164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
972164b576eSGleb Smirnoff 		return (ENXIO);
973164b576eSGleb Smirnoff 	}
974164b576eSGleb Smirnoff 	return (ng_ppp_comp_xmit(node, item, PROT_VJCOMP));
975164b576eSGleb Smirnoff }
976164b576eSGleb Smirnoff 
977164b576eSGleb Smirnoff /*
978164b576eSGleb Smirnoff  * Receive data on a hook vjc_uncomp.
979164b576eSGleb Smirnoff  */
980164b576eSGleb Smirnoff static int
981164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_uncomp(hook_p hook, item_p item)
982164b576eSGleb Smirnoff {
983164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
984164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
985164b576eSGleb Smirnoff 
986164b576eSGleb Smirnoff 	if (!priv->conf.enableVJCompression) {
987164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
988164b576eSGleb Smirnoff 		return (ENXIO);
989164b576eSGleb Smirnoff 	}
990164b576eSGleb Smirnoff 	return (ng_ppp_comp_xmit(node, item, PROT_VJUNCOMP));
991164b576eSGleb Smirnoff }
992164b576eSGleb Smirnoff 
993164b576eSGleb Smirnoff /*
994164b576eSGleb Smirnoff  * Receive data on a hook vjc_vjip.
995164b576eSGleb Smirnoff  */
996164b576eSGleb Smirnoff static int
997164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_vjip(hook_p hook, item_p item)
998164b576eSGleb Smirnoff {
999164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
1000164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1001164b576eSGleb Smirnoff 
1002164b576eSGleb Smirnoff 	if (!priv->conf.enableVJCompression) {
1003164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1004164b576eSGleb Smirnoff 		return (ENXIO);
1005164b576eSGleb Smirnoff 	}
1006164b576eSGleb Smirnoff 	return (ng_ppp_comp_xmit(node, item, PROT_IP));
1007164b576eSGleb Smirnoff }
1008164b576eSGleb Smirnoff 
1009164b576eSGleb Smirnoff static int
1010164b576eSGleb Smirnoff ng_ppp_hcomp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
1011164b576eSGleb Smirnoff {
1012164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1013164b576eSGleb Smirnoff 
1014164b576eSGleb Smirnoff 	if (priv->conf.enableVJDecompression && priv->vjCompHooked) {
1015164b576eSGleb Smirnoff 		hook_p outHook = NULL;
1016164b576eSGleb Smirnoff 
1017164b576eSGleb Smirnoff 		switch (proto) {
1018164b576eSGleb Smirnoff 		    case PROT_VJCOMP:
1019164b576eSGleb Smirnoff 			outHook = priv->hooks[HOOK_INDEX_VJC_COMP];
1020164b576eSGleb Smirnoff 			break;
1021164b576eSGleb Smirnoff 		    case PROT_VJUNCOMP:
1022164b576eSGleb Smirnoff 			outHook = priv->hooks[HOOK_INDEX_VJC_UNCOMP];
1023164b576eSGleb Smirnoff 			break;
1024164b576eSGleb Smirnoff 		}
1025164b576eSGleb Smirnoff 
1026164b576eSGleb Smirnoff 		if (outHook) {
102739c14742SGleb Smirnoff 			int error;
102839c14742SGleb Smirnoff 
1029164b576eSGleb Smirnoff 			/* Send packet out hook. */
1030164b576eSGleb Smirnoff 			NG_FWD_ITEM_HOOK(error, item, outHook);
1031164b576eSGleb Smirnoff 			return (error);
1032164b576eSGleb Smirnoff 		}
1033164b576eSGleb Smirnoff 	}
1034164b576eSGleb Smirnoff 
1035164b576eSGleb Smirnoff 	return (ng_ppp_proto_recv(node, item, proto, linkNum));
1036164b576eSGleb Smirnoff }
1037164b576eSGleb Smirnoff 
1038164b576eSGleb Smirnoff /*
1039164b576eSGleb Smirnoff  * Receive data on a hook vjc_ip.
1040164b576eSGleb Smirnoff  */
1041164b576eSGleb Smirnoff static int
1042164b576eSGleb Smirnoff ng_ppp_rcvdata_vjc_ip(hook_p hook, item_p item)
1043164b576eSGleb Smirnoff {
1044164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
1045164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1046164b576eSGleb Smirnoff 
10476d67c50bSAlexander Motin 	if (!priv->conf.enableVJDecompression) {
1048164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1049164b576eSGleb Smirnoff 		return (ENXIO);
1050164b576eSGleb Smirnoff 	}
1051164b576eSGleb Smirnoff 	return (ng_ppp_proto_recv(node, item, PROT_IP, NG_PPP_BUNDLE_LINKNUM));
1052164b576eSGleb Smirnoff }
1053164b576eSGleb Smirnoff 
1054164b576eSGleb Smirnoff /*
1055164b576eSGleb Smirnoff  * Compression layer
1056164b576eSGleb Smirnoff  */
1057164b576eSGleb Smirnoff 
1058164b576eSGleb Smirnoff static int
1059164b576eSGleb Smirnoff ng_ppp_comp_xmit(node_p node, item_p item, uint16_t proto)
1060164b576eSGleb Smirnoff {
1061164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1062164b576eSGleb Smirnoff 
1063164b576eSGleb Smirnoff 	if (priv->conf.enableCompression &&
1064164b576eSGleb Smirnoff 	    proto < 0x4000 &&
1065164b576eSGleb Smirnoff 	    proto != PROT_COMPD &&
1066164b576eSGleb Smirnoff 	    proto != PROT_CRYPTD &&
1067164b576eSGleb Smirnoff 	    priv->hooks[HOOK_INDEX_COMPRESS] != NULL) {
1068164b576eSGleb Smirnoff 	        struct mbuf *m;
1069164b576eSGleb Smirnoff 		int error;
1070164b576eSGleb Smirnoff 
1071164b576eSGleb Smirnoff 	        NGI_GET_M(item, m);
1072164b576eSGleb Smirnoff 		if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) {
1073164b576eSGleb Smirnoff 			NG_FREE_ITEM(item);
1074164b576eSGleb Smirnoff 			return (ENOBUFS);
1075164b576eSGleb Smirnoff 		}
1076164b576eSGleb Smirnoff 		NGI_M(item) = m;
1077164b576eSGleb Smirnoff 
1078164b576eSGleb Smirnoff 		/* Send packet out hook. */
1079164b576eSGleb Smirnoff 		NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_COMPRESS]);
1080164b576eSGleb Smirnoff 		return (error);
1081164b576eSGleb Smirnoff 	}
1082164b576eSGleb Smirnoff 
1083164b576eSGleb Smirnoff 	return (ng_ppp_crypt_xmit(node, item, proto));
1084164b576eSGleb Smirnoff }
1085164b576eSGleb Smirnoff 
1086164b576eSGleb Smirnoff /*
1087164b576eSGleb Smirnoff  * Receive data on a hook compress.
1088164b576eSGleb Smirnoff  */
1089164b576eSGleb Smirnoff static int
1090164b576eSGleb Smirnoff ng_ppp_rcvdata_compress(hook_p hook, item_p item)
1091164b576eSGleb Smirnoff {
1092164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
1093164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1094164b576eSGleb Smirnoff 	uint16_t proto;
1095164b576eSGleb Smirnoff 
1096164b576eSGleb Smirnoff 	switch (priv->conf.enableCompression) {
1097164b576eSGleb Smirnoff 	    case NG_PPP_COMPRESS_NONE:
1098164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1099164b576eSGleb Smirnoff 		return (ENXIO);
1100164b576eSGleb Smirnoff 	    case NG_PPP_COMPRESS_FULL:
1101164b576eSGleb Smirnoff 		{
1102164b576eSGleb Smirnoff 			struct mbuf *m;
1103164b576eSGleb Smirnoff 
1104164b576eSGleb Smirnoff 			NGI_GET_M(item, m);
1105164b576eSGleb Smirnoff 			if ((m = ng_ppp_cutproto(m, &proto)) == NULL) {
1106164b576eSGleb Smirnoff 				NG_FREE_ITEM(item);
1107164b576eSGleb Smirnoff 				return (EIO);
1108164b576eSGleb Smirnoff 			}
1109164b576eSGleb Smirnoff 			NGI_M(item) = m;
1110164b576eSGleb Smirnoff 			if (!PROT_VALID(proto)) {
1111164b576eSGleb Smirnoff 				NG_FREE_ITEM(item);
1112164b576eSGleb Smirnoff 				return (EIO);
1113164b576eSGleb Smirnoff 			}
1114164b576eSGleb Smirnoff 		}
1115164b576eSGleb Smirnoff 		break;
1116164b576eSGleb Smirnoff 	    default:
1117164b576eSGleb Smirnoff 		proto = PROT_COMPD;
1118164b576eSGleb Smirnoff 		break;
1119164b576eSGleb Smirnoff 	}
1120164b576eSGleb Smirnoff 	return (ng_ppp_crypt_xmit(node, item, proto));
1121164b576eSGleb Smirnoff }
1122164b576eSGleb Smirnoff 
1123164b576eSGleb Smirnoff static int
1124164b576eSGleb Smirnoff ng_ppp_comp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
1125164b576eSGleb Smirnoff {
1126164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1127164b576eSGleb Smirnoff 
1128164b576eSGleb Smirnoff 	if (proto < 0x4000 &&
1129164b576eSGleb Smirnoff 	    ((proto == PROT_COMPD && priv->conf.enableDecompression) ||
1130164b576eSGleb Smirnoff 	    priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) &&
1131164b576eSGleb Smirnoff 	    priv->hooks[HOOK_INDEX_DECOMPRESS] != NULL) {
1132164b576eSGleb Smirnoff 		int error;
1133164b576eSGleb Smirnoff 
1134164b576eSGleb Smirnoff 		if (priv->conf.enableDecompression == NG_PPP_DECOMPRESS_FULL) {
1135164b576eSGleb Smirnoff 			struct mbuf *m;
1136164b576eSGleb Smirnoff 			NGI_GET_M(item, m);
1137164b576eSGleb Smirnoff 			if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) {
1138164b576eSGleb Smirnoff 				NG_FREE_ITEM(item);
1139164b576eSGleb Smirnoff 				return (EIO);
1140164b576eSGleb Smirnoff 			}
1141164b576eSGleb Smirnoff 			NGI_M(item) = m;
1142164b576eSGleb Smirnoff 		}
1143164b576eSGleb Smirnoff 
1144164b576eSGleb Smirnoff 		/* Send packet out hook. */
1145164b576eSGleb Smirnoff 		NG_FWD_ITEM_HOOK(error, item,
1146164b576eSGleb Smirnoff 		    priv->hooks[HOOK_INDEX_DECOMPRESS]);
1147164b576eSGleb Smirnoff 		return (error);
114839c14742SGleb Smirnoff 	} else if (proto == PROT_COMPD) {
114939c14742SGleb Smirnoff 		/* Disabled protos MUST be silently discarded, but
115039c14742SGleb Smirnoff 		 * unsupported MUST not. Let user-level decide this. */
115139c14742SGleb Smirnoff 		return (ng_ppp_bypass(node, item, proto, linkNum));
1152164b576eSGleb Smirnoff 	}
1153164b576eSGleb Smirnoff 
1154164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_recv(node, item, proto, linkNum));
1155164b576eSGleb Smirnoff }
1156164b576eSGleb Smirnoff 
1157164b576eSGleb Smirnoff /*
1158164b576eSGleb Smirnoff  * Receive data on a hook decompress.
1159164b576eSGleb Smirnoff  */
1160164b576eSGleb Smirnoff static int
1161164b576eSGleb Smirnoff ng_ppp_rcvdata_decompress(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 	uint16_t proto;
1166164b576eSGleb Smirnoff 	struct mbuf *m;
1167164b576eSGleb Smirnoff 
1168164b576eSGleb Smirnoff 	if (!priv->conf.enableDecompression) {
1169164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1170164b576eSGleb Smirnoff 		return (ENXIO);
1171164b576eSGleb Smirnoff 	}
1172164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
1173164b576eSGleb Smirnoff 	if ((m = ng_ppp_cutproto(m, &proto)) == NULL) {
1174164b576eSGleb Smirnoff 	        NG_FREE_ITEM(item);
1175164b576eSGleb Smirnoff 	        return (EIO);
1176164b576eSGleb Smirnoff 	}
1177164b576eSGleb Smirnoff 	NGI_M(item) = m;
1178164b576eSGleb Smirnoff 	if (!PROT_VALID(proto)) {
1179164b576eSGleb Smirnoff 		priv->bundleStats.badProtos++;
1180164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1181164b576eSGleb Smirnoff 		return (EIO);
1182164b576eSGleb Smirnoff 	}
1183164b576eSGleb Smirnoff 	return (ng_ppp_hcomp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM));
1184164b576eSGleb Smirnoff }
1185164b576eSGleb Smirnoff 
1186164b576eSGleb Smirnoff /*
1187164b576eSGleb Smirnoff  * Encryption layer
1188164b576eSGleb Smirnoff  */
1189164b576eSGleb Smirnoff 
1190164b576eSGleb Smirnoff static int
1191164b576eSGleb Smirnoff ng_ppp_crypt_xmit(node_p node, item_p item, uint16_t proto)
1192164b576eSGleb Smirnoff {
1193164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1194164b576eSGleb Smirnoff 
1195164b576eSGleb Smirnoff 	if (priv->conf.enableEncryption &&
1196164b576eSGleb Smirnoff 	    proto < 0x4000 &&
1197164b576eSGleb Smirnoff 	    proto != PROT_CRYPTD &&
1198164b576eSGleb Smirnoff 	    priv->hooks[HOOK_INDEX_ENCRYPT] != NULL) {
1199164b576eSGleb Smirnoff 		struct mbuf *m;
1200164b576eSGleb Smirnoff 		int error;
1201164b576eSGleb Smirnoff 
1202164b576eSGleb Smirnoff 	        NGI_GET_M(item, m);
1203164b576eSGleb Smirnoff 		if ((m = ng_ppp_addproto(m, proto, 0)) == NULL) {
1204164b576eSGleb Smirnoff 			NG_FREE_ITEM(item);
1205164b576eSGleb Smirnoff 			return (ENOBUFS);
1206164b576eSGleb Smirnoff 		}
1207164b576eSGleb Smirnoff 		NGI_M(item) = m;
1208164b576eSGleb Smirnoff 
1209164b576eSGleb Smirnoff 		/* Send packet out hook. */
1210164b576eSGleb Smirnoff 		NG_FWD_ITEM_HOOK(error, item, priv->hooks[HOOK_INDEX_ENCRYPT]);
1211164b576eSGleb Smirnoff 		return (error);
1212164b576eSGleb Smirnoff 	}
1213164b576eSGleb Smirnoff 
1214164b576eSGleb Smirnoff 	return (ng_ppp_mp_xmit(node, item, proto));
1215164b576eSGleb Smirnoff }
1216164b576eSGleb Smirnoff 
1217164b576eSGleb Smirnoff /*
1218164b576eSGleb Smirnoff  * Receive data on a hook encrypt.
1219164b576eSGleb Smirnoff  */
1220164b576eSGleb Smirnoff static int
1221164b576eSGleb Smirnoff ng_ppp_rcvdata_encrypt(hook_p hook, item_p item)
1222164b576eSGleb Smirnoff {
1223164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
1224164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1225164b576eSGleb Smirnoff 
1226164b576eSGleb Smirnoff 	if (!priv->conf.enableEncryption) {
1227164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1228164b576eSGleb Smirnoff 		return (ENXIO);
1229164b576eSGleb Smirnoff 	}
1230164b576eSGleb Smirnoff 	return (ng_ppp_mp_xmit(node, item, PROT_CRYPTD));
1231164b576eSGleb Smirnoff }
1232164b576eSGleb Smirnoff 
1233164b576eSGleb Smirnoff static int
1234164b576eSGleb Smirnoff ng_ppp_crypt_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
1235164b576eSGleb Smirnoff {
1236164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1237164b576eSGleb Smirnoff 
123839c14742SGleb Smirnoff 	if (proto == PROT_CRYPTD) {
123939c14742SGleb Smirnoff 		if (priv->conf.enableDecryption &&
1240164b576eSGleb Smirnoff 		    priv->hooks[HOOK_INDEX_DECRYPT] != NULL) {
1241164b576eSGleb Smirnoff 			int error;
1242164b576eSGleb Smirnoff 
1243164b576eSGleb Smirnoff 			/* Send packet out hook. */
124439c14742SGleb Smirnoff 			NG_FWD_ITEM_HOOK(error, item,
124539c14742SGleb Smirnoff 			    priv->hooks[HOOK_INDEX_DECRYPT]);
1246164b576eSGleb Smirnoff 			return (error);
124739c14742SGleb Smirnoff 		} else {
124839c14742SGleb Smirnoff 			/* Disabled protos MUST be silently discarded, but
124939c14742SGleb Smirnoff 			 * unsupported MUST not. Let user-level decide this. */
125039c14742SGleb Smirnoff 			return (ng_ppp_bypass(node, item, proto, linkNum));
125139c14742SGleb Smirnoff 		}
1252164b576eSGleb Smirnoff 	}
1253164b576eSGleb Smirnoff 
1254164b576eSGleb Smirnoff 	return (ng_ppp_comp_recv(node, item, proto, linkNum));
1255164b576eSGleb Smirnoff }
1256164b576eSGleb Smirnoff 
1257164b576eSGleb Smirnoff /*
1258164b576eSGleb Smirnoff  * Receive data on a hook decrypt.
1259164b576eSGleb Smirnoff  */
1260164b576eSGleb Smirnoff static int
1261164b576eSGleb Smirnoff ng_ppp_rcvdata_decrypt(hook_p hook, item_p item)
1262164b576eSGleb Smirnoff {
1263164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
1264164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1265164b576eSGleb Smirnoff 	uint16_t proto;
1266164b576eSGleb Smirnoff 	struct mbuf *m;
1267164b576eSGleb Smirnoff 
1268164b576eSGleb Smirnoff 	if (!priv->conf.enableDecryption) {
1269164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1270164b576eSGleb Smirnoff 		return (ENXIO);
1271164b576eSGleb Smirnoff 	}
1272164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
1273164b576eSGleb Smirnoff 	if ((m = ng_ppp_cutproto(m, &proto)) == NULL) {
1274164b576eSGleb Smirnoff 	        NG_FREE_ITEM(item);
1275164b576eSGleb Smirnoff 	        return (EIO);
1276164b576eSGleb Smirnoff 	}
1277164b576eSGleb Smirnoff 	NGI_M(item) = m;
1278164b576eSGleb Smirnoff 	if (!PROT_VALID(proto)) {
1279164b576eSGleb Smirnoff 		priv->bundleStats.badProtos++;
1280164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
1281164b576eSGleb Smirnoff 		return (EIO);
1282164b576eSGleb Smirnoff 	}
1283164b576eSGleb Smirnoff 	return (ng_ppp_comp_recv(node, item, proto, NG_PPP_BUNDLE_LINKNUM));
1284164b576eSGleb Smirnoff }
1285164b576eSGleb Smirnoff 
1286164b576eSGleb Smirnoff /*
1287164b576eSGleb Smirnoff  * Link layer
1288164b576eSGleb Smirnoff  */
1289164b576eSGleb Smirnoff 
1290164b576eSGleb Smirnoff static int
1291e89c1507SAlexander Motin ng_ppp_link_xmit(node_p node, item_p item, uint16_t proto, uint16_t linkNum, int plen)
12924cf49a43SJulian Elischer {
129330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1294a9b3dca5SArchie Cobbs 	struct ng_ppp_link *link;
1295e149c4e2SArchie Cobbs 	int len, error;
1296069154d5SJulian Elischer 	struct mbuf *m;
1297164b576eSGleb Smirnoff 	uint16_t mru;
12984cf49a43SJulian Elischer 
1299164b576eSGleb Smirnoff 	/* Check if link correct. */
1300164b576eSGleb Smirnoff 	if (linkNum >= NG_PPP_MAX_LINKS) {
1301e89c1507SAlexander Motin 		ERROUT(ENETDOWN);
13023949bee8SArchie Cobbs 	}
1303164b576eSGleb Smirnoff 
1304164b576eSGleb Smirnoff 	/* Get link pointer (optimization). */
1305164b576eSGleb Smirnoff 	link = &priv->links[linkNum];
1306164b576eSGleb Smirnoff 
1307164b576eSGleb Smirnoff 	/* Check link status (if real). */
1308164b576eSGleb Smirnoff 	if (link->hook == NULL) {
1309e89c1507SAlexander Motin 		ERROUT(ENETDOWN);
13100e11d0f3SArchie Cobbs 	}
13113949bee8SArchie Cobbs 
1312164b576eSGleb Smirnoff 	/* Extract mbuf. */
1313164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
1314164b576eSGleb Smirnoff 
1315164b576eSGleb Smirnoff 	/* Check peer's MRU for this link. */
1316164b576eSGleb Smirnoff 	mru = link->conf.mru;
1317931c828aSArchie Cobbs 	if (mru != 0 && m->m_pkthdr.len > mru) {
1318931c828aSArchie Cobbs 		NG_FREE_M(m);
1319e89c1507SAlexander Motin 		ERROUT(EMSGSIZE);
1320931c828aSArchie Cobbs 	}
1321931c828aSArchie Cobbs 
1322164b576eSGleb Smirnoff 	/* Prepend protocol number, possibly compressed. */
1323164b576eSGleb Smirnoff 	if ((m = ng_ppp_addproto(m, proto, link->conf.enableProtoComp)) ==
1324164b576eSGleb Smirnoff 	    NULL) {
1325e89c1507SAlexander Motin 		ERROUT(ENOBUFS);
1326d690a6e7SArchie Cobbs 	}
1327d690a6e7SArchie Cobbs 
1328164b576eSGleb Smirnoff 	/* Prepend address and control field (unless compressed). */
1329a9b3dca5SArchie Cobbs 	if (proto == PROT_LCP || !link->conf.enableACFComp) {
1330e89c1507SAlexander Motin 		if ((m = ng_ppp_prepend(m, &ng_ppp_acf, 2)) == NULL)
1331e89c1507SAlexander Motin 			ERROUT(ENOBUFS);
1332d690a6e7SArchie Cobbs 	}
1333d690a6e7SArchie Cobbs 
1334164b576eSGleb Smirnoff 	/* Deliver frame. */
1335e149c4e2SArchie Cobbs 	len = m->m_pkthdr.len;
1336069154d5SJulian Elischer 	NG_FWD_NEW_DATA(error, item, link->hook, m);
1337fb1fc8abSArchie Cobbs 
1338e89c1507SAlexander Motin 	mtx_lock(&priv->xmtx);
1339e89c1507SAlexander Motin 
1340e89c1507SAlexander Motin 	/* Update link stats. */
1341a9b3dca5SArchie Cobbs 	link->stats.xmitFrames++;
1342a9b3dca5SArchie Cobbs 	link->stats.xmitOctets += len;
1343164b576eSGleb Smirnoff 
1344e89c1507SAlexander Motin 	/* Update bundle stats. */
1345e89c1507SAlexander Motin 	if (plen > 0) {
1346e89c1507SAlexander Motin 	    priv->bundleStats.xmitFrames++;
1347e89c1507SAlexander Motin 	    priv->bundleStats.xmitOctets += plen;
1348e89c1507SAlexander Motin 	}
1349e89c1507SAlexander Motin 
1350e89c1507SAlexander Motin 	/* Update 'bytes in queue' counter. */
1351e89c1507SAlexander Motin 	if (error == 0) {
1352164b576eSGleb Smirnoff 		/* bytesInQueue and lastWrite required only for mp_strategy. */
1353c35e19c4SAlexander Motin 		if (priv->conf.enableMultilink && !priv->allLinksEqual &&
1354c35e19c4SAlexander Motin 		    !priv->conf.enableRoundRobin) {
1355fd58342cSAlexander Motin 			/* If queue was empty, then mark this time. */
1356fd58342cSAlexander Motin 			if (link->bytesInQueue == 0)
1357a9b3dca5SArchie Cobbs 				getmicrouptime(&link->lastWrite);
1358fd58342cSAlexander Motin 			link->bytesInQueue += len + MP_AVERAGE_LINK_OVERHEAD;
1359fd58342cSAlexander Motin 			/* Limit max queue length to 50 pkts. BW can be defined
1360fd58342cSAlexander Motin 		    	   incorrectly and link may not signal overload. */
1361fd58342cSAlexander Motin 			if (link->bytesInQueue > 50 * 1600)
1362fd58342cSAlexander Motin 				link->bytesInQueue = 50 * 1600;
1363fb1fc8abSArchie Cobbs 		}
13643949bee8SArchie Cobbs 	}
1365e89c1507SAlexander Motin 	mtx_unlock(&priv->xmtx);
1366e89c1507SAlexander Motin 	return (error);
1367e89c1507SAlexander Motin 
1368e89c1507SAlexander Motin done:
1369e89c1507SAlexander Motin 	NG_FREE_ITEM(item);
1370164b576eSGleb Smirnoff 	return (error);
1371164b576eSGleb Smirnoff }
1372164b576eSGleb Smirnoff 
1373164b576eSGleb Smirnoff /*
1374164b576eSGleb Smirnoff  * Receive data on a hook linkX.
1375164b576eSGleb Smirnoff  */
1376164b576eSGleb Smirnoff static int
1377164b576eSGleb Smirnoff ng_ppp_rcvdata(hook_p hook, item_p item)
1378164b576eSGleb Smirnoff {
1379164b576eSGleb Smirnoff 	const node_p node = NG_HOOK_NODE(hook);
1380164b576eSGleb Smirnoff 	const priv_p priv = NG_NODE_PRIVATE(node);
1381164b576eSGleb Smirnoff 	const int index = (intptr_t)NG_HOOK_PRIVATE(hook);
1382164b576eSGleb Smirnoff 	const uint16_t linkNum = (uint16_t)~index;
1383164b576eSGleb Smirnoff 	struct ng_ppp_link * const link = &priv->links[linkNum];
1384164b576eSGleb Smirnoff 	uint16_t proto;
1385164b576eSGleb Smirnoff 	struct mbuf *m;
1386e89c1507SAlexander Motin 	int error = 0;
1387164b576eSGleb Smirnoff 
138833eb7cb0SMatt Jacob 	KASSERT(linkNum < NG_PPP_MAX_LINKS,
1389164b576eSGleb Smirnoff 	    ("%s: bogus index 0x%x", __func__, index));
1390164b576eSGleb Smirnoff 
1391164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
1392164b576eSGleb Smirnoff 
1393e89c1507SAlexander Motin 	mtx_lock(&priv->rmtx);
1394e89c1507SAlexander Motin 
1395164b576eSGleb Smirnoff 	/* Stats */
1396164b576eSGleb Smirnoff 	link->stats.recvFrames++;
1397164b576eSGleb Smirnoff 	link->stats.recvOctets += m->m_pkthdr.len;
1398164b576eSGleb Smirnoff 
1399164b576eSGleb Smirnoff 	/* Strip address and control fields, if present. */
1400e89c1507SAlexander Motin 	if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL)
1401e89c1507SAlexander Motin 		ERROUT(ENOBUFS);
1402b4d0be22SAlexander Motin 	if (mtod(m, uint8_t *)[0] == 0xff &&
1403b4d0be22SAlexander Motin 	    mtod(m, uint8_t *)[1] == 0x03)
1404164b576eSGleb Smirnoff 		m_adj(m, 2);
1405164b576eSGleb Smirnoff 
1406e89c1507SAlexander Motin 	/* Get protocol number */
1407e89c1507SAlexander Motin 	if ((m = ng_ppp_cutproto(m, &proto)) == NULL)
1408e89c1507SAlexander Motin 		ERROUT(ENOBUFS);
1409164b576eSGleb Smirnoff 	NGI_M(item) = m; 	/* Put changed m back into item. */
1410164b576eSGleb Smirnoff 
1411164b576eSGleb Smirnoff 	if (!PROT_VALID(proto)) {
1412164b576eSGleb Smirnoff 		link->stats.badProtos++;
1413e89c1507SAlexander Motin 		ERROUT(EIO);
1414164b576eSGleb Smirnoff 	}
1415164b576eSGleb Smirnoff 
141639c14742SGleb Smirnoff 	/* LCP packets must go directly to bypass. */
1417e89c1507SAlexander Motin 	if (proto >= 0xB000) {
1418e89c1507SAlexander Motin 		mtx_unlock(&priv->rmtx);
141939c14742SGleb Smirnoff 		return (ng_ppp_bypass(node, item, proto, linkNum));
142039c14742SGleb Smirnoff 	}
142139c14742SGleb Smirnoff 
1422e89c1507SAlexander Motin 	/* Other packets are denied on a disabled link. */
1423e89c1507SAlexander Motin 	if (!link->conf.enableLink)
1424e89c1507SAlexander Motin 		ERROUT(ENXIO);
1425e89c1507SAlexander Motin 
1426e89c1507SAlexander Motin 	/* Proceed to multilink layer. Mutex will be unlocked inside. */
1427e89c1507SAlexander Motin 	error = ng_ppp_mp_recv(node, item, proto, linkNum);
1428e89c1507SAlexander Motin 	mtx_assert(&priv->rmtx, MA_NOTOWNED);
1429e89c1507SAlexander Motin 	return (error);
1430e89c1507SAlexander Motin 
1431e89c1507SAlexander Motin done:
1432e89c1507SAlexander Motin 	mtx_unlock(&priv->rmtx);
1433e89c1507SAlexander Motin 	NG_FREE_ITEM(item);
1434e89c1507SAlexander Motin 	return (error);
1435164b576eSGleb Smirnoff }
1436164b576eSGleb Smirnoff 
1437164b576eSGleb Smirnoff /*
1438164b576eSGleb Smirnoff  * Multilink layer
1439164b576eSGleb Smirnoff  */
14403949bee8SArchie Cobbs 
14413949bee8SArchie Cobbs /*
14423949bee8SArchie Cobbs  * Handle an incoming multi-link fragment
1443a9b3dca5SArchie Cobbs  *
1444a9b3dca5SArchie Cobbs  * The fragment reassembly algorithm is somewhat complex. This is mainly
1445a9b3dca5SArchie Cobbs  * because we are required not to reorder the reconstructed packets, yet
1446a9b3dca5SArchie Cobbs  * fragments are only guaranteed to arrive in order on a per-link basis.
1447a9b3dca5SArchie Cobbs  * In other words, when we have a complete packet ready, but the previous
1448a9b3dca5SArchie Cobbs  * packet is still incomplete, we have to decide between delivering the
1449a9b3dca5SArchie Cobbs  * complete packet and throwing away the incomplete one, or waiting to
1450a9b3dca5SArchie Cobbs  * see if the remainder of the incomplete one arrives, at which time we
1451a9b3dca5SArchie Cobbs  * can deliver both packets, in order.
1452a9b3dca5SArchie Cobbs  *
1453a9b3dca5SArchie Cobbs  * This problem is exacerbated by "sequence number slew", which is when
1454a9b3dca5SArchie Cobbs  * the sequence numbers coming in from different links are far apart from
1455a9b3dca5SArchie Cobbs  * each other. In particular, certain unnamed equipment (*cough* Ascend)
1456a9b3dca5SArchie Cobbs  * has been seen to generate sequence number slew of up to 10 on an ISDN
1457a9b3dca5SArchie Cobbs  * 2B-channel MP link. There is nothing invalid about sequence number slew
1458a9b3dca5SArchie Cobbs  * but it makes the reasssembly process have to work harder.
1459a9b3dca5SArchie Cobbs  *
1460a9b3dca5SArchie Cobbs  * However, the peer is required to transmit fragments in order on each
1461a9b3dca5SArchie Cobbs  * link. That means if we define MSEQ as the minimum over all links of
1462a9b3dca5SArchie Cobbs  * the highest sequence number received on that link, then we can always
1463a9b3dca5SArchie Cobbs  * give up any hope of receiving a fragment with sequence number < MSEQ in
1464a9b3dca5SArchie Cobbs  * the future (all of this using 'wraparound' sequence number space).
1465a9b3dca5SArchie Cobbs  * Therefore we can always immediately throw away incomplete packets
1466a9b3dca5SArchie Cobbs  * missing fragments with sequence numbers < MSEQ.
1467a9b3dca5SArchie Cobbs  *
1468a9b3dca5SArchie Cobbs  * Here is an overview of our algorithm:
1469a9b3dca5SArchie Cobbs  *
1470a9b3dca5SArchie Cobbs  *    o Received fragments are inserted into a queue, for which we
1471a9b3dca5SArchie Cobbs  *	maintain these invariants between calls to this function:
1472a9b3dca5SArchie Cobbs  *
1473a9b3dca5SArchie Cobbs  *	- Fragments are ordered in the queue by sequence number
1474a9b3dca5SArchie Cobbs  *	- If a complete packet is at the head of the queue, then
1475a9b3dca5SArchie Cobbs  *	  the first fragment in the packet has seq# > MSEQ + 1
1476a9b3dca5SArchie Cobbs  *	  (otherwise, we could deliver it immediately)
1477a9b3dca5SArchie Cobbs  *	- If any fragments have seq# < MSEQ, then they are necessarily
1478a9b3dca5SArchie Cobbs  *	  part of a packet whose missing seq#'s are all > MSEQ (otherwise,
1479a9b3dca5SArchie Cobbs  *	  we can throw them away because they'll never be completed)
1480a9b3dca5SArchie Cobbs  *	- The queue contains at most MP_MAX_QUEUE_LEN fragments
1481a9b3dca5SArchie Cobbs  *
1482a9b3dca5SArchie Cobbs  *    o We have a periodic timer that checks the queue for the first
1483a9b3dca5SArchie Cobbs  *	complete packet that has been sitting in the queue "too long".
1484a9b3dca5SArchie Cobbs  *	When one is detected, all previous (incomplete) fragments are
1485a9b3dca5SArchie Cobbs  *	discarded, their missing fragments are declared lost and MSEQ
1486a9b3dca5SArchie Cobbs  *	is increased.
1487a9b3dca5SArchie Cobbs  *
1488a9b3dca5SArchie Cobbs  *    o If we recieve a fragment with seq# < MSEQ, we throw it away
1489a9b3dca5SArchie Cobbs  *	because we've already delcared it lost.
1490a9b3dca5SArchie Cobbs  *
1491a9b3dca5SArchie Cobbs  * This assumes linkNum != NG_PPP_BUNDLE_LINKNUM.
14923949bee8SArchie Cobbs  */
14933949bee8SArchie Cobbs static int
1494164b576eSGleb Smirnoff ng_ppp_mp_recv(node_p node, item_p item, uint16_t proto, uint16_t linkNum)
14953949bee8SArchie Cobbs {
149630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1497a9b3dca5SArchie Cobbs 	struct ng_ppp_link *const link = &priv->links[linkNum];
149893caf2e2SAlexander Motin 	struct ng_ppp_frag *frag;
1499a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent;
1500a9b3dca5SArchie Cobbs 	int i, diff, inserted;
1501069154d5SJulian Elischer 	struct mbuf *m;
1502e89c1507SAlexander Motin 	int	error = 0;
15033949bee8SArchie Cobbs 
1504e89c1507SAlexander Motin 	if ((!priv->conf.enableMultilink) || proto != PROT_MP) {
1505e89c1507SAlexander Motin 		/* Stats */
1506e89c1507SAlexander Motin 		priv->bundleStats.recvFrames++;
1507e89c1507SAlexander Motin 		priv->bundleStats.recvOctets += NGI_M(item)->m_pkthdr.len;
1508e89c1507SAlexander Motin 
1509e89c1507SAlexander Motin 		mtx_unlock(&priv->rmtx);
1510164b576eSGleb Smirnoff 		return (ng_ppp_crypt_recv(node, item, proto, linkNum));
1511e89c1507SAlexander Motin 	}
1512164b576eSGleb Smirnoff 
1513069154d5SJulian Elischer 	NGI_GET_M(item, m);
1514b4c44c30SArchie Cobbs 
151593caf2e2SAlexander Motin 	/* Get a new frag struct from the free queue */
151693caf2e2SAlexander Motin 	if ((frag = TAILQ_FIRST(&priv->fragsfree)) == NULL) {
151793caf2e2SAlexander Motin 		printf("No free fragments headers in ng_ppp!\n");
151893caf2e2SAlexander Motin 		NG_FREE_M(m);
151993caf2e2SAlexander Motin 		goto process;
152093caf2e2SAlexander Motin 	}
152193caf2e2SAlexander Motin 
15223949bee8SArchie Cobbs 	/* Extract fragment information from MP header */
15233949bee8SArchie Cobbs 	if (priv->conf.recvShortSeq) {
1524164b576eSGleb Smirnoff 		uint16_t shdr;
15253949bee8SArchie Cobbs 
15263949bee8SArchie Cobbs 		if (m->m_pkthdr.len < 2) {
1527a9b3dca5SArchie Cobbs 			link->stats.runts++;
1528069154d5SJulian Elischer 			NG_FREE_M(m);
1529e89c1507SAlexander Motin 			ERROUT(EINVAL);
15303949bee8SArchie Cobbs 		}
15313ca24c28SJulian Elischer 		if (m->m_len < 2 && (m = m_pullup(m, 2)) == NULL)
1532e89c1507SAlexander Motin 			ERROUT(ENOBUFS);
15333ca24c28SJulian Elischer 
1534164b576eSGleb Smirnoff 		shdr = ntohs(*mtod(m, uint16_t *));
15356f16db81SArchie Cobbs 		frag->seq = MP_SHORT_EXTEND(shdr);
15363949bee8SArchie Cobbs 		frag->first = (shdr & MP_SHORT_FIRST_FLAG) != 0;
15373949bee8SArchie Cobbs 		frag->last = (shdr & MP_SHORT_LAST_FLAG) != 0;
1538a9b3dca5SArchie Cobbs 		diff = MP_SHORT_SEQ_DIFF(frag->seq, priv->mseq);
15393949bee8SArchie Cobbs 		m_adj(m, 2);
15403949bee8SArchie Cobbs 	} else {
1541164b576eSGleb Smirnoff 		uint32_t lhdr;
15423949bee8SArchie Cobbs 
15433949bee8SArchie Cobbs 		if (m->m_pkthdr.len < 4) {
1544a9b3dca5SArchie Cobbs 			link->stats.runts++;
1545069154d5SJulian Elischer 			NG_FREE_M(m);
1546e89c1507SAlexander Motin 			ERROUT(EINVAL);
15473949bee8SArchie Cobbs 		}
15483ca24c28SJulian Elischer 		if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
1549e89c1507SAlexander Motin 			ERROUT(ENOBUFS);
15503ca24c28SJulian Elischer 
1551164b576eSGleb Smirnoff 		lhdr = ntohl(*mtod(m, uint32_t *));
15526f16db81SArchie Cobbs 		frag->seq = MP_LONG_EXTEND(lhdr);
15533949bee8SArchie Cobbs 		frag->first = (lhdr & MP_LONG_FIRST_FLAG) != 0;
15543949bee8SArchie Cobbs 		frag->last = (lhdr & MP_LONG_LAST_FLAG) != 0;
1555a9b3dca5SArchie Cobbs 		diff = MP_LONG_SEQ_DIFF(frag->seq, priv->mseq);
15563949bee8SArchie Cobbs 		m_adj(m, 4);
15573949bee8SArchie Cobbs 	}
15583949bee8SArchie Cobbs 	frag->data = m;
1559a9b3dca5SArchie Cobbs 	getmicrouptime(&frag->timestamp);
15603949bee8SArchie Cobbs 
1561a9b3dca5SArchie Cobbs 	/* If sequence number is < MSEQ, we've already declared this
1562a9b3dca5SArchie Cobbs 	   fragment as lost, so we have no choice now but to drop it */
1563a9b3dca5SArchie Cobbs 	if (diff < 0) {
1564a9b3dca5SArchie Cobbs 		link->stats.dropFragments++;
1565069154d5SJulian Elischer 		NG_FREE_M(m);
1566e89c1507SAlexander Motin 		ERROUT(0);
1567a9b3dca5SArchie Cobbs 	}
15683949bee8SArchie Cobbs 
1569a9b3dca5SArchie Cobbs 	/* Update highest received sequence number on this link and MSEQ */
1570a9b3dca5SArchie Cobbs 	priv->mseq = link->seq = frag->seq;
1571a9b3dca5SArchie Cobbs 	for (i = 0; i < priv->numActiveLinks; i++) {
1572a9b3dca5SArchie Cobbs 		struct ng_ppp_link *const alink =
1573a9b3dca5SArchie Cobbs 		    &priv->links[priv->activeLinks[i]];
1574a9b3dca5SArchie Cobbs 
15756f16db81SArchie Cobbs 		if (MP_RECV_SEQ_DIFF(priv, alink->seq, priv->mseq) < 0)
1576a9b3dca5SArchie Cobbs 			priv->mseq = alink->seq;
1577a9b3dca5SArchie Cobbs 	}
15783949bee8SArchie Cobbs 
157993caf2e2SAlexander Motin 	/* Remove frag struct from free queue. */
158093caf2e2SAlexander Motin 	TAILQ_REMOVE(&priv->fragsfree, frag, f_qent);
15813949bee8SArchie Cobbs 
1582a9b3dca5SArchie Cobbs 	/* Add fragment to queue, which is sorted by sequence number */
15831e7a9f72SArchie Cobbs 	inserted = 0;
15845f90cac7SKirk McKusick 	TAILQ_FOREACH_REVERSE(qent, &priv->frags, ng_ppp_fraglist, f_qent) {
15856f16db81SArchie Cobbs 		diff = MP_RECV_SEQ_DIFF(priv, frag->seq, qent->seq);
15863949bee8SArchie Cobbs 		if (diff > 0) {
15875f90cac7SKirk McKusick 			TAILQ_INSERT_AFTER(&priv->frags, qent, frag, f_qent);
15881e7a9f72SArchie Cobbs 			inserted = 1;
15893949bee8SArchie Cobbs 			break;
15903949bee8SArchie Cobbs 		} else if (diff == 0) {	     /* should never happen! */
1591a9b3dca5SArchie Cobbs 			link->stats.dupFragments++;
1592069154d5SJulian Elischer 			NG_FREE_M(frag->data);
159393caf2e2SAlexander Motin 			TAILQ_INSERT_HEAD(&priv->fragsfree, frag, f_qent);
1594e89c1507SAlexander Motin 			ERROUT(EINVAL);
15953949bee8SArchie Cobbs 		}
15963949bee8SArchie Cobbs 	}
15971e7a9f72SArchie Cobbs 	if (!inserted)
15985f90cac7SKirk McKusick 		TAILQ_INSERT_HEAD(&priv->frags, frag, f_qent);
15993949bee8SArchie Cobbs 
1600e89c1507SAlexander Motin process:
1601a9b3dca5SArchie Cobbs 	/* Process the queue */
1602e89c1507SAlexander Motin 	/* NOTE: rmtx will be unlocked for sending time! */
16035716b3f1SAlexander Motin 	error = ng_ppp_frag_process(node, item);
16045716b3f1SAlexander Motin 	mtx_unlock(&priv->rmtx);
16055716b3f1SAlexander Motin 	return (error);
1606e89c1507SAlexander Motin 
1607e89c1507SAlexander Motin done:
1608e89c1507SAlexander Motin 	mtx_unlock(&priv->rmtx);
16095716b3f1SAlexander Motin 	NG_FREE_ITEM(item);
1610e89c1507SAlexander Motin 	return (error);
16113949bee8SArchie Cobbs }
16121e7a9f72SArchie Cobbs 
1613164b576eSGleb Smirnoff /************************************************************************
1614164b576eSGleb Smirnoff 			HELPER STUFF
1615164b576eSGleb Smirnoff  ************************************************************************/
1616164b576eSGleb Smirnoff 
1617a9b3dca5SArchie Cobbs /*
16188e8f114eSAlexander Motin  * If new mseq > current then set it and update all active links
16198e8f114eSAlexander Motin  */
16208e8f114eSAlexander Motin static void
16218e8f114eSAlexander Motin ng_ppp_bump_mseq(node_p node, int32_t new_mseq)
16228e8f114eSAlexander Motin {
16238e8f114eSAlexander Motin 	const priv_p priv = NG_NODE_PRIVATE(node);
16248e8f114eSAlexander Motin 	int i;
16258e8f114eSAlexander Motin 
16268e8f114eSAlexander Motin 	if (MP_RECV_SEQ_DIFF(priv, priv->mseq, new_mseq) < 0) {
16278e8f114eSAlexander Motin 		priv->mseq = new_mseq;
16288e8f114eSAlexander Motin 		for (i = 0; i < priv->numActiveLinks; i++) {
16298e8f114eSAlexander Motin 			struct ng_ppp_link *const alink =
16308e8f114eSAlexander Motin 			    &priv->links[priv->activeLinks[i]];
16318e8f114eSAlexander Motin 
16328e8f114eSAlexander Motin 			if (MP_RECV_SEQ_DIFF(priv,
16338e8f114eSAlexander Motin 			    alink->seq, new_mseq) < 0)
16348e8f114eSAlexander Motin 				alink->seq = new_mseq;
16358e8f114eSAlexander Motin 		}
16368e8f114eSAlexander Motin 	}
16378e8f114eSAlexander Motin }
16388e8f114eSAlexander Motin 
16398e8f114eSAlexander Motin /*
1640a9b3dca5SArchie Cobbs  * Examine our list of fragments, and determine if there is a
1641a9b3dca5SArchie Cobbs  * complete and deliverable packet at the head of the list.
1642a9b3dca5SArchie Cobbs  * Return 1 if so, zero otherwise.
1643a9b3dca5SArchie Cobbs  */
1644a9b3dca5SArchie Cobbs static int
1645a9b3dca5SArchie Cobbs ng_ppp_check_packet(node_p node)
1646a9b3dca5SArchie Cobbs {
164730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1648a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext;
16493949bee8SArchie Cobbs 
1650a9b3dca5SArchie Cobbs 	/* Check for empty queue */
16515f90cac7SKirk McKusick 	if (TAILQ_EMPTY(&priv->frags))
1652a9b3dca5SArchie Cobbs 		return (0);
1653a9b3dca5SArchie Cobbs 
1654a9b3dca5SArchie Cobbs 	/* Check first fragment is the start of a deliverable packet */
16555f90cac7SKirk McKusick 	qent = TAILQ_FIRST(&priv->frags);
16566f16db81SArchie Cobbs 	if (!qent->first || MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) > 1)
1657a9b3dca5SArchie Cobbs 		return (0);
1658a9b3dca5SArchie Cobbs 
1659a9b3dca5SArchie Cobbs 	/* Check that all the fragments are there */
1660a9b3dca5SArchie Cobbs 	while (!qent->last) {
16615f90cac7SKirk McKusick 		qnext = TAILQ_NEXT(qent, f_qent);
16625f90cac7SKirk McKusick 		if (qnext == NULL)	/* end of queue */
1663a9b3dca5SArchie Cobbs 			return (0);
16646f16db81SArchie Cobbs 		if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq))
1665a9b3dca5SArchie Cobbs 			return (0);
1666a9b3dca5SArchie Cobbs 		qent = qnext;
1667a9b3dca5SArchie Cobbs 	}
1668a9b3dca5SArchie Cobbs 
1669a9b3dca5SArchie Cobbs 	/* Got one */
1670a9b3dca5SArchie Cobbs 	return (1);
1671a9b3dca5SArchie Cobbs }
1672a9b3dca5SArchie Cobbs 
1673a9b3dca5SArchie Cobbs /*
1674a9b3dca5SArchie Cobbs  * Pull a completed packet off the head of the incoming fragment queue.
1675a9b3dca5SArchie Cobbs  * This assumes there is a completed packet there to pull off.
1676a9b3dca5SArchie Cobbs  */
1677a9b3dca5SArchie Cobbs static void
16783ca24c28SJulian Elischer ng_ppp_get_packet(node_p node, struct mbuf **mp)
1679a9b3dca5SArchie Cobbs {
168030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1681a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext;
1682a9b3dca5SArchie Cobbs 	struct mbuf *m = NULL, *tail;
1683a9b3dca5SArchie Cobbs 
16845f90cac7SKirk McKusick 	qent = TAILQ_FIRST(&priv->frags);
16855f90cac7SKirk McKusick 	KASSERT(!TAILQ_EMPTY(&priv->frags) && qent->first,
16866e551fb6SDavid E. O'Brien 	    ("%s: no packet", __func__));
1687a9b3dca5SArchie Cobbs 	for (tail = NULL; qent != NULL; qent = qnext) {
16885f90cac7SKirk McKusick 		qnext = TAILQ_NEXT(qent, f_qent);
16895f90cac7SKirk McKusick 		KASSERT(!TAILQ_EMPTY(&priv->frags),
16906e551fb6SDavid E. O'Brien 		    ("%s: empty q", __func__));
16915f90cac7SKirk McKusick 		TAILQ_REMOVE(&priv->frags, qent, f_qent);
16923ca24c28SJulian Elischer 		if (tail == NULL)
16933949bee8SArchie Cobbs 			tail = m = qent->data;
16943ca24c28SJulian Elischer 		else {
16953949bee8SArchie Cobbs 			m->m_pkthdr.len += qent->data->m_pkthdr.len;
16963949bee8SArchie Cobbs 			tail->m_next = qent->data;
16973949bee8SArchie Cobbs 		}
16983949bee8SArchie Cobbs 		while (tail->m_next != NULL)
16993949bee8SArchie Cobbs 			tail = tail->m_next;
17008e8f114eSAlexander Motin 		if (qent->last) {
17013949bee8SArchie Cobbs 			qnext = NULL;
17028e8f114eSAlexander Motin 			/* Bump MSEQ if necessary */
17038e8f114eSAlexander Motin 			ng_ppp_bump_mseq(node, qent->seq);
17048e8f114eSAlexander Motin 		}
170593caf2e2SAlexander Motin 		TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent);
1706a9b3dca5SArchie Cobbs 	}
1707a9b3dca5SArchie Cobbs 	*mp = m;
17083949bee8SArchie Cobbs }
17093949bee8SArchie Cobbs 
1710a9b3dca5SArchie Cobbs /*
1711a9b3dca5SArchie Cobbs  * Trim fragments from the queue whose packets can never be completed.
1712a9b3dca5SArchie Cobbs  * This assumes a complete packet is NOT at the beginning of the queue.
1713a9b3dca5SArchie Cobbs  * Returns 1 if fragments were removed, zero otherwise.
1714a9b3dca5SArchie Cobbs  */
1715a9b3dca5SArchie Cobbs static int
1716a9b3dca5SArchie Cobbs ng_ppp_frag_trim(node_p node)
1717a9b3dca5SArchie Cobbs {
171830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1719a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext = NULL;
1720a9b3dca5SArchie Cobbs 	int removed = 0;
1721a9b3dca5SArchie Cobbs 
1722a9b3dca5SArchie Cobbs 	/* Scan for "dead" fragments and remove them */
1723a9b3dca5SArchie Cobbs 	while (1) {
1724a9b3dca5SArchie Cobbs 		int dead = 0;
1725a9b3dca5SArchie Cobbs 
1726a9b3dca5SArchie Cobbs 		/* If queue is empty, we're done */
17275f90cac7SKirk McKusick 		if (TAILQ_EMPTY(&priv->frags))
17283949bee8SArchie Cobbs 			break;
1729a9b3dca5SArchie Cobbs 
1730a9b3dca5SArchie Cobbs 		/* Determine whether first fragment can ever be completed */
17315f90cac7SKirk McKusick 		TAILQ_FOREACH(qent, &priv->frags, f_qent) {
17326f16db81SArchie Cobbs 			if (MP_RECV_SEQ_DIFF(priv, qent->seq, priv->mseq) >= 0)
1733a9b3dca5SArchie Cobbs 				break;
17345f90cac7SKirk McKusick 			qnext = TAILQ_NEXT(qent, f_qent);
17355f90cac7SKirk McKusick 			KASSERT(qnext != NULL,
17366e551fb6SDavid E. O'Brien 			    ("%s: last frag < MSEQ?", __func__));
17376f16db81SArchie Cobbs 			if (qnext->seq != MP_NEXT_RECV_SEQ(priv, qent->seq)
1738a9b3dca5SArchie Cobbs 			    || qent->last || qnext->first) {
1739a9b3dca5SArchie Cobbs 				dead = 1;
1740a9b3dca5SArchie Cobbs 				break;
1741a9b3dca5SArchie Cobbs 			}
1742a9b3dca5SArchie Cobbs 		}
1743a9b3dca5SArchie Cobbs 		if (!dead)
1744a9b3dca5SArchie Cobbs 			break;
1745a9b3dca5SArchie Cobbs 
1746a9b3dca5SArchie Cobbs 		/* Remove fragment and all others in the same packet */
17475f90cac7SKirk McKusick 		while ((qent = TAILQ_FIRST(&priv->frags)) != qnext) {
17485f90cac7SKirk McKusick 			KASSERT(!TAILQ_EMPTY(&priv->frags),
17496e551fb6SDavid E. O'Brien 			    ("%s: empty q", __func__));
1750a9b3dca5SArchie Cobbs 			priv->bundleStats.dropFragments++;
17515f90cac7SKirk McKusick 			TAILQ_REMOVE(&priv->frags, qent, f_qent);
1752069154d5SJulian Elischer 			NG_FREE_M(qent->data);
175393caf2e2SAlexander Motin 			TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent);
1754a9b3dca5SArchie Cobbs 			removed = 1;
1755a9b3dca5SArchie Cobbs 		}
1756a9b3dca5SArchie Cobbs 	}
1757a9b3dca5SArchie Cobbs 	return (removed);
17583949bee8SArchie Cobbs }
17593949bee8SArchie Cobbs 
1760a9b3dca5SArchie Cobbs /*
17618e8f114eSAlexander Motin  * Drop fragments on queue overflow.
17628e8f114eSAlexander Motin  * Returns 1 if fragments were removed, zero otherwise.
17638e8f114eSAlexander Motin  */
17648e8f114eSAlexander Motin static int
17658e8f114eSAlexander Motin ng_ppp_frag_drop(node_p node)
17668e8f114eSAlexander Motin {
17678e8f114eSAlexander Motin 	const priv_p priv = NG_NODE_PRIVATE(node);
17688e8f114eSAlexander Motin 
17698e8f114eSAlexander Motin 	/* Check queue length */
177093caf2e2SAlexander Motin 	if (TAILQ_EMPTY(&priv->fragsfree)) {
17718e8f114eSAlexander Motin 		struct ng_ppp_frag *qent;
17728e8f114eSAlexander Motin 
17738e8f114eSAlexander Motin 		/* Get oldest fragment */
17748e8f114eSAlexander Motin 		KASSERT(!TAILQ_EMPTY(&priv->frags),
17758e8f114eSAlexander Motin 		    ("%s: empty q", __func__));
17768e8f114eSAlexander Motin 		qent = TAILQ_FIRST(&priv->frags);
17778e8f114eSAlexander Motin 
17788e8f114eSAlexander Motin 		/* Bump MSEQ if necessary */
17798e8f114eSAlexander Motin 		ng_ppp_bump_mseq(node, qent->seq);
17808e8f114eSAlexander Motin 
17818e8f114eSAlexander Motin 		/* Drop it */
17828e8f114eSAlexander Motin 		priv->bundleStats.dropFragments++;
17838e8f114eSAlexander Motin 		TAILQ_REMOVE(&priv->frags, qent, f_qent);
17848e8f114eSAlexander Motin 		NG_FREE_M(qent->data);
178593caf2e2SAlexander Motin 		TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent);
17868e8f114eSAlexander Motin 
17878e8f114eSAlexander Motin 		return (1);
17888e8f114eSAlexander Motin 	}
17898e8f114eSAlexander Motin 	return (0);
17908e8f114eSAlexander Motin }
17918e8f114eSAlexander Motin 
17928e8f114eSAlexander Motin /*
1793a9b3dca5SArchie Cobbs  * Run the queue, restoring the queue invariants
1794a9b3dca5SArchie Cobbs  */
1795a9b3dca5SArchie Cobbs static int
17965716b3f1SAlexander Motin ng_ppp_frag_process(node_p node, item_p oitem)
1797a9b3dca5SArchie Cobbs {
179830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1799a9b3dca5SArchie Cobbs 	struct mbuf *m;
1800069154d5SJulian Elischer 	item_p item;
1801164b576eSGleb Smirnoff 	uint16_t proto;
1802a9b3dca5SArchie Cobbs 
18038e8f114eSAlexander Motin 	do {
1804a9b3dca5SArchie Cobbs 		/* Deliver any deliverable packets */
1805a9b3dca5SArchie Cobbs 		while (ng_ppp_check_packet(node)) {
18063ca24c28SJulian Elischer 			ng_ppp_get_packet(node, &m);
1807164b576eSGleb Smirnoff 			if ((m = ng_ppp_cutproto(m, &proto)) == NULL)
1808164b576eSGleb Smirnoff 				continue;
1809164b576eSGleb Smirnoff 			if (!PROT_VALID(proto)) {
1810164b576eSGleb Smirnoff 				priv->bundleStats.badProtos++;
1811164b576eSGleb Smirnoff 				NG_FREE_M(m);
1812164b576eSGleb Smirnoff 				continue;
1813164b576eSGleb Smirnoff 			}
18145716b3f1SAlexander Motin 			if (oitem) { /* If original item present - reuse it. */
18155716b3f1SAlexander Motin 				item = oitem;
18165716b3f1SAlexander Motin 				oitem = NULL;
18175716b3f1SAlexander Motin 				NGI_M(item) = m;
18185716b3f1SAlexander Motin 			} else {
18195716b3f1SAlexander Motin 				item = ng_package_data(m, NG_NOFLAGS);
18205716b3f1SAlexander Motin 			}
18215716b3f1SAlexander Motin 			if (item != NULL) {
1822e89c1507SAlexander Motin 				/* Stats */
1823e89c1507SAlexander Motin 				priv->bundleStats.recvFrames++;
1824e89c1507SAlexander Motin 				priv->bundleStats.recvOctets +=
1825e89c1507SAlexander Motin 				    NGI_M(item)->m_pkthdr.len;
1826e89c1507SAlexander Motin 
1827e89c1507SAlexander Motin 				/* Drop mutex for the sending time.
1828e89c1507SAlexander Motin 				 * Priv may change, but we are ready!
1829e89c1507SAlexander Motin 				 */
1830e89c1507SAlexander Motin 				mtx_unlock(&priv->rmtx);
1831164b576eSGleb Smirnoff 				ng_ppp_crypt_recv(node, item, proto,
1832164b576eSGleb Smirnoff 					NG_PPP_BUNDLE_LINKNUM);
1833e89c1507SAlexander Motin 				mtx_lock(&priv->rmtx);
1834e89c1507SAlexander Motin 			}
1835a9b3dca5SArchie Cobbs 		}
1836a9b3dca5SArchie Cobbs 	  /* Delete dead fragments and try again */
18378e8f114eSAlexander Motin 	} while (ng_ppp_frag_trim(node) || ng_ppp_frag_drop(node));
1838a9b3dca5SArchie Cobbs 
18395716b3f1SAlexander Motin 	/* If we haven't reused original item - free it. */
18405716b3f1SAlexander Motin 	if (oitem) NG_FREE_ITEM(oitem);
18415716b3f1SAlexander Motin 
1842a9b3dca5SArchie Cobbs 	/* Done */
1843a9b3dca5SArchie Cobbs 	return (0);
1844a9b3dca5SArchie Cobbs }
1845a9b3dca5SArchie Cobbs 
1846a9b3dca5SArchie Cobbs /*
1847a9b3dca5SArchie Cobbs  * Check for 'stale' completed packets that need to be delivered
1848a9b3dca5SArchie Cobbs  *
1849a9b3dca5SArchie Cobbs  * If a link goes down or has a temporary failure, MSEQ can get
1850a9b3dca5SArchie Cobbs  * "stuck", because no new incoming fragments appear on that link.
1851a9b3dca5SArchie Cobbs  * This can cause completed packets to never get delivered if
1852a9b3dca5SArchie Cobbs  * their sequence numbers are all > MSEQ + 1.
1853a9b3dca5SArchie Cobbs  *
1854a9b3dca5SArchie Cobbs  * This routine checks how long all of the completed packets have
1855a9b3dca5SArchie Cobbs  * been sitting in the queue, and if too long, removes fragments
1856a9b3dca5SArchie Cobbs  * from the queue and increments MSEQ to allow them to be delivered.
1857a9b3dca5SArchie Cobbs  */
1858a9b3dca5SArchie Cobbs static void
1859a9b3dca5SArchie Cobbs ng_ppp_frag_checkstale(node_p node)
1860a9b3dca5SArchie Cobbs {
186130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1862a9b3dca5SArchie Cobbs 	struct ng_ppp_frag *qent, *beg, *end;
1863a9b3dca5SArchie Cobbs 	struct timeval now, age;
1864a9b3dca5SArchie Cobbs 	struct mbuf *m;
18658e8f114eSAlexander Motin 	int seq;
1866069154d5SJulian Elischer 	item_p item;
18672b2a8188SArchie Cobbs 	int endseq;
1868164b576eSGleb Smirnoff 	uint16_t proto;
1869a9b3dca5SArchie Cobbs 
1870a9b3dca5SArchie Cobbs 	now.tv_sec = 0;			/* uninitialized state */
1871a9b3dca5SArchie Cobbs 	while (1) {
1872a9b3dca5SArchie Cobbs 
1873a9b3dca5SArchie Cobbs 		/* If queue is empty, we're done */
18745f90cac7SKirk McKusick 		if (TAILQ_EMPTY(&priv->frags))
1875a9b3dca5SArchie Cobbs 			break;
1876a9b3dca5SArchie Cobbs 
1877a9b3dca5SArchie Cobbs 		/* Find the first complete packet in the queue */
1878a9b3dca5SArchie Cobbs 		beg = end = NULL;
18795f90cac7SKirk McKusick 		seq = TAILQ_FIRST(&priv->frags)->seq;
18805f90cac7SKirk McKusick 		TAILQ_FOREACH(qent, &priv->frags, f_qent) {
1881a9b3dca5SArchie Cobbs 			if (qent->first)
1882a9b3dca5SArchie Cobbs 				beg = qent;
1883a9b3dca5SArchie Cobbs 			else if (qent->seq != seq)
1884a9b3dca5SArchie Cobbs 				beg = NULL;
1885a9b3dca5SArchie Cobbs 			if (beg != NULL && qent->last) {
1886a9b3dca5SArchie Cobbs 				end = qent;
1887a9b3dca5SArchie Cobbs 				break;
1888a9b3dca5SArchie Cobbs 			}
18896f16db81SArchie Cobbs 			seq = MP_NEXT_RECV_SEQ(priv, seq);
1890a9b3dca5SArchie Cobbs 		}
1891a9b3dca5SArchie Cobbs 
1892a9b3dca5SArchie Cobbs 		/* If none found, exit */
1893a9b3dca5SArchie Cobbs 		if (end == NULL)
1894a9b3dca5SArchie Cobbs 			break;
1895a9b3dca5SArchie Cobbs 
1896a9b3dca5SArchie Cobbs 		/* Get current time (we assume we've been up for >= 1 second) */
1897a9b3dca5SArchie Cobbs 		if (now.tv_sec == 0)
1898a9b3dca5SArchie Cobbs 			getmicrouptime(&now);
1899a9b3dca5SArchie Cobbs 
1900a9b3dca5SArchie Cobbs 		/* Check if packet has been queued too long */
1901a9b3dca5SArchie Cobbs 		age = now;
1902a9b3dca5SArchie Cobbs 		timevalsub(&age, &beg->timestamp);
1903a9b3dca5SArchie Cobbs 		if (timevalcmp(&age, &ng_ppp_max_staleness, < ))
1904a9b3dca5SArchie Cobbs 			break;
1905a9b3dca5SArchie Cobbs 
1906a9b3dca5SArchie Cobbs 		/* Throw away junk fragments in front of the completed packet */
19075f90cac7SKirk McKusick 		while ((qent = TAILQ_FIRST(&priv->frags)) != beg) {
19085f90cac7SKirk McKusick 			KASSERT(!TAILQ_EMPTY(&priv->frags),
19096e551fb6SDavid E. O'Brien 			    ("%s: empty q", __func__));
1910a9b3dca5SArchie Cobbs 			priv->bundleStats.dropFragments++;
19115f90cac7SKirk McKusick 			TAILQ_REMOVE(&priv->frags, qent, f_qent);
1912069154d5SJulian Elischer 			NG_FREE_M(qent->data);
191393caf2e2SAlexander Motin 			TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent);
1914a9b3dca5SArchie Cobbs 		}
1915a9b3dca5SArchie Cobbs 
1916a9b3dca5SArchie Cobbs 		/* Extract completed packet */
19172b2a8188SArchie Cobbs 		endseq = end->seq;
19183ca24c28SJulian Elischer 		ng_ppp_get_packet(node, &m);
1919a9b3dca5SArchie Cobbs 
1920164b576eSGleb Smirnoff 		if ((m = ng_ppp_cutproto(m, &proto)) == NULL)
1921164b576eSGleb Smirnoff 			continue;
1922164b576eSGleb Smirnoff 		if (!PROT_VALID(proto)) {
1923164b576eSGleb Smirnoff 			priv->bundleStats.badProtos++;
1924164b576eSGleb Smirnoff 			NG_FREE_M(m);
1925164b576eSGleb Smirnoff 			continue;
1926164b576eSGleb Smirnoff 		}
1927164b576eSGleb Smirnoff 
1928a9b3dca5SArchie Cobbs 		/* Deliver packet */
1929e89c1507SAlexander Motin 		if ((item = ng_package_data(m, NG_NOFLAGS)) != NULL) {
1930e89c1507SAlexander Motin 			/* Stats */
1931e89c1507SAlexander Motin 			priv->bundleStats.recvFrames++;
1932e89c1507SAlexander Motin 			priv->bundleStats.recvOctets += NGI_M(item)->m_pkthdr.len;
1933e89c1507SAlexander Motin 
1934164b576eSGleb Smirnoff 			ng_ppp_crypt_recv(node, item, proto,
1935164b576eSGleb Smirnoff 				NG_PPP_BUNDLE_LINKNUM);
1936a9b3dca5SArchie Cobbs 		}
1937a9b3dca5SArchie Cobbs 	}
1938e89c1507SAlexander Motin }
1939a9b3dca5SArchie Cobbs 
1940a9b3dca5SArchie Cobbs /*
1941a9b3dca5SArchie Cobbs  * Periodically call ng_ppp_frag_checkstale()
1942a9b3dca5SArchie Cobbs  */
1943a9b3dca5SArchie Cobbs static void
19449d952bd3SGleb Smirnoff ng_ppp_frag_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1945a9b3dca5SArchie Cobbs {
19469d952bd3SGleb Smirnoff 	/* XXX: is this needed? */
19479d952bd3SGleb Smirnoff 	if (NG_NODE_NOT_VALID(node))
1948a9b3dca5SArchie Cobbs 		return;
1949a9b3dca5SArchie Cobbs 
1950a9b3dca5SArchie Cobbs 	/* Scan the fragment queue */
1951a9b3dca5SArchie Cobbs 	ng_ppp_frag_checkstale(node);
19529d952bd3SGleb Smirnoff 
19539d952bd3SGleb Smirnoff 	/* Start timer again */
19549d952bd3SGleb Smirnoff 	ng_ppp_start_frag_timer(node);
19553949bee8SArchie Cobbs }
19563949bee8SArchie Cobbs 
19573949bee8SArchie Cobbs /*
19583949bee8SArchie Cobbs  * Deliver a frame out on the bundle, i.e., figure out how to fragment
19593949bee8SArchie Cobbs  * the frame across the individual PPP links and do so.
19603949bee8SArchie Cobbs  */
19613949bee8SArchie Cobbs static int
1962164b576eSGleb Smirnoff ng_ppp_mp_xmit(node_p node, item_p item, uint16_t proto)
19633949bee8SArchie Cobbs {
196430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1965931c828aSArchie Cobbs 	const int hdr_len = priv->conf.xmitShortSeq ? 2 : 4;
19663949bee8SArchie Cobbs 	int distrib[NG_PPP_MAX_LINKS];
19673949bee8SArchie Cobbs 	int firstFragment;
19683949bee8SArchie Cobbs 	int activeLinkNum;
1969164b576eSGleb Smirnoff 	struct mbuf *m;
19709c10d113SAlexander Motin 	int	plen;
1971e89c1507SAlexander Motin 	int	frags;
1972e89c1507SAlexander Motin 	int32_t	seq;
19733949bee8SArchie Cobbs 
19743949bee8SArchie Cobbs 	/* At least one link must be active */
19753949bee8SArchie Cobbs 	if (priv->numActiveLinks == 0) {
1976164b576eSGleb Smirnoff 		NG_FREE_ITEM(item);
19773949bee8SArchie Cobbs 		return (ENETDOWN);
19783949bee8SArchie Cobbs 	}
19793949bee8SArchie Cobbs 
1980e89c1507SAlexander Motin 	/* Save length for later stats. */
19819c10d113SAlexander Motin 	plen = NGI_M(item)->m_pkthdr.len;
1982164b576eSGleb Smirnoff 
1983e89c1507SAlexander Motin 	if (!priv->conf.enableMultilink) {
1984164b576eSGleb Smirnoff 		return (ng_ppp_link_xmit(node, item, proto,
19859c10d113SAlexander Motin 		    priv->activeLinks[0], plen));
1986e89c1507SAlexander Motin 	}
1987164b576eSGleb Smirnoff 
1988164b576eSGleb Smirnoff 	/* Extract mbuf. */
1989164b576eSGleb Smirnoff 	NGI_GET_M(item, m);
1990164b576eSGleb Smirnoff 
1991164b576eSGleb Smirnoff 	/* Prepend protocol number, possibly compressed. */
1992f9ec6990SAlexander Motin 	if ((m = ng_ppp_addproto(m, proto, 1)) == NULL) {
1993f9ec6990SAlexander Motin 		NG_FREE_ITEM(item);
1994164b576eSGleb Smirnoff 		return (ENOBUFS);
1995f9ec6990SAlexander Motin 	}
1996164b576eSGleb Smirnoff 
1997ccffcb51SAlexander Motin 	/* Clear distribution plan */
19983949bee8SArchie Cobbs 	bzero(&distrib, priv->numActiveLinks * sizeof(distrib[0]));
1999ccffcb51SAlexander Motin 
2000e89c1507SAlexander Motin 	mtx_lock(&priv->xmtx);
2001e89c1507SAlexander Motin 
2002ccffcb51SAlexander Motin 	/* Round-robin strategy */
2003ccffcb51SAlexander Motin 	if (priv->conf.enableRoundRobin) {
2004ccffcb51SAlexander Motin 		activeLinkNum = priv->lastLink++ % priv->numActiveLinks;
20053949bee8SArchie Cobbs 		distrib[activeLinkNum] = m->m_pkthdr.len;
20063949bee8SArchie Cobbs 		goto deliver;
20073949bee8SArchie Cobbs 	}
20083949bee8SArchie Cobbs 
20093949bee8SArchie Cobbs 	/* Strategy when all links are equivalent (optimize the common case) */
20103949bee8SArchie Cobbs 	if (priv->allLinksEqual) {
2011ccffcb51SAlexander Motin 		int	numFrags, fraction, remain;
2012ccffcb51SAlexander Motin 		int	i;
20133949bee8SArchie Cobbs 
2014ccffcb51SAlexander Motin 		/* Calculate optimal fragment count */
2015ccffcb51SAlexander Motin 		numFrags = priv->numActiveLinks;
2016ccffcb51SAlexander Motin 		if (numFrags > m->m_pkthdr.len / MP_MIN_FRAG_LEN)
2017ccffcb51SAlexander Motin 		    numFrags = m->m_pkthdr.len / MP_MIN_FRAG_LEN;
2018ccffcb51SAlexander Motin 		if (numFrags == 0)
2019ccffcb51SAlexander Motin 		    numFrags = 1;
2020ccffcb51SAlexander Motin 
2021ccffcb51SAlexander Motin 		fraction = m->m_pkthdr.len / numFrags;
2022ccffcb51SAlexander Motin 		remain = m->m_pkthdr.len - (fraction * numFrags);
2023ccffcb51SAlexander Motin 
2024ccffcb51SAlexander Motin 		/* Assign distribution */
2025ccffcb51SAlexander Motin 		for (i = 0; i < numFrags; i++) {
20263949bee8SArchie Cobbs 			distrib[priv->lastLink++ % priv->numActiveLinks]
2027ccffcb51SAlexander Motin 			    = fraction + (((remain--) > 0)?1:0);
20283949bee8SArchie Cobbs 		}
20293949bee8SArchie Cobbs 		goto deliver;
20303949bee8SArchie Cobbs 	}
20313949bee8SArchie Cobbs 
20323949bee8SArchie Cobbs 	/* Strategy when all links are not equivalent */
20333949bee8SArchie Cobbs 	ng_ppp_mp_strategy(node, m->m_pkthdr.len, distrib);
20343949bee8SArchie Cobbs 
20353949bee8SArchie Cobbs deliver:
2036e89c1507SAlexander Motin 	/* Estimate fragments count */
2037e89c1507SAlexander Motin 	frags = 0;
2038e89c1507SAlexander Motin 	for (activeLinkNum = priv->numActiveLinks - 1;
2039e89c1507SAlexander Motin 	    activeLinkNum >= 0; activeLinkNum--) {
2040e89c1507SAlexander Motin 		const uint16_t linkNum = priv->activeLinks[activeLinkNum];
2041e89c1507SAlexander Motin 		struct ng_ppp_link *const link = &priv->links[linkNum];
2042e89c1507SAlexander Motin 
2043e89c1507SAlexander Motin 		frags += (distrib[activeLinkNum] + link->conf.mru - hdr_len - 1) /
2044e89c1507SAlexander Motin 		    (link->conf.mru - hdr_len);
2045e89c1507SAlexander Motin 	}
2046e89c1507SAlexander Motin 
2047e89c1507SAlexander Motin 	/* Get out initial sequence number */
2048e89c1507SAlexander Motin 	seq = priv->xseq;
2049e89c1507SAlexander Motin 
2050e89c1507SAlexander Motin 	/* Update next sequence number */
2051e89c1507SAlexander Motin 	if (priv->conf.xmitShortSeq) {
2052e89c1507SAlexander Motin 	    priv->xseq = (seq + frags) & MP_SHORT_SEQ_MASK;
2053e89c1507SAlexander Motin 	} else {
2054e89c1507SAlexander Motin 	    priv->xseq = (seq + frags) & MP_LONG_SEQ_MASK;
2055e89c1507SAlexander Motin 	}
2056e89c1507SAlexander Motin 
2057e89c1507SAlexander Motin 	mtx_unlock(&priv->xmtx);
2058e89c1507SAlexander Motin 
20593949bee8SArchie Cobbs 	/* Send alloted portions of frame out on the link(s) */
20603949bee8SArchie Cobbs 	for (firstFragment = 1, activeLinkNum = priv->numActiveLinks - 1;
20613949bee8SArchie Cobbs 	    activeLinkNum >= 0; activeLinkNum--) {
2062164b576eSGleb Smirnoff 		const uint16_t linkNum = priv->activeLinks[activeLinkNum];
2063a9b3dca5SArchie Cobbs 		struct ng_ppp_link *const link = &priv->links[linkNum];
20643949bee8SArchie Cobbs 
20653949bee8SArchie Cobbs 		/* Deliver fragment(s) out the next link */
20663949bee8SArchie Cobbs 		for ( ; distrib[activeLinkNum] > 0; firstFragment = 0) {
20673949bee8SArchie Cobbs 			int len, lastFragment, error;
20683949bee8SArchie Cobbs 			struct mbuf *m2;
20693949bee8SArchie Cobbs 
20703949bee8SArchie Cobbs 			/* Calculate fragment length; don't exceed link MTU */
20713949bee8SArchie Cobbs 			len = distrib[activeLinkNum];
2072931c828aSArchie Cobbs 			if (len > link->conf.mru - hdr_len)
2073931c828aSArchie Cobbs 				len = link->conf.mru - hdr_len;
20743949bee8SArchie Cobbs 			distrib[activeLinkNum] -= len;
20753949bee8SArchie Cobbs 			lastFragment = (len == m->m_pkthdr.len);
20763949bee8SArchie Cobbs 
20773949bee8SArchie Cobbs 			/* Split off next fragment as "m2" */
20783949bee8SArchie Cobbs 			m2 = m;
20793949bee8SArchie Cobbs 			if (!lastFragment) {
2080a163d034SWarner Losh 				struct mbuf *n = m_split(m, len, M_DONTWAIT);
20813949bee8SArchie Cobbs 
20823949bee8SArchie Cobbs 				if (n == NULL) {
2083069154d5SJulian Elischer 					NG_FREE_M(m);
2084f9ec6990SAlexander Motin 					if (firstFragment)
2085f9ec6990SAlexander Motin 						NG_FREE_ITEM(item);
20863949bee8SArchie Cobbs 					return (ENOMEM);
20873949bee8SArchie Cobbs 				}
2088e07c5170SAlexander Motin 				m_tag_copy_chain(n, m, M_DONTWAIT);
20893949bee8SArchie Cobbs 				m = n;
20903949bee8SArchie Cobbs 			}
20913949bee8SArchie Cobbs 
20923949bee8SArchie Cobbs 			/* Prepend MP header */
20933949bee8SArchie Cobbs 			if (priv->conf.xmitShortSeq) {
2094164b576eSGleb Smirnoff 				uint16_t shdr;
20953949bee8SArchie Cobbs 
2096e89c1507SAlexander Motin 				shdr = seq;
2097e89c1507SAlexander Motin 				seq = (seq + 1) & MP_SHORT_SEQ_MASK;
20983949bee8SArchie Cobbs 				if (firstFragment)
20993949bee8SArchie Cobbs 					shdr |= MP_SHORT_FIRST_FLAG;
21003949bee8SArchie Cobbs 				if (lastFragment)
21013949bee8SArchie Cobbs 					shdr |= MP_SHORT_LAST_FLAG;
2102d690a6e7SArchie Cobbs 				shdr = htons(shdr);
2103d690a6e7SArchie Cobbs 				m2 = ng_ppp_prepend(m2, &shdr, 2);
21043949bee8SArchie Cobbs 			} else {
2105164b576eSGleb Smirnoff 				uint32_t lhdr;
21063949bee8SArchie Cobbs 
2107e89c1507SAlexander Motin 				lhdr = seq;
2108e89c1507SAlexander Motin 				seq = (seq + 1) & MP_LONG_SEQ_MASK;
21093949bee8SArchie Cobbs 				if (firstFragment)
21103949bee8SArchie Cobbs 					lhdr |= MP_LONG_FIRST_FLAG;
21113949bee8SArchie Cobbs 				if (lastFragment)
21123949bee8SArchie Cobbs 					lhdr |= MP_LONG_LAST_FLAG;
2113d690a6e7SArchie Cobbs 				lhdr = htonl(lhdr);
2114d690a6e7SArchie Cobbs 				m2 = ng_ppp_prepend(m2, &lhdr, 4);
21153949bee8SArchie Cobbs 			}
21163949bee8SArchie Cobbs 			if (m2 == NULL) {
21173949bee8SArchie Cobbs 				if (!lastFragment)
21183949bee8SArchie Cobbs 					m_freem(m);
2119f9ec6990SAlexander Motin 				if (firstFragment)
2120f9ec6990SAlexander Motin 					NG_FREE_ITEM(item);
21213949bee8SArchie Cobbs 				return (ENOBUFS);
21223949bee8SArchie Cobbs 			}
21233949bee8SArchie Cobbs 
21243949bee8SArchie Cobbs 			/* Send fragment */
2125f9ec6990SAlexander Motin 			if (firstFragment) {
2126f9ec6990SAlexander Motin 				NGI_M(item) = m2; /* Reuse original item. */
2127f9ec6990SAlexander Motin 			} else {
2128f9ec6990SAlexander Motin 				item = ng_package_data(m2, NG_NOFLAGS);
2129f9ec6990SAlexander Motin 			}
2130f9ec6990SAlexander Motin 			if (item != NULL) {
2131164b576eSGleb Smirnoff 				error = ng_ppp_link_xmit(node, item, PROT_MP,
21329c10d113SAlexander Motin 					    linkNum, (firstFragment?plen:0));
21333949bee8SArchie Cobbs 				if (error != 0) {
21343ca24c28SJulian Elischer 					if (!lastFragment)
2135069154d5SJulian Elischer 						NG_FREE_M(m);
21363949bee8SArchie Cobbs 					return (error);
21373949bee8SArchie Cobbs 				}
21383949bee8SArchie Cobbs 			}
21393949bee8SArchie Cobbs 		}
2140d0784b47SGleb Smirnoff 	}
21413949bee8SArchie Cobbs 
21423949bee8SArchie Cobbs 	/* Done */
21433949bee8SArchie Cobbs 	return (0);
21443949bee8SArchie Cobbs }
21453949bee8SArchie Cobbs 
21463949bee8SArchie Cobbs /*
21473949bee8SArchie Cobbs  * Computing the optimal fragmentation
21483949bee8SArchie Cobbs  * -----------------------------------
21493949bee8SArchie Cobbs  *
21503949bee8SArchie Cobbs  * This routine tries to compute the optimal fragmentation pattern based
21513949bee8SArchie Cobbs  * on each link's latency, bandwidth, and calculated additional latency.
21523949bee8SArchie Cobbs  * The latter quantity is the additional latency caused by previously
21533949bee8SArchie Cobbs  * written data that has not been transmitted yet.
21543949bee8SArchie Cobbs  *
21553949bee8SArchie Cobbs  * This algorithm is only useful when not all of the links have the
21563949bee8SArchie Cobbs  * same latency and bandwidth values.
21573949bee8SArchie Cobbs  *
21583949bee8SArchie Cobbs  * The essential idea is to make the last bit of each fragment of the
21593949bee8SArchie Cobbs  * frame arrive at the opposite end at the exact same time. This greedy
21603949bee8SArchie Cobbs  * algorithm is optimal, in that no other scheduling could result in any
21613949bee8SArchie Cobbs  * packet arriving any sooner unless packets are delivered out of order.
21623949bee8SArchie Cobbs  *
21633949bee8SArchie Cobbs  * Suppose link i has bandwidth b_i (in tens of bytes per milisecond) and
21643949bee8SArchie Cobbs  * latency l_i (in miliseconds). Consider the function function f_i(t)
21653949bee8SArchie Cobbs  * which is equal to the number of bytes that will have arrived at
21663949bee8SArchie Cobbs  * the peer after t miliseconds if we start writing continuously at
21673949bee8SArchie Cobbs  * time t = 0. Then f_i(t) = b_i * (t - l_i) = ((b_i * t) - (l_i * b_i).
21683949bee8SArchie Cobbs  * That is, f_i(t) is a line with slope b_i and y-intersect -(l_i * b_i).
21693949bee8SArchie Cobbs  * Note that the y-intersect is always <= zero because latency can't be
21703949bee8SArchie Cobbs  * negative.  Note also that really the function is f_i(t) except when
21713949bee8SArchie Cobbs  * f_i(t) is negative, in which case the function is zero.  To take
21723949bee8SArchie Cobbs  * care of this, let Q_i(t) = { if (f_i(t) > 0) return 1; else return 0; }.
21733949bee8SArchie Cobbs  * So the actual number of bytes that will have arrived at the peer after
21743949bee8SArchie Cobbs  * t miliseconds is f_i(t) * Q_i(t).
21753949bee8SArchie Cobbs  *
21763949bee8SArchie Cobbs  * At any given time, each link has some additional latency a_i >= 0
21773949bee8SArchie Cobbs  * due to previously written fragment(s) which are still in the queue.
21783949bee8SArchie Cobbs  * This value is easily computed from the time since last transmission,
21793949bee8SArchie Cobbs  * the previous latency value, the number of bytes written, and the
21803949bee8SArchie Cobbs  * link's bandwidth.
21813949bee8SArchie Cobbs  *
21823949bee8SArchie Cobbs  * Assume that l_i includes any a_i already, and that the links are
21833949bee8SArchie Cobbs  * sorted by latency, so that l_i <= l_{i+1}.
21843949bee8SArchie Cobbs  *
21853949bee8SArchie Cobbs  * Let N be the total number of bytes in the current frame we are sending.
21863949bee8SArchie Cobbs  *
21873949bee8SArchie Cobbs  * Suppose we were to start writing bytes at time t = 0 on all links
21883949bee8SArchie Cobbs  * simultaneously, which is the most we can possibly do.  Then let
21893949bee8SArchie Cobbs  * F(t) be equal to the total number of bytes received by the peer
21903949bee8SArchie Cobbs  * after t miliseconds. Then F(t) = Sum_i (f_i(t) * Q_i(t)).
21913949bee8SArchie Cobbs  *
21923949bee8SArchie Cobbs  * Our goal is simply this: fragment the frame across the links such
21933949bee8SArchie Cobbs  * that the peer is able to reconstruct the completed frame as soon as
21943949bee8SArchie Cobbs  * possible, i.e., at the least possible value of t. Call this value t_0.
21953949bee8SArchie Cobbs  *
21963949bee8SArchie Cobbs  * Then it follows that F(t_0) = N. Our strategy is first to find the value
21973949bee8SArchie Cobbs  * of t_0, and then deduce how many bytes to write to each link.
21983949bee8SArchie Cobbs  *
21993949bee8SArchie Cobbs  * Rewriting F(t_0):
22003949bee8SArchie Cobbs  *
22013949bee8SArchie Cobbs  *   t_0 = ( N + Sum_i ( l_i * b_i * Q_i(t_0) ) ) / Sum_i ( b_i * Q_i(t_0) )
22023949bee8SArchie Cobbs  *
22033949bee8SArchie Cobbs  * Now, we note that Q_i(t) is constant for l_i <= t <= l_{i+1}. t_0 will
22043949bee8SArchie Cobbs  * lie in one of these ranges.  To find it, we just need to find the i such
22053949bee8SArchie Cobbs  * that F(l_i) <= N <= F(l_{i+1}).  Then we compute all the constant values
22063949bee8SArchie Cobbs  * for Q_i() in this range, plug in the remaining values, solving for t_0.
22073949bee8SArchie Cobbs  *
22083949bee8SArchie Cobbs  * Once t_0 is known, then the number of bytes to send on link i is
22093949bee8SArchie Cobbs  * just f_i(t_0) * Q_i(t_0).
22103949bee8SArchie Cobbs  *
22113949bee8SArchie Cobbs  * In other words, we start allocating bytes to the links one at a time.
22123949bee8SArchie Cobbs  * We keep adding links until the frame is completely sent.  Some links
22133949bee8SArchie Cobbs  * may not get any bytes because their latency is too high.
22143949bee8SArchie Cobbs  *
22153949bee8SArchie Cobbs  * Is all this work really worth the trouble?  Depends on the situation.
22163949bee8SArchie Cobbs  * The bigger the ratio of computer speed to link speed, and the more
22173949bee8SArchie Cobbs  * important total bundle latency is (e.g., for interactive response time),
22183949bee8SArchie Cobbs  * the more it's worth it.  There is however the cost of calling this
22193949bee8SArchie Cobbs  * function for every frame.  The running time is O(n^2) where n is the
22203949bee8SArchie Cobbs  * number of links that receive a non-zero number of bytes.
22213949bee8SArchie Cobbs  *
22223949bee8SArchie Cobbs  * Since latency is measured in miliseconds, the "resolution" of this
22233949bee8SArchie Cobbs  * algorithm is one milisecond.
22243949bee8SArchie Cobbs  *
22253949bee8SArchie Cobbs  * To avoid this algorithm altogether, configure all links to have the
22263949bee8SArchie Cobbs  * same latency and bandwidth.
22273949bee8SArchie Cobbs  */
22283949bee8SArchie Cobbs static void
22293949bee8SArchie Cobbs ng_ppp_mp_strategy(node_p node, int len, int *distrib)
22303949bee8SArchie Cobbs {
223130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
22323949bee8SArchie Cobbs 	int latency[NG_PPP_MAX_LINKS];
22333949bee8SArchie Cobbs 	int sortByLatency[NG_PPP_MAX_LINKS];
2234a9b3dca5SArchie Cobbs 	int activeLinkNum;
22353949bee8SArchie Cobbs 	int t0, total, topSum, botSum;
22363949bee8SArchie Cobbs 	struct timeval now;
22373949bee8SArchie Cobbs 	int i, numFragments;
22383949bee8SArchie Cobbs 
22393949bee8SArchie Cobbs 	/* If only one link, this gets real easy */
22403949bee8SArchie Cobbs 	if (priv->numActiveLinks == 1) {
22413949bee8SArchie Cobbs 		distrib[0] = len;
22423949bee8SArchie Cobbs 		return;
22433949bee8SArchie Cobbs 	}
22443949bee8SArchie Cobbs 
22453949bee8SArchie Cobbs 	/* Get current time */
2246a9b3dca5SArchie Cobbs 	getmicrouptime(&now);
22473949bee8SArchie Cobbs 
22483949bee8SArchie Cobbs 	/* Compute latencies for each link at this point in time */
22493949bee8SArchie Cobbs 	for (activeLinkNum = 0;
22503949bee8SArchie Cobbs 	    activeLinkNum < priv->numActiveLinks; activeLinkNum++) {
2251a9b3dca5SArchie Cobbs 		struct ng_ppp_link *alink;
22523949bee8SArchie Cobbs 		struct timeval diff;
22533949bee8SArchie Cobbs 		int xmitBytes;
22543949bee8SArchie Cobbs 
22553949bee8SArchie Cobbs 		/* Start with base latency value */
2256a9b3dca5SArchie Cobbs 		alink = &priv->links[priv->activeLinks[activeLinkNum]];
225749728ffcSBjoern A. Zeeb 		latency[activeLinkNum] = alink->latency;
22583949bee8SArchie Cobbs 		sortByLatency[activeLinkNum] = activeLinkNum;	/* see below */
22593949bee8SArchie Cobbs 
22603949bee8SArchie Cobbs 		/* Any additional latency? */
2261a9b3dca5SArchie Cobbs 		if (alink->bytesInQueue == 0)
22623949bee8SArchie Cobbs 			continue;
22633949bee8SArchie Cobbs 
22643949bee8SArchie Cobbs 		/* Compute time delta since last write */
22653949bee8SArchie Cobbs 		diff = now;
2266a9b3dca5SArchie Cobbs 		timevalsub(&diff, &alink->lastWrite);
2267fd58342cSAlexander Motin 
2268fd58342cSAlexander Motin 		/* alink->bytesInQueue will be changed, mark change time. */
2269fd58342cSAlexander Motin 		alink->lastWrite = now;
2270fd58342cSAlexander Motin 
22713949bee8SArchie Cobbs 		if (now.tv_sec < 0 || diff.tv_sec >= 10) {	/* sanity */
2272a9b3dca5SArchie Cobbs 			alink->bytesInQueue = 0;
22733949bee8SArchie Cobbs 			continue;
22743949bee8SArchie Cobbs 		}
22753949bee8SArchie Cobbs 
22763949bee8SArchie Cobbs 		/* How many bytes could have transmitted since last write? */
2277fd58342cSAlexander Motin 		xmitBytes = (alink->conf.bandwidth * 10 * diff.tv_sec)
2278a9b3dca5SArchie Cobbs 		    + (alink->conf.bandwidth * (diff.tv_usec / 1000)) / 100;
2279a9b3dca5SArchie Cobbs 		alink->bytesInQueue -= xmitBytes;
2280a9b3dca5SArchie Cobbs 		if (alink->bytesInQueue < 0)
2281a9b3dca5SArchie Cobbs 			alink->bytesInQueue = 0;
22823949bee8SArchie Cobbs 		else
22833949bee8SArchie Cobbs 			latency[activeLinkNum] +=
2284a9b3dca5SArchie Cobbs 			    (100 * alink->bytesInQueue) / alink->conf.bandwidth;
22853949bee8SArchie Cobbs 	}
22863949bee8SArchie Cobbs 
2287a9b3dca5SArchie Cobbs 	/* Sort active links by latency */
22888250de83SGleb Smirnoff 	qsort_r(sortByLatency,
22898250de83SGleb Smirnoff 	    priv->numActiveLinks, sizeof(*sortByLatency), latency, ng_ppp_intcmp);
22903949bee8SArchie Cobbs 
22913949bee8SArchie Cobbs 	/* Find the interval we need (add links in sortByLatency[] order) */
22923949bee8SArchie Cobbs 	for (numFragments = 1;
22933949bee8SArchie Cobbs 	    numFragments < priv->numActiveLinks; numFragments++) {
22943949bee8SArchie Cobbs 		for (total = i = 0; i < numFragments; i++) {
22953949bee8SArchie Cobbs 			int flowTime;
22963949bee8SArchie Cobbs 
22973949bee8SArchie Cobbs 			flowTime = latency[sortByLatency[numFragments]]
22983949bee8SArchie Cobbs 			    - latency[sortByLatency[i]];
2299a9b3dca5SArchie Cobbs 			total += ((flowTime * priv->links[
2300a9b3dca5SArchie Cobbs 			    priv->activeLinks[sortByLatency[i]]].conf.bandwidth)
23013949bee8SArchie Cobbs 			    	+ 99) / 100;
23023949bee8SArchie Cobbs 		}
23033949bee8SArchie Cobbs 		if (total >= len)
23043949bee8SArchie Cobbs 			break;
23053949bee8SArchie Cobbs 	}
23063949bee8SArchie Cobbs 
23073949bee8SArchie Cobbs 	/* Solve for t_0 in that interval */
23083949bee8SArchie Cobbs 	for (topSum = botSum = i = 0; i < numFragments; i++) {
2309a9b3dca5SArchie Cobbs 		int bw = priv->links[
2310a9b3dca5SArchie Cobbs 		    priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
23113949bee8SArchie Cobbs 
23123949bee8SArchie Cobbs 		topSum += latency[sortByLatency[i]] * bw;	/* / 100 */
23133949bee8SArchie Cobbs 		botSum += bw;					/* / 100 */
23143949bee8SArchie Cobbs 	}
23153949bee8SArchie Cobbs 	t0 = ((len * 100) + topSum + botSum / 2) / botSum;
23163949bee8SArchie Cobbs 
23173949bee8SArchie Cobbs 	/* Compute f_i(t_0) all i */
23183949bee8SArchie Cobbs 	for (total = i = 0; i < numFragments; i++) {
2319a9b3dca5SArchie Cobbs 		int bw = priv->links[
2320a9b3dca5SArchie Cobbs 		    priv->activeLinks[sortByLatency[i]]].conf.bandwidth;
23213949bee8SArchie Cobbs 
23223949bee8SArchie Cobbs 		distrib[sortByLatency[i]] =
23233949bee8SArchie Cobbs 		    (bw * (t0 - latency[sortByLatency[i]]) + 50) / 100;
23243949bee8SArchie Cobbs 		total += distrib[sortByLatency[i]];
23253949bee8SArchie Cobbs 	}
23263949bee8SArchie Cobbs 
2327fb1fc8abSArchie Cobbs 	/* Deal with any rounding error */
2328fb1fc8abSArchie Cobbs 	if (total < len) {
2329a9b3dca5SArchie Cobbs 		struct ng_ppp_link *fastLink =
2330a9b3dca5SArchie Cobbs 		    &priv->links[priv->activeLinks[sortByLatency[0]]];
23313949bee8SArchie Cobbs 		int fast = 0;
23323949bee8SArchie Cobbs 
2333fb1fc8abSArchie Cobbs 		/* Find the fastest link */
23343949bee8SArchie Cobbs 		for (i = 1; i < numFragments; i++) {
2335a9b3dca5SArchie Cobbs 			struct ng_ppp_link *const link =
2336a9b3dca5SArchie Cobbs 			    &priv->links[priv->activeLinks[sortByLatency[i]]];
2337a9b3dca5SArchie Cobbs 
2338a9b3dca5SArchie Cobbs 			if (link->conf.bandwidth > fastLink->conf.bandwidth) {
23393949bee8SArchie Cobbs 				fast = i;
2340a9b3dca5SArchie Cobbs 				fastLink = link;
2341a9b3dca5SArchie Cobbs 			}
23423949bee8SArchie Cobbs 		}
23433949bee8SArchie Cobbs 		distrib[sortByLatency[fast]] += len - total;
2344fb1fc8abSArchie Cobbs 	} else while (total > len) {
2345a9b3dca5SArchie Cobbs 		struct ng_ppp_link *slowLink =
2346a9b3dca5SArchie Cobbs 		    &priv->links[priv->activeLinks[sortByLatency[0]]];
2347fb1fc8abSArchie Cobbs 		int delta, slow = 0;
23483949bee8SArchie Cobbs 
2349fb1fc8abSArchie Cobbs 		/* Find the slowest link that still has bytes to remove */
2350fb1fc8abSArchie Cobbs 		for (i = 1; i < numFragments; i++) {
2351a9b3dca5SArchie Cobbs 			struct ng_ppp_link *const link =
2352a9b3dca5SArchie Cobbs 			    &priv->links[priv->activeLinks[sortByLatency[i]]];
2353a9b3dca5SArchie Cobbs 
2354fb1fc8abSArchie Cobbs 			if (distrib[sortByLatency[slow]] == 0
2355fb1fc8abSArchie Cobbs 			  || (distrib[sortByLatency[i]] > 0
2356a9b3dca5SArchie Cobbs 			    && link->conf.bandwidth <
2357a9b3dca5SArchie Cobbs 			      slowLink->conf.bandwidth)) {
2358fb1fc8abSArchie Cobbs 				slow = i;
2359a9b3dca5SArchie Cobbs 				slowLink = link;
2360a9b3dca5SArchie Cobbs 			}
2361fb1fc8abSArchie Cobbs 		}
2362fb1fc8abSArchie Cobbs 		delta = total - len;
2363fb1fc8abSArchie Cobbs 		if (delta > distrib[sortByLatency[slow]])
2364fb1fc8abSArchie Cobbs 			delta = distrib[sortByLatency[slow]];
2365fb1fc8abSArchie Cobbs 		distrib[sortByLatency[slow]] -= delta;
2366fb1fc8abSArchie Cobbs 		total -= delta;
23673949bee8SArchie Cobbs 	}
23683949bee8SArchie Cobbs }
23693949bee8SArchie Cobbs 
23703949bee8SArchie Cobbs /*
23713949bee8SArchie Cobbs  * Compare two integers
23723949bee8SArchie Cobbs  */
23733949bee8SArchie Cobbs static int
23748250de83SGleb Smirnoff ng_ppp_intcmp(void *latency, const void *v1, const void *v2)
23753949bee8SArchie Cobbs {
23763949bee8SArchie Cobbs 	const int index1 = *((const int *) v1);
23773949bee8SArchie Cobbs 	const int index2 = *((const int *) v2);
23783949bee8SArchie Cobbs 
23798250de83SGleb Smirnoff 	return ((int *)latency)[index1] - ((int *)latency)[index2];
23803949bee8SArchie Cobbs }
23813949bee8SArchie Cobbs 
23823949bee8SArchie Cobbs /*
23833949bee8SArchie Cobbs  * Prepend a possibly compressed PPP protocol number in front of a frame
23843949bee8SArchie Cobbs  */
23853949bee8SArchie Cobbs static struct mbuf *
2386164b576eSGleb Smirnoff ng_ppp_addproto(struct mbuf *m, uint16_t proto, int compOK)
23873949bee8SArchie Cobbs {
2388d690a6e7SArchie Cobbs 	if (compOK && PROT_COMPRESSABLE(proto)) {
2389164b576eSGleb Smirnoff 		uint8_t pbyte = (uint8_t)proto;
23903949bee8SArchie Cobbs 
2391d690a6e7SArchie Cobbs 		return ng_ppp_prepend(m, &pbyte, 1);
2392d690a6e7SArchie Cobbs 	} else {
2393164b576eSGleb Smirnoff 		uint16_t pword = htons((uint16_t)proto);
2394d690a6e7SArchie Cobbs 
2395d690a6e7SArchie Cobbs 		return ng_ppp_prepend(m, &pword, 2);
2396d690a6e7SArchie Cobbs 	}
2397d690a6e7SArchie Cobbs }
2398d690a6e7SArchie Cobbs 
2399d690a6e7SArchie Cobbs /*
2400164b576eSGleb Smirnoff  * Cut a possibly compressed PPP protocol number from the front of a frame.
2401164b576eSGleb Smirnoff  */
2402164b576eSGleb Smirnoff static struct mbuf *
2403164b576eSGleb Smirnoff ng_ppp_cutproto(struct mbuf *m, uint16_t *proto)
2404164b576eSGleb Smirnoff {
2405164b576eSGleb Smirnoff 
2406164b576eSGleb Smirnoff 	*proto = 0;
2407164b576eSGleb Smirnoff 	if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL)
2408164b576eSGleb Smirnoff 		return (NULL);
2409164b576eSGleb Smirnoff 
2410164b576eSGleb Smirnoff 	*proto = *mtod(m, uint8_t *);
2411164b576eSGleb Smirnoff 	m_adj(m, 1);
2412164b576eSGleb Smirnoff 
2413164b576eSGleb Smirnoff 	if (!PROT_VALID(*proto)) {
2414164b576eSGleb Smirnoff 		if (m->m_len < 1 && (m = m_pullup(m, 1)) == NULL)
2415164b576eSGleb Smirnoff 			return (NULL);
2416164b576eSGleb Smirnoff 
2417164b576eSGleb Smirnoff 		*proto = (*proto << 8) + *mtod(m, uint8_t *);
2418164b576eSGleb Smirnoff 		m_adj(m, 1);
2419164b576eSGleb Smirnoff 	}
2420164b576eSGleb Smirnoff 
2421164b576eSGleb Smirnoff 	return (m);
2422164b576eSGleb Smirnoff }
2423164b576eSGleb Smirnoff 
2424164b576eSGleb Smirnoff /*
2425164b576eSGleb Smirnoff  * Prepend some bytes to an mbuf.
2426d690a6e7SArchie Cobbs  */
2427d690a6e7SArchie Cobbs static struct mbuf *
2428d690a6e7SArchie Cobbs ng_ppp_prepend(struct mbuf *m, const void *buf, int len)
2429d690a6e7SArchie Cobbs {
2430a163d034SWarner Losh 	M_PREPEND(m, len, M_DONTWAIT);
2431d690a6e7SArchie Cobbs 	if (m == NULL || (m->m_len < len && (m = m_pullup(m, len)) == NULL))
24324cf49a43SJulian Elischer 		return (NULL);
2433164b576eSGleb Smirnoff 	bcopy(buf, mtod(m, uint8_t *), len);
24343949bee8SArchie Cobbs 	return (m);
24353949bee8SArchie Cobbs }
24363949bee8SArchie Cobbs 
24373949bee8SArchie Cobbs /*
24383949bee8SArchie Cobbs  * Update private information that is derived from other private information
24393949bee8SArchie Cobbs  */
24403949bee8SArchie Cobbs static void
24413949bee8SArchie Cobbs ng_ppp_update(node_p node, int newConf)
24423949bee8SArchie Cobbs {
244330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
24443949bee8SArchie Cobbs 	int i;
24453949bee8SArchie Cobbs 
24463949bee8SArchie Cobbs 	/* Update active status for VJ Compression */
24473949bee8SArchie Cobbs 	priv->vjCompHooked = priv->hooks[HOOK_INDEX_VJC_IP] != NULL
24483949bee8SArchie Cobbs 	    && priv->hooks[HOOK_INDEX_VJC_COMP] != NULL
24493949bee8SArchie Cobbs 	    && priv->hooks[HOOK_INDEX_VJC_UNCOMP] != NULL
24503949bee8SArchie Cobbs 	    && priv->hooks[HOOK_INDEX_VJC_VJIP] != NULL;
24513949bee8SArchie Cobbs 
24523949bee8SArchie Cobbs 	/* Increase latency for each link an amount equal to one MP header */
24533949bee8SArchie Cobbs 	if (newConf) {
24543949bee8SArchie Cobbs 		for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
24553949bee8SArchie Cobbs 			int hdrBytes;
24563949bee8SArchie Cobbs 
2457fd58342cSAlexander Motin 			if (priv->links[i].conf.bandwidth == 0)
2458fd58342cSAlexander Motin 			    continue;
2459fd58342cSAlexander Motin 
2460fd58342cSAlexander Motin 			hdrBytes = MP_AVERAGE_LINK_OVERHEAD
2461fd58342cSAlexander Motin 			    + (priv->links[i].conf.enableACFComp ? 0 : 2)
2462a9b3dca5SArchie Cobbs 			    + (priv->links[i].conf.enableProtoComp ? 1 : 2)
24633949bee8SArchie Cobbs 			    + (priv->conf.xmitShortSeq ? 2 : 4);
246449728ffcSBjoern A. Zeeb 			priv->links[i].latency =
246549728ffcSBjoern A. Zeeb 			    priv->links[i].conf.latency +
2466fd58342cSAlexander Motin 			    (hdrBytes / priv->links[i].conf.bandwidth + 50) / 100;
24673949bee8SArchie Cobbs 		}
24683949bee8SArchie Cobbs 	}
24693949bee8SArchie Cobbs 
24703949bee8SArchie Cobbs 	/* Update list of active links */
24713949bee8SArchie Cobbs 	bzero(&priv->activeLinks, sizeof(priv->activeLinks));
24723949bee8SArchie Cobbs 	priv->numActiveLinks = 0;
24733949bee8SArchie Cobbs 	priv->allLinksEqual = 1;
24743949bee8SArchie Cobbs 	for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
2475a9b3dca5SArchie Cobbs 		struct ng_ppp_link *const link = &priv->links[i];
24760e11d0f3SArchie Cobbs 
2477a9b3dca5SArchie Cobbs 		/* Is link active? */
2478a9b3dca5SArchie Cobbs 		if (link->conf.enableLink && link->hook != NULL) {
2479a9b3dca5SArchie Cobbs 			struct ng_ppp_link *link0;
2480a9b3dca5SArchie Cobbs 
2481a9b3dca5SArchie Cobbs 			/* Add link to list of active links */
24823949bee8SArchie Cobbs 			priv->activeLinks[priv->numActiveLinks++] = i;
2483a9b3dca5SArchie Cobbs 			link0 = &priv->links[priv->activeLinks[0]];
2484a9b3dca5SArchie Cobbs 
2485a9b3dca5SArchie Cobbs 			/* Determine if all links are still equal */
248649728ffcSBjoern A. Zeeb 			if (link->latency != link0->latency
2487a9b3dca5SArchie Cobbs 			  || link->conf.bandwidth != link0->conf.bandwidth)
24883949bee8SArchie Cobbs 				priv->allLinksEqual = 0;
2489a9b3dca5SArchie Cobbs 
2490a9b3dca5SArchie Cobbs 			/* Initialize rec'd sequence number */
2491a9b3dca5SArchie Cobbs 			if (link->seq == MP_NOSEQ) {
2492a9b3dca5SArchie Cobbs 				link->seq = (link == link0) ?
2493a9b3dca5SArchie Cobbs 				    MP_INITIAL_SEQ : link0->seq;
24943949bee8SArchie Cobbs 			}
2495a9b3dca5SArchie Cobbs 		} else
2496a9b3dca5SArchie Cobbs 			link->seq = MP_NOSEQ;
24973949bee8SArchie Cobbs 	}
24983949bee8SArchie Cobbs 
2499a9b3dca5SArchie Cobbs 	/* Update MP state as multi-link is active or not */
2500a9b3dca5SArchie Cobbs 	if (priv->conf.enableMultilink && priv->numActiveLinks > 0)
2501a9b3dca5SArchie Cobbs 		ng_ppp_start_frag_timer(node);
2502a9b3dca5SArchie Cobbs 	else {
2503a9b3dca5SArchie Cobbs 		ng_ppp_stop_frag_timer(node);
2504a9b3dca5SArchie Cobbs 		ng_ppp_frag_reset(node);
2505a9b3dca5SArchie Cobbs 		priv->xseq = MP_INITIAL_SEQ;
2506a9b3dca5SArchie Cobbs 		priv->mseq = MP_INITIAL_SEQ;
2507a9b3dca5SArchie Cobbs 		for (i = 0; i < NG_PPP_MAX_LINKS; i++) {
2508a9b3dca5SArchie Cobbs 			struct ng_ppp_link *const link = &priv->links[i];
2509a9b3dca5SArchie Cobbs 
2510a9b3dca5SArchie Cobbs 			bzero(&link->lastWrite, sizeof(link->lastWrite));
2511a9b3dca5SArchie Cobbs 			link->bytesInQueue = 0;
2512a9b3dca5SArchie Cobbs 			link->seq = MP_NOSEQ;
2513a9b3dca5SArchie Cobbs 		}
25143949bee8SArchie Cobbs 	}
25153949bee8SArchie Cobbs }
25163949bee8SArchie Cobbs 
25173949bee8SArchie Cobbs /*
25183949bee8SArchie Cobbs  * Determine if a new configuration would represent a valid change
25193949bee8SArchie Cobbs  * from the current configuration and link activity status.
25203949bee8SArchie Cobbs  */
25213949bee8SArchie Cobbs static int
2522a9b3dca5SArchie Cobbs ng_ppp_config_valid(node_p node, const struct ng_ppp_node_conf *newConf)
25233949bee8SArchie Cobbs {
252430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
25253949bee8SArchie Cobbs 	int i, newNumLinksActive;
25263949bee8SArchie Cobbs 
25273949bee8SArchie Cobbs 	/* Check per-link config and count how many links would be active */
25283949bee8SArchie Cobbs 	for (newNumLinksActive = i = 0; i < NG_PPP_MAX_LINKS; i++) {
2529a9b3dca5SArchie Cobbs 		if (newConf->links[i].enableLink && priv->links[i].hook != NULL)
2530e149c4e2SArchie Cobbs 			newNumLinksActive++;
2531e149c4e2SArchie Cobbs 		if (!newConf->links[i].enableLink)
2532e149c4e2SArchie Cobbs 			continue;
25333949bee8SArchie Cobbs 		if (newConf->links[i].mru < MP_MIN_LINK_MRU)
25343949bee8SArchie Cobbs 			return (0);
25353949bee8SArchie Cobbs 		if (newConf->links[i].bandwidth == 0)
25363949bee8SArchie Cobbs 			return (0);
25373949bee8SArchie Cobbs 		if (newConf->links[i].bandwidth > NG_PPP_MAX_BANDWIDTH)
25383949bee8SArchie Cobbs 			return (0);
25393949bee8SArchie Cobbs 		if (newConf->links[i].latency > NG_PPP_MAX_LATENCY)
25403949bee8SArchie Cobbs 			return (0);
25413949bee8SArchie Cobbs 	}
25423949bee8SArchie Cobbs 
25433949bee8SArchie Cobbs 	/* Check bundle parameters */
2544a9b3dca5SArchie Cobbs 	if (newConf->bund.enableMultilink && newConf->bund.mrru < MP_MIN_MRRU)
25453949bee8SArchie Cobbs 		return (0);
25463949bee8SArchie Cobbs 
25473949bee8SArchie Cobbs 	/* Disallow changes to multi-link configuration while MP is active */
25483949bee8SArchie Cobbs 	if (priv->numActiveLinks > 0 && newNumLinksActive > 0) {
2549a9b3dca5SArchie Cobbs 		if (!priv->conf.enableMultilink
2550a9b3dca5SArchie Cobbs 				!= !newConf->bund.enableMultilink
2551a9b3dca5SArchie Cobbs 		    || !priv->conf.xmitShortSeq != !newConf->bund.xmitShortSeq
2552a9b3dca5SArchie Cobbs 		    || !priv->conf.recvShortSeq != !newConf->bund.recvShortSeq)
25533949bee8SArchie Cobbs 			return (0);
25543949bee8SArchie Cobbs 	}
25553949bee8SArchie Cobbs 
2556e149c4e2SArchie Cobbs 	/* At most one link can be active unless multi-link is enabled */
2557a9b3dca5SArchie Cobbs 	if (!newConf->bund.enableMultilink && newNumLinksActive > 1)
2558e149c4e2SArchie Cobbs 		return (0);
2559e149c4e2SArchie Cobbs 
2560e149c4e2SArchie Cobbs 	/* Configuration change would be valid */
25613949bee8SArchie Cobbs 	return (1);
25623949bee8SArchie Cobbs }
25633949bee8SArchie Cobbs 
25643949bee8SArchie Cobbs /*
25653949bee8SArchie Cobbs  * Free all entries in the fragment queue
25663949bee8SArchie Cobbs  */
25673949bee8SArchie Cobbs static void
2568a9b3dca5SArchie Cobbs ng_ppp_frag_reset(node_p node)
25693949bee8SArchie Cobbs {
257030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
25711e7a9f72SArchie Cobbs 	struct ng_ppp_frag *qent, *qnext;
25723949bee8SArchie Cobbs 
25735f90cac7SKirk McKusick 	for (qent = TAILQ_FIRST(&priv->frags); qent; qent = qnext) {
25745f90cac7SKirk McKusick 		qnext = TAILQ_NEXT(qent, f_qent);
2575069154d5SJulian Elischer 		NG_FREE_M(qent->data);
257693caf2e2SAlexander Motin 		TAILQ_INSERT_HEAD(&priv->fragsfree, qent, f_qent);
25773949bee8SArchie Cobbs 	}
25785f90cac7SKirk McKusick 	TAILQ_INIT(&priv->frags);
2579a9b3dca5SArchie Cobbs }
2580a9b3dca5SArchie Cobbs 
2581a9b3dca5SArchie Cobbs /*
2582a9b3dca5SArchie Cobbs  * Start fragment queue timer
2583a9b3dca5SArchie Cobbs  */
2584a9b3dca5SArchie Cobbs static void
2585a9b3dca5SArchie Cobbs ng_ppp_start_frag_timer(node_p node)
2586a9b3dca5SArchie Cobbs {
258730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2588a9b3dca5SArchie Cobbs 
2589dc2f4d7fSGleb Smirnoff 	if (!(callout_pending(&priv->fragTimer)))
25909d952bd3SGleb Smirnoff 		ng_callout(&priv->fragTimer, node, NULL, MP_FRAGTIMER_INTERVAL,
25919d952bd3SGleb Smirnoff 		    ng_ppp_frag_timeout, NULL, 0);
2592a9b3dca5SArchie Cobbs }
2593a9b3dca5SArchie Cobbs 
2594a9b3dca5SArchie Cobbs /*
2595a9b3dca5SArchie Cobbs  * Stop fragment queue timer
2596a9b3dca5SArchie Cobbs  */
2597a9b3dca5SArchie Cobbs static void
2598a9b3dca5SArchie Cobbs ng_ppp_stop_frag_timer(node_p node)
2599a9b3dca5SArchie Cobbs {
260030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
2601a9b3dca5SArchie Cobbs 
2602dc2f4d7fSGleb Smirnoff 	if (callout_pending(&priv->fragTimer))
26039d952bd3SGleb Smirnoff 		ng_uncallout(&priv->fragTimer, node);
2604a9b3dca5SArchie Cobbs }
2605